diff --git a/src/core/CoreTextureManager.ts b/src/core/CoreTextureManager.ts index 8934e1ed..cab7c89a 100644 --- a/src/core/CoreTextureManager.ts +++ b/src/core/CoreTextureManager.ts @@ -25,6 +25,8 @@ import { NoiseTexture } from './textures/NoiseTexture.js'; import { SubTexture } from './textures/SubTexture.js'; import { RenderTexture } from './textures/RenderTexture.js'; import type { Texture } from './textures/Texture.js'; +import type { CorePlatform } from './platforms/CorePlatform.js'; +import type { WebPlatform } from './platforms/web/WebPlatform.js'; /** * Augmentable map of texture class types @@ -139,6 +141,10 @@ export interface TextureOptions { } export class CoreTextureManager { + private platform: CorePlatform | WebPlatform; + public createImageBitmap: typeof createImageBitmap; + public hasCreateImageBitmap = false; + /** * Map of textures by cache key */ @@ -155,8 +161,8 @@ export class CoreTextureManager { txConstructors: Partial = {}; imageWorkerManager: ImageWorkerManager | null = null; - hasCreateImageBitmap = !!self.createImageBitmap; hasWorker = !!self.Worker; + /** * Renderer that this texture manager is associated with * @@ -177,7 +183,12 @@ export class CoreTextureManager { */ frameTime = 0; - constructor(numImageWorkers: number) { + constructor(numImageWorkers: number, platform: CorePlatform | WebPlatform) { + this.platform = platform; + + this.createImageBitmap = platform.createImageBitmap; + this.hasCreateImageBitmap = !!this.createImageBitmap; + // Register default known texture types if (this.hasCreateImageBitmap && this.hasWorker && numImageWorkers > 0) { this.imageWorkerManager = new ImageWorkerManager(numImageWorkers); diff --git a/src/core/Stage.ts b/src/core/Stage.ts index 2da105b7..323a04d9 100644 --- a/src/core/Stage.ts +++ b/src/core/Stage.ts @@ -122,7 +122,7 @@ export class Stage { private frameEventQueue: [name: string, payload: unknown][] = []; private fontResolveMap: Record = {}; - private platform: CorePlatform; + private platform: CorePlatform | WebPlatform | null = null; /// Debug data contextSpy: ContextSpy | null = null; @@ -152,7 +152,6 @@ export class Stage { this.shManager = new CoreShaderManager(); this.animationManager = new AnimationManager(); this.contextSpy = enableContextSpy ? new ContextSpy() : null; - this.platform = platform; let bm = [0, 0, 0, 0] as [number, number, number, number]; if (boundsMargin) { @@ -162,6 +161,11 @@ export class Stage { } this.boundsMargin = bm; + assertTruthy( + platform !== null, + 'A CorePlatform is not provided in the options', + ); + const rendererOptions: CoreRendererOptions = { stage: this, platform, @@ -264,7 +268,7 @@ export class Stage { } updateFrameTime() { - const newFrameTime = this.platform.getTimeStamp(); + const newFrameTime = this.platform?.getTimeStamp() || Date.now(); this.lastFrameTime = this.currentFrameTime; this.currentFrameTime = newFrameTime; this.deltaTime = !this.lastFrameTime diff --git a/src/core/lib/utils.ts b/src/core/lib/utils.ts index 0a5dc386..eb73a715 100644 --- a/src/core/lib/utils.ts +++ b/src/core/lib/utils.ts @@ -285,3 +285,37 @@ export function convertUrlToAbsolute(url: string): string { const absoluteUrl = new URL(url, self.location.href); return absoluteUrl.href; } + +/** + * Compare two arrays for equality. + * + * @remarks + * This function will not try to compare nested arrays or Float32Arrays and + * instead will always return false when they are encountered. + * + * @param a + * @param b + * @returns + */ +export function compareArrays(a: T[], b: T[]): boolean { + if (a.length !== b.length) { + return false; + } + + let result = false; + for (let i = 0; i < a.length; i++) { + if (Array.isArray(a[i]) || a[i] instanceof Float32Array) { + result = false; + break; + } + + if (a[i] !== b[i]) { + result = false; + break; + } + + result = true; + } + + return result; +} diff --git a/src/core/platforms/CoreGlContext.ts b/src/core/platforms/CoreGlContext.ts index 698dd623..f9b41fc0 100644 --- a/src/core/platforms/CoreGlContext.ts +++ b/src/core/platforms/CoreGlContext.ts @@ -18,6 +18,67 @@ */ export abstract class CoreGlContext { + //#region Cached WebGL State + protected abstract activeTextureUnit: number | undefined; + protected abstract texture2dUnits: Array; + protected abstract texture2dParams: WeakMap< + WebGLTexture, + Record + >; + protected abstract scissorEnabled: boolean; + protected abstract scissorX: number; + protected abstract scissorY: number; + protected abstract scissorWidth: number; + protected abstract scissorHeight: number; + protected abstract blendEnabled: boolean; + protected abstract blendSrcRgb: number; + protected abstract blendDstRgb: number; + protected abstract blendSrcAlpha: number; + protected abstract blendDstAlpha: number; + protected abstract boundArrayBuffer: WebGLBuffer | null; + protected abstract boundElementArrayBuffer: WebGLBuffer | null; + protected abstract curProgram: WebGLProgram | null; + //#endregion Cached WebGL State + + //#region Canvas + public abstract readonly canvas: HTMLCanvasElement | OffscreenCanvas; + //#endregion Canvas + + //#region WebGL Enums + public abstract readonly MAX_RENDERBUFFER_SIZE: GLenum; + public abstract readonly MAX_TEXTURE_SIZE: GLenum; + public abstract readonly MAX_VIEWPORT_DIMS: GLenum; + public abstract readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS: GLenum; + public abstract readonly MAX_TEXTURE_IMAGE_UNITS: GLenum; + public abstract readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS: GLenum; + public abstract readonly MAX_VERTEX_ATTRIBS: GLenum; + public abstract readonly MAX_VARYING_VECTORS: GLenum; + public abstract readonly MAX_VERTEX_UNIFORM_VECTORS: GLenum; + public abstract readonly MAX_FRAGMENT_UNIFORM_VECTORS: GLenum; + public abstract readonly TEXTURE_MAG_FILTER: GLenum; + public abstract readonly TEXTURE_MIN_FILTER: GLenum; + public abstract readonly TEXTURE_WRAP_S: GLenum; + public abstract readonly TEXTURE_WRAP_T: GLenum; + public abstract readonly LINEAR: GLenum; + public abstract readonly CLAMP_TO_EDGE: GLenum; + public abstract readonly RGBA: GLenum; + public abstract readonly UNSIGNED_BYTE: GLenum; + public abstract readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL: GLenum; + public abstract readonly UNPACK_FLIP_Y_WEBGL: GLenum; + public abstract readonly FLOAT: GLenum; + public abstract readonly TRIANGLES: GLenum; + public abstract readonly UNSIGNED_SHORT: GLenum; + public abstract readonly ONE: GLenum; + public abstract readonly ONE_MINUS_SRC_ALPHA: GLenum; + public abstract readonly VERTEX_SHADER: GLenum; + public abstract readonly FRAGMENT_SHADER: GLenum; + public abstract readonly STATIC_DRAW: GLenum; + public abstract readonly COMPILE_STATUS: GLenum; + public abstract readonly LINK_STATUS: GLenum; + public abstract readonly DYNAMIC_DRAW: GLenum; + public abstract readonly COLOR_ATTACHMENT0: GLenum; + //#endregion WebGL Enums + /** * Sets the active texture unit. * @param textureUnit - The index of the texture unit to activate. @@ -310,6 +371,17 @@ export abstract class CoreGlContext { v2: number, ): void; + /** + * Sets the value of a vec3 array uniform variable. + * + * @param location - The location of the uniform variable. + * @param value - The array of vec3 values to set. + */ + abstract uniform3fv( + location: WebGLUniformLocation | null, + value: Float32Array | number[], + ): void; + /** * Sets the value of a vec4 (4-component float vector) uniform variable. * @param location - The location of the uniform variable. @@ -357,7 +429,7 @@ export abstract class CoreGlContext { ): void; /** - * Sets the value of a 4x4 float matrix uniform variable. + * Sets the value of a 4x4 float matrix uniform variablisWebGl2e. * @param location - The location of the uniform variable. * @param value - The matrix to set (must be 4x4). */ @@ -385,6 +457,28 @@ export abstract class CoreGlContext { v1: number, ): void; + /** + * Sets the value of a vec2 array uniform variable. + * + * @param location - The location of the uniform variable. + * @param value - The array of vec2 values to set. + */ + abstract uniform2fv( + location: WebGLUniformLocation | null, + value: Float32Array | number[], + ): void; + + /** + * Sets the value of an ivec2 array uniform variable. + * + * @param location - The location of the uniform variable. + * @param value - The array of ivec2 values to set. + */ + abstract uniform2iv( + location: WebGLUniformLocation | null, + value: Int32Array | number[], + ): void; + /** * Sets the value of a vec3 (3-component integer vector) uniform variable. * @param location - The location of the uniform variable. @@ -399,6 +493,17 @@ export abstract class CoreGlContext { v2: number, ): void; + /** + * Sets the value of an ivec3 array uniform variable. + * + * @param location - The location of the uniform variable. + * @param value - The array of ivec3 values to set. + */ + abstract uniform3iv( + location: WebGLUniformLocation | null, + value: Int32Array | number[], + ): void; + /** * Sets the value of a vec4 (4-component integer vector) uniform variable. * @param location - The location of the uniform variable. @@ -425,6 +530,28 @@ export abstract class CoreGlContext { value: Int32Array | number[], ): void; + /** + * Sets the value of an ivec4 array uniform variable. + * + * @param location - The location of the uniform variable. + * @param value - The array of ivec4 values to set. + */ + abstract uniform4iv( + location: WebGLUniformLocation | null, + value: Int32Array | number[], + ): void; + + /** + * Sets the value of a vec4 array uniform variable. + * + * @param location - The location of the uniform variable. + * @param value - The array of vec4 values to set. + */ + abstract uniform4fv( + location: WebGLUniformLocation | null, + value: Float32Array | number[], + ): void; + //////////////////////// // Viewport and Scissor Management //////////////////////// @@ -470,6 +597,10 @@ export abstract class CoreGlContext { */ abstract blendFunc(src: GLenum, dst: GLenum): void; + //////////////////////// + // Main + //////////////////////// + /** * Sets the color values used when clearing the color buffer. * @param red - The red component of the clear color. @@ -488,7 +619,7 @@ export abstract class CoreGlContext { * Clears buffers to preset values. * @param mask - A bitmask indicating which buffer(s) to clear (e.g., `gl.COLOR_BUFFER_BIT`). */ - abstract clear(mask: GLbitfield): void; + abstract clear(mask?: GLbitfield): void; /** * Renders primitives from array data. @@ -504,6 +635,109 @@ export abstract class CoreGlContext { offset: GLintptr, ): void; + /** + * ``` + * gl.getAttribLocation(program, name); + * ``` + * + * @param program + * @param name + * @returns + */ + abstract getAttribLocation(program: WebGLProgram, name: string): GLint; + + /** + * ``` + * gl.enableVertexAttribArray(index); + * ``` + * + * @param index + */ + abstract enableVertexAttribArray(index: number): void; + + /** + * ``` + * gl.disableVertexAttribArray(index); + * ``` + * + * @param index + */ + abstract disableVertexAttribArray(index: number): void; + + /** + * ``` + * gl.bindBuffer(gl.ARRAY_BUFFER, buffer); + * gl.bufferData(gl.ARRAY_BUFFER, data, usage); + * ``` + * + * @remarks + * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function. + * + * @param buffer + * @param data + * @param usage + */ + abstract arrayBufferData( + buffer: WebGLBuffer | null, + data: ArrayBufferView, + usage: GLenum, + ): void; + + /** + * ``` + * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer); + * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage); + * ``` + * @remarks + * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function. + * + * @param buffer + * @param data + * @param usage + */ + abstract elementArrayBufferData( + buffer: WebGLBuffer | null, + data: ArrayBufferView, + usage: GLenum, + ): void; + + /** + * ``` + * gl.bindBuffer(gl.ARRAY_BUFFER, buffer); + * gl.vertexAttribPointer(index, size, type, normalized, stride, offset); + * ``` + * + * @remarks + * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function. + * + * @param buffer + * @param index + * @param size + * @param type + * @param normalized + * @param stride + * @param offset + */ + abstract vertexAttribPointer( + buffer: WebGLBuffer, + index: GLuint, + size: GLint, + type: GLenum, + normalized: GLboolean, + stride: GLsizei, + offset: GLintptr, + ): void; + + /** + * ``` + * gl.pixelStorei(pname, param); + * ``` + * + * @param pname + * @param param + */ + abstract pixelStorei(pname: GLenum, param: GLint | GLboolean): void; + /** * Renders primitives from the currently bound array data. * @param mode - The kind of primitives to render (e.g., `gl.TRIANGLES`). @@ -513,6 +747,34 @@ export abstract class CoreGlContext { // Currently not used in L3 renderer // abstract drawArrays(mode: GLenum, first: GLint, count: GLsizei): void; + //////////////////////// + // WebGL2 + //////////////////////// + + /** + * ``` + * gl.createVertexArray(); + * ``` + * + * @remarks + * This is a WebGL2 only function + * + * @returns + */ + abstract createVertexArray(): void; + + /** + * ``` + * gl.bindVertexArray(vertexArray); + * ``` + * + * @remarks + * This is a WebGL2 only function + * + * @param vertexArray + */ + abstract bindVertexArray(vertexArray: WebGLVertexArrayObject | null): void; + //////////////////////// // Extension handling //////////////////////// @@ -522,7 +784,7 @@ export abstract class CoreGlContext { * @param name - The name of the extension to retrieve. * @returns The extension object, or null if the extension is not supported. */ - abstract getExtension(name: string): any; + abstract getExtension(name: string): string | null; //////////////////////// // Query parameters and states @@ -533,7 +795,7 @@ export abstract class CoreGlContext { * @param pname - The name of the parameter to query (e.g., `gl.MAX_TEXTURE_SIZE`). * @returns The value of the requested parameter. */ - abstract getParameter(pname: GLenum): any; + abstract getParameter(pname: GLenum): GLenum; /** * Determines if the current WebGL context is WebGL2. diff --git a/src/core/platforms/web/WebGlContext.ts b/src/core/platforms/web/WebGlContext.ts index 54682e13..f1522f6e 100644 --- a/src/core/platforms/web/WebGlContext.ts +++ b/src/core/platforms/web/WebGlContext.ts @@ -28,29 +28,29 @@ import { CoreGlContext } from '../CoreGlContext.js'; */ export class WebGlContext extends CoreGlContext { //#region Cached WebGL State - private activeTextureUnit = 0; - private texture2dUnits: Array; - private texture2dParams: WeakMap< + protected activeTextureUnit = 0; + protected texture2dUnits: Array; + protected texture2dParams: WeakMap< WebGLTexture, Record > = new WeakMap(); - private scissorEnabled; - private scissorX: number; - private scissorY: number; - private scissorWidth: number; - private scissorHeight: number; - private blendEnabled; - private blendSrcRgb: number; - private blendDstRgb: number; - private blendSrcAlpha: number; - private blendDstAlpha: number; - private boundArrayBuffer: WebGLBuffer | null; - private boundElementArrayBuffer: WebGLBuffer | null; - private curProgram: WebGLProgram | null; + protected scissorEnabled; + protected scissorX: number; + protected scissorY: number; + protected scissorWidth: number; + protected scissorHeight: number; + protected blendEnabled; + protected blendSrcRgb: number; + protected blendDstRgb: number; + protected blendSrcAlpha: number; + protected blendDstAlpha: number; + protected boundArrayBuffer: WebGLBuffer | null; + protected boundElementArrayBuffer: WebGLBuffer | null; + protected curProgram: WebGLProgram | null; //#endregion Cached WebGL State //#region Canvas - public readonly canvas; + public readonly canvas: HTMLCanvasElement | OffscreenCanvas; //#endregion Canvas //#region WebGL Enums @@ -199,9 +199,8 @@ export class WebGlContext extends CoreGlContext { * @param textureUnit */ activeTexture(textureUnit: number) { - const { gl } = this; if (this.activeTextureUnit !== textureUnit) { - gl.activeTexture(textureUnit + gl.TEXTURE0); + this.gl.activeTexture(textureUnit + this.gl.TEXTURE0); this.activeTextureUnit = textureUnit; } } @@ -227,9 +226,8 @@ export class WebGlContext extends CoreGlContext { } private _getActiveTexture(): WebGLTexture | null { - const { activeTextureUnit, texture2dUnits } = this; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return texture2dUnits[activeTextureUnit]!; + return this.texture2dUnits[this.activeTextureUnit]!; } /** @@ -244,22 +242,23 @@ export class WebGlContext extends CoreGlContext { * @returns */ texParameteri(pname: number, param: number) { - const { gl, texture2dParams } = this; - const activeTexture = this._getActiveTexture(); - if (!activeTexture) { + if (activeTexture === null) { throw new Error('No active texture'); } - let textureParams = texture2dParams.get(activeTexture); - if (!textureParams) { + + let textureParams = this.texture2dParams.get(activeTexture); + if (textureParams === undefined) { textureParams = {}; - texture2dParams.set(activeTexture, textureParams); + this.texture2dParams.set(activeTexture, textureParams); } + if (textureParams[pname] === param) { return; } + textureParams[pname] = param; - gl.texParameteri(gl.TEXTURE_2D, pname, param); + this.gl.texParameteri(this.gl.TEXTURE_2D, pname, param); } /** @@ -315,10 +314,9 @@ export class WebGlContext extends CoreGlContext { type?: any, pixels?: any, ) { - const { gl } = this; if (format) { - gl.texImage2D( - gl.TEXTURE_2D, + this.gl.texImage2D( + this.gl.TEXTURE_2D, level, internalFormat, widthOrFormat, @@ -329,8 +327,8 @@ export class WebGlContext extends CoreGlContext { pixels, ); } else { - gl.texImage2D( - gl.TEXTURE_2D, + this.gl.texImage2D( + this.gl.TEXTURE_2D, level, internalFormat, widthOrFormat, @@ -356,9 +354,8 @@ export class WebGlContext extends CoreGlContext { border: GLint, data?: ArrayBufferView, ): void { - const { gl } = this; - gl.compressedTexImage2D( - gl.TEXTURE_2D, + this.gl.compressedTexImage2D( + this.gl.TEXTURE_2D, level, internalformat, width, @@ -367,6 +364,7 @@ export class WebGlContext extends CoreGlContext { data as ArrayBufferView, ); } + /** * ``` * gl.pixelStorei(pname, param); @@ -376,8 +374,7 @@ export class WebGlContext extends CoreGlContext { * @param param */ pixelStorei(pname: GLenum, param: GLint | GLboolean) { - const { gl } = this; - gl.pixelStorei(pname, param); + this.gl.pixelStorei(pname, param); } /** @@ -389,8 +386,7 @@ export class WebGlContext extends CoreGlContext { * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D` */ generateMipmap() { - const { gl } = this; - gl.generateMipmap(gl.TEXTURE_2D); + this.gl.generateMipmap(this.gl.TEXTURE_2D); } /** @@ -401,8 +397,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ createTexture() { - const { gl } = this; - return gl.createTexture(); + return this.gl.createTexture(); } /** @@ -413,11 +408,11 @@ export class WebGlContext extends CoreGlContext { * @param texture */ deleteTexture(texture: WebGLTexture | null) { - const { gl } = this; - if (texture) { + if (texture !== null) { this.texture2dParams.delete(texture); } - gl.deleteTexture(texture); + + this.gl.deleteTexture(texture); } /** @@ -426,8 +421,7 @@ export class WebGlContext extends CoreGlContext { * ``` */ viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) { - const { gl } = this; - gl.viewport(x, y, width, height); + this.gl.viewport(x, y, width, height); } /** @@ -441,8 +435,7 @@ export class WebGlContext extends CoreGlContext { * @param alpha */ clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) { - const { gl } = this; - gl.clearColor(red, green, blue, alpha); + this.gl.clearColor(red, green, blue, alpha); } /** @@ -452,15 +445,16 @@ export class WebGlContext extends CoreGlContext { * @param enable */ setScissorTest(enable: boolean) { - const { gl, scissorEnabled } = this; - if (enable === scissorEnabled) { + if (enable === this.scissorEnabled) { return; } - if (enable) { - gl.enable(gl.SCISSOR_TEST); + + if (enable === true) { + this.gl.enable(this.gl.SCISSOR_TEST); } else { - gl.disable(gl.SCISSOR_TEST); + this.gl.disable(this.gl.SCISSOR_TEST); } + this.scissorEnabled = enable; } @@ -475,14 +469,13 @@ export class WebGlContext extends CoreGlContext { * @param height */ scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) { - const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this; if ( - x !== scissorX || - y !== scissorY || - width !== scissorWidth || - height !== scissorHeight + x !== this.scissorX || + y !== this.scissorY || + width !== this.scissorWidth || + height !== this.scissorHeight ) { - gl.scissor(x, y, width, height); + this.gl.scissor(x, y, width, height); this.scissorX = x; this.scissorY = y; this.scissorWidth = width; @@ -499,15 +492,16 @@ export class WebGlContext extends CoreGlContext { * @returns */ setBlend(blend: boolean) { - const { gl, blendEnabled } = this; - if (blend === blendEnabled) { + if (blend === this.blendEnabled) { return; } - if (blend) { - gl.enable(gl.BLEND); + + if (blend === true) { + this.gl.enable(this.gl.BLEND); } else { - gl.disable(gl.BLEND); + this.gl.disable(this.gl.BLEND); } + this.blendEnabled = blend; } @@ -543,8 +537,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ createBuffer() { - const { gl } = this; - return gl.createBuffer(); + return this.gl.createBuffer(); } /** @@ -554,8 +547,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ createFramebuffer() { - const { gl } = this; - return gl.createFramebuffer(); + return this.gl.createFramebuffer(); } /** @@ -566,8 +558,7 @@ export class WebGlContext extends CoreGlContext { * @param framebuffer */ bindFramebuffer(framebuffer: WebGLFramebuffer | null) { - const { gl } = this; - gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); + this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, framebuffer); } /** @@ -583,11 +574,10 @@ export class WebGlContext extends CoreGlContext { texture: WebGLTexture | null, level: GLint, ) { - const { gl } = this; - gl.framebufferTexture2D( - gl.FRAMEBUFFER, + this.gl.framebufferTexture2D( + this.gl.FRAMEBUFFER, attachment, - gl.TEXTURE_2D, + this.gl.TEXTURE_2D, texture, level, ); @@ -601,9 +591,8 @@ export class WebGlContext extends CoreGlContext { * @remarks * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT` */ - clear() { - const { gl } = this; - gl.clear(gl.COLOR_BUFFER_BIT); + clear(mask?: GLbitfield) { + this.gl.clear(mask || this.gl.COLOR_BUFFER_BIT); } /** @@ -700,11 +689,10 @@ export class WebGlContext extends CoreGlContext { * @returns */ useProgram(program: WebGLProgram | null) { - const { gl, curProgram } = this; - if (curProgram === program) { + if (this.curProgram === program) { return; } - gl.useProgram(program); + this.gl.useProgram(program); this.curProgram = program; } @@ -715,8 +703,7 @@ export class WebGlContext extends CoreGlContext { * @param v0 - The value to set. */ uniform1f(location: WebGLUniformLocation | null, v0: number) { - const { gl } = this; - gl.uniform1f(location, v0); + this.gl.uniform1f(location, v0); } /** @@ -729,8 +716,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniform1fv(location, value); + this.gl.uniform1fv(location, value); } /** @@ -740,8 +726,7 @@ export class WebGlContext extends CoreGlContext { * @param v0 - The value to set. */ uniform1i(location: WebGLUniformLocation | null, v0: number) { - const { gl } = this; - gl.uniform1i(location, v0); + this.gl.uniform1i(location, v0); } /** @@ -754,8 +739,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Int32Array | number[], ) { - const { gl } = this; - gl.uniform1iv(location, value); + this.gl.uniform1iv(location, value); } /** @@ -766,8 +750,7 @@ export class WebGlContext extends CoreGlContext { * @param v1 - The second component of the vector. */ uniform2f(location: WebGLUniformLocation | null, v0: number, v1: number) { - const { gl } = this; - gl.uniform2f(location, v0, v1); + this.gl.uniform2f(location, v0, v1); } /** @@ -780,8 +763,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniform2fv(location, value); + this.gl.uniform2fv(location, value); } /** @@ -792,8 +774,7 @@ export class WebGlContext extends CoreGlContext { * @param v1 - The second component of the vector. */ uniform2i(location: WebGLUniformLocation | null, v0: number, v1: number) { - const { gl } = this; - gl.uniform2i(location, v0, v1); + this.gl.uniform2i(location, v0, v1); } /** @@ -806,8 +787,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Int32Array | number[], ) { - const { gl } = this; - gl.uniform2iv(location, value); + this.gl.uniform2iv(location, value); } /** @@ -824,8 +804,7 @@ export class WebGlContext extends CoreGlContext { v1: number, v2: number, ) { - const { gl } = this; - gl.uniform3f(location, v0, v1, v2); + this.gl.uniform3f(location, v0, v1, v2); } /** @@ -838,8 +817,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniform3fv(location, value); + this.gl.uniform3fv(location, value); } /** @@ -856,8 +834,7 @@ export class WebGlContext extends CoreGlContext { v1: number, v2: number, ) { - const { gl } = this; - gl.uniform3i(location, v0, v1, v2); + this.gl.uniform3i(location, v0, v1, v2); } /** @@ -870,8 +847,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Int32Array | number[], ) { - const { gl } = this; - gl.uniform3iv(location, value); + this.gl.uniform3iv(location, value); } /** @@ -890,8 +866,7 @@ export class WebGlContext extends CoreGlContext { v2: number, v3: number, ) { - const { gl } = this; - gl.uniform4f(location, v0, v1, v2, v3); + this.gl.uniform4f(location, v0, v1, v2, v3); } /** @@ -904,8 +879,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniform4fv(location, value); + this.gl.uniform4fv(location, value); } /** @@ -924,8 +898,7 @@ export class WebGlContext extends CoreGlContext { v2: number, v3: number, ) { - const { gl } = this; - gl.uniform4i(location, v0, v1, v2, v3); + this.gl.uniform4i(location, v0, v1, v2, v3); } /** @@ -938,8 +911,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Int32Array | number[], ) { - const { gl } = this; - gl.uniform4iv(location, value); + this.gl.uniform4iv(location, value); } /** @@ -953,8 +925,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniformMatrix2fv(location, false, value); + this.gl.uniformMatrix2fv(location, false, value); } /** @@ -966,8 +937,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniformMatrix3fv(location, false, value); + this.gl.uniformMatrix3fv(location, false, value); } /** @@ -979,8 +949,7 @@ export class WebGlContext extends CoreGlContext { location: WebGLUniformLocation | null, value: Float32Array | number[], ) { - const { gl } = this; - gl.uniformMatrix4fv(location, false, value); + this.gl.uniformMatrix4fv(location, false, value); } /** @@ -992,8 +961,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ getParameter(pname: GLenum): any { - const { gl } = this; - return gl.getParameter(pname); + return this.gl.getParameter(pname); } /** @@ -1007,8 +975,7 @@ export class WebGlContext extends CoreGlContext { * @param offset */ drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) { - const { gl } = this; - gl.drawElements(mode, count, type, offset); + this.gl.drawElements(mode, count, type, offset); } /** @@ -1020,8 +987,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ getExtension(name: string) { - const { gl } = this; - return gl.getExtension(name); + return this.gl.getExtension(name); } /** @@ -1032,9 +998,11 @@ export class WebGlContext extends CoreGlContext { * @returns */ createVertexArray() { - const { gl } = this; - assertTruthy(gl instanceof WebGL2RenderingContext); - return gl.createVertexArray(); + assertTruthy( + typeof (this.gl as WebGL2RenderingContext) === 'function', + 'createVertexArray is a WebGL2 only function and does not work on WebGL1', + ); + return (this.gl as WebGL2RenderingContext).createVertexArray(); } /** @@ -1045,9 +1013,11 @@ export class WebGlContext extends CoreGlContext { * @param vertexArray */ bindVertexArray(vertexArray: WebGLVertexArrayObject | null) { - const { gl } = this; - assertTruthy(gl instanceof WebGL2RenderingContext); - gl.bindVertexArray(vertexArray); + assertTruthy( + typeof (this.gl as WebGL2RenderingContext) === 'function', + 'bindVertexArray is a WebGL2 only function and does not work on WebGL1', + ); + (this.gl as WebGL2RenderingContext).bindVertexArray(vertexArray); } /** @@ -1057,11 +1027,10 @@ export class WebGlContext extends CoreGlContext { * * @param program * @param name - * @returns + * @returns {GLint} */ getAttribLocation(program: WebGLProgram, name: string) { - const { gl } = this; - return gl.getAttribLocation(program, name); + return this.gl.getAttribLocation(program, name); } /** @@ -1074,8 +1043,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ getUniformLocation(program: WebGLProgram, name: string) { - const { gl } = this; - return gl.getUniformLocation(program, name); + return this.gl.getUniformLocation(program, name); } /** @@ -1086,8 +1054,7 @@ export class WebGlContext extends CoreGlContext { * @param index */ enableVertexAttribArray(index: number) { - const { gl } = this; - gl.enableVertexAttribArray(index); + this.gl.enableVertexAttribArray(index); } /** @@ -1098,8 +1065,7 @@ export class WebGlContext extends CoreGlContext { * @param index */ disableVertexAttribArray(index: number) { - const { gl } = this; - gl.disableVertexAttribArray(index); + this.gl.disableVertexAttribArray(index); } /** @@ -1110,9 +1076,8 @@ export class WebGlContext extends CoreGlContext { * @param type * @returns */ - createShader(type: number) { - const { gl } = this; - return gl.createShader(type); + createShader(type: number): WebGLShader | null { + return this.gl.createShader(type); } /** @@ -1124,8 +1089,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ compileShader(shader: WebGLShader) { - const { gl } = this; - gl.compileShader(shader); + this.gl.compileShader(shader); } /** @@ -1137,8 +1101,7 @@ export class WebGlContext extends CoreGlContext { * @param shader */ attachShader(program: WebGLProgram, shader: WebGLShader) { - const { gl } = this; - gl.attachShader(program, shader); + this.gl.attachShader(program, shader); } /** @@ -1149,8 +1112,7 @@ export class WebGlContext extends CoreGlContext { * @param program */ linkProgram(program: WebGLProgram) { - const { gl } = this; - gl.linkProgram(program); + this.gl.linkProgram(program); } /** @@ -1161,8 +1123,7 @@ export class WebGlContext extends CoreGlContext { * @param shader */ deleteProgram(shader: WebGLProgram) { - const { gl } = this; - gl.deleteProgram(shader); + this.gl.deleteProgram(shader); } /** @@ -1174,8 +1135,7 @@ export class WebGlContext extends CoreGlContext { * @param pname */ getShaderParameter(shader: WebGLShader, pname: GLenum) { - const { gl } = this; - return gl.getShaderParameter(shader, pname); + return this.gl.getShaderParameter(shader, pname); } /** @@ -1186,8 +1146,7 @@ export class WebGlContext extends CoreGlContext { * @param shader */ getShaderInfoLog(shader: WebGLShader) { - const { gl } = this; - return gl.getShaderInfoLog(shader); + return this.gl.getShaderInfoLog(shader); } /** @@ -1198,8 +1157,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ createProgram() { - const { gl } = this; - return gl.createProgram(); + return this.gl.createProgram(); } /** @@ -1212,8 +1170,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ getProgramParameter(program: WebGLProgram, pname: GLenum) { - const { gl } = this; - return gl.getProgramParameter(program, pname); + return this.gl.getProgramParameter(program, pname); } /** @@ -1225,8 +1182,7 @@ export class WebGlContext extends CoreGlContext { * @returns */ getProgramInfoLog(program: WebGLProgram) { - const { gl } = this; - return gl.getProgramInfoLog(program); + return this.gl.getProgramInfoLog(program); } /** @@ -1238,8 +1194,7 @@ export class WebGlContext extends CoreGlContext { * @param source */ shaderSource(shader: WebGLShader, source: string) { - const { gl } = this; - gl.shaderSource(shader, source); + this.gl.shaderSource(shader, source); } /** @@ -1250,8 +1205,7 @@ export class WebGlContext extends CoreGlContext { * @param shader */ deleteShader(shader: WebGLShader) { - const { gl } = this; - gl.deleteShader(shader); + this.gl.deleteShader(shader); } } @@ -1272,37 +1226,3 @@ export type UniformMethodMap = { ? T : never; }; - -/** - * Compare two arrays for equality. - * - * @remarks - * This function will not try to compare nested arrays or Float32Arrays and - * instead will always return false when they are encountered. - * - * @param a - * @param b - * @returns - */ -export function compareArrays(a: T[], b: T[]): boolean { - if (a.length !== b.length) { - return false; - } - - let result = false; - for (let i = 0; i < a.length; i++) { - if (Array.isArray(a[i]) || a[i] instanceof Float32Array) { - result = false; - break; - } - - if (a[i] !== b[i]) { - result = false; - break; - } - - result = true; - } - - return result; -} diff --git a/src/core/renderers/CoreRenderer.ts b/src/core/renderers/CoreRenderer.ts index f8114968..4a5b710c 100644 --- a/src/core/renderers/CoreRenderer.ts +++ b/src/core/renderers/CoreRenderer.ts @@ -73,7 +73,7 @@ export interface CoreRendererOptions { bufferMemory: number; contextSpy: ContextSpy | null; forceWebGL2: boolean; - platform: WebPlatform; + platform: CorePlatform | WebPlatform; } export interface BufferInfo { diff --git a/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts b/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts index a670ad05..be28271b 100644 --- a/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +++ b/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts @@ -19,8 +19,9 @@ import type { Dimensions } from '../../../common/CommonTypes.js'; import { assertTruthy } from '../../../utils.js'; +import type { CoreGlContext } from '../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../platforms/web/WebGlContext.js'; import type { TextureMemoryManager } from '../../TextureMemoryManager.js'; -import type { WebGlContextWrapper } from '../../platforms/web/WebGlContextWrapper.js'; import type { RenderTexture } from '../../textures/RenderTexture.js'; import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js'; @@ -30,7 +31,7 @@ export class WebGlCoreCtxRenderTexture extends WebGlCoreCtxTexture { readonly framebuffer: WebGLFramebuffer; constructor( - glw: WebGlContextWrapper, + glw: CoreGlContext | WebGlContext, memManager: TextureMemoryManager, textureSource: RenderTexture, ) { diff --git a/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts b/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts index 1db0271d..95980c73 100644 --- a/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +++ b/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts @@ -18,14 +18,15 @@ */ import type { Dimensions } from '../../../common/CommonTypes.js'; +import type { CoreGlContext } from '../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../platforms/web/WebGlContext.js'; import type { TextureMemoryManager } from '../../TextureMemoryManager.js'; -import type { WebGlContextWrapper } from '../../platforms/web/WebGlContextWrapper.js'; import type { SubTexture } from '../../textures/SubTexture.js'; import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js'; export class WebGlCoreCtxSubTexture extends WebGlCoreCtxTexture { constructor( - glw: WebGlContextWrapper, + glw: CoreGlContext | WebGlContext, memManager: TextureMemoryManager, textureSource: SubTexture, ) { diff --git a/src/core/renderers/webgl/WebGlCoreCtxTexture.ts b/src/core/renderers/webgl/WebGlCoreCtxTexture.ts index 851cbdcb..bae8cca1 100644 --- a/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +++ b/src/core/renderers/webgl/WebGlCoreCtxTexture.ts @@ -19,8 +19,9 @@ import type { Dimensions } from '../../../common/CommonTypes.js'; import { assertTruthy } from '../../../utils.js'; +import type { CoreGlContext } from '../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../platforms/web/WebGlContext.js'; import type { TextureMemoryManager } from '../../TextureMemoryManager.js'; -import type { WebGlContextWrapper } from '../../platforms/web/WebGlContextWrapper.js'; import type { Texture } from '../../textures/Texture.js'; import { isPowerOfTwo } from '../../utils.js'; import { CoreContextTexture } from '../CoreContextTexture.js'; @@ -46,7 +47,7 @@ export class WebGlCoreCtxTexture extends CoreContextTexture { private _h = 0; constructor( - protected glw: WebGlContextWrapper, + protected glw: CoreGlContext | WebGlContext, memManager: TextureMemoryManager, textureSource: Texture, ) { @@ -88,7 +89,10 @@ export class WebGlCoreCtxTexture extends CoreContextTexture { this._nativeCtxTexture = this.createNativeCtxTexture(); if (this._nativeCtxTexture === null) { this._state = 'failed'; - this.textureSource.setState('failed', new Error('Could not create WebGL Texture')); + this.textureSource.setState( + 'failed', + new Error('Could not create WebGL Texture'), + ); console.error('Could not create WebGL Texture'); return; } diff --git a/src/core/renderers/webgl/WebGlCoreRenderOp.ts b/src/core/renderers/webgl/WebGlCoreRenderOp.ts index 5a3b4b78..a2981926 100644 --- a/src/core/renderers/webgl/WebGlCoreRenderOp.ts +++ b/src/core/renderers/webgl/WebGlCoreRenderOp.ts @@ -23,8 +23,9 @@ import type { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js'; import type { WebGlCoreRendererOptions } from './WebGlCoreRenderer.js'; import type { BufferCollection } from './internal/BufferCollection.js'; import type { Dimensions } from '../../../common/CommonTypes.js'; -import type { Rect, RectWithValid } from '../../lib/utils.js'; -import type { WebGlContextWrapper } from '../../platforms/web/WebGlContextWrapper.js'; +import type { RectWithValid } from '../../lib/utils.js'; +import type { CoreGlContext } from '../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../platforms/web/WebGlContext.js'; const MAX_TEXTURES = 8; // TODO: get from gl @@ -39,7 +40,7 @@ export class WebGlCoreRenderOp extends CoreRenderOp { readonly maxTextures: number; constructor( - readonly glw: WebGlContextWrapper, + readonly glw: CoreGlContext | WebGlContext, readonly options: WebGlCoreRendererOptions, readonly buffers: BufferCollection, readonly shader: WebGlCoreShader, diff --git a/src/core/renderers/webgl/WebGlCoreRenderer.ts b/src/core/renderers/webgl/WebGlCoreRenderer.ts index a968f552..925b856a 100644 --- a/src/core/renderers/webgl/WebGlCoreRenderer.ts +++ b/src/core/renderers/webgl/WebGlCoreRenderer.ts @@ -17,7 +17,7 @@ * limitations under the License. */ -import { assertTruthy, createWebGLContext, hasOwn } from '../../../utils.js'; +import { assertTruthy, hasOwn } from '../../../utils.js'; import { CoreRenderer, type BufferInfo, @@ -66,7 +66,7 @@ interface CoreWebGlSystem { export class WebGlCoreRenderer extends CoreRenderer { //// WebGL Native Context and Data - glw: WebGlContext; + glw: CoreGlContext | WebGlContext; system: CoreWebGlSystem; //// Persistent data @@ -121,20 +121,19 @@ export class WebGlCoreRenderer extends CoreRenderer { this.stage.requestRender(); }); - const glw = platform.createWebGLContext( + this.glw = platform.createWebGLContext( canvas, options.forceWebGL2, options.contextSpy, ); - this.glw = glw; const color = getNormalizedRgbaComponents(clearColor); - glw.viewport(0, 0, canvas.width, canvas.height); - glw.clearColor(color[0]!, color[1]!, color[2]!, color[3]!); - glw.setBlend(true); - glw.blendFunc(glw.ONE, glw.ONE_MINUS_SRC_ALPHA); + this.glw.viewport(0, 0, canvas.width, canvas.height); + this.glw.clearColor(color[0]!, color[1]!, color[2]!, color[3]!); + this.glw.setBlend(true); + this.glw.blendFunc(this.glw.ONE, this.glw.ONE_MINUS_SRC_ALPHA); - createIndexBuffer(glw, bufferMemory); + createIndexBuffer(this.glw, bufferMemory); this.system = { parameters: getWebGlParameters(this.glw), @@ -143,7 +142,7 @@ export class WebGlCoreRenderer extends CoreRenderer { this.shManager.renderer = this; this.defShaderCtrl = this.shManager.loadShader('DefaultShader'); this.defaultShader = this.defShaderCtrl.shader as WebGlCoreShader; - const quadBuffer = glw.createBuffer(); + const quadBuffer = this.glw.createBuffer(); assertTruthy(quadBuffer); const stride = 6 * Float32Array.BYTES_PER_ELEMENT; this.quadBufferCollection = new BufferCollection([ @@ -153,7 +152,7 @@ export class WebGlCoreRenderer extends CoreRenderer { a_position: { name: 'a_position', size: 2, // 2 components per iteration - type: glw.FLOAT, // the data is 32bit floats + type: this.glw.FLOAT, // the data is 32bit floats normalized: false, // don't normalize the data stride, // 0 = move forward size * sizeof(type) each iteration to get the next position offset: 0, // start at the beginning of the buffer @@ -161,7 +160,7 @@ export class WebGlCoreRenderer extends CoreRenderer { a_textureCoordinate: { name: 'a_textureCoordinate', size: 2, - type: glw.FLOAT, + type: this.glw.FLOAT, normalized: false, stride, offset: 2 * Float32Array.BYTES_PER_ELEMENT, @@ -169,7 +168,7 @@ export class WebGlCoreRenderer extends CoreRenderer { a_color: { name: 'a_color', size: 4, - type: glw.UNSIGNED_BYTE, + type: this.glw.UNSIGNED_BYTE, normalized: true, stride, offset: 4 * Float32Array.BYTES_PER_ELEMENT, @@ -177,7 +176,7 @@ export class WebGlCoreRenderer extends CoreRenderer { a_textureIndex: { name: 'a_textureIndex', size: 1, - type: glw.FLOAT, + type: this.glw.FLOAT, normalized: false, stride, offset: 5 * Float32Array.BYTES_PER_ELEMENT, @@ -188,12 +187,11 @@ export class WebGlCoreRenderer extends CoreRenderer { } reset() { - const { glw } = this; this.curBufferIdx = 0; this.curRenderOp = null; this.renderOps.length = 0; - glw.setScissorTest(false); - glw.clear(); + this.glw.setScissorTest(false); + this.glw.clear(); } override getShaderManager(): CoreShaderManager { diff --git a/src/core/renderers/webgl/WebGlCoreShader.ts b/src/core/renderers/webgl/WebGlCoreShader.ts index 0f5432d4..c9fffb36 100644 --- a/src/core/renderers/webgl/WebGlCoreShader.ts +++ b/src/core/renderers/webgl/WebGlCoreShader.ts @@ -19,7 +19,8 @@ import type { Dimensions } from '../../../common/CommonTypes.js'; import { assertTruthy, hasOwn } from '../../../utils.js'; -import type { WebGlContextWrapper } from '../../platforms/web/WebGlContextWrapper.js'; +import type { CoreGlContext } from '../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../platforms/web/WebGlContext.js'; import { CoreShader } from '../CoreShader.js'; import type { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js'; import type { WebGlCoreRenderOp } from './WebGlCoreRenderOp.js'; @@ -76,7 +77,7 @@ export abstract class WebGlCoreShader extends CoreShader { */ protected vao: WebGLVertexArrayObject | undefined; protected renderer: WebGlCoreRenderer; - protected glw: WebGlContextWrapper; + protected glw: CoreGlContext | WebGlContext; protected attributeBuffers: Record; protected attributeLocations: Record; protected attributeNames: string[]; diff --git a/src/core/renderers/webgl/internal/RendererUtils.ts b/src/core/renderers/webgl/internal/RendererUtils.ts index c4cea25e..9d93d603 100644 --- a/src/core/renderers/webgl/internal/RendererUtils.ts +++ b/src/core/renderers/webgl/internal/RendererUtils.ts @@ -17,7 +17,8 @@ * limitations under the License. */ -import type { WebGlContextWrapper } from '../../../platforms/web/WebGlContextWrapper.js'; +import type { CoreGlContext } from '../../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../../platforms/web/WebGlContext.js'; export interface CoreWebGlParameters { MAX_RENDERBUFFER_SIZE: number; @@ -37,7 +38,7 @@ export interface CoreWebGlParameters { * @param glw */ export function getWebGlParameters( - glw: WebGlContextWrapper, + glw: CoreGlContext | WebGlContext, ): CoreWebGlParameters { const params: CoreWebGlParameters = { MAX_RENDERBUFFER_SIZE: 0, @@ -82,7 +83,7 @@ export interface CoreWebGlExtensions { * @param glw */ export function getWebGlExtensions( - glw: WebGlContextWrapper, + glw: CoreGlContext | WebGlContext, ): CoreWebGlExtensions { const extensions: CoreWebGlExtensions = { ANGLE_instanced_arrays: null, @@ -113,7 +114,10 @@ export function getWebGlExtensions( * @param glw * @param size */ -export function createIndexBuffer(glw: WebGlContextWrapper, size: number) { +export function createIndexBuffer( + glw: CoreGlContext | WebGlContext, + size: number, +) { const maxQuads = ~~(size / 80); const indices = new Uint16Array(maxQuads * 6); diff --git a/src/core/renderers/webgl/internal/ShaderUtils.ts b/src/core/renderers/webgl/internal/ShaderUtils.ts index 91cbea2b..67f4b4e4 100644 --- a/src/core/renderers/webgl/internal/ShaderUtils.ts +++ b/src/core/renderers/webgl/internal/ShaderUtils.ts @@ -17,7 +17,8 @@ * limitations under the License. */ -import type { WebGlContextWrapper } from '../../../platforms/web/WebGlContextWrapper.js'; +import type { CoreGlContext } from '../../../platforms/CoreGlContext.js'; +import type { WebGlContext } from '../../../platforms/web/WebGlContext.js'; import type { WebGlCoreRenderer } from '../WebGlCoreRenderer.js'; //#region Types @@ -94,7 +95,7 @@ export interface ShaderProgramSources { //#endregion Types export function createShader( - glw: WebGlContextWrapper, + glw: CoreGlContext | WebGlContext, type: number, source: string, ) { @@ -114,7 +115,7 @@ export function createShader( } export function createProgram( - glw: WebGlContextWrapper, + glw: CoreGlContext | WebGlContext, vertexShader: WebGLShader, fragmentShader: WebGLShader, ) { diff --git a/src/core/renderers/webgl/shaders/DynamicShader.ts b/src/core/renderers/webgl/shaders/DynamicShader.ts index 4536843a..c5789ed0 100644 --- a/src/core/renderers/webgl/shaders/DynamicShader.ts +++ b/src/core/renderers/webgl/shaders/DynamicShader.ts @@ -33,7 +33,7 @@ import { } from './effects/ShaderEffect.js'; import type { EffectMap } from '../../../CoreShaderManager.js'; import { assertTruthy } from '../../../../utils.js'; -import type { UniformMethodMap } from '../../../platforms/web/WebGlContextWrapper.js'; +import type { UniformMethodMap } from '../../../platforms/web/WebGlContext.js'; export interface DynamicShaderProps extends DimensionsShaderProp, diff --git a/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts b/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts index eca1db65..69c07926 100644 --- a/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +++ b/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts @@ -1,6 +1,6 @@ import type { EffectMap } from '../../../../CoreShaderManager.js'; import type { ExtractProps } from '../../../../CoreTextureManager.js'; -import type { WebGlContextWrapper } from '../../../../platforms/web/WebGlContextWrapper.js'; + import type { AlphaShaderProp, DimensionsShaderProp, diff --git a/src/core/textures/ImageTexture.ts b/src/core/textures/ImageTexture.ts index 24efd7f6..40f4b37a 100644 --- a/src/core/textures/ImageTexture.ts +++ b/src/core/textures/ImageTexture.ts @@ -152,17 +152,24 @@ export class ImageTexture extends Texture { if (sw !== null && sh !== null) { return { - data: await createImageBitmap(blob, sx ?? 0, sy ?? 0, sw, sh, { - premultiplyAlpha: hasAlphaChannel ? 'premultiply' : 'none', - colorSpaceConversion: 'none', - imageOrientation: 'none', - }), + data: await this.txManager.createImageBitmap( + blob, + sx ?? 0, + sy ?? 0, + sw, + sh, + { + premultiplyAlpha: hasAlphaChannel ? 'premultiply' : 'none', + colorSpaceConversion: 'none', + imageOrientation: 'none', + }, + ), premultiplyAlpha: hasAlphaChannel, }; } return { - data: await createImageBitmap(blob, { + data: await this.txManager.createImageBitmap(blob, { premultiplyAlpha: hasAlphaChannel ? 'premultiply' : 'none', colorSpaceConversion: 'none', imageOrientation: 'none', diff --git a/src/main-api/Renderer.ts b/src/main-api/Renderer.ts index c00e10aa..2620d45f 100644 --- a/src/main-api/Renderer.ts +++ b/src/main-api/Renderer.ts @@ -372,11 +372,15 @@ export class RendererMain extends EventEmitter { inspector, } = resolvedSettings; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - const platform = (new settings.platform() || - new WebPlatform()) as CorePlatform; + let platform; + if (settings.platform) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + platform = new settings.platform() as WebPlatform; + } else { + platform = new WebPlatform(); + } + assertTruthy( platform instanceof CorePlatform, 'Platform is not a core platform immplementation',