From bfa8b17eeac0a943965726623a63f89c3f65b09d Mon Sep 17 00:00:00 2001 From: Sandilya Jandhyala Date: Tue, 28 Oct 2025 17:25:32 +0000 Subject: [PATCH 1/2] Add copilot instructions --- .github/copilot-instructions.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .github/copilot-instructions.md diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..1b10921 --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,2 @@ +- Do not add explanatory comments to generated code unless prompted to do so. +- When asked to generate code in a `.cpp` file, make sure to make necessary changes to the corresponding `.h` file as applicable. From db6fcaf315d8cdda3480559cd07d5f278559e92e Mon Sep 17 00:00:00 2001 From: Sandilya Jandhyala Date: Tue, 28 Oct 2025 17:25:49 +0000 Subject: [PATCH 2/2] Move some resources to bindless --- Core/GraphicsMemoryManager.cpp | 6 +++ Core/GraphicsMemoryManager.h | 7 +-- Core/Pipelines/BillboardPipeline.cpp | 56 +++++++++-------------- Core/Pipelines/BillboardPipeline.h | 14 +++++- Core/Pipelines/InstancedPBRPipeline.cpp | 39 ++++++++-------- Core/Pipelines/InstancedPBRPipeline.h | 14 +++++- Core/Pipelines/PBRPipeline.cpp | 30 +++++++------ Core/Pipelines/PBRPipeline.h | 14 +++++- Core/RootSignature.cpp | 5 ++- Core/Shaders/Leaf_Masked_PS.hlsl | 53 +++++++++++++--------- Core/Shaders/MaskedDepth_PS.hlsl | 15 +++++-- Core/Shaders/PBR_Masked_PS.hlsl | 59 ++++++++++++++++--------- Core/Shaders/pbr_ps.hlsl | 40 +++++++++++------ 13 files changed, 219 insertions(+), 133 deletions(-) diff --git a/Core/GraphicsMemoryManager.cpp b/Core/GraphicsMemoryManager.cpp index 4e771ee..eb3b083 100644 --- a/Core/GraphicsMemoryManager.cpp +++ b/Core/GraphicsMemoryManager.cpp @@ -286,4 +286,10 @@ namespace Gradient return gmm->GetSRVOrUAVGpuHandle(m_index); } + + uint32_t GraphicsMemoryManager::DescriptorIndexContainer::GetIndex() + { + assert(m_index <= UINT32_MAX); + return static_cast(m_index); + } } \ No newline at end of file diff --git a/Core/GraphicsMemoryManager.h b/Core/GraphicsMemoryManager.h index a97cc0e..8da6338 100644 --- a/Core/GraphicsMemoryManager.h +++ b/Core/GraphicsMemoryManager.h @@ -13,10 +13,6 @@ namespace Gradient class GraphicsMemoryManager { public: - // TODO: Make these things free themselves. - // The descriptor heap runs out of space whenever the - // window is resized because the old descriptors are - // never reclaimed. using DescriptorIndex = DirectX::DescriptorPile::IndexType; enum class DescriptorIndexType @@ -32,7 +28,8 @@ namespace Gradient DescriptorIndexContainer(const DescriptorIndexContainer&&) = delete; D3D12_CPU_DESCRIPTOR_HANDLE GetCPUHandle(); - D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle(); + D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle(); + uint32_t GetIndex(); ~DescriptorIndexContainer(); diff --git a/Core/Pipelines/BillboardPipeline.cpp b/Core/Pipelines/BillboardPipeline.cpp index c9e2859..ae78984 100644 --- a/Core/Pipelines/BillboardPipeline.cpp +++ b/Core/Pipelines/BillboardPipeline.cpp @@ -23,17 +23,11 @@ namespace Gradient::Pipelines m_rootSignature.AddCBV(1, 0); m_rootSignature.AddCBV(0, 1); m_rootSignature.AddCBV(1, 1); - - m_rootSignature.AddRootSRV(0, 0); // instance data - m_rootSignature.AddSRV(0, 1); + m_rootSignature.AddCBV(2, 1); // Material + m_rootSignature.AddRootSRV(0, 0); // instance data m_rootSignature.AddSRV(1, 1); - m_rootSignature.AddSRV(2, 1); - m_rootSignature.AddSRV(3, 1); - m_rootSignature.AddSRV(4, 1); - m_rootSignature.AddSRV(5, 1); m_rootSignature.AddSRV(6, 1); m_rootSignature.AddSRV(7, 1); - m_rootSignature.AddSRV(8, 1); m_rootSignature.AddStaticSampler( CD3DX12_STATIC_SAMPLER_DESC(0, D3D12_FILTER_ANISOTROPIC), @@ -145,7 +139,8 @@ namespace Gradient::Pipelines m_rootSignature.SetCBV(cl, 0, 0, vertexConstants); m_rootSignature.SetStructuredBufferSRV(cl, 0, 0, InstanceHandle); - m_rootSignature.SetSRV(cl, 0, 1, Material.Texture); + + SetMaterialCB(cl); DrawParamsCB drawConstants; drawConstants.cameraPosition = CameraPosition; @@ -154,14 +149,7 @@ namespace Gradient::Pipelines drawConstants.cardHeight = CardDimensions.y; drawConstants.numInstances = InstanceCount; drawConstants.totalTimeSeconds = TotalTimeSeconds; - if (UsingOrthographic) - { - drawConstants.useCameraDirectionForCulling = 1; - } - else - { - drawConstants.useCameraDirectionForCulling = 0; - } + drawConstants.useCameraDirectionForCulling = UsingOrthographic ? 1u : 0u; for (int i = 0; i < 6; i++) { drawConstants.cullingFrustumPlanes[i] = CullingFrustumPlanes[i]; @@ -182,14 +170,13 @@ namespace Gradient::Pipelines } else if (passType == DrawType::PixelDepthReadOnly) { - m_maskedPixelDepthReadPSO->Set(cl, multisampled); + m_maskedPixelDepthReadPSO->Set(cl, multisampled); } else if (passType == DrawType::PixelDepthReadWrite) { - m_maskedPixelDepthReadWritePSO->Set(cl, multisampled); + m_maskedPixelDepthReadWritePSO->Set(cl, multisampled); } - m_rootSignature.SetOnCommandList(cl); MatrixCB vertexConstants; @@ -206,14 +193,7 @@ namespace Gradient::Pipelines drawConstants.cardHeight = CardDimensions.y; drawConstants.numInstances = InstanceCount; drawConstants.totalTimeSeconds = TotalTimeSeconds; - if (UsingOrthographic) - { - drawConstants.useCameraDirectionForCulling = 1; - } - else - { - drawConstants.useCameraDirectionForCulling = 0; - } + drawConstants.useCameraDirectionForCulling = UsingOrthographic ? 1u : 0u; for (int i = 0; i < 6; i++) { drawConstants.cullingFrustumPlanes[i] = CullingFrustumPlanes[i]; @@ -251,18 +231,15 @@ namespace Gradient::Pipelines pixelConstants.tiling = Material.Tiling; pixelConstants.shadowTransform = DirectX::XMMatrixTranspose(SunlightParams.ShadowTransform); pixelConstants.emissiveRadiance = Material.EmissiveRadiance; + pixelConstants.SSAOMapIndex = GTAOTexture ? GTAOTexture->GetIndex() : 0u; m_rootSignature.SetCBV(cl, 1, 1, pixelConstants); - m_rootSignature.SetSRV(cl, 0, 1, Material.Texture); + SetMaterialCB(cl); + m_rootSignature.SetSRV(cl, 1, 1, SunlightParams.ShadowMap); - m_rootSignature.SetSRV(cl, 2, 1, Material.NormalMap); - m_rootSignature.SetSRV(cl, 3, 1, Material.AOMap); - m_rootSignature.SetSRV(cl, 4, 1, Material.MetalnessMap); - m_rootSignature.SetSRV(cl, 5, 1, Material.RoughnessMap); m_rootSignature.SetSRV(cl, 6, 1, EnvironmentMap); m_rootSignature.SetSRV(cl, 7, 1, ShadowCubeArray); - m_rootSignature.SetSRV(cl, 8, 1, GTAOTexture); cl->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); } @@ -276,4 +253,15 @@ namespace Gradient::Pipelines SunlightParams.LightIrradiance = dlight->GetIrradiance(); SunlightParams.LightDirection = dlight->GetDirection(); } + + void BillboardPipeline::SetMaterialCB(ID3D12GraphicsCommandList* cl) + { + BillboardPipeline::MaterialCB materialConstants{}; + materialConstants.AlbedoMapIndex = Material.Texture ? Material.Texture->GetIndex() : 0u; + materialConstants.NormalMapIndex = Material.NormalMap ? Material.NormalMap->GetIndex() : 0u; + materialConstants.MetallicMapIndex = Material.MetalnessMap ? Material.MetalnessMap->GetIndex() : 0u; + materialConstants.RoughnessMapIndex = Material.RoughnessMap ? Material.RoughnessMap->GetIndex() : 0u; + materialConstants.AOMapIndex = Material.AOMap ? Material.AOMap->GetIndex() : 0u; + m_rootSignature.SetCBV(cl, 2, 1, materialConstants); + } } diff --git a/Core/Pipelines/BillboardPipeline.h b/Core/Pipelines/BillboardPipeline.h index f442534..5d5893f 100644 --- a/Core/Pipelines/BillboardPipeline.h +++ b/Core/Pipelines/BillboardPipeline.h @@ -48,7 +48,7 @@ namespace Gradient::Pipelines DirectX::XMFLOAT3 cameraPosition; float tiling; DirectX::XMFLOAT3 emissiveRadiance; - float pad2; + uint32_t SSAOMapIndex; DirectX::XMMATRIX shadowTransform; }; @@ -59,6 +59,16 @@ namespace Gradient::Pipelines uint32_t numPointLights; }; + struct __declspec(align(16)) MaterialCB + { + uint32_t AlbedoMapIndex; + uint32_t NormalMapIndex; + uint32_t MetallicMapIndex; + uint32_t RoughnessMapIndex; + uint32_t AOMapIndex; + DirectX::XMFLOAT3 _padding[3]; + }; + using VertexType = DirectX::VertexPositionNormalTexture; explicit BillboardPipeline(ID3D12Device2* device); @@ -115,6 +125,8 @@ namespace Gradient::Pipelines void InitializeDepthWritePSO(ID3D12Device2* device); void ApplyDepthOnlyPipeline(ID3D12GraphicsCommandList* cl, bool multisampled, DrawType passType); + void SetMaterialCB(ID3D12GraphicsCommandList* cl); + RootSignature m_rootSignature; // All billboards are assumed to be masked diff --git a/Core/Pipelines/InstancedPBRPipeline.cpp b/Core/Pipelines/InstancedPBRPipeline.cpp index 5dad024..ba2bed5 100644 --- a/Core/Pipelines/InstancedPBRPipeline.cpp +++ b/Core/Pipelines/InstancedPBRPipeline.cpp @@ -22,17 +22,11 @@ namespace Gradient::Pipelines m_rootSignature.AddCBV(0, 0); m_rootSignature.AddCBV(0, 1); m_rootSignature.AddCBV(1, 1); - - m_rootSignature.AddRootSRV(0, 0); // instance data - m_rootSignature.AddSRV(0, 1); - m_rootSignature.AddSRV(1, 1); - m_rootSignature.AddSRV(2, 1); - m_rootSignature.AddSRV(3, 1); - m_rootSignature.AddSRV(4, 1); - m_rootSignature.AddSRV(5, 1); - m_rootSignature.AddSRV(6, 1); - m_rootSignature.AddSRV(7, 1); - m_rootSignature.AddSRV(8, 1); + m_rootSignature.AddCBV(2, 1); // Material + m_rootSignature.AddRootSRV(0, 0); // instance data (root SRV) + m_rootSignature.AddSRV(1, 1); // shadow map + m_rootSignature.AddSRV(6, 1); // environment map + m_rootSignature.AddSRV(7, 1); // point shadow maps m_rootSignature.AddStaticSampler( CD3DX12_STATIC_SAMPLER_DESC(0, D3D12_FILTER_ANISOTROPIC), @@ -179,7 +173,8 @@ namespace Gradient::Pipelines m_rootSignature.SetCBV(cl, 0, 0, vertexConstants); m_rootSignature.SetStructuredBufferSRV(cl, 0, 0, m_instanceHandle); - m_rootSignature.SetSRV(cl, 0, 1, m_material.Texture); + + SetMaterialCB(cl); cl->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); } @@ -253,22 +248,30 @@ namespace Gradient::Pipelines pixelConstants.tiling = m_material.Tiling; pixelConstants.shadowTransform = DirectX::XMMatrixTranspose(m_shadowTransform); pixelConstants.emissiveRadiance = m_material.EmissiveRadiance; + pixelConstants.SSAOMapIndex = GTAOTexture->GetIndex(); m_rootSignature.SetCBV(cl, 1, 1, pixelConstants); - m_rootSignature.SetSRV(cl, 0, 1, m_material.Texture); + SetMaterialCB(cl); + m_rootSignature.SetSRV(cl, 1, 1, m_shadowMap); - m_rootSignature.SetSRV(cl, 2, 1, m_material.NormalMap); - m_rootSignature.SetSRV(cl, 3, 1, m_material.AOMap); - m_rootSignature.SetSRV(cl, 4, 1, m_material.MetalnessMap); - m_rootSignature.SetSRV(cl, 5, 1, m_material.RoughnessMap); m_rootSignature.SetSRV(cl, 6, 1, m_environmentMap); m_rootSignature.SetSRV(cl, 7, 1, m_shadowCubeArray); - m_rootSignature.SetSRV(cl, 8, 1, GTAOTexture); cl->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); } + void InstancedPBRPipeline::SetMaterialCB(ID3D12GraphicsCommandList* cl) + { + MaterialCB materialConstants{}; + materialConstants.AlbedoMapIndex = m_material.Texture ? m_material.Texture->GetIndex() : 0u; + materialConstants.NormalMapIndex = m_material.NormalMap ? m_material.NormalMap->GetIndex() : 0u; + materialConstants.MetallicMapIndex = m_material.MetalnessMap ? m_material.MetalnessMap->GetIndex() : 0u; + materialConstants.RoughnessMapIndex = m_material.RoughnessMap ? m_material.RoughnessMap->GetIndex() : 0u; + materialConstants.AOMapIndex = m_material.AOMap ? m_material.AOMap->GetIndex() : 0u; + m_rootSignature.SetCBV(cl, 2, 1, materialConstants); + } + void InstancedPBRPipeline::SetMaterial(const Rendering::PBRMaterial& material) { m_material = material; diff --git a/Core/Pipelines/InstancedPBRPipeline.h b/Core/Pipelines/InstancedPBRPipeline.h index 39f5fbc..d031acb 100644 --- a/Core/Pipelines/InstancedPBRPipeline.h +++ b/Core/Pipelines/InstancedPBRPipeline.h @@ -32,7 +32,7 @@ namespace Gradient::Pipelines DirectX::XMFLOAT3 cameraPosition; float tiling; DirectX::XMFLOAT3 emissiveRadiance; - float pad2; + float SSAOMapIndex; DirectX::XMMATRIX shadowTransform; }; @@ -43,6 +43,16 @@ namespace Gradient::Pipelines uint32_t numPointLights; }; + struct __declspec(align(16)) MaterialCB + { + uint32_t AlbedoMapIndex; + uint32_t NormalMapIndex; + uint32_t MetallicMapIndex; + uint32_t RoughnessMapIndex; + uint32_t AOMapIndex; + DirectX::XMFLOAT3 _padding[3]; + }; + using VertexType = DirectX::VertexPositionNormalTexture; explicit InstancedPBRPipeline(ID3D12Device2* device); @@ -76,6 +86,8 @@ namespace Gradient::Pipelines void InitializeDepthWritePSO(ID3D12Device2* device); void ApplyDepthOnlyPipeline(ID3D12GraphicsCommandList* cl, bool multisampled, DrawType passType); + void SetMaterialCB(ID3D12GraphicsCommandList* cl); + RootSignature m_rootSignature; std::unique_ptr m_unmaskedShadowPipelineState; std::unique_ptr m_unmaskedDepthWriteOnlyPSO; diff --git a/Core/Pipelines/PBRPipeline.cpp b/Core/Pipelines/PBRPipeline.cpp index 8651732..ee93697 100644 --- a/Core/Pipelines/PBRPipeline.cpp +++ b/Core/Pipelines/PBRPipeline.cpp @@ -22,16 +22,10 @@ namespace Gradient::Pipelines m_rootSignature.AddCBV(0, 0); m_rootSignature.AddCBV(0, 1); m_rootSignature.AddCBV(1, 1); - - m_rootSignature.AddSRV(0, 1); + m_rootSignature.AddCBV(2, 1); // Material m_rootSignature.AddSRV(1, 1); - m_rootSignature.AddSRV(2, 1); - m_rootSignature.AddSRV(3, 1); - m_rootSignature.AddSRV(4, 1); - m_rootSignature.AddSRV(5, 1); m_rootSignature.AddSRV(6, 1); m_rootSignature.AddSRV(7, 1); - m_rootSignature.AddSRV(8, 1); m_rootSignature.AddStaticSampler( CD3DX12_STATIC_SAMPLER_DESC(0, D3D12_FILTER_ANISOTROPIC), @@ -179,6 +173,8 @@ namespace Gradient::Pipelines m_rootSignature.SetCBV(cl, 0, 0, vertexConstants); + SetMaterialCB(cl); + cl->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); } @@ -250,18 +246,15 @@ namespace Gradient::Pipelines pixelConstants.tiling = m_material.Tiling; pixelConstants.shadowTransform = DirectX::XMMatrixTranspose(m_shadowTransform); pixelConstants.emissiveRadiance = m_material.EmissiveRadiance; + pixelConstants.SSAOMapIndex = GTAOTexture->GetIndex(); m_rootSignature.SetCBV(cl, 1, 1, pixelConstants); - m_rootSignature.SetSRV(cl, 0, 1, m_material.Texture); + SetMaterialCB(cl); + m_rootSignature.SetSRV(cl, 1, 1, m_shadowMap); - m_rootSignature.SetSRV(cl, 2, 1, m_material.NormalMap); - m_rootSignature.SetSRV(cl, 3, 1, m_material.AOMap); - m_rootSignature.SetSRV(cl, 4, 1, m_material.MetalnessMap); - m_rootSignature.SetSRV(cl, 5, 1, m_material.RoughnessMap); m_rootSignature.SetSRV(cl, 6, 1, m_environmentMap); m_rootSignature.SetSRV(cl, 7, 1, m_shadowCubeArray); - m_rootSignature.SetSRV(cl, 8, 1, GTAOTexture); cl->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); } @@ -322,4 +315,15 @@ namespace Gradient::Pipelines { m_cameraPosition = cameraPosition; } + + void PBRPipeline::SetMaterialCB(ID3D12GraphicsCommandList* cl) + { + MaterialCB materialConstants{}; + materialConstants.AlbedoMapIndex = m_material.Texture ? m_material.Texture->GetIndex() : 0u; + materialConstants.NormalMapIndex = m_material.NormalMap ? m_material.NormalMap->GetIndex() : 0u; + materialConstants.MetallicMapIndex = m_material.MetalnessMap ? m_material.MetalnessMap->GetIndex() : 0u; + materialConstants.RoughnessMapIndex = m_material.RoughnessMap ? m_material.RoughnessMap->GetIndex() : 0u; + materialConstants.AOMapIndex = m_material.AOMap ? m_material.AOMap->GetIndex() : 0u; + m_rootSignature.SetCBV(cl, 2, 1, materialConstants); + } } diff --git a/Core/Pipelines/PBRPipeline.h b/Core/Pipelines/PBRPipeline.h index bcd3f5e..c3e7e89 100644 --- a/Core/Pipelines/PBRPipeline.h +++ b/Core/Pipelines/PBRPipeline.h @@ -31,7 +31,7 @@ namespace Gradient::Pipelines DirectX::XMFLOAT3 cameraPosition; float tiling; DirectX::XMFLOAT3 emissiveRadiance; - float pad2; + uint32_t SSAOMapIndex; DirectX::XMMATRIX shadowTransform; }; @@ -42,6 +42,16 @@ namespace Gradient::Pipelines uint32_t numPointLights; }; + struct __declspec(align(16)) MaterialCB + { + uint32_t AlbedoMapIndex; + uint32_t NormalMapIndex; + uint32_t MetallicMapIndex; + uint32_t RoughnessMapIndex; + uint32_t AOMapIndex; + DirectX::XMFLOAT3 _padding[3]; + }; + using VertexType = DirectX::VertexPositionNormalTexture; explicit PBRPipeline(ID3D12Device2* device); @@ -75,6 +85,8 @@ namespace Gradient::Pipelines bool multisampled, DrawType passType); + void SetMaterialCB(ID3D12GraphicsCommandList* cl); + RootSignature m_rootSignature; std::unique_ptr m_unmaskedPipelineState; std::unique_ptr m_unmaskedShadowPipelineState; diff --git a/Core/RootSignature.cpp b/Core/RootSignature.cpp index a58c5c7..d45fa4b 100644 --- a/Core/RootSignature.cpp +++ b/Core/RootSignature.cpp @@ -154,7 +154,10 @@ namespace Gradient m_staticSamplers.size(), m_staticSamplers.data()); - rootSig.Desc_1_0.Flags |= D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; + rootSig.Desc_1_0.Flags |= + D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT + | D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED + | D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; using Microsoft::WRL::ComPtr; ComPtr serializedRootSig; diff --git a/Core/Shaders/Leaf_Masked_PS.hlsl b/Core/Shaders/Leaf_Masked_PS.hlsl index ab31592..f4e40ec 100644 --- a/Core/Shaders/Leaf_Masked_PS.hlsl +++ b/Core/Shaders/Leaf_Masked_PS.hlsl @@ -4,15 +4,9 @@ #include "PBRLighting.hlsli" #include "Utils.hlsli" -Texture2D albedoMap : register(t0, space1); Texture2D shadowMap : register(t1, space1); -Texture2D normalMap : register(t2, space1); -Texture2D aoMap : register(t3, space1); -Texture2D metalnessMap : register(t4, space1); -Texture2D roughnessMap : register(t5, space1); TextureCube environmentMap : register(t6, space1); TextureCubeArray pointShadowMaps : register(t7, space1); -Texture2D ssaoMap : register(t8, space1); SamplerState linearSampler : register(s3, space1); SamplerState anisotropicSampler : register(s0, space1); @@ -32,10 +26,19 @@ cbuffer Constants : register(b1, space1) float3 cameraPosition; float uvTiling; float3 emissiveRadiance; - float pad2; + uint g_ssaoMapIndex; float4x4 shadowTransform; // TODO: put this into the light }; +cbuffer Material : register(b2, space1) +{ + uint g_albedoMapIndex; + uint g_normalMapIndex; + uint g_metallicMapIndex; + uint g_roughnessMapIndex; + uint g_aoMapIndex; +}; + struct InputType { float4 position : SV_POSITION; @@ -47,13 +50,20 @@ struct InputType float4 Leaf_Masked_PS(InputType input) : SV_TARGET { input.tex *= uvTiling; - + + // resolve bindless textures + Texture2D albedoMap = ResourceDescriptorHeap[g_albedoMapIndex]; + Texture2D normalMap = ResourceDescriptorHeap[g_normalMapIndex]; + Texture2D metalnessMap = ResourceDescriptorHeap[g_metallicMapIndex]; + Texture2D roughnessMap = ResourceDescriptorHeap[g_roughnessMapIndex]; + Texture2D aoMap = ResourceDescriptorHeap[g_aoMapIndex]; + float4 albedoSample = albedoMap.Sample(anisotropicSampler, input.tex); // This shader supports masking clip(albedoSample.a - 0.01); - + input.normal = normalize(input.normal); - + float3 N = perturbNormal( normalMap, linearSampler, @@ -61,10 +71,11 @@ float4 Leaf_Masked_PS(InputType input) : SV_TARGET input.worldPosition, input.tex); float3 V = normalize(cameraPosition - input.worldPosition); - + float3 albedo = albedoSample.rgb; + Texture2D ssaoMap = ResourceDescriptorHeap[g_ssaoMapIndex]; uint screenWidth, screenHeight; ssaoMap.GetDimensions(screenWidth, screenHeight); uint ssao = ssaoMap.Sample(linearSampler, float2(input.position.x / screenWidth, input.position.y / screenHeight)); @@ -72,26 +83,26 @@ float4 Leaf_Masked_PS(InputType input) : SV_TARGET //ao *= ao; float metalness = metalnessMap.Sample(linearSampler, input.tex).r; float roughness = roughnessMap.Sample(linearSampler, input.tex).r; - + float thickness = 0.01; float sharpness = 1; float refractiveIndex = 1; - - + + float3 directSSS = directionalLightSSS(g_directionalLight, N, V, thickness, sharpness, refractiveIndex) * 0.1 * albedo; - + float3 directRadiance = DirectionalLightContributionWithSSS( N, V, albedo, metalness, roughness, g_directionalLight, shadowMap, shadowMapSampler, shadowTransform, input.worldPosition, directSSS ); float3 pointRadiance = float3(0, 0, 0); - + for (int i = 0; i < g_numPointLights; i++) { float3 pointSSS = pointLightSSS(g_pointLights[i], @@ -101,24 +112,24 @@ float4 Leaf_Masked_PS(InputType input) : SV_TARGET thickness, sharpness, refractiveIndex) * 0.1 * albedo; - + pointRadiance += PointLightContributionWithSSS( N, V, albedo, metalness, roughness, g_pointLights[i], pointShadowMaps, shadowMapSampler, input.worldPosition, pointSSS ); } - + float3 ambient = IndirectLighting( environmentMap, linearSampler, N, V, albedo, ao, metalness, roughness ); - + float3 outputColour = ambient + directRadiance + pointRadiance + emissiveRadiance; - + outputColour = ApplyFog(outputColour, input.worldPosition, cameraPosition); - + return float4(outputColour, albedoSample.a); } \ No newline at end of file diff --git a/Core/Shaders/MaskedDepth_PS.hlsl b/Core/Shaders/MaskedDepth_PS.hlsl index 7abb706..e034d65 100644 --- a/Core/Shaders/MaskedDepth_PS.hlsl +++ b/Core/Shaders/MaskedDepth_PS.hlsl @@ -1,4 +1,12 @@ -Texture2D albedoMap : register(t0, space1); +cbuffer Material : register(b2, space1) +{ + uint g_albedoMapIndex; + uint g_normalMapIndex; + uint g_metallicMapIndex; + uint g_roughnessMapIndex; + uint g_aoMapIndex; +} + SamplerState anisotropicSampler : register(s0, space1); struct InputType @@ -13,8 +21,9 @@ struct InputType // when drawing shadows. float4 MaskedDepth_PS(InputType input) : SV_Target { + Texture2D albedoMap = ResourceDescriptorHeap[g_albedoMapIndex]; float4 albedoSample = albedoMap.Sample(anisotropicSampler, input.tex); clip(albedoSample.a - 0.01); - - return (0, 0, 0, albedoSample.a); + + return float4(0, 0, 0, albedoSample.a); } \ No newline at end of file diff --git a/Core/Shaders/PBR_Masked_PS.hlsl b/Core/Shaders/PBR_Masked_PS.hlsl index ffef28f..d02e80d 100644 --- a/Core/Shaders/PBR_Masked_PS.hlsl +++ b/Core/Shaders/PBR_Masked_PS.hlsl @@ -4,15 +4,9 @@ #include "PBRLighting.hlsli" #include "Utils.hlsli" -Texture2D albedoMap : register(t0, space1); Texture2D shadowMap : register(t1, space1); -Texture2D normalMap : register(t2, space1); -Texture2D aoMap : register(t3, space1); -Texture2D metalnessMap : register(t4, space1); -Texture2D roughnessMap : register(t5, space1); TextureCube environmentMap : register(t6, space1); TextureCubeArray pointShadowMaps : register(t7, space1); -Texture2D ssaoMap : register(t8, space1); SamplerState linearSampler : register(s3, space1); SamplerState anisotropicSampler : register(s0, space1); @@ -32,10 +26,19 @@ cbuffer Constants : register(b1, space1) float3 cameraPosition; float uvTiling; float3 emissiveRadiance; - float pad2; + uint g_ssaoMapIndex; // bindless index for SSAO map float4x4 shadowTransform; // TODO: put this into the light }; +cbuffer Material : register(b2, space1) +{ + uint g_albedoMapIndex; + uint g_normalMapIndex; + uint g_metallicMapIndex; + uint g_roughnessMapIndex; + uint g_aoMapIndex; +}; + struct InputType { float4 position : SV_POSITION; @@ -44,16 +47,32 @@ struct InputType float3 worldPosition : POSITION1; }; +float SampleSSAO(Texture2D ssaoMap, float2 pixelPosition) +{ + uint screenWidth, screenHeight; + ssaoMap.GetDimensions(screenWidth, screenHeight); + uint ssao = ssaoMap.Sample(linearSampler, float2(pixelPosition.x / screenWidth, pixelPosition.y / screenHeight)); + + return ssao / 255.f; +} + float4 PBR_Masked_PS(InputType input) : SV_TARGET { input.tex *= uvTiling; - + + // Resolve bindless resources from descriptor heap + Texture2D albedoMap = ResourceDescriptorHeap[g_albedoMapIndex]; + Texture2D normalMap = ResourceDescriptorHeap[g_normalMapIndex]; + Texture2D metalnessMap = ResourceDescriptorHeap[g_metallicMapIndex]; + Texture2D roughnessMap = ResourceDescriptorHeap[g_roughnessMapIndex]; + Texture2D aoMap = ResourceDescriptorHeap[g_aoMapIndex]; + float4 albedoSample = albedoMap.Sample(anisotropicSampler, input.tex); // This shader supports masking clip(albedoSample.a - 0.01); - + input.normal = normalize(input.normal); - + float3 N = perturbNormal( normalMap, linearSampler, @@ -61,23 +80,19 @@ float4 PBR_Masked_PS(InputType input) : SV_TARGET input.worldPosition, input.tex); float3 V = normalize(cameraPosition - input.worldPosition); - float3 albedo = albedoSample.rgb; - uint screenWidth, screenHeight; - ssaoMap.GetDimensions(screenWidth, screenHeight); - uint ssao = ssaoMap.Sample(linearSampler, float2(input.position.x / screenWidth, input.position.y / screenHeight)); - float ao = min(aoMap.Sample(linearSampler, input.tex).r, (ssao / 255.f)); - //ao *= ao; + Texture2D ssaoMap = ResourceDescriptorHeap[g_ssaoMapIndex]; + float ao = min(aoMap.Sample(linearSampler, input.tex).r, SampleSSAO(ssaoMap, input.position.xy)); float metalness = metalnessMap.Sample(linearSampler, input.tex).r; float roughness = roughnessMap.Sample(linearSampler, input.tex).r; - + float3 directRadiance = DirectionalLightContribution( N, V, albedo, metalness, roughness, g_directionalLight, shadowMap, shadowMapSampler, shadowTransform, input.worldPosition ); - + float3 pointRadiance = float3(0, 0, 0); for (int i = 0; i < g_numPointLights; i++) { @@ -86,18 +101,18 @@ float4 PBR_Masked_PS(InputType input) : SV_TARGET pointShadowMaps, shadowMapSampler, input.worldPosition ); } - + float3 ambient = IndirectLighting( environmentMap, linearSampler, N, V, albedo, ao, metalness, roughness ); - + float3 outputColour = ambient + directRadiance + pointRadiance + emissiveRadiance; - + outputColour = ApplyFog(outputColour, input.worldPosition, cameraPosition); - + return float4(outputColour, albedoSample.a); } \ No newline at end of file diff --git a/Core/Shaders/pbr_ps.hlsl b/Core/Shaders/pbr_ps.hlsl index 0eaef7f..5e3cef9 100644 --- a/Core/Shaders/pbr_ps.hlsl +++ b/Core/Shaders/pbr_ps.hlsl @@ -4,15 +4,9 @@ #include "PBRLighting.hlsli" #include "Utils.hlsli" -Texture2D albedoMap : register(t0, space1); Texture2D shadowMap : register(t1, space1); -Texture2D normalMap : register(t2, space1); -Texture2D aoMap : register(t3, space1); -Texture2D metalnessMap : register(t4, space1); -Texture2D roughnessMap : register(t5, space1); TextureCube environmentMap : register(t6, space1); TextureCubeArray pointShadowMaps : register(t7, space1); -Texture2D ssaoMap : register(t8, space1); SamplerState linearSampler : register(s3, space1); SamplerState anisotropicSampler : register(s0, space1); @@ -32,10 +26,19 @@ cbuffer Constants : register(b1, space1) float3 cameraPosition; float uvTiling; float3 emissiveRadiance; - float pad2; + uint g_ssaoMapIndex; // WIP: Bindless stuff float4x4 shadowTransform; // TODO: put this into the light }; +cbuffer Material : register(b2, space1) +{ + uint g_albedoMapIndex; + uint g_normalMapIndex; + uint g_metallicMapIndex; + uint g_roughnessMapIndex; + uint g_aoMapIndex; +}; + struct InputType { float4 position : SV_POSITION; @@ -44,9 +47,24 @@ struct InputType float3 worldPosition : POSITION1; }; +float SampleSSAO(Texture2D ssaoMap, float2 pixelPosition) +{ + uint screenWidth, screenHeight; + ssaoMap.GetDimensions(screenWidth, screenHeight); + uint ssao = ssaoMap.Sample(linearSampler, float2(pixelPosition.x / screenWidth, pixelPosition.y / screenHeight)); + + return ssao / 255.f; +} + float4 PBR_PS(InputType input) : SV_TARGET { input.tex *= uvTiling; + + Texture2D albedoMap = ResourceDescriptorHeap[g_albedoMapIndex]; + Texture2D normalMap = ResourceDescriptorHeap[g_normalMapIndex]; + Texture2D metalnessMap = ResourceDescriptorHeap[g_metallicMapIndex]; + Texture2D roughnessMap = ResourceDescriptorHeap[g_roughnessMapIndex]; + Texture2D aoMap = ResourceDescriptorHeap[g_aoMapIndex]; float4 albedoSample = albedoMap.Sample(anisotropicSampler, input.tex); @@ -60,14 +78,10 @@ float4 PBR_PS(InputType input) : SV_TARGET input.tex); float3 V = normalize(cameraPosition - input.worldPosition); - float3 albedo = albedoSample.rgb; - uint screenWidth, screenHeight; - ssaoMap.GetDimensions(screenWidth, screenHeight); - uint ssao = ssaoMap.Sample(linearSampler, float2(input.position.x / screenWidth, input.position.y / screenHeight)); - float ao = min(aoMap.Sample(linearSampler, input.tex).r, (ssao / 255.f)); - //ao *= ao; + Texture2D ssaoMap = ResourceDescriptorHeap[g_ssaoMapIndex]; + float ao = min(aoMap.Sample(linearSampler, input.tex).r, SampleSSAO(ssaoMap, input.position.xy)); float metalness = metalnessMap.Sample(linearSampler, input.tex).r; float roughness = roughnessMap.Sample(linearSampler, input.tex).r;