8 Replies Latest reply on May 4, 2012 5:12 AM by karen.zeng

    sampler2D produces artefacts with GL_REPEAT and GL_NEAREST

    sergik128

      Hi,

      In openGL 2.0 when using sampler2D in fragment shader in conjuction with GL_REPEAT wrapping mode and GL_NEAREST filter a hairline artefacts appear near the edges where texture coordinates approach 1.0, the edges where texture coordinates approach 0.0 are fine.

      In the example below a simple quad is rendered parallel to the screen plane rotated 20 degrees around Z axis. Texture is being interpolated between four vertices (0, 0;  1, 0;  1, 1;  0, 1). On the two edges where one of the coordinates approach 1.0 sampler2D overflows and starts sampling "outside" of the texture which leads to a hairline artefacts.

      The problem is reproducible on both Windows 7 (64-bit) and Ubuntu 10.10 Linux with Catalyst 11.2 driver running on Radeon HD 5850.

      I have a complete source code for both platforms and can provide it on request. The same code works fine with SGX 530/540 and Nvidia drivers.

       

       

       

      const char *vertShader = 

          "precision highp float;                                 \n"

          "attribute vec4 vVxPos;                                 \n"

          "attribute vec2 vTexCoord;                              \n"

          "uniform mat4 myPMVMatrix;                              \n"

          "varying   vec2 tex_coord;                              \n"

          "void main()                                            \n"

          "{                                                      \n"

          "   gl_Position = myPMVMatrix * vVxPos;                 \n"

          "   tex_coord = vTexCoord;                              \n"

          "}                                                      \n";

       

      const char *fragShader = 

          "precision highp float;                                 \n"

          "varying vec2      tex_coord;                           \n"

          "uniform sampler2D tx;                                  \n"

          "void main()                                            \n"

          "{                                                      \n"

          "  gl_FragColor = texture2D (tx, tex_coord);            \n"

          "}                                                      \n";

       



       

       

          GLuint texture_id;

          glEnable(GL_TEXTURE_2D);

          glGenTextures (1, &texture_id);

          glBindTexture (GL_TEXTURE_2D, texture_id);

          unsigned tex[] =

          {

              0xFF000000, 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF,

              0xFFFFFFFF, 0xFF00FFFF, 0xFF00FF00, 0xFF000000,

              0xFF000000, 0xFFFF0000, 0xFF0000FF, 0xFFFFFFFF,

              0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFF000000,

          };

          glTexImage2D(GL_TEXTURE_2D,

                       0,

                       GL_RGBA,

                       4,

                       4,

                       0,

                       GL_RGBA,

                       GL_UNSIGNED_BYTE,

                       tex);

          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);