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

其他格式: JSON · Markdown 中文 · English
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).

generic

官方文档

https://docs.unity3d.com/Manual/compute-shaders.html

解决方案

  1. 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);
  2. If using GraphicsBuffer, set the correct target (e.g., GraphicsBuffer.Target.Structured). Example: GraphicsBuffer gfxBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, count, stride);

无效尝试

常见但无效的做法:

  1. 60% 失败

    Increasing the buffer size does not fix type mismatch; the issue is stride or target, not capacity.

  2. 30% 失败

    Switching to a different shader variant does not change the buffer declaration mismatch.