Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaozhuai/1a47b3f1abdbd90a3b306a0ba8537c42 to your computer and use it in GitHub Desktop.
Save xiaozhuai/1a47b3f1abdbd90a3b306a0ba8537c42 to your computer and use it in GitHub Desktop.
Simplely implement GL_REPEAT & GL_MIRRORED_REPEAT in shader

OpenGL ES for iOS requires Power-of-Two images, unfortunately, as it is a rather strict implementation.

NPOT textures are technically enabled in the newer iOS hardware running 2.0 ES, but with extremely limited use.

Specifically, you must use linear filtering with clamp-to-edge, and no mipmapping.

And also GL_REPEAT & GL_MIRRORED_REPEAT are not supported.

In most cases a power-of-two image would still be more efficient/have higher framerates.

But this limits your use, there is a simplely way to implement it in fragment shader.

#define MODE_GL_REPEAT           0
#define MODE_GL_MIRRORED_REPEAT  1

varying highp vec2 vTexCoords;

uniform sampler2D texture;
uniform int repeatMode;

highp vec2 calcTexCoords(int repeatMode, highp vec2 coords) {
    highp vec2 newCoords = coords;
    if (repeatMode == MODE_GL_REPEAT) {
        newCoords = vec2(mod(coords.x, 1.0), mod(coords.y, 1.0));
    } else if (repeatMode == MODE_GL_MIRRORED_REPEAT) {
        lowp float signX = 1.0 - 2.0 * mod(floor(coords.x), 2.0);
        lowp float signY = 1.0 - 2.0 * mod(floor(coords.y), 2.0);
        newCoords = vec2(mod(coords.x * signX, 1.0), mod(coords.y * signY, 1.0));
    }
    return newCoords;
}

void main() {
    highp vec2 newCoords = calcTexCoords(repeatMode, vTexCoords);
    gl_FragColor = texture2D(texture, newCoords);
}

And c function isPOT

bool isPOT(int x) {
    return (x != 0) && ((x & (x - 1)) == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment