Skip to content

Commit

Permalink
Tutorial 29: a few minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Jan 6, 2025
1 parent 664b1c6 commit 6ccc30d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
1 change: 0 additions & 1 deletion Tutorials/Tutorial29_OIT/assets/common.fxh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
struct Constants
{
float4x4 ViewProj;
float4x4 Proj;
float4 LightDir;

float MinOpacity;
Expand Down
56 changes: 27 additions & 29 deletions Tutorials/Tutorial29_OIT/src/Tutorial29_OIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ SampleBase* CreateSample()
return new Tutorial29_OIT();
}

// Accumulate the total number of tail layers in R channel (Src * 1 + Dst * 1)
// Compute the total tail attenuation in A channel (Src * 0 + Dst * SrcA)
static constexpr BlendStateDesc BS_UpdateOITTail{
False, // AlphaToCoverageEnable
False, // IndependentBlendEnable
Expand All @@ -72,6 +74,7 @@ static constexpr BlendStateDesc BS_UpdateOITTail{
},
};

// Attenuate the background using transmittance (Src * 0 + Dst * SrcA)
static constexpr BlendStateDesc BS_AttenuateBackground{
False, // AlphaToCoverageEnable
False, // IndependentBlendEnable
Expand Down Expand Up @@ -144,17 +147,25 @@ void Tutorial29_OIT::ModifyEngineInitInfo(const ModifyEngineInitInfoAttribs& Att
Attribs.EngineCI.Features.PixelUAVWritesAndAtomics = DEVICE_FEATURE_STATE_ENABLED;
// We will create our own depth buffer
if (Attribs.DeviceType != RENDER_DEVICE_TYPE_GL && Attribs.DeviceType != RENDER_DEVICE_TYPE_GLES)
{
// We use our own depth buffer, but since OpenGL does not allow using depth buffer from the default framebuffer
// with any other render target, we have to use it.
Attribs.SCDesc.DepthBufferFormat = TEX_FORMAT_UNKNOWN;
}
}

void Tutorial29_OIT::CreatePipelineStates()
{
RenderDeviceX_N Device{m_pDevice};
// WebGPU does not support earlydepthstencil attribute
m_EarlyDepthStencilSupported = !Device.GetDeviceInfo().IsWebGPUDevice();
ShaderCreateInfo ShaderCI;

ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceFactory;
m_pEngineFactory->CreateDefaultShaderSourceStreamFactory(nullptr, &pShaderSourceFactory);
ShaderCreateInfo ShaderCI;
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
ShaderCI.pShaderSourceStreamFactory = pShaderSourceFactory;
ShaderCI.CompileFlags = SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR;

ShaderMacroHelper Macros;
Macros.Add("CONVERT_PS_OUTPUT_TO_GAMMA", m_ConvertPSOutputToGamma);
Expand All @@ -164,12 +175,6 @@ void Tutorial29_OIT::CreatePipelineStates()
Macros.Add("USE_MANUAL_DEPTH_TEST", !m_EarlyDepthStencilSupported);
ShaderCI.Macros = Macros;

// Create a shader source stream factory to load shaders from files.
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceFactory;
m_pEngineFactory->CreateDefaultShaderSourceStreamFactory(nullptr, &pShaderSourceFactory);
ShaderCI.pShaderSourceStreamFactory = pShaderSourceFactory;
ShaderCI.CompileFlags = SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR;

RefCntAutoPtr<IShader> pGeometryVS;
{
ShaderCI.Desc = {"Geometry VS", SHADER_TYPE_VERTEX, true};
Expand Down Expand Up @@ -269,24 +274,19 @@ void Tutorial29_OIT::CreatePipelineStates()
{
GraphicsPipelineStateCreateInfoX PsoCI;

// clang-format off
// Define vertex shader input layout
// This tutorial uses two types of input: per-vertex data and per-instance data.
InputLayoutDescX InputLayout
{
InputLayoutDescX InputLayout{
// Per-vertex data - first buffer slot
// Attribute 0 - vertex position
LayoutElement{0, 0, 3, VT_FLOAT32},
// Attribute 1 - normal
LayoutElement{1, 0, 3, VT_FLOAT32},

// Per-instance data - second buffer slot
// Attribute 2 - translation and scale
LayoutElement{2, 1, 4, VT_FLOAT32, False, INPUT_ELEMENT_FREQUENCY_PER_INSTANCE},
// Attribute 3 - color
LayoutElement{3, 1, 4, VT_FLOAT32, False, INPUT_ELEMENT_FREQUENCY_PER_INSTANCE}
LayoutElement{3, 1, 4, VT_FLOAT32, False, INPUT_ELEMENT_FREQUENCY_PER_INSTANCE},
};
// clang-format on

PipelineResourceLayoutDescX ResourceLayout;
ResourceLayout
Expand All @@ -303,7 +303,6 @@ void Tutorial29_OIT::CreatePipelineStates()
.AddRenderTarget(SCDesc.ColorBufferFormat)
.SetDepthFormat(m_DepthFormat)
.SetDepthStencilDesc(DSS_EnableDepthNoWrites);

m_AlphaBlendPSO = Device.CreateGraphicsPipelineState(PsoCI);
m_AlphaBlendPSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "cbConstants")->Set(m_Constants);
m_AlphaBlendSRB.Release();
Expand All @@ -320,8 +319,8 @@ void Tutorial29_OIT::CreatePipelineStates()
.SetBlendDesc(BS_AdditiveBlend)
.SetDepthStencilDesc(DSS_EnableDepthNoWrites)
.AddShader(pOITBlendPS);
m_OITBlendPSO = Device.CreateGraphicsPipelineState(PsoCI);
m_OITBlendPSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "cbConstants")->Set(m_Constants);
m_LayeredOITBlendPSO = Device.CreateGraphicsPipelineState(PsoCI);
m_LayeredOITBlendPSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "cbConstants")->Set(m_Constants);


ResourceLayout.AddVariable(SHADER_TYPE_PIXEL, "g_DepthBuffer", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE,
Expand All @@ -341,7 +340,7 @@ void Tutorial29_OIT::CreatePipelineStates()

ResourceLayout.RemoveVariable("g_DepthBuffer");
PsoCI
.SetName("Weighted blend PSO")
.SetName("Weighted blended PSO")
.SetResourceLayout(ResourceLayout)
.SetBlendDesc(BS_WeightedBlend)
.AddShader(pWeightedBlendPS)
Expand Down Expand Up @@ -457,10 +456,10 @@ void Tutorial29_OIT::PrepareLayeredOITResources()
m_UpdateOITLayersSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_DepthBuffer")->Set(m_DepthBuffer->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE));
}

m_OITBlendSRB.Release();
m_OITBlendPSO->CreateShaderResourceBinding(&m_OITBlendSRB, true);
m_OITBlendSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_OITLayers")->Set(m_OITLayers->GetDefaultView(BUFFER_VIEW_SHADER_RESOURCE));
m_OITBlendSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_OITTail")->Set(m_OITTail->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE));
m_LayeredOITBlendSRB.Release();
m_LayeredOITBlendPSO->CreateShaderResourceBinding(&m_LayeredOITBlendSRB, true);
m_LayeredOITBlendSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_OITLayers")->Set(m_OITLayers->GetDefaultView(BUFFER_VIEW_SHADER_RESOURCE));
m_LayeredOITBlendSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_OITTail")->Set(m_OITTail->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE));

m_AttenuateBackgroundSRB.Release();
m_AttenuateBackgroundPSO->CreateShaderResourceBinding(&m_AttenuateBackgroundSRB, true);
Expand All @@ -472,7 +471,7 @@ void Tutorial29_OIT::PrepareWeightedOITResources()
{
const SwapChainDesc& SCDesc = m_pSwapChain->GetDesc();
if (m_WeightedColor && m_WeightedColor->GetDesc().Width == SCDesc.Width && m_WeightedColor->GetDesc().Height == SCDesc.Height)
m_WeightedColor.Release();
return;

TextureDesc TexDesc;
TexDesc.Name = "Weighted color";
Expand Down Expand Up @@ -675,7 +674,7 @@ void Tutorial29_OIT::RenderLayered(ITextureView* pRTV, ITextureView* pDSV)

// Render transparent objects using OIT
{
RenderGrid(/*IsTransparent = */ true, m_OITBlendPSO, m_OITBlendSRB);
RenderGrid(/*IsTransparent = */ true, m_LayeredOITBlendPSO, m_LayeredOITBlendSRB);
}
}

Expand Down Expand Up @@ -717,7 +716,6 @@ void Tutorial29_OIT::Render()
// Map the buffer and write current world-view-projection matrix
MapHelper<HLSL::Constants> CBConstants{m_pImmediateContext, m_Constants, MAP_WRITE, MAP_FLAG_DISCARD};
CBConstants->ViewProj = m_ViewProjMatrix;
CBConstants->Proj = m_ProjMatrix;
CBConstants->LightDir = normalize(float3{0.57735f, -0.57735f, 0.157735f});
CBConstants->MinOpacity = m_MinOpacity;
CBConstants->MaxOpacity = m_MaxOpacity;
Expand Down Expand Up @@ -817,10 +815,10 @@ void Tutorial29_OIT::Update(double CurrTime, double ElapsedTime)
float4x4 SrfPreTransform = GetSurfacePretransformMatrix(float3{0, 0, 1});

// Get projection matrix adjusted to the current screen orientation
m_ProjMatrix = GetAdjustedProjectionMatrix(PI_F / 4.0f, 1.f, 5.f);
float4x4 Proj = GetAdjustedProjectionMatrix(PI_F / 4.0f, 1.f, 5.f);

// Compute view-projection matrix
m_ViewProjMatrix = View * SrfPreTransform * m_ProjMatrix;
m_ViewProjMatrix = View * SrfPreTransform * Proj;
}

} // namespace Diligent
6 changes: 2 additions & 4 deletions Tutorials/Tutorial29_OIT/src/Tutorial29_OIT.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,15 @@ class Tutorial29_OIT final : public SampleBase
RefCntAutoPtr<IPipelineState> m_AlphaBlendPSO;
RefCntAutoPtr<IPipelineState> m_WeightedBlendPSO;
RefCntAutoPtr<IShaderResourceBinding> m_AlphaBlendSRB;
RefCntAutoPtr<IPipelineState> m_OITBlendPSO;
RefCntAutoPtr<IShaderResourceBinding> m_OITBlendSRB;
RefCntAutoPtr<IPipelineState> m_LayeredOITBlendPSO;
RefCntAutoPtr<IShaderResourceBinding> m_LayeredOITBlendSRB;
RefCntAutoPtr<IPipelineState> m_UpdateOITLayersPSO;
RefCntAutoPtr<IShaderResourceBinding> m_UpdateOITLayersSRB;
RefCntAutoPtr<IPipelineState> m_AttenuateBackgroundPSO;
RefCntAutoPtr<IShaderResourceBinding> m_AttenuateBackgroundSRB;
RefCntAutoPtr<IPipelineState> m_WeightedResolvePSO;
RefCntAutoPtr<IShaderResourceBinding> m_WeightedResolveSRB;


enum class RenderMode : int
{
UnsortedAlphaBlend,
Expand All @@ -111,7 +110,6 @@ class Tutorial29_OIT final : public SampleBase

int m_NumOITLayers = 4;

float4x4 m_ProjMatrix;
float4x4 m_ViewProjMatrix;
int m_GridSize = 10;
float m_PercentOpaque = 10;
Expand Down

0 comments on commit 6ccc30d

Please sign in to comment.