Reputation: 513
I worked a little bit on my fragment shader to allow both texture rendering as well as color rendering. After I made the changes, all my textures were very pixelated (like an old 8-bit game) and I had to ramp up the precision for the texture coordinates to medium. This also gave me a performance hit. I just don't understand why I suddenly had to change the precision in the first place.
Here is the "original" shader:
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
uniform lowp float opacity;
void main(void) {
gl_FragColor = opacity * texture2D(Texture, TexCoordOut);
}
This is how the shader looks after the changes:
varying mediump vec2 TexCoordOut;
uniform sampler2D Texture;
uniform lowp vec4 color;
uniform lowp float opacity;
uniform int colorRender;
void main(void) {
if (colorRender == 1)
{
gl_FragColor = color;
} else {
gl_FragColor = opacity * texture2D(Texture, TexCoordOut);
}
}
Upvotes: 1
Views: 3358
Reputation: 1681
You blame the performance loss on switching to mediump, but you've also added branching to the shader. Given that every fragment for a single draw call will always take the same path, you should just have two different shaders, each without branching.
Upvotes: 2
Reputation: 126526
lowp
only has about 8 bits of precision minimum, so if your texture is 128x128 or larger you'll start to see artifacts, and if your texture is bigger than 256x256, some texels will be lost completely (the severe pixillation you describe).
mediump
has a minimum of 11 bits of precision, so should be fine up to 1024x1024 texels (though being a floating-point type, you can get pixellation if your texture coordinates are unnormalized)
highp
has a minimum of 24 bits of precision (same as IEEE single-precision float) so is even better.
Note that these precisions are minimums according to the spec -- hardware may use more precision for any of these if it would be faster.
Upvotes: 7
Reputation: 162327
The texture coordinates are evaluated for each fragment separately. However with low precision numbers are quantized down on a per-fragment base, which means that a lot of neighboring fragments will actually evaluate to the very same quantized texture coordinate. In principle the precision for a texture coordinate variable must allow for more quantization steps, than the number of pixels/texels in the texture in the sampled direction.
Upvotes: 1