Skip to content

Commit

Permalink
new fog pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Oct 13, 2024
1 parent c02263d commit 72e5768
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 34 deletions.
9 changes: 8 additions & 1 deletion game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ void Renderer::resetSwapchain() {
for(int i=0; i<Resources::ShadowLayers; ++i) {
if(settings.vsmEnabled && (i+1)!=Resources::ShadowLayers)
continue;
if(settings.vsmEnabled && !settings.giEnabled)
continue; //TODO: support vsm in gi code
shadowMap[i] = device.zbuffer(shadowFormat,smSize,smSize);
}
}
Expand Down Expand Up @@ -875,7 +877,12 @@ void Renderer::drawVsm(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint8_t fI
cmd.setUniforms(shaders.vsmMarkPages, vsm.uboPages, &settings.vsmMipBias, sizeof(settings.vsmMipBias));
cmd.dispatchThreads(zbuffer.size());

wview.vsmMarkSkyPages(cmd, fId);
// sky&fog
if(wview.sky().isVolumetric()) {
cmd.setUniforms(shaders.vsmMarkFogPages, vsm.uboPages);
cmd.dispatchThreads(zbuffer.size());
// wview.vsmMarkSkyPages(cmd, fId);
}

if(vsm.pageDataCs.isEmpty()) {
// trimming
Expand Down
29 changes: 15 additions & 14 deletions game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,22 @@ Shaders::Shaders() {
if(Gothic::options().doVirtualShadow) {
vsmClusterTask = computeShader("vsm_cluster_task.comp.sprv");
// vsmClusterTask = computeShader("vsm_cluster_task2.comp.sprv");
vsmClear = computeShader("vsm_clear.comp.sprv");
vsmClearPages = computeShader("vsm_clear_pages.comp.sprv");
vsmMarkPages = computeShader("vsm_mark_pages.comp.sprv");
vsmMarkSky = computeShader("fog3d_vsm_mark_pages.comp.sprv");
vsmTrimPages = computeShader("vsm_trim_pages.comp.sprv");
vsmClumpPages = computeShader("vsm_clump_pages.comp.sprv");
vsmListPages = computeShader("vsm_list_pages.comp.sprv");
vsmAllocPages = computeShader("vsm_alloc_pages.comp.sprv");
vsmMergePages = computeShader("vsm_merge_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);
vsmClear = computeShader("vsm_clear.comp.sprv");
vsmClearPages = computeShader("vsm_clear_pages.comp.sprv");
vsmMarkPages = computeShader("vsm_mark_pages.comp.sprv");
vsmMarkFogPages = computeShader("vsm_mark_fog_pages.comp.sprv");
vsmMarkSky = computeShader("fog3d_vsm_mark_pages.comp.sprv");
vsmTrimPages = computeShader("vsm_trim_pages.comp.sprv");
vsmClumpPages = computeShader("vsm_clump_pages.comp.sprv");
vsmListPages = computeShader("vsm_list_pages.comp.sprv");
vsmAllocPages = computeShader("vsm_alloc_pages.comp.sprv");
vsmMergePages = computeShader("vsm_merge_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);
// vsmReprojectSm = postEffect("copy", "vsm_reproject_sm", RenderState::ZTestMode::Always);
vsmDbg = postEffect("copy", "vsm_dbg", RenderState::ZTestMode::Always);
vsmRendering = computeShader("vsm_rendering.comp.sprv");
vsmDbg = postEffect("copy", "vsm_dbg", RenderState::ZTestMode::Always);
vsmRendering = computeShader("vsm_rendering.comp.sprv");
{
RenderState state;
state.setZWriteEnabled(true);
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Shaders {

// Virtual shadow
Tempest::ComputePipeline vsmClusterTask;
Tempest::ComputePipeline vsmClear, vsmClearPages, vsmMarkPages, vsmMarkSky;
Tempest::ComputePipeline vsmClear, vsmClearPages, vsmMarkPages, vsmMarkFogPages, vsmMarkSky;
Tempest::ComputePipeline vsmTrimPages, vsmListPages, vsmClumpPages, vsmAllocPages, vsmMergePages;
Tempest::ComputePipeline vsmPackDraw0, vsmPackDraw1;
Tempest::RenderPipeline vsmDirectLight;
Expand Down
6 changes: 5 additions & 1 deletion game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Sky::~Sky() {
void Sky::setupSettings() {
auto& device = Resources::device();
const bool fog = Gothic::inst().settingsGetI("RENDERER_D3D","zFogRadial")!=0;
const bool vsm = false; //Gothic::inst().options().doVirtualShadow;
const bool vsm = Gothic::inst().options().doVirtualShadow;

auto q = Quality::VolumetricLQ;
if(!fog) {
Expand Down Expand Up @@ -208,6 +208,10 @@ float Sky::isNight() const {
return 1.f - linearstep(-0.18f, 0.f, sun.dir().y);
}

bool Sky::isVolumetric() const {
return quality!=VolumetricLQ;
}

void Sky::setWorld(const World& world, const std::pair<Vec3, Vec3>& bbox) {
setupSettings();
}
Expand Down
1 change: 1 addition & 0 deletions game/graphics/sky/sky.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Sky final {
const State& cloudsNight() const { return clouds[1]; }
Tempest::Vec2 cloudsOffset(int layer) const;
float isNight() const;
bool isVolumetric() const;

private:
enum Quality : uint8_t {
Expand Down
33 changes: 17 additions & 16 deletions shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,23 @@ 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_direct_light.frag)
add_shader(vsm_reproject_sm.frag virtual_shadow/vsm_reproject_sm.frag)
add_shader(vsm_dbg.frag virtual_shadow/vsm_direct_light.frag -DDEBUG)
add_shader(vsm_mark_pages.comp virtual_shadow/vsm_mark_pages.comp)
add_shader(vsm_clear.comp virtual_shadow/vsm_clear.comp)
add_shader(vsm_clear_pages.comp virtual_shadow/vsm_clear_pages.comp)
add_shader(vsm_trim_pages.comp virtual_shadow/vsm_trim_pages.comp)
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_alloc_pages.comp virtual_shadow/vsm_alloc_pages.comp)
add_shader(vsm_merge_pages.comp virtual_shadow/vsm_merge_pages.comp)
add_shader(vsm_pack_draws0.comp virtual_shadow/vsm_pack_draws.comp -DPASS0)
add_shader(vsm_pack_draws1.comp virtual_shadow/vsm_pack_draws.comp -DPASS1)
add_shader(vsm_cluster_task.comp virtual_shadow/vsm_cluster_task.comp -DVIRTUAL_SHADOW)

add_shader(vsm_rendering.comp virtual_shadow/vsm_rendering.comp)
add_shader(direct_light_vsm.frag virtual_shadow/vsm_direct_light.frag)
add_shader(vsm_reproject_sm.frag virtual_shadow/vsm_reproject_sm.frag)
add_shader(vsm_dbg.frag virtual_shadow/vsm_direct_light.frag -DDEBUG)
add_shader(vsm_mark_pages.comp virtual_shadow/vsm_mark_pages.comp)
add_shader(vsm_mark_fog_pages.comp virtual_shadow/vsm_mark_fog_pages.comp)
add_shader(vsm_clear.comp virtual_shadow/vsm_clear.comp)
add_shader(vsm_clear_pages.comp virtual_shadow/vsm_clear_pages.comp)
add_shader(vsm_trim_pages.comp virtual_shadow/vsm_trim_pages.comp)
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_alloc_pages.comp virtual_shadow/vsm_alloc_pages.comp)
add_shader(vsm_merge_pages.comp virtual_shadow/vsm_merge_pages.comp)
add_shader(vsm_pack_draws0.comp virtual_shadow/vsm_pack_draws.comp -DPASS0)
add_shader(vsm_pack_draws1.comp virtual_shadow/vsm_pack_draws.comp -DPASS1)
add_shader(vsm_cluster_task.comp virtual_shadow/vsm_cluster_task.comp -DVIRTUAL_SHADOW)

add_shader(vsm_rendering.comp virtual_shadow/vsm_rendering.comp)

# software rendering
add_shader(sw_rendering_imm.comp software_rendering/sw_rendering_imm.comp)
Expand Down
3 changes: 2 additions & 1 deletion shader/virtual_shadow/vsm_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
const int VSM_PAGE_SIZE = 128;
const int VSM_PAGE_TBL_SIZE = 32; // small for testing, 64 can be better
const int VSM_PAGE_MIPS = 16;
const int VSM_FOG_MIP = 6;
//const int VSM_FOG_MIP = 6;
//const int VSM_PAGE_PER_ROW = 4096/VSM_PAGE_SIZE;
const int VSM_FOG_MIP = 5;
const int VSM_PAGE_PER_ROW = 8192/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;
Expand Down
123 changes: 123 additions & 0 deletions shader/virtual_shadow/vsm_mark_fog_pages.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#version 450

#extension GL_GOOGLE_include_directive : enable
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_samplerless_texture_functions : enable
// #extension GL_KHR_memory_scope_semantics : enable

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

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

const uint NumThreads = gl_WorkGroupSize.x*gl_WorkGroupSize.y*gl_WorkGroupSize.z;

layout(binding = 0, std140) uniform UboScene {
SceneDesc scene;
};
layout(binding = 1) uniform texture2D gbufDiffuse;
layout(binding = 2) uniform utexture2D gbufNormal;
layout(binding = 3) uniform texture2D gbufDepth;
layout(binding = 4, r32ui) uniform uimage3D pageTbl;
layout(binding = 5, r32ui) uniform uimage3D pageTblDepth;
// layout(binding = 6, r32ui) uniform writeonly restrict uimage2D occlusionLut;

const float dFogMin = 0;
const float dFogMax = 0.9999;

ivec2 fragCoord = ivec2(gl_GlobalInvocationID.xy);

shared uint tileDepthMin, tileDepthMax;

void markPage(ivec3 at, float z) {
imageAtomicExchange(pageTbl, at, 1u);
imageAtomicMin(pageTblDepth, at, floatBitsToUint(z));
}

void hiTile(float depth) {
uint d = floatBitsToUint(depth);
atomicMax(tileDepthMax, d);
atomicMin(tileDepthMin, d);
}

float interleavedGradientNoise() {
return interleavedGradientNoise(fragCoord.xy);
}

vec3 project(mat4 m, vec3 pos) {
vec4 p = m*vec4(pos,1);
return p.xyz/p.w;
}

void shadowFactor(vec4 shPos) {
vec3 shPos0 = shPos.xyz/shPos.w;

int mip = vsmCalcMipIndex(shPos0.xy, VSM_FOG_MIP);
vec2 page = shPos0.xy / (1 << mip);
if(any(greaterThan(abs(page), vec2(1))))
return;

ivec2 pageI = ivec2((page*0.5+0.5)*VSM_PAGE_TBL_SIZE);
ivec3 at = ivec3(pageI, mip);
markPage(at, shPos0.z);
}

void fogPages(const float depthMax) {
const int steps = 32;
const uint lane = gl_LocalInvocationIndex;
const float noise = interleavedGradientNoise()/steps;
const ivec2 size = textureSize(gbufDepth, 0).xy;

const vec2 frPos = vec2(gl_WorkGroupID.xy*gl_WorkGroupSize.xy) + vec2(gl_WorkGroupSize.xy)*0.5;
const vec2 inPos = (vec2(frPos+vec2(0.5))/vec2(size))*2.0 - vec2(1.0);

const vec3 pos0 = project(scene.viewProjectLwcInv, vec3(inPos, dFogMin));
const vec3 pos1 = project(scene.viewProjectLwcInv, vec3(inPos, dFogMax));
const vec3 posz = project(scene.viewProjectLwcInv, vec3(inPos, depthMax));

const vec4 shPos0 = scene.viewVirtualShadowLwc*vec4(pos0, 1);
const vec4 shPos1 = scene.viewVirtualShadowLwc*vec4(posz, 1);

const vec3 ray = pos1.xyz - pos0.xyz;
const float dist = length(ray)*0.01; // meters
const float distZ = length(posz-pos0)*0.01; // meters

if(lane < steps) {
float t = (lane+0.3)/float(steps);
float dd = (t*distZ)/(dist);
vec4 shPos = mix(shPos0, shPos1, t+noise);

shadowFactor(shPos);
}
/*
uint occlusion = 0;
[[dont_unroll]]
for(uint i=0; i<steps; ++i) {
float t = (i+0.3)/float(steps);
float dd = (t*distZ)/(dist);
vec4 shPos = mix(shPos0,shPos1,t+noise);
bool shadow = shadowFactor(shPos);
occlusion = occlusion | ((shadow ? 1u : 0u) << uint(i));
}
imageStore(occlusionLut, ivec2(invocationID.xy), uvec4(occlusion));
*/
}

void main() {
tileDepthMin = floatBitsToUint(1);
tileDepthMax = floatBitsToUint(0);
barrier();

const ivec2 imgSize = ivec2(textureSize(gbufDepth, 0));
const bool valid = all(lessThan(fragCoord, imgSize));
// const vec3 normal = valid ? normalFetch(gbufNormal, pix) : vec3(0);
const float depth = valid ? texelFetch(gbufDepth, fragCoord, 0).x : 1;

if(valid)
hiTile(depth);
barrier();

fogPages(uintBitsToFloat(tileDepthMax));
}

0 comments on commit 72e5768

Please sign in to comment.