From 30c8d8c6725b781cd6865d4c16af68fe0258a2ea Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 29 Oct 2023 21:24:47 -0700 Subject: [PATCH] Env map renderer: added option to provide custom PS main function --- Components/interface/EnvMapRenderer.hpp | 3 +++ Components/src/EnvMapRenderer.cpp | 33 +++++++++++++++++++++++-- PBR/src/PBR_Renderer.cpp | 1 - Shaders/Common/private/EnvMap.psh | 19 +++++++++----- shaders_inc/EnvMap.psh.h | 19 +++++++++----- 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/Components/interface/EnvMapRenderer.hpp b/Components/interface/EnvMapRenderer.hpp index c665b6b9..0233df0c 100644 --- a/Components/interface/EnvMapRenderer.hpp +++ b/Components/interface/EnvMapRenderer.hpp @@ -55,6 +55,8 @@ class EnvMapRenderer Uint8 NumRenderTargets = 1; TEXTURE_FORMAT RTVFormats[DILIGENT_MAX_RENDER_TARGETS] = {TEX_FORMAT_RGBA8_UNORM_SRGB}; TEXTURE_FORMAT DSVFormat = TEX_FORMAT_D32_FLOAT; + + const char* PSMainSource = nullptr; }; EnvMapRenderer(const CreateInfo& CI); @@ -106,6 +108,7 @@ class EnvMapRenderer const std::vector m_RTVFormats; const TEXTURE_FORMAT m_DSVFormat; + const std::string m_PSMainSource; std::unordered_map, PSOKey::Hasher> m_PSOs; diff --git a/Components/src/EnvMapRenderer.cpp b/Components/src/EnvMapRenderer.cpp index e931f3f1..13565c15 100644 --- a/Components/src/EnvMapRenderer.cpp +++ b/Components/src/EnvMapRenderer.cpp @@ -32,6 +32,7 @@ #include "CommonlyUsedStates.h" #include "MapHelper.hpp" #include "GraphicsUtilities.h" +#include "ShaderSourceFactoryUtils.h" namespace Diligent { @@ -61,7 +62,8 @@ EnvMapRenderer::EnvMapRenderer(const CreateInfo& CI) : m_pStateCache{CI.pStateCache}, m_pCameraAttribsCB{CI.pCameraAttribsCB}, m_RTVFormats{CI.RTVFormats, CI.RTVFormats + CI.NumRenderTargets}, - m_DSVFormat{CI.DSVFormat} + m_DSVFormat{CI.DSVFormat}, + m_PSMainSource{CI.PSMainSource != nullptr ? CI.PSMainSource : ""} { DEV_CHECK_ERR(m_pDevice != nullptr, "Device must not be null"); DEV_CHECK_ERR(m_pCameraAttribsCB != nullptr, "Camera Attribs CB must not be null"); @@ -70,6 +72,16 @@ EnvMapRenderer::EnvMapRenderer(const CreateInfo& CI) : VERIFY_EXPR(m_RenderAttribsCB != nullptr); } +static constexpr char DefaultPSMain[] = R"( +void main(in float4 Pos : SV_Position, + in float4 ClipPos : CLIP_POS, + out float4 Color : SV_Target) +{ + Color.rgb = SampleEnvMap(ClipPos); + Color.a = 1.0; +} +)"; + IPipelineState* EnvMapRenderer::GetPSO(const PSOKey& Key) { auto it = m_PSOs.find(Key); @@ -78,9 +90,26 @@ IPipelineState* EnvMapRenderer::GetPSO(const PSOKey& Key) RenderDeviceWithCache_N Device{m_pDevice, m_pStateCache}; + std::string PSMainSource = m_PSMainSource; + if (PSMainSource.empty()) + PSMainSource = DefaultPSMain; + + MemoryShaderSourceFileInfo GeneratedSources[] = + { + MemoryShaderSourceFileInfo{"PSMainGenerated.generated", PSMainSource}, + }; + MemoryShaderSourceFactoryCreateInfo MemorySourceFactoryCI{GeneratedSources, _countof(GeneratedSources)}; + RefCntAutoPtr pMemorySourceFactory; + CreateMemoryShaderSourceFactory(MemorySourceFactoryCI, &pMemorySourceFactory); + + IShaderSourceInputStreamFactory* ppShaderSourceFactories[] = {&DiligentFXShaderSourceStreamFactory::GetInstance(), pMemorySourceFactory}; + CompoundShaderSourceFactoryCreateInfo CompoundSourceFactoryCI{ppShaderSourceFactories, _countof(ppShaderSourceFactories)}; + RefCntAutoPtr pCompoundSourceFactory; + CreateCompoundShaderSourceFactory(CompoundSourceFactoryCI, &pCompoundSourceFactory); + ShaderCreateInfo ShaderCI; ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL; - ShaderCI.pShaderSourceStreamFactory = &DiligentFXShaderSourceStreamFactory::GetInstance(); + ShaderCI.pShaderSourceStreamFactory = pCompoundSourceFactory; ShaderMacroHelper Macros; Macros diff --git a/PBR/src/PBR_Renderer.cpp b/PBR/src/PBR_Renderer.cpp index b1c012b3..3eb7ad55 100644 --- a/PBR/src/PBR_Renderer.cpp +++ b/PBR/src/PBR_Renderer.cpp @@ -817,7 +817,6 @@ void PBR_Renderer::CreateShaders(PSO_FLAGS PSOFlags, const auto VSOutputStruct = GetVSOutputStruct(PSOFlags); std::string PSMainSource; - if (m_Settings.GetPSMainSource) { PSMainSource = m_Settings.GetPSMainSource(PSOFlags); diff --git a/Shaders/Common/private/EnvMap.psh b/Shaders/Common/private/EnvMap.psh index 9d05980a..ca2c30fa 100644 --- a/Shaders/Common/private/EnvMap.psh +++ b/Shaders/Common/private/EnvMap.psh @@ -19,13 +19,11 @@ cbuffer cbEnvMapRenderAttribs TextureCube EnvMap; SamplerState EnvMap_sampler; -void main(in float4 Pos : SV_Position, - in float4 ClipPos : CLIP_POS, - out float4 Color : SV_Target) +float3 SampleEnvMap(in float4 ClipPos) { - float4 WorldPos = mul(ClipPos, g_CameraAttribs.mViewProjInv); + float4 WorldPos = mul(ClipPos, g_CameraAttribs.mViewProjInv); float3 Direction = WorldPos.xyz / WorldPos.w - g_CameraAttribs.f4Position.xyz; - Color.rgb = EnvMap.SampleLevel(EnvMap_sampler, Direction, MipLevel).rgb; + float3 Color = EnvMap.SampleLevel(EnvMap_sampler, Direction, MipLevel).rgb; #if TONE_MAPPING_MODE > TONE_MAPPING_MODE_NONE Color.rgb = ToneMap(Color.rgb, g_ToneMappingAttribs, AverageLogLum); @@ -35,5 +33,14 @@ void main(in float4 Pos : SV_Position, Color.rgb = pow(Color.rgb, float3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2)); #endif - Color.a = 1.0; + return Color; } + +#include "PSMainGenerated.generated" +// void main(in float4 Pos : SV_Position, +// in float4 ClipPos : CLIP_POS, +// out float4 Color : SV_Target) +// { +// Color.rgb = SampleEnvMap(ClipPos); +// Color.a = 1.0; +// } diff --git a/shaders_inc/EnvMap.psh.h b/shaders_inc/EnvMap.psh.h index e4f91aef..3e30b581 100644 --- a/shaders_inc/EnvMap.psh.h +++ b/shaders_inc/EnvMap.psh.h @@ -19,13 +19,11 @@ "TextureCube EnvMap;\n" "SamplerState EnvMap_sampler;\n" "\n" -"void main(in float4 Pos : SV_Position,\n" -" in float4 ClipPos : CLIP_POS,\n" -" out float4 Color : SV_Target)\n" +"float3 SampleEnvMap(in float4 ClipPos)\n" "{\n" -" float4 WorldPos = mul(ClipPos, g_CameraAttribs.mViewProjInv);\n" +" float4 WorldPos = mul(ClipPos, g_CameraAttribs.mViewProjInv);\n" " float3 Direction = WorldPos.xyz / WorldPos.w - g_CameraAttribs.f4Position.xyz;\n" -" Color.rgb = EnvMap.SampleLevel(EnvMap_sampler, Direction, MipLevel).rgb;\n" +" float3 Color = EnvMap.SampleLevel(EnvMap_sampler, Direction, MipLevel).rgb;\n" "\n" "#if TONE_MAPPING_MODE > TONE_MAPPING_MODE_NONE\n" " Color.rgb = ToneMap(Color.rgb, g_ToneMappingAttribs, AverageLogLum);\n" @@ -35,5 +33,14 @@ " Color.rgb = pow(Color.rgb, float3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));\n" "#endif\n" "\n" -" Color.a = 1.0;\n" +" return Color;\n" "}\n" +"\n" +"#include \"PSMainGenerated.generated\"\n" +"// void main(in float4 Pos : SV_Position,\n" +"// in float4 ClipPos : CLIP_POS,\n" +"// out float4 Color : SV_Target)\n" +"// {\n" +"// Color.rgb = SampleEnvMap(ClipPos);\n" +"// Color.a = 1.0;\n" +"// }\n"