Cannot use per-instance for uniform sampler2D?

Godot Version

4.3

Question

In the gdshader, I am trying out:

instance uniform sampler2D instance_texture : source_color, filter_linear_mipmap, repeat_enable;

But Godot complains:

error(23): The 'SCOPE_INSTANCE' qualifier is not supported for sampler types.

Because of this, in the Shader, if I would like to modify the current texture per-instance from gdscript , can it be done?

There are some limitations to per-instance uniforms, including lacking sampler2D. They recommend a potential workarround using an array of textures.

Per-instance uniforms do not support textures, only regular scalar and vector types. As a workaround, you can pass a texture array as a regular uniform, then pass the index of the texture to be drawn using a per-instance uniform.

1 Like

@gertkeno

I see. From searching around, I found Texture2DArray as resource

a Texture2DArray is just a single file with each texture set in a grid.

If this is the case, then from the Shader perspective, this just means using a single file and changing UV positions around for each actual texture, I suppose?

a Texture2DArray could work, you could also create an array of textures

uniform sampler2D textures[4]; # for various images
uniform sampler2DArray textures; # for sprite-sheet style images
instance uniform int texture_index;
1 Like

this workaround doesnt work for me given the limitation of array length

why dont we just support instance uniform anything?

Chances are the “instance” uniforms are tied into a GPU instancing system which can have unique per-instance data, but not entire textures piped along with the draw call.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.