Files
sx/library/modules/opengl.sx
agra d4a342d0c1 mem: implicit-Context platform fixes — chess green on macOS/iOS/Android
Verify-step uncovered three categories of regressions where sx code
calls into the platform's C ABI through fn-pointer types or as a
registered callback. Every site now declares the right convention.

C-side calls INTO sx → callconv(.c) on the sx function:
- platform/android.sx: sx_android_render_thread_entry is the start
  routine pthread_create invokes — pthread treats it as a C function.
  Also annotate the pthread_create signature so the start-routine fn-
  pointer field rejects mismatching sx fns at compile time.

sx code calling typed fn-pointers cast from C symbols → callconv(.c)
on the fn-pointer type:
- opengl.sx: 55 GL fn-ptr globals + load_gl's proc-loader param. GL
  trampolines are macOS/iOS/Android system code.
- std/objc.sx: the two typed `objc_msgSend` casts.
- gpu/metal.sx: ~40 typed `objc_msgSend` casts across Metal command
  encoder / device / pipeline construction.

The block invoke trampolines (objc_block.sx) call back INTO sx (the
closure trampoline). The typed fn-ptr there stays default-conv so
ctx prepends correctly. Compiler change: a callconv(.c) sx function
now binds `current_ctx_ref` to `&__sx_default_context` at entry (used
to be gated by `isExportedEntryName`). C-callable sx callbacks like
the block invokes don't get their own __sx_ctx param but their bodies
still need a real Context to forward to the closure they delegate to.

Tests: 152/152 example suite + chess green on all 3 platforms.
Screenshots at /tmp/sx-game-{macos,iossim,android}.png.
2026-05-25 09:35:15 +03:00

200 lines
9.6 KiB
Plaintext

