bfulgham@apple.com | f519b7f | 2016-03-23 19:21:11 +0000 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>WebGL Non-Power of 2 texture conformance test.</title> |
| 5 | <script src="../../../resources/js-test.js"></script> |
| 6 | <script src="resources/webgl-test.js"> </script> |
| 7 | <script src="resources/webgl-test-utils.js"> </script> |
| 8 | </head> |
| 9 | <body> |
| 10 | <canvas id="example" width="4" height="4" style="width: 40px; height: 30px;"></canvas> |
| 11 | <div id="description"></div> |
| 12 | <div id="console"></div> |
| 13 | <script id="vshader" type="x-shader/x-vertex"> |
| 14 | attribute vec4 vPosition; |
| 15 | attribute vec2 texCoord0; |
| 16 | varying vec2 texCoord; |
| 17 | void main() |
| 18 | { |
| 19 | gl_Position = vPosition; |
| 20 | texCoord = texCoord0; |
| 21 | } |
| 22 | </script> |
| 23 | |
| 24 | <script id="fshader" type="x-shader/x-fragment"> |
| 25 | #ifdef GL_ES |
| 26 | precision mediump float; |
| 27 | #endif |
| 28 | uniform samplerCube tex; |
| 29 | varying vec2 texCoord; |
| 30 | void main() |
| 31 | { |
| 32 | gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1))); |
| 33 | } |
| 34 | </script> |
| 35 | <script> |
| 36 | if (window.internals) |
| 37 | window.internals.settings.setWebGLErrorsToConsoleEnabled(false); |
| 38 | |
| 39 | var wtu = WebGLTestUtils; |
| 40 | var canvas = document.getElementById("example"); |
| 41 | var gl = wtu.create3DContext(canvas); |
| 42 | var program = wtu.setupTexturedQuad(gl); |
| 43 | |
| 44 | glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
| 45 | |
| 46 | // Make 3 textures by using 3 active texture units. |
| 47 | var textures = []; |
| 48 | for (var ii = 0; ii < 3; ++ii) { |
| 49 | var tex = gl.createTexture(); |
| 50 | gl.activeTexture(gl.TEXTURE0 + ii); |
| 51 | gl.bindTexture(gl.TEXTURE_2D, tex); |
| 52 | textures[ii] = tex; |
| 53 | } |
| 54 | shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 55 | |
| 56 | // Alternate POT and NPOT textures, ending with NPOT. |
| 57 | // |
| 58 | // 1. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in NPOT texture) |
| 59 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 60 | wtu.fillTexture(gl, textures[0], 5, 3, [0, 192, 128, 255], 1); |
| 61 | glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D[0] with NPOT texture with level > 0 should return INVALID_VALUE"); |
| 62 | |
| 63 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 64 | wtu.fillTexture(gl, textures[1], 4, 4, [0, 192, 128, 255], 1); |
| 65 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[1] with POT texture with level > 0 should succeed"); |
| 66 | |
| 67 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 68 | wtu.fillTexture(gl, textures[2], 5, 3, [0, 192, 128, 255], 1); |
| 69 | glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D[2] with NPOT texture with level > 0 should return INVALID_VALUE"); |
| 70 | |
| 71 | // 2. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in POT texture) |
| 72 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 73 | wtu.fillTexture(gl, textures[0], 4, 4, [0, 192, 128, 255], 1); |
| 74 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[0] with POT texture with level > 0 should succeed"); |
| 75 | |
| 76 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 77 | wtu.fillTexture(gl, textures[1], 5, 3, [0, 192, 128, 255], 1); |
| 78 | glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D[1] with POT texture with level > 0 should return INVALID_VALUE"); |
| 79 | |
| 80 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 81 | wtu.fillTexture(gl, textures[2], 4, 4, [0, 192, 128, 255], 1); |
| 82 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[2] with POT texture with level > 0 should succeed"); |
| 83 | |
| 84 | // 3. Check that an NPOT texture on level 0 succeeds (alternating, ending in NPOT texture) |
| 85 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 86 | wtu.fillTexture(gl, textures[0], 5, 3, [0, 192, 128, 255]); |
| 87 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[0] with NPOT texture at level 0 should succeed"); |
| 88 | |
| 89 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 90 | wtu.fillTexture(gl, textures[1], 4, 4, [0, 192, 128, 255]); |
| 91 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[1] with POT texture at level 0 should succeed"); |
| 92 | |
| 93 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 94 | wtu.fillTexture(gl, textures[2], 5, 3, [0, 192, 128, 255]); |
| 95 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[2] with NPOT texture at level 0 should succeed"); |
| 96 | |
| 97 | // 4. Check that an NPOT texture on level 0 succeeds (alternating, ending in POT texture) |
| 98 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 99 | wtu.fillTexture(gl, textures[0], 4, 4, [0, 192, 128, 255]); |
| 100 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[0] with POT texture at level 0 should succeed"); |
| 101 | |
| 102 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 103 | wtu.fillTexture(gl, textures[1], 5, 3, [0, 192, 128, 255]); |
| 104 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[1] with NPOT texture at level 0 should succeed"); |
| 105 | |
| 106 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 107 | wtu.fillTexture(gl, textures[2], 4, 4, [0, 192, 128, 255]); |
| 108 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[2] with POT texture at level 0 should succeed"); |
| 109 | |
| 110 | debug(""); |
| 111 | debug("check using cubemap"); |
| 112 | var program = wtu.setupProgram( |
| 113 | gl, |
| 114 | [wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER), |
| 115 | wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER)], |
| 116 | ['vPosition', 'texCoord0'], [0, 1]); |
| 117 | |
| 118 | // Make 3 textures by using 3 active texture units. |
| 119 | var cubeTextures = []; |
| 120 | for (var ii = 0; ii < 3; ++ii) { |
| 121 | var tex = gl.createTexture(); |
| 122 | gl.activeTexture(gl.TEXTURE0 + ii); |
| 123 | gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex); |
| 124 | cubeTextures[ii] = tex; |
| 125 | } |
| 126 | shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 127 | |
| 128 | // 5. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in NPOT texture) |
| 129 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 130 | fillCubeTexture(gl, cubeTextures[0], 5, 3, [0, 192, 128, 255], 1); |
| 131 | glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE"); |
| 132 | |
| 133 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 134 | fillCubeTexture(gl, cubeTextures[1], 4, 4, [0, 192, 128, 255], 1); |
| 135 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture with level > 0 should succeed"); |
| 136 | |
| 137 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 138 | fillCubeTexture(gl, cubeTextures[2], 5, 3, [0, 192, 128, 255], 1); |
| 139 | glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE"); |
| 140 | |
| 141 | // 6. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in POT texture) |
| 142 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 143 | fillCubeTexture(gl, cubeTextures[0], 4, 4, [0, 192, 128, 255], 1); |
| 144 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture with level > 0 should succeed"); |
| 145 | |
| 146 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 147 | fillCubeTexture(gl, cubeTextures[1], 5, 3, [0, 192, 128, 255], 1); |
| 148 | glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE"); |
| 149 | |
| 150 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 151 | fillCubeTexture(gl, cubeTextures[2], 4, 4, [0, 192, 128, 255], 1); |
| 152 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture with level > 0 should succeed"); |
| 153 | |
| 154 | // 7. Check that an NPOT texture on level 0 succeeds (alternating, ending in NPOT texture) |
| 155 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 156 | fillCubeTexture(gl, cubeTextures[0], 5, 5, [0, 192, 128, 255]); |
| 157 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with NPOT texture at level 0 should succeed"); |
| 158 | |
| 159 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 160 | fillCubeTexture(gl, cubeTextures[1], 4, 4, [0, 192, 128, 255]); |
| 161 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture at level 0 should succeed"); |
| 162 | |
| 163 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 164 | fillCubeTexture(gl, cubeTextures[2], 5, 5, [0, 192, 128, 255]); |
| 165 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with NPOT texture at level 0 should succeed"); |
| 166 | |
| 167 | // 8. Check that an NPOT texture on level 0 succeeds (alternating, ending in POT texture) |
| 168 | gl.activeTexture(gl.TEXTURE0 + 0); |
| 169 | fillCubeTexture(gl, cubeTextures[0], 4, 4, [0, 192, 128, 255]); |
| 170 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture at level 0 should succeed"); |
| 171 | |
| 172 | gl.activeTexture(gl.TEXTURE0 + 1); |
| 173 | fillCubeTexture(gl, cubeTextures[1], 5, 5, [0, 192, 128, 255]); |
| 174 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with NPOT texture at level 0 should succeed"); |
| 175 | |
| 176 | gl.activeTexture(gl.TEXTURE0 + 2); |
| 177 | fillCubeTexture(gl, cubeTextures[2], 4, 4, [0, 192, 128, 255]); |
| 178 | glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture at level 0 should succeed"); |
| 179 | |
| 180 | function fillCubeTexture(gl, tex, width, height, color, opt_level) { |
| 181 | opt_level = opt_level || 0; |
| 182 | var canvas = document.createElement('canvas'); |
| 183 | canvas.width = width; |
| 184 | canvas.height = height; |
| 185 | var ctx2d = canvas.getContext('2d'); |
| 186 | ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")"; |
| 187 | ctx2d.fillRect(0, 0, width, height); |
| 188 | gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex); |
| 189 | var targets = [ |
| 190 | gl.TEXTURE_CUBE_MAP_POSITIVE_X, |
| 191 | gl.TEXTURE_CUBE_MAP_NEGATIVE_X, |
| 192 | gl.TEXTURE_CUBE_MAP_POSITIVE_Y, |
| 193 | gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, |
| 194 | gl.TEXTURE_CUBE_MAP_POSITIVE_Z, |
| 195 | gl.TEXTURE_CUBE_MAP_NEGATIVE_Z]; |
| 196 | for (var tt = 0; tt < targets.length; ++tt) { |
| 197 | gl.texImage2D( |
| 198 | targets[tt], opt_level, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas); |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | </script> |
| 203 | </body> |
| 204 | </html> |
| 205 | |