Skip to content

Commit

Permalink
refactor(webgl): rename factory functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: #210, rename factory functions (`defXXX`)

- rename buffer() => defBuffer()
- rename fbo() => defFBO()
- rename rbo() => defRBO()
- rename multipass() => defMultiPass()
- rename shader() => defShader()
- rename texture() => defTexture()
- rename cubeMap() => defTextureCubeMap()
- rename floatTexture() => defTextureFloat()
- rename cube() => defCubeModel()
- rename quad() => defQuadModel()
  • Loading branch information
postspectacular committed Mar 28, 2020
1 parent 71d73c3 commit 633f693
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 85 deletions.
12 changes: 6 additions & 6 deletions packages/webgl/src/buffer.ts
@@ -1,14 +1,14 @@
import type { TypedArray } from "@thi.ng/api";
import { AttribPool } from "@thi.ng/vector-pools";
import type { IndexBufferSpec, IWebGLBuffer } from "./api/buffers";
import {
DrawMode,
ModelAttributeSpec,
ModelAttributeSpecs,
ModelSpec
ModelSpec,
} from "./api/model";
import { isGL2Context } from "./checks";
import { error } from "./error";
import type { TypedArray } from "@thi.ng/api";
import type { IndexBufferSpec, IWebGLBuffer } from "./api/buffers";

export class WebGLArrayBuffer<T extends TypedArray> implements IWebGLBuffer<T> {
gl: WebGLRenderingContext;
Expand Down Expand Up @@ -58,7 +58,7 @@ export class WebGLArrayBuffer<T extends TypedArray> implements IWebGLBuffer<T> {
}
}

