Making an 8x8 matrix

Godot Version

4.4.1

Question

I am trying to create a shader that uses an 8x8 matrix of float values but I’m not sure how to go about it. As far as I can tell from the documentation there is only support for mat2, mat3, and mat4. I tried making a 2D array since that would probably be the easiest but I also can’t figure out how to make one. I try to make it in the traditional way similar to this: const float BAYER_8x8[2][2] = { { 4.0, 4.0 }, { 4.0, 4.0 } };
But this just throws an error. The best I could do to get it to work is this:

const float BAYER_8x8[8] = { 
								float[8] (0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0 ), 
								float[8] (48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0),
								float[8] (12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0),
								float[8] (60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0),
								float[8] (3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0),
								float[8] (51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0),
								float[8] (15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0),
								float[8] (63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0)
                              };

But I can’t seem to access any of the values by using the double brackets:

// Throws error saying 'type float can't be indexed
fragment_dither = BAYER_8x8[0][0];

What would be the best method of making this?

Personally, I’d probably be tempted to throw it into a small texture and use a sampler to pull values out.

1 Like

Gotcha. How would I go about doing that? I’m still fairly new to shaders.

I haven’t done much with shaders in Godot yet (most of my shader work has been in my own engine using OpenGL or Vulkan directly), but in principle you’d access it the same way you’d use a texture for color or normal maps. Get your 8x8 matrix into an 8x8 pixel image with floating point pixels, hook it up to the shader the same way you would any other texture, and pull values from it with a sampler in the standard way. You’ll want to set the min/mag filter to “nearest”, but other than that it should just work.

1 Like