Error: Invalid stride XXXX for Compute Buffer [SOLVED]

I have an error.

Invalid stride 9984 for Compute Buffer - must be greater than 0, less or equal to 2048 and a multiple of 4.

Can’t find what it means. Please, help.

Stride is the size of one unit of data in the compute buffer. For example, if your data structure is a Vector3 (or in the shader side, float3) the stride is sizeof(float) * 3.

If you are using a struct, you have to take into account the size of each of its members.

If you are using a struct, you have to take into account the size of each of its members.
i did
I’m just not getting what is the problem / the struct is too big?

Edit: just checked its not about the size of the struct / the problem is somewhere else.

I had problems once with a mixed type struct (ints, floats, and bools). I’ve learned that booleans in GPU are 4 bytes long, and I’ve ended up using ints for those.
That caused a memory misalignment. Maybe it’s something like that?

1 Like

DrBlort
i guess this is not a problem / because everything worked before with this struct. then ive been working on code for quite a while and now have this error and unity is not saying where is it coming from. it cud be from the shader or c# script.
maybe something to do with that i added more computebuffers or the way im creating them but not coz of the struct.

Hooray I found! It was wrong data size in ComputeBuffer constructor.
Why unity just doesnt say that.

1 Like

Glad you found it!

1 Like

Wait, so how did you go about setting bools in the compute shader? I’m currently getting an error on buffer.SetData() as the NativeArray that I am passing in is not a multiple of 4.

Sooooo… that was a trip down memory lane, it’s been 4 years since that post, hahah

I used ints like this, in my cginc file:

{
some ints
...
int withBorders; <-- this one is treated as boolean by my code
...
some floats
};```

in my C# code elsewhere:

```//Set the value
private bool withBorders;
...
Buffers.puzzleParametersArray[0].withBorders = withBorders ? 1 : 0;

//Some conditional checks
if (Buffers.puzzleParametersArray[0].withBorders == 1) ```

I don't remember exactly, but apparently never needed to use that value inside the actual compute shader as I ended up precalculating most of my data on the C# side.
I just used the same struct to make things easier.

I guess you can do the same kind of check in the shader, or even multiply the data by that 1 or 0 if it suits what you need to do.

Something like (pseudocode, I won't pretend that I remember the actual HLSL syntax):

```mycolor = (red * withBorders) + (blue * (1 - withBorders)); // red with borders, blue without```

Hope that this helps!
1 Like

Thanks for the effort, yeah I ended up using ints just like you demonstrated.