Skip to content

Commit

Permalink
Add LinearizeDepth pass + depth transparency + offset in materials
Browse files Browse the repository at this point in the history
Also lots of refactor related to HDR, MSAA and shaders.
  • Loading branch information
Benualdo committed Nov 23, 2024
1 parent 8ec9643 commit c7dfc02
Show file tree
Hide file tree
Showing 54 changed files with 649 additions and 184 deletions.
62 changes: 62 additions & 0 deletions data/Materials/Test/TransparentSphere.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<Root>
<Object class="MaterialResourceData">
<Property type="Uint32" name="m_uid" value="3526809256"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="Object" name="m_materialModelType">
<Object class="MaterialModelType">
<Property type="String" name="m_name" value=""/>
<Property type="Uint32" name="m_uid" value="3621471573"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="String" name="m_shader" value="Default"/>
</Object>
</Property>
<Property type="ObjectPtr" name="m_data">
<Object class="DefaultMaterialData">
<Property type="Uint32" name="m_uid" value="2954644824"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="EnumU8" name="m_surfaceType" value="AlphaBlend"/>
<Property type="EnumU8" name="m_cullMode" value="Back"/>
<Property type="EnumU32" name="m_UVSource" value="UV0"/>
<Property type="Float2" name="m_tiling" x="1" y="1"/>
<Property type="Bool" name="m_enableAlbedo" value="true"/>
<Property type="Resource" name="m_albedoMap">
<Object class="TextureResource">
<Property type="String" name="m_name" value=""/>
<Property type="Uint32" name="m_uid" value="2567142535"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="String" name="m_resourcePath" value=""/>
</Object>
</Property>
<Property type="Float4" name="m_albedoColor" x="1" y="1" z="1" w="0.50196081"/>
<Property type="Bool" name="m_enableNormal" value="false"/>
<Property type="Resource" name="m_normalMap">
<Object class="TextureResource">
<Property type="String" name="m_name" value=""/>
<Property type="Uint32" name="m_uid" value="2877981069"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="String" name="m_resourcePath" value=""/>
</Object>
</Property>
<Property type="Float" name="m_normalStrength" value="1"/>
<Property type="Bool" name="m_enablePbr" value="false"/>
<Property type="Resource" name="m_pbrMap">
<Object class="TextureResource">
<Property type="String" name="m_name" value=""/>
<Property type="Uint32" name="m_uid" value="4061527136"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="String" name="m_resourcePath" value=""/>
</Object>
</Property>
<Property type="Float" name="m_occlusion" value="1"/>
<Property type="Float" name="m_roughness" value="1"/>
<Property type="Float" name="m_metalness" value="1"/>
</Object>
</Property>
</Object>
</Root>
46 changes: 32 additions & 14 deletions data/Scenes/Aiguelongue.scene
Original file line number Diff line number Diff line change
Expand Up @@ -3540,8 +3540,8 @@
<Property type="String" name="m_name" value="Debug"/>
<Property type="Uint32" name="m_uid" value="1949923508"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="EnumFlagsU32" name="m_flags" value=""/>
<Property type="EnumFlagsU32" name="m_objectFlags" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" value="Enabled"/>
<Property type="Float4" name="m_color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="EnumFlagsU64" name="m_tags" value=""/>
Expand All @@ -3552,7 +3552,7 @@
<Property type="Uint32" name="m_uid" value="3740073557"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value="Prefab"/>
<Property type="EnumFlagsU32" name="m_flags" value="Enabled"/>
<Property type="EnumFlagsU32" name="m_flags" value=""/>
<Property type="Float4" name="m_color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="EnumFlagsU64" name="m_tags" value=""/>
Expand All @@ -3570,30 +3570,48 @@
<Property type="ObjectPtrVector" name="m_dynamicProperties"/>
</Object>
<Object class="GameObject">
<Property type="String" name="m_name" value="TestSound"/>
<Property type="Uint32" name="m_uid" value="2463671653"/>
<Property type="String" name="m_name" value="DepthTransparencyTest"/>
<Property type="Uint32" name="m_uid" value="33097108"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="EnumFlagsU32" name="m_flags" value="Enabled"/>
<Property type="Float4" name="m_color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="Float4x4" name="m_local" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="-2.5" Ty="5" Tz="0" Tw="1"/>
<Property type="EnumFlagsU64" name="m_tags" value=""/>
<Property type="ObjectPtrVector" name="m_components">
<Object class="SoundComponent">
<Property type="String" name="m_name" value="New SoundComponent"/>
<Property type="Uint32" name="m_uid" value="2213048044"/>
<Object class="MeshComponent">
<Property type="String" name="m_name" value="New MeshComponent"/>
<Property type="Uint32" name="m_uid" value="2928442164"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" value="Enabled"/>
<Property type="Object" name="m_sounds">
<Object class="SoundResourceList">
<Property type="String" name="m_name" value=""/>
<Property type="Uint32" name="m_uid" value="3853890545"/>
<Property type="Resource" name="m_meshResource">
<Object class="MeshResource">
<Property type="String" name="m_name" value="New MeshComponent"/>
<Property type="Uint32" name="m_uid" value="0"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="String" name="m_resourcePath" value="data/Meshes/unitsphere.fbx"/>
</Object>
</Property>
<Property type="Object" name="m_meshMaterials">
<Object class="MaterialResourceList">
<Property type="String" name="m_name" value="New MeshComponent"/>
<Property type="Uint32" name="m_uid" value="0"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="ResourceVector" name="m_resources"/>
<Property type="ResourceVector" name="m_resources">
<Object class="MaterialResource">
<Property type="String" name="m_name" value="TransparentSphere"/>
<Property type="Uint32" name="m_uid" value="1463542933"/>
<Property type="Uint32" name="m_originalUID" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" value=""/>
<Property type="String" name="m_resourcePath" value="data/Materials/Test/TransparentSphere.mat"/>
</Object>
</Property>
</Object>
</Property>
<Property type="BitMask" name="m_batchMask" BitCount="1" Value="0x0000000000000001"/>
</Object>
</Property>
<Property type="ObjectPtrVector" name="m_children"/>
Expand Down
17 changes: 17 additions & 0 deletions data/Shaders/default/default.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
#include "system/debugdisplay.hlsli"
#include "system/instancedata.hlsli"

