Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
Reduce state changes. There are only 3 possible "materials" in this s…
Browse files Browse the repository at this point in the history
…ample: sky, planet, and planet with sampler feedback.
  • Loading branch information
allenhux-intel committed Dec 9, 2022
1 parent c378b3a commit 88e0426
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 59 deletions.
60 changes: 30 additions & 30 deletions src/Expanse_vs2022.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{b85f76f3-fe1c-4329-8013-cdabe12d5fd8}</UniqueIdentifier>
</Filter>
<Filter Include="scripts">
<UniqueIdentifier>{23f41b89-5d6f-48b1-8016-ea5107d4976f}</UniqueIdentifier>
</Filter>
<Filter Include="include">
<UniqueIdentifier>{aca759ef-33ea-4723-8a6f-84302ce25d77}</UniqueIdentifier>
</Filter>
<Filter Include="config">
<UniqueIdentifier>{71df60be-5005-472b-a6c3-c52100ccae2b}</UniqueIdentifier>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aca759ef-33ea-4723-8a6f-84302ce25d77}</UniqueIdentifier>
</Filter>
<Filter Include="include">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="BufferViewer.cpp">
Expand Down Expand Up @@ -56,73 +56,73 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="BufferViewer.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="CommandLineArgs.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="CreateSphere.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="FrameEventTracing.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="FrustumViewer.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="Gui.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="Scene.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="SceneObject.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="SharedConstants.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="TerrainGenerator.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="TextureViewer.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="TimeTracing.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\include\ArgParser.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\include\ConfigurationParser.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\include\D3D12GpuTimer.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\include\DebugHelper.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\include\Timer.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\include\TimeTracing.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\include\WindowCapture.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\DirectXTK12\d3dx12.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\DirectXTK12\DDS.h">
<Filter>include</Filter>
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="AssetUploader.h">
<Filter>Header Files</Filter>
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
Expand Down
61 changes: 51 additions & 10 deletions src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,31 @@ UINT Scene::DetermineMaxNumFeedbackResolves()
return maxNumFeedbackResolves;
}

//-----------------------------------------------------------------------------
// crude system to minimize state transitions: group objects by PSO
//-----------------------------------------------------------------------------
typedef std::pair<SceneObjects::BaseObject*, UINT> ObjectIndexPair;
typedef std::vector<ObjectIndexPair> ObjectSet;

void DrawObjectSets(const std::vector<ObjectSet>& in_objectSets, SceneObjects::DrawParams& in_params,
const D3D12_GPU_DESCRIPTOR_HANDLE in_descriptorBase, UINT in_descriptorSize,
ID3D12GraphicsCommandList1* out_pCommandList)
{
for (auto& t : in_objectSets)
{
if (t.size())
{
t[0].first->SetCommonPipelineState(out_pCommandList, in_params); // these objects all share pipeline state
for (auto& o : t)
{
in_params.m_srvBaseGPU = CD3DX12_GPU_DESCRIPTOR_HANDLE(in_descriptorBase, (INT)(o.second * (INT)SceneObjects::Descriptors::NumEntries), in_descriptorSize);

o.first->Draw(out_pCommandList, in_params);
}
}
}
}

//-----------------------------------------------------------------------------
// draw all objects
// uses the min-mip-map created using Sampler Feedback on the GPU
Expand All @@ -1131,6 +1156,16 @@ void Scene::DrawObjects()

const D3D12_GPU_DESCRIPTOR_HANDLE srvBaseGPU = CD3DX12_GPU_DESCRIPTOR_HANDLE(m_srvHeap->GetGPUDescriptorHandleForHeapStart(), (UINT)DescriptorHeapOffsets::NumEntries, m_srvUavCbvDescriptorSize);

enum class MaterialType : UINT
{
Sky, // always draw the sky first, if there is one
PlanetWithFeedback,
Planet,
NumMaterials
};

std::vector<ObjectSet> objectSets((UINT)MaterialType::NumMaterials);

//------------------------------------------------------------------------------------
// set feedback state on each object
// objects with feedback enabled will queue feedback resolve on the TileUpdateManager
Expand All @@ -1147,7 +1182,8 @@ void Scene::DrawObjects()
UINT numFeedbackObjects = 0;
for (UINT i = m_queueFeedbackIndex; i < numObjects; i++)
{
auto o = m_objects[i % (UINT)m_objects.size()];
UINT objectIndex = i % (UINT)m_objects.size();
auto o = m_objects[objectIndex];

// FIXME: want proper frustum culling here
float w = XMVectorGetW(o->GetCombinedMatrix().r[3]);
Expand All @@ -1165,6 +1201,19 @@ void Scene::DrawObjects()
queueFeedback = true;
numFeedbackObjects++;
}

// only draw visible objects
// group objects by material (PSO)
MaterialType materialType = MaterialType::Sky;
if (o != m_pSky)
{
materialType = MaterialType::Planet;
if (queueFeedback)
{
materialType = MaterialType::PlanetWithFeedback;
}
}
objectSets[(UINT)materialType].emplace_back(ObjectIndexPair(o, objectIndex));
}
else // evict tiles of objects that are not visible
{
Expand All @@ -1183,15 +1232,7 @@ void Scene::DrawObjects()
m_prevNumFeedbackObjects[m_frameIndex] = numFeedbackObjects;
}

