Skip to content

Commit

Permalink
[wasm64] Fix reading/writing of gl attributes
Browse files Browse the repository at this point in the history
See #21177
  • Loading branch information
sbc100 committed Jan 27, 2024
1 parent 72c3be3 commit 2cf0fd6
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 53 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ jobs:
browser64_4gb.test_audio_worklet*
browser64_4gb.test_emscripten_log
browser64_4gb.test_clientside_vertex_arrays_es3
browser64_4gb.test_emscripten_animate_canvas_element_size*
"
test-browser-firefox:
executor: bionic
Expand Down
43 changes: 20 additions & 23 deletions src/library_html5_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ var LibraryHtml5WebGL = {
HEAP32[a+i] = 0;
}

HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.alpha }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.depth }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.antialias }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion }}}>>2)] =
HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault }}}>>2)] = 1;
HEAP8[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.alpha }}})] =
HEAP8[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.depth }}})] =
HEAP8[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.antialias }}})] =
HEAP8[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha }}})] =
HEAP8[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion }}})] =
HEAP8[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault }}})] = 1;

#if PTHREADS
// Default context initialization state (user can override):
Expand Down Expand Up @@ -80,24 +80,23 @@ var LibraryHtml5WebGL = {
#if ASSERTIONS
assert(attributes);
#endif
var a = attributes >> 2;
var powerPreference = HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.powerPreference }}}>>2)];
var powerPreference = {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.powerPreference, 'i32') }}};
var contextAttributes = {
'alpha': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.alpha }}}>>2)],
'depth': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.depth }}}>>2)],
'stencil': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.stencil }}}>>2)],
'antialias': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.antialias }}}>>2)],
'premultipliedAlpha': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha }}}>>2)],
'preserveDrawingBuffer': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer }}}>>2)],
'alpha': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.alpha, 'i32') }}},
'depth': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.depth, 'i32') }}},
'stencil': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.stencil, 'i32') }}},
'antialias': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.antialias, 'i32') }}},
'premultipliedAlpha': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.premultipliedAlpha, 'i32') }}},
'preserveDrawingBuffer': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer, 'i32') }}},
'powerPreference': emscripten_webgl_power_preferences[powerPreference],
'failIfMajorPerformanceCaveat': !!HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.failIfMajorPerformanceCaveat }}}>>2)],
'failIfMajorPerformanceCaveat': !!{{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.failIfMajorPerformanceCaveat, 'i32') }}},
// The following are not predefined WebGL context attributes in the WebGL specification, so the property names can be minified by Closure.
majorVersion: HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion }}}>>2)],
minorVersion: HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.minorVersion }}}>>2)],
enableExtensionsByDefault: HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault }}}>>2)],
explicitSwapControl: HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl }}}>>2)],
proxyContextToMainThread: HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.proxyContextToMainThread }}}>>2)],
renderViaOffscreenBackBuffer: HEAP32[a + ({{{ C_STRUCTS.EmscriptenWebGLContextAttributes.renderViaOffscreenBackBuffer }}}>>2)]
majorVersion: {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.majorVersion, 'i32') }}},
minorVersion: {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.minorVersion, 'i32') }}},
enableExtensionsByDefault: {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.enableExtensionsByDefault, 'i32') }}},
explicitSwapControl: {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl, 'i32') }}},
proxyContextToMainThread: {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.proxyContextToMainThread, 'i32') }}},
renderViaOffscreenBackBuffer: {{{ makeGetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.renderViaOffscreenBackBuffer, 'i32') }}}
};

