Skip to content

Commit 8486688

Browse files
Hydrogent: apply OIT attenuation to IBL and Base Color outputs
1 parent 3c5bf46 commit 8486688

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed

Hydrogent/src/Tasks/HnEndOITPassTask.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,21 @@ void HnEndOITPassTask::Prepare(pxr::HdTaskContext* TaskCtx,
7070

7171
if (!m_ApplyOITAttenuationPSO)
7272
{
73-
ITextureView* pColorRTV = FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_SCENE_COLOR];
74-
if (pColorRTV == nullptr)
73+
ITextureView* pColorRTV = FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_SCENE_COLOR];
74+
ITextureView* pBaseColorRTV = FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_BASE_COLOR];
75+
ITextureView* pIblRTV = FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_IBL];
76+
if (pColorRTV == nullptr || pBaseColorRTV == nullptr || pIblRTV == nullptr)
7577
{
76-
UNEXPECTED("Scene color target is null");
78+
UNEXPECTED("Scene color, base color or IBL target is null");
7779
return;
7880
}
7981

80-
const TextureViewDesc& ColorDesc = pColorRTV->GetDesc();
81-
Renderer.CreateApplyOITAttenuationPSO(ColorDesc.Format, TEX_FORMAT_UNKNOWN, &m_ApplyOITAttenuationPSO);
82+
const TextureViewDesc& ColorDesc = pColorRTV->GetDesc();
83+
const TextureViewDesc& BaseColorDesc = pBaseColorRTV->GetDesc();
84+
const TextureViewDesc& IblDesc = pIblRTV->GetDesc();
85+
const TEXTURE_FORMAT RTVFormats[] = {ColorDesc.Format, BaseColorDesc.Format, IblDesc.Format};
86+
87+
Renderer.CreateApplyOITAttenuationPSO(RTVFormats, _countof(RTVFormats), ~0u, TEX_FORMAT_UNKNOWN, &m_ApplyOITAttenuationPSO);
8288
VERIFY_EXPR(m_ApplyOITAttenuationPSO);
8389
}
8490

@@ -113,13 +119,18 @@ void HnEndOITPassTask::Execute(pxr::HdTaskContext* TaskCtx)
113119
return;
114120
}
115121

