void OnDisable() { if (m_renderer) { m_renderer.Release(); --s_instanceCount; if (s_instanceCount == 0) { ClearAllCacheRecords(); } } }
// actual initialization is done in render thread (create d3d12 device if needed and initialize GPU resources). // wait for complete actual initialization if 'wait' is true. // if already initialized (or failed) it immediately returns its result. so calling this in every frame is no problem. bool InitializeRenderer(bool wait = false, double timeoutInSeconds = 3.0) { if (m_initialized) { return(m_renderer); } #if UNITY_EDITOR // initializing renderer can interfere GI baking. so wait until it is completed. if (Lightmapping.isRunning) { return(false); } #endif if (!m_renderer) { m_renderer = rthsRenderer.Create(); m_renderer.name = gameObject.name; if (m_dbgVerboseLog) { Debug.Log(String.Format("Initializing Renderer start ({0}f)", Time.frameCount)); } } if (wait) { var freq = System.Diagnostics.Stopwatch.Frequency; var timeout = (long)(timeoutInSeconds * freq); var start = System.Diagnostics.Stopwatch.GetTimestamp(); while (!m_renderer.initialized && (System.Diagnostics.Stopwatch.GetTimestamp() - start) < timeout) { System.Threading.Thread.Sleep(10); } if (m_dbgVerboseLog) { var elapsed = System.Diagnostics.Stopwatch.GetTimestamp() - start; var elapsedMS = (double)elapsed / freq * 1000.0; Debug.Log(String.Format("{0}ms waited", elapsedMS)); } } if (m_renderer.initialized) { if (m_dbgVerboseLog) { Debug.Log(String.Format("Initializing Renderer finish ({0}f)", Time.frameCount)); } m_initialized = true; if (m_renderer.valid) { ++s_instanceCount; if (s_meshDataCache == null) { s_meshDataCache = new Dictionary <Mesh, MeshRecord>(); s_bakedMeshDataCache = new Dictionary <Component, MeshRecord>(); s_meshInstDataCache = new Dictionary <Component, MeshInstanceRecord>(); s_renderTargetCache = new Dictionary <RenderTexture, RenderTargetRecord>(); } } else { m_renderer.Release(); FeedErrorLog(); this.enabled = false; } } return(m_renderer); }