Skip to content

Commit 1b223b7

Browse files
add reprojection to fog & gi
1 parent 6d4f786 commit 1b223b7

18 files changed

+287
-33
lines changed

GLSLTestbed/GLSLTestbed.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ xcopy /y /s "$(ProjectDir)res" "$(TargetDir)res\"</Command>
169169
<ClInclude Include="src\Core\NoCopy.h" />
170170
<ClInclude Include="src\ECS\Contextual\Builders\Builders.h" />
171171
<ClInclude Include="src\ECS\Contextual\Engines\EngineCommandInput.h" />
172+
<ClInclude Include="src\ECS\Contextual\Engines\EngineScreenshot.h" />
172173
<ClInclude Include="src\ECS\Contextual\Engines\EngineUpdateTransforms.h" />
173174
<ClInclude Include="src\ECS\Contextual\Components\Components.h" />
174175
<ClInclude Include="src\ECS\Contextual\EntityViews\EntityViews.h" />
@@ -818,6 +819,7 @@ xcopy /y /s "$(ProjectDir)res" "$(TargetDir)res\"</Command>
818819
<ClCompile Include="src\Core\CommandConfig.cpp" />
819820
<ClCompile Include="src\ECS\Contextual\Builders\Builders.cpp" />
820821
<ClCompile Include="src\ECS\Contextual\Engines\EngineCommandInput.cpp" />
822+
<ClCompile Include="src\ECS\Contextual\Engines\EngineScreenshot.cpp" />
821823
<ClCompile Include="src\ECS\Contextual\Engines\EngineUpdateTransforms.cpp" />
822824
<ClCompile Include="src\Rendering\Batching.cpp" />
823825
<ClCompile Include="src\Rendering\Culling.cpp" />

GLSLTestbed/GLSLTestbed.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,9 @@
11851185
<ClInclude Include="src\Core\ConsoleCommandBinding.h">
11861186
<Filter>Header Files</Filter>
11871187
</ClInclude>
1188+
<ClInclude Include="src\ECS\Contextual\Engines\EngineScreenshot.h">
1189+
<Filter>Header Files</Filter>
1190+
</ClInclude>
11881191
</ItemGroup>
11891192
<ItemGroup>
11901193
<Library Include="libs\glfw3.lib" />
@@ -1842,6 +1845,9 @@
18421845
<ClCompile Include="src\Core\CommandConfig.cpp">
18431846
<Filter>Source Files</Filter>
18441847
</ClCompile>
1848+
<ClCompile Include="src\ECS\Contextual\Engines\EngineScreenshot.cpp">
1849+
<Filter>Source Files</Filter>
1850+
</ClCompile>
18451851
</ItemGroup>
18461852
<ItemGroup>
18471853
<Text Include="x64\Debug\GLImageProcessor.log" />

GLSLTestbed/res/configs/CommandConfig-Active.keycfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Commands:
44
- ["C", "reload time"]
55
- ["V", "application contextual log_camera_transform"]
66
- ["G", "application contextual toggle_gizmos"]
7+
- ["N", "application contextual take_screenshot"]
78
- ["B", "reload appconfig res/configs/"]

GLSLTestbed/res/shaders/CS_SceneGI_Bake_Checkerboard.shader

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,59 @@ layout(rgba16) uniform writeonly image2D pk_SceneGI_DiffuseWrite;
99
layout(rgba16) uniform writeonly image2D pk_SceneGI_SpecularWrite;
1010
uniform int2 pk_SceneGI_Checkerboard_Offset = int2(0,0);
1111

