Skip to content

Commit

Permalink
some tuning for vsm fog
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Oct 19, 2024
1 parent 32d98cd commit cf461b4
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 158 deletions.
9 changes: 4 additions & 5 deletions game/graphics/drawcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,14 @@ void DrawCommands::updateTasksUniforms() {
i.desc.set(T_Clusters, clusters.ssbo());
i.desc.set(T_Indirect, views[i.viewport].indirectCmd);
i.desc.set(T_Payload, views[i.viewport].visClusters);
i.desc.set(T_Scene, scene.uboGlobal[i.viewport]);
i.desc.set(T_Instance, owner.instanceSsbo());
i.desc.set(T_Bucket, buckets.ssbo());

if(i.viewport!=SceneGlobals::V_Vsm) {
i.desc.set(T_Scene, scene.uboGlobal[i.viewport]);
i.desc.set(T_Instance, owner.instanceSsbo());
i.desc.set(T_Bucket, buckets.ssbo());
i.desc.set(T_HiZ, *scene.hiZ);
} else {
i.desc.set(T_Scene, scene.uboGlobal[i.viewport]);
i.desc.set(T_Payload, views[i.viewport].vsmClusters);
i.desc.set(T_Payload, views[i.viewport].vsmClusters); //unsorted clusters
i.desc.set(T_HiZ, *scene.vsmPageHiZ);
i.desc.set(T_VsmPages, *scene.vsmPageList);
i.desc.set(8, scene.vsmDbg);
Expand Down
5 changes: 3 additions & 2 deletions game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void Renderer::resetSwapchain() {
if(settings.vsmEnabled && (i+1)!=Resources::ShadowLayers)
continue;
if(settings.vsmEnabled && !settings.giEnabled)
continue; //TODO: support vsm in gi code
;//continue; //TODO: support vsm in gi code
shadowMap[i] = device.zbuffer(shadowFormat,smSize,smSize);
}
}
Expand Down Expand Up @@ -486,6 +486,7 @@ void Renderer::prepareUniforms() {
if(!vsm.pageDataCs.isEmpty())
vsm.uboLight.set(6, vsm.pageDataCs); else
vsm.uboLight.set(6, vsm.pageData);
vsm.uboLight.set(8, wview->sceneGlobals().vsmDbg);

vsm.uboReproj.set(0, wview->sceneGlobals().uboGlobal[SceneGlobals::V_Main]);
vsm.uboReproj.set(1, vsm.pageTbl);
Expand Down Expand Up @@ -878,7 +879,7 @@ void Renderer::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fI
cmd.dispatchThreads(zbuffer.size());

// sky&fog
if(wview.sky().isVolumetric()) {
if(true && wview.sky().isVolumetric()) {
cmd.setUniforms(shaders.vsmMarkFogPages, vsm.uboPages);
cmd.dispatchThreads(zbuffer.size());
// wview.vsmMarkSkyPages(cmd, fId);
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Renderer final {

float vidResIndex = 0;

float vsmMipBias = -0.25;
float vsmMipBias = 0.25; //TODO: set to lower, eventually
} settings;

Frustrum frustrum[SceneGlobals::V_Count];
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/sceneglobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SceneGlobals::SceneGlobals() {
vsmPageData = &Resources::fallbackBlack();
vsmPageTbl = &Resources::fallbackImage3d();
vsmPageHiZ = &Resources::fallbackImage3d();
vsmDbg = device.image2d(Tempest::TextureFormat::R32U, 32, 32);
vsmDbg = device.image2d(Tempest::TextureFormat::R32U, 64, 64);

for(uint8_t lay=0; lay<V_Count; ++lay) {
uboGlobal[lay] = device.ssbo(nullptr,sizeof(UboGlobal));
Expand Down
1 change: 0 additions & 1 deletion game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ Shaders::Shaders() {
vsmClearPages = computeShader("vsm_clear_pages.comp.sprv");
vsmMarkPages = computeShader("vsm_mark_pages.comp.sprv");
vsmMarkFogPages = computeShader("vsm_mark_fog_pages.comp.sprv");
vsmMarkSky = computeShader("fog3d_vsm_mark_pages.comp.sprv");
vsmTrimPages = computeShader("vsm_trim_pages.comp.sprv");
vsmClumpPages = computeShader("vsm_clump_pages.comp.sprv");
vsmListPages = computeShader("vsm_list_pages.comp.sprv");
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Shaders {

// Virtual shadow
Tempest::ComputePipeline vsmClusterTask;
Tempest::ComputePipeline vsmClear, vsmClearPages, vsmMarkPages, vsmMarkFogPages, vsmMarkSky;
Tempest::ComputePipeline vsmClear, vsmClearPages, vsmMarkPages, vsmMarkFogPages;
Tempest::ComputePipeline vsmTrimPages, vsmListPages, vsmClumpPages, vsmAllocPages, vsmMergePages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
Tempest::RenderPipeline vsmDirectLight;
Expand Down
20 changes: 0 additions & 20 deletions game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,6 @@ void Sky::prepareUniforms() {
uboOcclusion.set(4, *scene.shadowMap[1], Resources::shadowSampler());
}

if(quality==VolumetricHQVsm) {
uboVsmPages = device.descriptors(Shaders::inst().vsmMarkSky);
uboVsmPages.set(1, *scene.zbuffer, Sampler::nearest());
uboVsmPages.set(2, scene.uboGlobal[SceneGlobals::V_Main]);
uboVsmPages.set(3, occlusionLut);
uboVsmPages.set(4, *scene.vsmPageTbl);
uboVsmPages.set(5, *scene.vsmPageHiZ);
}

uboFogViewLut3d = device.descriptors(Shaders::inst().fogViewLut3d);
uboFogViewLut3d.set(0, scene.uboGlobal[SceneGlobals::V_Main]);
uboFogViewLut3d.set(1, transLut, smpB);
Expand Down Expand Up @@ -459,17 +450,6 @@ void Sky::prepareSky(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint32_t fra
cmd.draw(Resources::fsqVbo());
}

void Sky::vsmMarkPage(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint32_t frameId) {
if(quality!=VolumetricHQVsm)
return;

UboSky ubo = mkPush();
auto& vsmMarkSky = Shaders::inst().vsmMarkSky;
cmd.setFramebuffer({});
cmd.setUniforms(vsmMarkSky, uboVsmPages, &ubo, sizeof(ubo));
cmd.dispatchThreads(occlusionLut.size());
}

void Sky::prepareFog(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint32_t frameId) {
UboSky ubo = mkPush();

Expand Down
2 changes: 0 additions & 2 deletions game/graphics/sky/sky.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Sky final {
void updateLight(const int64_t now);

void prepareSky (Tempest::Encoder<Tempest::CommandBuffer>& p, uint32_t frameId);
void vsmMarkPage(Tempest::Encoder<Tempest::CommandBuffer>& p, uint32_t frameId);
void drawSky (Tempest::Encoder<Tempest::CommandBuffer>& p, uint32_t frameId);
void drawSunMoon(Tempest::Encoder<Tempest::CommandBuffer>& p, uint32_t frameId);

Expand Down Expand Up @@ -84,7 +83,6 @@ class Sky final {
Tempest::DescriptorSet uboFogViewLut3d;
Tempest::DescriptorSet uboSky, uboFog, uboFog3d;
Tempest::DescriptorSet uboOcclusion, uboShadowRq;
Tempest::DescriptorSet uboVsmPages;
Tempest::DescriptorSet uboIrradiance, uboExp;

Tempest::DescriptorSet uboSkyPathtrace;
Expand Down
4 changes: 0 additions & 4 deletions game/graphics/worldview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ void WorldView::prepareExposure(Tempest::Encoder<Tempest::CommandBuffer>& cmd, u
gSky.prepareExposure(cmd, frameId);
}

void WorldView::vsmMarkSkyPages(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t frameId) {
gSky.vsmMarkPage(cmd, frameId);
}

void WorldView::updateFrustrum(const Frustrum fr[]) {
for(uint8_t i=0; i<SceneGlobals::V_Count; ++i)
sGlobal.frustrum[i] = fr[i];
Expand Down
2 changes: 0 additions & 2 deletions game/graphics/worldview.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class WorldView {
void prepareIrradiance(Tempest::Encoder<Tempest::CommandBuffer> &cmd, uint8_t frameId);
void prepareExposure (Tempest::Encoder<Tempest::CommandBuffer> &cmd, uint8_t frameId);

void vsmMarkSkyPages (Tempest::Encoder<Tempest::CommandBuffer> &cmd, uint8_t frameId);

bool updateLights();
bool updateRtScene();

Expand Down
1 change: 0 additions & 1 deletion shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ 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)
add_shader(fog3d_vsm_mark_pages.comp sky/fog.frag -S comp -DVOLUMETRIC -DVIRTUAL_SHADOW_MARK)
add_shader(fog3d_vsm.comp sky/fog.frag -S comp -DVOLUMETRIC -DVIRTUAL_SHADOW)
add_shader(fog3d_hq.frag sky/fog.frag -DVOLUMETRIC)
add_shader(fog.frag sky/fog.frag)
Expand Down
94 changes: 6 additions & 88 deletions shader/sky/fog.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_control_flow_attributes : enable

#if defined(VIRTUAL_SHADOW) || defined(VIRTUAL_SHADOW_MARK)
#if defined(VIRTUAL_SHADOW)
#include "virtual_shadow/vsm_common.glsl"
#endif

Expand Down Expand Up @@ -37,15 +37,10 @@ layout(binding = 3, r32ui) uniform writeonly restrict uimage2D occlusionLut;
layout(binding = 3, r32ui) uniform readonly restrict uimage2D occlusionLut;
#endif

#if defined(VOLUMETRIC) && !defined(VIRTUAL_SHADOW) && !defined(VIRTUAL_SHADOW_MARK) && defined(GL_COMPUTE_SHADER)
#if defined(VOLUMETRIC) && !defined(VIRTUAL_SHADOW) && defined(GL_COMPUTE_SHADER)
layout(binding = 4) uniform sampler2D textureSm1;
#endif

#if defined(VOLUMETRIC) && defined(VIRTUAL_SHADOW_MARK) && defined(GL_COMPUTE_SHADER)
layout(binding = 4, r32ui) uniform uimage3D pageTbl;
layout(binding = 5, r32ui) uniform uimage3D pageTblDepth;
#endif

#if defined(VOLUMETRIC) && defined(VIRTUAL_SHADOW) && defined(GL_COMPUTE_SHADER)
layout(binding = 4) uniform utexture3D pageTbl;
layout(binding = 5) uniform texture2D pageData;
Expand All @@ -59,60 +54,6 @@ const float dFogMax = 0.9999;
uvec2 invocationID = gl_GlobalInvocationID.xy;
#endif

#if defined (VIRTUAL_SHADOW_MARK)
shared uint pageHiZ[NumThreads];
//uint pageHiZTh = 0xFFFFFFFF;

void storeHiZValue(uint v) {
uvec4 dx = unpack565_16(v);
ivec3 at = ivec3(dx.xyz);
uint iz = floatBitsToUint(dx.w/float(0xFFFF));
imageAtomicExchange(pageTbl, at, 1u);
imageAtomicMin(pageTblDepth, at, iz);
}

void setupHiZ() {
const uint lane = gl_LocalInvocationIndex;
pageHiZ[lane] = 0xFFFFFFFF;
}

void markPage(ivec3 at, float z) {
if(z<0 || z>=1)
return;

uint iz = uint(z*0xFFFF);
uint cur = pack565_16(at,iz);
uint id = pageIdHash7(at) % pageHiZ.length();

/*
if((pageHiZTh==0xFFFFFFFF) || (pageHiZTh&0xFFFF0000)==(cur&0xFFFF0000)) {
// thread local cache
pageHiZTh = min(pageHiZTh, cur);
return;
}
*/

uint v = atomicMin(pageHiZ[id], cur);
if(v==0xFFFFFFFF)
return; // clean insert
if((v&0xFFFF0000)==(cur&0xFFFF0000))
return; // update same entry

// imageAtomicAdd(pageTbl, ivec3(0), 1u); //counter
storeHiZValue(v);
}

void flushHiZ() {
//if(pageHiZTh!=0xFFFFFFFF)
// storeHiZValue(pageHiZTh);
const uint lane = gl_LocalInvocationIndex;
const uint v = pageHiZ[lane];
if(v==0xFFFFFFFF)
return;
storeHiZValue(v);
}
#endif

float interleavedGradientNoise() {
#if defined(GL_COMPUTE_SHADER)
return interleavedGradientNoise(invocationID.xy);
Expand All @@ -121,25 +62,10 @@ float interleavedGradientNoise() {
#endif
}

#if defined(VOLUMETRIC) && defined(VIRTUAL_SHADOW_MARK) && defined(GL_COMPUTE_SHADER)
bool shadowFactor(vec4 shPos) {
vec3 shPos0 = shPos.xyz/shPos.w;

int mip = vsmCalcMipIndex(shPos0.xy, VSM_FOG_MIP);
vec2 page = shPos0.xy / (1 << mip);
if(any(greaterThan(abs(page), vec2(1))))
return true;

ivec2 pageI = ivec2((page*0.5+0.5)*VSM_PAGE_TBL_SIZE);
ivec3 at = ivec3(pageI, mip);
markPage(at, shPos0.z);
return true;
}
#elif defined(VOLUMETRIC) && defined(VIRTUAL_SHADOW) && defined(GL_COMPUTE_SHADER)
#if defined(VOLUMETRIC) && defined(VIRTUAL_SHADOW) && defined(GL_COMPUTE_SHADER)
bool shadowFactor(vec4 shPos) {
vec3 shPos0 = shPos.xyz/shPos.w;

int mip = vsmCalcMipIndex(shPos0.xy, VSM_FOG_MIP);
int mip = vsmCalcMipIndexFog(shPos0.xy);
vec2 page = shPos0.xy / (1 << mip);
if(any(greaterThan(abs(page), vec2(1))))
return true;
Expand Down Expand Up @@ -183,7 +109,7 @@ vec4 fog(vec2 uv, float z) {
const vec3 pos1 = project(scene.viewProjectLwcInv, vec3(inPos,dFogMax));
const vec3 posz = project(scene.viewProjectLwcInv, vec3(inPos,z));

#if defined(VIRTUAL_SHADOW) || defined(VIRTUAL_SHADOW_MARK)
#if defined(VIRTUAL_SHADOW)
const vec4 shPos0 = scene.viewVirtualShadowLwc*vec4(pos0, 1);
const vec4 shPos1 = scene.viewVirtualShadowLwc*vec4(posz, 1);
#else
Expand Down Expand Up @@ -312,15 +238,7 @@ void main_comp() {
#endif

void main() {
#if defined(VIRTUAL_SHADOW_MARK) && defined(GL_COMPUTE_SHADER)
setupHiZ();
barrier();

main_comp();
barrier();

flushHiZ();
#elif defined(GL_COMPUTE_SHADER)
#if defined(GL_COMPUTE_SHADER)
main_comp();
#else
main_frag();
Expand Down
22 changes: 22 additions & 0 deletions shader/virtual_shadow/vsm_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ int vsmCalcMipIndex(in vec2 shPos, int minMip) {
return max(vsmCalcMipIndex(shPos), minMip);
}

int vsmCalcMipIndexFog(in vec2 shPos) {
float d = max(abs(shPos.x), abs(shPos.y));
uint id = uint(d * 16.0);
return clamp(findMSB(id)+1, VSM_FOG_MIP, VSM_FOG_MIP+4);
}

uint pageIdHash7(ivec3 src) {
uint x = (src.x & 0x3) << 0;
uint y = (src.y & 0x3) << 2;
Expand All @@ -97,6 +103,20 @@ float vsmTexelFetch(in texture2D pageData, const ivec2 pixel) {
return texelFetch(pageData, pixel, 0).x;
}

uint shadowPageIdFetch(in vec2 page, in int mip, in utexture3D pageTbl) {
//page-local
const ivec2 pageI = ivec2((page*0.5+0.5)*VSM_PAGE_TBL_SIZE);
const vec2 pageF = fract((page*0.5+0.5)*VSM_PAGE_TBL_SIZE);
const ivec2 at = ivec2(pageF*VSM_PAGE_SIZE);

//page-global
const uint pageD = texelFetch(pageTbl, ivec3(pageI, mip), 0).x;
if(pageD==0)
return -1;

return pageD >> 16u;
}

float shadowTexelFetch(in vec2 page, in int mip, in utexture3D pageTbl,
#if defined(VSM_ATOMIC)
in utexture2D pageData
Expand All @@ -115,6 +135,8 @@ float shadowTexelFetch(in vec2 page, in int mip, in utexture3D pageTbl,
return -1;

const uint pageId = pageD >> 16u;

// const uint pageId = shadowPageIdFetch(page, mip, pageTbl);
const ivec2 pageImageAt = unpackVsmPageId(pageId)*VSM_PAGE_SIZE + at;
return vsmTexelFetch(pageData, pageImageAt);
}
Expand Down
9 changes: 8 additions & 1 deletion shader/virtual_shadow/vsm_direct_light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ layout(binding = 6) uniform utexture2D pageData;
#else
layout(binding = 6) uniform texture2D pageData;
#endif
layout(binding = 8, r32ui) uniform readonly uimage2D dbg;


layout(location = 0) out vec4 outColor;
Expand Down Expand Up @@ -109,7 +110,7 @@ int shadowLod(vec2 dx, vec2 dy) {

const float bias = vsmMipBias;
//return max(0, int((minLod + maxLod)*0.5 + bias + 0.5));
return max(0, int(minLod + bias + 0.5));
return max(0, int(minLod + bias));
}

float shadowTexelFetch(vec2 page, int mip) {
Expand Down Expand Up @@ -183,6 +184,12 @@ void main() {

outColor = vec4(luminance * scene.exposure, 1);

#if 0
const uint pageId = shadowPageIdFetch(page.xy,mip,pageTbl);
const uint v = imageLoad(dbg, unpackVsmPageId(pageId)).x;
outColor.rgb = vec3(v/512.0);
#endif

#if defined(DEBUG)
const ivec2 pageI = ivec2((page.xy*0.5+0.5)*VSM_PAGE_TBL_SIZE);
// int mip = 0;
Expand Down
Loading

0 comments on commit cf461b4

Please sign in to comment.