private void DrawReadbackStat(ReadbackStat stat) { EditorGUILayout.Space(); EditorGUILayout.LabelField("Readback Stat: "); EditorGUILayout.LabelField(string.Format(" Current Latency: {0} frame(s)", stat.CurrentLatency)); EditorGUILayout.LabelField(string.Format(" Average Latency: {0} frame(s)", stat.AverageLatency.ToString("N2"))); EditorGUILayout.LabelField(string.Format(" Max Latency : {0} frame(s)", stat.MaxLatency)); EditorGUILayout.LabelField(string.Format(" Fails : {0} time(s)", stat.FailedCount)); }
/// <summary> /// 发起回读请求 /// </summary> private void NewRequest(RenderTexture texture) { // 如果未完成的回读请求过多,就暂不生成新的请求 if (m_ReadbackRequests.Count > 8) { return; } // 缩放后的尺寸 var width = (int)(texture.width * m_ReadbackScale.ToFloat()); var height = (int)(texture.height * m_ReadbackScale.ToFloat()); // 先进行缩放 if (m_ReadbackScale != ScaleFactor.One) { if (m_DownScaleTexture == null || m_DownScaleTexture.width != width || m_DownScaleTexture.height != height) { m_DownScaleTexture = new RenderTexture(width, height, 0); } m_DownScaleTexture.DiscardContents(); Graphics.Blit(texture, m_DownScaleTexture, m_DownScaleMaterial, m_DownScaleMaterialPass); texture = m_DownScaleTexture; } // 贴图尺寸检测 if (m_ReadbackTexture == null || m_ReadbackTexture.width != width || m_ReadbackTexture.height != height) { m_ReadbackTexture = new Texture2D(width, height, TextureFormat.RGBA32, false); m_ReadbackTexture.filterMode = FilterMode.Point; m_ReadbackTexture.wrapMode = TextureWrapMode.Clamp; InitDebugTexture(width, height); } // 发起异步回读请求 var request = AsyncGPUReadback.Request(texture); m_ReadbackRequests.Enqueue(request); ReadbackStat.BeginRequest(request); }
/// <summary> /// 检测回读请求状态 /// </summary> private void UpdateRequest() { UpdateStat.BeginFrame(); bool complete = false; while (m_ReadbackRequests.Count > 0) { var req = m_ReadbackRequests.Peek(); if (req.hasError) { ReadbackStat.EndRequest(req, false); m_ReadbackRequests.Dequeue(); } else if (req.done) { // 更新数据并分发事件 m_ReadbackTexture.GetRawTextureData <Color32>().CopyFrom(req.GetData <Color32>()); complete = true; ReadbackStat.EndRequest(req, true); m_ReadbackRequests.Dequeue(); } else { break; } } if (complete) { UpdateStat.EndFrame(); OnFeedbackReadComplete?.Invoke(m_ReadbackTexture); UpdateDebugTexture(); } }