Skip to content

Commit

Permalink
vsm: 2x2 pages support
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Sep 14, 2024
1 parent b00a129 commit 8dcdb1d
Show file tree
Hide file tree
Showing 19 changed files with 295 additions and 114 deletions.
2 changes: 1 addition & 1 deletion game/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Matrix4x4 Camera::viewShadowVsm(const Tempest::Vec3& lightDir) const {

Matrix4x4 Camera::viewShadowVsm(const Vec3& cameraPos, float rotation, const Tempest::Matrix4x4& viewProj, const Vec3& ldir) const {
float smWidth = 1024; // ~4 pixels per santimeter
float smDepth = 10*5120;
float smDepth = 5*5120;
(void)rotation;

float smWidthInv = 1.f/smWidth;
Expand Down
41 changes: 25 additions & 16 deletions game/graphics/drawcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ DrawCommands::DrawCommands(VisualObjects& owner, DrawBuckets& buckets, DrawClust
tasks.emplace_back(std::move(cmd));
}

if(virtualShadowSys) {
Tempest::DispatchIndirectCommand cmd = {2000,1,1};
vsmIndirectCmd = Resources::device().ssbo(&cmd, sizeof(cmd));
}
}

DrawCommands::~DrawCommands() {
Expand Down Expand Up @@ -184,16 +188,13 @@ bool DrawCommands::commit() {
v.visClusters = device.ssbo(nullptr, size);
}
if(visChg && v.viewport==SceneGlobals::V_Vsm) {
Resources::recycle(std::move(v.vsmClusters));
v.vsmClusters = device.ssbo(nullptr, v.visClusters.byteSize());
}
if(cmdChg) {
Resources::recycle(std::move(v.indirectCmd));
v.indirectCmd = device.ssbo(cx.data(), sizeof(IndirectCmd)*cx.size());
}

if(cmdChg) {
Resources::recycle(std::move(v.pkgOffsets));
v.pkgOffsets = device.ssbo(nullptr, sizeof(uint32_t)*cx.size());
visChg |= (v.viewport==SceneGlobals::V_Vsm);
}

Resources::recycle(std::move(v.descInit));
Expand Down Expand Up @@ -239,13 +240,20 @@ void DrawCommands::updateTasksUniforms() {
for(auto& v:views) {
if(v.viewport!=SceneGlobals::V_Vsm)
continue;
Resources::recycle(std::move(v.descPackDraw));
v.descPackDraw = device.descriptors(Shaders::inst().vsmPackDraw0);
v.descPackDraw.set(1, v.vsmClusters);
v.descPackDraw.set(2, v.visClusters);
v.descPackDraw.set(3, v.indirectCmd);
v.descPackDraw.set(4, *scene.vsmPageList);
v.descPackDraw.set(5, v.pkgOffsets);
Resources::recycle(std::move(v.descPackDraw0));
v.descPackDraw0 = device.descriptors(Shaders::inst().vsmPackDraw0);
v.descPackDraw0.set(1, v.vsmClusters);
v.descPackDraw0.set(2, v.visClusters);
v.descPackDraw0.set(3, v.indirectCmd);
v.descPackDraw0.set(4, *scene.vsmPageList);
v.descPackDraw0.set(5, vsmIndirectCmd);

Resources::recycle(std::move(v.descPackDraw1));
v.descPackDraw1 = device.descriptors(Shaders::inst().vsmPackDraw1);
v.descPackDraw1.set(1, v.vsmClusters);
v.descPackDraw1.set(2, v.visClusters);
v.descPackDraw1.set(3, v.indirectCmd);
v.descPackDraw1.set(4, *scene.vsmPageList);
}

updateVsmUniforms();
Expand Down Expand Up @@ -327,7 +335,7 @@ void DrawCommands::updateCommandUniforms() {
}

if(v==SceneGlobals::V_Vsm) {
desc[v].set(L_CmdOffsets, views[v].pkgOffsets);
desc[v].set(L_CmdOffsets, views[v].indirectCmd);
desc[v].set(L_VsmPages, *scene.vsmPageList);
}
}
Expand Down Expand Up @@ -477,11 +485,12 @@ void DrawCommands::visibilityVsm(Encoder<CommandBuffer>& cmd, uint8_t fId) {
cmd.dispatch(push.meshletCount);
}

cmd.setUniforms(Shaders::inst().vsmPackDraw0, views[SceneGlobals::V_Vsm].descPackDraw);
cmd.setUniforms(Shaders::inst().vsmPackDraw0, views[SceneGlobals::V_Vsm].descPackDraw0);
cmd.dispatch(1);

cmd.setUniforms(Shaders::inst().vsmPackDraw1, views[SceneGlobals::V_Vsm].descPackDraw);
cmd.dispatch(2000); // TODO: indirect
cmd.setUniforms(Shaders::inst().vsmPackDraw1, views[SceneGlobals::V_Vsm].descPackDraw1);
cmd.dispatch(8096); // TODO: indirect
// cmd.dispatchIndirect(vsmIndirectCmd, 0);
}

void DrawCommands::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fId) {
Expand Down
7 changes: 5 additions & 2 deletions game/graphics/drawcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ class DrawCommands {
Tempest::DescriptorSet descInit;
Tempest::StorageBuffer visClusters, indirectCmd;

Tempest::DescriptorSet descPackDraw;
Tempest::StorageBuffer pkgOffsets, vsmClusters;
Tempest::DescriptorSet descPackDraw0;
Tempest::DescriptorSet descPackDraw1;
Tempest::StorageBuffer vsmClusters;

bool isEnabled() const;
};
Expand All @@ -144,4 +145,6 @@ class DrawCommands {
bool cmdDurtyBit = false;

View views[SceneGlobals::V_Count];

Tempest::StorageBuffer vsmIndirectCmd;
};
28 changes: 13 additions & 15 deletions game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,12 @@ void Renderer::resetSwapchain() {
vsm.pagesDbgPso = &Shaders::inst().vsmDbg;
vsm.uboDbg = device.descriptors(*vsm.pagesDbgPso);

vsm.pageTbl = device.image3d(TextureFormat::R32U, 64, 64, 32);
vsm.pageData = device.image2d(TextureFormat::RGBA8, 4096, 4096); // NOTE: not yet sure on what to use: depth or atomics
vsm.pageDataZ = device.zbuffer(shadowFormat, 4096, 4096);
vsm.pageTbl = device.image3d(TextureFormat::R32U, 32, 32, 16);
vsm.pageData = device.zbuffer(shadowFormat, 4096, 4096);
vsm.shadowMask = device.image2d(Tempest::RGBA8, w, h);

auto pageCount = uint32_t((vsm.pageDataZ.w()+128-1)/128) * uint32_t((vsm.pageDataZ.h()+128-1)/128);
vsm.pageList = device.ssbo(nullptr, (pageCount + 4)*sizeof(uint32_t));
auto pageCount = uint32_t((vsm.pageData.w()+128-1)/128) * uint32_t((vsm.pageData.h()+128-1)/128);
vsm.pageList = device.ssbo(nullptr, (pageCount + 2)*sizeof(uint32_t));
}

if(settings.swrEnabled) {
Expand Down Expand Up @@ -466,15 +465,15 @@ void Renderer::prepareUniforms() {
vsm.uboLight.set(2, gbufNormal, Sampler::nearest());
vsm.uboLight.set(3, zbuffer, Sampler::nearest());
vsm.uboLight.set(4, vsm.pageTbl);
vsm.uboLight.set(5, vsm.pageDataZ);
vsm.uboLight.set(5, vsm.pageData);
vsm.uboLight.set(6, vsm.shadowMask);

vsm.uboDbg.set(0, wview->sceneGlobals().uboGlobal[SceneGlobals::V_Main]);
vsm.uboDbg.set(1, gbufDiffuse, Sampler::nearest());
vsm.uboDbg.set(2, gbufNormal, Sampler::nearest());
vsm.uboDbg.set(3, zbuffer, Sampler::nearest());
vsm.uboDbg.set(4, vsm.pageTbl);
vsm.uboDbg.set(5, vsm.pageDataZ);
vsm.uboDbg.set(5, vsm.pageData);
vsm.uboDbg.set(6, vsm.shadowMask);
}

Expand All @@ -492,7 +491,7 @@ void Renderer::prepareUniforms() {
sh[i] = &textureCast<const Texture2d&>(shadowMap[i]);
}
wview->setShadowMaps(sh);
wview->setVirtualShadowMap(vsm.pageData, vsm.pageList);
wview->setVirtualShadowMap(StorageImage(), vsm.pageList);
wview->setSwRenderingImage(swr.outputImage);

wview->setHiZ(textureCast<const Texture2d&>(hiz.hiZ));
Expand Down Expand Up @@ -619,7 +618,7 @@ void Renderer::draw(Tempest::Attachment& result, Encoder<CommandBuffer>& cmd, ui
}
frustrum[SceneGlobals::V_Main].make(viewProj,zbuffer.w(),zbuffer.h());
frustrum[SceneGlobals::V_HiZ] = frustrum[SceneGlobals::V_Main];
frustrum[SceneGlobals::V_Vsm].make(shadowMatrixVsm, vsm.pageDataZ.w(), vsm.pageDataZ.h()); //TODO: mip0 resolution
frustrum[SceneGlobals::V_Vsm].make(shadowMatrixVsm, vsm.pageData.w(), vsm.pageData.h()); //TODO: remove
wview->updateFrustrum(frustrum);
}

Expand Down Expand Up @@ -859,8 +858,12 @@ void Renderer::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fI
cmd.setUniforms(*vsm.pagesMarkPso, vsm.uboPages);
cmd.dispatchThreads(zbuffer.size());

cmd.setUniforms(Shaders::inst().vsmClumpPages, vsm.uboList);
cmd.dispatchThreads(size_t(vsm.pageTbl.w()), size_t(vsm.pageTbl.h()), size_t(vsm.pageTbl.d()));

cmd.setUniforms(*vsm.pagesListPso, vsm.uboList);
cmd.dispatchThreads(size_t(vsm.pageTbl.w()), size_t(vsm.pageTbl.h()), size_t(vsm.pageTbl.d()));

// sort for debug purpose only
cmd.setDebugMarker("VSM-sort(debug)");
cmd.setUniforms(Shaders::inst().vsmSortPages, vsm.uboList);
Expand All @@ -870,13 +873,8 @@ void Renderer::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fI
view.visibilityVsm(cmd,fId);

cmd.setDebugMarker("VSM-rendering");
cmd.setFramebuffer({}, {vsm.pageDataZ, 0.f, Tempest::Preserve});
cmd.setFramebuffer({}, {vsm.pageData, 0.f, Tempest::Preserve});
view.drawVsm(cmd,fId);

//cmd.setDebugMarker("VSM-compose");
//cmd.setFramebuffer({});
//cmd.setUniforms(*vsm.pagesComposePso, vsm.uboCompose);
//cmd.dispatchThreads(zbuffer.size());
}

