Skip to content

Commit

Permalink
View additions
Browse files Browse the repository at this point in the history
Added lod camera to view
Also added additional permutation vars that can be set on the view. This is used to set a permutation var for the material preview in the editor that can be used by shaders to deactivate parts that need additional data which is not set in the material preview.
  • Loading branch information
C-Core authored Jan 29, 2021
1 parent 594d034 commit 1a744d8
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ezViewHandle ezMaterialViewContext::CreateView()
ezRenderWorld::CreateView("Material Editor - View", pView);

pView->SetRenderPipelineResource(CreateDefaultRenderPipeline());
pView->SetShaderPermutationVariable("MATERIAL_PREVIEW", "TRUE");

ezEngineProcessDocumentContext* pDocumentContext = GetDocumentContext();
pView->SetWorld(pDocumentContext->GetWorld());
Expand Down
4 changes: 2 additions & 2 deletions Code/Engine/Core/World/Implementation/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ void ezGameObject::SendEventMessage(ezEventMessage& msg, const ezComponent* pSen

void ezGameObject::SendEventMessage(ezEventMessage& msg, const ezComponent* pSenderComponent) const
{
if (const ezComponent* pReceiver = GetWorld()->FindEventMsgHandler(msg, const_cast<ezGameObject*>(this)))
if (const ezComponent* pReceiver = GetWorld()->FindEventMsgHandler(msg, this))
{
if (pSenderComponent)
{
Expand All @@ -908,7 +908,7 @@ void ezGameObject::SendEventMessage(ezEventMessage& msg, const ezComponent* pSen
void ezGameObject::PostEventMessage(
ezEventMessage& msg, const ezComponent* pSenderComponent, ezTime delay, ezObjectMsgQueueType::Enum queueType) const
{
if (const ezComponent* pReceiver = GetWorld()->FindEventMsgHandler(msg, const_cast<ezGameObject*>(this)))
if (const ezComponent* pReceiver = GetWorld()->FindEventMsgHandler(msg, this))
{
if (pSenderComponent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ ezUInt32 ezShadowPool::AddDirectionalLight(const ezDirectionalLightComponent* pD
{
pView->SetName(viewNames[i]);
pView->SetWorld(const_cast<ezWorld*>(pDirLight->GetWorld()));
pView->SetLodCamera(pReferenceCamera);
}

// Setup camera
Expand Down
9 changes: 4 additions & 5 deletions Code/Engine/RendererCore/Pipeline/ExtractedRenderData.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ class EZ_RENDERERCORE_DLL ezExtractedRenderData
ezExtractedRenderData();

EZ_ALWAYS_INLINE void SetCamera(const ezCamera& camera) { m_Camera = camera; }

EZ_ALWAYS_INLINE const ezCamera& GetCamera() const { return m_Camera; }

EZ_ALWAYS_INLINE void SetViewData(const ezViewData& viewData) { m_ViewData = viewData; }
EZ_ALWAYS_INLINE void SetLodCamera(const ezCamera& camera) { m_LodCamera = camera; }
EZ_ALWAYS_INLINE const ezCamera& GetLodCamera() const { return m_LodCamera; }

EZ_ALWAYS_INLINE void SetViewData(const ezViewData& viewData) { m_ViewData = viewData; }
EZ_ALWAYS_INLINE const ezViewData& GetViewData() const { return m_ViewData; }

EZ_ALWAYS_INLINE void SetWorldTime(ezTime time) { m_WorldTime = time; }

EZ_ALWAYS_INLINE ezTime GetWorldTime() const { return m_WorldTime; }

EZ_ALWAYS_INLINE void SetWorldDebugContext(const ezDebugRendererContext& debugContext) { m_WorldDebugContext = debugContext; }

EZ_ALWAYS_INLINE const ezDebugRendererContext& GetWorldDebugContext() const { return m_WorldDebugContext; }

EZ_ALWAYS_INLINE void SetViewDebugContext(const ezDebugRendererContext& debugContext) { m_ViewDebugContext = debugContext; }

EZ_ALWAYS_INLINE const ezDebugRendererContext& GetViewDebugContext() const { return m_ViewDebugContext; }

void AddRenderData(const ezRenderData* pRenderData, ezRenderData::Category category);
Expand Down Expand Up @@ -57,6 +55,7 @@ class EZ_RENDERERCORE_DLL ezExtractedRenderData
};

ezCamera m_Camera;
ezCamera m_LodCamera; // Temporary until we have a real LOD system
ezViewData m_ViewData;
ezTime m_WorldTime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ void ezRenderPipeline::ExtractData(const ezView& view)

// Store camera and viewdata
data.SetCamera(*view.GetCamera());
data.SetLodCamera(*view.GetLodCamera());
data.SetViewData(view.GetData());
data.SetWorldTime(view.GetWorld()->GetClock().GetAccumulatedTime());
data.SetWorldDebugContext(view.GetWorld());
Expand Down Expand Up @@ -1022,6 +1023,12 @@ void ezRenderPipeline::Render(ezRenderContext* pRenderContext)
else
pRenderContext->SetShaderPermutationVariable(sCameraMode, sPerspective);

// Also set pipeline specific permutation vars
for (auto& var : m_PermutationVars)
{
pRenderContext->SetShaderPermutationVariable(var.m_sName, var.m_sValue);
}

ezRenderWorldRenderEvent renderEvent;
renderEvent.m_Type = ezRenderWorldRenderEvent::Type::BeforePipelineExecution;
renderEvent.m_pPipeline = this;
Expand Down Expand Up @@ -1103,6 +1110,8 @@ void ezRenderPipeline::Render(ezRenderContext* pRenderContext)
renderEvent.m_Type = ezRenderWorldRenderEvent::Type::AfterPipelineExecution;
ezRenderWorld::s_RenderEvent.Broadcast(renderEvent);

pRenderContext->ResetContextState();

data.Clear();

m_CurrentRenderThread = (ezThreadID)0;
Expand Down
46 changes: 35 additions & 11 deletions Code/Engine/RendererCore/Pipeline/Implementation/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,9 @@ EZ_END_DYNAMIC_REFLECTED_TYPE;
ezView::ezView()
{
m_pExtractTask = EZ_DEFAULT_NEW(ezDelegateTask<void>, "", ezMakeDelegate(&ezView::ExtractData, this));

m_pWorld = nullptr;
m_pCamera = nullptr;
m_pCullingCamera = nullptr;

m_uiLastCameraSettingsModification = 0;
m_uiLastCameraOrientationModification = 0;
m_fLastViewportAspectRatio = 1.0f;

m_uiRenderPipelineResourceDescriptionCounter = 0;
}

ezView::~ezView() {}
ezView::~ezView() = default;

void ezView::SetName(const char* szName)
{
Expand Down Expand Up @@ -150,6 +140,30 @@ void ezView::ComputeCullingFrustum(ezFrustum& out_Frustum) const
out_Frustum.SetFrustum(projectionMatrix * viewMatrix);
}

void ezView::SetShaderPermutationVariable(const char* szName, const char* szValue)
{
ezHashedString sName;
sName.Assign(szName);

for (auto& var : m_PermutationVars)
{
if (var.m_sName == sName)
{
if (var.m_sValue != szValue)
{
var.m_sValue.Assign(szValue);
m_bPermutationVarsDirty = true;
}
return;
}
}

auto& var = m_PermutationVars.ExpandAndGetRef();
var.m_sName = sName;
var.m_sValue.Assign(szValue);
m_bPermutationVarsDirty = true;
}

void ezView::SetRenderPassProperty(const char* szPassName, const char* szPropertyName, const ezVariant& value)
{
SetProperty(m_PassProperties, szPassName, szPropertyName, value);
Expand Down Expand Up @@ -258,11 +272,21 @@ void ezView::EnsureUpToDate()
ResetAllPropertyStates(m_ExtractorProperties);
}

ApplyPermutationVars();
ApplyRenderPassProperties();
ApplyExtractorProperties();
}
}

void ezView::ApplyPermutationVars()
{
if (!m_bPermutationVarsDirty)
return;

m_pRenderPipeline->m_PermutationVars = m_PermutationVars;
m_bPermutationVarsDirty = false;
}

void ezView::SetProperty(ezMap<ezString, PropertyValue>& map, const char* szPassName, const char* szPropertyName, const ezVariant& value)
{
ezStringBuilder sKey(szPassName, "::", szPropertyName);
Expand Down
13 changes: 9 additions & 4 deletions Code/Engine/RendererCore/Pipeline/Implementation/View_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@ EZ_ALWAYS_INLINE const ezCamera* ezView::GetCamera() const
return m_pCamera;
}

EZ_ALWAYS_INLINE void ezView::SetCullingCamera(ezCamera* pCamera)
EZ_ALWAYS_INLINE void ezView::SetCullingCamera(const ezCamera* pCamera)
{
m_pCullingCamera = pCamera;
}

EZ_ALWAYS_INLINE ezCamera* ezView::GetCullingCamera()
EZ_ALWAYS_INLINE const ezCamera* ezView::GetCullingCamera() const
{
return m_pCullingCamera != nullptr ? m_pCullingCamera : m_pCamera;
}

EZ_ALWAYS_INLINE const ezCamera* ezView::GetCullingCamera() const
EZ_ALWAYS_INLINE void ezView::SetLodCamera(const ezCamera* pCamera)
{
return m_pCullingCamera != nullptr ? m_pCullingCamera : m_pCamera;
m_pLodCamera = pCamera;
}

EZ_ALWAYS_INLINE const ezCamera* ezView::GetLodCamera() const
{
return m_pLodCamera != nullptr ? m_pLodCamera : m_pCamera;
}

EZ_ALWAYS_INLINE ezEnum<ezCameraUsageHint> ezView::GetCameraUsageHint() const
Expand Down
3 changes: 3 additions & 0 deletions Code/Engine/RendererCore/Pipeline/RenderPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ezProfilingId;
class ezView;
class ezRenderPipelinePass;
class ezFrameDataProviderBase;
struct ezPermutationVar;

class EZ_RENDERERCORE_DLL ezRenderPipeline : public ezRefCounted
{
Expand Down Expand Up @@ -136,4 +137,6 @@ class EZ_RENDERERCORE_DLL ezRenderPipeline : public ezRefCounted
// Data Providers
mutable ezDynamicArray<ezUniquePtr<ezFrameDataProviderBase>> m_DataProviders;
mutable ezHashTable<const ezRTTI*, ezUInt32> m_TypeToDataProviderIndex;

ezDynamicArray<ezPermutationVar> m_PermutationVars;
};
30 changes: 20 additions & 10 deletions Code/Engine/RendererCore/Pipeline/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ class EZ_RENDERERCORE_DLL ezView : public ezRenderPipelineNode
ezCamera* GetCamera();
const ezCamera* GetCamera() const;

void SetCullingCamera(ezCamera* pCamera);
ezCamera* GetCullingCamera();
void SetCullingCamera(const ezCamera* pCamera);
const ezCamera* GetCullingCamera() const;

void SetLodCamera(const ezCamera* pCamera);
const ezCamera* GetLodCamera() const;

/// \brief Returns the camera usage hint for the view.
ezEnum<ezCameraUsageHint> GetCameraUsageHint() const;
/// \brief Sets the camera usage hint for the view. If not 'None', the camera component of the same usage will be auto-connected
Expand Down Expand Up @@ -102,6 +104,8 @@ class EZ_RENDERERCORE_DLL ezView : public ezRenderPipelineNode
/// \brief Returns the frustum that should be used for determine visible objects for this view.
void ComputeCullingFrustum(ezFrustum& out_Frustum) const;

void SetShaderPermutationVariable(const char* szName, const char* szValue);

void SetRenderPassProperty(const char* szPassName, const char* szPropertyName, const ezVariant& value);
void SetExtractorProperty(const char* szPassName, const char* szPropertyName, const ezVariant& value);

Expand All @@ -127,14 +131,15 @@ class EZ_RENDERERCORE_DLL ezView : public ezRenderPipelineNode

ezSharedPtr<ezTask> m_pExtractTask;

ezWorld* m_pWorld;
ezWorld* m_pWorld = nullptr;

ezGALRenderTargetSetup m_RenderTargetSetup;
ezRenderPipelineResourceHandle m_hRenderPipeline;
ezUInt32 m_uiRenderPipelineResourceDescriptionCounter;
ezUInt32 m_uiRenderPipelineResourceDescriptionCounter = 0;
ezSharedPtr<ezRenderPipeline> m_pRenderPipeline;
ezCamera* m_pCamera;
ezCamera* m_pCullingCamera;
ezCamera* m_pCamera = nullptr;
const ezCamera* m_pCullingCamera = nullptr;
const ezCamera* m_pLodCamera = nullptr;

private:
ezRenderPipelineNodeInputPin m_PinRenderTarget0;
Expand All @@ -149,13 +154,18 @@ class EZ_RENDERERCORE_DLL ezView : public ezRenderPipelineNode
/// \brief Rebuilds pipeline if necessary and pushes double-buffered settings into the pipeline.
void EnsureUpToDate();

mutable ezUInt32 m_uiLastCameraSettingsModification;
mutable ezUInt32 m_uiLastCameraOrientationModification;
mutable float m_fLastViewportAspectRatio;
mutable ezUInt32 m_uiLastCameraSettingsModification = 0;
mutable ezUInt32 m_uiLastCameraOrientationModification = 0;
mutable float m_fLastViewportAspectRatio = 1.0f;

mutable ezViewData m_Data;

ezInternal::RenderDataCache* m_pRenderDataCache;
ezInternal::RenderDataCache* m_pRenderDataCache = nullptr;

ezDynamicArray<ezPermutationVar> m_PermutationVars;
bool m_bPermutationVarsDirty = false;

void ApplyPermutationVars();

struct PropertyValue
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,11 @@ ezRenderContext::ezRenderContext()

m_hGlobalConstantBufferStorage = CreateConstantBufferStorage<ezGlobalConstants>();

ezRenderWorld::GetRenderEvent().AddEventHandler(ezMakeDelegate(&ezRenderContext::OnRenderEvent, this));

ResetContextState();
}

ezRenderContext::~ezRenderContext()
{
ezRenderWorld::GetRenderEvent().RemoveEventHandler(ezMakeDelegate(&ezRenderContext::OnRenderEvent, this));

DeleteConstantBufferStorage(m_hGlobalConstantBufferStorage);

if (s_DefaultInstance == this)
Expand Down Expand Up @@ -704,6 +700,7 @@ void ezRenderContext::ResetContextState()
m_hActiveShader.Invalidate();
m_hActiveGALShader.Invalidate();

m_PermutationVariables.Clear();
m_hNewMaterial.Invalidate();
m_hMaterial.Invalidate();

Expand Down Expand Up @@ -882,14 +879,6 @@ void ezRenderContext::OnEngineShutdown()
}
}

void ezRenderContext::OnRenderEvent(const ezRenderWorldRenderEvent& e)
{
if (e.m_Type == ezRenderWorldRenderEvent::Type::EndRender)
{
ResetContextState();
}
}

// static
ezResult ezRenderContext::BuildVertexDeclaration(ezGALShaderHandle hShader, const ezVertexDeclarationInfo& decl, ezGALVertexDeclarationHandle& out_Declaration)
{
Expand Down
2 changes: 0 additions & 2 deletions Code/Engine/RendererCore/RenderContext/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,6 @@ class EZ_RENDERERCORE_DLL ezRenderContext

static void OnEngineShutdown();

void OnRenderEvent(const ezRenderWorldRenderEvent& e);

private:
Statistics m_Statistics;
ezBitflags<ezRenderContextFlags> m_StateFlags;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bool MATERIAL_PREVIEW

0 comments on commit 1a744d8

Please sign in to comment.