Skip to content

Commit

Permalink
Support shader flags using more than one bit
Browse files Browse the repository at this point in the history
e.g. declFlags(AntiAliasingMode, ShaderStageFlags::CS, { "", "_FXAA", "_SMAA" } );
  • Loading branch information
Benualdo committed Oct 22, 2024
1 parent 0521331 commit 8b5fb04
Show file tree
Hide file tree
Showing 32 changed files with 420 additions and 136 deletions.
6 changes: 3 additions & 3 deletions data/Shaders/background/background.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace vg::gfx

setFile("background/background.hlsl");

auto vsQuad = addVS("VS_Background");
auto psQuad = addPS("PS_Gradient");
auto vsQuad = declVS("VS_Background");
auto psQuad = declPS("PS_Gradient");

auto & quad = addTechnique("Gradient");
auto & quad = declTechnique("Gradient");
{
quad.vs = vsQuad;
quad.ps = psQuad;
Expand Down
6 changes: 3 additions & 3 deletions data/Shaders/debugdraw/debugdraw.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace vg::gfx
{
setFile("debugdraw/debugdraw.hlsl");

auto & debugDraw = addTechnique("DebugDraw");
auto & debugDraw = declTechnique("DebugDraw");
{
debugDraw.vs = addVS("VS_DebugDraw");
debugDraw.ps = addPS("PS_DebugDraw");
debugDraw.vs = declVS("VS_DebugDraw");
debugDraw.ps = declPS("PS_DebugDraw");
}
}
};
Expand Down
30 changes: 15 additions & 15 deletions data/Shaders/default/default.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ namespace vg::gfx
{
setFile("default/default.hlsl");

addFlag(AlphaBlend, ShaderStageFlags::VS | ShaderStageFlags::PS, "_ALPHABLEND");
addFlag(AlphaTest, ShaderStageFlags::VS | ShaderStageFlags::PS, "_ALPHATEST");
addFlag(ZOnly, ShaderStageFlags::VS | ShaderStageFlags::PS, "_ZONLY");
addFlag(RayTracing, ShaderStageFlags::VS | ShaderStageFlags::PS, "_RAYTRACING");
addFlag(Toolmode, ShaderStageFlags::VS | ShaderStageFlags::PS, "_TOOLMODE");
declFlag(AlphaBlend, ShaderStageFlags::VS | ShaderStageFlags::PS, "_ALPHABLEND");
declFlag(AlphaTest, ShaderStageFlags::VS | ShaderStageFlags::PS, "_ALPHATEST");
declFlag(ZOnly, ShaderStageFlags::VS | ShaderStageFlags::PS, "_ZONLY");
declFlag(RayTracing, ShaderStageFlags::VS | ShaderStageFlags::PS, "_RAYTRACING");
declFlag(Toolmode, ShaderStageFlags::VS | ShaderStageFlags::PS, "_TOOLMODE");

auto & zonly = addTechnique("ZOnly");
auto & zonly = declTechnique("ZOnly");
{
zonly.vs = addVS("VS_Forward");
zonly.ps = addPS("PS_Forward");
zonly.addFlag(Flags::ZOnly);
zonly.vs = declVS("VS_Forward");
zonly.ps = declPS("PS_Forward");
zonly.setFlag(Flags::ZOnly);
}

auto & forward = addTechnique("Forward");
auto & forward = declTechnique("Forward");
{
forward.vs = addVS("VS_Forward");
forward.ps = addPS("PS_Forward");
forward.vs = declVS("VS_Forward");
forward.ps = declPS("PS_Forward");
}

auto & deferred = addTechnique("Deferred");
auto & deferred = declTechnique("Deferred");
{
deferred.vs = addVS("VS_Deferred");
deferred.ps = addPS("PS_Deferred");
deferred.vs = declVS("VS_Deferred");
deferred.ps = declPS("PS_Deferred");
}
}
};
Expand Down
22 changes: 11 additions & 11 deletions data/Shaders/driver/driver.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ namespace vg::gfx

setFile("driver/driver.hlsl");

addFlag(Gamma, ShaderStageFlags::PS, "_GAMMA");
addFlag(HDR10, ShaderStageFlags::PS, "_HDR10");
addFlag(HDR16, ShaderStageFlags::PS, "_HDR16");
declFlag(Gamma, ShaderStageFlags::PS, "_GAMMA");
declFlag(HDR10, ShaderStageFlags::PS, "_HDR10");
declFlag(HDR16, ShaderStageFlags::PS, "_HDR16");

auto vsQuad = addVS("VS_Quad");
auto psQuad = addPS("PS_Quad");
auto psCopy = addPS("PS_Copy");
auto vsQuad = declVS("VS_Quad");
auto psQuad = declPS("PS_Quad");
auto psCopy = declPS("PS_Copy");