void Renderer::drawSwr(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fId, WorldView& view) {
Expand Down
3 changes: 1 addition & 2 deletions game/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ class Renderer final {
Tempest::DescriptorSet uboDbg;

Tempest::StorageImage pageTbl;
Tempest::StorageImage pageData;
Tempest::ZBuffer pageDataZ;
Tempest::ZBuffer pageData;
Tempest::StorageBuffer pageList;
Tempest::StorageImage shadowMask;
} vsm;
Expand Down
1 change: 1 addition & 0 deletions game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Shaders::Shaders() {
vsmClusterTask = computeShader("vsm_cluster_task.comp.sprv");
vsmClear = computeShader("vsm_clear.comp.sprv");
vsmMarkPages = computeShader("vsm_mark_pages.comp.sprv");
vsmClumpPages = computeShader("vsm_clump_pages.comp.sprv");
vsmListPages = computeShader("vsm_list_pages.comp.sprv");
vsmSortPages = computeShader("vsm_sort_pages.comp.sprv");
vsmPackDraw0 = computeShader("vsm_pack_draws0.comp.sprv");
Expand Down
4 changes: 2 additions & 2 deletions game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class Shaders {

// Virtual shadow
Tempest::ComputePipeline vsmClusterTask;
Tempest::ComputePipeline vsmClear, vsmMarkPages, vsmListPages, vsmSortPages;
Tempest::ComputePipeline vsmClear, vsmMarkPages, vsmClumpPages, vsmListPages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
// Tempest::ComputePipeline vsmRendering;
Tempest::RenderPipeline vsmDirectLight;
Tempest::ComputePipeline vsmSortPages;
Tempest::RenderPipeline vsmDbg;

// Software rendering
Expand Down
3 changes: 2 additions & 1 deletion shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ add_shader(cmaa2_deferred_color_apply_2x2.frag antialiasing/cmaa2/deferred_c

# virtual shadows
add_shader(direct_light_vsm.frag virtual_shadow/vsm_mark_pages.comp -DCOMPOSE -S frag)
add_shader(vsm_dbg.frag virtual_shadow/vsm_mark_pages.comp -DDEBUG -S frag)
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_dbg.frag virtual_shadow/vsm_mark_pages.comp -DDEBUG -S frag)
add_shader(vsm_clump_pages.comp virtual_shadow/vsm_clump_pages.comp)
add_shader(vsm_list_pages.comp virtual_shadow/vsm_list_pages.comp)
add_shader(vsm_sort_pages.comp virtual_shadow/vsm_sort_pages.comp)
add_shader(vsm_pack_draws0.comp virtual_shadow/vsm_pack_draws.comp -DPASS0)
Expand Down
4 changes: 0 additions & 4 deletions shader/materials/main.frag
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ 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 Down
29 changes: 10 additions & 19 deletions shader/materials/main.vert
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,25 @@ 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, out float clipDistance[4]) {
const ivec3 page = unpackVsmPageInfo(vsm.pageList[shadowPageId]);
const uint data = vsm.pageList[shadowPageId];
const ivec3 page = unpackVsmPageInfo(data);
const ivec2 sz = unpackVsmPageSize(data);
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;
clipDistance[0] = 0 +pos.x;
clipDistance[1] = sz.x-pos.x;
clipDistance[2] = 0 +pos.y;
clipDistance[3] = sz.y-pos.y;
}

const vec2 pageId = vec2(unpackVsmPageId(shadowPageId));
Expand All @@ -87,12 +83,7 @@ vec4 mapViewport(vec4 pos, out float clipDistance[4]) {

#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
shadowPageId = task.w & 0xFFFF;
}
#endif

Expand Down Expand Up @@ -243,12 +234,12 @@ void main() {
#endif

#if defined(VIRTUAL_SHADOW)
const uint firstMeshlet = cmdOffsets[push.commandId];
const uint firstMeshlet = cmd[push.commandId].writeOffset;
#else
const uint firstMeshlet = push.firstMeshlet;
#endif

const uvec4 task = payload[workIndex + firstMeshlet];
const uvec4 task = payload[workIndex + firstMeshlet];

#if defined(VIRTUAL_SHADOW)
initVsm(task);
Expand Down
8 changes: 6 additions & 2 deletions shader/materials/materials_common.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef MATERIALS_COMMON_GLSL
#define MATERIALS_COMMON_GLSL

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

#include "common.glsl"
#include "scene.glsl"

Expand Down Expand Up @@ -201,8 +205,8 @@ layout(binding = L_GDepth ) uniform sampler2D gbufferDepth;
#endif

#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;
layout(binding = L_CmdOffsets, std430) readonly buffer IndirectBuf { IndirectCmd cmd[]; };
layout(binding = L_VsmPages, std430) readonly buffer Pages { VsmHeader header; uint pageList[]; } vsm;
#endif

#if !defined(CLUSTER) && (MESH_TYPE!=T_PFX)
Expand Down
8 changes: 5 additions & 3 deletions shader/virtual_shadow/vsm_clear.comp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_samplerless_texture_functions : enable

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

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

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

Expand All @@ -22,6 +23,7 @@ void main() {

imageStore(pageTbl, at, uvec4(0));
if(at==ivec3(0)) {
header = uvec4(0,1,1,0);
header.pageCount = 0;
header.meshletCount = 0;
}
}
Loading

0 comments on commit 8dcdb1d

Please sign in to comment.