unity
runtime_error
ai_generated
true
ArgumentException: 缓冲区'MyBuffer'在着色器中声明为结构化缓冲区,但C#脚本绑定了不同类型的缓冲区(ComputeBuffer或GraphicsBuffer)
ArgumentException: Buffer 'MyBuffer' has been declared as a structured buffer in the shader, but the C# script is binding a different type (ComputeBuffer or GraphicsBuffer)
ID: unity/graphics-buffer-mismatch
85%修复率
88%置信度
1证据数
2023-11-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Unity 2022.3 | active | — | — | — |
| Unity 2023.1 | active | — | — | — |
| Unity 6.0 | active | — | — | — |
根因分析
HLSL着色器中声明的缓冲区类型(例如StructuredBuffer)与C#中创建的缓冲区类型(例如ComputeBuffer步幅错误或GraphicsBuffer目标错误)不匹配。
English
Mismatch between the buffer type declared in the HLSL shader (e.g., StructuredBuffer) and the buffer type created in C# (e.g., ComputeBuffer with wrong stride or GraphicsBuffer with wrong target).
官方文档
https://docs.unity3d.com/Manual/compute-shaders.html解决方案
-
Ensure the C# ComputeBuffer stride equals the size of the struct used in the shader. For a float3 buffer, stride = 12 bytes. Example: ComputeBuffer buffer = new ComputeBuffer(count, sizeof(float) * 3); shader.SetBuffer(kernel, "MyBuffer", buffer);
-
If using GraphicsBuffer, set the correct target (e.g., GraphicsBuffer.Target.Structured). Example: GraphicsBuffer gfxBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, count, stride);
无效尝试
常见但无效的做法:
-
60% 失败
Increasing the buffer size does not fix type mismatch; the issue is stride or target, not capacity.
-
30% 失败
Switching to a different shader variant does not change the buffer declaration mismatch.