12+
void ReprojectNeighbours(int2 basecoord, int2 coord, int2 size)
13+
{
14+
int2 coord0 = basecoord + ((pk_SceneGI_Checkerboard_Offset + int2(1)) % int2(2));
15+
int2 coord1 = int2(coord.x, coord0.y);
16+
int2 coord2 = int2(coord0.x, coord.y);
17+
18+
float3 worldposition0 = SampleWorldPosition(coord0, size);
19+
float3 worldposition1 = SampleWorldPosition(coord1, size);
20+
float3 worldposition2 = SampleWorldPosition(coord2, size);
21+
22+
float2 uv0 = ClipToUVW(mul(pk_MATRIX_L_VP, float4(worldposition0, 1.0f))).xy;
23+
float2 uv1 = ClipToUVW(mul(pk_MATRIX_L_VP, float4(worldposition1, 1.0f))).xy;
24+
float2 uv2 = ClipToUVW(mul(pk_MATRIX_L_VP, float4(worldposition2, 1.0f))).xy;
25+
26+
imageStore(pk_SceneGI_DiffuseWrite, coord0, tex2D(pk_ScreenGI_Diffuse, uv0));
27+
imageStore(pk_SceneGI_SpecularWrite, coord0, tex2D(pk_ScreenGI_Specular, uv0));
28+
29+
imageStore(pk_SceneGI_DiffuseWrite, coord1, tex2D(pk_ScreenGI_Diffuse, uv1));
30+
imageStore(pk_SceneGI_SpecularWrite, coord1, tex2D(pk_ScreenGI_Specular, uv1));
31+
32+
imageStore(pk_SceneGI_DiffuseWrite, coord2, tex2D(pk_ScreenGI_Diffuse, uv2));
33+
imageStore(pk_SceneGI_SpecularWrite, coord2, tex2D(pk_ScreenGI_Specular, uv2));
34+
}
35+
1236
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
1337
void main()
1438
{
1539
int2 size = imageSize(pk_SceneGI_DiffuseWrite).xy;
16-
int2 id = int2(gl_GlobalInvocationID.xy) * 2 + pk_SceneGI_Checkerboard_Offset;
40+
int2 basecoord = int2(gl_GlobalInvocationID.xy) * 2;
41+
int2 coord = basecoord + pk_SceneGI_Checkerboard_Offset;
1742

18-
if (any(Greater(id, size)))
43+
if (any(Greater(coord, size)))
1944
{
2045
return;
2146
}
2247

23-
float3 worldposition = SampleWorldPosition(id, size);
48+
float3 worldposition = SampleWorldPosition(coord, size);
2449

2550
if (Greater(abs(WorldToVoxelClipSpace(worldposition)), 1.0f.xxx))
2651
{
27-
imageStore(pk_SceneGI_DiffuseWrite, id, float4(0.0f.xxx, 1.0f));
28-
imageStore(pk_SceneGI_SpecularWrite, id, float4(0.0f.xxx, 1.0f));
52+
imageStore(pk_SceneGI_DiffuseWrite, coord, float4(0.0f.xxx, 1.0f));
53+
imageStore(pk_SceneGI_SpecularWrite, coord, float4(0.0f.xxx, 1.0f));
2954
return;
3055
}
3156

3257
// Find a base for the side cones with the normal as one of its base vectors.
33-
const float3 N = SampleWorldSpaceNormal(id);
58+
const float3 N = SampleWorldSpaceNormal(coord);
3459
const float3 O = worldposition;
3560
const float3 V = normalize(worldposition - pk_WorldSpaceCameraPos.xyz);
3661
const float3 R = reflect(V, N);
37-
const float3 D = GlobalNoiseBlue(uint2(id + pk_Time.xy * 512)).xyz;
62+
const float3 D = GlobalNoiseBlue(uint2(coord + pk_Time.xy * 512)).xyz;
3863

39-
imageStore(pk_SceneGI_DiffuseWrite, id, ConeTraceDiffuse(O, N, D.x));
40-
imageStore(pk_SceneGI_SpecularWrite, id, ConeTraceSpecular(O, N, R, D.y, SampleRoughness(id)));
64+
imageStore(pk_SceneGI_DiffuseWrite, coord, ConeTraceDiffuse(O, N, D.x));
65+
imageStore(pk_SceneGI_SpecularWrite, coord, ConeTraceSpecular(O, N, R, D.y, SampleRoughness(coord)));
66+
ReprojectNeighbours(basecoord, coord, size);
4167
}

