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>
12 <div id=
"description"></div>
13 <div id=
"console"></div>
14 <!-- Shaders for testing instanced draws -->
15 <script id=
"vs" type=
"text/plain">
17 flat out highp int vVertexID;
20 vVertexID = gl_VertexID;
22 gl_Position = vec4(
0,
0,
0,
1);
25 <script id=
"fs" type=
"text/plain">
27 flat in highp int vVertexID;
28 out highp int oVertexID;
30 oVertexID = vVertexID;
36 description("Test gl_VertexID");
40 const wtu
= WebGLTestUtils
;
41 const canvas
= document
.createElement("canvas");
44 const gl
= wtu
.create3DContext(canvas
, null, 2);
48 testFailed("WebGL context does not exist");
51 testPassed("WebGL context exists");
53 const vs
= document
.getElementById("vs").innerHTML
.trim();
54 const fs
= document
.getElementById("fs").innerHTML
.trim();
55 const prog
= wtu
.loadProgram(gl
, vs
, fs
);
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");
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
;
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
);
84 gl
.clearBufferiv(gl
.COLOR
, 0, new Int32Array([42, 0, 0, 0]));
85 ensureVal("After clear", 42);
90 debug("----------------");
93 let test = function(first
, count
) {
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);
113 const INT32_MAX
= 0x7fffffff;
115 test = function(first
, count
) {
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);
124 test(INT32_MAX
-2, 1);
125 test(INT32_MAX
-1, 1);
131 debug("----------------");
132 debug("drawElements");
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
);
140 test = function(first
, count
) {
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]);
148 for (let f
= 0; f
< indexData
.length
; ++f
) {
149 for (let c
= 1; f
+ c
<= indexData
.length
; ++c
) {
157 debug("----------------");
158 debug("Via transform feedback");
160 gl
.transformFeedbackVaryings(prog
, ["vVertexID"], gl
.INTERLEAVED_ATTRIBS
);
161 wtu
.linkProgram(gl
, prog
);
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
);
168 test = function(offset
, count
) {
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
);
176 for (let i
= 0; i
< readValData
.length
; i
++) {
179 const expected
= offset
+ i
;
180 const was
= readValData
[i
];
181 if (was
!= expected
) {
182 testFailed("[" + i
+ "] expected " + expected
+ ", was " + was
);
204 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "There should be no remaining errors");
208 var successfullyParsed
= true;
210 <script src=
"../../js/js-test-post.js"></script>
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.