7#include <unordered_map>
8#include <unordered_set>
11#include "render/general/resource/UploadBuffer.h"
12#include "render/general/resource/frame/FrameResourceManager.h"
13#include "shader/general/resource/ShaderLightArray.h"
15#include "render/directx/resource/DirectXResource.h"
16#include "shader/hlsl/RootSignatureGenerator.h"
17#include "render/directx/pipeline/DirectXPso.h"
21#include "vulkan/vulkan_core.h"
23#include "directx/d3dx12.h"
29 using namespace Microsoft::WRL;
34 class ComputeShaderInterface;
43 alignas(iVkVec4Alignment) glm::vec4
position = glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
58 alignas(iVkVec4Alignment) glm::vec4
ambientLight = glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
163 inline void setShadowPassLightInfoViewToCommandList(
165 const ComPtr<ID3D12GraphicsCommandList>& pCommandList,
166 size_t iCurrentFrameResourceIndex) {
168 std::scoped_lock gpuDataGuard(
mtxGpuData.first);
175 pipelineData.rootParameterIndices.end()) [[unlikely]] {
176 Error error(std::format(
177 "shader resource \"{}\" is not used in the shaders of the specified PSO \"{}\" "
178 "but you are attempting to set this resource to a command list",
182 throw std::runtime_error(error.getFullErrorMessage());
187 setLightingArrayViewToCommandList(
190 iCurrentFrameResourceIndex,
193 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
194 SpecialRootParameterSlot::SHADOW_PASS_LIGHT_INFO)]);
214 static inline void setLightingArrayViewToCommandList(
216 const ComPtr<ID3D12GraphicsCommandList>& pCommandList,
217 size_t iCurrentFrameResourceIndex,
218 const std::unique_ptr<ShaderLightArray>& pArray,
219 const std::string& sArrayNameInShaders,
220 UINT iArrayRootParameterIndex) {
221 std::scoped_lock lightsGuard(pArray->mtxResources.first);
225 if (pPso->getInternalResources()->second.rootParameterIndices.find(sArrayNameInShaders) ==
226 pPso->getInternalResources()->second.rootParameterIndices.end()) [[unlikely]] {
227 Error error(std::format(
228 "shader resource \"{}\" is not used in the shaders of the specified PSO \"{}\" "
229 "but you are attempting to set this resource to a command list",
231 pPso->getPipelineIdentifier()));
233 throw std::runtime_error(error.getFullErrorMessage());
239 pCommandList->SetGraphicsRootShaderResourceView(
240 iArrayRootParameterIndex,
241 reinterpret_cast<DirectXResource*
>(
242 pArray->mtxResources.second.vGpuArrayLightDataResources[iCurrentFrameResourceIndex]
243 ->getInternalResource())
244 ->getInternalResource()
245 ->GetGPUVirtualAddress());
329 inline void setResourceViewToCommandList(
331 const ComPtr<ID3D12GraphicsCommandList>& pCommandList,
332 size_t iCurrentFrameResourceIndex) {
334 std::scoped_lock gpuDataGuard(
mtxGpuData.first);
336 auto& pipelineData = pPso->getInternalResources()->second;
341 pipelineData.rootParameterIndices.end()) [[unlikely]] {
342 Error error(std::format(
343 "shader resource \"{}\" is not used in the shaders of the specified PSO \"{}\" "
344 "but you are attempting to set this resource to a command list",
346 pPso->getPipelineIdentifier()));
348 throw std::runtime_error(error.getFullErrorMessage());
353 pCommandList->SetGraphicsRootConstantBufferView(
354 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
355 SpecialRootParameterSlot::GENERAL_LIGHTING)],
356 reinterpret_cast<DirectXResource*
>(
357 mtxGpuData.second.vGeneralDataGpuResources[iCurrentFrameResourceIndex]
358 ->getInternalResource())
359 ->getInternalResource()
360 ->GetGPUVirtualAddress());
363 setLightingArrayViewToCommandList(
366 iCurrentFrameResourceIndex,
369 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
370 SpecialRootParameterSlot::POINT_LIGHTS)]);
373 setLightingArrayViewToCommandList(
376 iCurrentFrameResourceIndex,
379 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
380 SpecialRootParameterSlot::DIRECTIONAL_LIGHTS)]);
383 setLightingArrayViewToCommandList(
386 iCurrentFrameResourceIndex,
389 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
390 SpecialRootParameterSlot::SPOT_LIGHTS)]);
393 static_assert(
sizeof(LightArrays) == 32,
"consider adding new arrays here");
408 inline void setOpaqueLightGridResourcesViewToCommandList(
409 DirectXPso* pPso,
const ComPtr<ID3D12GraphicsCommandList>& pCommandList)
const {
410 auto& pipelineData = pPso->getInternalResources()->second;
413 pCommandList->SetGraphicsRootUnorderedAccessView(
414 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
415 SpecialRootParameterSlot::LIGHT_CULLING_POINT_LIGHT_INDEX_LIST)],
416 reinterpret_cast<DirectXResource*
>(
418 ->getInternalResource()
419 ->GetGPUVirtualAddress());
422 pCommandList->SetGraphicsRootUnorderedAccessView(
423 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
424 SpecialRootParameterSlot::LIGHT_CULLING_SPOT_LIGHT_INDEX_LIST)],
425 reinterpret_cast<DirectXResource*
>(
427 ->getInternalResource()
428 ->GetGPUVirtualAddress());
431 pCommandList->SetGraphicsRootDescriptorTable(
432 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
433 SpecialRootParameterSlot::LIGHT_CULLING_POINT_LIGHT_GRID)],
434 *
reinterpret_cast<DirectXResource*
>(
436 ->getBindedDescriptorGpuHandle(DirectXDescriptorType::UAV));
439 pCommandList->SetGraphicsRootDescriptorTable(
440 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
441 SpecialRootParameterSlot::LIGHT_CULLING_SPOT_LIGHT_GRID)],
442 *
reinterpret_cast<DirectXResource*
>(
444 ->getBindedDescriptorGpuHandle(DirectXDescriptorType::UAV));
458 inline void setTransparentLightGridResourcesViewToCommandList(
459 DirectXPso* pPso,
const ComPtr<ID3D12GraphicsCommandList>& pCommandList)
const {
460 auto& pipelineData = pPso->getInternalResources()->second;
463 pCommandList->SetGraphicsRootUnorderedAccessView(
464 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
465 SpecialRootParameterSlot::LIGHT_CULLING_POINT_LIGHT_INDEX_LIST)],
466 reinterpret_cast<DirectXResource*
>(
468 ->getInternalResource()
469 ->GetGPUVirtualAddress());
472 pCommandList->SetGraphicsRootUnorderedAccessView(
473 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
474 SpecialRootParameterSlot::LIGHT_CULLING_SPOT_LIGHT_INDEX_LIST)],
475 reinterpret_cast<DirectXResource*
>(
477 ->getInternalResource()
478 ->GetGPUVirtualAddress());
481 pCommandList->SetGraphicsRootDescriptorTable(
482 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
483 SpecialRootParameterSlot::LIGHT_CULLING_POINT_LIGHT_GRID)],
484 *
reinterpret_cast<DirectXResource*
>(
486 ->getBindedDescriptorGpuHandle(DirectXDescriptorType::UAV));
489 pCommandList->SetGraphicsRootDescriptorTable(
490 pipelineData.vSpecialRootParameterIndices[
static_cast<size_t>(
491 SpecialRootParameterSlot::LIGHT_CULLING_SPOT_LIGHT_GRID)],
492 *
reinterpret_cast<DirectXResource*
>(
494 ->getBindedDescriptorGpuHandle(DirectXDescriptorType::UAV));
610 const std::pair<unsigned int, unsigned int>& renderTargetSize,
611 const glm::mat4& inverseProjectionMatrix);
753 [[nodiscard]] std::optional<Error>
initialize(
799 size_t iCurrentFrameResourceIndex,
804 GpuResource* pNonCulledSpotlightsIndicesArray)
const;
835 "globalCountersIntoLightIndexList";
866 const std::pair<unsigned int, unsigned int>& renderTargetSize,
867 const glm::mat4& inverseProjectionMatrix);
1004 "pointLightsInCameraFrustumIndices";
1008 "spotlightsInCameraFrustumIndices";
Definition: DirectXPso.h:22
std::pair< std::recursive_mutex, InternalResources > * getInternalResources()
Definition: DirectXPso.h:144
static constexpr unsigned int getFrameResourceCount()
Definition: FrameResourceManager.h:74
Definition: GpuResource.h:16
Definition: LightingShaderResourceManager.h:50
ShaderLightArray * getPointLightDataArray() const
Definition: LightingShaderResourceManager.cpp:19
static std::unique_ptr< LightingShaderResourceManager > create(Renderer *pRenderer)
Definition: LightingShaderResourceManager.cpp:1328
std::unique_ptr< ComputeShaderInterface > pPrepareLightCullingComputeInterface
Definition: LightingShaderResourceManager.h:982
std::optional< Error > bindDescriptorsToRecreatedPipelineResources()
Definition: LightingShaderResourceManager.cpp:35
LightArrays lightArrays
Definition: LightingShaderResourceManager.h:970
ShaderLightArray * getSpotlightDataArray() const
Definition: LightingShaderResourceManager.cpp:27
void setAmbientLight(const glm::vec3 &ambientLight)
Definition: LightingShaderResourceManager.cpp:1273
void updateResources(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex)
Definition: LightingShaderResourceManager.cpp:131
static const std::string sSpotlightsInCameraFrustumIndicesShaderResourceName
Definition: LightingShaderResourceManager.h:1007
void onSpotlightsInFrustumCulled(size_t iCurrentFrameResourceIndex)
Definition: LightingShaderResourceManager.cpp:294
void onDirectionalLightArraySizeChanged(size_t iNewSize)
Definition: LightingShaderResourceManager.cpp:245
static const std::string sGeneralLightingDataShaderResourceName
Definition: LightingShaderResourceManager.h:991
static std::string getGeneralLightingDataShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1299
static const std::string sShadowPassLightInfoShaderResourceName
Definition: LightingShaderResourceManager.h:1014
ComputeShaderData::FrustumGridComputeShader::ComputeShader frustumGridComputeShaderData
Definition: LightingShaderResourceManager.h:976
std::optional< Error > rebindGpuDataToPipelineIfUsed(Pipeline *pPipeline)
Definition: LightingShaderResourceManager.cpp:361
static const std::string sDirectionalLightsShaderResourceName
Definition: LightingShaderResourceManager.h:997
Renderer * pRenderer
Definition: LightingShaderResourceManager.h:985
ShaderLightArray * getShadowPassLightInfoArray() const
Definition: LightingShaderResourceManager.cpp:31
static const std::string sPointLightsInCameraFrustumIndicesShaderResourceName
Definition: LightingShaderResourceManager.h:1003
static std::string getPointLightWorldLocationsShaderResourceName()
std::optional< Error > recalculateLightTileFrustums(const std::pair< unsigned int, unsigned int > &renderTargetSize, const glm::mat4 &inverseProjectionMatrix)
Definition: LightingShaderResourceManager.cpp:1158
void onPointLightArraySizeChanged(size_t iNewSize)
Definition: LightingShaderResourceManager.cpp:196
std::pair< std::recursive_mutex, GpuData > * getInternalResources()
Definition: LightingShaderResourceManager.cpp:127
static const std::string sSpotlightsShaderResourceName
Definition: LightingShaderResourceManager.h:1000
std::optional< Error > rebindGpuDataToAllPipelines()
Definition: LightingShaderResourceManager.cpp:327
void onPointLightsInFrustumCulled(size_t iCurrentFrameResourceIndex)
Definition: LightingShaderResourceManager.cpp:220
ComputeShaderData::LightCullingComputeShader::ComputeShader lightCullingComputeShaderData
Definition: LightingShaderResourceManager.h:979
static std::string getPointLightsShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1303
void onEngineShadersCompiled()
Definition: LightingShaderResourceManager.cpp:1271
static std::string getShadowPassLightInfoArrayShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1323
std::optional< Error > updateDescriptorsForPipelineResource(Pipeline *pPipeline)
Definition: LightingShaderResourceManager.cpp:81
ShaderLightArray * getDirectionalLightDataArray() const
Definition: LightingShaderResourceManager.cpp:23
static std::string getSpotlightsShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1311
static std::string getSpotlightsInCameraFrustumIndicesShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1319
static std::string getPointLightsInCameraFrustumIndicesShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1315
void onSpotlightArraySizeChanged(size_t iNewSize)
Definition: LightingShaderResourceManager.cpp:270
static std::string getDirectionalLightsShaderResourceName()
Definition: LightingShaderResourceManager.cpp:1307
bool bEngineShadersCompiled
Definition: LightingShaderResourceManager.h:988
static const std::string sPointLightsShaderResourceName
Definition: LightingShaderResourceManager.h:994
static constexpr auto generalLightingDataDescriptorType
Definition: LightingShaderResourceManager.h:1017
void copyDataToGpu(size_t iCurrentFrameResourceIndex)
Definition: LightingShaderResourceManager.cpp:319
std::pair< std::recursive_mutex, GpuData > mtxGpuData
Definition: LightingShaderResourceManager.h:973
Definition: Pipeline.h:20
std::string getPipelineIdentifier() const
Definition: Pipeline.cpp:175
Definition: Renderer.h:43
Definition: FrameResource.h:15
Definition: LightingShaderResourceManager.h:544
unsigned int iThreadGroupCountY
Definition: LightingShaderResourceManager.h:549
unsigned int iTileCountX
Definition: LightingShaderResourceManager.h:552
unsigned int iThreadGroupCountX
Definition: LightingShaderResourceManager.h:546
unsigned int iTileCountY
Definition: LightingShaderResourceManager.h:555
Definition: LightingShaderResourceManager.h:589
ShaderResources resources
Definition: LightingShaderResourceManager.h:617
static const auto sCalculatedFrustumsShaderResourceName
Definition: LightingShaderResourceManager.h:650
static const auto sScreenToViewDataShaderResourceName
Definition: LightingShaderResourceManager.h:644
std::optional< Error > initialize(Renderer *pRenderer)
Definition: LightingShaderResourceManager.cpp:726
std::unique_ptr< ComputeShaderInterface > pComputeInterface
Definition: LightingShaderResourceManager.h:614
unsigned int iLastUpdateTileCountY
Definition: LightingShaderResourceManager.h:629
unsigned int iLastUpdateTileCountX
Definition: LightingShaderResourceManager.h:623
static const auto sComputeInfoShaderResourceName
Definition: LightingShaderResourceManager.h:638
std::optional< Error > updateDataAndSubmitShader(Renderer *pRenderer, const std::pair< unsigned int, unsigned int > &renderTargetSize, const glm::mat4 &inverseProjectionMatrix)
Definition: LightingShaderResourceManager.cpp:637
bool bIsInitialized
Definition: LightingShaderResourceManager.h:632
Definition: LightingShaderResourceManager.h:559
unsigned int iRenderTargetWidth
Definition: LightingShaderResourceManager.h:564
glm::mat4 inverseProjectionMatrix
Definition: LightingShaderResourceManager.h:561
unsigned int iRenderTargetHeight
Definition: LightingShaderResourceManager.h:567
Definition: LightingShaderResourceManager.h:571
std::unique_ptr< GpuResource > pCalculatedFrustums
Definition: LightingShaderResourceManager.h:585
std::unique_ptr< UploadBuffer > pComputeInfo
Definition: LightingShaderResourceManager.h:576
std::unique_ptr< UploadBuffer > pScreenToViewData
Definition: LightingShaderResourceManager.h:582
Definition: LightingShaderResourceManager.h:541
Definition: LightingShaderResourceManager.h:535
Plane planes[4]
Definition: LightingShaderResourceManager.h:537
Definition: LightingShaderResourceManager.h:743
bool bIsInitialized
Definition: LightingShaderResourceManager.h:816
std::variant< bool, Error > createLightIndexListsAndLightGrid(Renderer *pRenderer, size_t iTileCountX, size_t iTileCountY)
Definition: LightingShaderResourceManager.cpp:890
static const auto sGlobalCountersIntoLightIndexListShaderResourceName
Definition: LightingShaderResourceManager.h:834
ShaderResources resources
Definition: LightingShaderResourceManager.h:810
ThreadGroupCount threadGroupCount
Definition: LightingShaderResourceManager.h:813
std::optional< Error > initialize(Renderer *pRenderer, const FrustumGridComputeShader::ComputeShader &frustumGridShader)
Definition: LightingShaderResourceManager.cpp:802
std::optional< Error > queueExecutionForNextFrame(Renderer *pRenderer, FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, GpuResource *pGeneralLightingData, GpuResource *pPointLightArray, GpuResource *pSpotlightArray, GpuResource *pNonCulledPointLightsIndicesArray, GpuResource *pNonCulledSpotlightsIndicesArray) const
Definition: LightingShaderResourceManager.cpp:1044
static const auto sThreadGroupCountShaderResourceName
Definition: LightingShaderResourceManager.h:828
static const auto sDepthTextureShaderResourceName
Definition: LightingShaderResourceManager.h:822
std::unique_ptr< ComputeShaderInterface > pComputeInterface
Definition: LightingShaderResourceManager.h:807
Definition: LightingShaderResourceManager.h:657
unsigned int iPointLightListOpaque
Definition: LightingShaderResourceManager.h:659
unsigned int iSpotlightListTransparent
Definition: LightingShaderResourceManager.h:668
unsigned int iSpotlightListOpaque
Definition: LightingShaderResourceManager.h:662
unsigned int iPointLightListTransparent
Definition: LightingShaderResourceManager.h:665
Definition: LightingShaderResourceManager.h:683
std::unique_ptr< GpuResource > pOpaqueSpotLightGrid
Definition: LightingShaderResourceManager.h:715
size_t iLightGridTileCountY
Definition: LightingShaderResourceManager.h:739
std::unique_ptr< UploadBuffer > pThreadGroupCount
Definition: LightingShaderResourceManager.h:688
std::unique_ptr< GpuResource > pTransparentPointLightGrid
Definition: LightingShaderResourceManager.h:721
std::unique_ptr< GpuResource > pTransparentSpotLightIndexList
Definition: LightingShaderResourceManager.h:703
size_t iLightGridTileCountX
Definition: LightingShaderResourceManager.h:733
std::unique_ptr< GpuResource > pOpaquePointLightIndexList
Definition: LightingShaderResourceManager.h:694
std::unique_ptr< GpuResource > pGlobalCountersIntoLightIndexList
Definition: LightingShaderResourceManager.h:691
std::unique_ptr< GpuResource > pOpaquePointLightGrid
Definition: LightingShaderResourceManager.h:709
std::unique_ptr< GpuResource > pOpaqueSpotLightIndexList
Definition: LightingShaderResourceManager.h:697
std::unique_ptr< GpuResource > pTransparentPointLightIndexList
Definition: LightingShaderResourceManager.h:700
std::unique_ptr< GpuResource > pTransparentSpotLightGrid
Definition: LightingShaderResourceManager.h:727
Definition: LightingShaderResourceManager.h:674
unsigned int iThreadGroupCountY
Definition: LightingShaderResourceManager.h:679
unsigned int iThreadGroupCountX
Definition: LightingShaderResourceManager.h:676
Definition: LightingShaderResourceManager.h:655
Definition: LightingShaderResourceManager.h:522
glm::vec3 normal
Definition: LightingShaderResourceManager.h:524
float distanceFromOrigin
Definition: LightingShaderResourceManager.h:527
Definition: LightingShaderResourceManager.h:516
Definition: LightingShaderResourceManager.h:56
glm::vec4 ambientLight
Definition: LightingShaderResourceManager.h:58
unsigned int iPointLightCountInCameraFrustum
Definition: LightingShaderResourceManager.h:61
unsigned int iSpotLightCountInCameraFrustum
Definition: LightingShaderResourceManager.h:67
unsigned int iDirectionalLightCount
Definition: LightingShaderResourceManager.h:64
Definition: LightingShaderResourceManager.h:71
std::array< std::unique_ptr< UploadBuffer >, FrameResourceManager::getFrameResourceCount()> vGeneralDataGpuResources
Definition: LightingShaderResourceManager.h:76
GeneralLightingShaderData generalData
Definition: LightingShaderResourceManager.h:83
Definition: LightingShaderResourceManager.h:501
std::unique_ptr< ShaderLightArray > pShadowPassLightInfoArray
Definition: LightingShaderResourceManager.h:512
std::unique_ptr< ShaderLightArray > pSpotlightDataArray
Definition: LightingShaderResourceManager.h:509
std::unique_ptr< ShaderLightArray > pDirectionalLightDataArray
Definition: LightingShaderResourceManager.h:506
std::unique_ptr< ShaderLightArray > pPointLightDataArray
Definition: LightingShaderResourceManager.h:503
Definition: LightingShaderResourceManager.h:38
glm::vec4 position
Definition: LightingShaderResourceManager.h:43
glm::mat4 viewProjectionMatrix
Definition: LightingShaderResourceManager.h:40