Skip to content

Commit

Permalink
Env map renderer: added option to provide custom PS main function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 30, 2023
1 parent a59d857 commit 30c8d8c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Components/interface/EnvMapRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -106,6 +108,7 @@ class EnvMapRenderer

const std::vector<TEXTURE_FORMAT> m_RTVFormats;
const TEXTURE_FORMAT m_DSVFormat;
const std::string m_PSMainSource;

std::unordered_map<PSOKey, RefCntAutoPtr<IPipelineState>, PSOKey::Hasher> m_PSOs;

Expand Down
33 changes: 31 additions & 2 deletions Components/src/EnvMapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "CommonlyUsedStates.h"
#include "MapHelper.hpp"
#include "GraphicsUtilities.h"
#include "ShaderSourceFactoryUtils.h"

namespace Diligent
{
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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<IShaderSourceInputStreamFactory> pMemorySourceFactory;
CreateMemoryShaderSourceFactory(MemorySourceFactoryCI, &pMemorySourceFactory);

IShaderSourceInputStreamFactory* ppShaderSourceFactories[] = {&DiligentFXShaderSourceStreamFactory::GetInstance(), pMemorySourceFactory};
CompoundShaderSourceFactoryCreateInfo CompoundSourceFactoryCI{ppShaderSourceFactories, _countof(ppShaderSourceFactories)};
RefCntAutoPtr<IShaderSourceInputStreamFactory> pCompoundSourceFactory;
CreateCompoundShaderSourceFactory(CompoundSourceFactoryCI, &pCompoundSourceFactory);

ShaderCreateInfo ShaderCI;
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
ShaderCI.pShaderSourceStreamFactory = &DiligentFXShaderSourceStreamFactory::GetInstance();
ShaderCI.pShaderSourceStreamFactory = pCompoundSourceFactory;

ShaderMacroHelper Macros;
Macros
Expand Down
1 change: 0 additions & 1 deletion PBR/src/PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 13 additions & 6 deletions Shaders/Common/private/EnvMap.psh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
// }
19 changes: 13 additions & 6 deletions shaders_inc/EnvMap.psh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

0 comments on commit 30c8d8c

Please sign in to comment.