auto & quad = addTechnique("Quad", vsQuad, psQuad);
auto & copy = addTechnique("Copy", vsQuad, psCopy);
auto & gamma = addTechnique("Gamma", vsQuad, psCopy, Flags::Gamma);
auto & hdr10 = addTechnique("HDR10", vsQuad, psCopy, Flags::HDR10);
auto & hdr16 = addTechnique("HDR16", vsQuad, psCopy, Flags::HDR16);
auto & quad = declTechnique("Quad", vsQuad, psQuad);
auto & copy = declTechnique("Copy", vsQuad, psCopy);
auto & gamma = declTechnique("Gamma", vsQuad, psCopy, Flags::Gamma);
auto & hdr10 = declTechnique("HDR10", vsQuad, psCopy, Flags::HDR10);
auto & hdr16 = declTechnique("HDR16", vsQuad, psCopy, Flags::HDR16);
}
};
}
8 changes: 4 additions & 4 deletions data/Shaders/lighting/deferredLighting.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace vg::gfx

setFile("lighting/deferredLighting.hlsl");

addFlag(RayTracing, ShaderStageFlags::CS, "_RAYTRACING");
addFlag(Toolmode, ShaderStageFlags::CS, "_TOOLMODE");
declFlag(RayTracing, ShaderStageFlags::CS, "_RAYTRACING");
declFlag(Toolmode, ShaderStageFlags::CS, "_TOOLMODE");

auto & deferredLightingTech = addTechnique("DeferredLightingCS");
auto & deferredLightingTech = declTechnique("DeferredLightingCS");
{
deferredLightingTech.cs = addCS("CS_DeferredLighting");
deferredLightingTech.cs = declCS("CS_DeferredLighting");
deferredLightingTech.flags = (Flags)0;
}
}
Expand Down
21 changes: 10 additions & 11 deletions data/Shaders/postprocess/postprocess.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "system/bindless.hlsli"
#include "system/samplers.hlsli"
#include "system/view.hlsli"
#include "system/depthstencil.hlsli"

#if _TOOLMODE
#if _RAYTRACING
Expand Down Expand Up @@ -159,21 +160,19 @@ void CS_PostProcessMain(int2 dispatchThreadID : SV_DispatchThreadID)
int3 address = int3(dispatchThreadID.xy, 0);

float4 color = getTexture2D(postProcessConstants.getColor()).Load(address);

// Red contains depth.
float depth = getTexture2D(postProcessConstants.getDepth()).Load(address).r;

#ifdef VG_VULKAN
// Vulkan : Red contains stencil
uint stencil = getTexture2D_UInt2(postProcessConstants.getStencil()).Load(address).r;
#else
// DX12 : Green contains stencil
uint stencil = getTexture2D_UInt2(postProcessConstants.getStencil()).Load(address).g;
#endif
float depth = loadDepth(postProcessConstants.getDepth(), address);
uint stencil = loadStencil(postProcessConstants.getStencil(), address);

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

// Test multiple keywords
#if _FXAA
color.rgb = color.rgg;
#elif _SMAA
color.rgb = color.bba;
#endif

#if _TOOLMODE

