From 8b2404cceccc5f1caf120280ff378b457a13fd64 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Sun, 28 Jan 2024 00:05:00 +0100 Subject: [PATCH] volumetric: options should be forwarded properly --- data/base/shaders/vk/tcmask_instanced.frag | 14 +++++++------- data/base/shaders/vk/terrain_combined_high.frag | 13 ++++++------- lib/ivis_opengl/gfx_api.h | 4 +++- lib/ivis_opengl/gfx_api_vk.cpp | 12 +++++++++--- lib/ivis_opengl/gfx_api_vk.h | 1 + lib/ivis_opengl/piestate.cpp | 3 ++- lib/ivis_opengl/piestate.h | 2 +- src/main.cpp | 2 +- 8 files changed, 30 insertions(+), 21 deletions(-) diff --git a/data/base/shaders/vk/tcmask_instanced.frag b/data/base/shaders/vk/tcmask_instanced.frag index cca5a9c8c75..9515640c0a6 100644 --- a/data/base/shaders/vk/tcmask_instanced.frag +++ b/data/base/shaders/vk/tcmask_instanced.frag @@ -8,6 +8,7 @@ layout (constant_id = 1) const uint WZ_SHADOW_MODE = 1; layout (constant_id = 2) const uint WZ_SHADOW_FILTER_SIZE = 5; layout (constant_id = 3) const uint WZ_SHADOW_CASCADES_COUNT = 3; layout (constant_id = 4) const uint WZ_POINT_LIGHT_ENABLED = 0; +layout (constant_id = 5) const uint WZ_VOLUMETRIC_LIGHTING_ENABLED = 0; layout(set = 2, binding = 0) uniform sampler2D Texture; // diffuse layout(set = 2, binding = 1) uniform sampler2D TextureTcmask; // tcmask @@ -375,12 +376,12 @@ void main() fragColour.a = 0.66 + 0.66 * graphicsCycle; } -#if 1 - vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight); - vec4 volumetric = volumetricLights(clipSpaceCoord, cameraPos.xyz, fragPos, diffuse.xyz); - fragColour.xyz = toneMap(fragColour.xyz * volumetric.a + volumetric.xyz); -#else - if (fogEnabled > 0) + if (WZ_VOLUMETRIC_LIGHTING_ENABLED == 1) { + vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight); + vec4 volumetric = volumetricLights(clipSpaceCoord, cameraPos.xyz, fragPos, diffuse.xyz); + fragColour.xyz = toneMap(fragColour.xyz * volumetric.a + volumetric.xyz); + } + else if (fogEnabled > 0) { // Calculate linear fog float fogFactor = (fogEnd - vertexDistance) / (fogEnd - fogStart); @@ -389,7 +390,6 @@ void main() // Return fragment color fragColour = mix(fragColour, vec4(fogColor.xyz, fragColour.w), fogFactor); } -#endif FragColor = fragColour; } diff --git a/data/base/shaders/vk/terrain_combined_high.frag b/data/base/shaders/vk/terrain_combined_high.frag index 7879cfd3b41..8c1a0211fe8 100644 --- a/data/base/shaders/vk/terrain_combined_high.frag +++ b/data/base/shaders/vk/terrain_combined_high.frag @@ -7,6 +7,7 @@ layout (constant_id = 1) const uint WZ_SHADOW_MODE = 1; layout (constant_id = 2) const uint WZ_SHADOW_FILTER_SIZE = 5; layout (constant_id = 3) const uint WZ_SHADOW_CASCADES_COUNT = 3; layout (constant_id = 4) const uint WZ_POINT_LIGHT_ENABLED = 0; +layout (constant_id = 5) const uint WZ_VOLUMETRIC_LIGHTING_ENABLED = 0; layout(set = 1, binding = 0) uniform sampler2D lightmap_tex; @@ -125,12 +126,11 @@ void main() { vec4 fragColor = main_bumpMapping(); -#if 1 - - vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight); - vec4 volumetric = volumetricLights(clipSpaceCoord, cameraPos.xyz, frag.fragPos, diffuseLight.xyz); - fragColor.xyz = toneMap(fragColor.xyz * volumetric.a + volumetric.xyz); -#else + if (WZ_VOLUMETRIC_LIGHTING_ENABLED == 1) { + vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight); + vec4 volumetric = volumetricLights(clipSpaceCoord, cameraPos.xyz, frag.fragPos, diffuseLight.xyz); + fragColor.xyz = toneMap(fragColor.xyz * volumetric.a + volumetric.xyz); + } if (fogEnabled > 0) { // Calculate linear fog @@ -140,6 +140,5 @@ void main() // Return fragment color fragColor = mix(fragColor, vec4(fogColor.xyz, fragColor.w), fogFactor); } -#endif FragColor = fragColor; } diff --git a/lib/ivis_opengl/gfx_api.h b/lib/ivis_opengl/gfx_api.h index 0be8233e5a3..3a74969c485 100644 --- a/lib/ivis_opengl/gfx_api.h +++ b/lib/ivis_opengl/gfx_api.h @@ -326,13 +326,15 @@ namespace gfx_api uint32_t shadowFilterSize = 5; uint32_t shadowCascadesCount = WZ_MAX_SHADOW_CASCADES; bool isPointLightPerPixelEnabled = false; + bool isVolumetricLightingEnabled = false; bool operator==(const lighting_constants& rhs) const { return shadowMode == rhs.shadowMode && shadowFilterSize == rhs.shadowFilterSize && shadowCascadesCount == rhs.shadowCascadesCount - && isPointLightPerPixelEnabled == rhs.isPointLightPerPixelEnabled; + && isPointLightPerPixelEnabled == rhs.isPointLightPerPixelEnabled + && isVolumetricLightingEnabled == rhs.isVolumetricLightingEnabled; } }; diff --git a/lib/ivis_opengl/gfx_api_vk.cpp b/lib/ivis_opengl/gfx_api_vk.cpp index bdcaadb16b9..cea3a54dd0e 100644 --- a/lib/ivis_opengl/gfx_api_vk.cpp +++ b/lib/ivis_opengl/gfx_api_vk.cpp @@ -1147,12 +1147,13 @@ struct shader_infos bool specializationConstant_2_shadowFilterSize = false; bool specializationConstant_3_shadowCascadesCount = false; bool specializationConstant_4_pointLightEnabled = false; + bool specializationConstant_5_volumetricEnabled = false; }; static const std::map spv_files { std::make_pair(SHADER_COMPONENT, shader_infos{ "shaders/vk/tcmask.vert.spv", "shaders/vk/tcmask.frag.spv", true }), - std::make_pair(SHADER_COMPONENT_INSTANCED, shader_infos{ "shaders/vk/tcmask_instanced.vert.spv", "shaders/vk/tcmask_instanced.frag.spv", true, true, true, true, true }), + std::make_pair(SHADER_COMPONENT_INSTANCED, shader_infos{ "shaders/vk/tcmask_instanced.vert.spv", "shaders/vk/tcmask_instanced.frag.spv", true, true, true, true, true, true }), std::make_pair(SHADER_COMPONENT_DEPTH_INSTANCED, shader_infos{ "shaders/vk/tcmask_depth_instanced.vert.spv", "shaders/vk/tcmask_depth_instanced.frag.spv" }), std::make_pair(SHADER_NOLIGHT, shader_infos{ "shaders/vk/nolight.vert.spv", "shaders/vk/nolight.frag.spv", true }), std::make_pair(SHADER_NOLIGHT_INSTANCED, shader_infos{ "shaders/vk/nolight_instanced.vert.spv", "shaders/vk/nolight_instanced.frag.spv", true }), @@ -1162,7 +1163,7 @@ static const std::map spv_files std::make_pair(SHADER_DECALS, shader_infos{ "shaders/vk/decals.vert.spv", "shaders/vk/decals.frag.spv", true }), std::make_pair(SHADER_TERRAIN_COMBINED_CLASSIC, shader_infos{ "shaders/vk/terrain_combined.vert.spv", "shaders/vk/terrain_combined_classic.frag.spv", true, true, true, true }), std::make_pair(SHADER_TERRAIN_COMBINED_MEDIUM, shader_infos{ "shaders/vk/terrain_combined.vert.spv", "shaders/vk/terrain_combined_medium.frag.spv", true, true, true, true }), - std::make_pair(SHADER_TERRAIN_COMBINED_HIGH, shader_infos{ "shaders/vk/terrain_combined.vert.spv", "shaders/vk/terrain_combined_high.frag.spv", true, true, true, true, true }), + std::make_pair(SHADER_TERRAIN_COMBINED_HIGH, shader_infos{ "shaders/vk/terrain_combined.vert.spv", "shaders/vk/terrain_combined_high.frag.spv", true, true, true, true, true, true }), std::make_pair(SHADER_WATER, shader_infos{ "shaders/vk/terrain_water.vert.spv", "shaders/vk/water.frag.spv", true }), std::make_pair(SHADER_WATER_HIGH, shader_infos{ "shaders/vk/terrain_water_high.vert.spv", "shaders/vk/terrain_water_high.frag.spv", true }), std::make_pair(SHADER_WATER_CLASSIC, shader_infos{ "shaders/vk/terrain_water_classic.vert.spv", "shaders/vk/terrain_water_classic.frag.spv", true }), @@ -1773,6 +1774,11 @@ VkPSO::VkPSO(vk::Device _dev, appendSpecializationConstant_uint32(4, static_cast(root->shadowConstants.isPointLightPerPixelEnabled)); hasSpecializationConstant_PointLightConstants = true; } + if (shaderInfo.specializationConstant_4_pointLightEnabled) + { + appendSpecializationConstant_uint32(5, static_cast(root->shadowConstants.isVolumetricLightingEnabled)); + hasSpecializationConstant_VolumetricLightingConstants = true; + } if (!specializationEntries.empty()) { ASSERT(pipelineStages[1].pSpecializationInfo == nullptr, "get_stages unexpectedly set pSpecializationInfo - this will overwrite!"); @@ -6174,7 +6180,7 @@ bool VkRoot::setShadowConstants(gfx_api::lighting_constants newValues) auto& renderPass = renderPasses[renderPassId]; ASSERT(pipeline->renderpass_compat, "Pipeline has no associated renderpass compat structure"); - if (pipeline->hasSpecializationConstant_ShadowConstants || pipeline->hasSpecializationConstant_PointLightConstants) + if (pipeline->hasSpecializationConstant_ShadowConstants || pipeline->hasSpecializationConstant_PointLightConstants || pipeline->hasSpecializationConstant_VolumetricLightingConstants) { buffering_mechanism::get_current_resources().pso_to_delete.emplace_back(pipeline); pipelineInfo.renderPassPSO[renderPassId] = new VkPSO(dev, physDeviceProps.limits, pipelineInfo.createInfo, renderPass.rp, renderPass.rp_compat_info, renderPass.msaaSamples, vkDynLoader, *this); diff --git a/lib/ivis_opengl/gfx_api_vk.h b/lib/ivis_opengl/gfx_api_vk.h index 323990caae0..d174cb91c49 100644 --- a/lib/ivis_opengl/gfx_api_vk.h +++ b/lib/ivis_opengl/gfx_api_vk.h @@ -375,6 +375,7 @@ struct VkPSO final std::shared_ptr renderpass_compat; bool hasSpecializationConstant_ShadowConstants = false; bool hasSpecializationConstant_PointLightConstants = false; + bool hasSpecializationConstant_VolumetricLightingConstants = false; private: // Read shader into text buffer diff --git a/lib/ivis_opengl/piestate.cpp b/lib/ivis_opengl/piestate.cpp index a0b21c034e6..2cea47ea60d 100644 --- a/lib/ivis_opengl/piestate.cpp +++ b/lib/ivis_opengl/piestate.cpp @@ -119,7 +119,7 @@ void pie_FreeShaders() //static float fogEnd; // Run from screen.c on init. -bool pie_LoadShaders(uint32_t shadowFilterSize, bool pointLightEnabled) +bool pie_LoadShaders(uint32_t shadowFilterSize, bool pointLightEnabled, bool volumetricEnabled) { // note: actual loading of shaders now occurs in gfx_api @@ -128,6 +128,7 @@ bool pie_LoadShaders(uint32_t shadowFilterSize, bool pointLightEnabled) auto shadowConstants = gfx_api::context::get().getShadowConstants(); shadowConstants.shadowFilterSize = shadowFilterSize; shadowConstants.isPointLightPerPixelEnabled = pointLightEnabled; + shadowConstants.isVolumetricLightingEnabled = volumetricEnabled; gfx_api::context::get().setShadowConstants(shadowConstants); if (!pie_supportsShadowMapping().value_or(false)) diff --git a/lib/ivis_opengl/piestate.h b/lib/ivis_opengl/piestate.h index 8072477ec6d..42c956525d1 100644 --- a/lib/ivis_opengl/piestate.h +++ b/lib/ivis_opengl/piestate.h @@ -82,7 +82,7 @@ const Vector3f& getDefaultSunPosition(); int pie_GetMaxAntialiasing(); -bool pie_LoadShaders(uint32_t shadowFilterSize, bool pointLightEnabled); +bool pie_LoadShaders(uint32_t shadowFilterSize, bool pointLightEnabled, bool volumetricEnabled); void pie_FreeShaders(); namespace pie_internal diff --git a/src/main.cpp b/src/main.cpp index 1d78634f109..d364fbf905f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2128,7 +2128,7 @@ int realmain(int argc, char *argv[]) { return EXIT_FAILURE; } - if (!pie_LoadShaders(war_getShadowFilterSize(), war_getPointLightPerPixelLighting() && getTerrainShaderQuality() == TerrainShaderQuality::NORMAL_MAPPING)) + if (!pie_LoadShaders(war_getShadowFilterSize(), war_getPointLightPerPixelLighting() && getTerrainShaderQuality() == TerrainShaderQuality::NORMAL_MAPPING, war_getVolumetricLighting())) { return EXIT_FAILURE; }