0

I have a GLSL fragment shader source that is producing correct results on half of our machines and incorrect (but consistent) on the other half.

I basically have two matrices or arrays that I am adding. Data is mapped to 2D textures of size 7x9. Both inputs are of indentical size of 60 float32's. The closest texture size that my code is finding is 7x9.

The following script produces correct results on all machines. TexCoords is my varying:

    glFragColor = texture2D(A, TexCoords) + texture2D(B, TexCoords);

However the following (simplified) version does not. Which indicates either my logic of mapping coordinates to offsets and back is incorrect or something more mysterious is at play here:

    const float xScale = 7.0; // x size of the texture
    const float yScale = 9.0; // y size of the texture
    float s = TexCoords.s * xScale; // denormalize x
    float t = TexCoords.t * yScale; // denormalize y
    int offset = int(t) * 7 + int(s); // flattened offset from 0

    s = mod(float(offset), 7.0); // recalc x from offset
    t = floor(float(offset) / 7.0); // recalc y from offset

    vec2 coords = vec2(s / xScale, t / yScale); // normalize
    glFragColor = (texture2D(A, coords) + texture2D(B, coords);

Example of correct results (truncated)

EXPECTED: type=float32; dims=[3,4,5]; data=[1.0915919542312622,0.04060405492782593,0.16559171676635742

Example of incorrect one:

ACTUAL: type=float32; dims=[3,4,5]; data=[1.0915919542312622,1.0915919542312622,0.04060405492782593,...  
gman
  • 100,619
  • 31
  • 269
  • 393
Jeff Saremi
  • 2,674
  • 3
  • 33
  • 57
  • well i tried to answer your question but my answer was deleted as a duplicate so apparently i'm supposed to close your question as a duplicate since the answer is similar. I don't agree that it's a duplicate question but it's likely the same fundamental problem. You're computing texel edges not texels. – gman Aug 14 '18 at 14:20

1 Answers1

0

By doing a lot of trial and error I came to realization that to go from the denormalized number to normalized coordinates I can't just divide the offset/index by the width/height of the texture. There must be some tolerance included. For instance, the actual numbers in TexCoords never start from 0. and they're never on the 1/w or 1/h but always noticably larger or smaller.

So for now I am adding a 0.5 (I still don't have the scientific reason for it) to my index before getting it's normalized coords:

vec2 coords = vec2((s+0.5)/xScale, (t+0.5)/yScale)
Jeff Saremi
  • 2,674
  • 3
  • 33
  • 57
  • There's actually a better way described here: https://stackoverflow.com/questions/40574677/how-to-normalize-image-coordinates-for-texture-space-in-opengl – Jeff Saremi Aug 14 '18 at 08:12
  • that other answer wrong. What you have above is correct. – gman Aug 14 '18 at 13:21