116-
ITextureView* pColorRTV = FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_SCENE_COLOR];
117-
if (pColorRTV == nullptr)
122+
ITextureView* ppRTVs[] = {
123+
FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_SCENE_COLOR],
124+
FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_BASE_COLOR],
125+
FrameTargets->GBufferRTVs[HnFrameRenderTargets::GBUFFER_TARGET_IBL],
126+
};
127+
if (ppRTVs[0] == nullptr || ppRTVs[1] == nullptr || ppRTVs[2] == nullptr)
118128
{
119-
UNEXPECTED("Scene color target is null");
129+
UNEXPECTED("Scene color, base color or IBL target is null");
120130
return;
121131
}
122-
pCtx->SetRenderTargets(1, &pColorRTV, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
132+
133+
pCtx->SetRenderTargets(_countof(ppRTVs), ppRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
123134

124135
const StateTransitionDesc Barriers[] =
125136
{

PBR/interface/PBR_Renderer.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,11 @@ class PBR_Renderer
798798
void CreateClearOITLayersSRB(IBuffer* pFrameAttribs, IBuffer* OITLayers, IShaderResourceBinding** ppSRB) const;
799799
void CreateRWOITLayersSRB(IBuffer* OITLayers, ITextureView* pDepthSRV, IShaderResourceBinding** ppSRB) const;
800800
void ClearOITLayers(IDeviceContext* pCtx, IShaderResourceBinding* pSRB, Uint32 Width, Uint32 Height) const;
801-
void CreateApplyOITAttenuationPSO(TEXTURE_FORMAT ColorFormat, TEXTURE_FORMAT DepthFormat, IPipelineState** ppPSO) const;
801+
void CreateApplyOITAttenuationPSO(const TEXTURE_FORMAT* RTVFormats,
802+
Uint32 NumRenderTargets,
803+
Uint32 RenderTargetMask,
804+
TEXTURE_FORMAT DepthFormat,
805+
IPipelineState** ppPSO) const;
802806
void CreateApplyOITAttenuationSRB(IBuffer* pFrameAttribs,
803807
IBuffer* OITLayers,
804808
ITexture* OITTail,

PBR/src/PBR_Renderer.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,21 +2268,61 @@ static constexpr BlendStateDesc BS_OITAttenuation{
22682268
BLEND_FACTOR_SRC_ALPHA, // DestBlend
22692269
BLEND_OPERATION_ADD, // BlendOp
22702270
BLEND_FACTOR_ZERO, // SrcBlendAlpha
2271-
BLEND_FACTOR_ONE, // DestBlendAlpha
2271+
BLEND_FACTOR_SRC_ALPHA, // DestBlendAlpha
22722272
BLEND_OPERATION_ADD, // BlendOpAlpha
22732273
},
22742274
};
22752275

2276-
void PBR_Renderer::CreateApplyOITAttenuationPSO(TEXTURE_FORMAT ColorFormat, TEXTURE_FORMAT DepthFormat, IPipelineState** ppPSO) const
2276+
void PBR_Renderer::CreateApplyOITAttenuationPSO(const TEXTURE_FORMAT* RTVFormats,
2277+
Uint32 NumRenderTargets,
2278+
Uint32 RenderTargetMask,
2279+
TEXTURE_FORMAT DepthFormat,
2280+
IPipelineState** ppPSO) const
22772281
{
2282+
GraphicsPipelineStateCreateInfoX PsoCI{"OIT Attenuation"};
2283+
2284+
std::stringstream PSOutputSS;
2285+
std::stringstream PSMainFooterSS;
2286+
PSOutputSS << "struct PSOutput" << std::endl
2287+
<< "{" << std::endl;
2288+
for (Uint32 rt = 0; rt < NumRenderTargets; ++rt)
2289+
{
2290+
PsoCI.AddRenderTarget(RTVFormats[rt]);
2291+
if (RTVFormats[rt] == TEX_FORMAT_UNKNOWN || (RenderTargetMask & (1u << rt)) == 0)
2292+
{
2293+
PsoCI.GraphicsPipeline.BlendDesc.RenderTargets[rt].RenderTargetWriteMask = COLOR_MASK_NONE;
2294+
}
2295+
else
2296+
{
2297+
PSOutputSS << " float4 Color" << rt << " : SV_Target" << rt << ";" << std::endl;
2298+
PSMainFooterSS << " PSOut.Color" << rt << " = OutColor;" << std::endl;
2299+
}
2300+
}
2301+
PSOutputSS << "};" << std::endl;
2302+
2303+
const std::string PSOutputStruct = PSOutputSS.str();
2304+
const std::string PSMainFooter = PSMainFooterSS.str();
2305+
2306+
// Keep copies of generated strings in the factory when hot shader reload is allowed.
2307+
const bool CopyGeneratedStrings = m_Settings.AllowHotShaderReload;
2308+
2309+
RefCntAutoPtr<IShaderSourceInputStreamFactory> pMemorySourceFactory =
2310+
CreateMemoryShaderSourceFactory({
2311+
MemoryShaderSourceFileInfo{"PSOutputStruct.generated", PSOutputStruct},
2312+
MemoryShaderSourceFileInfo{"PSMainFooter.generated", PSMainFooter},
2313+
},
2314+
CopyGeneratedStrings);
2315+
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceFactory =
2316+
CreateCompoundShaderSourceFactory({&DiligentFXShaderSourceStreamFactory::GetInstance(), pMemorySourceFactory});
2317+
22782318
ShaderMacroHelper Macros;
22792319
Macros.Add("NUM_OIT_LAYERS", static_cast<int>(m_Settings.OITLayerCount));
22802320

22812321
ShaderCreateInfo ShaderCI;
22822322
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
22832323
ShaderCI.CompileFlags = m_Settings.PackMatrixRowMajor ? SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR : SHADER_COMPILE_FLAG_NONE;
2284-
ShaderCI.pShaderSourceStreamFactory = &DiligentFXShaderSourceStreamFactory::GetInstance();
22852324
ShaderCI.Macros = Macros;
2325+
ShaderCI.pShaderSourceStreamFactory = pShaderSourceFactory;
22862326

22872327
RefCntAutoPtr<IShader> pScreenTriangleVS;
22882328
{
@@ -2302,10 +2342,8 @@ void PBR_Renderer::CreateApplyOITAttenuationPSO(TEXTURE_FORMAT ColorFormat, TEXT
23022342
pAttenuationPS = m_Device.CreateShader(ShaderCI);
23032343
}
23042344

2305-
GraphicsPipelineStateCreateInfoX PsoCI{"OIT Attenuation"};
23062345
PsoCI
23072346
.AddSignature(m_OITAttenuationSignature)
2308-
.AddRenderTarget(ColorFormat)
23092347
.SetDepthFormat(DepthFormat)
23102348
.SetBlendDesc(BS_OITAttenuation)
23112349
.SetDepthStencilDesc(DSS_DisableDepth)

Shaders/PBR/private/OIT/ApplyOITAttenuation.psh

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ cbuffer cbFrameAttribs
1010
StructuredBuffer<uint> g_OITLayers;
1111
Texture2D<float4> g_OITTail;
1212

13-
struct PSOutput
14-
{
15-
float4 Color : SV_TARGET;
16-
};
13+
#include "PSOutputStruct.generated"
14+
//struct PSOutput
15+
//{
16+
// float4 Color0 : SV_Target0;
17+
// float4 Color1 : SV_Target1;
18+
// float4 Color2 : SV_Target2;
19+
//};
1720

1821
void main(FullScreenTriangleVSOutput VSOut,
1922
out PSOutput PSOut)
2023
{
2124
uint2 Pos = uint2(VSOut.f4PixelPos.xy);
22-
25+
2326
uint Offset = GetOITLayerDataOffset(Pos.xy, uint2(g_Camera.f4ViewportSize.xy), uint(NUM_OIT_LAYERS));
2427
float T = 1.0;
25-
28+
2629
// Apply attenuation of each layer
2730
uint layer = 0u;
2831
while (layer < uint(NUM_OIT_LAYERS))
@@ -48,5 +51,10 @@ void main(FullScreenTriangleVSOutput VSOut,
4851
}
4952

5053
// RGB blend: Src * 0 + Dst * SrcA
51-
PSOut.Color = float4(0.0, 0.0, 0.0, T);
54+
float4 OutColor = float4(0.0, 0.0, 0.0, T);
55+
56+
#include "PSMainFooter.generated"
57+
//PSOut.Color0 = OutColor;
58+
//PSOut.Color1 = OutColor;
59+
//PSOut.Color2 = OutColor;
5260
}

0 commit comments

Comments
 (0)