< Summary

Class:DCL.Helpers.SRPBatchingHelper
Assembly:SRPBatchingHelper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MaterialHelpers/SRPBatchingHelper/SRPBatchingHelper.cs
Covered lines:33
Uncovered lines:2
Coverable lines:35
Total lines:75
Line coverage:94.2% (33 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SRPBatchingHelper()0%110100%
OptimizeMaterial(...)0%10.0210094.12%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MaterialHelpers/SRPBatchingHelper/SRPBatchingHelper.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using UnityEngine;
 4
 5namespace DCL.Helpers
 6{
 7    public static class SRPBatchingHelper
 8    {
 9        public static System.Action<Material> OnMaterialProcess;
 110        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.
 33115            material.EnableKeyword("_EMISSION");
 33116            material.EnableKeyword("_NORMALMAP");
 17
 33118            if (!material.IsKeywordEnabled("_ALPHATEST_ON") && material.HasProperty(ShaderUtils.Cutoff))
 18719                material.SetFloat(ShaderUtils.Cutoff, 0);
 20
 33121            material.EnableKeyword("_ALPHATEST_ON");
 33122            material.DisableKeyword("_ALPHABLEND_ON");
 33123            material.DisableKeyword("_ENVIRONMENTREFLECTIONS_OFF");
 33124            material.DisableKeyword("_SPECULARHIGHLIGHTS_OFF");
 33125            material.DisableKeyword("VERTEX_COLOR_ON");
 26
 33127            material.enableInstancing = false;
 28
 33129            if (material.HasProperty(ShaderUtils.ZWrite))
 30            {
 32131                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.
 32135                if (zWrite == 0)
 36                {
 5837                    material.SetInt("_Surface", 1);
 5838                    material.DisableKeyword("_SCREEN_SPACE_OCCLUSION");
 5839                    material.SetShaderPassEnabled("DepthNormals", false);
 5840                    material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent;
 5841                    OnMaterialProcess?.Invoke(material);
 042                    return;
 43                }
 44            }
 45
 27346            material.SetInt("_Surface", 0);
 47
 27348            int cullMode = (int) UnityEngine.Rendering.CullMode.Off;
 49
 27350            if (material.HasProperty(ShaderUtils.Cull))
 51            {
 26352                cullMode = 2 - (int) material.GetFloat(ShaderUtils.Cull);
 53            }
 54
 55            int baseQueue;
 56
 27357            if (material.renderQueue == (int) UnityEngine.Rendering.RenderQueue.AlphaTest)
 1458                baseQueue = (int) UnityEngine.Rendering.RenderQueue.Geometry + 600;
 59            else
 25960                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
 27363            string appendedKeywords = material.shader.name + string.Join("", material.shaderKeywords);
 27364            int crc = Shader.PropertyToID(appendedKeywords);
 65
 27366            if (!crcToQueue.ContainsKey(crc))
 567                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).
 27370            int queueOffset = (cullMode + 1) * 150;
 27371            material.renderQueue = baseQueue + crcToQueue[crc] + queueOffset;
 27372            OnMaterialProcess?.Invoke(material);
 073        }
 74    }
 75}