Skip to content

Commit

Permalink
Added PostProcessDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGorobets committed Dec 6, 2023
1 parent ce847d2 commit 91de429
Show file tree
Hide file tree
Showing 15 changed files with 1,329 additions and 0 deletions.
1 change: 1 addition & 0 deletions Samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ endif()
if(TARGET Diligent-AssetLoader AND TARGET DiligentFX)
add_subdirectory(GLTFViewer)
add_subdirectory(Shadows)
add_subdirectory(PostProcessDemo)
endif()

add_subdirectory(ImguiDemo)
Expand Down
57 changes: 57 additions & 0 deletions Samples/PostProcessDemo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
cmake_minimum_required (VERSION 3.6)

project(PostProcessDemo CXX)

set(SOURCE
src/PostProcessDemo.cpp
)

set(INCLUDE
src/PostProcessDemo.hpp
)

set(SHADERS
assets/shaders/GenerateRoughnessNormal.fx
assets/shaders/ComputeMotionVectors.fx
assets/shaders/ApplyPostEffects.fx
)

set(EXTERNAL_SHADERS
../../../DiligentFX/Shaders/Common/public/BasicStructures.fxh
../../../DiligentFX/Shaders/Common/private/FullScreenTriangleVS.fx
../../../DiligentFX/Shaders/Common/private/FullScreenTriangleVSOutput.fxh
../../../DiligentFX/Shaders/PostProcess/ToneMapping/public/ToneMappingStructures.fxh
../../../DiligentFX/Shaders/PostProcess/ToneMapping/public/ToneMapping.fxh
)

set(ALL_SHADERS ${SHADERS} ${EXTERNAL_SHADERS})

add_sample_app("PostProcessDemo" "DiligentSamples/Samples" "${SOURCE}" "${INCLUDE}" "${ALL_SHADERS}" "${ASSETS}")

target_link_libraries(PostProcessDemo
PRIVATE
Diligent-AssetLoader
Diligent-RenderStateNotation
DiligentFX
)

target_include_directories(PostProcessDemo
PRIVATE
../../../DiligentFX/Shaders/Common/public/
)

foreach(FILE ${EXTERNAL_SHADERS})
# Copy external shaders
add_custom_command(TARGET PostProcessDemo PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}" "${CMAKE_CURRENT_SOURCE_DIR}/assets/shaders"
)
endforeach(FILE)

set_source_files_properties(${ALL_SHADERS} PROPERTIES
VS_DEPLOYMENT_LOCATION "shaders"
MACOSX_PACKAGE_LOCATION Resources/shaders
)

# We have to use a different group name (Assets with capital A) to override grouping that was set by add_sample_app
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/assets PREFIX Assets FILES ${ASSETS} ${SHADERS})
source_group("Assets\\shaders" FILES ${EXTERNAL_SHADERS})
15 changes: 15 additions & 0 deletions Samples/PostProcessDemo/assets/shaders/ApplyPostEffects.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "ToneMapping.fxh"

cbuffer cbToneMappingAttribs
{
ToneMappingAttribs g_ToneMappingAttribs;
}

Texture2D<float3> g_TextureRadiance;
Texture2D<float3> g_TextureRadianceSSR;

float4 ApplyPostEffectsPS(in float4 Position : SV_Position) : SV_Target
{
float3 AccumulatedRadiance = 0.001 * g_TextureRadiance.Load(int3(Position.xy, 0)) + g_TextureRadianceSSR.Load(int3(Position.xy, 0));
return float4(ToneMap(AccumulatedRadiance, g_ToneMappingAttribs, asfloat(g_ToneMappingAttribs.Padding0)), 1.0);
}
162 changes: 162 additions & 0 deletions Samples/PostProcessDemo/assets/shaders/BasicStructures.fxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#ifndef _BASIC_STRUCTURES_FXH_
#define _BASIC_STRUCTURES_FXH_


#ifdef __cplusplus

# ifndef BOOL
# define BOOL int32_t // Do not use bool, because sizeof(bool)==1 !
# endif

# ifndef CHECK_STRUCT_ALIGNMENT
// Note that defining empty macros causes GL shader compilation error on Mac, because
// it does not allow standalone semicolons outside of main.
// On the other hand, adding semicolon at the end of the macro definition causes gcc error.
# define CHECK_STRUCT_ALIGNMENT(s) static_assert( sizeof(s) % 16 == 0, "sizeof(" #s ") is not multiple of 16" )
# endif

# ifndef DEFAULT_VALUE
# define DEFAULT_VALUE(x) =x
# endif

#else

# ifndef BOOL
# define BOOL bool
# endif

# ifndef DEFAULT_VALUE
# define DEFAULT_VALUE(x)
# endif

#endif


struct CascadeAttribs
{
float4 f4LightSpaceScale;
float4 f4LightSpaceScaledBias;
float4 f4StartEndZ;

// Cascade margin in light projection space ([-1, +1] x [-1, +1] x [-1(GL) or 0, +1])
float4 f4MarginProjSpace;
};
#ifdef CHECK_STRUCT_ALIGNMENT
CHECK_STRUCT_ALIGNMENT(CascadeAttribs);
#endif

