Skip to content

Commit

Permalink
vsm: configurable page size; sw-shadows initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Sep 17, 2024
1 parent 4c5b6c0 commit 4e096ff
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 299 deletions.
2 changes: 1 addition & 1 deletion game/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Matrix4x4 Camera::viewShadowVsmLwc(const Tempest::Vec3& ldir) const {

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

float smWidthInv = 1.f/smWidth;
float zScale = 1.f/smDepth;
Expand Down
33 changes: 28 additions & 5 deletions game/graphics/drawcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ DrawCommands::DrawCommands(VisualObjects& owner, DrawBuckets& buckets, DrawClust
Tempest::DispatchIndirectCommand cmd = {2000,1,1};
vsmIndirectCmd = Resources::device().ssbo(&cmd, sizeof(cmd));
}
// vsmSwrImage = Resources::device().image2d(TextureFormat::R16, 4096, 4096);
// vsmSwrImage = Resources::device().image2d(TextureFormat::R32U, 4096, 4096);
}

DrawCommands::~DrawCommands() {
Expand Down Expand Up @@ -349,10 +351,7 @@ void DrawCommands::updateCommandUniforms() {
}

void DrawCommands::updateVsmUniforms() {
if(Gothic::options().swRenderingPreset==0)
return;

if(scene.swMainImage==nullptr)
if(Gothic::options().swRenderingPreset==0 && Gothic::options().doVirtualShadow==false)
return;

auto& device = Resources::device();
Expand All @@ -377,7 +376,7 @@ void DrawCommands::updateVsmUniforms() {
}

const uint32_t preset = Gothic::options().swRenderingPreset;
if(preset>0 && !Shaders::inst().swRendering.isEmpty()) {
if(preset>0 && !Shaders::inst().swRendering.isEmpty() && scene.swMainImage!=nullptr) {
Resources::recycle(std::move(swrDesc));
swrDesc = device.descriptors(Shaders::inst().swRendering);
swrDesc.set(0, *scene.swMainImage);
Expand All @@ -394,6 +393,21 @@ void DrawCommands::updateVsmUniforms() {
swrDesc.set(10, owner.instanceSsbo());
}
}

if(false && Gothic::options().doVirtualShadow && scene.vsmPageList!=nullptr) {
Resources::recycle(std::move(vsmDesc));
vsmDesc = device.descriptors(Shaders::inst().vsmRendering);

vsmDesc.set(0, vsmSwrImage);
vsmDesc.set(1, scene.uboGlobal[SceneGlobals::V_Vsm]);
vsmDesc.set(2, *scene.vsmPageList);
vsmDesc.set(3, clusters.ssbo());
vsmDesc.set(4, owner.instanceSsbo());
vsmDesc.set(5, ibo);
vsmDesc.set(6, vbo);
vsmDesc.set(7, tex);
vsmDesc.set(8, Sampler::bilinear());
}
}

void DrawCommands::prepareUniforms() {
Expand Down Expand Up @@ -517,6 +531,15 @@ void DrawCommands::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_
cmd.dispatchMeshIndirect(view.indirectCmd, sizeof(IndirectCmd)*id + sizeof(uint32_t)); else
cmd.drawIndirect(view.indirectCmd, sizeof(IndirectCmd)*id);
}

if(false) {
cmd.setFramebuffer({});
struct Push { uint32_t meshletCount; } push = {};
push.meshletCount = uint32_t(clusters.size());
cmd.setUniforms(Shaders::inst().vsmRendering, vsmDesc, &push, sizeof(push));
// const auto sz = Shaders::inst().vsmRendering.workGroupSize();
cmd.dispatch(1024u);
}
}

void DrawCommands::drawHiZ(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fId) {
Expand Down
2 changes: 2 additions & 0 deletions game/graphics/drawcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@ class DrawCommands {
View views[SceneGlobals::V_Count];

Tempest::StorageBuffer vsmIndirectCmd;
Tempest::StorageImage vsmSwrImage;
Tempest::DescriptorSet vsmDesc;
};
4 changes: 3 additions & 1 deletion game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ void Renderer::resetSwapchain() {
vsm.pageData = device.zbuffer(shadowFormat, 4096, 4096);
vsm.shadowMask = device.image2d(Tempest::RGBA8, w, h);

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

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

//TODO: trimming
//cmd.setUniforms(Shaders::inst().vsmClumpPages0, vsm.uboList);
//cmd.dispatch(1);

Expand Down
2 changes: 1 addition & 1 deletion game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Shaders::Shaders() {
vsmPackDraw1 = computeShader("vsm_pack_draws1.comp.sprv");
vsmDirectLight = postEffect("copy", "direct_light_vsm", RenderState::ZTestMode::NoEqual);
vsmDbg = postEffect("copy", "vsm_dbg", RenderState::ZTestMode::Always);
// vsmRendering = computeShader("vsm_rendering.comp.sprv");
vsmRendering = computeShader("vsm_rendering.comp.sprv");
}

if(Gothic::options().swRenderingPreset>0) {
Expand Down
2 changes: 2 additions & 0 deletions game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Shaders {
Tempest::ComputePipeline vsmSortPages;
Tempest::RenderPipeline vsmDbg;

Tempest::ComputePipeline vsmRendering;

// Software rendering
Tempest::ComputePipeline swRendering;
Tempest::RenderPipeline swRenderingDbg;
Expand Down
22 changes: 11 additions & 11 deletions shader/materials/materials_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@ const vec3 debugColors[MAX_DEBUG_COLORS] = {
};
#endif

#define MAX_NUM_SKELETAL_NODES 96
#define MAX_MORPH_LAYERS 4
#define MaxVert 64
#define MaxPrim 64
#define MaxInd (MaxPrim*3)

#define T_LANDSCAPE 0
#define T_OBJ 1
#define T_SKINING 2
#define T_MORPH 3
#define T_PFX 4
const uint MAX_NUM_SKELETAL_NODES = 96;
const uint MAX_MORPH_LAYERS = 4;
const uint MaxVert = 64;
const uint MaxPrim = 64;
const uint MaxInd = MaxPrim*3;

const uint L_Scene = 0;
const uint L_Payload = 1;
Expand All @@ -68,6 +62,12 @@ const uint L_GDepth = 13;
const uint L_CmdOffsets = 14;
const uint L_VsmPages = 15;

#define T_LANDSCAPE 0
#define T_OBJ 1
#define T_SKINING 2
#define T_MORPH 3
#define T_PFX 4

#ifndef MESH_TYPE
#define MESH_TYPE 255
#endif
Expand Down
2 changes: 1 addition & 1 deletion shader/software_rendering/sw_light.comp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ layout(push_constant, std430) uniform UboPush {
} push;

//layout(binding = 0, rgba8) uniform image2D outputImage;
layout(binding = 0, r32ui) uniform uimage2D outputImage;
layout(binding = 0, r32ui) uniform uimage2D outputImage;
layout(binding = 1, std140) uniform UboScene {
SceneDesc scene;
};
Expand Down
65 changes: 3 additions & 62 deletions shader/virtual_shadow/vsm_clump_pages.comp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void trimMip(int mip) {
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>0 && b>0 && c>0 && d>0) {
if(a+b+c+d == 4) {
imageStore(pageTbl, ax, uvec4(0));
}
}
Expand All @@ -62,7 +62,7 @@ void mainGroups() {
uint b = imageLoad(pageTbl, at+ivec3(1,0,0)).r;
uint c = imageLoad(pageTbl, at+ivec3(1,1,0)).r;
uint d = imageLoad(pageTbl, at+ivec3(0,1,0)).r;
if(a==1 && b==1 && c==1 && d==1) {
if(a+b+c+d >= 2) {
imageStore(pageTbl, at+ivec3(0,0,0), uvec4(2));
imageStore(pageTbl, at+ivec3(1,0,0), uvec4(0));
imageStore(pageTbl, at+ivec3(0,1,0), uvec4(0));
Expand All @@ -77,7 +77,7 @@ void mainGroups() {
uint b = imageLoad(pageTbl, at+ivec3(2,0,0)).r;
uint c = imageLoad(pageTbl, at+ivec3(2,2,0)).r;
uint d = imageLoad(pageTbl, at+ivec3(0,2,0)).r;
if(a==2 && b==2 && c==2 && d==2) {
if(a+b+c+d >= 3) {
imageStore(pageTbl, at+ivec3(0,0,0), uvec4(4));
imageStore(pageTbl, at+ivec3(2,0,0), uvec4(0));
imageStore(pageTbl, at+ivec3(0,2,0), uvec4(0));
Expand All @@ -95,62 +95,3 @@ void main() {
#error "invalid pass-id"
#endif
}

/*
shared uint pageVal[gl_WorkGroupSize.x][gl_WorkGroupSize.y];
void main2() {
const ivec3 size = imageSize(pageTbl);
const ivec3 at = ivec3(gl_GlobalInvocationID);
const ivec3 id = ivec3(gl_LocalInvocationID);

#if 0
if(at.z==1) {
uint pageS = 0;
if(all(lessThan(at, size)))
pageS = imageLoad(pageTbl, at).r;

const int step = VSM_PAGE_TBL_SIZE/4;
if(all(lessThan(ivec2(step), at.xy)) && all(lessThan(at.xy, ivec2(VSM_PAGE_TBL_SIZE-step)))) {
ivec3 x = ivec3(ivec2(at.xy - step)*2, 0);

bool t = (imageLoad(pageTbl, x+ivec3(0,0,0)).r>0) &&
(imageLoad(pageTbl, x+ivec3(1,0,0)).r>0) &&
(imageLoad(pageTbl, x+ivec3(0,1,0)).r>0) &&
(imageLoad(pageTbl, x+ivec3(1,1,0)).r>0);

if(t)
;//imageStore(pageTbl, at, uvec4(0));
}
}
#endif

uint pageS = 0;
if(all(lessThan(at, size)))
pageS = imageLoad(pageTbl, at).r;
pageVal[id.x][id.y] = pageS;
barrier();

// only 2x2 for now
if(id.x%2==0 && id.y%2==0) {
uint pageL = pageVal[id.x+1][id.y+0];
uint pageR = pageVal[id.x+0][id.y+1];
uint pageD = pageVal[id.x+1][id.y+1];

if(pageS==1 && pageL==1 && pageR==1 && pageD==1) {
pageS = 2;
pageVal[id.x+0][id.y+0] = 2;
pageVal[id.x+1][id.y+0] = 0;
pageVal[id.x+0][id.y+1] = 0;
pageVal[id.x+1][id.y+1] = 0;
}
}
barrier();

if(pageS>1) {
imageStore(pageTbl, at+ivec3(0,0,0), uvec4(pageS,0,0,0));
imageStore(pageTbl, at+ivec3(1,0,0), uvec4(0));
imageStore(pageTbl, at+ivec3(0,1,0), uvec4(0));
imageStore(pageTbl, at+ivec3(1,1,0), uvec4(0));
}
}
*/
2 changes: 1 addition & 1 deletion shader/virtual_shadow/vsm_cluster_task.comp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bool projectSphere(const vec4 sphere, out vec4 aabb, out float depthMin) {
const float R = sphere.w;

const float smWidthInv = 1.0/(1024.0); // NOTE: from camera.cpp
const float zScale = 1.0/(5*5120.0);
const float zScale = 1.0/(10*5120.0);

float Rz = R * zScale;
if(c.z - Rz > 1 || c.z + Rz < 0)
Expand Down
6 changes: 3 additions & 3 deletions shader/virtual_shadow/vsm_common.glsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#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 = 32; // small for testing, 64 can be better
const int VSM_PAGE_PER_ROW = 32;
const int VSM_PAGE_TBL_SIZE = 32; // small for testing, 64 can be better
const int VSM_PAGE_PER_ROW = 4096/VSM_PAGE_SIZE;
const int VSM_MAX_PAGES = VSM_PAGE_PER_ROW * VSM_PAGE_PER_ROW; // 1024;
const int VSM_CLIPMAP_SIZE = VSM_PAGE_SIZE * VSM_PAGE_TBL_SIZE;

struct VsmHeader {
Expand Down
Loading

0 comments on commit 4e096ff

Please sign in to comment.