// draw the objects in the same order each time
auto descriptorBase = CD3DX12_GPU_DESCRIPTOR_HANDLE(srvBaseGPU);
for (auto o : m_objects)
{
drawParams.m_srvBaseGPU = descriptorBase;
descriptorBase.Offset((UINT)SceneObjects::Descriptors::NumEntries, m_srvUavCbvDescriptorSize);

o->Draw(m_commandList.Get(), drawParams);
}
DrawObjectSets(objectSets, drawParams, srvBaseGPU, m_srvUavCbvDescriptorSize, m_commandList.Get());
}

//-------------------------------------------------------------------------
Expand Down
47 changes: 29 additions & 18 deletions src/SceneObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,35 @@ void SceneObjects::BaseObject::SetGeometry(ID3D12Resource* in_pVertexBuffer, UIN
lod.m_indexBufferView = { lod.m_indexBuffer->GetGPUVirtualAddress(), (UINT)in_pIndexBuffer->GetDesc().Width, DXGI_FORMAT_R32_UINT};
}

//-------------------------------------------------------------------------
// state common to multiple objects
// basic scene consists of a sky (1 or none), objects using feedback, and objects not using feedback
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::SetCommonPipelineState(ID3D12GraphicsCommandList1* in_pCommandList, const SceneObjects::DrawParams& in_drawParams)
{
// if feedback is enabled, 2 things:
// 1. tell the tile update manager to queue a readback of the resolved feedback
// 2. draw the object with a shader that calls WriteSamplerFeedback()
if (m_feedbackEnabled)
{
SetRootSigPsoFB(in_pCommandList);
}
else
{
SetRootSigPso(in_pCommandList);
}
in_pCommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

// shared min mip map resource
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamSharedTextures, in_drawParams.m_sharedMinMipMap);

// frame constant buffer
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamConstantBuffers, in_drawParams.m_constantBuffers);

// samplers
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamSamplers, in_drawParams.m_samplers);
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::Draw(ID3D12GraphicsCommandList1* in_pCommandList, const SceneObjects::DrawParams& in_drawParams)
Expand Down Expand Up @@ -331,39 +360,21 @@ void SceneObjects::BaseObject::Draw(ID3D12GraphicsCommandList1* in_pCommandList,
}
const auto& geometry = m_lods[lod];

// if feedback is enabled, 2 things:
// 1. tell the tile update manager to queue a readback of the resolved feedback
// 2. draw the object with a shader that calls WriteSamplerFeedback()
if (m_feedbackEnabled)
{
auto feedbackDescriptor = CD3DX12_GPU_DESCRIPTOR_HANDLE(in_drawParams.m_srvBaseGPU,
UINT(SceneObjects::Descriptors::HeapOffsetFeedback), m_srvUavCbvDescriptorSize);
m_pTileUpdateManager->QueueFeedback(GetStreamingResource(), feedbackDescriptor);
SetRootSigPsoFB(in_pCommandList);
}
else
{
SetRootSigPso(in_pCommandList);
}

// uavs and srvs
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)RootSigParams::ParamObjectTextures, in_drawParams.m_srvBaseGPU);

// shared min mip map resource
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamSharedTextures, in_drawParams.m_sharedMinMipMap);

// constant buffers
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamConstantBuffers, in_drawParams.m_constantBuffers);

// samplers
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamSamplers, in_drawParams.m_samplers);

ModelConstantData modelConstantData{};
SetModelConstants(modelConstantData, in_drawParams.m_projection, in_drawParams.m_view);
UINT num32BitValues = sizeof(ModelConstantData) / sizeof(UINT32);
in_pCommandList->SetGraphicsRoot32BitConstants((UINT)RootSigParams::Param32BitConstants, num32BitValues, &modelConstantData, 0);

in_pCommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
in_pCommandList->IASetIndexBuffer(&geometry.m_indexBufferView);
in_pCommandList->IASetVertexBuffers(0, 1, &geometry.m_vertexBufferView);
in_pCommandList->DrawIndexedInstanced(geometry.m_numIndices, 1, 0, 0, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/SceneObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ namespace SceneObjects

bool GetPackedMipsPresent() const { return m_pStreamingResource->GetPackedMipsResident(); }

// state re-used by a number of objects
void SetCommonPipelineState(ID3D12GraphicsCommandList1* in_pCommandList, const SceneObjects::DrawParams& in_drawParams);

virtual void Draw(ID3D12GraphicsCommandList1* in_pCommandList, const DrawParams& in_drawParams);

DirectX::XMMATRIX& GetModelMatrix() { return m_matrix; }
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/terrainPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ float3 evaluateLight(in float3 normal, in float3 pos, in float3 tex)
float specDot = saturate(dot(reflected, pointToLight));
float specular = pow(specDot, g_specularColor.a);

float3 color = ambient + (diffuse * g_lightColor.xyz);
float3 color = max(ambient, diffuse) * g_lightColor.xyz;
color *= tex;
color = pow(color, 1.0f / 1.3f);
color = saturate(color);
Expand Down

0 comments on commit 88e0426

Please sign in to comment.