Skip to content

Commit 98ca5e2

Browse files
authored
Merge pull request #4892 from juj/webgl2_texture_fixes
webgl2_texture_fixes
2 parents e16dfd0 + de80d09 commit 98ca5e2

File tree

1 file changed

+129
-9
lines changed

1 file changed

+129
-9
lines changed

src/library_gl.js

Lines changed: 129 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,25 +1233,120 @@ var LibraryGL = {
12331233
}
12341234
},
12351235

1236+
#if USE_WEBGL2
1237+
$emscriptenWebGLGetHeapForType: function(type) {
1238+
switch(type) {
1239+
case 0x1400 /* GL_BYTE */:
1240+
return HEAP8;
1241+
case 0x1401 /* GL_UNSIGNED_BYTE */:
1242+
return HEAPU8;
1243+
case 0x1402 /* GL_SHORT */:
1244+
return HEAP16;
1245+
case 0x1403 /* GL_UNSIGNED_SHORT */:
1246+
case 0x8363 /* GL_UNSIGNED_SHORT_5_6_5 */:
1247+
case 0x8033 /* GL_UNSIGNED_SHORT_4_4_4_4 */:
1248+
case 0x8034 /* GL_UNSIGNED_SHORT_5_5_5_1 */:
1249+
case 0x8D61 /* GL_HALF_FLOAT_OES */:
1250+
case 0x140B /* GL_HALF_FLOAT */:
1251+
return HEAPU16;
1252+
case 0x1404 /* GL_INT */:
1253+
return HEAP32;
1254+
case 0x1405 /* GL_UNSIGNED_INT */:
1255+
case 0x84FA /* GL_UNSIGNED_INT_24_8_WEBGL/GL_UNSIGNED_INT_24_8 */:
1256+
case 0x8C3E /* GL_UNSIGNED_INT_5_9_9_9_REV */:
1257+
case 0x8368 /* GL_UNSIGNED_INT_2_10_10_10_REV */:
1258+
case 0x8C3B /* GL_UNSIGNED_INT_10F_11F_11F_REV */:
1259+
case 0x84FA /* GL_UNSIGNED_INT_24_8 */:
1260+
return HEAPU32;
1261+
case 0x1406 /* GL_FLOAT */:
1262+
return HEAPF32;
1263+
default:
1264+
return null;
1265+
}
1266+
},
1267+
1268+
$emscriptenWebGLGetShiftForType: function(type) {
1269+
switch(type) {
1270+
case 0x1400 /* GL_BYTE */:
1271+
case 0x1401 /* GL_UNSIGNED_BYTE */:
1272+
return 0;
1273+
case 0x1402 /* GL_SHORT */:
1274+
case 0x1403 /* GL_UNSIGNED_SHORT */:
1275+
case 0x8363 /* GL_UNSIGNED_SHORT_5_6_5 */:
1276+
case 0x8033 /* GL_UNSIGNED_SHORT_4_4_4_4 */:
1277+
case 0x8034 /* GL_UNSIGNED_SHORT_5_5_5_1 */:
1278+
case 0x8D61 /* GL_HALF_FLOAT_OES */:
1279+
case 0x140B /* GL_HALF_FLOAT */:
1280+
return 1;
1281+
case 0x1404 /* GL_INT */:
1282+
case 0x1406 /* GL_FLOAT */:
1283+
case 0x1405 /* GL_UNSIGNED_INT */:
1284+
case 0x84FA /* GL_UNSIGNED_INT_24_8_WEBGL/GL_UNSIGNED_INT_24_8 */:
1285+
case 0x8C3E /* GL_UNSIGNED_INT_5_9_9_9_REV */:
1286+
case 0x8368 /* GL_UNSIGNED_INT_2_10_10_10_REV */:
1287+
case 0x8C3B /* GL_UNSIGNED_INT_10F_11F_11F_REV */:
1288+
case 0x84FA /* GL_UNSIGNED_INT_24_8 */:
1289+
return 2;
1290+
default:
1291+
return 0;
1292+
}
1293+
},
1294+
#endif
1295+
12361296
glTexImage2D__sig: 'viiiiiiiii',
1237-
glTexImage2D__deps: ['$emscriptenWebGLGetTexPixelData'],
1297+
glTexImage2D__deps: ['$emscriptenWebGLGetTexPixelData', '$emscriptenWebGLGetHeapForType', '$emscriptenWebGLGetShiftForType'],
12381298
glTexImage2D: function(target, level, internalFormat, width, height, border, format, type, pixels) {
1299+
#if USE_WEBGL2
1300+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1301+
if (GLctx.currentPixelUnpackBufferBinding) {
1302+
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
1303+
} else if (pixels != 0) {
1304+
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, emscriptenWebGLGetHeapForType(type), pixels >> emscriptenWebGLGetShiftForType(type));
1305+
} else {
1306+
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, null);
1307+
}
1308+
return;
1309+
}
1310+
#endif
1311+
12391312
var pixelData = null;
12401313
if (pixels) pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, internalFormat);
12411314
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixelData);
12421315
},
12431316

