Skip to content

Commit

Permalink
vsm code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Oct 8, 2024
1 parent 94e4ed0 commit 8ceb66d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 37 deletions.
1 change: 1 addition & 0 deletions game/graphics/drawcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ void DrawCommands::updateTasksUniforms() {
i.desc.set(T_Payload, views[i.viewport].vsmClusters);
i.desc.set(T_HiZ, *scene.vsmPageHiZ);
i.desc.set(T_VsmPages, *scene.vsmPageList);
i.desc.set(8, scene.vsmDbg);
// i.desc.set(T_PkgOffsets, views[i.viewport].pkgOffsets);
}
}
Expand Down
7 changes: 3 additions & 4 deletions game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ void Renderer::resetSwapchain() {
vsm.pagesDbgPso = &Shaders::inst().vsmDbg;
vsm.uboLight = device.descriptors(*vsm.directLightPso);

vsm.pageDbg = device.image2d(TextureFormat::R32U, 32, 32);
vsm.pageTbl = device.image3d(TextureFormat::R32U, 32, 32, 16);
vsm.pageHiZ = device.image3d(TextureFormat::R32U, 32, 32, 16);
vsm.pageData = device.zbuffer(shadowFormat, 4096, 4096);
Expand Down Expand Up @@ -472,7 +471,7 @@ void Renderer::prepareUniforms() {

vsm.uboAlloc.set(0, vsm.pageList);
vsm.uboAlloc.set(1, vsm.pageTbl);
vsm.uboAlloc.set(2, vsm.pageDbg);
vsm.uboAlloc.set(2, wview->sceneGlobals().vsmDbg);

vsm.uboLight.set(0, wview->sceneGlobals().uboGlobal[SceneGlobals::V_Main]);
vsm.uboLight.set(1, gbufDiffuse, Sampler::nearest());
Expand Down Expand Up @@ -584,8 +583,8 @@ void Renderer::dbgDraw(Tempest::Painter& p) {
//tex.push_back(&textureCast(hiz.smProj));
//tex.push_back(&textureCast(hiz.hiZSm1));
//tex.push_back(&textureCast(shadowMap[1]));
tex.push_back(&textureCast<const Texture2d&>(shadowMap[0]));
//tex.push_back(&textureCast<const Texture2d&>(vsm.pageDataZ));
//tex.push_back(&textureCast<const Texture2d&>(shadowMap[0]));
tex.push_back(&textureCast<const Texture2d&>(vsm.pageData));

static int size = 400;
int left = 10;
Expand Down
1 change: 0 additions & 1 deletion game/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ class Renderer final {
Tempest::RenderPipeline* pagesDbgPso = nullptr;
Tempest::DescriptorSet uboLight;

Tempest::StorageImage pageDbg;
Tempest::StorageImage pageTbl;
Tempest::StorageImage pageHiZ;
Tempest::StorageImage pageDataCs;
Expand Down
1 change: 1 addition & 0 deletions game/graphics/sceneglobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SceneGlobals::SceneGlobals() {
vsmPageData = &Resources::fallbackBlack();
vsmPageTbl = &Resources::fallbackImage3d();
vsmPageHiZ = &Resources::fallbackImage3d();
vsmDbg = device.image2d(Tempest::TextureFormat::R32U, 32, 32);

for(uint8_t lay=0; lay<V_Count; ++lay) {
uboGlobal[lay] = device.ssbo(nullptr,sizeof(UboGlobal));
Expand Down
1 change: 1 addition & 0 deletions game/graphics/sceneglobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class SceneGlobals final {
const Tempest::StorageImage* vsmPageTbl = nullptr;
const Tempest::StorageImage* vsmPageHiZ = nullptr;
const Tempest::StorageBuffer* vsmPageList = nullptr;
Tempest::StorageImage vsmDbg;

const Tempest::StorageImage* swMainImage = nullptr;

Expand Down
10 changes: 6 additions & 4 deletions shader/virtual_shadow/vsm_cluster_task.comp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ layout(local_size_x = 1024) in;

const uint NumThreads = gl_WorkGroupSize.x*gl_WorkGroupSize.y*gl_WorkGroupSize.z;

layout(push_constant, std430) uniform UboPush {
int meshletCount;
} push;
layout(binding = 0, std140) uniform UboScene {
SceneDesc scene;
};
Expand All @@ -39,9 +42,7 @@ layout(binding = 5, std430) readonly buffer Cbo { Cluster clusters[]; };
layout(binding = 6, r32ui) uniform uimage3D pageTblDepth;
layout(binding = 7, std430) buffer Pages { VsmHeader header; uint pageList[]; } vsm;

layout(push_constant, std430) uniform UboPush {
int meshletCount;
} push;
layout(binding = 8, r32ui) uniform uimage2D dbg;

shared uint pageListSize;
shared uint pageList[VSM_MAX_PAGES];
Expand Down Expand Up @@ -253,7 +254,7 @@ bool pageHiZTest(vec4 aabb, float depthMax, const ivec3 at, const ivec2 sz) {

// if(p!=0xFFFFFFFF)
// return true;
if(f<depthMax)
if(f<=depthMax)
return true;
}

Expand Down Expand Up @@ -291,6 +292,7 @@ void runCluster(const uint clusterId) {
continue;
if(!emitCluster(cluster, i))
break;
imageAtomicAdd(dbg, unpackVsmPageId(i), 1);
}
}

Expand Down
36 changes: 17 additions & 19 deletions shader/virtual_shadow/vsm_direct_light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ float shadowTexelFetch(vec2 page, int mip) {
return shadowTexelFetch(page, mip, pageTbl, pageData);
}

float shadowTest(vec2 page, int mip, in float refZ, bool isATest, float LdotN) {
float shadowTest(vec2 page, int mip, in float refZ) {
// const float bias = (isATest ? 8 : -1)/(65535.0);
// const float bias = -(16*LdotN)/(65535.0);
// const float bias = 2.0/(65535.0); // self-occlusion on trees
// refZ += bias;
const float z = shadowTexelFetch(page, mip);
return z<refZ ? 1 : 0;
return (z==0 || z<refZ) ? 1 : 0;
}

float lambert(vec3 normal) {
Expand All @@ -143,6 +143,12 @@ bool calcMipIndex(out vec3 pagePos, out int mip, const float z, const vec3 norma
return mip<VSM_PAGE_MIPS;
}

float shadowTest(float z, vec3 normal, out vec3 page, out int mip) {
if(!calcMipIndex(page, mip, z, normal))
return 1;
return shadowTest(page.xy, mip, page.z);
}

void main() {
outColor = vec4(0,0,0, 1);

Expand All @@ -160,39 +166,31 @@ void main() {

const vec4 diff = texelFetch (gbufDiffuse, fragCoord, 0);
const vec3 normal = normalFetch(gbufNormal, fragCoord);
if(dot(scene.sunDir,normal)<=0)
return;

vec3 page = vec3(0);
int mip = 0;
if(!calcMipIndex(page, mip, z, normal))
return;

bool isFlat = false;
bool isATest = false;
bool isWater = false;
decodeBits(diff.a, isFlat, isATest, isWater);

const float LdotN = dot(scene.sunDir,normal);
const float light = (isFlat ? 0 : lambert(normal));
float shadow = 1;
if(light>0) {
shadow = shadowTest(page.xy, mip, page.z, isATest, LdotN);
}
vec3 page = vec3(0);
int mip = 0;
const float light = (isFlat ? 0 : lambert(normal));
const float shadow = (light<=0 ? 0 : shadowTest(z, normal, page, mip));

const vec3 illuminance = scene.sunColor * light * shadow;
const vec3 linear = textureAlbedo(diff.rgb);
const vec3 luminance = linear * Fd_Lambert * illuminance;
const vec3 illuminance = scene.sunColor * light * shadow;
const vec3 linear = textureAlbedo(diff.rgb);
const vec3 luminance = linear * Fd_Lambert * illuminance;

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

#if defined(DEBUG)
const ivec2 pageI = ivec2((page.xy*0.5+0.5)*VSM_PAGE_TBL_SIZE);
// int mip = 0;
// vec3 color = directLight(page, mip, shPos0.z);
vec3 color = debugColors[hash(uvec3(pageI,mip)) % debugColors.length()];
// vec3 color = debugColors[mip % debugColors.length()];
// color *= (1.0 - shadowTexelFetch(page, mip));
color *= (shadowTest(page.xy, mip, page.z, false, LdotN)*0.9+0.1);
color *= (shadowTest(page.xy, mip, page.z)*0.9+0.1);
// vec3 color = vec3(shPos0, 0);
// vec3 color = vec3(page, 0);
// vec3 color = vec3(fract(page*VSM_PAGE_TBL_SIZE), 0);
Expand Down
30 changes: 21 additions & 9 deletions shader/virtual_shadow/vsm_mark_pages.comp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ layout(binding = 3) uniform texture2D depth;
layout(binding = 4, r32ui) uniform uimage3D pageTbl;
layout(binding = 5, r32ui) uniform uimage3D pageTblDepth;

ivec2 pixelPos = ivec2(gl_GlobalInvocationID.xy);
ivec2 fragCoord = ivec2(gl_GlobalInvocationID.xy);

void storeHiZValue(uint v) {
uvec4 dx = unpack565_16(v);
ivec3 at = ivec3(dx.xyz);
uint iz = floatBitsToUint(dx.w/float(0xFFFF));
//uint iz = floatBitsToUint(dx.w/float(0xFFFF));
uint iz = (dx.w << 16);

//NOTE: require vulkan memory-model extension or vulkan 1.2
//imageAtomicStore(pageTbl, at, 1u, gl_ScopeDevice, gl_StorageSemanticsNone, gl_SemanticsRelaxed);
Expand Down Expand Up @@ -89,9 +90,8 @@ void markPage(ivec3 at, float z) {
if(z<0 || z>=1)
return;

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

uint v = atomicMin(pageHiZ[id], cur);
Expand Down Expand Up @@ -124,7 +124,7 @@ vec4 worldPos(ivec2 frag, float depth) {
}

vec3 shadowPos(float z, vec3 normal, ivec2 offset) {
const vec4 wpos = worldPos(ivec2(pixelPos.xy) + offset, z) + vec4(normal*0.002, 0);
const vec4 wpos = worldPos(ivec2(fragCoord.xy) + offset, z) + vec4(normal*0.002, 0);
#if defined(LWC)
vec4 shPos = scene.viewVirtualShadowLwc * wpos;
#else
Expand All @@ -134,6 +134,10 @@ vec3 shadowPos(float z, vec3 normal, ivec2 offset) {
return shPos.xyz;
}

float lambert(vec3 normal) {
return max(0.0, dot(scene.sunDir,normal));
}

int shadowLod(vec2 dx, vec2 dy) {
float px = dot(dx, dx);
float py = dot(dy, dy);
Expand Down Expand Up @@ -161,15 +165,23 @@ bool calcMipIndex(out vec3 pagePos, out int mip, const float z, const vec3 norma

void realMain() {
const ivec2 size = textureSize(depth, 0);
if(any(greaterThanEqual(pixelPos, size)))
if(any(greaterThanEqual(fragCoord, size)))
return;

const float z = texelFetch(depth, pixelPos, 0).x;
const float z = texelFetch(depth, fragCoord, 0).x;
if(z==1)
return;

const vec3 normal = normalFetch(gbufNormal, pixelPos);
if(dot(scene.sunDir,normal)<=0)
const vec4 diff = texelFetch (gbufDiffuse, fragCoord, 0);
const vec3 normal = normalFetch(gbufNormal, fragCoord);

bool isFlat = false;
bool isATest = false;
bool isWater = false;
decodeBits(diff.a, isFlat, isATest, isWater);

const float light = (isFlat ? 0 : lambert(normal));
if(light<=0)
return;

vec3 page = vec3(0);
Expand Down

0 comments on commit 8ceb66d

Please sign in to comment.