Godot Version
v4.6.2.stable.arch_linux [001aa128b]
Question
Hello!
I’ve encountered some weird behavior with a shader I wrote.
Also to preface: I’ve since had reason to change this shader a lot so this isn’t actually an immediate issue, but I’m posting this because I’m curious as to what is wrong with my code (or Godot, or my GPU drivers).
My issue is that my original shader (right) where I only sample the relevant texture has a blocky appearance, as though it’s only one of the two textures for some large area instead of per-pixel. The working one (left) I sample both and then just don’t use one of them. You might have to zoom to see it but on a large monitor it’s very distracting.
Broken (right):
shader_type spatial;
uniform sampler2D textures[2]: source_color;
uniform sampler2D bias;
uniform float texture_repeats: hint_range(1.0, 100.0, 1.0);
void fragment() {
float t0_priority = texture(textures[0], UV*texture_repeats).a;
float t1_priority = texture(textures[1], UV*texture_repeats).a + texture(bias, UV).r*2.0-1.0;
int opted_texture = t0_priority > t1_priority? 0:1;
ALBEDO = texture(textures[opted_texture], UV*texture_repeats).rgb;
}
Working (left):
shader_type spatial;
uniform sampler2D textures[2]: source_color;
uniform sampler2D bias;
uniform float texture_repeats: hint_range(1.0, 100.0, 1.0);
void fragment() {
float t0_priority = texture(textures[0], UV*texture_repeats).a;
float t1_priority = texture(textures[1], UV*texture_repeats).a + texture(bias, UV).r*2.0-1.0;
int opted_texture = t0_priority > t1_priority? 0:1;
vec4 samples[2] = {
texture(textures[0], UV*texture_repeats),
texture(textures[1], UV*texture_repeats)
};
ALBEDO = samples[opted_texture].rgb;
}
My understanding is that these should be the same, given that the sampler2Ds are readonly. To me what this feels like is I’ve broken an assumption some optimization somewhere has made, and that doing both samples disables that optimization path. I’m not sure though. I do observe this behavior on both my desktop and laptop, but they are both intel (Arc B580 and UHD Graphics 620 respectively).




