Array in constant buffer

Started by
20 comments, last by myvraccount 8 years, 2 months ago

I'm using SharpDX (compatible with SlimDX) and trying to put arrays into a constant buffer.

I made a struct and gave it a StructLayout with LayoutKind.Explicit and I manually set Size also.

Inside the struct, I make arrays with attributes FieldOffset and MarshalAs in which I set UnmanagedType.ByValArray and I manually set SizeConst.

When I instantiate the struct, I create the arrays to the size I specified. Then I populate them with the values I want.

Then I update the constant buffer with my struct data, and the size is divisible by 16.

In the shader, I have the arrays of corresponding data types set to the sizes to match the ones in C#. It's all inside a tbuffer.

But then when I run the pixel shader, all the values it reads from the arrays seem to be 0, regardless of what values I set in it.

I know the constant buffer in general is working, because it was working before I added the arrays to the structure.

So what could be wrong?

Advertisement

I tried some more stuff but it still doesn't work:

I tried to make the tbuffer a cbuffer because it didn't seem to be compatible with tbuffers (but the reason I made it a tbuffer in the first place was that I got an error that the cbuffer was bigger than it was allowed to be).

But then I separated it into to structs, and kept that part without arrays as a cbuffer and put all arrays into a tbuffer.

I also made two ConstantBuffer objects - one for each structure type, and I set them to slot 0 and slot 1 and I update them both just before trying to draw anything.

Also, I tried setting the cbuffer to : register (b0) and the tbuffer to : register (t1) - is that right?

Now it's only drawing an image if I give it hard-coded numbers in the pixel shader, so I know it's able to draw, but it still gets 0 (looks black) whenever it reads any number as a color from an array in the tbuffer.

Maybe if you include some code snippet on the thread and highlight the exact problem areas that would be better.

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

I can't. I don't have it with me and I'm not at liberty to divulge. I'm not sure why everyone always wants to see the code every single time though, no matter how detailed my explanation is.

Really all I need is any possible explanation of how to use constant or texture buffers with arrays in them and get the data into the array so that it actually gets into the pixel shader.

I wouldn't think it should be this insufferably difficult to do.

People are asking to see code snippets because code is significantly harder to debug when you aren't actually looking at it.

Yeah I know, but I don't have it here.

What I really need is any source code or a link so it that has a complete example (even a simple one) of just how to put arrays into a buffer and get them to be used by the pixel shader. That's all.

Clicky


What I really need is any source code or a link so it that has a complete example (even a simple one) of just how to put arrays into a buffer and get them to be used by the pixel shader. That's all.

In that case, You can google search for tutorials and sample codes on the subject. Unbird's post-link could be a starting place

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

I have searched for them and the ones I used didn't work. That's why I came here.

Thanks for the datastream idea, unbird. I'll try it.

Well I tried it but it didn't work.

By the way, I did it slightly differently, because I'm not using an array, I'm using a structure that contains several arrays of different types. And I'm putting it in a texture buffer rather than a constant buffer (the constant buffers don't provide enough memory).

So the way I did it was to use Write<> rather than WriteRange. But I set up the buffer description the same but with the size in bytes that I need.

Other than that, I did it how you said, but I still got a black screen.

Also, when I update my constant buffer resource (I do that for the constant buffer, and the texture buffer, because I couldn't find a different function for that), I tried setting it with only the first 2 parameters but was afraid it may be overwriting it so I also tried setting the third (optional) parameter (it's an int called subresource or something) and I set it to 0 for the constant buffer and 1 for the texture buffer (that corresponds to the registers they use although they're called b0 and t1 respectively). I don't know if this is correct but I tried it with and without it.

Anyway, whatever I do, it's always a black screen.

This topic is closed to new replies.

Advertisement