|
1 <!-- |
|
2 Copyright (C) 2011 Apple Computer, Inc. All rights reserved. |
|
3 |
|
4 Redistribution and use in source and binary forms, with or without |
|
5 modification, are permitted provided that the following conditions |
|
6 are met: |
|
7 1. Redistributions of source code must retain the above copyright |
|
8 notice, this list of conditions and the following disclaimer. |
|
9 2. Redistributions in binary form must reproduce the above copyright |
|
10 notice, this list of conditions and the following disclaimer in the |
|
11 documentation and/or other materials provided with the distribution. |
|
12 |
|
13 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
14 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 --> |
|
25 |
|
26 <!DOCTYPE html> |
|
27 <html> |
|
28 <head> |
|
29 <meta charset="utf-8"> |
|
30 <title>vertexattribpointer offsets test</title> |
|
31 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
|
32 <script src="../../resources/js-test-pre.js"></script> |
|
33 <script src="../resources/webgl-test.js"> </script> |
|
34 <script src="../resources/webgl-test-utils.js"> </script> |
|
35 </head> |
|
36 <body> |
|
37 <canvas id="example" width="50" height="50"> |
|
38 There is supposed to be an example drawing here, but it's not important. |
|
39 </canvas> |
|
40 <div id="description"></div> |
|
41 <div id="console"></div> |
|
42 <script id="vshader" type="x-shader/x-vertex"> |
|
43 attribute vec4 vPosition; |
|
44 void main() |
|
45 { |
|
46 gl_Position = vPosition; |
|
47 } |
|
48 </script> |
|
49 |
|
50 <script id="fshader" type="x-shader/x-fragment"> |
|
51 precision mediump float; |
|
52 uniform vec4 color; |
|
53 void main() |
|
54 { |
|
55 gl_FragColor = color; |
|
56 } |
|
57 </script> |
|
58 |
|
59 <script> |
|
60 function fail(x,y, buf, shouldBe) |
|
61 { |
|
62 var i = (y*50+x) * 4; |
|
63 var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe; |
|
64 testFailed(reason); |
|
65 } |
|
66 |
|
67 function pass() |
|
68 { |
|
69 testPassed("drawing is correct"); |
|
70 } |
|
71 |
|
72 function init() |
|
73 { |
|
74 if (window.initNonKhronosFramework) { |
|
75 window.initNonKhronosFramework(false); |
|
76 } |
|
77 description("test vertexattribpointer offsets work"); |
|
78 |
|
79 wtu = WebGLTestUtils; |
|
80 gl = initWebGL("example", "vshader", "fshader", [ "vPosition"], [ 0, 0, 0, 1 ], 1); |
|
81 |
|
82 var tests = [ |
|
83 { data: new Float32Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]), |
|
84 type: gl.FLOAT, |
|
85 componentSize: 4, |
|
86 normalize: false, |
|
87 }, |
|
88 { data: new Float32Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]), |
|
89 type: gl.FLOAT, |
|
90 componentSize: 4, |
|
91 normalize: false, |
|
92 }, |
|
93 { data: new Uint16Array([ 0, 32767, 0, 32767, 0, 0, 0, 0, 0 ]), |
|
94 type: gl.SHORT, |
|
95 componentSize: 2, |
|
96 normalize: true, |
|
97 }, |
|
98 { data: new Uint16Array([ 0, 65535, 0, 65535, 0, 0, 0, 0, 0 ]), |
|
99 type: gl.UNSIGNED_SHORT, |
|
100 componentSize: 2, |
|
101 normalize: true, |
|
102 }, |
|
103 { data: new Uint16Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]), |
|
104 type: gl.UNSIGNED_SHORT, |
|
105 componentSize: 2, |
|
106 normalize: false, |
|
107 }, |
|
108 { data: new Uint16Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]), |
|
109 type: gl.SHORT, |
|
110 componentSize: 2, |
|
111 normalize: false, |
|
112 }, |
|
113 { data: new Uint8Array([ 0, 127, 0, 127, 0, 0, 0, 0, 0 ]), |
|
114 type: gl.BYTE, |
|
115 componentSize: 1, |
|
116 normalize: true, |
|
117 }, |
|
118 { data: new Uint8Array([ 0, 255, 0, 255, 0, 0, 0, 0, 0 ]), |
|
119 type: gl.UNSIGNED_BYTE, |
|
120 componentSize: 1, |
|
121 normalize: true, |
|
122 }, |
|
123 { data: new Uint8Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]), |
|
124 type: gl.BYTE, |
|
125 componentSize: 1, |
|
126 normalize: false, |
|
127 }, |
|
128 { data: new Uint8Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]), |
|
129 type: gl.UNSIGNED_BYTE, |
|
130 componentSize: 1, |
|
131 normalize: false, |
|
132 } |
|
133 ]; |
|
134 |
|
135 var vertexObject = gl.createBuffer(); |
|
136 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
|
137 gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW); |
|
138 gl.enableVertexAttribArray(0); |
|
139 |
|
140 var colorLoc = gl.getUniformLocation(gl.program, "color"); |
|
141 var kNumVerts = 3; |
|
142 var kNumComponents = 3; |
|
143 |
|
144 var count = 0; |
|
145 for (var tt = 0; tt < tests.length; ++tt) { |
|
146 var test = tests[tt]; |
|
147 for (var oo = 0; oo < 3; ++oo) { |
|
148 for (var ss = 0; ss < 3; ++ss) { |
|
149 var offset = (oo + 1) * test.componentSize; |
|
150 var color = (count % 2) ? [1, 0, 0, 1] : [0, 1, 0, 1]; |
|
151 var stride = test.componentSize * kNumComponents + test.componentSize * ss; |
|
152 debug(""); |
|
153 debug("check with " + wtu.glEnumToString(gl, test.type) + " at offset: " + offset + " with stride:" + stride + " normalize: " + test.normalize); |
|
154 gl.uniform4fv(colorLoc, color); |
|
155 var data = new Uint8Array(test.componentSize * kNumVerts * kNumComponents + stride * (kNumVerts - 1)); |
|
156 var view = new Uint8Array(test.data.buffer); |
|
157 var size = test.componentSize * kNumComponents; |
|
158 for (var jj = 0; jj < kNumVerts; ++jj) { |
|
159 var off1 = jj * size; |
|
160 var off2 = jj * stride; |
|
161 for (var zz = 0; zz < size; ++zz) { |
|
162 data[off2 + zz] = view[off1 + zz]; |
|
163 } |
|
164 } |
|
165 gl.bufferSubData(gl.ARRAY_BUFFER, offset, data); |
|
166 gl.vertexAttribPointer(0, 3, test.type, test.normalize, stride, offset); |
|
167 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
|
168 gl.drawArrays(gl.TRIANGLES, 0, 3); |
|
169 |
|
170 var buf = new Uint8Array(50 * 50 * 4); |
|
171 gl.readPixels(0, 0, 50, 50, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
|
172 |
|
173 var black = [0, 0, 0, 255]; |
|
174 var other = [color[0] * 255, color[1] * 255, color[2] * 255, color[3] * 255]; |
|
175 var otherMsg = "should be " + ((count % 2) ? "red" : "green") |
|
176 wtu.checkCanvasRect(gl, 0, 0, 1, 1, black, "should be black", 0); |
|
177 wtu.checkCanvasRect(gl, 0, 49, 1, 1, black, "should be black", 0); |
|
178 wtu.checkCanvasRect(gl, 26, 40, 1, 1, other, otherMsg, 0); |
|
179 wtu.checkCanvasRect(gl, 26, 27, 1, 1, other, otherMsg, 0); |
|
180 wtu.checkCanvasRect(gl, 40, 27, 1, 1, other, otherMsg, 0); |
|
181 ++count; |
|
182 } |
|
183 } |
|
184 } |
|
185 } |
|
186 |
|
187 init(); |
|
188 successfullyParsed = true; |
|
189 </script> |
|
190 <script>finishTest();</script> |
|
191 |
|
192 </body> |
|
193 </html> |