export const buffer = (
export const defBuffer = (
gl: WebGLRenderingContext,
data?: TypedArray,
target = gl.ARRAY_BUFFER,
Expand Down Expand Up @@ -161,7 +161,7 @@ export const compileAttribPool = (
target = gl.ARRAY_BUFFER,
mode = gl.STATIC_DRAW
) => {
const buf = buffer(gl, pool.bytes(), target, mode);
const buf = defBuffer(gl, pool.bytes(), target, mode);
const spec = <ModelAttributeSpecs>{};
for (let id of ids || Object.keys(pool.specs)) {
const attr = pool.specs[id];
Expand All @@ -170,7 +170,7 @@ export const compileAttribPool = (
size: attr.size,
type: attr.type,
stride: pool.byteStride,
offset: attr.byteOffset
offset: attr.byteOffset,
};
}
return spec;
Expand Down
4 changes: 2 additions & 2 deletions packages/webgl/src/fbo.ts
@@ -1,9 +1,9 @@
import { assert } from "@thi.ng/api";
import type { FboOpts, IFbo } from "./api/buffers";
import { ITexture, TEX_FORMATS } from "./api/texture";
import { isGL2Context } from "./checks";
import { error } from "./error";
import { RBO } from "./rbo";
import type { FboOpts, IFbo } from "./api/buffers";

const GL_COLOR_ATTACHMENT0_WEBGL = 0x8ce0;
const GL_MAX_COLOR_ATTACHMENTS_WEBGL = 0x8cdf;
Expand Down Expand Up @@ -131,5 +131,5 @@ export class FBO implements IFbo {
}
}

export const fbo = (gl: WebGLRenderingContext, opts?: Partial<FboOpts>) =>
export const defFBO = (gl: WebGLRenderingContext, opts?: Partial<FboOpts>) =>
new FBO(gl, opts);
2 changes: 1 addition & 1 deletion packages/webgl/src/geo/cube.ts
Expand Up @@ -7,7 +7,7 @@ export interface CubeOpts {
}

// prettier-ignore
export const cube = (opts?: Partial<CubeOpts>) => {
export const defCubeModel = (opts?: Partial<CubeOpts>) => {
opts = { size: 1, normal: true, uv: true, ...opts};
const s = opts.size!;
const spec: ModelSpec = {
Expand Down
12 changes: 6 additions & 6 deletions packages/webgl/src/geo/quad.ts
@@ -1,22 +1,22 @@
import { DrawMode, ModelSpec } from "../api/model";

export const quad = (uv = true): ModelSpec => ({
export const defQuadModel = (uv = true): ModelSpec => ({
attribs: {
position: {
data: new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]),
size: 2
size: 2,
},
...(uv
? {
uv: {
data: new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]),
size: 2
}
size: 2,
},
}
: null)
: null),
},
uniforms: {},
shader: <any>null,
mode: DrawMode.TRIANGLE_STRIP,
num: 4
num: 4,
});
75 changes: 36 additions & 39 deletions packages/webgl/src/multipass.ts
@@ -1,25 +1,23 @@
import { assert, IObjectOf } from "@thi.ng/api";
import {
assocObj,
map,
range,
some,
transduce
} from "@thi.ng/transducers";
import { assocObj, map, range, some, transduce } from "@thi.ng/transducers";
import type { ExtensionBehaviors } from "./api/ext";
import type { Multipass, MultipassOpts, PassOpts } from "./api/multipass";
import type {
ShaderOutputSpec,
ShaderSpec,
ShaderUniformSpecs,
} from "./api/shader";
import type { ITexture } from "./api/texture";
import { compileModel } from "./buffer";
import { isFloatTexture, isGL2Context } from "./checks";
import { draw } from "./draw";
import { fbo } from "./fbo";
import { quad } from "./geo/quad";
import { shader } from "./shader";
import { defFBO } from "./fbo";
import { defQuadModel } from "./geo/quad";
import { defShader } from "./shader";
import { PASSTHROUGH_VS } from "./shaders/pipeline";
import { texture } from "./texture";
import type { ExtensionBehaviors } from "./api/ext";
import type { Multipass, MultipassOpts, PassOpts } from "./api/multipass";
import type { ShaderOutputSpec, ShaderSpec, ShaderUniformSpecs } from "./api/shader";
import type { ITexture } from "./api/texture";
import { defTexture } from "./texture";

export const multipass = (opts: MultipassOpts) => {
export const defMultiPass = (opts: MultipassOpts) => {
const gl = opts.gl;
const isGL2 = isGL2Context(gl);
const numPasses = opts.passes.length;
Expand All @@ -33,22 +31,22 @@ export const multipass = (opts: MultipassOpts) => {
vs: pass.vs || PASSTHROUGH_VS,
fs: pass.fs,
attribs: pass.attribs || {
position: "vec2"
position: "vec2",
},
varying: pass.varying,
uniforms: <ShaderUniformSpecs>{
...pass.uniforms,
...(numIns
? {
inputs: ["sampler2D[]", numIns, [...range(numIns)]]
inputs: ["sampler2D[]", numIns, [...range(numIns)]],
}
: null)
: null),
},
outputs: numOuts
? transduce(
map<number, [string, ShaderOutputSpec]>((i) => [
`output${i}`,
["vec4", i]
["vec4", i],
]),
assocObj(),
range(numOuts)
Expand All @@ -59,7 +57,7 @@ export const multipass = (opts: MultipassOpts) => {
post: pass.post,
replacePrelude: pass.replacePrelude,
generateDecls: pass.generateDecls,
ext
ext,
};
const floatIn = some((id) => isFloatTexture(textures[id]), pass.inputs);
const floatOut = some(
Expand All @@ -74,25 +72,22 @@ export const multipass = (opts: MultipassOpts) => {
ext[isGL2 ? "EXT_color_buffer_float" : "WEBGL_color_buffer_float"] =
"require";
}
return shader(gl, spec);
return defShader(gl, spec);
};

const textures = Object.keys(opts.textures).reduce(
(acc, id) => {
acc[id] = texture(gl, {
width: opts.width,
height: opts.height,
filter: gl.NEAREST,
wrap: gl.CLAMP_TO_EDGE,
image: null,
...opts.textures[id]
});
return acc;
},
<IObjectOf<ITexture>>{}
);
const textures = Object.keys(opts.textures).reduce((acc, id) => {
acc[id] = defTexture(gl, {
width: opts.width,
height: opts.height,
filter: gl.NEAREST,
wrap: gl.CLAMP_TO_EDGE,
image: null,
...opts.textures[id],
});
return acc;
}, <IObjectOf<ITexture>>{});

const model = compileModel(gl, quad(false));
const model = compileModel(gl, defQuadModel(false));
const models = opts.passes.map((pass) => {
const m = pass.model ? compileModel(gl, <any>pass.model) : { ...model };
m.shader = initShader(pass);
Expand All @@ -106,7 +101,9 @@ export const multipass = (opts: MultipassOpts) => {
const fbos = (useMainBuffer
? opts.passes.slice(0, numPasses - 1)
: opts.passes
).map((pass) => fbo(gl, { tex: pass.outputs.map((id) => textures[id]) }));
).map((pass) =>
defFBO(gl, { tex: pass.outputs.map((id) => textures[id]) })
);

const drawPass = (i: number, time: number) => {
const pass = opts.passes[i];
Expand Down Expand Up @@ -157,7 +154,7 @@ export const multipass = (opts: MultipassOpts) => {
passes: opts.passes,
fbos,
models,
textures
textures,
};

return instance;
Expand Down
4 changes: 2 additions & 2 deletions packages/webgl/src/rbo.ts
@@ -1,5 +1,5 @@
import { error } from "./error";
import type { IRenderBuffer, RboOpts } from "./api/buffers";
import { error } from "./error";

export class RBO implements IRenderBuffer {
gl: WebGLRenderingContext;
Expand Down Expand Up @@ -47,5 +47,5 @@ export class RBO implements IRenderBuffer {
}
}

export const rbo = (gl: WebGLRenderingContext, opts: RboOpts) =>
export const defRBO = (gl: WebGLRenderingContext, opts: RboOpts) =>
new RBO(gl, opts);
33 changes: 13 additions & 20 deletions packages/webgl/src/shader.ts
@@ -1,28 +1,24 @@
import type { Fn3, IDeref, IObjectOf } from "@thi.ng/api";
import {
existsAndNotNull,
implementsFunction,
isArray,
isBoolean,
isFunction
isFunction,
} from "@thi.ng/checks";
import { unsupported } from "@thi.ng/errors";
import {
input,
output,
program,
Sym,
sym,
uniform
} from "@thi.ng/shader-ast";
import { input, output, program, Sym, sym, uniform } from "@thi.ng/shader-ast";
import { GLSLVersion, targetGLSL } from "@thi.ng/shader-ast-glsl";
import { vals } from "@thi.ng/transducers";
import {
ExtensionBehavior,
ExtensionBehaviors,
ExtensionName,
GL_EXT_INFO
GL_EXT_INFO,
} from "./api/ext";
import type { GLSL } from "./api/glsl";
import { LOGGER } from "./api/logger";
import type { ModelAttributeSpecs, ModelSpec } from "./api/model";
import {
DEFAULT_OUTPUT,
GLSLDeclPrefixes,
Expand All @@ -37,16 +33,13 @@ import {
ShaderUniforms,
ShaderUniformSpecs,
UniformValue,
UniformValues
UniformValues,
} from "./api/shader";
import { getExtensions } from "./canvas";
import { isGL2Context } from "./checks";
import { error } from "./error";
import { GLSL_HEADER, NO_PREFIXES, SYNTAX } from "./syntax";
import { UNIFORM_SETTERS } from "./uniforms";
import type { Fn3, IDeref, IObjectOf } from "@thi.ng/api";
import type { GLSL } from "./api/glsl";
import type { ModelAttributeSpecs, ModelSpec } from "./api/model";

const ERROR_REGEXP = /ERROR: \d+:(\d+): (.*)/;

Expand Down Expand Up @@ -190,7 +183,7 @@ export class Shader implements IShader {
}
}

export const shader = (gl: WebGLRenderingContext, spec: ShaderSpec) => {
export const defShader = (gl: WebGLRenderingContext, spec: ShaderSpec) => {
const version = isGL2Context(gl)
? GLSLVersion.GLES_300
: GLSLVersion.GLES_100;
Expand Down Expand Up @@ -344,7 +337,7 @@ export const shaderSourceFromAST = (
const target = targetGLSL({
type,
version,
prelude
prelude,
});
return (
target(
Expand All @@ -354,8 +347,8 @@ export const shaderSourceFromAST = (
...vals(outputs),
...(<ShaderFn>spec[type])(target, unis, inputs, {
...outputs,
...outputAliases
})
...outputAliases,
}),
])
) + (spec.post ? "\n" + spec.post : "")
);
Expand Down Expand Up @@ -451,7 +444,7 @@ const initAttributes = (
} else {
res[id] = {
type,
loc: gl.getAttribLocation(prog, aid)
loc: gl.getAttribLocation(prog, aid),
};
}
}
Expand Down Expand Up @@ -487,7 +480,7 @@ const initUniforms = (
setter: setter(gl, loc, defaultVal),
defaultFn,
defaultVal,
type
type,
};
} else {
error(`invalid uniform type: ${type}`);
Expand Down

0 comments on commit 633f693

Please sign in to comment.