Hi, I’ve come across an issue in hlsl where in my “game of life”-like simulation, a pixel needs to “jump” to another pixel. I’m trying to implement this in an HLSL shader for performance.
This is a surprisingly hard thing to do. I tried to write to a static float4 array so I could do a second pass where I read from it, but the maximum array size in a shader is too small (around 60000, I need roughly 10 times more space than that)
So I looked at RWByteAddressBuffer, but I’m not even sure how to implement that. It seems it’s very hard to get a grip on how to do things in hlsl in general, with the only real documentation being MS’s official, which is robotic and only useful for people who are not learning.
That is the proper way of looking at it, but a game of life shader is very atypical.
In my case there are upwards of 32*32 surrounding pixels that may affect the color. I would have to do a for loop, some arithmetic, as well as a conditional check on each of them. Only one of them will affect the output.
I’ll give that a shot if I can’t figure out a better way. But I’d really like to establish that there is no other way. This method would make my shader thousands of times slower.
It would be much more practical if it could write the destination to a buffer. And that may be close, I could almost do it if the array size limit was higher.
I may be getting close with RWByteAddressBuffer, maybe if I have a field like
Throws a null reference exception because it cannot find the parameter. The solution may be interfacing with it through SharpDX.Utilities.AllocateMemory() as suggested below. Maybe I will have to do it all through DirectX/SharpX.
I wonder if this is an issue with Monogame? Seems like effect.Parameters should be able to access it.
Just for clarity, I’ll post my .fx code so far, with much commented out to focus on the issue. Maybe someone can point out a dumb mistake I’m making. That’s enough research for me tonight though.
I have no idea if this will help you but the shader i use for blur (grabbed from offline somewhere) uses an array of weights and offsets to average each pixel to generate a blur effect by sampling neighbouring pixels. So its kind of similar in sampling other pixels.
Like i said no idea if this will help you but you might be able to grab something to speed your shader up. My shader knowledge is very limited.
I may end up doing that. My RWTexture2D doesn’t show up in parameters. The other method is setting GraphicsDevice.Texture[index] where index is the register.
aka
C#
Texture2D texture = new Texture2D(Parent.GraphicsDevice, Width, Height);
Color[] arr = new Color[Width * Height];
for (int i = 0; i < arr.Length; i++) arr[i] = Color.Red;
texture.SetData(arr);
Parent.GraphicsDevice.Textures[3] = texture;
I’ll try the latest dev build and then open a git issue if it’s still present. From my point of view it’s less about doing it this specific way and more about making sure HLSL is functional and not missing any features it should have. There’s a lot I would like to do in Monogame with HLSL that’s going to get more complex.