unity runtime_error ai_generated true

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

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-11-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Unity 2022.3 active
Unity 2023.1 active
Unity 6.0 active

Root Cause

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

中文

HLSL着色器中声明的缓冲区类型(例如StructuredBuffer)与C#中创建的缓冲区类型(例如ComputeBuffer步幅错误或GraphicsBuffer目标错误)不匹配。

Official Documentation

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

Workarounds

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

中文步骤

  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);

Dead Ends

Common approaches that don't work:

  1. 60% fail

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

  2. 30% fail

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