You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity Build中Compute Shader调用导致后续Blit失效,编辑器正常

解决Unity M1 Mac Build版本中Blur材质Blit失效问题

排查与修复方案

1. 解决RenderTexture读写锁冲突

Compute Shader写入tempRt后,GPU可能仍持有该纹理的写锁,导致后续Graphics.Blit无法正常读取。在Compute Shader执行后显式释放GPU使用权:

// 在ExtractBloomLayer.Dispatch之后添加
RenderTexture.active = null;
tempRt.Release();
tempRt.Create();

2. 统一RenderTexture格式与权限

M1 Mac的Metal后端对纹理格式兼容性要求严格,初始化tempRt和tempRt2时确保格式与源纹理一致,并开启随机写入权限:

// 初始化代码示例(建议在Start或Awake中执行)
tempRt = new RenderTexture(source.width, source.height, 0, source.format);
tempRt.enableRandomWrite = true; // Compute Shader写入必须开启
tempRt.Create();

tempRt2 = new RenderTexture(source.width, source.height, 0, source.format);
tempRt2.Create();

3. 强制Blur材质的渲染状态

Build版本中Metal可能对材质渲染路径做了优化,手动指定Blur Shader的渲染标签与关键字:

  • 打开Blur对应的Shader文件,添加标签:Tags { "RenderType"="Opaque" }
  • 代码中强制启用材质关键字:
blurMat.EnableKeyword("FORWARD");

4. 禁用Metal异步着色器编译

Unity在M1 Mac Build版本默认启用的异步编译可能导致Blur Shader未及时加载:

  • 打开PlayerSettings -> Other Settings -> Metal
  • 关闭Async Shader Compilation,勾选Prebake Shaders并确保Blur Shader在预编译列表中

5. 增加中间纹理隔离Compute Shader输出

避免直接使用Compute Shader写入的纹理作为Blit源,先复制到临时纹理:

// Compute Shader执行后
RenderTexture tempCopy = RenderTexture.GetTemporary(tempRt.width, tempRt.height, 0, tempRt.format);
Graphics.Blit(tempRt, tempCopy);
// 用临时纹理执行Blur
Graphics.Blit(tempCopy, tempRt2, blurMat);
RenderTexture.ReleaseTemporary(tempCopy);

内容的提问来源于stack exchange,提问作者spacemann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:24:59