#if _ALPHABLEND
#include "system/transparentPass.hlsli"
#endif

#include "default.hlsli"

struct VS_Output
{
float4 pos : Position;
float4 vpos : ViewPos;
float3 wpos : WorldPos;
float3 nrm : Normal;
float3 tan : Tangent;
Expand Down Expand Up @@ -60,6 +65,7 @@ VS_Output VS_Forward(uint _vertexID : VertexID)
viewPos.z += WIREFRAME_DEPTHBIAS;
#endif

output.vpos = viewPos;
output.pos = mul(viewPos, proj);

return output;
Expand Down Expand Up @@ -169,6 +175,17 @@ PS_Output PS_Forward(VS_Output _input)
output.color0.a = 1.0f;
#endif

#if _ALPHABLEND
TransparentPassConstants transparentPassConstants;
transparentPassConstants.Load(getBuffer(RESERVEDSLOT_BUFSRV_TRANSPARENTPASS));

float linearDepthBuffer = getTexture2D(transparentPassConstants.getLinearDepth()).SampleLevel(nearestClamp, screenPos.xy, 0).y;
float linearZ = -_input.vpos.z;
float depthTransparencyScale = 0.1;
float alpha = saturate((linearDepthBuffer - linearZ)/depthTransparencyScale);
output.color0.a *= alpha;
#endif

#if _ZONLY
return;
#else
Expand Down
37 changes: 36 additions & 1 deletion data/Shaders/driver/driver.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "system/color.hlsli"
#include "system/view.hlsli"
#include "system/msaa.hlsli"
#include "system/depthstencil.hlsli"

struct VS_Output_Quad
{
Expand Down Expand Up @@ -75,7 +76,7 @@ PS_Output_Quad PS_Copy(PS_Input_Quad _input)

#endif

// Apply HDR
// Apply HDR
float standardNits = 80; // temp
const float st2084max = 10000.0;
const float hdrScalar = standardNits / st2084max;
Expand Down Expand Up @@ -105,4 +106,38 @@ PS_Output_Quad PS_Copy(PS_Input_Quad _input)
return output;
}

struct PS_Output_MinMaxDepth
{
float2 color0 : Color0;
};

PS_Output_MinMaxDepth PS_CopyLinearDepth(PS_Input_Quad _input)
{
PS_Output_MinMaxDepth output;
float2 uv = _input.uv;

ViewConstants viewConstants;
viewConstants.Load(getBuffer(RESERVEDSLOT_BUFSRV_VIEWCONSTANTS));

uint2 address = viewConstants.getScreenSize().xy * uv;

#if SAMPLE_COUNT > 1
float2 minMaxDepth = float2(1,0);
[unroll]
for (uint i = 0; i < SAMPLE_COUNT; ++i)
{
float depth = loadDepthMSAA(rootConstants2D.texID, address, i);
minMaxDepth.x = min(minMaxDepth.x, depth);
minMaxDepth.y = max(minMaxDepth.y, depth);
}
output.color0 = float2(viewConstants.getLinearDepth(minMaxDepth.x), viewConstants.getLinearDepth(minMaxDepth.y));

#else
float depth = loadDepth(rootConstants2D.texID, address);
output.color0 = viewConstants.getLinearDepth(depth).xx;
#endif

return output;
}


48 changes: 33 additions & 15 deletions data/Shaders/driver/driver.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,65 @@ namespace vg::gfx
{
class DriverHLSLDesc : public HLSLDesc
{
public:
enum Flags : ShaderKey::Flags
{
Gamma = 0,
HDR = 1, // 2 bits
MSAA = 3 // 3 bits
Gamma = 0, // 1 bit (ON/OFF)
HDR = 1, // 2 bits (HDR enum)
MSAA = 3, // 3 bits (MSAA enum)
Depth = 6 // 1 bit (ON/OFF)
};

public:
DriverHLSLDesc()
{
setFile("driver/driver.hlsl");

declFlag(Gamma, ShaderStageFlags::PS, "_GAMMA");
declFlags(HDR, ShaderStageFlags::PS, { "", "_HDR10", "_HDR16" });
declFlags(MSAA, ShaderStageFlags::PS, { "", "_MSAA2X", "_MSAA4X", "_MSAA8X", "_MSAA16X" });
declFlag(Depth, ShaderStageFlags::PS, "_DEPTH");

auto vsQuad = declVS("VS_Quad");
auto psCopy = declPS("PS_Copy");

auto & copy = declTechnique("Copy", vsQuad, psCopy);

auto & gamma = declTechnique("Gamma", vsQuad, psCopy);
gamma.setFlag(Flags::Gamma, true);
gamma.setFlag(Gamma, true);

auto & hdr10 = declTechnique("HDR10", vsQuad, psCopy);
hdr10.setFlags(Flags::HDR, 1);
hdr10.setValue(HDR, HDR::HDR10);

auto & hdr16 = declTechnique("HDR16", vsQuad, psCopy);
hdr16.setFlags(Flags::HDR, 2);
hdr16.setValue(HDR, HDR::HDR16);

auto & resolveColorMSAA2X = declTechnique("ResolveColorMSAA2X", vsQuad, psCopy);
resolveColorMSAA2X.setValue(MSAA, MSAA::MSAA2X);

auto & resolveColorMSAA4X = declTechnique("ResolveColorMSAA4X", vsQuad, psCopy);
resolveColorMSAA4X.setValue(MSAA, MSAA::MSAA4X);

auto & resolveColorMSAA8X = declTechnique("ResolveColorMSAA8X", vsQuad, psCopy);
resolveColorMSAA8X.setValue(MSAA, MSAA::MSAA8X);

auto & resolveColorMSAA16X = declTechnique("ResolveColorMSAA16X", vsQuad, psCopy);
resolveColorMSAA16X.setValue(MSAA, MSAA::MSAA16X);

auto psCopyLinearDepth = declPS("PS_CopyLinearDepth");

auto & copyLinearDepth = declTechnique("CopyLinearDepth", vsQuad, psCopyLinearDepth);

auto & msaa2X = declTechnique("MSAA2X", vsQuad, psCopy);
msaa2X.setFlags(MSAA, 1);
auto & resolveLinearDepthMSAA2X = declTechnique("ResolveLinearDepthMSAA2X", vsQuad, psCopyLinearDepth);
resolveLinearDepthMSAA2X.setValue(MSAA, MSAA::MSAA2X);

auto & msaa4X = declTechnique("MSAA4X", vsQuad, psCopy);
msaa4X.setFlags(MSAA, 2);
auto & resolveLinearDepthMSAA4X = declTechnique("ResolveLinearDepthMSAA4X", vsQuad, psCopyLinearDepth);
resolveLinearDepthMSAA4X.setValue(MSAA, MSAA::MSAA4X);

auto & msaa8X = declTechnique("MSAA8X", vsQuad, psCopy);
msaa8X.setFlags(MSAA, 3);
auto & resolveLinearDepthMSAA8X = declTechnique("ResolveLinearDepthMSAA8X", vsQuad, psCopyLinearDepth);
resolveLinearDepthMSAA8X.setValue(MSAA, MSAA::MSAA8X);

auto & msaa16X = declTechnique("MSAA16X", vsQuad, psCopy);
msaa16X.setFlags(MSAA, 4);
auto & resolveLinearDepthMSAA16X = declTechnique("ResolveLinearDepthMSAA16X", vsQuad, psCopyLinearDepth);
resolveLinearDepthMSAA16X.setValue(MSAA, MSAA::MSAA16X);
}
};
}
5 changes: 3 additions & 2 deletions data/Shaders/lighting/deferredLighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "system/lighting.hlsli"
#include "system/msaa.hlsli"
#include "system/environment.hlsli"
#include "system/depthstencil.hlsli"

struct DepthStencilSample
{
Expand All @@ -13,9 +14,9 @@ struct DepthStencilSample
void Load(int2 _coords, uint _sampleIndex)
{
#if SAMPLE_COUNT > 1
depth = getTexture2DMS(deferredLightingConstants.getDepth()).Load(_coords, _sampleIndex).r;
depth = loadDepthMSAA(deferredLightingConstants.getDepth(), _coords, _sampleIndex);
#else
depth = getTexture2D(deferredLightingConstants.getDepth()).Load(int3(_coords, 0)).r;
depth = loadDepth(deferredLightingConstants.getDepth(), _coords);
#endif

#if VG_GFX_REVERSE_DEPTH
Expand Down
21 changes: 18 additions & 3 deletions data/Shaders/postprocess/postprocess.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,15 @@ void CS_PostProcessMain(int2 dispatchThreadID : SV_DispatchThreadID)
color = getTexture2D(postProcessConstants.getColor()).Load(address);
#endif

#if SAMPLE_COUNT > 1
// TODO: pass to shader if original depthstencil is MSAA
float depth = loadDepthMSAA(postProcessConstants.getDepth(), address, 0);
uint stencil = loadStencilMSAA(postProcessConstants.getStencil(), address, 0);
#else
float depth = loadDepth(postProcessConstants.getDepth(), address);
uint stencil = loadStencil(postProcessConstants.getStencil(), address);

#endif

#if _TOOLMODE

#if _RAYTRACING
Expand All @@ -309,13 +315,22 @@ void CS_PostProcessMain(int2 dispatchThreadID : SV_DispatchThreadID)
break;

case DisplayMode::PostProcess_Depth:
color.rgb = frac(depth * viewConstants.getCameraNearFar().y);
color.rgb = frac(depth.xxx * float3(65536, 256, 1));
break;

case DisplayMode::PostProcess_LinearDepth:
{
#if 0
// Compute linear depth from ZBuffer
float linearDepth = viewConstants.getLinearDepth(depth);
color.rgb = depth < 1.0f ? frac(linearDepth * viewConstants.getCameraNearFar().y) : 0.0f;
color.rgb = frac(linearDepth * (viewConstants.getCameraNearFar().y-viewConstants.getCameraNearFar().x));
#else
// Use resolved MSAA linear depth texture
float2 linearDepth = getTexture2D(postProcessConstants.getLinearDepth()).SampleLevel(nearestClamp, uv, 0).rg;
//linearDepth *= viewConstants.getCameraDepthRange();
//color.rgb = float3(frac(linearDepth.xy), (linearDepth.x == linearDepth.y) ? frac(linearDepth.x) : 0);
color.rgb = frac(linearDepth.xxx); // minZ
#endif
}
break;

Expand Down
Loading

0 comments on commit c7dfc02

Please sign in to comment.