From 6b652d29e9960e4325c0b64f9421c748d43b7c5e Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Mon, 23 Nov 2026 02:09:36 +0000 Subject: [PATCH 1/2] perf: the world and its VOBs are SRP batched - Lit/World's _FocusBrightness was outside UnityPerMaterial and ~1900 VOBs had a MaterialPropertyBlock Co-Authored-By: Claude Opus 5.5 --- .../Services/World/StationaryLightsService.cs | 12 +++++++----- Assets/Gothic-Core/Shaders/Lit-World.shader | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs b/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs index c3d4fcc2b..ea89703d8 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs @@ -307,7 +307,6 @@ public void ClearWorldChunks() public bool IsWorldLit(Renderer renderer) => _worldLitRenderers.Contains(renderer); // Lightmap light per cell (ToCell) of the light mapped world polygons, sampled at their centers by WorldMeshBuilder. private readonly Dictionary _lightmapCells = new(); - private MaterialPropertyBlock _vobLightBlock; /// /// The lightmap light of the world's light mapped polygons per cell (rgb summed, w = polygon count). @@ -378,15 +377,18 @@ public void ApplyWorldLight(GameObject vobGo) if (!light.HasValue) return; - _vobLightBlock ??= new MaterialPropertyBlock(); var tint = new Vector4(light.Value.r, light.Value.g, light.Value.b, 1f); foreach (var renderer in renderers) { if (renderer is MeshRenderer meshRenderer && IsWorldChunk(meshRenderer)) continue; - renderer.GetPropertyBlock(_vobLightBlock); - _vobLightBlock.SetVector(_vobLightTintShaderId, tint); - renderer.SetPropertyBlock(_vobLightBlock); + // Into the renderer's own materials (each has its own - UpdateRenderer writes its light indices there + // too), not a MaterialPropertyBlock: the SRP batcher skips renderers with one, ~1900 VOBs in Khorinis + // were drawn one by one. + renderer.GetSharedMaterials(_rendererMaterials); + foreach (var material in _rendererMaterials) + if (material != null) + material.SetVector(_vobLightTintShaderId, tint); _worldLitRenderers.Add(renderer); } } diff --git a/Assets/Gothic-Core/Shaders/Lit-World.shader b/Assets/Gothic-Core/Shaders/Lit-World.shader index 1a0f2d4f2..5cdd9b9ec 100644 --- a/Assets/Gothic-Core/Shaders/Lit-World.shader +++ b/Assets/Gothic-Core/Shaders/Lit-World.shader @@ -31,8 +31,6 @@ Shader "Lit/World" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" - float _FocusBrightness; - struct appdata { float4 vertex : POSITION; @@ -68,6 +66,8 @@ Shader "Lit/World" SAMPLER(sampler_WorldLightmaps); float _WorldLightmapBrightness; + // Every material property in here, the same in every pass - else the SRP batcher skips the world and + // draws it chunk by chunk. CBUFFER_START(UnityPerMaterial) float4 _MainTex_TexelSize; int _StationaryLightCount; @@ -75,6 +75,7 @@ Shader "Lit/World" float4x4 _StationaryLightIndices2; float _WindStrength; float4 _VobLightTint; + float _FocusBrightness; CBUFFER_END #include "GothicIncludes.hlsl" From fdfd9cad578d017726f906764d78a70edddc58c8 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Tue, 24 Nov 2026 17:44:12 +0000 Subject: [PATCH 2/2] fix: a focused chest or door in a cave keeps its dark world light; X marks get their own material (review #558) - DynamicMaterialService copies the original material for the highlight, so _VobLightTint and the lamp indices stay - StationaryLightsService writes per-renderer values only into owned materials (never the shared LoadingMaterial) - untextured color materials are per renderer (texture cached per color), so one VOB's lights no longer light all - architecture.md P4 and world-lighting.md: SRP batcher rule, no MaterialPropertyBlock on world/VOB renderers Co-Authored-By: Claude Opus 5.5 --- .../Meshes/Builder/AbstractMeshBuilder.cs | 22 +++++++-------- .../Services/Caches/MultiTypeCacheService.cs | 8 +++--- .../Services/Meshes/DynamicMaterialService.cs | 13 ++++----- .../Services/World/StationaryLightsService.cs | 28 +++++++++++++++---- Docs/architecture.md | 6 ++-- Docs/world-lighting.md | 1 + 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs b/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs index d5ebc33d3..726e1d1f2 100644 --- a/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs +++ b/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/AbstractMeshBuilder.cs @@ -856,22 +856,22 @@ private Material GetDefaultMaterial(TextureCacheService.TextureArrayTypes textur /// /// The SingleMesh shader only samples _MainTex. A 1x1 texture with the material's color gives the same look. - /// Cached per color, as every VOB using it shares the very same material. + /// The texture is cached per color; the material is per renderer, as the light services write each renderer's + /// lights and world-light tint into it (a shared one lit every X mark like the last one). Same shader variant, + /// so they still batch. /// private Material GetUntexturedMaterial(System.Drawing.Color color) { var unityColor = new Color32(color.R, color.G, color.B, color.A); - if (_multiTypeCacheService.UntexturedMaterials.TryGetValue(unityColor, out var material)) - return material; - - var texture = new Texture2D(1, 1); - texture.SetPixel(0, 0, unityColor); - texture.Apply(); - - material = new Material(Constants.ShaderSingleMeshLit) { mainTexture = texture }; - _multiTypeCacheService.UntexturedMaterials[unityColor] = material; + if (!_multiTypeCacheService.UntexturedTextures.TryGetValue(unityColor, out var texture)) + { + texture = new Texture2D(1, 1); + texture.SetPixel(0, 0, unityColor); + texture.Apply(); + _multiTypeCacheService.UntexturedTextures[unityColor] = texture; + } - return material; + return new Material(Constants.ShaderSingleMeshLit) { mainTexture = texture }; } protected Material GetWaterMaterial() diff --git a/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs b/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs index 33439e93a..7ce2685a7 100644 --- a/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Caches/MultiTypeCacheService.cs @@ -39,10 +39,10 @@ public class MultiTypeCacheService public Dictionary AudioClips = new(); /// - /// Materials of texture-less, color-only meshes (e.g. G2's treasure X marks) - shared by all VOBs with the same - /// color. + /// 1x1 textures of texture-less, color-only meshes (e.g. G2's treasure X marks) - shared by all VOBs with the + /// same color. Each renderer gets its own material with it: per-renderer light values are written there. /// - public Dictionary UntexturedMaterials = new(); + public Dictionary UntexturedTextures = new(); /// /// Colliders of items without pre-cached ones, calculated from their mesh once per mesh name @@ -65,7 +65,7 @@ public void Dispose() FontCache.Clear(); Meshes.Clear(); AudioClips.Clear(); - UntexturedMaterials.Clear(); + UntexturedTextures.Clear(); ItemColliders.Clear(); NpcCache.Clear(); VobCache.Clear(); diff --git a/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs b/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs index 91fbaa5d3..6757e737b 100644 --- a/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Meshes/DynamicMaterialService.cs @@ -40,7 +40,6 @@ private class CacheEntry { Constants.ShaderLitAlphaToCoverageName, new (Constants.ShaderLitAlphaToCoverageDynamic, (int)RenderQueue.AlphaTest) } }; - private static readonly int _fatnessShaderId = Shader.PropertyToID("_Fatness"); // Some objects (like NPCs) have multiple meshes. We therefore add all self+children renderers/materials. private Dictionary _cache = new(); @@ -109,7 +108,8 @@ private static bool IsOutdated(GameObject go, CacheEntry entry) } /// - /// Only our own dynamic material copies are changed - the default materials are shared with other objects. + /// Only our own dynamic material copies are changed - the default materials stay as they are (some are + /// shared with other objects, e.g. Constants.LoadingMaterial; VOB world materials are per renderer). /// private static void SetValue(CacheEntry entry, int shaderProperty, float shaderValue) { @@ -147,14 +147,13 @@ private void CacheGameObject(GameObject go) continue; } - var newMaterial = new Material(shaderMapEntry.dynamicShader) + // A copy of the original keeps every per-renderer value (texture, fatness, the world-light tint + // _VobLightTint, the stationary light indices): a chest in the Old Mine stays dark while focused. + var newMaterial = new Material(mat) { - mainTexture = mat.mainTexture, + shader = shaderMapEntry.dynamicShader, renderQueue = shaderMapEntry.shaderType }; - // A fat NPC stays fat while it is highlighted or see-through. - if (mat.HasProperty(_fatnessShaderId) && newMaterial.HasProperty(_fatnessShaderId)) - newMaterial.SetFloat(_fatnessShaderId, mat.GetFloat(_fatnessShaderId)); dynamicSlots[i] = newMaterial; } diff --git a/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs b/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs index ea89703d8..6a3aa2d18 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/StationaryLightsService.cs @@ -161,7 +161,7 @@ private void UpdateRenderer(MeshRenderer renderer) var nonAllocMaterials = _rendererMaterials; var indicesMatrix = Matrix4x4.identity; - renderer.GetSharedMaterials(nonAllocMaterials); + GetOwnedMaterials(renderer, nonAllocMaterials); for (var i = 0; i < Mathf.Min(16, rendererLights.Count); i++) { indicesMatrix[i / 4, i % 4] = rendererLights[i].Index; @@ -382,17 +382,33 @@ public void ApplyWorldLight(GameObject vobGo) { if (renderer is MeshRenderer meshRenderer && IsWorldChunk(meshRenderer)) continue; - // Into the renderer's own materials (each has its own - UpdateRenderer writes its light indices there - // too), not a MaterialPropertyBlock: the SRP batcher skips renderers with one, ~1900 VOBs in Khorinis - // were drawn one by one. - renderer.GetSharedMaterials(_rendererMaterials); + // Into the renderer's own materials (UpdateRenderer writes its light indices there too), not a + // MaterialPropertyBlock: the SRP batcher skips renderers with one, ~1900 VOBs in Khorinis were drawn + // one by one. + GetOwnedMaterials(renderer, _rendererMaterials); foreach (var material in _rendererMaterials) - if (material != null) + if (material.HasProperty(_vobLightTintShaderId)) material.SetVector(_vobLightTintShaderId, tint); _worldLitRenderers.Add(renderer); } } + /// + /// The renderer's materials this service writes per-renderer values into (light indices, world-light tint). + /// Every VOB and chunk renderer has its own (mesh builders); the loading material is the one shared exception, + /// left in place when a mesh failed to build - writing there would light every renderer still using it. + /// + private static void GetOwnedMaterials(Renderer renderer, List materials) + { + renderer.GetSharedMaterials(materials); + // Backwards and without a lambda: called for hundreds of renderers at once (GC spikes on Quest). + for (var i = materials.Count - 1; i >= 0; i--) + { + if (materials[i] == null || materials[i] == Constants.LoadingMaterial) + materials.RemoveAt(i); + } + } + // Diagnostics: how many VOBs got the world light, with a few examples (VOBs in the Old Mine stayed bright). private int _vobLightApplied; private int _vobLightMissed; diff --git a/Docs/architecture.md b/Docs/architecture.md index a63b9ccae..a2e44992d 100644 --- a/Docs/architecture.md +++ b/Docs/architecture.md @@ -376,8 +376,10 @@ player's view, which in VR causes nausea. ### P4. Rendering has a budget **Rule:** A feature that adds draw calls, materials, realtime lights, shadows, transparency, post-processing or -shader variants says what it costs on Quest in its PR, and has a quality setting if it's optional. Meshes share -materials and texture arrays so they batch. +shader variants says what it costs on Quest in its PR, and has a quality setting if it's optional. World and VOB +shaders stay SRP batcher compatible: every `Properties` entry sits in `UnityPerMaterial`, the same in every pass, and +world/VOB renderers get no `MaterialPropertyBlock`. A per-renderer value goes into the renderer's own material (the +mesh builders give each one its own); meshes share texture arrays. **Why:** On a mobile GPU the frame is usually lost in rendering, not in C#. Each eye renders the scene, so every draw call and every overdrawn pixel counts. diff --git a/Docs/world-lighting.md b/Docs/world-lighting.md index 4e720ef16..ddf768d5a 100644 --- a/Docs/world-lighting.md +++ b/Docs/world-lighting.md @@ -32,6 +32,7 @@ Core code is in `Assets/Gothic-Core/Scripts/`. |------|----------| | A static VOB in a light mapped area takes the light of the floor below it (nearest lightmap cell, searched ring by ring) | `StationaryLightsService.ApplyWorldLight`, called by `VobInitializerDomain` | | A VOB lit this way ignores the baked static lights, else it gets the light twice | `Adapters/Vob/StationaryLight.cs` | +| The tint is `_VobLightTint` in the VOB's own materials, never a MaterialPropertyBlock: VOBs stay SRP batched; the focus highlight copies the material, so the tint stays | `StationaryLightsService.ApplyWorldLight`, `DynamicMaterialService.CacheGameObject` | ## Fireplaces and torches