GLSLTestbed/res/shaders/CS_VolumeFogLightDensity.shader

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ void main()
6666

6767
float density = Density(worldpos);
6868

69-
float4 preval = imageLoad(pk_Volume_Inject, int3(id));
69+
float4 preval = tex2D(pk_Volume_InjectRead, ReprojectWorldToCoord(worldpos));
7070
float4 curval = float4(pk_Volume_Intensity * density * color, density);
7171

72-
curval = lerp(preval, curval, VOLUME_ACCUMULATION_LD);
73-
curval.a = max(curval.a, VOLUME_MIN_DENSITY);
72+
curval = lerp(preval, curval, VOLUME_ACCUMULATION);
73+
curval.a = VOLUME_MIN_DENSITY + curval.a;
7474

7575
imageStore(pk_Volume_Inject, int3(id), curval);
7676
}

GLSLTestbed/res/shaders/CS_VolumeFogScatter.shader

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ void main()
99
{
1010
float4 accumulation = float4(0, 0, 0, 1);
1111
int3 pos = int3(gl_GlobalInvocationID.xy, 0);
12+
float3 vpos = ClipToViewPos((VOLUME_SIZE_ST.zz + pos.xy) * VOLUME_SIZE_ST.xy, 1.0f);
1213

1314
#pragma unroll VOLUME_DEPTH
1415
for (;pos.z < VOLUME_DEPTH; ++pos.z)
1516
{
16-
float slicewidth = GetVolumeSliceWidth(pos.z);
17+
float2 depths = GetVolumeCellDepth(float2(pos.z, pos.z + 1.0f));
18+
float depth = lerp(depths.x, depths.y, 0.5f);
19+
float slicewidth = depths.y - depths.x;
1720

1821
float4 slice = imageLoad(pk_Volume_Inject, pos);
1922

@@ -23,8 +26,8 @@ void main()
2326
accumulation.rgb += lightintegral * accumulation.a;
2427
accumulation.a *= transmittance;
2528

26-
float4 preval = imageLoad(pk_Volume_Scatter, pos);
27-
float4 outval = lerp(preval, accumulation, VOLUME_ACCUMULATION_SC);
29+
float4 preval = tex2D(pk_Volume_ScatterRead, ReprojectViewToCoord(vpos * depth));
30+
float4 outval = lerp(preval, accumulation, VOLUME_ACCUMULATION);
2831

2932
imageStore(pk_Volume_Scatter, pos, outval);
3033
}

GLSLTestbed/res/shaders/includes/PKCommon.glsl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ PK_DECLARE_CBUFFER(pk_PerFrameConstants)
6060
float4x4 pk_MATRIX_VP;
6161
// Current inverse view * projection matrix.
6262
float4x4 pk_MATRIX_I_VP;
63+
// Last view * projection matrix.
64+
float4x4 pk_MATRIX_L_VP;
6365

6466
// Scene reflections
6567
sampler2D pk_SceneOEM_HDR;
@@ -168,6 +170,14 @@ float3 ClipToViewPos(float2 uv, float linearDeth)
168170
return float3((uv * 2 - 1) * float2(pk_MATRIX_I_P[0][0], pk_MATRIX_I_P[1][1]), 1) * linearDeth;
169171
}
170172

