Martin Perry
Martin Perry

Reputation: 9527

OpenGL ES texture is shifted with the same code as in OpenGL version

In my Windows code (OpenGL 4), I have this to calculate texture position in Fragment shader:

vec2 pixel = "some equation"
vec2 textureSize = vec2(512.0, 512.0);
vec2 texel = floor(pixel + vec2(0.5)) + vec2(0.5);
texel /= textureSize

If I run this code on iOS (OpenGL ES 3.0), my entire image is partialy shifted in x and y direction. I have tried to remove vec2(0.5) and just use

vec2 texel = floor(pixel + vec2(0.5))

but it was even more incorrect.

What is going on in ES?

Upvotes: 1

Views: 63

Answers (1)

Columbo
Columbo

Reputation: 6776

Presumably shader precision. Try adding:

precision highp float;

at the top of your fragment shader.

Without that statement, and in the absence of precision qualifiers on individual vars, then your maths operations may (depending on the implementation) take place with half precision floats rather than single precision (16-bit floats instead of 32-bit floats), and that lack of precision could explain the artifacts you're seeing.

Note that OpenGLES implementations are free to not support highp in fragment shader, so this approach may not work everywhere. However, it is supported on all iOS devices, which is all you mention.

Upvotes: 1

Related Questions