Skip to content

Commit

Permalink
vsm: composing pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Sep 10, 2024
1 parent c8c7ddc commit b00a129
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
19 changes: 18 additions & 1 deletion game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ void Renderer::resetSwapchain() {
vsm.pagesListPso = &Shaders::inst().vsmListPages;
vsm.uboList = device.descriptors(*vsm.pagesListPso);

vsm.directLightPso = &Shaders::inst().vsmDirectLight;
vsm.uboLight = device.descriptors(*vsm.directLightPso);

vsm.pagesDbgPso = &Shaders::inst().vsmDbg;
vsm.uboDbg = device.descriptors(*vsm.pagesDbgPso);

Expand Down Expand Up @@ -458,6 +461,14 @@ void Renderer::prepareUniforms() {
vsm.uboList.set(0, vsm.pageList);
vsm.uboList.set(1, vsm.pageTbl);

vsm.uboLight.set(0, wview->sceneGlobals().uboGlobal[SceneGlobals::V_Main]);
vsm.uboLight.set(1, gbufDiffuse, Sampler::nearest());
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(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());
Expand Down Expand Up @@ -750,7 +761,7 @@ void Renderer::stashSceneAux(Encoder<CommandBuffer>& cmd, uint8_t fId) {
}

void Renderer::drawVsmDbg(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fId) {
static bool enable = true;
static bool enable = false;
if(!enable || !settings.vsmEnabled)
return;

Expand Down Expand Up @@ -933,6 +944,12 @@ void Renderer::drawShadowResolve(Encoder<CommandBuffer>& cmd, uint8_t fId, const
static bool useDsm = true;
if(!useDsm)
return;
if(settings.vsmEnabled) {
cmd.setDebugMarker("DirectSunLight-VSM");
cmd.setUniforms(*vsm.directLightPso, vsm.uboLight);
cmd.draw(Resources::fsqVbo());
return;
}
cmd.setDebugMarker("DirectSunLight");
cmd.setUniforms(*shadow.directLightPso, shadow.ubo);
cmd.draw(Resources::fsqVbo());
Expand Down
3 changes: 3 additions & 0 deletions game/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ class Renderer final {
Tempest::ComputePipeline* pagesListPso = nullptr;
Tempest::DescriptorSet uboList;

Tempest::RenderPipeline* directLightPso = nullptr;
Tempest::DescriptorSet uboLight;

Tempest::RenderPipeline* pagesDbgPso = nullptr;
Tempest::DescriptorSet uboDbg;

Expand Down
1 change: 1 addition & 0 deletions game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Shaders::Shaders() {
vsmSortPages = computeShader("vsm_sort_pages.comp.sprv");
vsmPackDraw0 = computeShader("vsm_pack_draws0.comp.sprv");
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");
}
Expand Down
1 change: 1 addition & 0 deletions game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Shaders {
Tempest::ComputePipeline vsmClear, vsmMarkPages, vsmListPages, vsmSortPages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
// Tempest::ComputePipeline vsmRendering;
Tempest::RenderPipeline vsmDirectLight;
Tempest::RenderPipeline vsmDbg;

// Software rendering
Expand Down
1 change: 1 addition & 0 deletions shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ add_shader(cmaa2_deferred_color_apply_2x2.vert antialiasing/cmaa2/deferred_c
add_shader(cmaa2_deferred_color_apply_2x2.frag antialiasing/cmaa2/deferred_color_apply_2x2.frag)

# virtual shadows
add_shader(direct_light_vsm.frag virtual_shadow/vsm_mark_pages.comp -DCOMPOSE -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)
Expand Down
7 changes: 7 additions & 0 deletions shader/common.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef COMMON_GLSL
#define COMMON_GLSL

#extension GL_EXT_samplerless_texture_functions : enable

const float M_PI = 3.1415926535897932384626433832795;

//Environment
Expand Down Expand Up @@ -281,4 +283,9 @@ vec3 normalFetch(in usampler2D gbufNormal, ivec2 p) {
return decodeNormal(n);
}

vec3 normalFetch(in utexture2D gbufNormal, ivec2 p) {
const uint n = texelFetch(gbufNormal, p, 0).r;
return decodeNormal(n);
}

#endif
58 changes: 48 additions & 10 deletions shader/virtual_shadow/vsm_mark_pages.comp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#extension GL_EXT_samplerless_texture_functions : enable

#include "virtual_shadow/vsm_common.glsl"
#include "lighting/tonemapping.glsl"
#include "scene.glsl"
#include "common.glsl"

Expand Down Expand Up @@ -39,7 +40,7 @@ layout(binding = 5) uniform texture2D pageData;
layout(binding = 6, rgba8) uniform image2D shadowMask;
#endif

#if defined(DEBUG)
#if defined(COMPOSE) || defined(DEBUG)
layout(location = 0) out vec4 outColor;
#endif

Expand Down Expand Up @@ -67,6 +68,16 @@ vec3 shadowPos(float z, ivec2 offset) {
return shPos.xyz;
}

int shadowLod(vec2 dx, vec2 dy) {
float px = dot(dx, dx);
float py = dot(dy, dy);
float maxLod = 0.5 * log2(max(px, py)); // log2(sqrt()) = 0.5*log2()
float minLod = 0.5 * log2(min(px, py));

const float bias = -1;
return int((minLod + maxLod)*0.5 + bias + 0.5);
}

float shadowTexelFetch(vec2 page, int mip) {
#if defined(COMPOSE) || defined(DEBUG)
//page-local
Expand Down Expand Up @@ -99,15 +110,39 @@ void markPage(ivec2 pageI, int mip) {
#endif
}

int shadowLod(vec2 dx, vec2 dy) {
float px = dot(dx, dx);
float py = dot(dy, dy);
float maxLod = 0.5 * log2(max(px, py)); // log2(sqrt()) = 0.5*log2()
float minLod = 0.5 * log2(min(px, py));
#if defined(GL_FRAGMENT_SHADER)
float lambert(vec3 normal) {
return max(0.0, dot(scene.sunDir,normal));
}

const float bias = -1;
return int((minLod + maxLod)*0.5 + bias + 0.5);
vec3 directLight(vec2 page, int mip, float refZ) {
const ivec2 fragCoord = ivec2(gl_FragCoord.xy);
const float d = texelFetch(depth, fragCoord, 0).r;
if(d==1.0) {
return vec3(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));
float shadow = 1;
if(light>0) {
shadow = shadowTest(page, mip, refZ);
}

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

return luminance * scene.exposure;
}
#endif

void main() {
#if defined(DEBUG)
Expand Down Expand Up @@ -141,14 +176,17 @@ void main() {
ivec2 pageI = ivec2((page*0.5+0.5)*VSM_PAGE_TBL_SIZE);
#if defined(MARK_PAGES)
markPage(pageI, mip);
#elif defined(COMPOSE)
outColor = vec4(directLight(page, mip, shPos0.z), 1);
#endif

#if defined(DEBUG)
// 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, mip, shPos0.z)*0.9+0.1);
color *= (1.0 - shadowTexelFetch(page, mip));
// color *= (shadowTest(page, mip, shPos0.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

0 comments on commit b00a129

Please sign in to comment.