From 1c25519d999914e2329a9ac5fcf466201fb335e3 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 16 Aug 2026 12:50:44 +0200 Subject: [PATCH] Backport rendering fixes from https://github.com/RomanZhu/FlaxEngine --- Content/Shaders/GI/GlobalSurfaceAtlas.flax | 4 ++-- Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp | 2 +- Source/Shaders/GI/GlobalSurfaceAtlas.shader | 5 +++-- Source/Shaders/GlobalSignDistanceField.hlsl | 12 ++++++------ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Content/Shaders/GI/GlobalSurfaceAtlas.flax b/Content/Shaders/GI/GlobalSurfaceAtlas.flax index 78a60a909..50c7a3de3 100644 --- a/Content/Shaders/GI/GlobalSurfaceAtlas.flax +++ b/Content/Shaders/GI/GlobalSurfaceAtlas.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0b16112fd0efc9ac6b9a29a6b9b7aee4672ae26393900aa3be5a8e53fbc4b67a -size 14393 +oid sha256:786268598e13a6d63a593f028813a207858b8e4f5a5f9cbfc4520f715e17d395 +size 14482 diff --git a/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp b/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp index 9b44b9cc7..b2c4347e0 100644 --- a/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp +++ b/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp @@ -1272,7 +1272,7 @@ bool GlobalSurfaceAtlasPass::Render(RenderContext& renderContext, GPUContext* co // Init constants result.Constants.ViewPos = renderContext.View.Position; result.Constants.Resolution = (float)resolution; - result.Constants.ChunkSize = distance / (float)GLOBAL_SURFACE_ATLAS_CHUNKS_RESOLUTION; + result.Constants.ChunkSize = distance * 2.0f / (float)GLOBAL_SURFACE_ATLAS_CHUNKS_RESOLUTION; result.Constants.ObjectsCount = surfaceAtlasData.Objects.Count(); // If we don't know the culled objects buffer capacity then we shouldn't use atlas results as many objects are still missing (see CulledObjectsCounterIndex usage) diff --git a/Source/Shaders/GI/GlobalSurfaceAtlas.shader b/Source/Shaders/GI/GlobalSurfaceAtlas.shader index fbd0bbd1b..752f51dce 100644 --- a/Source/Shaders/GI/GlobalSurfaceAtlas.shader +++ b/Source/Shaders/GI/GlobalSurfaceAtlas.shader @@ -271,11 +271,12 @@ void CS_CullObjects(uint3 DispatchThreadId : SV_DispatchThreadID, uint3 GroupId } } GroupMemoryBarrierWithGroupSync(); + uint culledObjectsCount = min(SharedCulledObjectsCount, GLOBAL_SURFACE_ATLAS_SHARED_CULL_SIZE); // Cull objects from the shared buffer against active thread's chunk uint objectsCount = 0; LOOP - for (uint i = 0; i < SharedCulledObjectsCount; i++) + for (uint i = 0; i < culledObjectsCount; i++) { uint objectAddress = SharedCulledObjects[i]; float4 objectBounds = LoadGlobalSurfaceAtlasObjectBounds(GlobalSurfaceAtlasObjects, objectAddress); @@ -310,7 +311,7 @@ void CS_CullObjects(uint3 DispatchThreadId : SV_DispatchThreadID, uint3 GroupId // Copy objects data in this chunk (cull from the shared buffer) LOOP - for (uint i = 0; i < SharedCulledObjectsCount; i++) + for (uint i = 0; i < culledObjectsCount; i++) { uint objectAddress = SharedCulledObjects[i]; float4 objectBounds = LoadGlobalSurfaceAtlasObjectBounds(GlobalSurfaceAtlasObjects, objectAddress); diff --git a/Source/Shaders/GlobalSignDistanceField.hlsl b/Source/Shaders/GlobalSignDistanceField.hlsl index 41ce0c00e..6987e10cb 100644 --- a/Source/Shaders/GlobalSignDistanceField.hlsl +++ b/Source/Shaders/GlobalSignDistanceField.hlsl @@ -134,10 +134,10 @@ float SampleGlobalSDF(const GlobalSDFData data, Texture3D tex, floa float voxelSize = data.CascadeVoxelSize[cascade]; float chunkMargin = voxelSize * (GLOBAL_SDF_CHUNK_MARGIN_SCALE * GLOBAL_SDF_RASTERIZE_CHUNK_MARGIN); float maxDistanceTex = data.CascadeMaxDistanceTex[cascade]; - float distanceTex = tex.SampleLevel(GLOBAL_SDF_SAMPLER, textureUV, 0); + float distanceTex = tex.SampleLevel(GLOBAL_SDF_SAMPLER, textureUV, 0) * maxDistanceTex; if (distanceTex < chunkMargin && all(cascadeUV > 0) && all(cascadeUV < 1)) { - distance = distanceTex * maxDistanceTex; + distance = distanceTex; break; } } @@ -159,10 +159,10 @@ float SampleGlobalSDF(const GlobalSDFData data, Texture3D tex, Text float chunkSize = voxelSize * GLOBAL_SDF_RASTERIZE_CHUNK_SIZE; float chunkMargin = voxelSize * (GLOBAL_SDF_CHUNK_MARGIN_SCALE * GLOBAL_SDF_RASTERIZE_CHUNK_MARGIN); float maxDistanceMip = data.CascadeMaxDistanceMip[cascade]; - float distanceMip = mip.SampleLevel(GLOBAL_SDF_SAMPLER, textureMipUV, 0); + float distanceMip = mip.SampleLevel(GLOBAL_SDF_SAMPLER, textureMipUV, 0) * maxDistanceMip; if (distanceMip < chunkSize && all(cascadeUV > 0) && all(cascadeUV < 1)) { - distance = distanceMip * maxDistanceMip; + distance = distanceMip; float maxDistanceTex = data.CascadeMaxDistanceTex[cascade]; float distanceTex = tex.SampleLevel(GLOBAL_SDF_SAMPLER, textureUV, 0) * maxDistanceTex; if (distanceTex < chunkMargin) @@ -188,7 +188,7 @@ float3 SampleGlobalSDFGradient(const GlobalSDFData data, Texture3D float voxelSize = data.CascadeVoxelSize[cascade]; float chunkMargin = voxelSize * (GLOBAL_SDF_CHUNK_MARGIN_SCALE * GLOBAL_SDF_RASTERIZE_CHUNK_MARGIN); float maxDistanceTex = data.CascadeMaxDistanceTex[cascade]; - float distanceTex = tex.SampleLevel(GLOBAL_SDF_SAMPLER, textureUV, 0); + float distanceTex = tex.SampleLevel(GLOBAL_SDF_SAMPLER, textureUV, 0) * maxDistanceTex; if (distanceTex < chunkMargin && all(cascadeUV > 0) && all(cascadeUV < 1)) { float texelOffset = 1.0f / data.Resolution; @@ -200,7 +200,7 @@ float3 SampleGlobalSDFGradient(const GlobalSDFData data, Texture3D float zp = tex.SampleLevel(GLOBAL_SDF_SAMPLER, float3(textureUV.x, textureUV.y, textureUV.z + texelOffset), 0).x; float zn = tex.SampleLevel(GLOBAL_SDF_SAMPLER, float3(textureUV.x, textureUV.y, textureUV.z - texelOffset), 0).x; gradient = float3(xp - xn, yp - yn, zp - zn) * maxDistanceTex; - distance = distanceTex * maxDistanceTex; + distance = distanceTex; break; } }