var canvas = findCanvasEventTarget(target);
Expand Down Expand Up @@ -243,10 +242,8 @@ var LibraryHtml5WebGL = {
},
emscripten_webgl_do_commit_frame: () => {
#if TRACE_WEBGL_CALLS
var threadId = (typeof _pthread_self != 'undefined') ? _pthread_self : () => 1;
err(`[Thread ${threadId()}, GL ctx: ${GL.currentContext.handle}]: emscripten_webgl_do_commit_frame()`);
#endif
if (!GL.currentContext || !GL.currentContext.GLctx) {
#if GL_DEBUG
dbg('emscripten_webgl_commit_frame() failed: no GL context set current via emscripten_webgl_make_context_current()!');
Expand Down
24 changes: 13 additions & 11 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
emscripten_webgl_enable_WEBGL_multi_draw: (ctx) => webgl_enable_WEBGL_multi_draw(GL.contexts[ctx].GLctx),

$getEmscriptenSupportedExtensions__internal: true,
$getEmscriptenSupportedExtensions: function(ctx) {
$getEmscriptenSupportedExtensions: (ctx) => {
// Restrict the list of advertised extensions to those that we actually
// support.
var supportedExtensions = [
Expand Down Expand Up @@ -280,7 +280,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
// glGetError() to fetch it. As per GLES2 spec, only the first error is
// remembered, and subsequent errors are discarded until the user has
// cleared the stored error by a call to glGetError().
recordError: function recordError(errorCode) {
recordError: (errorCode) => {
#if GL_TRACK_ERRORS
if (!GL.lastError) {
GL.lastError = errorCode;
Expand Down Expand Up @@ -370,7 +370,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}
},

getTempVertexBuffer: function getTempVertexBuffer(sizeBytes) {
getTempVertexBuffer: (sizeBytes) => {
var idx = GL.log2ceilLookup(sizeBytes);
var ringbuffer = GL.currentContext.tempVertexBuffers1[idx];
var nextFreeBufferIndex = GL.currentContext.tempVertexBufferCounters1[idx];
Expand All @@ -387,7 +387,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
return ringbuffer[nextFreeBufferIndex];
},

getTempIndexBuffer: function getTempIndexBuffer(sizeBytes) {
getTempIndexBuffer: (sizeBytes) => {
var idx = GL.log2ceilLookup(sizeBytes);
var ibo = GL.currentContext.tempIndexBuffers[idx];
if (ibo) {
Expand All @@ -405,7 +405,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
// doublebuffered temp VB memory pointers, so that every second frame
// utilizes different set of temp buffers. The aim is to keep the set of
// buffers being rendered, and the set of buffers being updated disjoint.
newRenderingFrameStarted: function newRenderingFrameStarted() {
newRenderingFrameStarted: () => {
if (!GL.currentContext) {
return;
}
Expand Down Expand Up @@ -450,13 +450,13 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};

#if GL_FFP_ONLY
enabledClientAttribIndices: [],
enableVertexAttribArray: function enableVertexAttribArray(index) {
enableVertexAttribArray: (index) => {
if (!GL.enabledClientAttribIndices[index]) {
GL.enabledClientAttribIndices[index] = true;
GLctx.enableVertexAttribArray(index);
}
},
disableVertexAttribArray: function disableVertexAttribArray(index) {
disableVertexAttribArray: (index) => {
if (GL.enabledClientAttribIndices[index]) {
GL.enabledClientAttribIndices[index] = false;
GLctx.disableVertexAttribArray(index);
Expand All @@ -465,7 +465,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#endif

#if FULL_ES2
calcBufLength: function calcBufLength(size, type, stride, count) {
calcBufLength: (size, type, stride, count) => {
if (stride > 0) {
return count * stride; // XXXvlad this is not exactly correct I don't think
}
Expand All @@ -475,7 +475,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};

usedTempBuffers: [],

preDrawHandleClientVertexAttribBindings: function preDrawHandleClientVertexAttribBindings(count) {
preDrawHandleClientVertexAttribBindings: (count) => {
GL.resetBufferBinding = false;

// TODO: initial pass to detect ranges we need to upload, might not need
Expand All @@ -499,7 +499,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}
},

postDrawHandleClientVertexAttribBindings: function postDrawHandleClientVertexAttribBindings() {
postDrawHandleClientVertexAttribBindings: () => {
if (GL.resetBufferBinding) {
GLctx.bindBuffer(0x8892 /*GL_ARRAY_BUFFER*/, GL.buffers[GLctx.currentArrayBufferBinding]);
}
Expand Down Expand Up @@ -1607,6 +1607,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#endif
],
glTexImage2D: (target, level, internalFormat, width, height, border, format, type, pixels) => {
err("glTexImage2D", pixels);
#if MAX_WEBGL_VERSION >= 2
#if WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION
if ({{{ isCurrentContextWebGL2() }}}) {
Expand Down Expand Up @@ -3100,6 +3101,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_ASSERTIONS
GL.validateGLObjectID(GL.shaders, shader, 'glShaderSource', 'shader');
#endif
out('glShaderSource', shader, count, string, length);
var source = GL.getSource(shader, count, string, length);
#if WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION
Expand Down Expand Up @@ -3673,7 +3675,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
, '$emulGlGenVertexArrays'
#endif
],
glGenVertexArrays: function (n, arrays) {
glGenVertexArrays: (n, arrays) => {
#if LEGACY_GL_EMULATION
emulGlGenVertexArrays(n, arrays);
#else
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4681,
"a.js.gz": 2412,
"a.js": 4787,
"a.js.gz": 2446,
"a.wasm": 10388,
"a.wasm.gz": 6692,
"total": 15638,
"total_gz": 9483
"total": 15744,
"total_gz": 9517
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17917,
"a.js.gz": 8089,
"a.js": 18028,
"a.js.gz": 8125,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21607,
"total_gz": 11161
"total": 21718,
"total_gz": 11197
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4167,
"a.js.gz": 2236,
"a.js": 4294,
"a.js.gz": 2275,
"a.wasm": 10388,
"a.wasm.gz": 6692,
"total": 15124,
"total_gz": 9307
"total": 15251,
"total_gz": 9346
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17395,
"a.js.gz": 7900,
"a.js": 17526,
"a.js.gz": 7947,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21085,
"total_gz": 10972
"total": 21216,
"total_gz": 11019
}
13 changes: 10 additions & 3 deletions test/float_tex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
#include <cmath>
#include <iostream>
#include <vector>
extern "C" {
#include <GL/gl.h>
#include <GL/glut.h>
}

static const char vertex_shader[] =
"#ifdef GL_ES\n"
"precision lowp float;\n"
Expand Down Expand Up @@ -44,6 +43,7 @@ static const char fragment_shader[] =
"}\n"
"if ( dst > 0.5) discard;\n"
"}";

struct NodeInfo { //structure that we want to transmit to our shaders
float x;
float y;
Expand All @@ -55,8 +55,9 @@ GLuint nodeSamplerLocation; //shader sampler address
GLuint indicesAttributeLocation; //shader attribute address
GLuint indicesVBO; //Vertex Buffer Object Id;
const int nbNodes = 512;
NodeInfo * data = new NodeInfo[nbNodes]; //our data that will be transmitted using float texture.
NodeInfo data[nbNodes]; //our data that will be transmitted using float texture.
double alpha = 0; //use to make a simple funny effect;

static void updateFloatTexture() {
int count = 0;
for (float x=0; x < nbNodes; ++x ) {
Expand All @@ -79,7 +80,9 @@ static void updateFloatTexture() {
glBindTexture(GL_TEXTURE_2D, 0);
alpha -= 0.001;
}

static void glut_draw_callback(void) {
printf("glut_draw_callback\n");
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
Expand All @@ -96,6 +99,7 @@ static void glut_draw_callback(void) {
glDrawArrays(GL_POINTS, 0, nbNodes);
glutSwapBuffers();
}

GLuint createShader(const char source[], int type) {
GLint status;
char msg[512];
Expand All @@ -110,6 +114,7 @@ GLuint createShader(const char source[], int type) {
assert(status == GL_TRUE);
return shader;
}

static void gl_init(void) {
GLuint program = glCreateProgram();
glAttachShader(program, createShader(vertex_shader , GL_VERTEX_SHADER));
Expand Down Expand Up @@ -145,6 +150,7 @@ static void gl_init(void) {
glEnable(GL_POINT_SPRITE);
#endif
}

int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
Expand All @@ -153,6 +159,7 @@ int main(int argc, char *argv[]) {
/* Set up glut callback functions */
glutDisplayFunc(glut_draw_callback );
gl_init();
printf("done setup\n");
glutMainLoop();
return 0;
}
Expand Down
Loading

0 comments on commit 2cf0fd6

Please sign in to comment.