From aaf655d5ecc2c3e687a08269ab8a797314642fa9 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 16 Oct 2023 20:47:36 -0700 Subject: [PATCH] PBR Renderer: added option to output custom data to render target 1 --- PBR/interface/PBR_Renderer.hpp | 10 ++++++---- PBR/src/PBR_Renderer.cpp | 27 ++++++++++++++++++++++++++- Shaders/PBR/private/RenderPBR.psh | 19 +++++++++++++++++-- Shaders/PBR/public/PBR_Structures.fxh | 2 ++ shaders_inc/PBR_Structures.fxh.h | 2 ++ shaders_inc/RenderPBR.psh.h | 19 +++++++++++++++++-- 6 files changed, 70 insertions(+), 9 deletions(-) diff --git a/PBR/interface/PBR_Renderer.hpp b/PBR/interface/PBR_Renderer.hpp index 991ce1e0..aafe8f62 100644 --- a/PBR/interface/PBR_Renderer.hpp +++ b/PBR/interface/PBR_Renderer.hpp @@ -215,11 +215,12 @@ class PBR_Renderer PSO_FLAG_USE_EMISSIVE_MAP = 1u << 11u, PSO_FLAG_USE_IBL = 1u << 12u, - PSO_FLAG_ENABLE_DEBUG_VIEW = 1u << 13u, - PSO_FLAG_USE_TEXTURE_ATLAS = 1u << 14u, - PSO_FLAG_CONVERT_OUTPUT_TO_SRGB = 1u << 15u, + PSO_FLAG_ENABLE_DEBUG_VIEW = 1u << 13u, + PSO_FLAG_USE_TEXTURE_ATLAS = 1u << 14u, + PSO_FLAG_CONVERT_OUTPUT_TO_SRGB = 1u << 15u, + PSO_FLAG_ENABLE_CUSTOM_DATA_OUTPUT = 1u << 16u, - PSO_FLAG_LAST = PSO_FLAG_CONVERT_OUTPUT_TO_SRGB, + PSO_FLAG_LAST = PSO_FLAG_ENABLE_CUSTOM_DATA_OUTPUT, PSO_FLAG_VERTEX_ATTRIBS = PSO_FLAG_USE_VERTEX_COLORS | @@ -389,6 +390,7 @@ class PBR_Renderer bool CreateIfNull); static std::string GetVSOutputStruct(PSO_FLAGS PSOFlags); + static std::string GetPSOutputStruct(PSO_FLAGS PSOFlags); void CreateShaders(PSO_FLAGS PSOFlags, const char* VSPath, diff --git a/PBR/src/PBR_Renderer.cpp b/PBR/src/PBR_Renderer.cpp index 594b777e..4914de1e 100644 --- a/PBR/src/PBR_Renderer.cpp +++ b/PBR/src/PBR_Renderer.cpp @@ -591,6 +591,7 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags) const Macros.Add("USE_HDR_IBL_CUBEMAPS", true); Macros.Add("USE_SEPARATE_METALLIC_ROUGHNESS_TEXTURES", m_Settings.UseSeparateMetallicRoughnessTextures); + static_assert(static_cast(DebugViewType::NumDebugViews) == 18, "Did you add debug view? You may need to handle it here."); // clang-format off Macros.Add("DEBUG_VIEW_NONE", static_cast(DebugViewType::None)); Macros.Add("DEBUG_VIEW_TEXCOORD0", static_cast(DebugViewType::Texcoord0)); @@ -612,6 +613,7 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags) const Macros.Add("DEBUG_VIEW_SPECULAR_IBL", static_cast(DebugViewType::SpecularIBL)); // clang-format on + static_assert(PSO_FLAG_LAST == 1u << 16u, "Did you add new PSO Flag? You may need to handle it here."); #define ADD_PSO_FLAG_MACRO(Flag) Macros.Add(#Flag, (PSOFlags & PSO_FLAG_##Flag) != PSO_FLAG_NONE) ADD_PSO_FLAG_MACRO(USE_VERTEX_COLORS); ADD_PSO_FLAG_MACRO(USE_VERTEX_NORMALS); @@ -632,6 +634,7 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags) const ADD_PSO_FLAG_MACRO(ENABLE_DEBUG_VIEW); ADD_PSO_FLAG_MACRO(USE_TEXTURE_ATLAS); ADD_PSO_FLAG_MACRO(CONVERT_OUTPUT_TO_SRGB); + ADD_PSO_FLAG_MACRO(ENABLE_CUSTOM_DATA_OUTPUT); #undef ADD_PSO_FLAG_MACRO Macros.Add("TEX_COLOR_CONVERSION_MODE_NONE", CreateInfo::TEX_COLOR_CONVERSION_MODE_NONE); @@ -764,6 +767,26 @@ std::string PBR_Renderer::GetVSOutputStruct(PSO_FLAGS PSOFlags) return ss.str(); } +std::string PBR_Renderer::GetPSOutputStruct(PSO_FLAGS PSOFlags) +{ + // struct PSOutput + // { + // float4 Color : SV_Target0; + // float4 CustomData : SV_Target1; + // }; + + std::stringstream ss; + ss << "struct PSOutput" << std::endl + << "{" << std::endl + << " float4 Color : SV_Target0;" << std::endl; + if (PSOFlags & PSO_FLAG_ENABLE_CUSTOM_DATA_OUTPUT) + { + ss << " float4 CustomData : SV_Target1;" << std::endl; + } + ss << "};" << std::endl; + return ss.str(); +} + void PBR_Renderer::CreateShaders(PSO_FLAGS PSOFlags, const char* VSPath, const char* VSName, @@ -776,12 +799,14 @@ void PBR_Renderer::CreateShaders(PSO_FLAGS PSOFlags, std::string VSInputStruct; GetVSInputStructAndLayout(PSOFlags, VSInputStruct, InputLayout); - auto VSOutputStruct = GetVSOutputStruct(PSOFlags); + const auto VSOutputStruct = GetVSOutputStruct(PSOFlags); + const auto PSOutputStruct = GetPSOutputStruct(PSOFlags); MemoryShaderSourceFileInfo GeneratedSources[] = { MemoryShaderSourceFileInfo{"VSInputStruct.generated", VSInputStruct}, MemoryShaderSourceFileInfo{"VSOutputStruct.generated", VSOutputStruct}, + MemoryShaderSourceFileInfo{"PSOutputStruct.generated", PSOutputStruct}, }; MemoryShaderSourceFactoryCreateInfo MemorySourceFactoryCI{GeneratedSources, _countof(GeneratedSources)}; RefCntAutoPtr pMemorySourceFactory; diff --git a/Shaders/PBR/private/RenderPBR.psh b/Shaders/PBR/private/RenderPBR.psh index d1d949c4..3574e7f6 100644 --- a/Shaders/PBR/private/RenderPBR.psh +++ b/Shaders/PBR/private/RenderPBR.psh @@ -17,6 +17,13 @@ // float2 UV1 : UV1; // }; +#include "PSOutputStruct.generated" +// struct PSOutput +// { +// float4 Color : SV_Target0; +// float4 CustomData : SV_Target1; +// }; + #ifndef USE_TEXTURE_ATLAS # define USE_TEXTURE_ATLAS 0 @@ -295,7 +302,7 @@ float4 GetPhysicalDesc(VSOutput VSOut) void main(in VSOutput VSOut, in bool IsFrontFace : SV_IsFrontFace, - out float4 OutColor : SV_Target) + out PSOutput PSOut) { float4 BaseColor = GetBaseColor(VSOut); @@ -422,7 +429,7 @@ void main(in VSOutput VSOut, // Add highlight color color = lerp(color, g_PBRAttribs.Renderer.HighlightColor.rgb, g_PBRAttribs.Renderer.HighlightColor.a); - OutColor = float4(color, BaseColor.a); + float4 OutColor = float4(color, BaseColor.a); # if ENABLE_DEBUG_VIEW // Shader inputs debug visualization @@ -460,4 +467,12 @@ void main(in VSOutput VSOut, OutColor.rgb = FastLinearToSRGB(OutColor.rgb); } # endif + + PSOut.Color = OutColor; + +# if ENABLE_CUSTOM_DATA_OUTPUT + { + PSOut.CustomData = g_PBRAttribs.Renderer.CustomData; + } +# endif } diff --git a/Shaders/PBR/public/PBR_Structures.fxh b/Shaders/PBR/public/PBR_Structures.fxh index de53e29d..e981adc5 100644 --- a/Shaders/PBR/public/PBR_Structures.fxh +++ b/Shaders/PBR/public/PBR_Structures.fxh @@ -63,6 +63,8 @@ struct PBRRendererShaderParameters float4 WireframeColor; float4 HighlightColor; + float4 CustomData; + uint MeshId; uint Padding0; uint Padding1; diff --git a/shaders_inc/PBR_Structures.fxh.h b/shaders_inc/PBR_Structures.fxh.h index 67658c98..4c7309f8 100644 --- a/shaders_inc/PBR_Structures.fxh.h +++ b/shaders_inc/PBR_Structures.fxh.h @@ -63,6 +63,8 @@ " float4 WireframeColor;\n" " float4 HighlightColor;\n" "\n" +" float4 CustomData;\n" +"\n" " uint MeshId;\n" " uint Padding0;\n" " uint Padding1;\n" diff --git a/shaders_inc/RenderPBR.psh.h b/shaders_inc/RenderPBR.psh.h index 207e48a8..72478c2a 100644 --- a/shaders_inc/RenderPBR.psh.h +++ b/shaders_inc/RenderPBR.psh.h @@ -17,6 +17,13 @@ "// float2 UV1 : UV1;\n" "// };\n" "\n" +"#include \"PSOutputStruct.generated\"\n" +"// struct PSOutput\n" +"// {\n" +"// float4 Color : SV_Target0;\n" +"// float4 CustomData : SV_Target1;\n" +"// };\n" +"\n" "\n" "#ifndef USE_TEXTURE_ATLAS\n" "# define USE_TEXTURE_ATLAS 0\n" @@ -295,7 +302,7 @@ "\n" "void main(in VSOutput VSOut,\n" " in bool IsFrontFace : SV_IsFrontFace,\n" -" out float4 OutColor : SV_Target)\n" +" out PSOutput PSOut)\n" "{\n" " float4 BaseColor = GetBaseColor(VSOut);\n" "\n" @@ -422,7 +429,7 @@ " // Add highlight color\n" " color = lerp(color, g_PBRAttribs.Renderer.HighlightColor.rgb, g_PBRAttribs.Renderer.HighlightColor.a);\n" "\n" -" OutColor = float4(color, BaseColor.a);\n" +" float4 OutColor = float4(color, BaseColor.a);\n" "\n" "# if ENABLE_DEBUG_VIEW\n" " // Shader inputs debug visualization\n" @@ -460,4 +467,12 @@ " OutColor.rgb = FastLinearToSRGB(OutColor.rgb);\n" " }\n" "# endif\n" +"\n" +" PSOut.Color = OutColor;\n" +"\n" +"# if ENABLE_CUSTOM_DATA_OUTPUT\n" +" {\n" +" PSOut.CustomData = g_PBRAttribs.Renderer.CustomData;\n" +" }\n" +"# endif\n" "}\n"