SetConstantBuffer not working?

    terrainBlendBuffers = new ComputeBuffer(batchesT.Length, 16, ComputeBufferType.Constant);
    terrainBlendBuffers[i].SetData(batchesT);
 
    prop = new MaterialPropertyBlock();

    terrainBlendBuffers[i].name = "terrainBlend";
    prop.SetConstantBuffer("_TerrainBlendParamsInstanced", terrainBlendBuffers, 0, terrainBlendBuffers[i].count * 16);

Graphics.DrawMeshInstanced(mesh, submeshIndex, _blendMaterial, batchM, batchM.Length, prop,
    ShadowCastingMode.Off, true, layer, Camera.current);

I wrote this code, but my CBuffer is 0 as showed in renderdoc:

Very similar code works if I use structured buffers instead.

This is URP Unity 2021.3.9. The only info I found is this:

is this an actual bug or I am getting something wrong?

I wonder if it broke with the introduction of

Graphics.RenderMeshInstanced

I need to check if it works with it

Will it work if you manually define ComputeBufferMode.Dynamic (add at the end of ComputeBuffer constructor).

Also - how do you use it in shader? Like this through macro?

CBUFFER_START(YourConstantBuffer)
        float _someYrBufferValue;
CBUFFER_END

Or manually through

cbuffer YourConstantBuffer {
float someValue;
}

Constant buffers definitely works with CommandBuffer.SetGlobalConstantBuffer, maybe something with MPB here.

1 Like

no, I tried all the combinations.

Ah and one more - if you just do MPB.SetBuffer ?

SetBuffer is a structured buffer. With StructuredBuffers it works. Are you asking me to use SetBuffer as constant buffer? hmmm

I mean, I wouldn’t be totally surprised if it worked :smile:

didn’t work. if I had the time I’d test the Graphics.RenderMeshInstanced hyphotesis, but I have to move to other tasks now

I missed the edit. In the shader they are defined as:

CBUFFER_START( TerrainBlend )
          float4 _TerrainBlendParamsInstanced[256];
           CBUFFER_END

ok same result with RenderMeshInstanced too

Any chance you found a solution?