Can't read a FORMAT_R8 uniform sampler2D texture correctly from within a shader

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Petruza

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:


extends ViewportContainer

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

    var ink =
    ink.create_from_image(image, 0)
    (self.material as ShaderMaterial).set_shader_param("ink", ink)


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

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.

Calinou | 2021-05-11 00:29

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?

Petruza | 2021-05-11 01:47

I also could not get isampler2D nor usampler2D to work on Windows.
The values always were in the 2^30 range like you described.

Using sampler2D and converting float to int lead to problems on my android phone. I might have to try that again.

My workaround for now is to use multiple uniform int. This works for me currently because i am just prototyping and do not need to support many values.


uniform int inkSize;
uniform int ink00;
uniform int ink01;
uniform int ink02;
// etc ..
int getInk(int index) {
    switch (index) {
        case 0:
            return ink00;
        case 1:
            return ink01;
        case 2:
            return ink02;
    // etc

void fragment() {
    for(int i = 0; i < inkSize; ++i) {
        int ink = getInk(i);


var ink = [7, 3, 5]
shader_material.set_shader_param("inkSize", ink.size())
for i in range(ink.size()):
    var shader_var_name = "ink%02d" % i
    shader_material.set_shader_param(shader_var_name, ink[i])

sheffield | 2022-03-24 05:54