Skip to content

Commit

Permalink
[wasm64] Fix library_glemu.js under wasm64 (#21241)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Feb 2, 2024
1 parent 2b9870c commit 8747ab0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
29 changes: 25 additions & 4 deletions src/library_glemu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
* SPDX-License-Identifier: MIT
*/

{{{
globalThis.fromPtr = (arg) => {
if (CAN_ADDRESS_2GB) {
return `${arg} >>>= 0`;
} else if (MEMORY64) {
return `${arg} = Number(${arg})`;
}
return '';
}
null;
}}}

var LibraryGLEmulation = {
// GL emulation: provides misc. functionality not present in OpenGL ES 2.0 or WebGL
$GLEmulation__deps: ['$GLImmediateSetup', 'glEnable', 'glDisable',
Expand Down Expand Up @@ -309,6 +321,7 @@ var LibraryGLEmulation = {
_glGetBooleanv = _emscripten_glGetBooleanv = (pname, p) => {
var attrib = GLEmulation.getAttributeFromCapability(pname);
if (attrib !== null) {
{{{ fromPtr('p') }}}
var result = GLImmediate.enabledClientAttributes[attrib];
{{{ makeSetValue('p', '0', 'result === true ? 1 : 0', 'i8') }}};
return;
Expand All @@ -318,6 +331,7 @@ var LibraryGLEmulation = {

var glGetIntegerv = _glGetIntegerv;
_glGetIntegerv = _emscripten_glGetIntegerv = (pname, params) => {
{{{ fromPtr('params') }}}
switch (pname) {
case 0x84E2: pname = GLctx.MAX_TEXTURE_IMAGE_UNITS /* fake it */; break; // GL_MAX_TEXTURE_UNITS
case 0x8B4A: { // GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB
Expand Down Expand Up @@ -439,6 +453,8 @@ var LibraryGLEmulation = {

var glShaderSource = _glShaderSource;
_glShaderSource = _emscripten_glShaderSource = (shader, count, string, length) => {
{{{ fromPtr('string') }}}
{{{ fromPtr('length') }}}
var source = GL.getSource(shader, count, string, length);
#if GL_DEBUG
dbg("glShaderSource: Input: \n" + source);
Expand Down Expand Up @@ -647,14 +663,15 @@ var LibraryGLEmulation = {

var glGetFloatv = _glGetFloatv;
_glGetFloatv = _emscripten_glGetFloatv = (pname, params) => {
{{{ fromPtr('params') }}}
if (pname == 0xBA6) { // GL_MODELVIEW_MATRIX
HEAPF32.set(GLImmediate.matrix[0/*m*/], params >> 2);
HEAPF32.set(GLImmediate.matrix[0/*m*/], {{{ getHeapOffset('params', 'float') }}});
} else if (pname == 0xBA7) { // GL_PROJECTION_MATRIX
HEAPF32.set(GLImmediate.matrix[1/*p*/], params >> 2);
HEAPF32.set(GLImmediate.matrix[1/*p*/], {{{ getHeapOffset('params', 'float') }}});
} else if (pname == 0xBA8) { // GL_TEXTURE_MATRIX
HEAPF32.set(GLImmediate.matrix[2/*t*/ + GLImmediate.clientActiveTexture], params >> 2);
HEAPF32.set(GLImmediate.matrix[2/*t*/ + GLImmediate.clientActiveTexture], {{{ getHeapOffset('params', 'float') }}});
} else if (pname == 0xB66) { // GL_FOG_COLOR
HEAPF32.set(GLEmulation.fogColor, params >> 2);
HEAPF32.set(GLEmulation.fogColor, {{{ getHeapOffset('params', 'float') }}});
} else if (pname == 0xB63) { // GL_FOG_START
{{{ makeSetValue('params', '0', 'GLEmulation.fogStart', 'float') }}};
} else if (pname == 0xB64) { // GL_FOG_END
Expand Down Expand Up @@ -2783,6 +2800,7 @@ var LibraryGLEmulation = {
var glTexEnvi = (typeof _glTexEnvi != 'undefined') ? _glTexEnvi : () => {};
/** @suppress {checkTypes} */
_glTexEnvi = _emscripten_glTexEnvi = (target, pname, param) => {
{{{ fromPtr('param') }}}
GLImmediate.TexEnvJIT.hook_texEnvi(target, pname, param);
// Don't call old func, since we are the implementor.
//glTexEnvi(target, pname, param);
Expand All @@ -2791,16 +2809,19 @@ var LibraryGLEmulation = {
var glTexEnvfv = (typeof _glTexEnvfv != 'undefined') ? _glTexEnvfv : () => {};
/** @suppress {checkTypes} */
_glTexEnvfv = _emscripten_glTexEnvfv = (target, pname, param) => {
{{{ fromPtr('param') }}}
GLImmediate.TexEnvJIT.hook_texEnvfv(target, pname, param);
// Don't call old func, since we are the implementor.
//glTexEnvfv(target, pname, param);
};

_glGetTexEnviv = (target, pname, param) => {
{{{ fromPtr('param') }}}
GLImmediate.TexEnvJIT.hook_getTexEnviv(target, pname, param);
};

_glGetTexEnvfv = (target, pname, param) => {
{{{ fromPtr('param') }}}
GLImmediate.TexEnvJIT.hook_getTexEnvfv(target, pname, param);
};

Expand Down
4 changes: 4 additions & 0 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4279,6 +4279,10 @@ function recordGLProcAddressGet(lib) {
Object.keys(lib).forEach((x) => {
if (x.startsWith('gl') && !isDecorator(x)) {
lib['emscripten_' + x] = x;
var sig = LibraryManager.library[x + '__sig'];
if (sig) {
lib['emscripten_' + x + '__sig'] = sig;
}
}
});
}
Expand Down
34 changes: 0 additions & 34 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,27 +2023,23 @@ def test_gl_textures(self, args):
self.btest_exit('gl_textures.cpp', args=['-lGL', '-g', '-sSTACK_SIZE=1MB'] + args)

@requires_graphics_hardware
@no_wasm64('TODO: wasm64 + LEGACY_GL_EMULATION')
def test_gl_ps(self):
# pointers and a shader
shutil.copyfile(test_file('screenshot.png'), 'screenshot.png')
self.btest('gl_ps.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '--use-preload-plugins'], reference_slack=1)

@requires_graphics_hardware
@no_wasm64('TODO: wasm64 + LEGACY_GL_EMULATION')
def test_gl_ps_packed(self):
# packed data that needs to be strided
shutil.copyfile(test_file('screenshot.png'), 'screenshot.png')
self.btest('gl_ps_packed.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '--use-preload-plugins'], reference_slack=1)

@requires_graphics_hardware
@no_wasm64('TODO: wasm64 + LEGACY_GL_EMULATION')
def test_gl_ps_strides(self):
shutil.copyfile(test_file('screenshot.png'), 'screenshot.png')
self.btest('gl_ps_strides.c', reference='gl_ps_strides.png', args=['--preload-file', 'screenshot.png', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '--use-preload-plugins'])

@requires_graphics_hardware
@no_wasm64('TODO: wasm64 + LEGACY_GL_EMULATION')
def test_gl_ps_worker(self):
shutil.copyfile(test_file('screenshot.png'), 'screenshot.png')
self.btest('gl_ps_worker.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '--use-preload-plugins'], reference_slack=1, also_proxied=True)
Expand Down Expand Up @@ -2076,37 +2072,31 @@ def test_gles2_conformance(self):
def test_matrix_identity(self):
self.btest('gl_matrix_identity.c', expected=['-1882984448', '460451840', '1588195328', '2411982848'], args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre(self):
self.btest('third_party/cubegeom/cubegeom_pre.c', reference='third_party/cubegeom/cubegeom_pre.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre_regal(self):
self.btest('third_party/cubegeom/cubegeom_pre.c', reference='third_party/cubegeom/cubegeom_pre.png', args=['-sUSE_REGAL', '-DUSE_REGAL', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre_relocatable(self):
self.btest('third_party/cubegeom/cubegeom_pre.c', reference='third_party/cubegeom/cubegeom_pre.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-sRELOCATABLE'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre2(self):
self.btest('third_party/cubegeom/cubegeom_pre2.c', reference='third_party/cubegeom/cubegeom_pre2.png', args=['-sGL_DEBUG', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL']) # some coverage for GL_DEBUG not breaking the build

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre3(self):
self.btest('third_party/cubegeom/cubegeom_pre3.c', reference='third_party/cubegeom/cubegeom_pre2.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@parameterized({
'': ([],),
'tracing': (['-sTRACE_WEBGL_CALLS'],),
Expand All @@ -2117,18 +2107,15 @@ def test_cubegeom(self, args):
# proxied)
self.btest('third_party/cubegeom/cubegeom.c', reference='third_party/cubegeom/cubegeom.png', args=['-O2', '-g', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'] + args, also_proxied=not args)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_regal(self):
self.btest('third_party/cubegeom/cubegeom.c', reference='third_party/cubegeom/cubegeom.png', args=['-O2', '-g', '-DUSE_REGAL', '-sUSE_REGAL', '-lGL', '-lSDL', '-lc++', '-lc++abi'], also_proxied=True)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_threads
@requires_graphics_hardware
def test_cubegeom_regal_mt(self):
self.btest('third_party/cubegeom/cubegeom.c', reference='third_party/cubegeom/cubegeom.png', args=['-O2', '-g', '-pthread', '-DUSE_REGAL', '-pthread', '-sUSE_REGAL', '-lGL', '-lSDL', '-lc++', '-lc++abi'], also_proxied=False)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@parameterized({
'': ([],),
Expand All @@ -2151,108 +2138,88 @@ def test_cubegeom_proc(self, opts):
''')
self.btest('third_party/cubegeom/cubegeom_proc.c', reference='third_party/cubegeom/cubegeom.png', args=opts + ['side.c', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-sGL_ENABLE_GET_PROC_ADDRESS'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@also_with_wasmfs
@requires_graphics_hardware
def test_cubegeom_glew(self):
self.btest('third_party/cubegeom/cubegeom_glew.c', reference='third_party/cubegeom/cubegeom.png', args=['-O2', '--closure=1', '-sLEGACY_GL_EMULATION', '-lGL', '-lGLEW', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_color(self):
self.btest('third_party/cubegeom/cubegeom_color.c', reference='third_party/cubegeom/cubegeom_color.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_normal(self):
self.btest('third_party/cubegeom/cubegeom_normal.c', reference='third_party/cubegeom/cubegeom_normal.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], also_proxied=True)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_normal_dap(self): # draw is given a direct pointer to clientside memory, no element array buffer
self.btest('third_party/cubegeom/cubegeom_normal_dap.c', reference='third_party/cubegeom/cubegeom_normal.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], also_proxied=True)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_normal_dap_far(self): # indices do nto start from 0
self.btest('third_party/cubegeom/cubegeom_normal_dap_far.c', reference='third_party/cubegeom/cubegeom_normal.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_normal_dap_far_range(self): # glDrawRangeElements
self.btest('third_party/cubegeom/cubegeom_normal_dap_far_range.c', reference='third_party/cubegeom/cubegeom_normal.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_normal_dap_far_glda(self): # use glDrawArrays
self.btest('third_party/cubegeom/cubegeom_normal_dap_far_glda.c', reference='third_party/cubegeom/cubegeom_normal_dap_far_glda.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_firefox('fails on CI but works locally')
def test_cubegeom_normal_dap_far_glda_quad(self): # with quad
self.btest('third_party/cubegeom/cubegeom_normal_dap_far_glda_quad.c', reference='third_party/cubegeom/cubegeom_normal_dap_far_glda_quad.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_mt(self):
self.btest('third_party/cubegeom/cubegeom_mt.c', reference='third_party/cubegeom/cubegeom_mt.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL']) # multitexture

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_color2(self):
self.btest('third_party/cubegeom/cubegeom_color2.c', reference='third_party/cubegeom/cubegeom_color2.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], also_proxied=True)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_texturematrix(self):
self.btest('third_party/cubegeom/cubegeom_texturematrix.c', reference='third_party/cubegeom/cubegeom_texturematrix.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_fog(self):
self.btest('third_party/cubegeom/cubegeom_fog.c', reference='third_party/cubegeom/cubegeom_fog.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre_vao(self):
self.btest('third_party/cubegeom/cubegeom_pre_vao.c', reference='third_party/cubegeom/cubegeom_pre_vao.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre_vao_regal(self):
self.btest('third_party/cubegeom/cubegeom_pre_vao.c', reference='third_party/cubegeom/cubegeom_pre_vao.png', args=['-sUSE_REGAL', '-DUSE_REGAL', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre2_vao(self):
self.btest('third_party/cubegeom/cubegeom_pre2_vao.c', reference='third_party/cubegeom/cubegeom_pre_vao.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-sGL_ENABLE_GET_PROC_ADDRESS'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_pre2_vao2(self):
self.btest('third_party/cubegeom/cubegeom_pre2_vao2.c', reference='third_party/cubegeom/cubegeom_pre2_vao2.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-sGL_ENABLE_GET_PROC_ADDRESS'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
@no_swiftshader
def test_cubegeom_pre_vao_es(self):
self.btest('third_party/cubegeom/cubegeom_pre_vao_es.c', reference='third_party/cubegeom/cubegeom_pre_vao.png', args=['-sFULL_ES2', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cubegeom_u4fv_2(self):
self.btest('third_party/cubegeom/cubegeom_u4fv_2.c', reference='third_party/cubegeom/cubegeom_u4fv_2.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_cube_explosion(self):
self.btest('cube_explosion.c', reference='cube_explosion.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], also_proxied=True)

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_glgettexenv(self):
self.btest('glgettexenv.c', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], expected='1')
Expand Down Expand Up @@ -2337,7 +2304,6 @@ def test_anisotropic(self):
shutil.copyfile(test_file('browser/water.dds'), 'water.dds')
self.btest('test_anisotropic.c', reference='browser/test_anisotropic.png', reference_slack=2, args=['--preload-file', 'water.dds', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-Wno-incompatible-pointer-types'])

@no_wasm64('wasm64 + LEGACY_GL_EMULATION')
@requires_graphics_hardware
def test_tex_nonbyte(self):
self.btest('tex_nonbyte.c', reference='tex_nonbyte.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
Expand Down

0 comments on commit 8747ab0

Please sign in to comment.