void ModelViewer::RenderObjects( GraphicsContext& gfxContext, const Matrix4& ViewProjMat ) { struct VSConstants { Matrix4 modelToProjection; Matrix4 modelToShadow; XMFLOAT3 viewerPos; } vsConstants; vsConstants.modelToProjection = ViewProjMat; vsConstants.modelToShadow = m_SunShadow.GetShadowMatrix(); XMStoreFloat3(&vsConstants.viewerPos, m_Camera.GetPosition()); gfxContext.SetDynamicConstantBufferView(0, sizeof(vsConstants), &vsConstants); uint32_t materialIdx = 0xFFFFFFFFul; uint32_t VertexStride = m_Model.m_VertexStride; for (unsigned int meshIndex = 0; meshIndex < m_Model.m_Header.meshCount; meshIndex++) { const Model::Mesh& mesh = m_Model.m_pMesh[meshIndex]; uint32_t indexCount = mesh.indexCount; uint32_t startIndex = mesh.indexDataByteOffset / sizeof(uint16_t); uint32_t baseVertex = mesh.vertexDataByteOffset / VertexStride; if (mesh.materialIndex != materialIdx) { materialIdx = mesh.materialIndex; gfxContext.SetDynamicDescriptors(3, 0, 6, m_Model.GetSRVs(materialIdx) ); } #if USE_VERTEX_BUFFER gfxContext.DrawIndexed(indexCount, startIndex, baseVertex); #else gfxContext.SetConstants(5, baseVertex); gfxContext.DrawIndexed(indexCount, startIndex); #endif } }
void ModelViewer::RenderScene( void ) { GraphicsContext& gfxContext = GraphicsContext::Begin(L"Scene Render"); ParticleEffects::Update(gfxContext.GetComputeContext(), Graphics::GetFrameTime()); __declspec(align(16)) struct { Vector3 sunDirection; Vector3 sunLight; Vector3 ambientLight; float ShadowTexelSize; } psConstants; psConstants.sunDirection = m_SunDirection; psConstants.sunLight = Vector3(1.0f, 1.0f, 1.0f) * m_SunLightIntensity; psConstants.ambientLight = Vector3(0.2f, 0.2f, 0.2f); psConstants.ShadowTexelSize = 1.0f / g_ShadowBuffer.GetWidth(); { ScopedTimer _prof(L"Z PrePass", gfxContext); gfxContext.ClearDepth(g_SceneDepthBuffer); gfxContext.SetRootSignature(m_RootSig); gfxContext.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); gfxContext.SetIndexBuffer(m_Model.m_IndexBuffer.IndexBufferView()); #if USE_VERTEX_BUFFER gfxContext.SetVertexBuffer(0, m_Model.m_VertexBuffer.VertexBufferView()); #elif USE_ROOT_BUFFER_SRV gfxContext.SetBufferSRV(2, m_Model.m_VertexBuffer); #else gfxContext.SetDynamicDescriptor(2, 0, m_Model.m_VertexBuffer.GetSRV()); #endif gfxContext.SetDynamicConstantBufferView(1, sizeof(psConstants), &psConstants); gfxContext.SetPipelineState(m_DepthPSO); gfxContext.SetDepthStencilTarget(g_SceneDepthBuffer); gfxContext.SetViewportAndScissor(m_MainViewport, m_MainScissor); RenderObjects( gfxContext, m_ViewProjMatrix ); } SSAO::Render(gfxContext, m_Camera); if (!SSAO::DebugDraw) { ScopedTimer _prof(L"Main Render", gfxContext); gfxContext.ClearColor(g_SceneColorBuffer); gfxContext.SetRootSignature(m_RootSig); gfxContext.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); gfxContext.SetIndexBuffer(m_Model.m_IndexBuffer.IndexBufferView()); #if USE_VERTEX_BUFFER gfxContext.SetVertexBuffer(0, m_Model.m_VertexBuffer.VertexBufferView()); #elif USE_ROOT_BUFFER_SRV gfxContext.SetBufferSRV(2, m_Model.m_VertexBuffer); #else gfxContext.SetDynamicDescriptor(2, 0, m_Model.m_VertexBuffer.GetSRV()); #endif gfxContext.SetDynamicDescriptors(4, 0, 2, m_ExtraTextures); gfxContext.SetDynamicConstantBufferView(1, sizeof(psConstants), &psConstants); { ScopedTimer _prof(L"Render Shadow Map", gfxContext); m_SunShadow.UpdateMatrix( -m_SunDirection, Vector3(0, -500.0f, 0), Vector3(ShadowDimX, ShadowDimY, ShadowDimZ), (uint32_t)g_ShadowBuffer.GetWidth(), (uint32_t)g_ShadowBuffer.GetHeight(), 16 ); gfxContext.SetPipelineState(m_ShadowPSO); g_ShadowBuffer.BeginRendering(gfxContext); RenderObjects( gfxContext, m_SunShadow.GetViewProjMatrix() ); g_ShadowBuffer.EndRendering(gfxContext); } { ScopedTimer _prof(L"Render Color", gfxContext); gfxContext.SetPipelineState(m_ModelPSO); gfxContext.SetRenderTarget(g_SceneColorBuffer, g_SceneDepthBuffer, true); gfxContext.SetViewportAndScissor(m_MainViewport, m_MainScissor); RenderObjects( gfxContext, m_ViewProjMatrix ); } } ParticleEffects::Render(gfxContext, m_Camera, g_SceneColorBuffer, g_SceneDepthBuffer, g_LinearDepth); MotionBlur::RenderCameraBlur(gfxContext, m_Camera); gfxContext.CloseAndExecute(); }
void ModelViewer::RenderScene( void ) { GraphicsContext& gfxContext = GraphicsContext::Begin(L"Scene Render"); ParticleEffects::Update(gfxContext.GetComputeContext(), Graphics::GetFrameTime()); __declspec(align(16)) struct { Vector3 sunDirection; Vector3 sunLight; Vector3 ambientLight; float ShadowTexelSize; } psConstants; psConstants.sunDirection = m_SunDirection; psConstants.sunLight = Vector3(1.0f, 1.0f, 1.0f) * m_SunLightIntensity; psConstants.ambientLight = Vector3(1.0f, 1.0f, 1.0f) * m_AmbientIntensity; psConstants.ShadowTexelSize = 1.0f / g_ShadowBuffer.GetWidth(); { ScopedTimer _prof(L"Z PrePass", gfxContext); gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_WRITE, true); gfxContext.ClearDepth(g_SceneDepthBuffer); gfxContext.SetRootSignature(m_RootSig); gfxContext.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); gfxContext.SetIndexBuffer(m_Model.m_IndexBuffer.IndexBufferView()); #if USE_VERTEX_BUFFER gfxContext.SetVertexBuffer(0, m_Model.m_VertexBuffer.VertexBufferView()); #elif USE_ROOT_BUFFER_SRV gfxContext.SetBufferSRV(2, m_Model.m_VertexBuffer); #else gfxContext.SetDynamicDescriptor(2, 0, m_Model.m_VertexBuffer.GetSRV()); #endif gfxContext.SetDynamicConstantBufferView(1, sizeof(psConstants), &psConstants); gfxContext.SetPipelineState(m_DepthPSO); gfxContext.SetDepthStencilTarget(g_SceneDepthBuffer.GetDSV()); gfxContext.SetViewportAndScissor(m_MainViewport, m_MainScissor); RenderObjects(gfxContext, m_ViewProjMatrix ); } SSAO::Render(gfxContext, m_Camera); if (!SSAO::DebugDraw) { ScopedTimer _prof(L"Main Render", gfxContext); gfxContext.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, true); gfxContext.ClearColor(g_SceneColorBuffer); // Set the default state for command lists auto& pfnSetupGraphicsState = [&](void) { gfxContext.SetRootSignature(m_RootSig); gfxContext.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); gfxContext.SetIndexBuffer(m_Model.m_IndexBuffer.IndexBufferView()); #if USE_VERTEX_BUFFER gfxContext.SetVertexBuffer(0, m_Model.m_VertexBuffer.VertexBufferView()); #elif USE_ROOT_BUFFER_SRV gfxContext.SetBufferSRV(2, m_Model.m_VertexBuffer); #else gfxContext.SetDynamicDescriptor(2, 0, m_Model.m_VertexBuffer.GetSRV()); #endif gfxContext.SetDynamicDescriptors(4, 0, 2, m_ExtraTextures); gfxContext.SetDynamicConstantBufferView(1, sizeof(psConstants), &psConstants); }; pfnSetupGraphicsState(); { ScopedTimer _prof(L"Render Shadow Map", gfxContext); m_SunShadow.UpdateMatrix(-m_SunDirection, Vector3(0, -500.0f, 0), Vector3(ShadowDimX, ShadowDimY, ShadowDimZ), (uint32_t)g_ShadowBuffer.GetWidth(), (uint32_t)g_ShadowBuffer.GetHeight(), 16); gfxContext.SetPipelineState(m_ShadowPSO); g_ShadowBuffer.BeginRendering(gfxContext); RenderObjects(gfxContext, m_SunShadow.GetViewProjMatrix()); g_ShadowBuffer.EndRendering(gfxContext); } if (SSAO::AsyncCompute) { gfxContext.Flush(); pfnSetupGraphicsState(); // Make the 3D queue wait for the Compute queue to finish SSAO g_CommandManager.GetGraphicsQueue().StallForProducer(g_CommandManager.GetComputeQueue()); } { ScopedTimer _prof(L"Render Color", gfxContext); gfxContext.SetPipelineState(m_ModelPSO); gfxContext.TransitionResource(g_SSAOFullScreen, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); gfxContext.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET); gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_READ); gfxContext.SetRenderTarget(g_SceneColorBuffer.GetRTV(), g_SceneDepthBuffer.GetDSV_DepthReadOnly()); gfxContext.SetViewportAndScissor(m_MainViewport, m_MainScissor); RenderObjects( gfxContext, m_ViewProjMatrix ); } } ParticleEffects::Render(gfxContext, m_Camera, g_SceneColorBuffer, g_SceneDepthBuffer, g_LinearDepth); // Until I work out how to couple these two, it's "either-or". if (DepthOfField::Enable) DepthOfField::Render(gfxContext, m_Camera.GetNearClip(), m_Camera.GetFarClip()); else MotionBlur::RenderCameraBlur(gfxContext, m_Camera); gfxContext.Finish(); }