Skip to content

Commit

Permalink
Made RT requirement optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaniti committed Dec 5, 2023
1 parent 623de9d commit c0003dc
Show file tree
Hide file tree
Showing 25 changed files with 273 additions and 112 deletions.
5 changes: 5 additions & 0 deletions BoolkaCommon/SolutionHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
for (auto& elem : arr) \
elem.Unload(); \
}
#define BLK_SAFE_UNLOAD_ARRAY(arr) \
{ \
for (auto& elem : arr) \
elem.SafeUnload(); \
}

#define BLK_FLOAT_PI 3.141592f

Expand Down
49 changes: 43 additions & 6 deletions D3D12Backend/APIWrappers/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ namespace Boolka
return m_DStorageFactory;
}

bool Device::SupportsRaytracing() const
{
return m_SupportsRaytracing;
}

bool Device::Initialize(Factory& factory)
{
BLK_ASSERT(m_Device == nullptr);
Expand All @@ -71,6 +76,7 @@ namespace Boolka

if (!SelectAndCreateDevice(factory))
return false;
InitializeFeatureSupport();

BLK_RENDER_PROFILING_ONLY(InitializeProfiling());
BLK_RENDER_DEBUG_ONLY(InitializeDebug());
Expand Down Expand Up @@ -99,10 +105,12 @@ namespace Boolka
m_GraphicQueue.Unload();
m_DStorageFactory.Unload();

BLK_RENDER_DEBUG_ONLY(ReportObjectLeaks());
BLK_RENDER_DEBUG_ONLY(ReportDeviceObjectLeaks());

m_Device->Release();
m_Device = nullptr;
m_Adapter->Release();
m_Adapter = nullptr;
}

void Device::Flush()
Expand Down Expand Up @@ -174,19 +182,48 @@ namespace Boolka
continue;
}

m_Adapter = adapter;
m_Device = device;
return true;
if (FeatureSupportHelper::HasPreferredFeatures(device))
{
BLK_SAFE_RELEASE(m_Adapter);
BLK_SAFE_RELEASE(m_Device);
m_Adapter = adapter;
m_Device = device;
return true;
}

if (m_Adapter == nullptr)
{
m_Adapter = adapter;
m_Device = device;
}
else
{
adapter->Release();
device->Release();
}

++i;
continue;
}

if (m_Adapter != nullptr)
{
return true;
}

::MessageBoxW(0,
L"No supported GPUs found.\nRequired features are:\nResource Binding Tier 3\n"
L"Shader Model 6.5\nRaytracing Tier 1.0\nMesh Shaders",
L"Shader Model 6.5\nRaytracing Tier 1.0\nMesh Shaders\n32 Wave width",
L"GPU and/or driver unsupported", MB_OK | MB_ICONERROR);
BLK_CRITICAL_DEBUG_BREAK();
return false;
}

void Device::InitializeFeatureSupport()
{
m_SupportsRaytracing = FeatureSupportHelper::SupportRaytracing(m_Device);
}

#ifdef BLK_RENDER_PROFILING
void Device::InitializeProfiling()
{
Expand Down Expand Up @@ -218,7 +255,7 @@ namespace Boolka
debugInfoQueue->Release();
}

void Device::ReportObjectLeaks()
void Device::ReportDeviceObjectLeaks()
{
ID3D12DebugDevice2* debugDevice = nullptr;
HRESULT hr = m_Device->QueryInterface(IID_PPV_ARGS(&debugDevice));
Expand Down
7 changes: 6 additions & 1 deletion D3D12Backend/APIWrappers/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace Boolka

[[nodiscard]] DStorageFactory& GetDStorageFactory();

[[nodiscard]] bool SupportsRaytracing() const;

bool Initialize(Factory& factory);
void Unload();

Expand All @@ -42,13 +44,14 @@ namespace Boolka
#endif
private:
bool SelectAndCreateDevice(Factory& factory);
void InitializeFeatureSupport();
#ifdef BLK_RENDER_PROFILING
void InitializeProfiling();
#endif
#ifdef BLK_RENDER_DEBUG
void InitializeDebug();
void SetDebugBreakSeverity(D3D12_MESSAGE_SEVERITY severity);
void ReportObjectLeaks();
void ReportDeviceObjectLeaks();
#endif

IDXGIAdapter4* m_Adapter;
Expand All @@ -60,6 +63,8 @@ namespace Boolka
ComputeQueue m_ComputeQueue;
CopyQueue m_CopyQueue;
DStorageQueue m_DStorageQueue;

bool m_SupportsRaytracing;
};

} // namespace Boolka
5 changes: 5 additions & 0 deletions D3D12Backend/APIWrappers/PipelineState/StateObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ namespace Boolka
m_StateObject = nullptr;
}

void StateObject::SafeUnload()
{
BLK_SAFE_RELEASE(m_StateObject);
}

bool StateObject::InitializeInternal(Device& device, const wchar_t* name,
const D3D12_STATE_OBJECT_DESC& desc)
{
Expand Down
1 change: 1 addition & 0 deletions D3D12Backend/APIWrappers/PipelineState/StateObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Boolka
};

