1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
** Copyright (c) 2015 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
function generateTest(internalFormat, pixelFormat, pixelType, prologue, resourcePath, defaultContextVersion) {
var wtu = WebGLTestUtils;
var tiu = TexImageUtils;
var gl = null;
var successfullyParsed = false;
var redColor = [255, 0, 0];
var greenColor = [0, 255, 0];
function init()
{
description('Verify texImage3D and texSubImage3D code paths taking webgl canvas elements (' + internalFormat + '/' + pixelFormat + '/' + pixelType + ')');
// Set the default context version while still allowing the webglVersion URL query string to override it.
wtu.setDefault3DContextVersion(defaultContextVersion);
gl = wtu.create3DContext("example");
if (!prologue(gl)) {
finishTest();
return;
}
switch (gl[pixelFormat]) {
case gl.RED:
case gl.RED_INTEGER:
greenColor = [0, 0, 0];
break;
default:
break;
}
gl.clearColor(0,0,0,1);
gl.clearDepth(1);
runTest();
}
function setCanvasToRedGreen(ctx) {
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var halfHeight = Math.floor(height / 2);
ctx.viewport(0, 0, width, height);
ctx.enable(ctx.SCISSOR_TEST);
ctx.scissor(0, 0, width, halfHeight);
ctx.clearColor(1.0, 0, 0, 1.0);
ctx.clear(ctx.COLOR_BUFFER_BIT);
ctx.scissor(0, halfHeight, width, height - halfHeight);
ctx.clearColor(0.0, 1.0, 0, 1.0);
ctx.clear(ctx.COLOR_BUFFER_BIT);
ctx.disable(ctx.SCISSOR_TEST);
}
function setCanvasTo257x257(ctx, bindingTarget) {
ctx.canvas.width = 257;
ctx.canvas.height = 257;
setCanvasToRedGreen(ctx);
}
function setCanvasToMin(ctx, bindingTarget) {
ctx.canvas.width = 1;
ctx.canvas.height = 2;
setCanvasToRedGreen(ctx);
}
function runOneIteration(canvas, flipY, program, bindingTarget, opt_texture)
{
debug('Testing ' + flipY + ' bindingTarget=' + (bindingTarget == gl.TEXTURE_3D ? 'TEXTURE_3D' : 'TEXTURE_2D_ARRAY') +
' canvas size: ' + canvas.width + 'x' + canvas.height + ' with red-green');
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if (!opt_texture) {
var texture = gl.createTexture();
// Bind the texture to texture unit 0
gl.bindTexture(bindingTarget, texture);
// Set up texture parameters
gl.texParameteri(bindingTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(bindingTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
} else {
var texture = opt_texture;
}
// Set up pixel store parameters
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
wtu.failIfGLError(gl, 'gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);');
// Upload the image into the texture
// Initialize the texture to black first
gl.texImage3D(bindingTarget, 0, gl[internalFormat], canvas.width, canvas.height, 1 /* depth */, 0,
gl[pixelFormat], gl[pixelType], null);
gl.texSubImage3D(bindingTarget, 0, 0, 0, 0, canvas.width, canvas.height, 1 /* depth */,
gl[pixelFormat], gl[pixelType], canvas);
var width = gl.canvas.width;
var height = gl.canvas.height;
var halfHeight = Math.floor(height / 2);
var top = flipY ? (height - halfHeight) : 0;
var bottom = flipY ? 0 : (height - halfHeight);
// Draw the triangles
wtu.clearAndDrawUnitQuad(gl, [0, 255, 0, 255]);
// Check the top and bottom halves and make sure they have the right color.
debug("Checking " + (flipY ? "top" : "bottom"));
wtu.checkCanvasRect(gl, 0, bottom, width, halfHeight, redColor, "shouldBe " + redColor);
debug("Checking " + (flipY ? "bottom" : "top"));
wtu.checkCanvasRect(gl, 0, top, width, halfHeight, greenColor, "shouldBe " + greenColor);
if (false) {
var ma = wtu.makeImageFromCanvas(canvas);
document.getElementById("console").appendChild(ma);
var m = wtu.makeImageFromCanvas(gl.canvas);
document.getElementById("console").appendChild(m);
document.getElementById("console").appendChild(document.createElement("hr"));
}
return texture;
}
function runTest()
{
var ctx = wtu.create3DContext();
var canvas = ctx.canvas;
var cases = [
{ flipY: true, init: setCanvasToMin },
{ flipY: false },
{ flipY: true, init: setCanvasTo257x257 },
{ flipY: false },
];
function runTexImageTest(bindingTarget) {
var program;
if (bindingTarget == gl.TEXTURE_3D) {
program = tiu.setupTexturedQuadWith3D(gl, internalFormat);
} else {
program = tiu.setupTexturedQuadWith2DArray(gl, internalFormat);
}
return new Promise(function(resolve, reject) {
var count = 4;
var caseNdx = 0;
var texture = undefined;
function runNextTest() {
var c = cases[caseNdx];
if (c.init) {
c.init(ctx, bindingTarget);
}
texture = runOneIteration(canvas, c.flipY, program, bindingTarget, texture);
// for the first 2 iterations always make a new texture.
if (count > 2) {
texture = undefined;
}
++caseNdx;
if (caseNdx == cases.length) {
caseNdx = 0;
--count;
if (!count) {
resolve("SUCCESS");
return;
}
}
wtu.waitForComposite(runNextTest);
}
runNextTest();
});
}
runTexImageTest(gl.TEXTURE_3D).then(function(val) {
runTexImageTest(gl.TEXTURE_2D_ARRAY).then(function(val) {
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
finishTest();
});
});
}
return init;
}
|