#if _RAYTRACING
Expand Down
28 changes: 16 additions & 12 deletions data/Shaders/postprocess/postprocess.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ namespace vg::gfx
class PostProcessHLSLDesc : public HLSLDesc
{
public:
PostProcessHLSLDesc()
enum Flags : ShaderKey::Flags
{
enum Flags : ShaderKey::Flags
{
// Flags 13..15 are reserved and should be common for all shaders
ZOnly = HLSLDesc::Flags::ZOnly,
RayTracing = HLSLDesc::Flags::RayTracing,
Toolmode = HLSLDesc::Flags::Toolmode
};
// Bits 0..1 for AA mode
AntiAliasingMode = 0,

// Flags 13..15 are reserved and should be common for all shaders
//ZOnly = HLSLDesc::Flags::ZOnly,
RayTracing = HLSLDesc::Flags::RayTracing,
Toolmode = HLSLDesc::Flags::Toolmode
};

PostProcessHLSLDesc()
{
setFile("postprocess/postprocess.hlsl");

addFlag(RayTracing, ShaderStageFlags::CS, "_RAYTRACING");
addFlag(Toolmode, ShaderStageFlags::CS, "_TOOLMODE");
declFlags(AntiAliasingMode, ShaderStageFlags::CS, { "", "_FXAA", "_SMAA" } );
declFlag(RayTracing, ShaderStageFlags::CS, "_RAYTRACING");
declFlag(Toolmode, ShaderStageFlags::CS, "_TOOLMODE");

auto csPostProcessMain = addCS("CS_PostProcessMain");
auto csPostProcessMain = declCS("CS_PostProcessMain");

auto & quad = addTechnique("PostProcessMainCS");
auto & quad = declTechnique("PostProcessMainCS");
{
quad.cs = csPostProcessMain;
quad.flags = (Flags)0;
Expand Down
6 changes: 3 additions & 3 deletions data/Shaders/skinning/skinning.hlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace vg::gfx
{
setFile("skinning/skinning.hlsl");

auto csSkinningMain = addCS("CS_Skinning");
auto csSkinningMain = declCS("CS_Skinning");

auto & skinning = addTechnique("SkinningCS");
auto & skinning = declTechnique("SkinningCS");
{
skinning.cs = addVS("CS_Skinning");
skinning.cs = declVS("CS_Skinning");
}
}
};
Expand Down
1 change: 0 additions & 1 deletion data/Shaders/system/bindless.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ DECL_DESCRIPTOR_RANGE_RO(RaytracingAccelerationStructure, g_TLASTable, BINDLESS_
//
//RaytracingAccelerationStructure getTLAS(uint _handle) { return g_TLASTable[_handle - BINDLESS_TLAS_START]; }


#endif // __cplusplus
15 changes: 15 additions & 0 deletions data/Shaders/system/depthstencil.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _DEPTHSTENCIL__HLSLI_
#define _DEPTHSTENCIL__HLSLI_

//--------------------------------------------------------------------------------------
// Load Depth/Stencil components
//--------------------------------------------------------------------------------------
#define loadDepth(depthTex, address) getTexture2D(depthTex).Load(address).r;

#ifdef VG_VULKAN
#define loadStencil(stencilTex, address) getTexture2D_UInt2(stencilTex).Load(address).r;
#else
#define loadStencil(stencilTex, address) getTexture2D_UInt2(stencilTex).Load(address).g;
#endif

#endif // _DRIVER__HLSLI_
6 changes: 6 additions & 0 deletions src/gfx/Device/Device_consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ namespace vg::gfx
VSync_4
);

vg_enum_class(AntiAliasing, core::u8,
None = 0,
FXAA,
SMAA
);

vg_enum_class(HDR, core::u8,
None = 0,
HDR10,
Expand Down
16 changes: 15 additions & 1 deletion src/gfx/Shader/ComputeShaderKey.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#if VG_ENABLE_ASSERT
#define VG_VERIFY_COMPUTESHADERKEY_FLAGS 1
#endif

namespace vg::gfx
{
class ComputeShaderKey
Expand All @@ -19,8 +23,16 @@ namespace vg::gfx
void clear();
void init(const core::string & _file, const core::string & _technique);

inline void setFlags(core::uint _index, bool _enable = true)
#if VG_VERIFY_COMPUTESHADERKEY_FLAGS
void verifyComputeShaderKeyFlags(core::uint _index, core::uint _value);
#endif

inline void setFlag(core::uint _index, bool _enable)
{
#if VG_VERIFY_COMPUTESHADERKEY_FLAGS
verifyComputeShaderKeyFlags(_index, 1);
#endif

const ComputeShaderKey::Flags value = (ComputeShaderKey::Flags)(1 << _index);

if (_enable)
Expand All @@ -29,6 +41,8 @@ namespace vg::gfx
flags &= value;
}

void setFlag(core::uint _index, core::uint _value);

union
{
struct
Expand Down
29 changes: 29 additions & 0 deletions src/gfx/Shader/ComputeShaderKey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,33 @@ namespace vg::gfx
ShaderManager::get()->initComputeShaderKey(*this, _file, _technique);
}

//--------------------------------------------------------------------------------------
#if VG_VERIFY_COMPUTESHADERKEY_FLAGS
void ComputeShaderKey::verifyComputeShaderKeyFlags(core::uint _index, core::uint _value)
{
const HLSLDesc * desc = ShaderManager::get()->getHLSLDescriptor(file);
VG_ASSERT(nullptr != desc, "Could not get HLSL descriptor for shader file %u", file);
if (desc)
{
VG_ASSERT(desc->isValidFlagIndex(_index), "%u is not a valid index for shader \"%s\"", _index, desc->getFile().c_str());
VG_ASSERT(desc->isValidFlagValue(_index, _value), "%u is not a valid value at index %u for shader \"%s\"", _value, _index, desc->getFile().c_str());
}
}
#endif

//--------------------------------------------------------------------------------------
void ComputeShaderKey::setFlag(core::uint _index, core::uint _value)
{
#if VG_VERIFY_COMPUTESHADERKEY_FLAGS
verifyComputeShaderKeyFlags(_index, _value);
#endif

const HLSLDesc * desc = ShaderManager::get()->getHLSLDescriptor(file);
VG_ASSERT(nullptr != desc, "Could not get HLSL descriptor for shader file %u", file);
if (desc)
{
const uint mask = desc->getBitMask(_index);
flags = (flags & ~mask) | _value;
}
}
}
Loading

0 comments on commit 8b5fb04

Please sign in to comment.