Skip to content

Commit

Permalink
vsm fog initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Sep 26, 2024
1 parent 9184db0 commit 5a031f4
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 71 deletions.
6 changes: 3 additions & 3 deletions game/graphics/drawcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,14 @@ void DrawCommands::updateCommandUniforms() {
desc[v].set(L_GDepth, *scene.sceneDepth, smp);
}

if(v==SceneGlobals::V_Vsm && scene.vsmPageData!=nullptr && !scene.vsmPageData->isEmpty()) {
if(v==SceneGlobals::V_Vsm && scene.vsmPageDataCs->w()>1) {
// atomic
desc[v].set(L_CmdOffsets, views[v].indirectCmd);
desc[v].set(L_VsmPages, *scene.vsmPageList);
desc[v].set(L_VsmTbl, *scene.vsmPageTbl);
desc[v].set(L_VsmData, *scene.vsmPageData);
desc[v].set(L_VsmData, *scene.vsmPageDataCs);
}
if(v==SceneGlobals::V_Vsm && scene.vsmPageData!=nullptr && scene.vsmPageData->isEmpty()) {
else if(v==SceneGlobals::V_Vsm) {
// raster
desc[v].set(L_CmdOffsets, views[v].indirectCmd);
desc[v].set(L_VsmPages, *scene.vsmPageList);
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void Renderer::prepareUniforms() {
sh[i] = &textureCast<const Texture2d&>(shadowMap[i]);
}
wview->setShadowMaps(sh);
wview->setVirtualShadowMap(vsm.pageDataCs, vsm.pageTbl, vsm.pageHiZ, vsm.pageList);
wview->setVirtualShadowMap(vsm.pageData, vsm.pageDataCs, vsm.pageTbl, vsm.pageHiZ, vsm.pageList);
wview->setSwRenderingImage(swr.outputImage);

wview->setHiZ(textureCast<const Texture2d&>(hiz.hiZ));
Expand Down
16 changes: 11 additions & 5 deletions game/graphics/sceneglobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ SceneGlobals::SceneGlobals() {
for(auto& i:shadowMap)
i = &Resources::fallbackBlack();

vsmPageData = &Resources::fallbackBlack();
vsmPageTbl = &Resources::fallbackImage3d();
vsmPageHiZ = &Resources::fallbackImage3d();

for(uint8_t lay=0; lay<V_Count; ++lay) {
uboGlobal[lay] = device.ssbo(nullptr,sizeof(UboGlobal));
}
Expand Down Expand Up @@ -211,14 +215,16 @@ void SceneGlobals::setShadowMap(const Tempest::Texture2d* tex[]) {
shadowMap[i] = tex[i];
}

void SceneGlobals::setVirtualShadowMap(const Tempest::StorageImage& pageData,
void SceneGlobals::setVirtualShadowMap(const Tempest::ZBuffer& pageData,
const Tempest::StorageImage& pageDataCs,
const Tempest::StorageImage& pageTbl,
const Tempest::StorageImage& pageHiZ,
const Tempest::StorageBuffer& pageList) {
vsmPageData = &pageData;
vsmPageTbl = &pageTbl;
vsmPageHiZ = &pageHiZ;
vsmPageList = &pageList;
vsmPageData = &Tempest::textureCast<const Tempest::Texture2d&>(pageData);
vsmPageDataCs = &pageDataCs;
vsmPageTbl = &pageTbl;
vsmPageHiZ = &pageHiZ;
vsmPageList = &pageList;
}

void SceneGlobals::setSwRenderingImage(const Tempest::StorageImage& mainView) {
Expand Down
12 changes: 7 additions & 5 deletions game/graphics/sceneglobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class SceneGlobals final {
void setHiZ(const Tempest::Texture2d& hiZ);
void setShadowMap(const Tempest::Texture2d* tex[]);

void setVirtualShadowMap(const Tempest::StorageImage& vsmPageData,
void setVirtualShadowMap(const Tempest::ZBuffer& vsmPageData,
const Tempest::StorageImage& vsmPageDataCs,
const Tempest::StorageImage& pageTbl,
const Tempest::StorageImage& pageHiZ,
const Tempest::StorageBuffer& vsmPageList);
Expand Down Expand Up @@ -76,10 +77,11 @@ class SceneGlobals final {

const Tempest::StorageBuffer* lights = nullptr;

const Tempest::StorageImage* vsmPageData = nullptr;
const Tempest::StorageImage* vsmPageTbl = nullptr;
const Tempest::StorageImage* vsmPageHiZ = nullptr;
const Tempest::StorageBuffer* vsmPageList = nullptr;
const Tempest::Texture2d* vsmPageData = nullptr;
const Tempest::StorageImage* vsmPageDataCs = nullptr;
const Tempest::StorageImage* vsmPageTbl = nullptr;
const Tempest::StorageImage* vsmPageHiZ = nullptr;
const Tempest::StorageBuffer* vsmPageList = nullptr;

const Tempest::StorageImage* swMainImage = nullptr;

Expand Down
4 changes: 3 additions & 1 deletion game/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ Shaders::Shaders() {
fogViewLut3d = computeShader("fog_view_lut.comp.sprv");
shadowDownsample = computeShader("shadow_downsample.comp.sprv");
fogOcclusion = computeShader("fog3d.comp.sprv");
if(Gothic::options().doVirtualShadow)
fogOcclusionVsm = computeShader("fog3d_vsm.comp.sprv");

skyExposure = computeShader("sky_exposure.comp.sprv");

sky = postEffect("sky");
fog = fogShader ("fog");
fog3dHQ = fogShader ("fog3d_hq");
Expand Down Expand Up @@ -195,6 +196,7 @@ 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");
Expand Down
2 changes: 1 addition & 1 deletion game/graphics/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Shaders {
Tempest::RenderPipeline fog;
Tempest::RenderPipeline fog3dHQ;
Tempest::RenderPipeline sun;
Tempest::ComputePipeline cloudsLut, fogOcclusion;
Tempest::ComputePipeline cloudsLut, fogOcclusion, fogOcclusionVsm;
Tempest::ComputePipeline fogViewLut3d, shadowDownsample;
Tempest::ComputePipeline skyExposure;

Expand Down
42 changes: 33 additions & 9 deletions game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,21 @@ Sky::~Sky() {
}

void Sky::setupSettings() {
auto& device = Resources::device();
bool fog = Gothic::inst().settingsGetI("RENDERER_D3D","zFogRadial")!=0;
auto& device = Resources::device();
const bool fog = Gothic::inst().settingsGetI("RENDERER_D3D","zFogRadial")!=0;
const bool vsm = false;//Gothic::inst().options().doVirtualShadow;

auto q = Quality::VolumetricLQ;
if(!fog) {
q = Quality::VolumetricLQ;
}
else if(fog && !vsm) {
q = Quality::VolumetricHQ;
}
else if(fog && vsm) {
q = Quality::VolumetricHQVsm;
}

auto q = fog ? Quality::VolumetricHQ : Quality::VolumetricLQ;
if(pathTrace)
q = PathTrace;

Expand All @@ -129,6 +140,7 @@ void Sky::setupSettings() {
occlusionLut = StorageImage();
break;
case VolumetricHQ:
case VolumetricHQVsm:
// fogLut and oclussion are decupled
fogLut3D = device.image3d(lutRGBAFormat,128,64,32);
shadowDw = StorageImage();
Expand Down Expand Up @@ -274,7 +286,7 @@ void Sky::prepareUniforms() {
auto smpB = Sampler::bilinear();
smpB.setClamping(ClampMode::ClampToEdge);

if(quality==VolumetricHQ) {
if(quality==VolumetricHQ || quality==VolumetricHQVsm) {
occlusionScale = 1;
if(uint32_t(scene.zbuffer->h()) > 1080 &&
device.properties().type!=DeviceType::Discrete) {
Expand Down Expand Up @@ -340,15 +352,22 @@ void Sky::prepareUniforms() {
uboFog.set(2, scene.uboGlobal[SceneGlobals::V_Main]);
}

if(quality==VolumetricHQ) {
if(quality==VolumetricHQ || quality==VolumetricHQVsm) {
auto smpLut3d = Sampler::bilinear();
smpLut3d.setClamping(ClampMode::ClampToEdge);

uboOcclusion = device.descriptors(Shaders::inst().fogOcclusion);
const bool vsm = (quality==VolumetricHQVsm);
auto& fogOcclusion = vsm ? Shaders::inst().fogOcclusionVsm : Shaders::inst().fogOcclusion;
uboOcclusion = device.descriptors(fogOcclusion);
uboOcclusion.set(1, *scene.zbuffer, Sampler::nearest());
uboOcclusion.set(2, scene.uboGlobal[SceneGlobals::V_Main]);
uboOcclusion.set(3, occlusionLut);
uboOcclusion.set(4, *scene.shadowMap[1], Resources::shadowSampler());
if(quality==VolumetricHQVsm) {
uboOcclusion.set(4, *scene.vsmPageTbl);
uboOcclusion.set(5, *scene.vsmPageData);
} else {
uboOcclusion.set(4, *scene.shadowMap[1], Resources::shadowSampler());
}

uboFogViewLut3d = device.descriptors(Shaders::inst().fogViewLut3d);
uboFogViewLut3d.set(0, scene.uboGlobal[SceneGlobals::V_Main]);
Expand Down Expand Up @@ -438,9 +457,13 @@ void Sky::prepareFog(Tempest::Encoder<Tempest::CommandBuffer>& cmd, uint32_t fra
cmd.dispatchThreads(uint32_t(fogLut3D.w()),uint32_t(fogLut3D.h()));
break;
}
case VolumetricHQ: {
case VolumetricHQ:
case VolumetricHQVsm: {
const bool vsm = (quality==VolumetricHQVsm);
auto& fogOcclusion = vsm ? Shaders::inst().fogOcclusionVsm : Shaders::inst().fogOcclusion;

cmd.setFramebuffer({});
cmd.setUniforms(Shaders::inst().fogOcclusion, uboOcclusion, &ubo, sizeof(ubo));
cmd.setUniforms(fogOcclusion, uboOcclusion, &ubo, sizeof(ubo));
cmd.dispatchThreads(occlusionLut.size());

cmd.setUniforms(Shaders::inst().fogViewLut3d, uboFogViewLut3d, &ubo, sizeof(ubo));
Expand Down Expand Up @@ -479,6 +502,7 @@ void Sky::drawFog(Tempest::Encoder<CommandBuffer>& cmd, uint32_t fId) {
cmd.setUniforms(Shaders::inst().fog, uboFog, &ubo, sizeof(ubo));
break;
case VolumetricHQ:
case VolumetricHQVsm:
cmd.setUniforms(Shaders::inst().fog3dHQ, uboFog3d, &ubo, sizeof(ubo));
break;
case PathTrace:
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 @@ -48,6 +48,7 @@ class Sky final {
None,
VolumetricLQ,
VolumetricHQ,
VolumetricHQVsm,
PathTrace,
};

Expand Down
5 changes: 3 additions & 2 deletions game/graphics/worldview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ void WorldView::setShadowMaps(const Tempest::Texture2d* sh[]) {
sGlobal.setShadowMap(shadow);
}

void WorldView::setVirtualShadowMap(const Tempest::StorageImage& pageData,
void WorldView::setVirtualShadowMap(const Tempest::ZBuffer& pageData,
const Tempest::StorageImage& pageDataCs,
const Tempest::StorageImage& pageTbl,
const Tempest::StorageImage& pageHiZ,
const Tempest::StorageBuffer& pageList) {
sGlobal.setVirtualShadowMap(pageData, pageTbl, pageHiZ, pageList);
sGlobal.setVirtualShadowMap(pageData, pageDataCs, pageTbl, pageHiZ, pageList);
}

void WorldView::setSwRenderingImage(const Tempest::StorageImage& mainView) {
Expand Down
3 changes: 2 additions & 1 deletion game/graphics/worldview.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class WorldView {
void setGbuffer(const Tempest::Texture2d& diffuse,
const Tempest::Texture2d& norm);
void setShadowMaps (const Tempest::Texture2d* shadow[]);
void setVirtualShadowMap(const Tempest::StorageImage& pageData,
void setVirtualShadowMap(const Tempest::ZBuffer& pageData,
const Tempest::StorageImage& pageDataCs,
const Tempest::StorageImage& pageTbl,
const Tempest::StorageImage& pageHiZ,
const Tempest::StorageBuffer& pageList);
Expand Down
11 changes: 11 additions & 0 deletions game/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ Resources::Resources(Tempest::Device &device)
fbZero = device.texture(pm);
}

fbImg = device.image2d(TextureFormat::R32U,1,1);
fbImg3d = device.image3d(TextureFormat::R32U,1,1,1);

// Set up the DirectMusic loader
DmResult rv = DmLoader_create(&dmLoader, DmLoader_DOWNLOAD);
if(rv != DmResult_SUCCESS) {
Expand Down Expand Up @@ -283,6 +286,14 @@ const Texture2d &Resources::fallbackBlack() {
return inst->fbZero;
}

const Tempest::StorageImage& Resources::fallbackImage() {
return inst->fbImg;
}

const Tempest::StorageImage& Resources::fallbackImage3d() {
return inst->fbImg3d;
}

const zenkit::Vfs& Resources::vdfsIndex() {
return inst->gothicAssets;
}
Expand Down
3 changes: 3 additions & 0 deletions game/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class Resources final {

static const Tempest::Texture2d& fallbackTexture();
static const Tempest::Texture2d& fallbackBlack();
static auto fallbackImage() -> const Tempest::StorageImage&;
static auto fallbackImage3d() -> const Tempest::StorageImage&;
static const Tempest::Texture2d* loadTexture(std::string_view name);
static const Tempest::Texture2d* loadTexture(Tempest::Color color);
static const Tempest::Texture2d* loadTexture(std::string_view name, int32_t v, int32_t c);
Expand Down Expand Up @@ -190,6 +192,7 @@ class Resources final {
Tempest::VertexBuffer<Vertex> sphere(int passCount, float R);

Tempest::Texture2d fallback, fbZero;
Tempest::StorageImage fbImg, fbImg3d;

using BindK = std::tuple<const Skeleton*,const ProtoMesh*>;
using FontK = std::pair<const std::string,FontType>;
Expand Down
6 changes: 4 additions & 2 deletions shader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ add_shader(clouds_lut.comp sky/clouds_lut.comp)

add_shader(fog_view_lut.comp sky/fog_view_lut.comp -DCOMPUTE)

add_shader(fog3d_hq.frag sky/fog.frag -S frag -DVOLUMETRIC_HQ)
add_shader(fog3d.comp sky/fog.frag -S comp -DVOLUMETRIC_HQ -DCOMPUTE)
add_shader(fog3d_hq.frag sky/fog.frag -S frag -DVOLUMETRIC)
add_shader(fog3d.comp sky/fog.frag -S comp -DVOLUMETRIC -DCOMPUTE)
add_shader(fog3d_vsm.comp sky/fog.frag -S comp -DVOLUMETRIC -DCOMPUTE -DVIRTUAL_SHADOW)

add_shader(fog.frag sky/fog.frag)
add_shader(sky.frag sky/sky.frag)
Expand Down Expand Up @@ -307,6 +308,7 @@ add_shader(vsm_alloc_pages.comp virtual_shadow/vsm_alloc_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_list_pages.comp virtual_shadow/vsm_list_pages.comp)

add_shader(vsm_rendering.comp virtual_shadow/vsm_rendering.comp)

Expand Down
Loading

0 comments on commit 5a031f4

Please sign in to comment.