12441317
glTexSubImage2D__sig: 'viiiiiiiii',
1245-
glTexSubImage2D__deps: ['$emscriptenWebGLGetTexPixelData'],
1318+
glTexSubImage2D__deps: ['$emscriptenWebGLGetTexPixelData', '$emscriptenWebGLGetHeapForType', '$emscriptenWebGLGetShiftForType'],
12461319
glTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, type, pixels) {
1320+
#if USE_WEBGL2
1321+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1322+
if (GLctx.currentPixelUnpackBufferBinding) {
1323+
GLctx.texSubImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
1324+
} else if (pixels != 0) {
1325+
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, emscriptenWebGLGetHeapForType(type), pixels >> emscriptenWebGLGetShiftForType(type));
1326+
} else {
1327+
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, null);
1328+
}
1329+
return;
1330+
}
1331+
#endif
12471332
var pixelData = null;
12481333
if (pixels) pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, 0);
12491334
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixelData);
12501335
},
12511336

12521337
glReadPixels__sig: 'viiiiiii',
1253-
glReadPixels__deps: ['$emscriptenWebGLGetTexPixelData'],
1338+
glReadPixels__deps: ['$emscriptenWebGLGetTexPixelData', '$emscriptenWebGLGetHeapForType', '$emscriptenWebGLGetShiftForType'],
12541339
glReadPixels: function(x, y, width, height, format, type, pixels) {
1340+
#if USE_WEBGL2
1341+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1342+
if (GLctx.currentPixelPackBufferBinding) {
1343+
GLctx.readPixels(x, y, width, height, format, type, pixels);
1344+
} else {
1345+
GLctx.readPixels(x, y, width, height, format, type, emscriptenWebGLGetHeapForType(type), pixels >> emscriptenWebGLGetShiftForType(type));
1346+
}
1347+
return;
1348+
}
1349+
#endif
12551350
var pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, format);
12561351
if (!pixelData) {
12571352
GL.recordError(0x0500/*GL_INVALID_ENUM*/);
@@ -1708,15 +1803,27 @@ var LibraryGL = {
17081803
},
17091804

17101805
glTexImage3D__sig: 'viiiiiiiiii',
1711-
glTexImage3D: function(target, level, internalFormat, width, height, depth, border, format, type, data) {
1712-
GLctx['texImage3D'](target, level, internalFormat, width, height, depth, border, format, type,
1713-
HEAPU8.subarray(data));
1806+
glTexImage3D__deps: ['$emscriptenWebGLGetTexPixelData', '$emscriptenWebGLGetHeapForType', '$emscriptenWebGLGetShiftForType'],
1807+
glTexImage3D: function(target, level, internalFormat, width, height, depth, border, format, type, pixels) {
1808+
if (GLctx.currentPixelUnpackBufferBinding) {
1809+
GLctx['texImage3D'](target, level, internalFormat, width, height, depth, border, format, type, pixels);
1810+
} else if (pixels != 0) {
1811+
GLctx['texImage3D'](target, level, internalFormat, width, height, depth, border, format, type, emscriptenWebGLGetHeapForType(type), pixels >> emscriptenWebGLGetShiftForType(type));
1812+
} else {
1813+
GLctx['texImage3D'](target, level, internalFormat, width, height, depth, border, format, type, null);
1814+
}
17141815
},
17151816

17161817
glTexSubImage3D__sig: 'viiiiiiiiiii',
1717-
glTexSubImage3D: function(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, data) {
1718-
GLctx['texSubImage3D'](target, level, xoffset, yoffset, zoffset, width, height, depth, format, type,
1719-
HEAPU8.subarray(data));
1818+
glTexSubImage3D__deps: ['$emscriptenWebGLGetTexPixelData', '$emscriptenWebGLGetHeapForType', '$emscriptenWebGLGetShiftForType'],
1819+
glTexSubImage3D: function(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels) {
1820+
if (GLctx.currentPixelUnpackBufferBinding) {
1821+
GLctx['texSubImage3D'](target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels);
1822+
} else if (pixels != 0) {
1823+
GLctx['texSubImage3D'](target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, emscriptenWebGLGetHeapForType(type), pixels >> emscriptenWebGLGetShiftForType(type));
1824+
} else {
1825+
GLctx['texSubImage3D'](target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, null);
1826+
}
17201827
},
17211828

17221829
// Queries
@@ -3130,6 +3237,19 @@ var LibraryGL = {
31303237
}
31313238
#endif
31323239

3240+
#if USE_WEBGL2
3241+
if (target == 0x88EB /*GL_PIXEL_PACK_BUFFER*/) {
3242+
// In WebGL 2 glReadPixels entry point, we need to use a different WebGL 2 API function call when a buffer is bound to
3243+
// GL_PIXEL_PACK_BUFFER_BINDING point, so must keep track whether that binding point is non-null to know what is
3244+
// the proper API function to call.
3245+
GLctx.currentPixelPackBufferBinding = buffer;
3246+
} else if (target == 0x88EC /*GL_PIXEL_UNPACK_BUFFER*/) {
3247+
// In WebGL 2 glTexImage2D, glTexSubImage2D, glTexImage3D and glTexSubImage3D entry points, we need to use a different WebGL 2 API function
3248+
// call when a buffer is bound to GL_PIXEL_UNPACK_BUFFER_BINDING point, so must keep track whether that binding point is non-null to know what
3249+
// is the proper API function to call.
3250+
GLctx.currentPixelUnpackBufferBinding = buffer;
3251+
}
3252+
#endif
31333253
GLctx.bindBuffer(target, bufferObj);
31343254
},
31353255

0 commit comments

Comments
 (0)