|
| 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 int vVertexID; |
| 18 | + |
| 19 | +void main() { |
| 20 | + vVertexID = gl_VertexID; |
| 21 | + gl_Position = vec4(0,0,0,1); |
| 22 | +} |
| 23 | +</script> |
| 24 | +<script id="fs" type="text/plain"> |
| 25 | +#version 300 es |
| 26 | +flat in int vVertexID; |
| 27 | +out int oVertexID; |
| 28 | +void main() { |
| 29 | + oVertexID = vVertexID; |
| 30 | +} |
| 31 | +</script> |
| 32 | + |
| 33 | +<script> |
| 34 | +"use strict"; |
| 35 | +description("Test gl_VertexID"); |
| 36 | + |
| 37 | +debug(""); |
| 38 | + |
| 39 | +const wtu = WebGLTestUtils; |
| 40 | +const canvas = document.createElement("canvas"); |
| 41 | +canvas.width = 1; |
| 42 | +canvas.height = 1; |
| 43 | +const gl = wtu.create3DContext(canvas, null, 2); |
| 44 | + |
| 45 | +(function() { |
| 46 | + if (!gl) { |
| 47 | + testFailed("WebGL context does not exist"); |
| 48 | + return; |
| 49 | + } |
| 50 | + testPassed("WebGL context exists"); |
| 51 | + |
| 52 | + const vs = document.getElementById("vs").innerHTML.trim(); |
| 53 | + const fs = document.getElementById("fs").innerHTML.trim(); |
| 54 | + const prog = wtu.loadProgram(gl, vs, fs); |
| 55 | + gl.useProgram(prog); |
| 56 | + |
| 57 | + const tex = gl.createTexture(); |
| 58 | + gl.bindTexture(gl.TEXTURE_2D, tex); |
| 59 | + gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R32I, 1, 1); |
| 60 | + const fb = gl.createFramebuffer(); |
| 61 | + gl.bindFramebuffer(gl.FRAMEBUFFER, fb); |
| 62 | + gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); |
| 63 | + shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE"); |
| 64 | + wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors after setup"); |
| 65 | + |
| 66 | + function shouldBeVal(prefix, expected, was) { |
| 67 | + let text = prefix + "Should be " + expected; |
| 68 | + let func = testPassed; |
| 69 | + if (was != expected) { |
| 70 | + text = text + ", was " + was; |
| 71 | + func = testFailed; |
| 72 | + } |
| 73 | + func(text); |
| 74 | + }; |
| 75 | + |
| 76 | + const readValData = new Int32Array(10000); |
| 77 | + function ensureVal(prefix, expected) { |
| 78 | + gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.INT, readValData); |
| 79 | + const was = readValData[0]; |
| 80 | + shouldBeVal(prefix, expected, was); |
| 81 | + }; |
| 82 | + |
| 83 | + gl.clearBufferiv(gl.COLOR, 0, new Int32Array([42, 0, 0, 0])); |
| 84 | + ensureVal("After clear", 42); |
| 85 | + |
| 86 | + // - |
| 87 | + |
| 88 | + debug(""); |
| 89 | + debug("----------------"); |
| 90 | + debug("drawArrays"); |
| 91 | + |
| 92 | + let test = function(first, count) { |
| 93 | + debug(""); |
| 94 | + debug(`drawArrays(first: ${first}, count: ${count})`); |
| 95 | + gl.drawArrays(gl.POINTS, first, count); |
| 96 | + wtu.glErrorShouldBe(gl, gl.NO_ERROR); |
| 97 | + ensureVal("", first+count-1); |
| 98 | + }; |
| 99 | + |
| 100 | + test(0, 1); |
| 101 | + test(1, 1); |
| 102 | + test(10000, 1); |
| 103 | + test(100000, 1); |
| 104 | + test(1000000, 1); |
| 105 | + |
| 106 | + test(0, 2); |
| 107 | + test(1, 2); |
| 108 | + test(10000, 2); |
| 109 | + test(100000, 2); |
| 110 | + test(1000000, 2); |
| 111 | + |
| 112 | + const INT32_MAX = 0x7fffffff; |
| 113 | + |
| 114 | + test = function(first, count) { |
| 115 | + debug(""); |
| 116 | + debug(`drawArrays(first: ${first}, count: ${count})`); |
| 117 | + gl.drawArrays(gl.POINTS, first, count); |
| 118 | + if (!wtu.glErrorShouldBe(gl, [gl.NO_ERROR, gl.OUT_OF_MEMORY])) { |
| 119 | + ensureVal("", first+count-1); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + test(INT32_MAX-2, 1); |
| 124 | + test(INT32_MAX-1, 1); |
| 125 | + test(INT32_MAX, 1); |
| 126 | + |
| 127 | + // - |
| 128 | + |
| 129 | + debug(""); |
| 130 | + debug("----------------"); |
| 131 | + debug("drawElements"); |
| 132 | + |
| 133 | + const indexBuffer = gl.createBuffer(); |
| 134 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); |
| 135 | + const indexData = new Uint16Array([1, 2, 5, 3, 10000]); |
| 136 | + debug("indexData: " + indexData); |
| 137 | + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexData, gl.STATIC_DRAW); |
| 138 | + |
| 139 | + test = function(first, count) { |
| 140 | + debug(""); |
| 141 | + debug(`drawElements(first: ${first}, count: ${count})`); |
| 142 | + gl.drawElements(gl.POINTS, count, gl.UNSIGNED_SHORT, first*2); |
| 143 | + wtu.glErrorShouldBe(gl, gl.NO_ERROR); |
| 144 | + ensureVal("", indexData[first+count-1]); |
| 145 | + }; |
| 146 | + |
| 147 | + for (let f = 0; f < indexData.length; ++f) { |
| 148 | + for (let c = 1; f + c <= indexData.length; ++c) { |
| 149 | + test(f, c); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // - |
| 154 | + |
| 155 | + debug(""); |
| 156 | + debug("----------------"); |
| 157 | + debug("Via transform feedback"); |
| 158 | + |
| 159 | + gl.transformFeedbackVaryings(prog, ["vVertexID"], gl.INTERLEAVED_ATTRIBS); |
| 160 | + wtu.linkProgram(gl, prog); |
| 161 | + gl.useProgram(prog); |
| 162 | + |
| 163 | + const tfBuffer = gl.createBuffer(); |
| 164 | + gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, tfBuffer); |
| 165 | + gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, 4*10000, gl.DYNAMIC_READ); |
| 166 | + |
| 167 | + test = function(offset, count) { |
| 168 | + debug(""); |
| 169 | + debug("drawArrays(" + offset + ", " + count + ")"); |
| 170 | + gl.beginTransformFeedback(gl.POINTS); |
| 171 | + gl.drawArrays(gl.POINTS, offset, count); |
| 172 | + gl.endTransformFeedback(); |
| 173 | + gl.getBufferSubData(gl.TRANSFORM_FEEDBACK_BUFFER, 0, readValData); |
| 174 | + let ok = true; |
| 175 | + for (let i = 0; i < readValData.length; i++) { |
| 176 | + if (i >= count) |
| 177 | + break; |
| 178 | + const expected = offset + i; |
| 179 | + const was = readValData[i]; |
| 180 | + if (was != expected) { |
| 181 | + testFailed("[" + i + "] expected " + expected + ", was " + was); |
| 182 | + ok = false; |
| 183 | + break; |
| 184 | + } |
| 185 | + } |
| 186 | + if (ok) { |
| 187 | + testPassed("ok"); |
| 188 | + } |
| 189 | + }; |
| 190 | + |
| 191 | + test(0, 1); |
| 192 | + test(1, 1); |
| 193 | + test(10000, 1); |
| 194 | + |
| 195 | + test(0, 2); |
| 196 | + test(1, 2); |
| 197 | + test(10000, 2); |
| 198 | + |
| 199 | + test(10000, 10000); |
| 200 | + |
| 201 | + // - |
| 202 | + |
| 203 | + wtu.glErrorShouldBe(gl, gl.NO_ERROR, "There should be no remaining errors"); |
| 204 | +})(); |
| 205 | + |
| 206 | +debug(""); |
| 207 | +var successfullyParsed = true; |
| 208 | +</script> |
| 209 | +<script src="../../js/js-test-post.js"></script> |
| 210 | + |
| 211 | +</body> |
| 212 | +</html> |
| 213 | + |
| 214 | +<!-- |
| 215 | +
|
| 216 | +/* |
| 217 | +** Copyright (c) 2013 The Khronos Group Inc. |
| 218 | +** |
| 219 | +** Permission is hereby granted, free of charge, to any person obtaining a |
| 220 | +** copy of this software and/or associated documentation files (the |
| 221 | +** "Materials"), to deal in the Materials without restriction, including |
| 222 | +** without limitation the rights to use, copy, modify, merge, publish, |
| 223 | +** distribute, sublicense, and/or sell copies of the Materials, and to |
| 224 | +** permit persons to whom the Materials are furnished to do so, subject to |
| 225 | +** the following conditions: |
| 226 | +** |
| 227 | +** The above copyright notice and this permission notice shall be included |
| 228 | +** in all copies or substantial portions of the Materials. |
| 229 | +** |
| 230 | +** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 231 | +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 232 | +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 233 | +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 234 | +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 235 | +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 236 | +** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 237 | +*/ |
| 238 | +
|
| 239 | +--> |
0 commit comments