Skip to content

Commit

Permalink
vsm: use clip-distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Sep 10, 2024
1 parent a49258e commit 00ae46b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
5 changes: 3 additions & 2 deletions shader/materials/main.frag
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,9 @@ void main() {
#endif

#if defined(VIRTUAL_SHADOW)
if(!vsmPageClip(ivec2(gl_FragCoord.xy), vsmPageId))
discard;
// NOTE: use clip-distance is used instead
// if(!vsmPageClip(ivec2(gl_FragCoord.xy), vsmPageId))
// discard;
#endif

#if defined(MAT_COLOR)
Expand Down
38 changes: 28 additions & 10 deletions shader/materials/main.vert
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ layout(push_constant, std430) uniform Push {

#if defined(GL_VERTEX_SHADER)
out gl_PerVertex {
vec4 gl_Position;
vec4 gl_Position;
#if defined(VIRTUAL_SHADOW)
float gl_ClipDistance[4];
#endif
};
#else
out gl_MeshPerVertexEXT {
vec4 gl_Position;
vec4 gl_Position;
#if defined(VIRTUAL_SHADOW)
float gl_ClipDistance[4];
#endif
} gl_MeshVerticesEXT[];
#endif

Expand All @@ -57,21 +63,26 @@ uint shadowPageId = 0;
#endif

#if defined(VIRTUAL_SHADOW)
vec4 mapViewport(vec4 pos) {
vec4 mapViewport(vec4 pos, out float clipDistance[4]) {
const ivec3 page = unpackVsmPageInfo(vsm.pageList[shadowPageId]);
pos.xy /= float(1u << page.z);

pos.xy = (pos.xy*0.5+0.5); // [0..1]
pos.xy = (pos.xy*VSM_PAGE_TBL_SIZE - page.xy);

{
clipDistance[0] = 0+pos.x;
clipDistance[1] = 1-pos.x;
clipDistance[2] = 0+pos.y;
clipDistance[3] = 1-pos.y;
}

const vec2 pageId = vec2(unpackVsmPageId(shadowPageId));
pos.xy = (pos.xy + pageId)/VSM_PAGE_PER_ROW;

pos.xy = pos.xy*2.0-1.0; // [-1..1]
return pos;
}
#else
vec4 mapViewport(vec4 pos) { return pos; }
#endif

#if defined(VIRTUAL_SHADOW)
Expand Down Expand Up @@ -134,8 +145,7 @@ vec4 processVertex(out Varyings var, uint instanceOffset, const uint meshletId,
#else
const Vertex vert = pullVertex(bucketId, vboOffset);
#endif
const vec4 ret = processVertex(var, vert, bucketId, instanceOffset, vboOffset);
return mapViewport(ret);
return processVertex(var, vert, bucketId, instanceOffset, vboOffset);
}

#if defined(GL_VERTEX_SHADER)
Expand All @@ -159,8 +169,12 @@ void vertexShader(const uvec4 task) {
#endif

Varyings var;
uint idx = processPrimitive(meshletId, bucketId, laneID)[gl_VertexIndex%3];
gl_Position = processVertex(var, instanceId, meshletId, bucketId, idx);
uint idx = processPrimitive(meshletId, bucketId, laneID)[gl_VertexIndex%3];
vec4 pos = processVertex(var, instanceId, meshletId, bucketId, idx);
#if defined(VIRTUAL_SHADOW)
pos = mapViewport(pos, gl_ClipDistance);
#endif
gl_Position = pos;
#if defined(MAT_VARYINGS)
shOut = var;
#endif
Expand Down Expand Up @@ -188,7 +202,11 @@ void meshShader(const uvec4 task) {
if(laneID<primCount)
gl_PrimitiveTriangleIndicesEXT[laneID] = processPrimitive(meshletId, bucketId, laneID);
if(laneID<vertCount) {
gl_MeshVerticesEXT[laneID].gl_Position = processVertex(var, instanceId, meshletId, bucketId, laneID);
vec4 pos = processVertex(var, instanceId, meshletId, bucketId, laneID);
#if defined(VIRTUAL_SHADOW)
pos = mapViewport(pos, gl_MeshVerticesEXT[laneID].gl_ClipDistance);
#endif
gl_MeshVerticesEXT[laneID].gl_Position = pos;

/*
Workaround the AMD driver issue where assigning to a vec3 output results in a driver crash.
Expand Down
4 changes: 2 additions & 2 deletions shader/virtual_shadow/vsm_cluster_task.comp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ void runCluster(const uint clusterId) {
const uint maxPages = vsm.header.x;

for(uint i=gl_LocalInvocationIndex; i<maxPages; i+=gl_WorkGroupSize.y) {
if(i!=13)
continue;
// if(i!=13)
// continue;
const ivec3 page = unpackVsmPageInfo(vsm.pageList[i]);
if(!pageBoundsTest(aabb, page))
continue;
Expand Down

0 comments on commit 00ae46b

Please sign in to comment.