void Unload();
void SafeUnload();

private:
bool InitializeInternal(Device& device, const wchar_t* name,
Expand Down
5 changes: 5 additions & 0 deletions D3D12Backend/APIWrappers/Resources/Buffers/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace Boolka
return true;
}

void Buffer::SafeUnload()
{
BLK_SAFE_RELEASE(m_Resource);
}

void Buffer::Unload()
{
BLK_ASSERT(m_Resource != nullptr);
Expand Down
4 changes: 3 additions & 1 deletion D3D12Backend/APIWrappers/Resources/Buffers/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace Boolka
~Buffer() = default;

bool Initialize(Device& device, UINT64 size, D3D12_HEAP_TYPE heapType,
D3D12_RESOURCE_FLAGS resourceFlags, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON);
D3D12_RESOURCE_FLAGS resourceFlags,
D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON);
void SafeUnload();
void Unload();
};

Expand Down
96 changes: 51 additions & 45 deletions D3D12Backend/Containers/PSOContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Boolka
BLK_ASSERT_VAR(res);
DebugFileReader::FreeMemory(PS);
DebugFileReader::FreeMemory(VS);
emptyInputLayout.Unload();

PS = DebugFileReader::ReadFile("GBufferPassPixelShader.cso");
AS = DebugFileReader::ReadFile("AmplificationShader.cso");
Expand Down Expand Up @@ -121,48 +122,50 @@ namespace Boolka
DebugFileReader::FreeMemory(CS);
#endif

DebugProfileTimer rtpsoTimer;
rtpsoTimer.Start();

MemoryBlock shaderLib = DebugFileReader::ReadFile("RaytracePassLib.cso");
const wchar_t* rayGenExport = L"RayGeneration";
const wchar_t* missExport = L"MissShader";
const wchar_t* closestHitExport = L"ClosestHit";
const wchar_t* libExports[] = {rayGenExport, missExport, closestHitExport};
const wchar_t* hitGroupExport = L"HitGroup";
res = GetPSO(RTPSO::RaytracePass)
.Initialize(device, L"RTPSO::RaytracePass",
GlobalRootSignatureParam{defaultRootSig},
DXILLibraryParam<ARRAYSIZE(libExports)>{shaderLib, libExports},
HitGroupParam{hitGroupExport, closestHitExport},
RaytracingShaderConfigParam{sizeof(HLSLShared::RaytracePayload),
sizeof(Vector2)},
RaytracingPipelineConfigParam{BLK_RT_MAX_RECURSION_DEPTH});
BLK_ASSERT_VAR(res);
DebugFileReader::FreeMemory(shaderLib);

rtpsoTimer.Stop(L"RTPSO compile");

UINT64 shaderTableSize = ShaderTable::CalculateRequiredBufferSize(1, 1, 1);

Buffer& shaderTableBuffer =
engineContext.GetResourceContainer().GetBuffer(ResourceContainer::Buf::RTShaderTable);
m_ShaderTablesUploadBuffer.Initialize(device, shaderTableSize);
void* uploadBuffer = m_ShaderTablesUploadBuffer.Map();
GetShaderTable(RTShaderTable::Default)
.Build(shaderTableBuffer->GetGPUVirtualAddress(), uploadBuffer,
GetPSO(RTPSO::RaytracePass), 1, &rayGenExport, 1, &missExport, 1,
&hitGroupExport);

GraphicCommandListImpl& initializationCommandList =
engineContext.GetInitializationCommandList();
initializationCommandList->CopyResource(shaderTableBuffer.Get(),
m_ShaderTablesUploadBuffer.Get());

BLK_RENDER_DEBUG_ONLY(device.RemoveLastMessageFilter());
emptyInputLayout.Unload();

timer.Stop(L"All PSOs compile");
if (device.SupportsRaytracing())
{
DebugProfileTimer rtpsoTimer;
rtpsoTimer.Start();

MemoryBlock shaderLib = DebugFileReader::ReadFile("RaytracePassLib.cso");
const wchar_t* rayGenExport = L"RayGeneration";
const wchar_t* missExport = L"MissShader";
const wchar_t* closestHitExport = L"ClosestHit";
const wchar_t* libExports[] = {rayGenExport, missExport, closestHitExport};
const wchar_t* hitGroupExport = L"HitGroup";
res = GetPSO(RTPSO::RaytracePass)
.Initialize(device, L"RTPSO::RaytracePass",
GlobalRootSignatureParam{defaultRootSig},
DXILLibraryParam<ARRAYSIZE(libExports)>{shaderLib, libExports},
HitGroupParam{hitGroupExport, closestHitExport},
RaytracingShaderConfigParam{sizeof(HLSLShared::RaytracePayload),
sizeof(Vector2)},
RaytracingPipelineConfigParam{BLK_RT_MAX_RECURSION_DEPTH});
BLK_ASSERT_VAR(res);
DebugFileReader::FreeMemory(shaderLib);

rtpsoTimer.Stop(L"RTPSO compile");

UINT64 shaderTableSize = ShaderTable::CalculateRequiredBufferSize(1, 1, 1);

Buffer& shaderTableBuffer = engineContext.GetResourceContainer().GetBuffer(
ResourceContainer::Buf::RTShaderTable);
m_ShaderTablesUploadBuffer.Initialize(device, shaderTableSize);
void* uploadBuffer = m_ShaderTablesUploadBuffer.Map();
GetShaderTable(RTShaderTable::Default)
.Build(shaderTableBuffer->GetGPUVirtualAddress(), uploadBuffer,
GetPSO(RTPSO::RaytracePass), 1, &rayGenExport, 1, &missExport, 1,
&hitGroupExport);

GraphicCommandListImpl& initializationCommandList =
engineContext.GetInitializationCommandList();
initializationCommandList->CopyResource(shaderTableBuffer.Get(),
m_ShaderTablesUploadBuffer.Get());

BLK_RENDER_DEBUG_ONLY(device.RemoveLastMessageFilter());

timer.Stop(L"All PSOs compile");
}

