Skip to content

Commit

Permalink
vsm: page trimming; fog
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Oct 2, 2024
1 parent 7146a51 commit 444596b
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 68 deletions.
6 changes: 3 additions & 3 deletions game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,9 @@ void Renderer::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fI
cmd.dispatchThreads(zbuffer.size());

if(vsm.pageDataCs.isEmpty()) {
//TODO: trimming
//cmd.setUniforms(shaders.vsmClumpPages0, vsm.uboClump);
//cmd.dispatch(1);
// trimming
cmd.setUniforms(shaders.vsmTrimPages, vsm.uboClump);
cmd.dispatch(1);

// clump
cmd.setUniforms(shaders.vsmClumpPages, vsm.uboClump);
Expand Down
1 change: 1 addition & 0 deletions game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ Shaders::Shaders() {
vsmClear = computeShader("vsm_clear.comp.sprv");
vsmClearPages = computeShader("vsm_clear_pages.comp.sprv");
vsmMarkPages = computeShader("vsm_mark_pages.comp.sprv");
vsmTrimPages = computeShader("vsm_trim_pages.comp.sprv");
vsmClumpPages = computeShader("vsm_clump_pages.comp.sprv");
vsmAllocPages = computeShader("vsm_alloc_pages.comp.sprv");
vsmPackDraw0 = computeShader("vsm_pack_draws0.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 @@ -77,7 +77,7 @@ class Shaders {
// Virtual shadow
Tempest::ComputePipeline vsmClusterTask;
Tempest::ComputePipeline vsmClear, vsmClearPages, vsmMarkPages;
Tempest::ComputePipeline vsmClumpPages, vsmAllocPages;
Tempest::ComputePipeline vsmTrimPages, vsmClumpPages, vsmAllocPages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
Tempest::RenderPipeline vsmDirectLight;
Tempest::RenderPipeline vsmDbg;
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Sky::~Sky() {
void Sky::setupSettings() {
auto& device = Resources::device();
const bool fog = Gothic::inst().settingsGetI("RENDERER_D3D","zFogRadial")!=0;
const bool vsm = false;//Gothic::inst().options().doVirtualShadow;
const bool vsm = false; //Gothic::inst().options().doVirtualShadow;

auto q = Quality::VolumetricLQ;
if(!fog) {
Expand Down
1 change: 1 addition & 0 deletions shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ add_shader(vsm_dbg.frag virtual_shadow/vsm_direct_light.frag -DDEBUG)
add_shader(vsm_mark_pages.comp virtual_shadow/vsm_mark_pages.comp)
add_shader(vsm_clear.comp virtual_shadow/vsm_clear.comp)
add_shader(vsm_clear_pages.comp virtual_shadow/vsm_clear_pages.comp)
add_shader(vsm_trim_pages.comp virtual_shadow/vsm_trim_pages.comp)
add_shader(vsm_clump_pages.comp virtual_shadow/vsm_clump_pages.comp)
add_shader(vsm_alloc_pages.comp virtual_shadow/vsm_alloc_pages.comp)
add_shader(vsm_pack_draws0.comp virtual_shadow/vsm_pack_draws.comp -DPASS0)
Expand Down
4 changes: 2 additions & 2 deletions shader/virtual_shadow/vsm_alloc_pages.comp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void horizontalMerge() {
uint sz = unpackVsmPageSize(p0).x;
ivec3 at = unpackVsmPageInfo(p0);
uint size = sz;
for(uint i=col+sz; i < VSM_PAGE_TBL_SIZE; i+=sz) {
for(uint i=col+sz; i<VSM_PAGE_TBL_SIZE; i+=sz) {
const uint pId1 = off+i;
const uint p1 = vsm.pageList[pId1];
const uint sz1 = unpackVsmPageSize(p1).x;
Expand Down Expand Up @@ -243,5 +243,5 @@ void main() {
layoutPages(frag, size, 2);
layoutPages(frag, size, 1);

//horizontalMerge();
horizontalMerge();
}
28 changes: 0 additions & 28 deletions shader/virtual_shadow/vsm_clump_pages.comp
Original file line number Diff line number Diff line change
Expand Up @@ -177,31 +177,3 @@ void main() {

storePageTable();
}

/*
void trimMip(int mip) {
const ivec3 size = imageSize(pageTbl);
const ivec3 id = ivec3(gl_LocalInvocationID);

const ivec2 b = ivec2(VSM_PAGE_TBL_SIZE/4);
const ivec2 h = ivec2(VSM_PAGE_TBL_SIZE/2);

for(int i=id.x; i<h.x; i+=int(gl_WorkGroupSize.x)) {
for(int r=id.y; r<h.y; r+=int(gl_WorkGroupSize.y)) {
ivec3 ax = ivec3(i+b.x, r+b.y, mip);
uint st = imageLoad(pageTbl, ax).r;
if(st==0)
continue;

ivec3 at = ivec3(i*2, r*2, mip-1);
uint a = imageLoad(pageTbl, at+ivec3(0,0,0)).r;
uint b = imageLoad(pageTbl, at+ivec3(1,0,0)).r;
uint c = imageLoad(pageTbl, at+ivec3(0,1,0)).r;
uint d = imageLoad(pageTbl, at+ivec3(1,1,0)).r;
if(a+b+c+d == 4) {
imageStore(pageTbl, ax, uvec4(0));
}
}
}
}
*/
61 changes: 28 additions & 33 deletions shader/virtual_shadow/vsm_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,48 +70,43 @@ float vsmTexelFetch(in texture2D pageData, const ivec2 pixel) {
return texelFetch(pageData, pixel, 0).x;
}

float shadowTexelFetch(in vec2 page, in int mip, in utexture3D pageTbl,
#if defined(VSM_ATOMIC)
in utexture2D pageData
#else
in texture2D pageData
#endif
) {
#if 1
while(mip>=0) {
if(abs(page.x)>=1 || abs(page.y)>=1)
break;
//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 pageId = texelFetch(pageTbl, ivec3(pageI, mip), 0).x >> 16u;
if(pageId==0xFFFF) {
page *= 2.0;
--mip;
continue;
}

const ivec2 pageImageAt = unpackVsmPageId(pageId)*VSM_PAGE_SIZE + at;
return vsmTexelFetch(pageData, pageImageAt);
}
return 0;
#else
float shadowTexelFetchDirect(in vec2 page, in int mip, in utexture3D pageTbl,
#if defined(VSM_ATOMIC)
in utexture2D pageData
#else
in texture2D pageData
#endif
) {
//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 pageId = texelFetch(pageTbl, ivec3(pageI, mip), 0).x >> 16u;
if(pageId==0xFFFF)
return 0;
const uint pageD = texelFetch(pageTbl, ivec3(pageI, mip), 0).x;
if(pageD==0)
return -1;

const uint pageId = pageD >> 16u;
const ivec2 pageImageAt = unpackVsmPageId(pageId)*VSM_PAGE_SIZE + at;
return vsmTexelFetch(pageData, pageImageAt);
#endif
}

float shadowTexelFetch(in vec2 page, in int mip, in utexture3D pageTbl,
#if defined(VSM_ATOMIC)
in utexture2D pageData
#else
in texture2D pageData
#endif
) {
while(mip >= 0) {
float s = shadowTexelFetchDirect(page, mip, pageTbl, pageData);
if(s>=0)
return s;
page *= 2.0;
mip--;
}
return 0;
}

#endif
46 changes: 46 additions & 0 deletions shader/virtual_shadow/vsm_trim_pages.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#version 450

#extension GL_GOOGLE_include_directive : enable
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_samplerless_texture_functions : enable

#include "virtual_shadow/vsm_common.glsl"
#include "scene.glsl"
#include "common.glsl"

layout(local_size_x = 32, local_size_y = 32) in;

layout(binding = 0, std430) buffer Pages { VsmHeader header; uint pageList[]; } vsm;
layout(binding = 1, r32ui) uniform uimage3D pageTbl;

void trimPage(int mip) {
const ivec3 size = imageSize(pageTbl);
const ivec3 id = ivec3(gl_LocalInvocationID);

const ivec2 b = ivec2(VSM_PAGE_TBL_SIZE/4);
const ivec2 h = ivec2(VSM_PAGE_TBL_SIZE/2);

for(int i=id.x; i<h.x; i+=int(gl_WorkGroupSize.x)) {
for(int r=id.y; r<h.y; r+=int(gl_WorkGroupSize.y)) {
ivec3 ax = ivec3(i+b.x, r+b.y, mip);
uint st = imageLoad(pageTbl, ax).r;
if(st==0)
continue;

ivec3 at = ivec3(i*2, r*2, mip-1);
uint a = imageLoad(pageTbl, at+ivec3(0,0,0)).r;
uint b = imageLoad(pageTbl, at+ivec3(1,0,0)).r;
uint c = imageLoad(pageTbl, at+ivec3(0,1,0)).r;
uint d = imageLoad(pageTbl, at+ivec3(1,1,0)).r;
if(a+b+c+d == 4) {
imageStore(pageTbl, ax, uvec4(0));
}
}
}
}

void main() {
const ivec3 size = imageSize(pageTbl);
for(int i=0; i<size.z; ++i)
trimPage(i);
}

0 comments on commit 444596b

Please sign in to comment.