#define SHADOW_MODE_PCF 1
#define SHADOW_MODE_VSM 2
#define SHADOW_MODE_EVSM2 3
#define SHADOW_MODE_EVSM4 4
#ifndef SHADOW_MODE
# define SHADOW_MODE SHADOW_MODE_PCF
#endif

#define MAX_CASCADES 8
struct ShadowMapAttribs
{
// 0
#ifdef __cplusplus
float4x4 mWorldToLightViewT; // Matrices in HLSL are COLUMN-major while float4x4 is ROW major
#else
matrix mWorldToLightView; // Transform from view space to light projection space
#endif
// 16
CascadeAttribs Cascades[MAX_CASCADES];

#ifdef __cplusplus
float4x4 mWorldToShadowMapUVDepthT[MAX_CASCADES];
float fCascadeCamSpaceZEnd[MAX_CASCADES];
#else
matrix mWorldToShadowMapUVDepth[MAX_CASCADES];
float4 f4CascadeCamSpaceZEnd[MAX_CASCADES/4];
#endif

float4 f4ShadowMapDim; // Width, Height, 1/Width, 1/Height

// Number of shadow cascades
int iNumCascades DEFAULT_VALUE(0);
float fNumCascades DEFAULT_VALUE(0);
// Do not use bool, because sizeof(bool)==1 !
BOOL bVisualizeCascades DEFAULT_VALUE(0);
BOOL bVisualizeShadowing DEFAULT_VALUE(0);

float fReceiverPlaneDepthBiasClamp DEFAULT_VALUE(10);
float fFixedDepthBias DEFAULT_VALUE(1e-5f);
float fCascadeTransitionRegion DEFAULT_VALUE(0.1f);
int iMaxAnisotropy DEFAULT_VALUE(4);

float fVSMBias DEFAULT_VALUE(1e-4f);
float fVSMLightBleedingReduction DEFAULT_VALUE(0);
float fEVSMPositiveExponent DEFAULT_VALUE(40);
float fEVSMNegativeExponent DEFAULT_VALUE(5);

BOOL bIs32BitEVSM DEFAULT_VALUE(1);
int iFixedFilterSize DEFAULT_VALUE(3); // 3x3 filter
float fFilterWorldSize DEFAULT_VALUE(0);
BOOL fDummy;
};
#ifdef CHECK_STRUCT_ALIGNMENT
CHECK_STRUCT_ALIGNMENT(ShadowMapAttribs);
#endif

struct LightAttribs
{
float4 f4Direction DEFAULT_VALUE(float4(0, 0,-1, 0));
float4 f4AmbientLight DEFAULT_VALUE(float4(0, 0, 0, 0));
float4 f4Intensity DEFAULT_VALUE(float4(1, 1, 1, 1));

ShadowMapAttribs ShadowAttribs;
};
#ifdef CHECK_STRUCT_ALIGNMENT
CHECK_STRUCT_ALIGNMENT(LightAttribs);
#endif

struct CameraAttribs
{
float4 f4Position; // Camera world position
float4 f4ViewportSize; // (width, height, 1/width, 1/height)

float2 f2ViewportOrigin; // (min x, min y)
float fNearPlaneZ;
float fFarPlaneZ; // fNearPlaneZ < fFarPlaneZ

#ifdef __cplusplus
float4x4 mViewT;
float4x4 mProjT;
float4x4 mViewProjT;
float4x4 mViewInvT;
float4x4 mProjInvT;
float4x4 mViewProjInvT;

float4x4 mPrevViewT;
float4x4 mPrevProjT;
float4x4 mPrevViewProjT;
float4x4 mPrevViewInvT;
float4x4 mPrevProjInvT;
float4x4 mPrevViewProjInvT;
#else
matrix mView;
matrix mProj;
matrix mViewProj;
matrix mViewInv;
matrix mProjInv;
matrix mViewProjInv;

matrix mPrevView;
matrix mPrevProj;
matrix mPrevViewProj;
matrix mPrevViewInv;
matrix mPrevProjInv;
matrix mPrevViewProjInv;
#endif

float4 f4ExtraData[5]; // Any appliation-specific data
// Sizeof(CameraAttribs) == 256*2
};
#ifdef CHECK_STRUCT_ALIGNMENT
CHECK_STRUCT_ALIGNMENT(CameraAttribs);
#endif

#endif //_BASIC_STRUCTURES_FXH_
41 changes: 41 additions & 0 deletions Samples/PostProcessDemo/assets/shaders/ComputeMotionVectors.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "BasicStructures.fxh"

cbuffer cbCameraAttribs
{
CameraAttribs g_CameraAttibs;
}

Texture2D<float> g_InputDepth;

