0 votes

I need to pass an array of integers to a shader as a uniform. As uniform arrays are not yet supported, I'm using a FORMAT_R8 texture/isampler2D to achieve this.

If I'm correctly reading the bytes from the texture, they should be in the range [0..7] and then by normalizing that value, I should get different shades of red, but that's not happening.

In the screenshot you can see that when the value is read as 0, it's correctly displaying it black, but when it's other than 0, it's full red, no shades. I tried comparing the value read and it's always greater than any value I can try, up to the maximum value of a 32 bit signed integer, so no idea what value I'm reading back from the texture.

Any ideas?

The full project is here: https://github.com/Drean64/clash-of-the-colors/tree/simple

GDScript:

extends ViewportContainer

func _ready():
    var attr_ink = PoolByteArray([])
    attr_ink.resize(32*24)
    for i in range(0, 32*24):
        attr_ink[i] = i % 8 # fill array with [0..7]

    var image = Image.new()
    image.create_from_data(32, 24, false, Image.FORMAT_R8, attr_ink)

    var ink = ImageTexture.new()
    ink.create_from_image(image, 0)

    (self.material as ShaderMaterial).set_shader_param("ink", ink)

Shader:

shader_type canvas_item;

uniform isampler2D ink;

void fragment() {
    COLOR = vec4(0., 0., 0., 1.); // Black
    ivec2 cell = ivec2(UV / TEXTURE_PIXEL_SIZE) / 8; // 8x8 pixel cell coordinate
    int ink_color = texelFetch(ink, cell, 0).r; // Should be [0..7]
    COLOR.r = float(ink_color) / 7.; // Should normalize the red value to [0..1]
    // Line above is a kind of debug to get a hint at what the ink_color value is
}
Godot version 3.2.3 (.stable.official)
in Engine by (14 points)
recategorized by

You may want to try reproducing this issue on a Windows or Linux machine, as there are some macOS-specific rendering bugs in both GLES3 and GLES2.

I first tried declaring the array inside the shader as a constant and it worked perfectly, what I'm not getting right is how to read back the texture. Do you see any problem in the code?

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.