// OpenGL 3.3 Core — runtime-loaded function pointers
// No #library needed — caller provides a loader (e.g. SDL_GL_GetProcAddress)
// Constants
GL_FALSE :s32: 0;
GL_TRUE :s32: 1;
GL_DEPTH_TEST :u32: 0x0B71;
GL_CULL_FACE :u32: 0x0B44;
GL_BLEND :u32: 0x0BE2;
GL_TRIANGLES :u32: 4;
GL_LINES :u32: 1;
GL_FLOAT :u32: 0x1406;
GL_UNSIGNED_INT :u32: 0x1405;
GL_VERTEX_SHADER :u32: 0x8B31;
GL_FRAGMENT_SHADER :u32: 0x8B30;
GL_COMPILE_STATUS :u32: 0x8B81;
GL_LINK_STATUS :u32: 0x8B82;
GL_ARRAY_BUFFER :u32: 0x8892;
GL_ELEMENT_ARRAY_BUFFER :u32: 0x8893;
GL_STATIC_DRAW :u32: 0x88E4;
GL_COLOR_BUFFER_BIT :u32: 0x4000;
GL_DEPTH_BUFFER_BIT :u32: 0x0100;
GL_FRONT_AND_BACK :u32: 0x0408;
GL_LINE :u32: 0x1B01;
GL_FILL :u32: 0x1B02;
// Function pointer variables (mutable, loaded at runtime)
glClearColor : (f32, f32, f32, f32) -> void callconv(.c) = ---;
glClear : (u32) -> void callconv(.c) = ---;
glEnable : (u32) -> void callconv(.c) = ---;
glDisable : (u32) -> void callconv(.c) = ---;
glViewport : (s32, s32, s32, s32) -> void callconv(.c) = ---;
glFlush : () -> void callconv(.c) = ---;
glDrawArrays : (u32, s32, s32) -> void callconv(.c) = ---;
glPolygonMode : (u32, u32) -> void callconv(.c) = ---;
glLineWidth : (f32) -> void callconv(.c) = ---;
glCreateShader : (u32) -> u32 callconv(.c) = ---;
glShaderSource : (u32, s32, *[:0]u8, *s32) -> void callconv(.c) = ---;
glCompileShader : (u32) -> void callconv(.c) = ---;
glGetShaderiv : (u32, u32, *s32) -> void callconv(.c) = ---;
glGetShaderInfoLog : (u32, s32, *s32, [*]u8) -> void callconv(.c) = ---;
glCreateProgram : () -> u32 callconv(.c) = ---;
glAttachShader : (u32, u32) -> void callconv(.c) = ---;
glLinkProgram : (u32) -> void callconv(.c) = ---;
glGetProgramiv : (u32, u32, *s32) -> void callconv(.c) = ---;
glGetProgramInfoLog : (u32, s32, *s32, [*]u8) -> void callconv(.c) = ---;
glUseProgram : (u32) -> void callconv(.c) = ---;
glDeleteShader : (u32) -> void callconv(.c) = ---;
glGenVertexArrays : (s32, *u32) -> void callconv(.c) = ---;
glGenBuffers : (s32, *u32) -> void callconv(.c) = ---;
glBindVertexArray : (u32) -> void callconv(.c) = ---;
glBindBuffer : (u32, u32) -> void callconv(.c) = ---;
glBufferData : (u32, isize, *void, u32) -> void callconv(.c) = ---;
glVertexAttribPointer : (u32, s32, u32, u8, s32, *void) -> void callconv(.c) = ---;
glEnableVertexAttribArray : (u32) -> void callconv(.c) = ---;
glGetUniformLocation : (u32, [*]u8) -> s32 callconv(.c) = ---;
glUniformMatrix4fv : (s32, s32, u8, [*]f32) -> void callconv(.c) = ---;
glUniform3f : (s32, f32, f32, f32) -> void callconv(.c) = ---;
glDepthFunc : (u32) -> void callconv(.c) = ---;
glUniform1f : (s32, f32) -> void callconv(.c) = ---;
GL_LESS :u32: 0x0201;
GL_LEQUAL :u32: 0x0203;
GL_SCISSOR_TEST :u32: 0x0C11;
GL_DYNAMIC_DRAW :u32: 0x88E8;
GL_TEXTURE_2D :u32: 0x0DE1;
GL_TEXTURE_MIN_FILTER :u32: 0x2801;
GL_TEXTURE_MAG_FILTER :u32: 0x2800;
GL_NEAREST :u32: 0x2600;
GL_RGBA :u32: 0x1908;
GL_UNSIGNED_BYTE :u32: 0x1401;
GL_SRC_ALPHA :u32: 0x0302;
GL_ONE_MINUS_SRC_ALPHA :u32: 0x0303;
GL_TEXTURE0 :u32: 0x84C0;
GL_LINEAR :u32: 0x2601;
GL_RED :u32: 0x1903;
GL_R8 :u32: 0x8229;
GL_UNPACK_ALIGNMENT :u32: 0x0CF5;
glScissor : (s32, s32, s32, s32) -> void callconv(.c) = ---;
glBufferSubData : (u32, isize, isize, *void) -> void callconv(.c) = ---;
glGenTextures : (s32, *u32) -> void callconv(.c) = ---;
glBindTexture : (u32, u32) -> void callconv(.c) = ---;
glTexImage2D : (u32, s32, s32, s32, s32, s32, u32, u32, *void) -> void callconv(.c) = ---;
glTexParameteri : (u32, u32, s32) -> void callconv(.c) = ---;
glBlendFunc : (u32, u32) -> void callconv(.c) = ---;
glReadPixels : (s32, s32, s32, s32, u32, u32, *void) -> void callconv(.c) = ---;
glActiveTexture : (u32) -> void callconv(.c) = ---;
glUniform1i : (s32, s32) -> void callconv(.c) = ---;
glPixelStorei : (u32, s32) -> void callconv(.c) = ---;
glTexSubImage2D : (u32, s32, s32, s32, s32, s32, u32, u32, *void) -> void callconv(.c) = ---;
glDeleteTextures : (s32, *u32) -> void callconv(.c) = ---;
glGenFramebuffers : (s32, *u32) -> void callconv(.c) = ---;
glGenRenderbuffers : (s32, *u32) -> void callconv(.c) = ---;
glBindFramebuffer : (u32, u32) -> void callconv(.c) = ---;
glBindRenderbuffer : (u32, u32) -> void callconv(.c) = ---;
glFramebufferRenderbuffer : (u32, u32, u32, u32) -> void callconv(.c) = ---;
glGetRenderbufferParameteriv : (u32, u32, *s32) -> void callconv(.c) = ---;
glCheckFramebufferStatus : (u32) -> u32 callconv(.c) = ---;
GL_TEXTURE_WRAP_S :u32: 0x2802;
GL_TEXTURE_WRAP_T :u32: 0x2803;
GL_CLAMP_TO_EDGE :u32: 0x812F;
// Loader: call once after creating GL context
// Pass in a proc loader (e.g. SDL_GL_GetProcAddress)
load_gl :: (get_proc: ([*]u8) -> *void callconv(.c)) {
glClearColor = xx get_proc("glClearColor");
glClear = xx get_proc("glClear");
glEnable = xx get_proc("glEnable");
glDisable = xx get_proc("glDisable");
glViewport = xx get_proc("glViewport");
glFlush = xx get_proc("glFlush");
glDrawArrays = xx get_proc("glDrawArrays");
glPolygonMode = xx get_proc("glPolygonMode");
glLineWidth = xx get_proc("glLineWidth");
glCreateShader = xx get_proc("glCreateShader");
glShaderSource = xx get_proc("glShaderSource");
glCompileShader = xx get_proc("glCompileShader");
glGetShaderiv = xx get_proc("glGetShaderiv");
glGetShaderInfoLog = xx get_proc("glGetShaderInfoLog");
glCreateProgram = xx get_proc("glCreateProgram");
glAttachShader = xx get_proc("glAttachShader");
glLinkProgram = xx get_proc("glLinkProgram");
glGetProgramiv = xx get_proc("glGetProgramiv");
glGetProgramInfoLog = xx get_proc("glGetProgramInfoLog");
glUseProgram = xx get_proc("glUseProgram");
glDeleteShader = xx get_proc("glDeleteShader");
glGenVertexArrays = xx get_proc("glGenVertexArrays");
glGenBuffers = xx get_proc("glGenBuffers");
glBindVertexArray = xx get_proc("glBindVertexArray");
glBindBuffer = xx get_proc("glBindBuffer");
glBufferData = xx get_proc("glBufferData");
glVertexAttribPointer = xx get_proc("glVertexAttribPointer");
glEnableVertexAttribArray = xx get_proc("glEnableVertexAttribArray");
glGetUniformLocation = xx get_proc("glGetUniformLocation");
glUniformMatrix4fv = xx get_proc("glUniformMatrix4fv");
glUniform3f = xx get_proc("glUniform3f");
glDepthFunc = xx get_proc("glDepthFunc");
glUniform1f = xx get_proc("glUniform1f");
glScissor = xx get_proc("glScissor");
glBufferSubData = xx get_proc("glBufferSubData");
glGenTextures = xx get_proc("glGenTextures");
glBindTexture = xx get_proc("glBindTexture");
glTexImage2D = xx get_proc("glTexImage2D");
glTexParameteri = xx get_proc("glTexParameteri");
glBlendFunc = xx get_proc("glBlendFunc");
glReadPixels = xx get_proc("glReadPixels");
glActiveTexture = xx get_proc("glActiveTexture");
glUniform1i = xx get_proc("glUniform1i");
glPixelStorei = xx get_proc("glPixelStorei");
glTexSubImage2D = xx get_proc("glTexSubImage2D");
glDeleteTextures = xx get_proc("glDeleteTextures");
glGenFramebuffers = xx get_proc("glGenFramebuffers");
glGenRenderbuffers = xx get_proc("glGenRenderbuffers");
glBindFramebuffer = xx get_proc("glBindFramebuffer");
glBindRenderbuffer = xx get_proc("glBindRenderbuffer");
glFramebufferRenderbuffer = xx get_proc("glFramebufferRenderbuffer");
glGetRenderbufferParameteriv = xx get_proc("glGetRenderbufferParameteriv");
glCheckFramebufferStatus = xx get_proc("glCheckFramebufferStatus");
}
// --- Shader utilities ---
create_program :: (vert_src: [:0]u8, frag_src: [:0]u8) -> u32 {
vs := compile_shader(GL_VERTEX_SHADER, vert_src);
fs := compile_shader(GL_FRAGMENT_SHADER, frag_src);
prog := glCreateProgram();
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
status : s32 = 0;
glGetProgramiv(prog, GL_LINK_STATUS, @status);
if status == GL_FALSE {
log_buf: [512]u8 = ---;
glGetProgramInfoLog(prog, 512, null, log_buf);
}
glDeleteShader(vs);
glDeleteShader(fs);
return prog;
}
compile_shader :: (shader_type : u32, source: [:0]u8) -> u32 {
shader := glCreateShader(shader_type);
glShaderSource(shader, 1, source, null);
glCompileShader(shader);
status : s32 = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, @status);
if status == GL_FALSE {
log_buf : [512]u8 = ---;
glGetShaderInfoLog(shader, 512, null, log_buf);
}
return shader;
}