vertex-id.html (5893B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>WebGL 2 gl_VertexID Tests</title> 6 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 7 <script src="../../js/desktop-gl-constants.js"></script> 8 <script src="../../js/js-test-pre.js"></script> 9 <script src="../../js/webgl-test-utils.js"></script> 10 </head> 11 <body> 12 <div id="description"></div> 13 <div id="console"></div> 14 <!-- Shaders for testing instanced draws --> 15 <script id="vs" type="text/plain"> 16 #version 300 es 17 flat out highp int vVertexID; 18 19 void main() { 20 vVertexID = gl_VertexID; 21 gl_PointSize = 1.0; 22 gl_Position = vec4(0,0,0,1); 23 } 24 </script> 25 <script id="fs" type="text/plain"> 26 #version 300 es 27 flat in highp int vVertexID; 28 out highp int oVertexID; 29 void main() { 30 oVertexID = vVertexID; 31 } 32 </script> 33 34 <script> 35 "use strict"; 36 description("Test gl_VertexID"); 37 38 debug(""); 39 40 const wtu = WebGLTestUtils; 41 const canvas = document.createElement("canvas"); 42 canvas.width = 1; 43 canvas.height = 1; 44 const gl = wtu.create3DContext(canvas, null, 2); 45 46 (function() { 47 if (!gl) { 48 testFailed("WebGL context does not exist"); 49 return; 50 } 51 testPassed("WebGL context exists"); 52 53 const vs = document.getElementById("vs").innerHTML.trim(); 54 const fs = document.getElementById("fs").innerHTML.trim(); 55 const prog = wtu.loadProgram(gl, vs, fs); 56 gl.useProgram(prog); 57 58 const tex = gl.createTexture(); 59 gl.bindTexture(gl.TEXTURE_2D, tex); 60 gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R32I, 1, 1); 61 const fb = gl.createFramebuffer(); 62 gl.bindFramebuffer(gl.FRAMEBUFFER, fb); 63 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); 64 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE"); 65 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors after setup"); 66 67 function shouldBeVal(prefix, expected, was) { 68 let text = prefix + "Should be " + expected; 69 let func = testPassed; 70 if (was != expected) { 71 text = text + ", was " + was; 72 func = testFailed; 73 } 74 func(text); 75 }; 76 77 const readValData = new Int32Array(10000); 78 function ensureVal(prefix, expected) { 79 gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.INT, readValData); 80 const was = readValData[0]; 81 shouldBeVal(prefix, expected, was); 82 }; 83 84 gl.clearBufferiv(gl.COLOR, 0, new Int32Array([42, 0, 0, 0])); 85 ensureVal("After clear", 42); 86 87 // - 88 89 debug(""); 90 debug("----------------"); 91 debug("drawArrays"); 92 93 let test = function(first, count) { 94 debug(""); 95 debug(`drawArrays(first: ${first}, count: ${count})`); 96 gl.drawArrays(gl.POINTS, first, count); 97 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 98 ensureVal("", first+count-1); 99 }; 100 101 test(0, 1); 102 test(1, 1); 103 test(10000, 1); 104 test(100000, 1); 105 test(1000000, 1); 106 107 test(0, 2); 108 test(1, 2); 109 test(10000, 2); 110 test(100000, 2); 111 test(1000000, 2); 112 113 const INT32_MAX = 0x7fffffff; 114 115 test = function(first, count) { 116 debug(""); 117 debug(`drawArrays(first: ${first}, count: ${count})`); 118 gl.drawArrays(gl.POINTS, first, count); 119 if (!wtu.glErrorShouldBe(gl, [gl.NO_ERROR, gl.OUT_OF_MEMORY])) { 120 ensureVal("", first+count-1); 121 } 122 }; 123 124 test(INT32_MAX-2, 1); 125 test(INT32_MAX-1, 1); 126 test(INT32_MAX, 1); 127 128 // - 129 130 debug(""); 131 debug("----------------"); 132 debug("drawElements"); 133 134 const indexBuffer = gl.createBuffer(); 135 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); 136 const indexData = new Uint16Array([1, 2, 5, 3, 10000]); 137 debug("indexData: " + indexData); 138 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexData, gl.STATIC_DRAW); 139 140 test = function(first, count) { 141 debug(""); 142 debug(`drawElements(first: ${first}, count: ${count})`); 143 gl.drawElements(gl.POINTS, count, gl.UNSIGNED_SHORT, first*2); 144 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 145 ensureVal("", indexData[first+count-1]); 146 }; 147 148 for (let f = 0; f < indexData.length; ++f) { 149 for (let c = 1; f + c <= indexData.length; ++c) { 150 test(f, c); 151 } 152 } 153 154 // - 155 156 debug(""); 157 debug("----------------"); 158 debug("Via transform feedback"); 159 160 gl.transformFeedbackVaryings(prog, ["vVertexID"], gl.INTERLEAVED_ATTRIBS); 161 wtu.linkProgram(gl, prog); 162 gl.useProgram(prog); 163 164 const tfBuffer = gl.createBuffer(); 165 gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, tfBuffer); 166 gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, 4*10000, gl.DYNAMIC_READ); 167 168 test = function(offset, count) { 169 debug(""); 170 debug("drawArrays(" + offset + ", " + count + ")"); 171 gl.beginTransformFeedback(gl.POINTS); 172 gl.drawArrays(gl.POINTS, offset, count); 173 gl.endTransformFeedback(); 174 gl.getBufferSubData(gl.TRANSFORM_FEEDBACK_BUFFER, 0, readValData); 175 let ok = true; 176 for (let i = 0; i < readValData.length; i++) { 177 if (i >= count) 178 break; 179 const expected = offset + i; 180 const was = readValData[i]; 181 if (was != expected) { 182 testFailed("[" + i + "] expected " + expected + ", was " + was); 183 ok = false; 184 break; 185 } 186 } 187 if (ok) { 188 testPassed("ok"); 189 } 190 }; 191 192 test(0, 1); 193 test(1, 1); 194 test(10000, 1); 195 196 test(0, 2); 197 test(1, 2); 198 test(10000, 2); 199 200 test(10000, 10000); 201 202 // - 203 204 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "There should be no remaining errors"); 205 })(); 206 207 debug(""); 208 var successfullyParsed = true; 209 </script> 210 <script src="../../js/js-test-post.js"></script> 211 212 </body> 213 </html> 214 215 <!-- 216 Copyright (c) 2019 The Khronos Group Inc. 217 Use of this source code is governed by an MIT-style license that can be 218 found in the LICENSE.txt file. 219 -->