173+
float3 ClipToUVW(float4 clippos)
174+
{
175+
float3 uvw = clippos.xyz / clippos.w;
176+
uvw.xy = uvw.xy * 0.5f + 0.5f;
177+
uvw.z = uvw.z * 0.5f + 0.5f;
178+
return uvw;
179+
}
180+
171181
float3 ScreenToViewPos(float3 screenpos)
172182
{
173183
float2 texCoord = screenpos.xy * pk_ScreenParams.zw;
@@ -191,10 +201,7 @@ float3x3 ComposeMikkTangentSpaceMatrix(float3 normal, float4 tangent)
191201
bool TryGetWorldToClipUVW(float3 worldpos, inout float3 uvw)
192202
{
193203
float4 clippos = WorldToClipPos(worldpos);
194-
uvw = clippos.xyz / clippos.w;
195-
uvw.xy = uvw.xy * 0.5f + 0.5f;
196-
uvw.z = uvw.z * 0.5f + 0.5f;
197-
204+
uvw = ClipToUVW(clippos);
198205
return clippos.z > 0.0f && all(lessThan(abs(clippos.xy / clippos.w), 1.0f.xx));
199206
}
200207

GLSLTestbed/res/shaders/includes/Reconstruction.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ float3 SampleViewPosition(float2 uv)
3636
float3 SampleViewPosition(int2 coord, int2 size)
3737
{
3838
float depth = SampleLinearDepth(coord);
39-
return ClipToViewPos(coord / float2(size), depth);
39+
return ClipToViewPos((coord + 0.5f.xx) / float2(size), depth);
4040
}
4141

4242
float3 SampleWorldPosition(float2 uv) { return mul(pk_MATRIX_I_V, float4(SampleViewPosition(uv), 1.0f)).xyz; }

GLSLTestbed/res/shaders/includes/VolumeFogShared.glsl

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
#define VOLUME_COMPOSITE_DITHER_AMOUNT 2.0f * float3(0.00625f, 0.0111111111111111f, 0.0078125f)
1111
#define VOLUME_DEPTH_BATCH_SIZE_PX 16
1212
#define VOLUME_MIN_DENSITY 0.000001f
13-
14-
#define VOLUME_ACCUMULATION_LD min(1.0f, 35.0f * max(pk_DeltaTime.x, 0.01f))
15-
#define VOLUME_ACCUMULATION_SC min(1.0f, 14.0f * max(pk_DeltaTime.x, 0.01f))
13+
#define VOLUME_ACCUMULATION clamp(20.0f * pk_DeltaTime.x, 0.01f, 1.0f)
1614

1715
PK_DECLARE_CBUFFER(pk_VolumeResources)
1816
{
@@ -28,11 +26,14 @@ PK_DECLARE_CBUFFER(pk_VolumeResources)
2826
float pk_Volume_NoiseFogScale;
2927
float pk_Volume_WindSpeed;
3028
sampler3D pk_Volume_ScatterRead;
29+
sampler3D pk_Volume_InjectRead;
3130
};
3231

3332
layout(rgba16f) uniform image3D pk_Volume_Inject;
3433
layout(rgba16f) uniform image3D pk_Volume_Scatter;
3534

35+
const float4x4 pk_MATRIX_LD_VP = mul(pk_MATRIX_L_VP, pk_MATRIX_I_V);
36+
3637
PK_DECLARE_BUFFER(uint, pk_VolumeMaxDepths);
3738

3839
#define VOLUME_LOAD_MAX_DEPTH(index) uintBitsToFloat(PK_BUFFER_DATA(pk_VolumeMaxDepths, index))
@@ -42,22 +43,35 @@ float GetVolumeCellDepth(float index)
4243
return pk_ProjectionParams.x * pow(pk_ExpProjectionParams.z, index / VOLUME_DEPTH);
4344
}
4445

45-
float GetVolumeWCoord(float depth)
46+
float2 GetVolumeCellDepth(float2 index)
4647
{
47-
return max(log2(depth) * pk_ExpProjectionParams.x + pk_ExpProjectionParams.y, 0.0);
48+
return pk_ProjectionParams.xx * pow(pk_ExpProjectionParams.zz, index * VOLUME_INV_DEPTH);
4849
}
4950

50-
float GetVolumeSliceWidth(int index)
51+
float GetVolumeWCoord(float depth)
5152
{
52-
float2 nf = pk_ProjectionParams.xx * pow(pk_ExpProjectionParams.zz, float2(index, index + 1) * VOLUME_INV_DEPTH);
53-
return nf.y - nf.x;
53+
return max(log2(depth) * pk_ExpProjectionParams.x + pk_ExpProjectionParams.y, 0.0);
5454
}
5555

