< 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:28
Uncovered lines:7
Coverable lines:35
Total lines:74
Line coverage:80% (28 of 35)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3
 4namespace DCL.Helpers
 5{
 6    public static class SRPBatchingHelper
 7    {
 8        public static System.Action<Material> OnMaterialProcess;
 19        static Dictionary<int, int> crcToQueue = new Dictionary<int, int>();
 10
 11        public static void OptimizeMaterial(Material material)
 12        {
 13            //NOTE(Brian): Just enable these keywords so the SRP batcher batches more stuff.
 15414            material.EnableKeyword("_EMISSION");
 15415            material.EnableKeyword("_NORMALMAP");
 16
 15417            if (!material.IsKeywordEnabled("_ALPHATEST_ON"))
 7718                material.SetFloat(ShaderUtils.Cutoff, 0);
 19
 15420            material.EnableKeyword("_ALPHATEST_ON");
 15421            material.DisableKeyword("_ALPHABLEND_ON");
 15422            material.DisableKeyword("_ENVIRONMENTREFLECTIONS_OFF");
 15423            material.DisableKeyword("_SPECULARHIGHLIGHTS_OFF");
 15424            material.DisableKeyword("VERTEX_COLOR_ON");
 25
 15426            material.enableInstancing = false;
 27
 15428            if (material.HasProperty(ShaderUtils.ZWrite))
 29            {
 15230                int zWrite = (int) material.GetFloat(ShaderUtils.ZWrite);
 31
 32                //NOTE(Brian): for transparent meshes skip further variant optimization.
 33                //             Transparency needs clip space z sorting to be displayed correctly.
 15234                if (zWrite == 0)
 35                {
 036                    material.SetInt("_Surface", 1);
 037                    material.DisableKeyword("_SCREEN_SPACE_OCCLUSION");
 038                    material.SetShaderPassEnabled("DepthNormals", false);
 039                    material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent;
 040                    OnMaterialProcess?.Invoke(material);
 041                    return;
 42                }
 43            }
 44
 15445            material.SetInt("_Surface", 0);
 46
 15447            int cullMode = (int) UnityEngine.Rendering.CullMode.Off;
 48
 15449            if (material.HasProperty(ShaderUtils.Cull))
 50            {
 15251                cullMode = 2 - (int) material.GetFloat(ShaderUtils.Cull);
 52            }
 53
 54            int baseQueue;
 55
 15456            if (material.renderQueue == (int) UnityEngine.Rendering.RenderQueue.AlphaTest)
 1357                baseQueue = (int) UnityEngine.Rendering.RenderQueue.Geometry + 600;
 58            else
 14159                baseQueue = (int) UnityEngine.Rendering.RenderQueue.Geometry;
 60
 61            //NOTE(Brian): This guarantees grouping calls by same shader keywords. Needed to take advantage of SRP batch
 15462            string appendedKeywords = material.shader.name + string.Join("", material.shaderKeywords);
 15463            int crc = Shader.PropertyToID(appendedKeywords);
 64
 15465            if (!crcToQueue.ContainsKey(crc))
 466                crcToQueue.Add(crc, crcToQueue.Count + 1);
 67
 68            //NOTE(Brian): we use 0, 100, 200 to group calls by culling mode (must group them or batches will break).
 15469            int queueOffset = (cullMode + 1) * 150;
 15470            material.renderQueue = baseQueue + crcToQueue[crc] + queueOffset;
 15471            OnMaterialProcess?.Invoke(material);
 072        }
 73    }
 74}