SRP Batcher 完全工程化:Unity URP 底层 CBuffer 原理、DrawCall 优化与实战
SRP Batcher 是 Unity 2018 引入的「SRP 时代核心优化」——通过把材质数据「持久化」到 GPU 内存、按 CB(Constant Buffer)预分组,让 DrawCall 的 SetPass 开销降到接近零。官方在 SRPBatcherBenchmark 测试中,CPU Rendering Time 从 17.9ms 降到 3.6ms——5 倍性能提升。本文从「为什么 SetPass 这么贵」的根源出发,深度拆解 SRP Batcher 的底层机制与实战。
1. 为什么 SetPass 这么贵:传统 Unity 渲染管线的问题
1.1 DX9 时代的 SetPass 工作
Unity 早期(DX9/OpenGL ES 2.0 时代)每次 DrawCall 都要做大量 SetPass 工作:
// 传统 DrawCall(伪代码) void DrawObject(Mesh mesh, Material material, Matrix4x4 transform) { // 1. 设置 Shader(GPU 状态机切换) material.shader.Use(); // 2. 设置所有 Properties(CPU → GPU 上传) material.SetFloat("_Glossiness", 0.5f);