| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Helpers |
| | 6 | | { |
| | 7 | | public static class SRPBatchingHelper |
| | 8 | | { |
| | 9 | | public static System.Action<Material> OnMaterialProcess; |
| 1 | 10 | | static Dictionary<int, int> crcToQueue = new Dictionary<int, int>(); |
| | 11 | |
|
| | 12 | | public static void OptimizeMaterial(Material material) |
| | 13 | | { |
| | 14 | | //NOTE(Brian): Just enable these keywords so the SRP batcher batches more stuff. |
| 372 | 15 | | material.EnableKeyword("_EMISSION"); |
| 372 | 16 | | material.EnableKeyword("_NORMALMAP"); |
| | 17 | |
|
| 372 | 18 | | if (!material.IsKeywordEnabled("_ALPHATEST_ON") && material.HasProperty(ShaderUtils.Cutoff)) |
| 202 | 19 | | material.SetFloat(ShaderUtils.Cutoff, 0); |
| | 20 | |
|
| 372 | 21 | | material.EnableKeyword("_ALPHATEST_ON"); |
| 372 | 22 | | material.DisableKeyword("_ALPHABLEND_ON"); |
| 372 | 23 | | material.DisableKeyword("_ENVIRONMENTREFLECTIONS_OFF"); |
| 372 | 24 | | material.DisableKeyword("_SPECULARHIGHLIGHTS_OFF"); |
| 372 | 25 | | material.DisableKeyword("VERTEX_COLOR_ON"); |
| | 26 | |
|
| 372 | 27 | | material.enableInstancing = false; |
| | 28 | |
|
| 372 | 29 | | if (material.HasProperty(ShaderUtils.ZWrite)) |
| | 30 | | { |
| 361 | 31 | | int zWrite = (int) material.GetFloat(ShaderUtils.ZWrite); |
| | 32 | |
|
| | 33 | | //NOTE(Brian): for transparent meshes skip further variant optimization. |
| | 34 | | // Transparency needs clip space z sorting to be displayed correctly. |
| 361 | 35 | | if (zWrite == 0) |
| | 36 | | { |
| 53 | 37 | | material.SetInt("_Surface", 1); |
| 53 | 38 | | material.DisableKeyword("_SCREEN_SPACE_OCCLUSION"); |
| 53 | 39 | | material.SetShaderPassEnabled("DepthNormals", false); |
| 53 | 40 | | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent; |
| 53 | 41 | | OnMaterialProcess?.Invoke(material); |
| 0 | 42 | | return; |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| 319 | 46 | | material.SetInt("_Surface", 0); |
| | 47 | |
|
| 319 | 48 | | int cullMode = (int) UnityEngine.Rendering.CullMode.Off; |
| | 49 | |
|
| 319 | 50 | | if (material.HasProperty(ShaderUtils.Cull)) |
| | 51 | | { |
| 308 | 52 | | cullMode = 2 - (int) material.GetFloat(ShaderUtils.Cull); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | int baseQueue; |
| | 56 | |
|
| 319 | 57 | | if (material.renderQueue == (int) UnityEngine.Rendering.RenderQueue.AlphaTest) |
| 16 | 58 | | baseQueue = (int) UnityEngine.Rendering.RenderQueue.Geometry + 600; |
| | 59 | | else |
| 303 | 60 | | baseQueue = (int) UnityEngine.Rendering.RenderQueue.Geometry; |
| | 61 | |
|
| | 62 | | //NOTE(Brian): This guarantees grouping calls by same shader keywords. Needed to take advantage of SRP batch |
| 319 | 63 | | string appendedKeywords = material.shader.name + string.Join("", material.shaderKeywords); |
| 319 | 64 | | int crc = Shader.PropertyToID(appendedKeywords); |
| | 65 | |
|
| 319 | 66 | | if (!crcToQueue.ContainsKey(crc)) |
| 5 | 67 | | crcToQueue.Add(crc, crcToQueue.Count + 1); |
| | 68 | |
|
| | 69 | | //NOTE(Brian): we use 0, 100, 200 to group calls by culling mode (must group them or batches will break). |
| 319 | 70 | | int queueOffset = (cullMode + 1) * 150; |
| 319 | 71 | | material.renderQueue = baseQueue + crcToQueue[crc] + queueOffset; |
| 319 | 72 | | OnMaterialProcess?.Invoke(material); |
| 0 | 73 | | } |
| | 74 | | } |
| | 75 | | } |