5656
float3 GetVolumeCellNoise(uint3 id)
5757
{
5858
return GlobalNoiseBlue(id.xy + id.z * int2(VOLUME_WIDTH, VOLUME_HEIGHT) + int(pk_Time.w * 1000).xx);
5959
}
6060

61+
float3 ReprojectWorldToCoord(float3 worldpos)
62+
{
63+
float3 uvw = ClipToUVW(mul(pk_MATRIX_L_VP, float4(worldpos, 1.0f)));
64+
uvw.z = GetVolumeWCoord(LinearizeDepth(uvw.z));
65+
return uvw;
66+
}
67+
68+
float3 ReprojectViewToCoord(float3 viewpos)
69+
{
70+
float3 uvw = ClipToUVW(mul(pk_MATRIX_LD_VP, float4(viewpos, 1.0f)));
71+
uvw.z = GetVolumeWCoord(LinearizeDepth(uvw.z));
72+
return uvw;
73+
}
74+
6175
uint GetVolumeDepthTileIndex(float2 uv)
6276
{
6377
return uint(uv.x * VOLUME_WIDTH) + VOLUME_WIDTH * uint(uv.y * VOLUME_HEIGHT);

GLSLTestbed/src/Core/Application.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ECS/Contextual/Engines/EngineEditorCamera.h"
1414
#include "ECS/Contextual/Engines/EngineDebug.h"
1515
#include "ECS/Contextual/Engines/EngineCommandInput.h"
16+
#include "ECS/Contextual/Engines/EngineScreenshot.h"
1617
#include "ECS/Contextual/Engines/EngineUpdateTransforms.h"
1718
#include "Rendering/GraphicsAPI.h"
1819
#include <math.h>
@@ -59,6 +60,7 @@ namespace PK::Core
5960
auto renderPipeline = m_services->Create<RenderPipeline>(assetDatabase, entityDb, config);
6061
auto engineEditorCamera = m_services->Create<ECS::Engines::EngineEditorCamera>(time, config);
6162
auto engineUpdateTransforms = m_services->Create<ECS::Engines::EngineUpdateTransforms>(entityDb);
63+
auto engineScreenshot = m_services->Create<ECS::Engines::EngineScreenshot>();
6264
auto engineDebug = m_services->Create<ECS::Engines::EngineDebug>(assetDatabase, entityDb, config);
6365
auto gizmoRenderer = m_services->Create<GizmoRenderer>(sequencer, assetDatabase, config->EnableGizmos);
6466
auto engineCommands = m_services->Create<ECS::Engines::EngineCommandInput>(assetDatabase, sequencer, time, entityDb, commandConfig);
@@ -72,7 +74,7 @@ namespace PK::Core
7274
{ (int)UpdateStep::UpdateInput, { input } },
7375
{ (int)UpdateStep::UpdateEngines, { PK_STEP_S(engineDebug), PK_STEP_S(engineUpdateTransforms) }},
7476
{ (int)UpdateStep::PreRender, { PK_STEP_S(renderPipeline) }},
75-
{ (int)UpdateStep::Render, { PK_STEP_S(renderPipeline), PK_STEP_S(gizmoRenderer) }},
77+
{ (int)UpdateStep::Render, { PK_STEP_S(renderPipeline), PK_STEP_S(gizmoRenderer), PK_STEP_S(engineScreenshot) }},
7678
{ (int)UpdateStep::PostRender, { PK_STEP_S(renderPipeline) }},
7779
{ (int)UpdateStep::CloseFrame, { PK_STEP_S(renderPipeline), input, time }},
7880
}
@@ -89,7 +91,8 @@ namespace PK::Core
8991
engineCommands,
9092
{
9193
PK_STEP_T(engineEditorCamera, ConsoleCommandToken),
92-
PK_STEP_T(gizmoRenderer, ConsoleCommandToken)
94+
PK_STEP_T(gizmoRenderer, ConsoleCommandToken),
95+
PK_STEP_T(engineScreenshot, ConsoleCommandToken),
9396
}
9497
},
9598
{

0 commit comments

Comments
 (0)