Merge pull request #6069 from karl-zylinski/webgl-binding-fixes

WebGL binding additions
This commit is contained in:
gingerBill
2025-12-30 13:38:40 +00:00
committed by GitHub
4 changed files with 195 additions and 8 deletions

View File

@@ -22,6 +22,8 @@ foreign dom_lib {
window_set_scroll :: proc(x, y: f64) ---
set_element_style :: proc(id: string, key: string, value: string) ---
set_document_title :: proc(title: string) ---
}
get_element_value_string :: proc "contextless" (id: string, buf: []byte) -> string {

View File

@@ -616,6 +616,9 @@ class WebGLInterface {
FramebufferTexture2D: (target, attachment, textarget, texture, level) => {
this.ctx.framebufferTexture2D(target, attachment, textarget, this.textures[texture], level);
},
CheckFramebufferStatus: (target) => {
return this.ctx.checkFramebufferStatus(target)
},
FrontFace: (mode) => {
this.ctx.frontFace(mode);
},
@@ -625,6 +628,47 @@ class WebGLInterface {
this.ctx.generateMipmap(target);
},
GetActiveAttrib: (program, index, size_ptr, type_ptr, name_buf_ptr, name_buf_len, name_len_ptr) => {
const info = this.ctx.getActiveAttrib(this.programs[program], index);
if (size_ptr) {
this.mem.storeInt(size_ptr, info.size);
}
if (type_ptr) {
this.mem.storeI32(type_ptr, info.type);
}
if (name_buf_ptr && name_buf_len > 0) {
let n = Math.min(name_buf_len, info.name.length);
let name = info.name.substring(0, n);
this.mem.loadBytes(name_buf_ptr, name_buf_len).set(new TextEncoder().encode(name));
this.mem.storeInt(name_len_ptr, n);
} else if (name_len_ptr) {
this.mem.storeInt(name_len_ptr, info.name.length);
}
},
GetActiveUniform: (program, index, size_ptr, type_ptr, name_buf_ptr, name_buf_len, name_len_ptr) => {
let info = this.ctx.getActiveUniform(this.programs[program], index);
if (size_ptr) {
this.mem.storeInt(size_ptr, info.size);
}
if (type_ptr) {
this.mem.storeI32(type_ptr, info.type);
}
if (name_buf_ptr && name_buf_len > 0) {
let n = Math.min(name_buf_len, info.name.length);
let name = info.name.substring(0, n);
this.mem.loadBytes(name_buf_ptr, name_buf_len).set(new TextEncoder().encode(name));
this.mem.storeInt(name_len_ptr, n);
} else if (name_len_ptr) {
this.mem.storeInt(name_len_ptr, info.name.length);
}
},
GetAttribLocation: (program, name_ptr, name_len) => {
let name = this.mem.loadString(name_ptr, name_len);
@@ -1260,15 +1304,34 @@ class WebGLInterface {
this.assertWebGL2();
return this.ctx.getUniformBlockIndex(this.programs[program], this.mem.loadString(uniformBlockName_ptr, uniformBlockName_len));
},
// any getActiveUniformBlockParameter(WebGLProgram program, GLuint uniformBlockIndex, GLenum pname);
GetActiveUniformBlockName: (program, uniformBlockIndex, buf_ptr, buf_len, length_ptr) => {
GetActiveUniformBlockName: (program, uniformBlockIndex, name_buf_ptr, name_buf_len, name_length_ptr) => {
this.assertWebGL2();
let name = this.ctx.getActiveUniformBlockName(this.programs[program], uniformBlockIndex);
let n = Math.min(buf_len, name.length);
name = name.substring(0, n);
this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder().encode(name))
this.mem.storeInt(length_ptr, n);
if (name_buf_ptr && name_buf_len > 0) {
let n = Math.min(name_buf_len, name.length);
name = name.substring(0, n);
this.mem.loadBytes(name_buf_ptr, name_buf_len).set(new TextEncoder().encode(name));
this.mem.storeInt(name_length_ptr, n);
} else if (name_length_ptr) {
this.mem.storeInt(name_length_ptr, name.length);
}
},
GetActiveUniforms: (program, uniformIndices_ptr, uniformIndices_len, pname, res_ptr) => {
this.assertWebGL2();
let indices = this.mem.loadU32Array(uniformIndices_ptr, uniformIndices_len);
this.ctx.getActiveUniforms(this.programs[program], indices, pname)
this.mem.loadI32Array(res_ptr, indices.length).set(indices)
},
GetActiveUniformBlockParameter: (program, uniformBlockIndex, pname, params_ptr) => {
this.assertWebGL2();
let res = this.ctx.getActiveUniformBlockParameter(this.programs[program], uniformBlockIndex, pname);
if (res instanceof Uint32Array) { // for pname GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES
this.mem.loadU32Array(params_ptr, res.length).set(res)
} else {
this.mem.storeI32(params_ptr, res)
}
},
UniformBlockBinding: (program, uniformBlockIndex, uniformBlockBinding) => {
this.assertWebGL2();
@@ -1840,6 +1903,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory) {
return false;
},
// Writes a struct of type `Gamepad_State`, see `core/sys/wasm/js/events.odin`
get_gamepad_state: (gamepad_id, ep) => {
let index = gamepad_id;
let gps = navigator.getGamepads();
@@ -1986,6 +2050,11 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory) {
}
},
set_document_title: (title_ptr, title_len) => {
let title = wasmMemoryInterface.loadString(title_ptr, title_len);
document.title = title;
},
get_element_key_f64: (id_ptr, id_len, key_ptr, key_len) => {
let id = wasmMemoryInterface.loadString(id_ptr, id_len);
let key = wasmMemoryInterface.loadString(key_ptr, key_len);

View File

@@ -3,6 +3,7 @@ package webgl
foreign import "webgl"
import glm "core:math/linalg/glsl"
import "base:runtime"
Enum :: distinct u32
@@ -25,6 +26,12 @@ ContextAttribute :: enum u32 {
}
ContextAttributes :: distinct bit_set[ContextAttribute; u32]
ActiveInfo :: struct {
size: int,
type: Enum,
name: string,
}
DEFAULT_CONTEXT_ATTRIBUTES :: ContextAttributes{}
@(default_calling_convention="contextless")
@@ -106,6 +113,7 @@ foreign webgl {
Flush :: proc() ---
FramebufferRenderbuffer :: proc(target, attachment, renderbufertarget: Enum, renderbuffer: Renderbuffer) ---
FramebufferTexture2D :: proc(target, attachment, textarget: Enum, texture: Texture, level: i32) ---
CheckFramebufferStatus :: proc(target: Enum) -> Enum ---
FrontFace :: proc(mode: Enum) ---
GenerateMipmap :: proc(target: Enum) ---
@@ -260,7 +268,6 @@ UniformMatrix4fv :: proc "contextless" (location: i32, m: glm.mat4) {
value := transmute([4*4]f32)m
_UniformMatrix4fv(location, &value[0])
}
GetShaderiv :: proc "contextless" (shader: Shader, pname: Enum) -> (p: i32) {
foreign webgl {
@(link_name="GetShaderiv")
@@ -270,6 +277,79 @@ GetShaderiv :: proc "contextless" (shader: Shader, pname: Enum) -> (p: i32) {
return
}
GetActiveAttribBuf :: proc "contextless" (program: Program, index: u32, name_buf: []byte) -> (info: ActiveInfo) {
foreign webgl {
@(link_name="GetActiveAttrib")
_GetActiveAttrib :: proc "contextless" (shader: Program, index: u32, size: ^int, type: ^Enum, name_buf: []byte, name_len: ^int) ---
}
name_len: int
_GetActiveAttrib(program, index, &info.size, &info.type, name_buf, &name_len)
info.name = string(name_buf[:name_len])
return
}
GetActiveAttribAlloc :: proc(program: Program, index: u32, allocator: runtime.Allocator, loc := #caller_location) -> (info: ActiveInfo) {
foreign webgl {
@(link_name="GetActiveAttrib")
_GetActiveAttrib :: proc "contextless" (shader: Program, index: u32, size: ^int, type: ^Enum, name_buf: []byte, name_len: ^int) ---
}
name_len: int
// Passing {} to the buf but giving it a name_len ptr will write the needed len into that int
_GetActiveAttrib(program, index, &info.size, &info.type, {}, &name_len)
if name_len > 0 {
name_buf := make([]byte, name_len, allocator, loc)
_GetActiveAttrib(program, index, &info.size, &info.type, name_buf, &name_len)
assert(name_len == len(name_buf))
info.name = string(name_buf[:name_len])
}
return
}
GetActiveAttrib :: proc {
GetActiveAttribBuf,
GetActiveAttribAlloc,
}
GetActiveUniformBuf :: proc "contextless" (program: Program, index: u32, name_buf: []byte) -> (info: ActiveInfo) {
foreign webgl {
@(link_name="GetActiveUniform")
_GetActiveUniform :: proc "contextless" (shader: Program, index: u32, size: ^int, type: ^Enum, name_buf: []byte, name_len: ^int) ---
}
name_len: int
_GetActiveUniform(program, index, &info.size, &info.type, name_buf, &name_len)
info.name = string(name_buf[:name_len])
return
}
GetActiveUniformAlloc :: proc(program: Program, index: u32, allocator: runtime.Allocator, loc := #caller_location) -> (info: ActiveInfo) {
foreign webgl {
@(link_name="GetActiveUniform")
_GetActiveUniform :: proc "contextless" (shader: Program, index: u32, size: ^int, type: ^Enum, name_buf: []byte, name_len: ^int) ---
}
name_len: int
// Passing {} to the buf but giving it a name_len ptr will write the needed len into that int
_GetActiveUniform(program, index, &info.size, &info.type, {}, &name_len)
if name_len > 0 {
name_buf := make([]byte, name_len, allocator, loc)
_GetActiveUniform(program, index, &info.size, &info.type, name_buf, &name_len)
assert(name_len == len(name_buf))
info.name = string(name_buf[:name_len])
}
return
}
GetActiveUniform :: proc {
GetActiveUniformBuf,
GetActiveUniformAlloc,
}
GetProgramInfoLog :: proc "contextless" (program: Program, buf: []byte) -> string {
foreign webgl {

View File

@@ -3,6 +3,7 @@ package webgl
foreign import "webgl2"
import "base:intrinsics"
import "base:runtime"
import glm "core:math/linalg/glsl"
Query :: distinct u32
@@ -103,14 +104,21 @@ foreign webgl2 {
GetUniformBlockIndex :: proc(program: Program, uniformBlockName: string) -> i32 ---
UniformBlockBinding :: proc(program: Program, uniformBlockIndex: i32, uniformBlockBinding: i32) ---
// if `pname` is `UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES` then an array will be written at
// `params`, in that case the length `params` need to have is given first querying using `pname`
// `UNIFORM_BLOCK_ACTIVE_UNIFORMS`.
GetActiveUniformBlockParameter :: proc(program: Program, uniformBlockIndex: i32, pname: Enum, params: [^]i32) ---
GetActiveUniforms :: proc(program: Program, uniformIndices: []u32, pname: Enum, res: [^]i32) ---
CreateVertexArray :: proc() -> VertexArrayObject ---
DeleteVertexArray :: proc(vertexArray: VertexArrayObject) ---
IsVertexArray :: proc(vertexArray: VertexArrayObject) -> bool ---
BindVertexArray :: proc(vertexArray: VertexArrayObject) ---
}
GetActiveUniformBlockName :: proc(program: Program, uniformBlockIndex: i32, buf: []byte) -> string {
GetActiveUniformBlockNameBuf :: proc(program: Program, uniformBlockIndex: i32, buf: []byte) -> string {
foreign webgl2 {
@(link_name="GetActiveUniformBlockName")
_GetActiveUniformBlockName :: proc "contextless" (program: Program, uniformBlockIndex: i32, buf: []byte, length: ^int) ---
}
n: int
@@ -118,6 +126,28 @@ GetActiveUniformBlockName :: proc(program: Program, uniformBlockIndex: i32, buf:
return string(buf[:n])
}
GetActiveUniformBlockNameAlloc :: proc(program: Program, uniformBlockIndex: i32, allocator: runtime.Allocator, loc := #caller_location) -> string {
foreign webgl2 {
@(link_name="GetActiveUniformBlockName")
_GetActiveUniformBlockName :: proc "contextless" (program: Program, uniformBlockIndex: i32, buf: []byte, length: ^int) ---
}
n: int
_GetActiveUniformBlockName(program, uniformBlockIndex, {}, &n)
if n > 0 {
buf := make([]byte, n, allocator, loc)
_GetActiveUniformBlockName(program, uniformBlockIndex, buf, &n)
assert(n == len(buf))
return string(buf[:n])
}
return ""
}
GetActiveUniformBlockName :: proc {
GetActiveUniformBlockNameBuf,
GetActiveUniformBlockNameAlloc,
}
Uniform1uiv :: proc "contextless" (location: i32, v: u32) {
Uniform1ui(location, v)
@@ -134,6 +164,7 @@ Uniform4uiv :: proc "contextless" (location: i32, v: glm.uvec4) {
UniformMatrix3x2fv :: proc "contextless" (location: i32, m: glm.mat3x2) {
foreign webgl2 {
@(link_name="UniformMatrix3x2fv")
_UniformMatrix3x2fv :: proc "contextless" (location: i32, addr: [^]f32) ---
}
array := intrinsics.matrix_flatten(m)
@@ -141,6 +172,7 @@ UniformMatrix3x2fv :: proc "contextless" (location: i32, m: glm.mat3x2) {
}
UniformMatrix4x2fv :: proc "contextless" (location: i32, m: glm.mat4x2) {
foreign webgl2 {
@(link_name="UniformMatrix4x2fv")
_UniformMatrix4x2fv :: proc "contextless" (location: i32, addr: [^]f32) ---
}
array := intrinsics.matrix_flatten(m)
@@ -148,6 +180,7 @@ UniformMatrix4x2fv :: proc "contextless" (location: i32, m: glm.mat4x2) {
}
UniformMatrix2x3fv :: proc "contextless" (location: i32, m: glm.mat2x3) {
foreign webgl2 {
@(link_name="UniformMatrix2x3fv")
_UniformMatrix2x3fv :: proc "contextless" (location: i32, addr: [^]f32) ---
}
array := intrinsics.matrix_flatten(m)
@@ -155,6 +188,7 @@ UniformMatrix2x3fv :: proc "contextless" (location: i32, m: glm.mat2x3) {
}
UniformMatrix4x3fv :: proc "contextless" (location: i32, m: glm.mat4x3) {
foreign webgl2 {
@(link_name="UniformMatrix4x3fv")
_UniformMatrix4x3fv :: proc "contextless" (location: i32, addr: [^]f32) ---
}
array := intrinsics.matrix_flatten(m)
@@ -162,6 +196,7 @@ UniformMatrix4x3fv :: proc "contextless" (location: i32, m: glm.mat4x3) {
}
UniformMatrix2x4fv :: proc "contextless" (location: i32, m: glm.mat2x4) {
foreign webgl2 {
@(link_name="UniformMatrix2x4fv")
_UniformMatrix2x4fv :: proc "contextless" (location: i32, addr: [^]f32) ---
}
array := intrinsics.matrix_flatten(m)
@@ -169,6 +204,7 @@ UniformMatrix2x4fv :: proc "contextless" (location: i32, m: glm.mat2x4) {
}
UniformMatrix3x4fv :: proc "contextless" (location: i32, m: glm.mat3x4) {
foreign webgl2 {
@(link_name="UniformMatrix3x4fv")
_UniformMatrix3x4fv :: proc "contextless" (location: i32, addr: [^]f32) ---
}
array := intrinsics.matrix_flatten(m)