Skip to content

Commit

Permalink
feat(webgl): TS strictNullChecks, assertions, minor type updates
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 11, 2019
1 parent d007434 commit ad672c7
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 130 deletions.
39 changes: 32 additions & 7 deletions packages/webgl/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,9 @@ export type ShaderOutputSpec = GLSL | [GLSL, number];
export interface ShaderUniform {
type: GLSL;
loc: WebGLUniformLocation;
setter: Fn<UniformValue, void>;
defaultFn: (shaderUnis: any, specUnis: any) => UniformValue;
defaultVal: UniformValue;
setter: Fn<UniformValue | undefined | null, void>;
defaultFn?: (shaderUnis: any, specUnis: any) => UniformValue;
defaultVal?: UniformValue;
}

export enum GLSLVersion {
Expand Down Expand Up @@ -1033,7 +1033,7 @@ export interface ModelAttributeSpec {
/**
* Raw attribute data from which `buffer` will be initialized
*/
data: AttribBufferData;
data?: AttribBufferData;
/**
* Attribute element size (in component values, not bytes).
* Default: 3
Expand Down Expand Up @@ -1084,7 +1084,7 @@ export interface InstancingSpec {
}

export interface TextureOpts {
image: ArrayBufferView | TexImageSource;
image: ArrayBufferView | TexImageSource | null;
target: GLenum;
type: GLenum;
filter: GLenum | [GLenum, GLenum?];
Expand Down Expand Up @@ -1120,7 +1120,7 @@ export interface FboOpts {
}

export interface RboOpts {
format: number;
format?: number;
width: number;
height: number;
}
Expand All @@ -1140,7 +1140,7 @@ export interface WeblGLCanvasOpts {
height: number;
autoScale: boolean;
onContextLost: EventListener;
ext: string[];
ext: (keyof WebGLExtensionMap)[];
}

export interface GPGPUOpts {
Expand Down Expand Up @@ -1190,3 +1190,28 @@ export const GL_EXT_INFO = {
alias: "GL_OES_standard_derivatives"
}
};

export interface WebGLExtensionMap {
EXT_blend_minmax: EXT_blend_minmax;
EXT_texture_filter_anisotropic: EXT_texture_filter_anisotropic;
EXT_frag_depth: EXT_frag_depth;
EXT_shader_texture_lod: EXT_shader_texture_lod;
EXT_sRGB: EXT_sRGB;
OES_vertex_array_object: OES_vertex_array_object;
WEBGL_color_buffer_float: WEBGL_color_buffer_float;
WEBGL_compressed_texture_astc: WEBGL_compressed_texture_astc;
WEBGL_compressed_texture_s3tc_srgb: WEBGL_compressed_texture_s3tc_srgb;
WEBGL_debug_shaders: WEBGL_debug_shaders;
WEBGL_draw_buffers: WEBGL_draw_buffers;
WEBGL_lose_context: WEBGL_lose_context;
WEBGL_depth_texture: WEBGL_depth_texture;
WEBGL_debug_renderer_info: WEBGL_debug_renderer_info;
WEBGL_compressed_texture_s3tc: WEBGL_compressed_texture_s3tc;
OES_texture_half_float_linear: OES_texture_half_float_linear;
OES_texture_half_float: OES_texture_half_float;
OES_texture_float_linear: OES_texture_float_linear;
OES_texture_float: OES_texture_float;
OES_standard_derivatives: OES_standard_derivatives;
OES_element_index_uint: OES_element_index_uint;
ANGLE_instanced_arrays: ANGLE_instanced_arrays;
}
23 changes: 12 additions & 11 deletions packages/webgl/src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ModelAttributeSpecs,
ModelSpec
} from "./api";
import { error } from "./error";
import { isGL2Context } from "./utils";

export class WebGLArrayBuffer<T extends TypedArray> implements IWebGLBuffer<T> {
Expand All @@ -21,7 +22,7 @@ export class WebGLArrayBuffer<T extends TypedArray> implements IWebGLBuffer<T> {
mode = gl.STATIC_DRAW
) {
this.gl = gl;
this.buffer = gl.createBuffer();
this.buffer = gl.createBuffer() || error("error creating WebGL buffer");
this.target = target;
this.mode = mode;
data && this.set(data);
Expand Down Expand Up @@ -113,7 +114,7 @@ const compileAttribs = (

export const compileIndices = (
gl: WebGLRenderingContext,
index: IndexBufferSpec,
index: IndexBufferSpec | undefined,
mode: GLenum = gl.STATIC_DRAW
) => {
if (index) {
Expand All @@ -134,25 +135,26 @@ export const compileIndices = (
export const compileVAO = (gl: WebGLRenderingContext, spec: ModelSpec) => {
if (spec.shader) {
const isGL2 = isGL2Context(gl);
const ext = !isGL2 && gl.getExtension("OES_vertex_array_object");
const ext = !isGL2 ? gl.getExtension("OES_vertex_array_object") : null;
if (isGL2 || ext) {
let vao: WebGLVertexArrayObject;
if (isGL2) {
vao = (<WebGL2RenderingContext>gl).createVertexArray();
vao = (<WebGL2RenderingContext>gl).createVertexArray()!;
(<WebGL2RenderingContext>gl).bindVertexArray(vao);
} else {
vao = ext.createVertexArrayOES();
ext.bindVertexArrayOES(vao);
vao = ext!.createVertexArrayOES()!;
ext!.bindVertexArrayOES(vao);
}
!!vao && error("error creating VAO");
spec.shader.bindAttribs(spec.attribs);
if (spec.indices) {
spec.indices.buffer.bind();
if (spec.indices!) {
spec.indices!.buffer!.bind();
}
spec.shader.unbind(null);
spec.shader.unbind(<any>null);
if (isGL2) {
(<WebGL2RenderingContext>gl).bindVertexArray(null);
} else {
ext.bindVertexArrayOES(null);
ext!.bindVertexArrayOES(null);
}
return vao;
}
Expand All @@ -172,7 +174,6 @@ export const compileAttribPool = (
const attr = pool.specs[id];
spec[id] = {
buffer: buf,
data: null,
size: attr.size,
type: attr.type,
stride: pool.byteStride,
Expand Down
12 changes: 6 additions & 6 deletions packages/webgl/src/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isString } from "@thi.ng/checks";
import { WeblGLCanvasOpts } from "./api";
import { WebGLExtensionMap, WeblGLCanvasOpts } from "./api";
import { error } from "./error";

const defaultOpts: WebGLContextAttributes = {
Expand Down Expand Up @@ -36,16 +36,16 @@ export const glCanvas = (opts: Partial<WeblGLCanvasOpts> = {}) => {
return {
canvas,
gl,
ext: getExtensions(gl, opts.ext)
ext: getExtensions(gl, opts.ext!)
};
};

export const getExtensions = (
export const getExtensions = <K extends keyof WebGLExtensionMap>(
gl: WebGLRenderingContext,
ids: string[],
ids: K[],
required = true
) => {
const ext = {};
): Pick<WebGLExtensionMap, K> => {
const ext: any = {};
if (ids) {
for (let id of ids) {
ext[id] = gl.getExtension(id);
Expand Down
40 changes: 20 additions & 20 deletions packages/webgl/src/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export const draw = (specs: ModelSpec | ModelSpec[]) => {
const spec = _specs[i];
const indices = spec.indices;
const gl = spec.shader.gl;
bindTextures(spec.textures);
spec.textures && bindTextures(spec.textures);
spec.shader.prepareState();
spec.shader.bind(spec);
if (indices) {
if (indices && indices.buffer) {
indices.buffer.bind();
if (spec.instances) {
drawInstanced(gl, spec);
} else {
gl.drawElements(
spec.mode,
spec.mode!,
spec.num,
indices.data instanceof Uint32Array
? gl.UNSIGNED_INT
Expand All @@ -31,21 +31,21 @@ export const draw = (specs: ModelSpec | ModelSpec[]) => {
if (spec.instances) {
drawInstanced(gl, spec);
} else {
gl.drawArrays(spec.mode, 0, spec.num);
gl.drawArrays(spec.mode!, 0, spec.num);
}
}
spec.shader.unbind(null);
spec.shader.unbind(<any>null);
}
};

const drawInstanced = (gl: WebGLRenderingContext, spec: ModelSpec) => {
const isGL2 = isGL2Context(gl);
const ext = !isGL2 && gl.getExtension("ANGLE_instanced_arrays");
const ext = !isGL2 ? gl.getExtension("ANGLE_instanced_arrays") : undefined;
if (!(isGL2 || ext)) {
error("instancing not supported");
}
const sattribs = spec.shader.attribs;
const iattribs = spec.instances.attribs;
const iattribs = spec.instances!.attribs;
spec.shader.bindAttribs(iattribs);
for (let id in iattribs) {
const attr = sattribs[id];
Expand All @@ -57,7 +57,7 @@ const drawInstanced = (gl: WebGLRenderingContext, spec: ModelSpec) => {
attr.loc,
div
)
: ext.vertexAttribDivisorANGLE(attr.loc, div);
: ext!.vertexAttribDivisorANGLE(attr.loc, div);
}
}
if (spec.indices) {
Expand All @@ -67,32 +67,32 @@ const drawInstanced = (gl: WebGLRenderingContext, spec: ModelSpec) => {
: gl.UNSIGNED_SHORT;
isGL2
? (<WebGL2RenderingContext>gl).drawElementsInstanced(
spec.mode,
spec.mode!,
spec.num,
type,
0,
spec.instances.num
spec.instances!.num
)
: ext.drawElementsInstancedANGLE(
spec.mode,
: ext!.drawElementsInstancedANGLE(
spec.mode!,
spec.num,
type,
0,
spec.instances.num
spec.instances!.num
);
} else {
isGL2
? (<WebGL2RenderingContext>gl).drawArraysInstanced(
spec.mode,
spec.mode!,
0,
spec.num,
spec.instances.num
spec.instances!.num
)
: ext.drawArraysInstancedANGLE(
spec.mode,
: ext!.drawArraysInstancedANGLE(
spec.mode!,
0,
spec.num,
spec.instances.num
spec.instances!.num
);
}
// reset attrib divisors to allow non-instanced draws later on
Expand All @@ -101,7 +101,7 @@ const drawInstanced = (gl: WebGLRenderingContext, spec: ModelSpec) => {
attr &&
(isGL2
? (<WebGL2RenderingContext>gl).vertexAttribDivisor(attr.loc, 0)
: ext.vertexAttribDivisorANGLE(attr.loc, 0));
: ext!.vertexAttribDivisorANGLE(attr.loc, 0));
}
spec.shader.unbind(null);
spec.shader.unbind(<any>null);
};
9 changes: 5 additions & 4 deletions packages/webgl/src/fbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import { isGL2Context } from "./utils";
export class FBO implements IFbo {
gl: WebGLRenderingContext;
fbo: WebGLFramebuffer;
ext: WEBGL_draw_buffers;
ext?: WEBGL_draw_buffers;
maxAttachments: number;

constructor(gl: WebGLRenderingContext, opts?: Partial<FboOpts>) {
this.gl = gl;
this.fbo = gl.createFramebuffer();
this.fbo = gl.createFramebuffer() || error("error creating FBO");
this.ext = !isGL2Context(gl)
? gl.getExtension("WEBGL_draw_buffers")
? gl.getExtension("WEBGL_draw_buffers") ||
error("missing WEBGL_draw_buffers ext")
: undefined;
this.maxAttachments = gl.getParameter(GL_MAX_COLOR_ATTACHMENTS_WEBGL);
opts && this.configure(opts);
Expand Down Expand Up @@ -118,7 +119,7 @@ export class FBO implements IFbo {
case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
error("FBO incomplete missing attachment");
default:
error(`FBO error: ${err}`);
return error(`FBO error: ${err}`);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webgl/src/geo/cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface CubeOpts {
// prettier-ignore
export const cube = (opts?: Partial<CubeOpts>) => {
opts = { size: 1, normal: true, uv: true, ...opts};
const s = opts.size;
const s = opts.size!;
const spec: ModelSpec = {
attribs: {
position: {
Expand All @@ -20,7 +20,7 @@ export const cube = (opts?: Partial<CubeOpts>) => {
data: new Uint16Array([0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23])
},
uniforms: {},
shader: null,
shader: <any>null,
num: 36
};
opts.normal && (spec.attribs.normal = {
Expand Down
2 changes: 1 addition & 1 deletion packages/webgl/src/geo/quad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const quad = (uv = true): ModelSpec => ({
: null)
},
uniforms: {},
shader: null,
shader: <any>null,
mode: 5, // TRIANGLE_STRIP,
num: 4
});
11 changes: 7 additions & 4 deletions packages/webgl/src/glsl/assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import { ShaderSnippet } from "../api";
* @param spec
* @param graph
*/
const buildGraph = (spec: ShaderSnippet, graph?: DGraph<ShaderSnippet>) =>
const buildGraph = (
graph: DGraph<ShaderSnippet>,
spec: ShaderSnippet
): DGraph<ShaderSnippet> =>
spec.deps
? spec.deps.reduce(
(graph, d) => buildGraph(d, graph.addDependency(spec, d)),
graph || new DGraph<ShaderSnippet>()
(graph, d) => buildGraph(graph.addDependency(spec, d), d),
graph
)
: graph;

Expand All @@ -23,7 +26,7 @@ const buildGraph = (spec: ShaderSnippet, graph?: DGraph<ShaderSnippet>) =>
*/
export const assemble = (spec: ShaderSnippet) =>
spec.deps
? buildGraph(spec)
? buildGraph(new DGraph<ShaderSnippet>(), spec)
.sort()
.map((s) => s.src)
.join("\n")
Expand Down
6 changes: 4 additions & 2 deletions packages/webgl/src/glsl/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ const arrayDecl = (
) => {
const type = isArray(decl) ? decl[0] : decl;
return type >= GLSL.bool_array
? `${qualifier} ${GLSL[type].replace("_array", "")} ${id}[${decl[1]}];`
? `${qualifier} ${GLSL[type].replace("_array", "")} ${id}[${
(<[GLSL, number]>decl)[1]
}];`
: `${qualifier} ${GLSL[type]} ${id};`;
};

Expand All @@ -86,7 +88,7 @@ export const VERSION_CHECK = (ver: number, ok: string, fail = "") => {
let cmp = ">=";
if (!ok) {
ok = fail;
fail = null;
fail = <any>null;
cmp = "<";
}
return `#if __VERSION__ ${cmp} ${ver}
Expand Down
Loading

0 comments on commit ad672c7

Please sign in to comment.