OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 <!DOCTYPE html> |
| 7 <html> |
| 8 <head> |
| 9 <meta charset="utf-8"> |
| 10 <title>WebGL texture mips conformance test.</title> |
| 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 12 <script src="../../resources/js-test-pre.js"></script> |
| 13 <script src="../resources/webgl-test.js"> </script> |
| 14 <script src="../resources/webgl-test-utils.js"></script> |
| 15 </head> |
| 16 <body> |
| 17 <canvas id="example" width="2" height="2" style="width: 40px; height: 40px;"></c
anvas> |
| 18 <div id="description"></div> |
| 19 <div id="console"></div> |
| 20 <script id="vshader" type="x-shader/x-vertex"> |
| 21 uniform vec4 uMult; |
| 22 attribute vec4 vPosition; |
| 23 attribute vec2 texCoord0; |
| 24 varying vec2 texCoord; |
| 25 void main() |
| 26 { |
| 27 gl_Position = vPosition * uMult; |
| 28 texCoord = texCoord0; |
| 29 } |
| 30 </script> |
| 31 |
| 32 <script id="fshader" type="x-shader/x-fragment"> |
| 33 precision mediump float; |
| 34 uniform sampler2D tex; |
| 35 varying vec2 texCoord; |
| 36 void main() |
| 37 { |
| 38 gl_FragColor = texture2D(tex, texCoord); |
| 39 } |
| 40 </script> |
| 41 <script> |
| 42 var canvas; |
| 43 var wtu = WebGLTestUtils; |
| 44 function init() |
| 45 { |
| 46 if (window.initNonKhronosFramework) { |
| 47 window.initNonKhronosFramework(false); |
| 48 } |
| 49 |
| 50 description("Checks mip issues"); |
| 51 |
| 52 canvas = document.getElementById("example"); |
| 53 shouldBe("canvas.width", "2"); |
| 54 shouldBe("canvas.height", "2"); |
| 55 |
| 56 gl = wtu.create3DContext(canvas); |
| 57 wtu.setupUnitQuad(gl, 0, 1); |
| 58 var program = wtu.setupProgram( |
| 59 gl, |
| 60 [wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER), |
| 61 wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER)], |
| 62 ['vPosition', 'texCoord0'], [0, 1]); |
| 63 |
| 64 gl.disable(gl.DEPTH_TEST); |
| 65 gl.disable(gl.BLEND); |
| 66 |
| 67 var colors = { |
| 68 blue: [0, 0, 255, 255], |
| 69 red: [255, 0, 0, 255], |
| 70 green: [0, 255, 0, 255], |
| 71 cyan: [128, 255, 255, 255], |
| 72 black: [0, 0, 0, 255] |
| 73 }; |
| 74 |
| 75 var mips = [ |
| 76 ]; |
| 77 |
| 78 var texLoc = gl.getUniformLocation(program, "tex"); |
| 79 gl.uniform1i(texLoc, 0); |
| 80 var multLoc = gl.getUniformLocation(program, "uMult"); |
| 81 |
| 82 // ---------------------------------------------------- |
| 83 var tex = createTexture(); |
| 84 gl.uniform4f(multLoc, 1, 1, 1, 1); |
| 85 |
| 86 gl.bindTexture(gl.TEXTURE_2D, tex); |
| 87 // 16x16 texture no mips |
| 88 fillLevel(tex, 0, 16, 'cyan'); |
| 89 |
| 90 check('black', |
| 91 "texture that is missing mips when TEXTURE_MIN_FILTER not NEAREST or LIN
EAR"); |
| 92 |
| 93 generateMipmap(); |
| 94 |
| 95 check('cyan', "texture that has all mips"); |
| 96 |
| 97 // Fill in the bottom 2 mips with a different color. |
| 98 fillLevel(tex, 4, 1, 'green'); |
| 99 fillLevel(tex, 3, 2, 'green'); |
| 100 |
| 101 // Choose the nearest mip |
| 102 texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); |
| 103 |
| 104 check('green', "texture that is only using the smallest 2 mips"); |
| 105 |
| 106 gl.uniform4f(multLoc, 16, 16, 1, 1); |
| 107 |
| 108 check('cyan', "texture that is using only the largest 2 mips"); |
| 109 |
| 110 // Set the top level |
| 111 fillLevel(tex, 0, 1, 'red'); |
| 112 check('red', |
| 113 "texture that is only using the top level even though other levels are d
efined"); |
| 114 |
| 115 // Set the top 2 levels using generateMipmap |
| 116 fillLevel(tex, 0, 2, 'blue'); |
| 117 generateMipmap(); |
| 118 |
| 119 check('blue', |
| 120 "texture that is only using the top 2 levels even though other levels ar
e defined"); |
| 121 |
| 122 // Set the top 2 levels back to sizes that end up using levels 2, 3, and 4 aga
in. |
| 123 fillLevel(tex, 0, 16, 'blue'); |
| 124 fillLevel(tex, 1, 8, 'blue'); |
| 125 check('blue', "texture that is only using the largest 2 mips"); |
| 126 gl.uniform4f(multLoc, 1, 1, 1, 1); |
| 127 check('green', "texture that is only using the smallest 2 mips"); |
| 128 |
| 129 // ---------------------------------------------------- |
| 130 var tex = createTexture(); |
| 131 gl.uniform4f(multLoc, 1, 1, 1, 1); |
| 132 fillLevel(tex, 0, 8, 'cyan'); |
| 133 generateMipmap(); |
| 134 check('cyan', "texture that has 3 mips"); |
| 135 |
| 136 fillLevel(tex, 0, 16, 'blue'); |
| 137 texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| 138 check('blue', "texture that is only using top mips"); |
| 139 |
| 140 fillLevel(tex, 0, 8, 'red'); |
| 141 texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); |
| 142 check('cyan', "texture that is only using smallest mips"); |
| 143 |
| 144 gl.uniform4f(multLoc, 16, 16, 1, 1); |
| 145 check('red', "texture that is using only the largest mip"); |
| 146 |
| 147 // ---------------------------------------------------- |
| 148 var tex = createTexture(); |
| 149 gl.uniform4f(multLoc, 1, 1, 1, 1); |
| 150 fillLevel(tex, 2, 1, 'green'); |
| 151 fillLevel(tex, 1, 2, 'green'); |
| 152 fillLevel(tex, 0, 4, 'green'); |
| 153 check('green', "texture that was built smallest mip first"); |
| 154 |
| 155 // ---------------------------------------------------- |
| 156 var tex = createTexture(); |
| 157 gl.uniform4f(multLoc, 1, 1, 1, 1); |
| 158 fillLevel(tex, 0, 16, 'red'); |
| 159 generateMipmap(); |
| 160 check('red', "texture with 1 genmipmaps"); |
| 161 fillLevel(tex, 0, 16, 'blue'); |
| 162 generateMipmap(); |
| 163 fillLevel(tex, 0, 16, 'green'); |
| 164 generateMipmap(); |
| 165 check('green', "texture with 2 genmipmaps"); |
| 166 |
| 167 // ---------------------------------------------------- |
| 168 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors."); |
| 169 |
| 170 function createTexture() { |
| 171 debug("<hr/>gl.createTexture()"); |
| 172 mips = []; |
| 173 makeDivMipChain(); |
| 174 return gl.createTexture(); |
| 175 } |
| 176 |
| 177 function texParameteri(target, pname, value) { |
| 178 debug("gl.texParameteri(" + |
| 179 wtu.glEnumToString(gl, target) + ", " + |
| 180 wtu.glEnumToString(gl, pname) + ", " + |
| 181 wtu.glEnumToString(gl, value) + ")") |
| 182 gl.texParameteri(target, pname, value); |
| 183 } |
| 184 |
| 185 function generateMipmap() { |
| 186 debug("gl.genreateMipmap(gl.TEXTURE_2D)"); |
| 187 gl.generateMipmap(gl.TEXTURE_2D); |
| 188 var mip0 = mips[0]; |
| 189 var size = mip0.size; |
| 190 var level = 1; |
| 191 for(;;) { |
| 192 size = Math.floor(size / 2); |
| 193 if (!size) { |
| 194 break; |
| 195 } |
| 196 setMipData(level, size, mip0.color); |
| 197 ++level; |
| 198 } |
| 199 makeDivMipChain(); |
| 200 } |
| 201 |
| 202 function check(color, msg) { |
| 203 wtu.drawQuad(gl); |
| 204 wtu.checkCanvas(gl, colors[color], msg + " should draw with " + color); |
| 205 } |
| 206 |
| 207 function fillLevel(tex, level, size, color) { |
| 208 setMipData(level, size, color); |
| 209 debug("gl.texImage2D(gl.TEXTURE_2D, " + level + ", gl.RGBA, " + size + ", "
+ size + |
| 210 ", 0, gl.RGBA, gl.UNSIGNED_BYTE, " + color + ");"); |
| 211 wtu.fillTexture(gl, tex, size, size, colors[color], level); |
| 212 makeDivMipChain(); |
| 213 } |
| 214 |
| 215 function setMipData(level, size, color) { |
| 216 mips[level] = { |
| 217 size: size, |
| 218 color: color |
| 219 }; |
| 220 } |
| 221 |
| 222 function makeDivMipChain(color) { |
| 223 var html = [ |
| 224 '<div style="height: 68px; margin-top: 5px">', |
| 225 '<div style="float:left;">mips: </div>']; |
| 226 for (var ii = 0; ii < 5; ++ii) { |
| 227 var mip = mips[ii]; |
| 228 if (mip) { |
| 229 html.push(makeDivSquare(mip.size, mip.color)); |
| 230 } else { |
| 231 html.push(makeDivSquare(16, undefined)); |
| 232 } |
| 233 } |
| 234 html.push("</div>"); |
| 235 debug(html.join("")); |
| 236 } |
| 237 |
| 238 function makeDivSquare(size, color) { |
| 239 size *= 4; |
| 240 var c = color ? colors[color] : [255,255,255]; |
| 241 var border = color ? 'solid' : 'dashed'; |
| 242 return '<div style="float:left; width: ' + size + 'px; height: ' + size + |
| 243 'px; background-color: ' + rgb(c) + |
| 244 '; border: 1px ' + border + ' black; margin-right: 3px;"></div>'; |
| 245 } |
| 246 |
| 247 function rgb(c) { |
| 248 return 'rgb(' + c[0] + ',' + c[1] + ',' + c[2] +')'; |
| 249 } |
| 250 } |
| 251 |
| 252 init(); |
| 253 successfullyParsed = true; |
| 254 </script> |
| 255 |
| 256 <script src="../../resources/js-test-post.js"></script> |
| 257 </body> |
| 258 </html> |
| 259 |
OLD | NEW |