From 790b8d2af49044cd1663f07300e9c0900e8da824 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Sun, 14 Jan 2024 16:10:08 +0100 Subject: [PATCH] volumetric: make it works for tcinstanced objects --- data/base/shaders/vk/tcmask_instanced.frag | 17 ++++++++++----- data/base/shaders/vk/tcmask_instanced.glsl | 1 + lib/ivis_opengl/gfx_api.h | 1 + lib/ivis_opengl/piedraw.cpp | 24 ++++++++++++++-------- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/data/base/shaders/vk/tcmask_instanced.frag b/data/base/shaders/vk/tcmask_instanced.frag index 937e3f670c0..02397444573 100644 --- a/data/base/shaders/vk/tcmask_instanced.frag +++ b/data/base/shaders/vk/tcmask_instanced.frag @@ -31,15 +31,13 @@ layout(location = 12) in vec3 fragPos; layout(location = 0) out vec4 FragColor; -#include "pointlights.glsl" - float getShadowMapDepthComp(vec2 base_uv, float u, float v, vec2 shadowMapSizeInv, int cascadeIndex, float z) { vec2 uv = base_uv + vec2(u, v) * shadowMapSizeInv; return texture( shadowMap, vec4(uv, cascadeIndex, z) ); } -float getShadowVisibility() +float getShadowVisibility(vec3 fragPos) { if (WZ_SHADOW_MODE == 0 || WZ_SHADOW_FILTER_SIZE == 0) { @@ -264,6 +262,9 @@ float getShadowVisibility() } } + +#include "pointlights.glsl" + vec3 blendAddEffectLighting(vec3 a, vec3 b) { return min(a + b, vec3(1.0)); } @@ -305,7 +306,7 @@ void main() vec4 light = sceneColor; vec3 L = normalize(lightDir); float lambertTerm = max(dot(N, L), 0.0); //diffuse light - float visibility = getShadowVisibility(); + float visibility = getShadowVisibility(fragPos); vec4 lightmap_vec4 = texture(lightmap_tex, uvLightmap.xy, 0.f); float distanceAboveTerrain = uvLightmap.z; float lightmapFactor = 1.0f - (clamp(distanceAboveTerrain, 0.f, 300.f) / 300.f); @@ -371,7 +372,12 @@ void main() { fragColour.a = 0.66 + 0.66 * graphicsCycle; } - + +#if 1 + vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight); + vec4 volumetric = volumetricIterateOverAllPointLights(clipSpaceCoord, cameraPos.xyz, fragPos); + fragColour.xyz = toneMap(fragColour.xyz * volumetric.a + volumetric.xyz); +#else if (fogEnabled > 0) { // Calculate linear fog @@ -381,6 +387,7 @@ void main() // Return fragment color fragColour = mix(fragColour, vec4(fogColor.xyz, fragColour.w), fogFactor); } +#endif FragColor = fragColour; } diff --git a/data/base/shaders/vk/tcmask_instanced.glsl b/data/base/shaders/vk/tcmask_instanced.glsl index aa40eb921e5..4973d87b040 100644 --- a/data/base/shaders/vk/tcmask_instanced.glsl +++ b/data/base/shaders/vk/tcmask_instanced.glsl @@ -31,6 +31,7 @@ layout(std140, set = 0, binding = 0) uniform globaluniforms vec4 PointLightsColorAndEnergy[WZ_MAX_POINT_LIGHTS]; ivec4 bucketOffsetAndSize[WZ_BUCKET_DIMENSION * WZ_BUCKET_DIMENSION]; ivec4 PointLightsIndex[WZ_MAX_INDEXED_POINT_LIGHTS]; + vec4 cameraPos; // in modelSpace }; layout(std140, set = 1, binding = 0) uniform meshuniforms diff --git a/lib/ivis_opengl/gfx_api.h b/lib/ivis_opengl/gfx_api.h index d4de8c4be13..8ada2536c72 100644 --- a/lib/ivis_opengl/gfx_api.h +++ b/lib/ivis_opengl/gfx_api.h @@ -764,6 +764,7 @@ namespace gfx_api std::array PointLightsColorAndEnergy; std::array bucketOffsetAndSize; std::array indexed_lights; + glm::vec4 cameraPos; }; // Only change per mesh diff --git a/lib/ivis_opengl/piedraw.cpp b/lib/ivis_opengl/piedraw.cpp index aa33a00bf77..62222eb609f 100644 --- a/lib/ivis_opengl/piedraw.cpp +++ b/lib/ivis_opengl/piedraw.cpp @@ -1383,29 +1383,35 @@ bool InstancedMeshRenderer::DrawAll(uint64_t currentGameFrame, const glm::mat4& glm::vec4 specular(lighting0[LIGHT_SPECULAR][0], lighting0[LIGHT_SPECULAR][1], lighting0[LIGHT_SPECULAR][2], lighting0[LIGHT_SPECULAR][3]); const auto &renderState = getCurrentRenderState(); - const glm::vec4 fogColor = renderState.fogEnabled ? glm::vec4( - renderState.fogColour.vector[0] / 255.f, - renderState.fogColour.vector[1] / 255.f, - renderState.fogColour.vector[2] / 255.f, - renderState.fogColour.vector[3] / 255.f - ) : glm::vec4(0.f); - if (useInstancedRendering) { - + const glm::vec3 cameraPos = (glm::inverse(viewMatrix) * glm::vec4(0, 0, 0, 1)).xyz(); + const glm::vec4 fogColor = glm::vec4( + renderState.fogColour.vector[0] / 255.f, + renderState.fogColour.vector[1] / 255.f, + renderState.fogColour.vector[2] / 255.f, + renderState.fogColour.vector[3] / 255.f + ); auto bucketLight = getCurrentLightingManager().getPointLightBuckets(); auto dimension = gfx_api::context::get().getDrawableDimensions(); gfx_api::Draw3DShapeInstancedGlobalUniforms globalUniforms { projectionMatrix, viewMatrix, modelUVLightmapMatrix, {shadowCascades.shadowMVPMatrix[0], shadowCascades.shadowMVPMatrix[1], shadowCascades.shadowMVPMatrix[2]}, glm::vec4(currentSunPosition, 0.f), sceneColor, ambient, diffuse, specular, fogColor, {shadowCascades.shadowCascadeSplit[0], shadowCascades.shadowCascadeSplit[1], shadowCascades.shadowCascadeSplit[2], pie_getPerspectiveZFar()}, shadowCascades.shadowMapSize, - renderState.fogBegin, renderState.fogEnd, pie_GetShaderTime(), renderState.fogEnabled, static_cast(dimension.first), static_cast(dimension.second), 0.f, bucketLight.positions, bucketLight.colorAndEnergy, bucketLight.bucketOffsetAndSize, bucketLight.light_index + renderState.fogBegin, renderState.fogEnd, pie_GetShaderTime(), renderState.fogEnabled, static_cast(dimension.first), static_cast(dimension.second), 0.f, bucketLight.positions, bucketLight.colorAndEnergy, bucketLight.bucketOffsetAndSize, bucketLight.light_index, + glm::vec4(cameraPos, 0.f) }; Draw3DShapes_Instanced(currentGameFrame, perFrameUniformsShaderOnce, globalUniforms, drawParts, depthPass); } else { + const glm::vec4 fogColor = renderState.fogEnabled ? glm::vec4( + renderState.fogColour.vector[0] / 255.f, + renderState.fogColour.vector[1] / 255.f, + renderState.fogColour.vector[2] / 255.f, + renderState.fogColour.vector[3] / 255.f + ) : glm::vec4(0.f); gfx_api::Draw3DShapeGlobalUniforms globalUniforms { projectionMatrix, viewMatrix, shadowCascades.shadowMVPMatrix[0], glm::vec4(currentSunPosition, 0.f), sceneColor, ambient, diffuse, specular, fogColor,