Skip to content

Commit

Permalink
PBR Renderer: added option to output custom data to render target 1
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 17, 2023
1 parent 0615614 commit aaf655d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
10 changes: 6 additions & 4 deletions PBR/interface/PBR_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion PBR/src/PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<int>(DebugViewType::None));
Macros.Add("DEBUG_VIEW_TEXCOORD0", static_cast<int>(DebugViewType::Texcoord0));
Expand All @@ -612,6 +613,7 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags) const
Macros.Add("DEBUG_VIEW_SPECULAR_IBL", static_cast<int>(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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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<IShaderSourceInputStreamFactory> pMemorySourceFactory;
Expand Down
19 changes: 17 additions & 2 deletions Shaders/PBR/private/RenderPBR.psh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions Shaders/PBR/public/PBR_Structures.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct PBRRendererShaderParameters
float4 WireframeColor;
float4 HighlightColor;

float4 CustomData;

uint MeshId;
uint Padding0;
uint Padding1;
Expand Down
2 changes: 2 additions & 0 deletions shaders_inc/PBR_Structures.fxh.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
" float4 WireframeColor;\n"
" float4 HighlightColor;\n"
"\n"
" float4 CustomData;\n"
"\n"
" uint MeshId;\n"
" uint Padding0;\n"
" uint Padding1;\n"
Expand Down
19 changes: 17 additions & 2 deletions shaders_inc/RenderPBR.psh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

0 comments on commit aaf655d

Please sign in to comment.