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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
*/
function runOneIterationImageBitmapTest(useTexSubImage, bindingTarget, program, bitmap, flipY, premultiplyAlpha, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu)
{
var halfRed = [128, 0, 0];
var halfGreen = [0, 128, 0];
var redColor = [255, 0, 0];
var greenColor = [0, 255, 0];
var blackColor = [0, 0, 0];
switch (gl[pixelFormat]) {
case gl.RED:
case gl.RED_INTEGER:
greenColor = [0, 0, 0];
halfGreen = [0, 0, 0];
break;
case gl.LUMINANCE:
case gl.LUMINANCE_ALPHA:
redColor = [255, 255, 255];
greenColor = [0, 0, 0];
halfRed = [128, 128, 128];
halfGreen = [0, 0, 0];
break;
case gl.ALPHA:
redColor = [0, 0, 0];
greenColor = [0, 0, 0];
halfRed = [0, 0, 0];
halfGreen = [0, 0, 0];
break;
default:
break;
}
switch (gl[internalFormat]) {
case gl.SRGB8:
case gl.SRGB8_ALPHA8:
halfRed = wtu.sRGBToLinear(halfRed);
halfGreen = wtu.sRGBToLinear(halfGreen);
break;
default:
break;
}
var str;
if (optionsVal.is3D) {
str = 'Testing ' + (useTexSubImage ? 'texSubImage3D' : 'texImage3D') +
' with flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha +
', bindingTarget=' + (bindingTarget == gl.TEXTURE_3D ? 'TEXTURE_3D' : 'TEXTURE_2D_ARRAY');
} else {
str = 'Testing ' + (useTexSubImage ? 'texSubImage2D' : 'texImage2D') +
' with flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha +
', bindingTarget=' + (bindingTarget == gl.TEXTURE_2D ? 'TEXTURE_2D' : 'TEXTURE_CUBE_MAP');
}
debug(str);
bufferedLogToConsole(str);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Enable writes to the RGBA channels
gl.colorMask(1, 1, 1, 0);
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_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
var targets = [bindingTarget];
if (bindingTarget == gl.TEXTURE_CUBE_MAP) {
targets = [gl.TEXTURE_CUBE_MAP_POSITIVE_X,
gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
}
bufferedLogToConsole("Starts uploading the image into texture");
// Upload the image into the texture
for (var tt = 0; tt < targets.length; ++tt) {
if (optionsVal.is3D) {
gl.texImage3D(targets[tt], 0, gl[internalFormat], bitmap.width, bitmap.height, 1 /* depth */, 0,
gl[pixelFormat], gl[pixelType], null);
gl.texSubImage3D(targets[tt], 0, 0, 0, 0, bitmap.width, bitmap.height, 1,
gl[pixelFormat], gl[pixelType], bitmap);
} else {
if (useTexSubImage) {
// Initialize the texture to black first
gl.texImage2D(targets[tt], 0, gl[internalFormat], bitmap.width, bitmap.height, 0,
gl[pixelFormat], gl[pixelType], null);
gl.texSubImage2D(targets[tt], 0, 0, 0, gl[pixelFormat], gl[pixelType], bitmap);
} else {
gl.texImage2D(targets[tt], 0, gl[internalFormat], gl[pixelFormat], gl[pixelType], bitmap);
}
}
}
bufferedLogToConsole("Uploading texture completed");
var width = gl.canvas.width;
var halfWidth = Math.floor(width / 2);
var quarterWidth = Math.floor(halfWidth / 2);
var height = gl.canvas.height;
var halfHeight = Math.floor(height / 2);
var quarterHeight = Math.floor(halfHeight / 2);
var top = flipY ? quarterHeight : (height - halfHeight + quarterHeight);
var bottom = flipY ? (height - halfHeight + quarterHeight) : quarterHeight;
var left = quarterWidth;
var right = halfWidth + quarterWidth / 2;
var tl = redColor;
var tr = premultiplyAlpha ? ((optionsVal.alpha == 0.5) ? halfRed : (optionsVal.alpha == 1) ? redColor : blackColor) : redColor;
var bl = greenColor;
var br = premultiplyAlpha ? ((optionsVal.alpha == 0.5) ? halfGreen : (optionsVal.alpha == 1) ? greenColor : blackColor) : greenColor;
var loc;
if (bindingTarget == gl.TEXTURE_CUBE_MAP) {
loc = gl.getUniformLocation(program, "face");
}
var tolerance = 10;
for (var tt = 0; tt < targets.length; ++tt) {
if (bindingTarget == gl.TEXTURE_CUBE_MAP) {
gl.uniform1i(loc, targets[tt]);
}
// Draw the triangles
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
// Check the top pixel and bottom pixel and make sure they have
// the right color.
bufferedLogToConsole("Checking " + (flipY ? "top" : "bottom"));
wtu.checkCanvasRect(gl, left, bottom, 2, 2, tl, "shouldBe " + tl, tolerance);
wtu.checkCanvasRect(gl, right, bottom, 2, 2, tr, "shouldBe " + tr, tolerance);
bufferedLogToConsole("Checking " + (flipY ? "bottom" : "top"));
wtu.checkCanvasRect(gl, left, top, 2, 2, bl, "shouldBe " + bl, tolerance);
wtu.checkCanvasRect(gl, right, top, 2, 2, br, "shouldBe " + br, tolerance);
}
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
}
function resetUnpackParams(gl)
{
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);
gl.pixelStorei(gl.UNPACK_ROW_LENGTH, 0);
gl.pixelStorei(gl.UNPACK_IMAGE_HEIGHT, 0);
}
function runOneIterationImageBitmapTestSubSource(useTexSubImage, bindingTarget, program, bitmap, flipY, premultiplyAlpha, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu)
{
var halfRed = [128, 0, 0];
var halfGreen = [0, 128, 0];
var redColor = [255, 0, 0];
var greenColor = [0, 255, 0];
var blackColor = [0, 0, 0];
switch (gl[pixelFormat]) {
case gl.RED:
case gl.RED_INTEGER:
greenColor = [0, 0, 0];
halfGreen = [0, 0, 0];
break;
case gl.LUMINANCE:
case gl.LUMINANCE_ALPHA:
redColor = [255, 255, 255];
greenColor = [0, 0, 0];
halfRed = [128, 128, 128];
halfGreen = [0, 0, 0];
break;
case gl.ALPHA:
redColor = [0, 0, 0];
greenColor = [0, 0, 0];
halfRed = [0, 0, 0];
halfGreen = [0, 0, 0];
break;
default:
break;
}
switch (gl[internalFormat]) {
case gl.SRGB8:
case gl.SRGB8_ALPHA8:
halfRed = wtu.sRGBToLinear(halfRed);
halfGreen = wtu.sRGBToLinear(halfGreen);
break;
default:
break;
}
var str;
if (optionsVal.is3D) {
str = 'Testing ' + (useTexSubImage ? 'texSubImage3D' : 'texImage3D') + '[SubSource]' +
' with flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha +
', bindingTarget=TEXTURE_3D';
} else {
str = 'Testing ' + (useTexSubImage ? 'texSubImage2D' : 'texImage2D') + '[SubSource]' +
' with flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha +
', bindingTarget=TEXTURE_2D';
}
debug(str);
bufferedLogToConsole(str);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Enable writes to the RGBA channels
gl.colorMask(1, 1, 1, 0);
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_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(bindingTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
var srcTL = redColor;
var srcTR = premultiplyAlpha ? ((optionsVal.alpha == 0.5) ? halfRed : (optionsVal.alpha == 1) ? redColor : blackColor) : redColor;
var srcBL = greenColor;
var srcBR = premultiplyAlpha ? ((optionsVal.alpha == 0.5) ? halfGreen : (optionsVal.alpha == 1) ? greenColor : blackColor) : greenColor;
var tl, tr, bl, br;
bufferedLogToConsole("Starts uploading the image into texture");
// Upload the image into the texture
if (optionsVal.is3D) {
if (useTexSubImage) {
// Initialize the texture to black first
gl.texImage3D(bindingTarget, 0, gl[internalFormat], bitmap.width, bitmap.height, 1 /* depth */, 0,
gl[pixelFormat], gl[pixelType], null);
// Only upload the left half image to the right half texture.
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);
gl.texSubImage3D(bindingTarget, 0, bitmap.width / 2, 0, 0, bitmap.width / 2, bitmap.height, 1,
gl[pixelFormat], gl[pixelType], bitmap);
tl = blackColor;
tr = srcTL;
bl = blackColor;
br = srcBL;
} else {
// Only upload the bottom middle quarter image
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, bitmap.height / 2);
gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);
gl.texImage3D(bindingTarget, 0, gl[internalFormat], bitmap.width, bitmap.height / 2, 1 /* depth */, 0,
gl[pixelFormat], gl[pixelType], bitmap);
if (!flipY) {
tl = srcBL;
tr = srcBR;
bl = srcBL;
br = srcBR;
} else {
tl = srcTL;
tr = srcTR;
bl = srcTL;
br = srcTR;
}
}
} else {
if (useTexSubImage) {
// Initialize the texture to black first
gl.texImage2D(bindingTarget, 0, gl[internalFormat], bitmap.width, bitmap.height, 0,
gl[pixelFormat], gl[pixelType], null);
// Only upload the left half image to the right half texture.
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);
gl.texSubImage2D(bindingTarget, 0, bitmap.width / 2, 0, bitmap.width / 2, bitmap.height,
gl[pixelFormat], gl[pixelType], bitmap);
tl = blackColor;
tr = srcTL;
bl = blackColor;
br = srcBL;
} else {
// Only upload the right bottom image.
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, bitmap.width / 2);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, bitmap.height / 2);
gl.texImage2D(bindingTarget, 0, gl[internalFormat], bitmap.width / 2, bitmap.height / 2, 0,
gl[pixelFormat], gl[pixelType], bitmap);
resetUnpackParams(gl);
if (!flipY) {
tl = srcBR;
tr = srcBR;
bl = srcBR;
br = srcBR;
} else {
tl = srcTR;
tr = srcTR;
bl = srcTR;
br = srcTR;
}
}
}
bufferedLogToConsole("Uploading texture completed");
var width = gl.canvas.width;
var halfWidth = Math.floor(width / 2);
var quarterWidth = Math.floor(halfWidth / 2);
var height = gl.canvas.height;
var halfHeight = Math.floor(height / 2);
var quarterHeight = Math.floor(halfHeight / 2);
var top = flipY ? quarterHeight : (height - halfHeight + quarterHeight);
var bottom = flipY ? (height - halfHeight + quarterHeight) : quarterHeight;
var tolerance = 10;
// Draw the triangles
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
// Check the top pixel and bottom pixel and make sure they have
// the right color.
// For right side, check pixels closer to left to avoid border in the video tests.
bufferedLogToConsole("Checking " + (flipY ? "top" : "bottom"));
wtu.checkCanvasRect(gl, quarterWidth, bottom, 2, 2, tl, "shouldBe " + tl, tolerance);
wtu.checkCanvasRect(gl, halfWidth + quarterWidth / 2, bottom, 2, 2, tr, "shouldBe " + tr, tolerance);
bufferedLogToConsole("Checking " + (flipY ? "bottom" : "top"));
wtu.checkCanvasRect(gl, quarterWidth, top, 2, 2, bl, "shouldBe " + bl, tolerance);
wtu.checkCanvasRect(gl, halfWidth + quarterWidth / 2, top, 2, 2, br, "shouldBe " + br, tolerance);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
}
function runTestOnBindingTargetImageBitmap(bindingTarget, program, cases, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu)
{
cases.forEach(x => {
runOneIterationImageBitmapTest(x.sub, bindingTarget, program, x.bitmap,
x.bitmap.flipY, x.bitmap.premultiply, optionsVal, internalFormat, pixelFormat, pixelType, gl, tiu, wtu);
});
if (wtu.getDefault3DContextVersion() <= 1 ||
(bindingTarget == gl.TEXTURE_CUBE_MAP || bindingTarget == gl.TEXTURE_2D_ARRAY))
{
// Skip testing source sub region on TEXTURE_CUBE_MAP and TEXTURE_2D_ARRAY on WebGL2 to save
// running time.
return;
}
cases.forEach(x => {
runOneIterationImageBitmapTestSubSource(x.sub, bindingTarget, program, x.bitmap,
x.bitmap.flipY, x.bitmap.premultiply, optionsVal, internalFormat, pixelFormat, pixelType, gl, tiu, wtu);
});
}
function runImageBitmapTestInternal(bitmaps, alphaVal, internalFormat, pixelFormat, pixelType, gl, tiu, wtu, is3D)
{
var cases = [];
bitmaps.forEach(bitmap => {
cases.push({bitmap: bitmap, sub: false});
cases.push({bitmap: bitmap, sub: true});
});
var optionsVal = {alpha: alphaVal, is3D: is3D};
var program;
if (is3D) {
program = tiu.setupTexturedQuadWith3D(gl, internalFormat);
runTestOnBindingTargetImageBitmap(gl.TEXTURE_3D, program, cases, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu);
} else {
program = tiu.setupTexturedQuad(gl, internalFormat);
runTestOnBindingTargetImageBitmap(gl.TEXTURE_2D, program, cases, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu);
}
// cube map texture must be square
if (bitmaps[0].width == bitmaps[0].height) {
if (is3D) {
program = tiu.setupTexturedQuadWith2DArray(gl, internalFormat);
runTestOnBindingTargetImageBitmap(gl.TEXTURE_2D_ARRAY, program, cases, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu);
} else {
program = tiu.setupTexturedQuadWithCubeMap(gl, internalFormat);
runTestOnBindingTargetImageBitmap(gl.TEXTURE_CUBE_MAP, program, cases, optionsVal,
internalFormat, pixelFormat, pixelType, gl, tiu, wtu);
}
}
}
function runImageBitmapTest(source, alphaVal, internalFormat, pixelFormat, pixelType, gl, tiu, wtu, is3D)
{
var p = createImageBitmap(source)
.then(cur => { cur.flipY = false; cur.premultiply = false; return cur; });
return Promise.all([p])
.then( bitmaps => {
bufferedLogToConsole("All createImageBitmap promises are resolved");
runImageBitmapTestInternal(bitmaps, alphaVal, internalFormat, pixelFormat, pixelType, gl, tiu, wtu, is3D);
}, (e) => {
// This will fail here when running from file:// instead of https://.
testFailed("createImageBitmap(source) failed: \"" + e.message + "\"");
});
}
|