< Summary

Class:DCL.Helpers.MaterialCachingHelper
Assembly:MaterialCachingHelper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MaterialHelpers/MaterialCachingHelper/MaterialCachingHelper.cs
Covered lines:46
Uncovered lines:8
Coverable lines:54
Total lines:138
Line coverage:85.1% (46 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MaterialCachingHelper()0%110100%
ComputeHash(...)0%2100%
EnsureMainShader()0%6200%
ProcessSingleMaterial(...)0%6.046090%
Process()0%11.0411093.33%

File(s)

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

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using UnityEngine;
 6using UnityGLTF.Cache;
 7
 8namespace DCL.Helpers
 9{
 10    public static class MaterialCachingHelper
 11    {
 12        [System.Flags]
 13        public enum Mode
 14        {
 15            NONE = 0,
 16            CACHE_MATERIALS = 1,
 17            CACHE_SHADERS = 2,
 18            CACHE_EVERYTHING = CACHE_MATERIALS | CACHE_SHADERS,
 19        }
 20
 1121        public static bool timeBudgetEnabled => CommonScriptableObjects.rendererState.Get();
 122        public static float timeBudgetMax = 0.003f;
 123        public static float timeBudget = 0;
 24
 125        public static Dictionary<string, Shader> shaderByHash = new Dictionary<string, Shader>();
 26
 27        private static Shader mainShader;
 28
 029        public static string ComputeHash(Material mat) { return mat.ComputeCRC().ToString(); }
 30
 31        static Shader EnsureMainShader()
 32        {
 033            if (mainShader == null)
 34            {
 035                mainShader = Shader.Find("DCL/Universal Render Pipeline/Lit");
 36            }
 37
 038            return mainShader;
 39        }
 40
 41        public static Material ProcessSingleMaterial(Material mat, Mode cachingFlags = Mode.CACHE_EVERYTHING)
 42        {
 1143            if ((cachingFlags & Mode.CACHE_SHADERS) != 0)
 44            {
 1145                string shaderHash = mat.shader.name;
 46
 1147                if (!shaderByHash.ContainsKey(shaderHash))
 48                {
 149                    if (!mat.shader.name.Contains("Error"))
 50                    {
 151                        shaderByHash.Add(shaderHash, Shader.Find(mat.shader.name));
 152                    }
 53                    else
 54                    {
 055                        shaderByHash.Add(shaderHash, EnsureMainShader());
 56                    }
 57                }
 58
 1159                mat.shader = shaderByHash[shaderHash];
 60            }
 61
 62            //NOTE(Brian): Have to copy material because will be unloaded later.
 1163            var materialCopy = new Material(mat);
 1164            SRPBatchingHelper.OptimizeMaterial(materialCopy);
 65
 1166            if ((cachingFlags & Mode.CACHE_MATERIALS) != 0)
 67            {
 1168                int crc = mat.ComputeCRC();
 1169                string hash = crc.ToString();
 70
 71                RefCountedMaterialData refCountedMat;
 72
 1173                if (!PersistentAssetCache.MaterialCacheByCRC.ContainsKey(hash))
 74                {
 75#if UNITY_EDITOR
 276                    materialCopy.name += $" (crc: {materialCopy.ComputeCRC()})";
 77#endif
 278                    PersistentAssetCache.MaterialCacheByCRC.Add(hash, new RefCountedMaterialData(hash, materialCopy));
 79                }
 80
 1181                refCountedMat = PersistentAssetCache.MaterialCacheByCRC[hash];
 1182                refCountedMat.IncreaseRefCount();
 1183                return refCountedMat.material;
 84            }
 85
 086            return materialCopy;
 87        }
 88
 89        public static IEnumerator Process(List<Renderer> renderers, bool enableRenderers = true, Mode cachingFlags = Mod
 90        {
 1191            if (renderers == null)
 092                yield break;
 93
 1194            int renderersCount = renderers.Count;
 95
 1196            if (renderersCount == 0)
 097                yield break;
 98
 1199            var matList = new List<Material>(1);
 100
 44101            for (int i = 0; i < renderersCount; i++)
 102            {
 11103                Renderer r = renderers[i];
 104
 11105                if (!enableRenderers)
 11106                    r.enabled = false;
 107
 11108                matList.Clear();
 109
 11110                var sharedMats = r.sharedMaterials;
 111
 44112                for (int i1 = 0; i1 < sharedMats.Length; i1++)
 113                {
 11114                    Material mat = sharedMats[i1];
 115
 11116                    float elapsedTime = Time.realtimeSinceStartup;
 117
 11118                    matList.Add( ProcessSingleMaterial(mat, cachingFlags) );
 119
 11120                    if (timeBudgetEnabled)
 121                    {
 11122                        elapsedTime = Time.realtimeSinceStartup - elapsedTime;
 11123                        timeBudget -= elapsedTime;
 124
 11125                        if (timeBudget < 0)
 126                        {
 1127                            yield return null;
 1128                            timeBudget += timeBudgetMax;
 129                        }
 130                    }
 131                }
 132
 11133                if (r != null)
 11134                    r.sharedMaterials = matList.ToArray();
 11135            }
 11136        }
 137    }
 138}