Skip to content

Commit

Permalink
vsmfog optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Nov 25, 2024
1 parent dc4114b commit 101e5ef
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 43 deletions.
19 changes: 18 additions & 1 deletion game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ void Renderer::resetSwapchain() {
vsm.uboFogPages = device.descriptors(Shaders::inst().vsmFogPages);
vsm.uboFogShadow = device.descriptors(Shaders::inst().vsmFogShadow);
vsm.uboFogSample = device.descriptors(Shaders::inst().vsmFogSample);
vsm.uboFogTrace = device.descriptors(Shaders::inst().vsmFogTrace);
vsm.uboClump = device.descriptors(Shaders::inst().vsmClumpPages);
vsm.uboAlloc = device.descriptors(Shaders::inst().vsmAllocPages);

Expand All @@ -212,10 +213,10 @@ void Renderer::resetSwapchain() {
vsm.pageTbl = device.image3d(TextureFormat::R32U, 32, 32, 16);
vsm.pageHiZ = device.image3d(TextureFormat::R32U, 32, 32, 16);
vsm.pageData = device.zbuffer(shadowFormat, 8192, 8192);
// vsm.pageDataCs = device.image2d(TextureFormat::R32U, 4096, 4096);

// vsm.ssTrace = device.image2d(TextureFormat::RGBA8, w, h);
vsm.ssTrace = device.image2d(TextureFormat::R32U, w, h);
vsm.fogDbg = device.image2d(TextureFormat::RGBA8, w, h);
vsm.epTrace = device.image2d(TextureFormat::R16, 1024, 2*1024);
vsm.epipoles = device.ssbo(nullptr, Shaders::inst().vsmFogEpipolar.sizeOfBuffer(3, size_t(vsm.epTrace.h())));

Expand Down Expand Up @@ -504,6 +505,15 @@ void Renderer::prepareUniforms() {
vsm.uboFogSample.set(3, vsm.epipoles);
vsm.uboFogSample.set(4, zbuffer);

vsm.uboFogTrace.set(0, vsm.fogDbg);
vsm.uboFogTrace.set(1, vsm.epTrace);
vsm.uboFogTrace.set(2, wview->sceneGlobals().uboGlobal[SceneGlobals::V_Main]);
vsm.uboFogTrace.set(3, vsm.epipoles);
vsm.uboFogTrace.set(4, zbuffer);
vsm.uboFogTrace.set(5, vsm.pageTbl);
vsm.uboFogTrace.set(6, vsm.pageData);
vsm.uboFogTrace.set(7, wview->sky().fogLut3d());

vsm.uboClump.set(0, vsm.pageList);
vsm.uboClump.set(1, vsm.pageTbl);

Expand Down Expand Up @@ -959,6 +969,13 @@ void Renderer::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fI
cmd.setUniforms(shaders.vsmFogSample, vsm.uboFogSample);
cmd.dispatchThreads(zbuffer.size());
}

if(false) {
// experimental
cmd.setFramebuffer({});
cmd.setDebugMarker("VSM-trace");
cmd.setUniforms(shaders.vsmFogTrace, vsm.uboFogTrace);
cmd.dispatchThreads(zbuffer.size());
}

void Renderer::drawSwr(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fId, WorldView& view) {
Expand Down
5 changes: 3 additions & 2 deletions game/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ class Renderer final {
Tempest::DescriptorSet uboClear;
Tempest::DescriptorSet uboClearPages;
Tempest::DescriptorSet uboPages;
Tempest::DescriptorSet uboEpipole, uboFogPages, uboFogSample, uboFogShadow;

Tempest::DescriptorSet uboEpipole, uboFogPages, uboFogSample, uboFogShadow, uboFogTrace;
Tempest::DescriptorSet uboClump, uboAlloc;

Tempest::RenderPipeline* directLightPso = nullptr;
Expand All @@ -233,6 +232,8 @@ class Renderer final {
Tempest::StorageImage ssTrace;
Tempest::StorageImage epTrace;
Tempest::StorageBuffer epipoles;

Tempest::StorageImage fogDbg;
} vsm;

struct {
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Shaders::Shaders() {
skyViewCldLut = postEffect("sky", "sky_view_clouds_lut");

fogViewLut3d = computeShader("fog_view_lut.comp.sprv");
shadowDownsample = computeShader("shadow_downsample.comp.sprv");
fogOcclusion = computeShader("fog3d.comp.sprv");

skyExposure = computeShader("sky_exposure.comp.sprv");
Expand Down Expand Up @@ -209,6 +208,7 @@ Shaders::Shaders() {
vsmFogPages = computeShader("vsm_fog_mark_pages.comp.sprv");
vsmFogShadow = computeShader("vsm_fog_shadow.comp.sprv");
vsmFogSample = computeShader("vsm_fog_sample.comp.sprv");
vsmFogTrace = computeShader("vsm_fog_trace.comp.sprv");

vsmDirectLight = postEffect("copy", "direct_light_vsm", RenderState::ZTestMode::NoEqual);
vsmDbg = postEffect("copy", "vsm_dbg", RenderState::ZTestMode::Always);
Expand Down
4 changes: 2 additions & 2 deletions game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Shaders {
Tempest::RenderPipeline fog3dHQ;
Tempest::RenderPipeline sun;
Tempest::ComputePipeline cloudsLut, fogOcclusion;
Tempest::ComputePipeline fogViewLut3d, shadowDownsample;
Tempest::ComputePipeline fogViewLut3d;
Tempest::ComputePipeline skyExposure;

Tempest::RenderPipeline skyPathTrace;
Expand Down Expand Up @@ -79,7 +79,7 @@ class Shaders {
Tempest::ComputePipeline vsmClear, vsmClearPages, vsmMarkPages;
Tempest::ComputePipeline vsmTrimPages, vsmListPages, vsmClumpPages, vsmAllocPages, vsmMergePages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
Tempest::ComputePipeline vsmFogEpipolar, vsmFogPages, vsmFogShadow, vsmFogSample;
Tempest::ComputePipeline vsmFogEpipolar, vsmFogPages, vsmFogShadow, vsmFogSample, vsmFogTrace;
Tempest::RenderPipeline vsmDirectLight;
Tempest::RenderPipeline vsmDbg;

Expand Down
2 changes: 0 additions & 2 deletions game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,12 @@ void Sky::setupSettings() {
case None:
case VolumetricLQ:
fogLut3D = device.image3d(lutRGBAFormat, 160, 90, 64);
shadowDw = StorageImage();
occlusionLut = StorageImage();
break;
case VolumetricHQ:
case VolumetricHQVsm:
// fogLut and oclussion are decupled
fogLut3D = device.image3d(lutRGBAFormat,128,64,32);
shadowDw = StorageImage();
break;
case PathTrace:
break;
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/sky/sky.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Sky final {
Tempest::TextureFormat lutRGBFormat = Tempest::TextureFormat::R11G11B10UF;
Tempest::TextureFormat lutRGBAFormat = Tempest::TextureFormat::RGBA16F;
Tempest::Attachment transLut, multiScatLut, viewLut, viewCldLut;
Tempest::StorageImage cloudsLut, fogLut3D, shadowDw;
Tempest::StorageImage cloudsLut, fogLut3D;
Tempest::StorageImage occlusionLut, irradianceLut;

Tempest::DescriptorSet uboClouds;
Expand Down
3 changes: 1 addition & 2 deletions shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ add_shader(sky_multi_scattering.frag sky/sky_multi_scattering.frag)
add_shader(sky_view_lut.frag sky/sky_view_lut.frag)
add_shader(sky_view_clouds_lut.frag sky/sky_view_clouds_lut.frag)

add_shader(shadow_downsample.comp sky/shadow_downsample.comp)
add_shader(clouds_lut.comp sky/clouds_lut.comp)

add_shader(fog_view_lut.comp sky/fog_view_lut.comp)

add_shader(fog3d.comp sky/fog.frag -S comp -DVOLUMETRIC)
Expand Down Expand Up @@ -315,6 +313,7 @@ add_shader(vsm_fog_epipolar.comp virtual_shadow/vsm_fog_epipolar.comp)
add_shader(vsm_fog_shadow.comp virtual_shadow/vsm_fog_shadow.comp)
add_shader(vsm_fog_mark_pages.comp virtual_shadow/vsm_fog_shadow.comp -DPAGES)
add_shader(vsm_fog_sample.comp virtual_shadow/vsm_fog_sample.comp)
add_shader(vsm_fog_trace.comp virtual_shadow/vsm_fog_trace.comp)

add_shader(vsm_rendering.comp virtual_shadow/vsm_rendering.comp)

Expand Down
2 changes: 2 additions & 0 deletions shader/virtual_shadow/vsm_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct Epipole {
vec2 rayDir;
float tMin;
float tMax;
float dBegin;
float dEnd;
};

uint packVsmPageInfo(ivec3 at, ivec2 size) {
Expand Down
20 changes: 12 additions & 8 deletions shader/virtual_shadow/vsm_fog_epipolar.comp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const float dFogMax = 0.9999;

shared vec2 rayOrig, rayDir;
shared uint tMinUint, tMaxUint;
shared uint dBeginUint, dEndUint;
shared uint tDbg;

vec4 dbgColor = vec4(0,1,0,0);
Expand Down Expand Up @@ -93,8 +94,8 @@ vec2 rayPosition2d() {
return vec2(float(viewportSize.x-0.5)/float(viewportSize.x), (rayId+0.5)/float(vert));
}

void minMaxRayT(const vec2 rposIn) {
const vec2 rpos = rposIn - rayOrig;
void minMaxRayT(const vec3 rposIn) {
const vec2 rpos = rposIn.xy - rayOrig;
const float t = dot(rpos, rayDir);
if(t<0) {
// atomicAdd(tDbg, 1);
Expand All @@ -103,7 +104,6 @@ void minMaxRayT(const vec2 rposIn) {

atomicMax(tMaxUint, floatBitsToUint(t));
atomicMin(tMinUint, floatBitsToUint(t));

//float d = abs(dot(normalize(rpos), rayDir));
//atomicMin(tDbg, floatBitsToUint(d));
}
Expand Down Expand Up @@ -134,26 +134,30 @@ void trace(const vec2 src, const vec2 dst) {
const float z = min(texelFetch(depth, ivec2(pix*viewportSize), 0).x, dFogMax);

const vec2 inPos = pix*2.0-1.0;
const vec2 shPos0 = screenToShadow(vec3(inPos, 0)).xy;
const vec2 shPos1 = screenToShadow(vec3(inPos, z)).xy;
const vec3 shPos0 = screenToShadow(vec3(inPos, 0));
const vec3 shPos1 = screenToShadow(vec3(inPos, z));

minMaxRayT(shPos0);
minMaxRayT(shPos1);

//imageStore(result, ivec2(pix*viewportSize), dbgColor);
//imageStore(result, ivec2(pix*viewportSize), vec4(dbg0, dbg1, 1, 1));
atomicMin(dBeginUint, floatBitsToUint(shPos0.z));
atomicMin(dEndUint, floatBitsToUint(shPos1.z));
}
barrier();

const float tMin = uintBitsToFloat(tMinUint);
const float tMax = uintBitsToFloat(tMaxUint);
const float dBegin = uintBitsToFloat(dBeginUint);
const float dEnd = uintBitsToFloat(dEndUint);
const uint sliceId = gl_WorkGroupID.x;

if(true && lane==0) {
if(lane==0) {
epipole[sliceId].rayOrig = rayOrig;
epipole[sliceId].rayDir = rayDir;
epipole[sliceId].tMin = tMin;
epipole[sliceId].tMax = tMax;
epipole[sliceId].dBegin = dBegin;
epipole[sliceId].dEnd = dEnd;
}
}

Expand Down
26 changes: 4 additions & 22 deletions shader/virtual_shadow/vsm_fog_sample.comp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ layout(local_size_x = 8, local_size_y = 8) in;

layout(binding = 0, r32ui) uniform uimage2D result;
//layout(binding = 0, rgba8) uniform image2D result;
layout(binding = 1, rgba8) uniform image2D rayData;
layout(binding = 1) uniform texture2D rayData;
layout(binding = 2, std140) uniform UboScene {
SceneDesc scene;
};
Expand All @@ -22,8 +22,8 @@ layout(binding = 4) uniform texture2D depth;

const float dFogMin = 0;
const float dFogMax = 0.9999;
uint NumSamples = imageSize(rayData).x;
uint NumSlices = imageSize(rayData).y;
uint NumSamples = textureSize(rayData,0).x;
uint NumSlices = textureSize(rayData,0).y;
ivec2 fragCoord = ivec2(gl_GlobalInvocationID.xy);
ivec2 viewportSize = textureSize(depth, 0);

Expand Down Expand Up @@ -134,35 +134,17 @@ void trace(const vec2 inPos, const float z, ivec2 pix, const uint sliceId) {
//vec3 shPos = shPos1;
float shV = dot(shPos.xy - rayA, rayDir) / (tMax-tMin);

float sh = imageLoad(rayData, ivec2(shV*NumSamples, sliceId)).x;
float sh = texelFetch(rayData, ivec2(shV*NumSamples, sliceId), 0).x;
bool shadow = (sh < shPos.z);

occlusion = occlusion | ((shadow ? 1u : 0u) << uint(i));
if(i+1==steps) {
//float x = dot(normalize(shPos.xy/shPos.w - rayA), rayDir);
//imageStore(result, pix, vec4(abs(x)*dbgColor));
//imageStore(result, pix, vec4(dbgColor));
//imageStore(result, pix, vec4(sh));
}
}

//imageStore(result, pix, vec4(bitCount(occlusion)/float(steps)));
imageStore(result, pix, uvec4(occlusion));
}

void dbgEpipole(const vec2 inPos, float z) {
const vec3 debugColors[] = {
vec3(1,1,1),
vec3(1,0,0),
vec3(0,1,0),
vec3(0,0,1),
vec3(1,1,0),
vec3(1,0,1),
vec3(0,1,1),
vec3(1,0.5,0),
vec3(0.5,1,0),
vec3(0,0.5,1),
};
const vec2 sun = sunPosition2d();
const vec3 camPos = wrldToShadow(scene.camPos);
const vec3 shPos1 = screenToShadow(vec3(inPos, 1));
Expand Down
9 changes: 7 additions & 2 deletions shader/virtual_shadow/vsm_fog_shadow.comp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,20 @@ void innerMain() {
vec2 rayDir = epipole[sliceId].rayDir;
float tMin = epipole[sliceId].tMin;
float tMax = epipole[sliceId].tMax;
float dBegin = epipole[sliceId].dBegin;
float dEnd = epipole[sliceId].dEnd;

const float a = (i+0.5)/float(NumSamples);
const float t = tMin + a*(tMax-tMin);
// const float depth = dBegin + a*(dBegin-dEnd);
const float depth = min(dBegin, dEnd);
const vec2 shPos = rayOrig + t*rayDir;
#if !defined(PAGES)
const float sh = shadowTexelFetch(shPos);
imageStore(rayData, ivec2(i, sliceId), vec4(sh));
//imageStore(rayData, ivec2(i, sliceId), vec4(sh));
imageStore(rayData, ivec2(i, sliceId), vec4(sh<depth ? 0 : sh));
#else
markPage(vec3(shPos,0));
markPage(vec3(shPos,depth));
#endif
}

Expand Down
Loading

0 comments on commit 101e5ef

Please sign in to comment.