return true;
}
Expand All @@ -171,12 +174,15 @@ namespace Boolka
{
BLK_UNLOAD_ARRAY(m_GraphicPSOs);
BLK_UNLOAD_ARRAY(m_ComputePSOs);
BLK_UNLOAD_ARRAY(m_RTPSOs);
BLK_SAFE_UNLOAD_ARRAY(m_RTPSOs);
}

void PSOContainer::FinishInitialization()
void PSOContainer::FinishInitialization(Device& device)
{
m_ShaderTablesUploadBuffer.Unload();
if (device.SupportsRaytracing())
{
m_ShaderTablesUploadBuffer.Unload();
}
}

GraphicPipelineState& PSOContainer::GetPSO(GraphicPSO id)
Expand Down
2 changes: 1 addition & 1 deletion D3D12Backend/Containers/PSOContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Boolka
bool Initialize(Device& device, RenderEngineContext& engineContext);
void Unload();

void FinishInitialization();
void FinishInitialization(Device& device);

[[nodiscard]] GraphicPipelineState& GetPSO(GraphicPSO id);
[[nodiscard]] ComputePipelineState& GetPSO(ComputePSO id);
Expand Down
5 changes: 5 additions & 0 deletions D3D12Backend/Containers/RTASContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ namespace Boolka
return true;
}

void RTASContainer::SafeUnload()
{
m_ASBuffer.SafeUnload();
}

void RTASContainer::Unload()
{
m_ASBuffer.Unload();
Expand Down
1 change: 1 addition & 0 deletions D3D12Backend/Containers/RTASContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Boolka
bool Initialize(Device& device, RenderEngineContext& engineContext,
const SceneDataReader::HeaderWrapper& headerWrapper, Buffer& vertexBuffer,
Buffer& indexBuffer);
void SafeUnload();
void Unload();

void FinishLoading(Device& device, RenderEngineContext& engineContext,
Expand Down
23 changes: 15 additions & 8 deletions D3D12Backend/Containers/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ namespace Boolka
InitializeTextures(device, sceneHeader, headerWrapper, textureOffsets, mainSRVHeap,
mainSRVHeapOffset);

MemoryBlock rtCacheheaderWrapper{};

UINT64 sourceOffset = 0;

UploadBuffers(device, sceneHeader, sourceOffset);

m_RTASContainer.Initialize(device, engineContext, headerWrapper, m_VertexBuffer1,
m_RTIndexBuffer);
if (device.SupportsRaytracing())
{
m_RTASContainer.Initialize(device, engineContext, headerWrapper, m_VertexBuffer1,
m_RTIndexBuffer);
}

UploadSkyBox(device, sceneHeader, sourceOffset);
UploadTextures(device, sceneHeader, headerWrapper, sourceOffset);
Expand All @@ -87,7 +88,7 @@ namespace Boolka
{
m_DataReader.CloseReader();

m_RTASContainer.Unload();
m_RTASContainer.SafeUnload();

BLK_UNLOAD_ARRAY(m_SceneTextures);
m_SceneTextures.clear();
Expand Down Expand Up @@ -116,12 +117,18 @@ namespace Boolka
{
const SceneDataReader::HeaderWrapper headerWrapper = m_DataReader.GetHeaderWrapper();

m_RTASContainer.FinishLoading(device, engineContext, headerWrapper);
if (device.SupportsRaytracing())
{
m_RTASContainer.FinishLoading(device, engineContext, headerWrapper);
}
}

void Scene::FinishInitialization()
void Scene::FinishInitialization(Device& device)
{
m_RTASContainer.FinishInitialization();
if (device.SupportsRaytracing())
{
m_RTASContainer.FinishInitialization();
}
}

UINT Scene::GetObjectCount() const
Expand Down
2 changes: 1 addition & 1 deletion D3D12Backend/Containers/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Boolka

void FinishLoading(Device& device, RenderEngineContext& engineContext);

void FinishInitialization();
void FinishInitialization(Device& device);

// All opaque objects placed before all transparent objects
// So objects in range [0, m_OpaqueObjectCount) - are opaque
Expand Down
Loading

0 comments on commit c0003dc

Please sign in to comment.