Skip to content

Commit

Permalink
chore(webgl): Start fixing strictNullChecks issues. (#2057)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Apr 1, 2024
1 parent 9ed3a8b commit 26b179f
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 35 deletions.
8 changes: 4 additions & 4 deletions modules/webgl/src/adapter/converters/texture-formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ export function isRenderbufferFormatSupported(
extensions: GLExtensions
): boolean {
// Note: Order is important since the function call initializes extensions.
return isTextureFormatSupported(gl, format, extensions) && TEXTURE_FORMATS[format]?.rb;
return isTextureFormatSupported(gl, format, extensions) && Boolean(TEXTURE_FORMATS[format]?.rb);
}

/** Checks if a texture format is supported */
Expand All @@ -509,7 +509,7 @@ export function getTextureFormatSupport(

// Support Check that we have a GL constant
let supported = info.gl === undefined;
supported = supported && checkTextureFeature(gl, info.f, extensions);
supported = supported && checkTextureFeature(gl, info.f as DeviceFeature, extensions);

// Filtering
// const filterable = info.filter
Expand All @@ -521,8 +521,8 @@ export function getTextureFormatSupport(

return {
supported,
renderable: supported && checkTextureFeature(gl, info.render, extensions),
filterable: supported && checkTextureFeature(gl, info.filter, extensions),
renderable: supported && checkTextureFeature(gl, info.render as DeviceFeature, extensions),
filterable: supported && checkTextureFeature(gl, info.filter as DeviceFeature, extensions),
blendable: false, // tod,
storable: false
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class WebGLDeviceFeatures extends DeviceFeatures {
}

override has(feature: DeviceFeature): boolean {
if (this.disabledFeatures[feature]) {
if (this.disabledFeatures?.[feature]) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export class WebGLDeviceLimits extends DeviceLimits {
if (this.limits[parameter] === undefined) {
this.limits[parameter] = this.gl.getParameter(parameter);
}
return this.limits[parameter];
return this.limits[parameter] || 0;
}
}
6 changes: 3 additions & 3 deletions modules/webgl/src/adapter/resources/webgl-shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export class WEBGLShader extends Shader {
return this.getCompilationInfoSync();
}

override getCompilationInfoSync() {
override getCompilationInfoSync(): readonly CompilerMessage[] {
const log = this.device.gl.getShaderInfoLog(this.handle);
return parseShaderCompilerLog(log);
return log ? parseShaderCompilerLog(log) : [];
}

override getTranslatedSource(): string | null {
const extensions = this.device.getExtension('WEBGL_debug_shaders');
const ext = extensions.WEBGL_debug_shaders;
return ext?.getTranslatedShaderSource(this.handle);
return ext?.getTranslatedShaderSource(this.handle) || null;
}

// PRIVATE METHODS
Expand Down
4 changes: 1 addition & 3 deletions modules/webgl/src/adapter/resources/webgl-texture-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {WEBGLTexture} from './webgl-texture';
export class WEBGLTextureView extends TextureView {
readonly device: WebGLDevice;
readonly gl: WebGL2RenderingContext;
readonly handle: WebGLTexture;

readonly handle: null; // Does not have a WebGL representation
readonly texture: WEBGLTexture;

constructor(device: Device, props: TextureViewProps & {texture: WEBGLTexture}) {
Expand All @@ -22,7 +21,6 @@ export class WEBGLTextureView extends TextureView {
this.device = device as WebGLDevice;
this.gl = this.device.gl;
this.handle = null;

this.texture = props.texture;
}
}
14 changes: 7 additions & 7 deletions modules/webgl/src/adapter/resources/webgl-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ export class WEBGLTexture extends Texture {
readonly gl: WebGL2RenderingContext;
handle: WebGLTexture;

sampler: WEBGLSampler = undefined; // TODO - currently unused in WebGL
view: WEBGLTextureView = undefined;
sampler: WEBGLSampler = undefined; // TODO - currently unused in WebGL. Create dummy sampler?
view: WEBGLTextureView = undefined; // TODO - currently unused in WebGL. Create dummy view?

mipmaps: boolean = undefined;
mipmaps: boolean = false;

/**
* @note `target` cannot be modified by bind:
Expand Down Expand Up @@ -140,11 +140,11 @@ export class WEBGLTexture extends Texture {
video: HTMLVideoElement;
parameters: any;
lastTime: number;
};
} | null = null;

constructor(device: Device, props: TextureProps) {
// Note: Clear out `props.data` so that we don't hold a reference to any big memory chunks
super(device, {...Texture.defaultProps, ...props, data: undefined});
super(device, {...Texture.defaultProps, ...props, data: undefined!});

this.device = device as WebGLDevice;
this.gl = this.device.gl;
Expand Down Expand Up @@ -226,7 +226,7 @@ export class WEBGLTexture extends Texture {
}
}

this.mipmaps = props.mipmaps;
this.mipmaps = Boolean(props.mipmaps);

if (this.mipmaps) {
this.generateMipmap();
Expand Down Expand Up @@ -633,7 +633,7 @@ export class WEBGLTexture extends Texture {
return textureUnit;
}

unbind(textureUnit?: number): number {
unbind(textureUnit?: number): number | undefined {
const {gl} = this;

if (textureUnit !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions modules/webgl/src/adapter/resources/webgl-vertex-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export class WEBGLVertexArray extends VertexArray {
}

// Create a VertexArray
constructor(device: WebGLDevice, props?: VertexArrayProps) {
constructor(device: WebGLDevice, props: VertexArrayProps) {
super(device, props);
this.device = device;
this.handle = this.device.gl.createVertexArray();
this.handle = this.device.gl.createVertexArray()!;
}

override destroy(): void {
Expand Down
2 changes: 1 addition & 1 deletion modules/webgl/src/adapter/webgl-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex

private renderPass: WEBGLRenderPass | null = null;

override createCommandEncoder(props?: CommandEncoderProps): WEBGLCommandEncoder {
override createCommandEncoder(props: CommandEncoderProps = {}): WEBGLCommandEncoder {
return new WEBGLCommandEncoder(this, props);
}

Expand Down
9 changes: 6 additions & 3 deletions modules/webgl/src/classic/copy-and-blit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {WEBGLFramebuffer} from '../adapter/resources/webgl-framebuffer';
import {getGLTypeFromTypedArray, getTypedArrayFromGLType} from './typed-array-utils';
import {glFormatToComponents, glTypeToBytes} from './format-utils';
import {WEBGLBuffer} from '../adapter/resources/webgl-buffer';
import {WEBGLTexture} from '../adapter/resources/webgl-texture';

/**
* Copies data from a type or a Texture object into ArrayBuffer object.
Expand Down Expand Up @@ -204,15 +205,17 @@ export function copyToTexture(
// TODO - support gl.readBuffer (WebGL2 only)
// const prevBuffer = gl.readBuffer(attachment);
// assert(target);
let texture = null;
let texture: WEBGLTexture | null = null;
let textureTarget: GL;
if (target instanceof Texture) {
if (target instanceof WEBGLTexture) {
texture = target;
width = Number.isFinite(width) ? width : texture.width;
height = Number.isFinite(height) ? height : texture.height;
texture.bind(0);
texture?.bind(0);
// @ts-ignore
textureTarget = texture.target;
} else {
// @ts-ignore
textureTarget = target;
}

Expand Down
7 changes: 1 addition & 6 deletions modules/webgl/src/context/debug/webgl-developer-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ export async function loadWebGLDeveloperTools(): Promise<void> {
export function makeDebugContext(
gl: WebGL2RenderingContext,
props: DebugContextProps = {}
): WebGL2RenderingContext | null {
// Return null to ensure we don't try to create a context in this case (TODO what case is that?)
if (!gl) {
return null;
}

): WebGL2RenderingContext {
return props.debug ? getDebugContext(gl, props) : getRealContext(gl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const pixelStorei = (gl: WebGL2RenderingContext, value: number | boolean, key: G

const bindFramebuffer = (gl: WebGL2RenderingContext, value: unknown, key: GL) => {
const target = key === GL.FRAMEBUFFER_BINDING ? GL.DRAW_FRAMEBUFFER : GL.READ_FRAMEBUFFER;
return gl.bindFramebuffer(target, value);
return gl.bindFramebuffer(target, value as WebGLFramebuffer);
};

const bindBuffer = (gl: WebGL2RenderingContext, value: unknown, key: GL) => {
Expand All @@ -113,7 +113,7 @@ const bindBuffer = (gl: WebGL2RenderingContext, value: unknown, key: GL) => {
};
const glTarget = bindingMap[key];

gl.bindBuffer(glTarget, value);
gl.bindBuffer(glTarget as number, value as WebGLBuffer | null);
};

// Utility
Expand Down Expand Up @@ -304,7 +304,7 @@ export const GL_PARAMETER_SETTERS = {
gl.polygonOffset(...value),

sampleCoverage: (gl: WebGL2RenderingContext, value: [number, boolean?]) =>
gl.sampleCoverage(...value),
gl.sampleCoverage(value[0], value[1] || false),

scissorTest: (gl: WebGL2RenderingContext, value) =>
value ? gl.enable(GL.SCISSOR_TEST) : gl.disable(GL.SCISSOR_TEST),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function trackContextState(
log?: any;
}
): WebGL2RenderingContext {
const {enable = true, copyState} = options;
const {enable = true, copyState} = options || {};
// assert(copyState !== undefined);
// @ts-expect-error
if (!gl.state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {WEBGLBuffer, WEBGLVertexArray} from '@luma.gl/webgl';

// TODO(v9): Fix and re-enable test.
test.skip('WEBGLVertexArray#divisors', t => {
// @ts-ignore
const vertexArray = new WEBGLVertexArray(device);

const maxVertexAttributes = device.limits.maxVertexAttributes;
Expand Down Expand Up @@ -80,6 +81,7 @@ test.skip('WEBGLVertexArray#enable', t => {

// TODO(v9): Fix and re-enable test.
test.skip('WEBGLVertexArray#getConstantBuffer', t => {
// @ts-ignore
const vertexArray = new WEBGLVertexArray(device);

let buffer = vertexArray.getConstantBuffer(100, new Float32Array([5, 4, 3])) as WEBGLBuffer;
Expand Down
1 change: 1 addition & 0 deletions modules/webgl/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"exclude": ["node_modules"],
"compilerOptions": {
// "strict": true,
// "strictNullChecks": true,
// "noImplicitAny": true,
"composite": true,
"rootDir": "src",
Expand Down

0 comments on commit 26b179f

Please sign in to comment.