2 <title>WebGL test: Drawing without attrib arrays
</title>
3 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
4 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
5 <script src=
"driver-info.js"></script>
6 <script src=
"webgl-util.js"></script>
7 <script id=
"vs-no-attrib" type=
"x-shader/x-vertex">
11 gl_Position = vec4(vec3(
0.0),
1.0);
15 <script id=
"vs-attrib" type=
"x-shader/x-vertex">
17 attribute vec3 aPosition;
21 gl_Position = vec4(aPosition,
1.0);
25 <script id=
"fs" type=
"x-shader/x-fragment">
27 precision mediump float;
30 gl_FragColor = vec4(
1.0,
1.0,
1.0,
1.0);
35 <canvas id=
"c" width=
"64" height=
"64"></canvas>
38 // Give ourselves a scope to return early from:
40 var gl
= c
.getContext('webgl');
42 todo(false, 'WebGL is unavailable.');
46 DriverInfo
.dump(x
=> ok(true, x
));
48 var attribProg
= WebGLUtil
.createProgramByIds(gl
, 'vs-attrib', 'fs');
49 var noAttribProg
= WebGLUtil
.createProgramByIds(gl
, 'vs-no-attrib', 'fs');
50 if (!attribProg
|| !noAttribProg
) {
51 ok(false, 'Program linking should succeed.');
55 attribProg
.aPosition
= gl
.getAttribLocation(attribProg
, "aPosition");
56 ok(attribProg
.aPosition
>= 0, '`aPosition` should be valid.');
58 function isScreenBlack() {
59 var pixels
= gl
.drawingBufferWidth
* gl
.drawingBufferHeight
;
60 var data
= new Uint8Array(4 * pixels
);
62 gl
.drawingBufferWidth
, gl
.drawingBufferHeight
,
63 gl
.RGBA
, gl
.UNSIGNED_BYTE
,
67 for (var i
= 0; i
< pixels
; i
++) {
68 accum
+= data
[4*i
+ 0];
69 accum
+= data
[4*i
+ 1];
70 accum
+= data
[4*i
+ 2];
76 function checkGLError(func
, info
) {
77 var error
= gl
.getError();
78 func(!error
, '[' + info
+ '] gl.getError should be 0, was 0x' + error
.toString(16) + '.');
81 function testDrawing(info
) {
82 var cruelNumber
= 1024*1024;
83 // Really, we should test for INT32_MAX-1 here, but we don't gracefully chunk these calls,
84 // and so try to create a VBO of size INT32_MAX-1 to pretend that vert attrib 0 is an array.
85 // (INT32_MAX-1 because we check that `first+count` is a valid GLsizei, which is int32_t)
86 var UINT16_MAX
= 0xffff;
87 var INT32_MAX
= 0x7fffffff;
88 var UINT32_MAX
= 0xffffffff;
90 // `first` needs room for `first+count` <= sizeof(GLsizei) == INT32_MAX
91 var hugeFirst
= Math
.min(cruelNumber
, INT32_MAX
-1);
92 var hugeIndex
= Math
.min(cruelNumber
, UINT32_MAX
);
94 var indexType
= gl
.UNSIGNED_SHORT
;
96 var indexArr
= new Uint16Array([0, 1, Math
.min(hugeIndex
, UINT16_MAX
)]);
97 if (gl
.getExtension('OES_element_index_uint')) {
98 indexType
= gl
.UNSIGNED_INT
;
100 indexArr
= new Uint32Array([0, 1, hugeIndex
]);
103 gl
.clear(gl
.COLOR_BUFFER_BIT
);
104 gl
.drawArrays(gl
.POINTS
, 0, 1);
105 ok(!isScreenBlack(), '[' + info
+ '] drawArrays should color pixels.');
107 gl
.clear(gl
.COLOR_BUFFER_BIT
);
108 gl
.drawArrays(gl
.POINTS
, hugeFirst
, 1);
109 ok(!isScreenBlack(), '[' + info
+ '] drawArrays[huge first] should color pixels.');
111 checkGLError(ok
, info
);
113 var elemTestFunc
= ok
;
114 var checkGLTestFunc
= ok
;
115 if (DriverInfo
.getDriver() == DriverInfo
.DRIVER
.ANDROID_X86_EMULATOR
) {
116 // ...but the Android 4.2 x86 emulator environment is different
118 checkGLTestFunc
= ok
;
121 // Now for drawElements:
122 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, gl
.createBuffer());
123 gl
.bufferData(gl
.ELEMENT_ARRAY_BUFFER
, indexArr
, gl
.STATIC_DRAW
);
125 gl
.clear(gl
.COLOR_BUFFER_BIT
);
126 gl
.drawElements(gl
.POINTS
, 1, indexType
, 0);
127 elemTestFunc(!isScreenBlack(), '[' + info
+ '] drawElements[0] should color pixels.');
129 gl
.clear(gl
.COLOR_BUFFER_BIT
);
130 gl
.drawElements(gl
.POINTS
, 1, indexType
, 1*indexStride
);
131 elemTestFunc(!isScreenBlack(), '[' + info
+ '] drawElements[1] should color pixels.');
133 gl
.clear(gl
.COLOR_BUFFER_BIT
);
134 gl
.drawElements(gl
.POINTS
, 1, indexType
, 2*indexStride
);
135 elemTestFunc(!isScreenBlack(), '[' + info
+ '] drawElements[huge offset] should color pixels.');
137 checkGLError(checkGLTestFunc
, info
);
141 gl
.clearColor(0.0, 0.0, 0.0, 1.0);
142 gl
.disable(gl
.DEPTH_TEST
);
145 gl
.useProgram(noAttribProg
);
146 testDrawing('no-attrib');
148 // One-attrib, no-array prog:
149 gl
.useProgram(attribProg
);
150 gl
.disableVertexAttribArray(attribProg
.aPosition
);
151 gl
.vertexAttrib3fv(attribProg
.aPosition
, [0.0, 0.0, 0.0]);
152 testDrawing('one-attrib, no-array');