Skip to content

Commit

Permalink
vsm: drawing now works
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Sep 5, 2024
1 parent 602f77e commit 20cb0f2
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 56 deletions.
1 change: 1 addition & 0 deletions game/graphics/drawcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ void DrawCommands::updateCommandUniforms() {

if(v==SceneGlobals::V_Vsm) {
desc[v].set(L_CmdOffsets, views[v].pkgOffsets);
desc[v].set(L_VsmPages, *scene.vsmPageList);
}
}

Expand Down
1 change: 1 addition & 0 deletions game/graphics/drawcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class DrawCommands {
L_SceneClr = 12,
L_GDepth = 13,
L_CmdOffsets = 14,
L_VsmPages = 15,
};

struct IndirectCmd {
Expand Down
1 change: 0 additions & 1 deletion game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ Shaders::Shaders() {
vsmPackDraw1 = computeShader("vsm_pack_draws1.comp.sprv");
vsmDbg = postEffect("copy", "vsm_dbg", RenderState::ZTestMode::Always);
// vsmRendering = computeShader("vsm_rendering.comp.sprv");
// vsmComposePso = computeShader("vsm_compose.comp.sprv");
}

if(Gothic::options().swRenderingPreset>0) {
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 vsmClear, vsmMarkPages, vsmListPages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
// Tempest::ComputePipeline vsmComposePso, vsmRendering;
// Tempest::ComputePipeline vsmRendering;
Tempest::RenderPipeline vsmDbg;

// Software rendering
Expand Down
5 changes: 2 additions & 3 deletions shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ add_geom_shader(main_lnd "-DMESH_TYPE=0" -DFLAT_NORMAL)
add_geom_shader(main_lnd_f "-DMESH_TYPE=0" -DFLAT_NORMAL -DFORWARD)
add_geom_shader(main_lnd_d "-DMESH_TYPE=0" -DFLAT_NORMAL -DDEPTH_ONLY)
add_geom_shader(main_lnd_d_at "-DMESH_TYPE=0" -DFLAT_NORMAL -DDEPTH_ONLY -DATEST)
add_geom_shader(main_lnd_v "-DMESH_TYPE=0" -DFLAT_NORMAL -DEPTH_ONLY -DVIRTUAL_SHADOW)
add_geom_shader(main_lnd_v_at "-DMESH_TYPE=0" -DFLAT_NORMAL -DEPTH_ONLY -DVIRTUAL_SHADOW -DATEST)
add_geom_shader(main_lnd_v "-DMESH_TYPE=0" -DFLAT_NORMAL -DDEPTH_ONLY -DVIRTUAL_SHADOW)
add_geom_shader(main_lnd_v_at "-DMESH_TYPE=0" -DFLAT_NORMAL -DDEPTH_ONLY -DVIRTUAL_SHADOW -DATEST)

add_geom_shader(main_obj "-DMESH_TYPE=1")
add_geom_shader(main_obj_f "-DMESH_TYPE=1" -DFORWARD)
Expand Down Expand Up @@ -301,7 +301,6 @@ add_shader(cmaa2_deferred_color_apply_2x2.frag antialiasing/cmaa2/deferred_c
# virtual shadows
add_shader(vsm_clear.comp virtual_shadow/vsm_clear.comp)
add_shader(vsm_mark_pages.comp virtual_shadow/vsm_mark_pages.comp -DMARK_PAGES)
add_shader(vsm_compose.comp virtual_shadow/vsm_mark_pages.comp -DCOMPOSE)
add_shader(vsm_dbg.frag virtual_shadow/vsm_mark_pages.comp -DDEBUG -S frag)
add_shader(vsm_list_pages.comp virtual_shadow/vsm_list_pages.comp)
add_shader(vsm_pack_draws0.comp virtual_shadow/vsm_pack_draws.comp -DPASS0)
Expand Down
4 changes: 2 additions & 2 deletions shader/materials/cluster_task.comp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ void runCluster(const uint clusterId) {
const uint meshletId = cluster.firstMeshlet;

#if 1 && defined(VIRTUAL_SHADOW)
for(uint r=0; r<1 && r<vsm.header.x; ++r) {
const uint page = vsm.pageList[r];
for(uint r=0; r<vsm.header.x; ++r) {
const uint page = r; //vsm.pageList[r];
const uint at = atomicAdd(vsm.header.w, cluster.meshletCount);
for(int i=0; i<cluster.meshletCount; ++i)
payload[at+i] = uvec4(instanceId, meshletId+i, cluster.bucketId_commandId, page);
Expand Down
19 changes: 19 additions & 0 deletions shader/materials/main.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable

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

#include "materials_common.glsl"
#include "water/gerstner_wave.glsl"
#include "lighting/shadow_sampling.glsl"
Expand All @@ -13,6 +17,10 @@ layout(location = 0) in flat uint bucketId;
layout(location = 1) in Varyings shInp;
#endif

#if defined(VIRTUAL_SHADOW)
layout(location = 3) in flat uint vsmPageId;
#endif

#if DEBUG_DRAW
layout(location = DEBUG_DRAW_LOC) in flat uint debugId;
#endif
Expand All @@ -28,6 +36,12 @@ layout(location = 2) out uint outNormal;
layout(location = 0) out vec4 outColor;
#endif

#if defined(VIRTUAL_SHADOW)
layout(push_constant, std430) uniform Push {
uint commandId;
} push;
#endif

#if defined(WATER) || defined(GHOST)
float unproject(float depth) {
mat4 projInv = scene.projectInv;
Expand Down Expand Up @@ -306,6 +320,11 @@ void main() {
# endif
#endif

#if defined(VIRTUAL_SHADOW)
if(!vsmPageClip(ivec2(gl_FragCoord.xy), vsmPageId))
discard;
#endif

#if defined(MAT_COLOR)
t *= shInp.color;
#endif
Expand Down
50 changes: 49 additions & 1 deletion shader/materials/main.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_control_flow_attributes : enable

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

#include "materials_common.glsl"
#include "vertex_process.glsl"

Expand Down Expand Up @@ -42,6 +46,45 @@ layout(location = 0) out flat uint bucketIdOut[]; //TODO: per-primitive
layout(location = 1) out Varyings shOut[];
#endif

#if defined(GL_VERTEX_SHADER) && defined(VIRTUAL_SHADOW)
layout(location = 3) out flat uint vsmPageIdOut;
#elif defined(VIRTUAL_SHADOW)
layout(location = 3) out flat uint vsmPageIdOut[];
#endif

#if defined(VIRTUAL_SHADOW)
uint shadowPageId = 0;
#endif

#if defined(VIRTUAL_SHADOW)
vec4 mapViewport(vec4 pos) {
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);

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

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

#if defined(VIRTUAL_SHADOW)
void initVsm(uvec4 task) {
shadowPageId = task.w;
#if defined(GL_VERTEX_SHADER)
vsmPageIdOut = task.w;
#else
vsmPageIdOut[gl_LocalInvocationIndex] = task.w;
#endif
}
#endif

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

#if defined(GL_VERTEX_SHADER)
Expand Down Expand Up @@ -188,6 +232,10 @@ void main() {

const uvec4 task = payload[workIndex + firstMeshlet];

#if defined(VIRTUAL_SHADOW)
initVsm(task);
#endif

#if defined(GL_VERTEX_SHADER)
vertexShader(task);
#else
Expand Down
10 changes: 8 additions & 2 deletions shader/materials/materials_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const uint L_Morph = 11;
const uint L_SceneClr = 12;
const uint L_GDepth = 13;
const uint L_CmdOffsets = 14;
const uint L_VsmPages = 15;

#ifndef MESH_TYPE
#define MESH_TYPE 255
Expand All @@ -80,7 +81,7 @@ const uint L_CmdOffsets = 14;
#define MAT_NORMAL 1
#endif

#if defined(FLAT_NORMAL) || defined(FORWARD) || defined(WATER)
#if (defined(FLAT_NORMAL) && !defined(DEPTH_ONLY)) || defined(FORWARD) || defined(WATER)
#define MAT_POSITION 1
#endif

Expand All @@ -92,6 +93,10 @@ const uint L_CmdOffsets = 14;
#define MAT_VARYINGS 1
#endif

#if defined(DEPTH_ONLY) && defined(MAT_NORMAL)
#error "normals are not intended to be in use by depth-only pass"
#endif

struct Varyings {
#if defined(MAT_UV)
vec2 uv;
Expand Down Expand Up @@ -195,8 +200,9 @@ layout(binding = L_SceneClr) uniform sampler2D sceneColor;
layout(binding = L_GDepth ) uniform sampler2D gbufferDepth;
#endif

#if defined(VIRTUAL_SHADOW)
#if defined(VIRTUAL_SHADOW) && !defined(CLUSTER)
layout(binding = L_CmdOffsets, std430) readonly buffer Offsets { uint cmdOffsets[]; };
layout(binding = L_VsmPages, std430) readonly buffer Pages { uvec4 header; uint pageList[]; } vsm;
#endif

#if !defined(CLUSTER) && (MESH_TYPE!=T_PFX)
Expand Down
40 changes: 40 additions & 0 deletions shader/virtual_shadow/vsm_common.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef VSM_COMMON_GLSL
#define VSM_COMMON_GLSL

// const int VSM_MAX_PAGES = 1024;
const int VSM_PAGE_SIZE = 128;
const int VSM_PAGE_TBL_SIZE = 64;
const int VSM_CLIPMAP_SIZE = VSM_PAGE_SIZE * VSM_PAGE_TBL_SIZE;

uint packVsmPageInfo(ivec3 at) {
return (at.x & 0xFF) | ((at.y & 0xFF) << 8) | ((at.z & 0xFF) << 16);
}

ivec3 unpackVsmPageInfo(uint p) {
ivec3 r;
r.x = int(p ) & 0xFF;
r.y = int(p >> 8) & 0xFF;
r.z = int(p >> 16) & 0xFF;
return r;
}

uint packVsmPageId(ivec2 at) {
return (at.x + at.y*32);
}

ivec2 unpackVsmPageId(uint pageId) {
return ivec2(pageId%32, pageId/32);
}

bool vsmPageClip(ivec2 fragCoord, const uint page) {
ivec4 pg = ivec4(unpackVsmPageId(page) * VSM_PAGE_SIZE, 0, 0);
pg.zw = pg.xy + ivec2(VSM_PAGE_SIZE);

ivec2 f = fragCoord;
if(pg.x <= f.x && f.x<pg.z &&
pg.y <= f.y && f.y<pg.w)
return true;
return false;
}

#endif
19 changes: 3 additions & 16 deletions shader/virtual_shadow/vsm_list_pages.comp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#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"

Expand All @@ -20,7 +21,7 @@ void main() {
const ivec3 at = ivec3(gl_GlobalInvocationID);
if(any(greaterThanEqual(at, size)))
return;
#if 1

const uint alloc = imageLoad(pageTbl, at).r;
if(alloc==0)
return;
Expand All @@ -31,20 +32,6 @@ void main() {
return;
}

pageList[pageId] = packUint4x8(uvec4(at.xyz,0));
imageStore(pageTbl, at, uvec4(pageId,0,0,0));
#else
// debug
if(gl_GlobalInvocationID.z!=1) {
imageStore(pageTbl, at, uvec4(0xFFFFFFFF));
return;
}

const uint page = packUint4x8(uvec4(at.xyz,0));
//const uint pageId = atomicAdd(header.x, 1);
const uint pageId = (at.x + at.y*32);

pageList[pageId] = page;
pageList[pageId] = packVsmPageInfo(at.xyz);
imageStore(pageTbl, at, uvec4(pageId,0,0,0));
#endif
}
Loading

0 comments on commit 20cb0f2

Please sign in to comment.