Skip to content

Commit

Permalink
Add type assertions to webgl bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jan 20, 2024
1 parent 860d5e7 commit beaf2d1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
1 change: 0 additions & 1 deletion jsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noFallthroughCasesInSwitch": true,
"verbatimModuleSyntax": true,
"useDefineForClassFields": true
Expand Down
26 changes: 13 additions & 13 deletions wasm/webgl/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ export function makeWebGLState() {
version: 1,
id_counter: 1,
last_error: 0,
buffers: [],
programs: [],
framebuffers: [],
renderbuffers: [],
textures: [],
uniforms: [],
shaders: [],
vaos: [],
queries: [],
samplers: [],
transform_feedbacks: [],
syncs: [],
program_infos: [],
buffers: [null],
programs: [null],
framebuffers: [null],
renderbuffers: [null],
textures: [null],
uniforms: [null],
shaders: [null],
vaos: [null],
queries: [null],
samplers: [null],
transform_feedbacks: [null],
syncs: [null],
program_infos: [null],
}
}

Expand Down
43 changes: 31 additions & 12 deletions wasm/webgl/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ export function makeOdinWebGL(webgl, wasm) {
},
/** @returns {void} */
AttachShader: (/** @type {number} */ program, /** @type {number} */ shader) => {
webgl.ctx.attachShader(webgl.programs[program], webgl.shaders[shader])
webgl.ctx.attachShader(
/** @type {WebGLProgram} */ (webgl.programs[program]),
/** @type {WebGLShader} */ (webgl.shaders[shader]),
)
},
/**
* @param {number} program
Expand All @@ -151,7 +154,11 @@ export function makeOdinWebGL(webgl, wasm) {
*/
BindAttribLocation: (program, index, name_ptr, name_len) => {
const name = mem.load_string_raw(wasm.memory.buffer, name_ptr, name_len)
webgl.ctx.bindAttribLocation(webgl.programs[program], index, name)
webgl.ctx.bindAttribLocation(
/** @type {WebGLProgram} */ (webgl.programs[program]),
index,
name,
)
},
/**
* @param {number} target
Expand Down Expand Up @@ -184,7 +191,7 @@ export function makeOdinWebGL(webgl, wasm) {
// @ts-expect-error
webgl.ctx.currentPixelUnpackBufferBinding = buffer
}
webgl.ctx.bindBuffer(target, buffer ? webgl.buffers[buffer] : null)
webgl.ctx.bindBuffer(target, webgl.buffers[buffer])
},
/**
* @param {number} target
Expand Down Expand Up @@ -311,7 +318,7 @@ export function makeOdinWebGL(webgl, wasm) {
* @returns {void}
*/
CompileShader: shader => {
webgl.ctx.compileShader(webgl.shaders[shader])
webgl.ctx.compileShader(/** @type {WebGLShader} */(webgl.shaders[shader]))
},
CompressedTexImage2D: (
/** @type {number} */ target,
Expand Down Expand Up @@ -567,7 +574,10 @@ export function makeOdinWebGL(webgl, wasm) {
* @returns {void}
*/
DetachShader: (program, shader) => {
webgl.ctx.detachShader(webgl.programs[program], webgl.shaders[shader])
webgl.ctx.detachShader(
/** @type {WebGLProgram} */ (webgl.programs[program]),
/** @type {WebGLShader} */(webgl.shaders[shader]),
)
},
/**
* @param {number} cap
Expand Down Expand Up @@ -676,7 +686,10 @@ export function makeOdinWebGL(webgl, wasm) {
*/
GetAttribLocation: (program, name_ptr, name_len) => {
const name = mem.load_string_raw(wasm.memory.buffer, name_ptr, name_len)
return webgl.ctx.getAttribLocation(webgl.programs[program], name)
return webgl.ctx.getAttribLocation(
/** @type {WebGLProgram} */ (webgl.programs[program]),
name,
)
},
/**
* @param {number} pname
Expand All @@ -691,7 +704,10 @@ export function makeOdinWebGL(webgl, wasm) {
* @returns {number}
*/
GetProgramParameter: (program, pname) => {
return webgl.ctx.getProgramParameter(webgl.programs[program], pname)
return webgl.ctx.getProgramParameter(
/** @type {WebGLProgram} */ (webgl.programs[program]),
pname,
)
},
/**
* @param {number} program
Expand All @@ -703,7 +719,10 @@ export function makeOdinWebGL(webgl, wasm) {
GetProgramInfoLog: (program, buf_ptr, buf_len, length_ptr) => {
if (buf_len <= 0 || !buf_ptr) return

const log = webgl.ctx.getProgramInfoLog(webgl.programs[program]) ?? "(unknown error)"
const log =
webgl.ctx.getProgramInfoLog(
/** @type {WebGLProgram} */ (webgl.programs[program]),
) ?? "(unknown error)"
const n = mem.store_string_raw(wasm.memory.buffer, buf_ptr, buf_len, log)
mem.store_int(new DataView(wasm.memory.buffer), length_ptr, n)
},
Expand All @@ -717,7 +736,7 @@ export function makeOdinWebGL(webgl, wasm) {
GetShaderInfoLog: (shader, buf_ptr, buf_len, length_ptr) => {
if (buf_len <= 0 || !buf_ptr) return

const log = webgl.ctx.getShaderInfoLog(webgl.shaders[shader]) ?? "(unknown error)"
const log = webgl.ctx.getShaderInfoLog(/** @type {WebGLShader} */(webgl.shaders[shader])) ?? "(unknown error)"
const n = mem.store_string_raw(wasm.memory.buffer, buf_ptr, buf_len, log)
mem.store_int(new DataView(wasm.memory.buffer), length_ptr, n)
},
Expand Down Expand Up @@ -850,7 +869,7 @@ export function makeOdinWebGL(webgl, wasm) {
* @returns {void}
*/
LinkProgram: program => {
webgl.ctx.linkProgram(webgl.programs[program])
webgl.ctx.linkProgram(/** @type {WebGLProgram} */ (webgl.programs[program]))
webgl.program_infos[program] = null
populateUniformTable(webgl, program)
},
Expand Down Expand Up @@ -928,7 +947,7 @@ export function makeOdinWebGL(webgl, wasm) {
*/
ShaderSource: (shader, strings_ptr, strings_length) => {
const source = getSource(wasm.memory.buffer, strings_ptr, strings_length)
webgl.ctx.shaderSource(webgl.shaders[shader], source)
webgl.ctx.shaderSource(/** @type {WebGLShader} */(webgl.shaders[shader]), source)
},
/**
* @param {number} func
Expand Down Expand Up @@ -1173,7 +1192,7 @@ export function makeOdinWebGL(webgl, wasm) {
* @returns {void}
*/
ValidateProgram: program => {
webgl.ctx.validateProgram(webgl.programs[program])
webgl.ctx.validateProgram(/** @type {WebGLProgram} */ (webgl.programs[program]))
},
/**
* @param {number} index
Expand Down
54 changes: 38 additions & 16 deletions wasm/webgl/webgl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export function makeOdinWegGL2(
) => {
webgl.ctx.invalidateFramebuffer(
target,
new Uint32Array(wasm.memory.buffer, attachments_ptr, attachments_len),
/** @type {any} */ (
new Uint32Array(wasm.memory.buffer, attachments_ptr, attachments_len)
),
)
},
InvalidateSubFramebuffer: (
Expand All @@ -109,7 +111,9 @@ export function makeOdinWegGL2(
) => {
webgl.ctx.invalidateSubFramebuffer(
target,
new Uint32Array(wasm.memory.buffer, attachments_ptr, attachments_len),
/** @type {any} */ (
new Uint32Array(wasm.memory.buffer, attachments_ptr, attachments_len)
),
x,
y,
width,
Expand Down Expand Up @@ -280,7 +284,7 @@ export function makeOdinWegGL2(
/** @type {number} */ name_len,
) => {
return webgl.ctx.getFragDataLocation(
webgl.programs[program],
/** @type {WebGLProgram} */ (webgl.programs[program]),
mem.load_string_raw(wasm.memory.buffer, name_ptr, name_len),
)
},
Expand Down Expand Up @@ -463,7 +467,9 @@ export function makeOdinWegGL2(

/* Multiple Render Targets */
DrawBuffers: (/** @type {number} */ buffers_ptr, /** @type {number} */ buffers_len) => {
webgl.ctx.drawBuffers(mem.load_u32_array(wasm.memory.buffer, buffers_ptr, buffers_len))
webgl.ctx.drawBuffers(
/** @type {any} */ (new Uint32Array(wasm.memory.buffer, buffers_ptr, buffers_len)),
)
},
ClearBufferfv: (
/** @type {number} */ buffer,
Expand All @@ -474,7 +480,7 @@ export function makeOdinWegGL2(
webgl.ctx.clearBufferfv(
buffer,
drawbuffer,
mem.load_f32_array(wasm.memory.buffer, values_ptr, values_len),
new Float32Array(wasm.memory.buffer, values_ptr, values_len),
)
},
ClearBufferiv: (
Expand All @@ -486,7 +492,7 @@ export function makeOdinWegGL2(
webgl.ctx.clearBufferiv(
buffer,
drawbuffer,
mem.load_i32_array(wasm.memory.buffer, values_ptr, values_len),
new Int32Array(wasm.memory.buffer, values_ptr, values_len),
)
},
ClearBufferuiv: (
Expand All @@ -498,7 +504,7 @@ export function makeOdinWegGL2(
webgl.ctx.clearBufferuiv(
buffer,
drawbuffer,
mem.load_u32_array(wasm.memory.buffer, values_ptr, values_len),
new Uint32Array(wasm.memory.buffer, values_ptr, values_len),
)
},
ClearBufferfi: (
Expand Down Expand Up @@ -547,7 +553,7 @@ export function makeOdinWegGL2(
* @param {number} query
*/
BeginQuery: (target, query) => {
webgl.ctx.beginQuery(target, webgl.queries[query])
webgl.ctx.beginQuery(target, /** @type {WebGLQuery} */ (webgl.queries[query]))
},
/** @param {number} target */
EndQuery: target => {
Expand Down Expand Up @@ -619,7 +625,11 @@ export function makeOdinWegGL2(
* @returns {void}
*/
SamplerParameteri: (sampler, pname, param) => {
webgl.ctx.samplerParameteri(webgl.samplers[sampler], pname, param)
webgl.ctx.samplerParameteri(
/** @type {WebGLSampler} */ (webgl.samplers[sampler]),
pname,
param,
)
},
/**
* @param {number} sampler
Expand All @@ -628,7 +638,11 @@ export function makeOdinWegGL2(
* @returns {void}
*/
SamplerParameterf: (sampler, pname, param) => {
webgl.ctx.samplerParameterf(webgl.samplers[sampler], pname, param)
webgl.ctx.samplerParameterf(
/** @type {WebGLSampler} */ (webgl.samplers[sampler]),
pname,
param,
)
},

/*
Expand Down Expand Up @@ -674,7 +688,11 @@ export function makeOdinWegGL2(
* @returns {number}
*/
ClientWaitSync: (sync, flags, timeout) => {
return webgl.ctx.clientWaitSync(webgl.syncs[sync], flags, timeout)
return webgl.ctx.clientWaitSync(
/** @type {WebGLSync} */ (webgl.syncs[sync]),
flags,
timeout,
)
},
/**
* @param {number} sync
Expand All @@ -683,7 +701,7 @@ export function makeOdinWegGL2(
* @returns {void}
*/
WaitSync: (sync, flags, timeout) => {
webgl.ctx.waitSync(webgl.syncs[sync], flags, timeout)
webgl.ctx.waitSync(/** @type {WebGLSync} */ (webgl.syncs[sync]), flags, timeout)
},

/*
Expand Down Expand Up @@ -738,7 +756,11 @@ export function makeOdinWegGL2(
) => {
const data = new DataView(wasm.memory.buffer)
const varyings = mem.load_strings(data, varyings_ptr, varyings_len)
webgl.ctx.transformFeedbackVaryings(webgl.programs[program], varyings, buffer_mode)
webgl.ctx.transformFeedbackVaryings(
/** @type {WebGLProgram} */ (webgl.programs[program]),
varyings,
buffer_mode,
)
},
/** @returns {void} */
PauseTransformFeedback: () => {
Expand Down Expand Up @@ -778,7 +800,7 @@ export function makeOdinWegGL2(
/** @type {number} */ name_len,
) => {
return webgl.ctx.getUniformBlockIndex(
webgl.programs[program],
/** @type {WebGLProgram} */ (webgl.programs[program]),
mem.load_string_raw(wasm.memory.buffer, name_ptr, name_len),
)
},
Expand All @@ -791,7 +813,7 @@ export function makeOdinWegGL2(
/** @type {number} */ length_ptr,
) => {
const name = webgl.ctx.getActiveUniformBlockName(
webgl.programs[program],
/** @type {WebGLProgram} */ (webgl.programs[program]),
uniform_block_index,
)
if (!name) {
Expand All @@ -808,7 +830,7 @@ export function makeOdinWegGL2(
/** @type {number} */ uniformBlockBinding,
) => {
webgl.ctx.uniformBlockBinding(
webgl.programs[program],
/** @type {WebGLProgram} */ (webgl.programs[program]),
uniformBlockIndex,
uniformBlockBinding,
)
Expand Down

0 comments on commit beaf2d1

Please sign in to comment.