float2 ScreenSpaceToNDC(float2 Location, float2 InvDimension)
{
float2 NDC = 2.0f * Location.xy * InvDimension - 1.0f;
return float2(NDC.x, -NDC.y);
}

bool IsBackground(float depth)
{
#if SSR_OPTION_INVERTED_DEPTH
return depth < 1.e-6f;
#else
return depth >= (1.0f - 1.e-6f);
#endif
}


float2 ComputeMotionVectorsPS(in float4 Position : SV_Position) : SV_Target
{
float depth = g_InputDepth.Load(int3(Position.xy, 0.0));

float2 ndcXY1 = ScreenSpaceToNDC(Position.xy, g_CameraAttibs.f4ViewportSize.zw);

float4 position = mul(float4(ndcXY1, depth, 1.0f), g_CameraAttibs.mViewProjInv);
position /= position.w;

float4 ndcXY0 = mul(position, g_CameraAttibs.mPrevViewProj);
ndcXY0 = ndcXY0 / ndcXY0.w;

float2 motion = -0.5 * (ndcXY1.xy - ndcXY0.xy);
motion.y = -motion.y;
return !IsBackground(depth) ? motion : float2(0.0, 0.0);
}
21 changes: 21 additions & 0 deletions Samples/PostProcessDemo/assets/shaders/FullScreenTriangleVS.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "FullScreenTriangleVSOutput.fxh"

void FullScreenTriangleVS(in uint VertexId : SV_VertexID,
in uint InstID : SV_InstanceID,
out FullScreenTriangleVSOutput VSOut)
{
float2 PosXY[3];
PosXY[0] = float2(-1.0, -1.0);
PosXY[1] = float2(-1.0, +3.0);
PosXY[2] = float2(+3.0, -1.0);

float2 f2XY = PosXY[VertexId];
VSOut.f2NormalizedXY = f2XY;
VSOut.fInstID = float( InstID );

// Write 0 to the depth buffer
// NDC_MIN_Z == 0 in DX
// NDC_MIN_Z == -1 in GL
float z = NDC_MIN_Z;
VSOut.f4PixelPos = float4(f2XY, z, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

struct FullScreenTriangleVSOutput
{
float4 f4PixelPos : SV_Position; // Pixel position on the screen
float2 f2NormalizedXY : NORMALIZED_XY; // Normalized device XY coordinates [-1,1]x[-1,1]
float fInstID : INSTANCE_ID;
};
38 changes: 38 additions & 0 deletions Samples/PostProcessDemo/assets/shaders/GenerateRoughnessNormal.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "BasicStructures.fxh"

cbuffer cbCameraAttribs
{
CameraAttribs g_CameraAttibs;
}

Texture2D<float> g_InputDepth;

struct PSOutput
{
float4 MaterialParameters : SV_Target0;
float3 Normal : SV_Target1;
};

float2 ScreenSpaceToNDC(float2 Location, float2 InvDimension)
{
float2 NDC = 2.0f * Location.xy * InvDimension - 1.0f;
return float2(NDC.x, -NDC.y);
}

float3 GetPositionWS(float2 Location, float Depth)
{
float2 NDC = ScreenSpaceToNDC(Location, g_CameraAttibs.f4ViewportSize.zw);
float4 Position = mul(float4(NDC, Depth, 1.0), g_CameraAttibs.mViewProjInv);
return Position.xyz / Position.w;
}

PSOutput GenerateRoughnessNormalPS(in float4 Position : SV_Position)
{
float3 WorldPosition = GetPositionWS(Position.xy, g_InputDepth.Load(int3(Position.xy, 0)));
float3 DepthNormal = normalize(cross(ddx(WorldPosition), ddy(WorldPosition)));

PSOutput Output;
Output.Normal = DepthNormal;
Output.MaterialParameters = float4(g_CameraAttibs.f4ExtraData[0].x, 0.0, 0.0, 0.0);
return Output;
}
31 changes: 31 additions & 0 deletions Samples/PostProcessDemo/assets/shaders/RenderPBR_Structures.fxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _RENDER_PBR_STRUCTURES_FXH_
#define _RENDER_PBR_STRUCTURES_FXH_

// #include "BasicStructures.fxh"
// #include "PBR_Structures.fxh"


struct PBRFrameAttribs
{
CameraAttribs Camera;
PBRRendererShaderParameters Renderer;
PBRLightAttribs Light;
};
#ifdef CHECK_STRUCT_ALIGNMENT
CHECK_STRUCT_ALIGNMENT(PBRFrameAttribs);
#endif


struct PBRPrimitiveAttribs
{
GLTFNodeShaderTransforms Transforms;
PBRMaterialShaderInfo Material;

float4 CustomData;
};
#ifdef CHECK_STRUCT_ALIGNMENT
CHECK_STRUCT_ALIGNMENT(PBRPrimitiveAttribs);
#endif


#endif // _RENDER_PBR_STRUCTURES_FXH_
Loading

0 comments on commit 91de429

Please sign in to comment.