Skip to content

Commit

Permalink
feat(webgl): add blending & stencil enums/types
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 15, 2019
1 parent 759ace7 commit c8898a0
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 20 deletions.
34 changes: 34 additions & 0 deletions packages/webgl/src/api/blend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Tuple } from "@thi.ng/api";

export const enum Blend {
ZERO = 0,
ONE = 1,
SRC_COLOR = 768,
ONE_MINUS_SRC_COLOR = 769,
DST_COLOR = 774,
ONE_MINUS_DST_COLOR = 775,
SRC_ALPHA = 770,
ONE_MINUS_SRC_ALPHA = 771,
DST_ALPHA = 772,
ONE_MINUS_DST_ALPHA = 773,
CONSTANT_COLOR = 32769,
ONE_MINUS_CONSTANT_COLOR = 32770,
CONSTANT_ALPHA = 32771,
ONE_MINUS_CONSTANT_ALPHA = 32772,
SRC_ALPHA_SATURATE = 776
}

export const enum BlendEquation {
FUNC_ADD = 32774,
FUNC_REVERSE_SUBTRACT = 32779,
FUNC_SUBTRACT = 32778,
MAX = 32776,
MIN = 32775
}

export type BlendFunc = Tuple<Blend, 2>;

export const DEFAULT_BLEND: BlendFunc = [
Blend.SRC_ALPHA,
Blend.ONE_MINUS_SRC_ALPHA
];
9 changes: 0 additions & 9 deletions packages/webgl/src/api/constants.ts

This file was deleted.

13 changes: 7 additions & 6 deletions packages/webgl/src/api/shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
IDeref,
IObjectOf,
IRelease,
Tuple,
TypedArray
} from "@thi.ng/api";
import { Func, Sym } from "@thi.ng/shader-ast";
import { GLSLTarget } from "@thi.ng/shader-ast-glsl";
import { ReadonlyVec } from "@thi.ng/vectors";
import { BlendEquation, BlendFunc } from "./blend";
import {
GLIntVec,
GLIntVec2,
Expand All @@ -32,6 +32,7 @@ import {
GLVec4
} from "./glsl";
import { ModelAttributeSpecs, ModelSpec } from "./model";
import { StencilFnParams, StencilOpParams } from "./stencil";

export interface GLSLSyntax {
number: number;
Expand Down Expand Up @@ -259,27 +260,27 @@ export interface ShaderState {
* 2-element array of glBlendFunction coefficients
* (default: `[gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA]`)
*/
blendFn: Tuple<GLenum, 2>;
blendFn: BlendFunc;
/**
* glBlendEquation mode
*/
blendEq: GLenum;
blendEq: BlendEquation;
/**
* Enable stencil test
*/
stencil: boolean;
/**
* glStencilFn params
*/
stencilFn: Tuple<GLenum, 3>;
stencilFn: StencilFnParams;
/**
* glStencilOp params
*/
stencilOp: Tuple<GLenum, 3>;
stencilOp: StencilOpParams;
/**
* glStencilMask arg
*/
stencilMask: GLenum;
stencilMask: number;
}

export interface ShaderOpts<T> {
Expand Down
27 changes: 27 additions & 0 deletions packages/webgl/src/api/stencil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Tuple } from "@thi.ng/api";

export const enum StencilOp {
KEEP = 7680,
ZERO = 0,
REPLACE = 7681,
INCR = 7682,
INCR_WRAP = 34055,
DECR = 7683,
DECR_WRAP = 34056,
INVERT = 5386
}

export const enum StencilFn {
NEVER = 512,
LESS = 513,
EQUAL = 514,
LEQUAL = 515,
GREATER = 516,
NOTEQUAL = 517,
GEQUAL = 518,
ALWAYS = 519
}

export type StencilOpParams = Tuple<StencilOp, 3>;

export type StencilFnParams = [StencilFn, number, number];
6 changes: 3 additions & 3 deletions packages/webgl/src/api/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ export const enum TextureType {
FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8dad
}

export enum TextureTarget {
export const enum TextureTarget {
TEXTURE_2D = 3553,
TEXTURE_3D = 32879,
TEXTURE_CUBE_MAP = 34067,
TEXTURE_2D_ARRAY = 35866
}

export enum TextureFilter {
export const enum TextureFilter {
LINEAR = 9729,
NEAREST = 9728,
NEAREST_MIPMAP_NEAREST = 9984,
Expand All @@ -107,7 +107,7 @@ export enum TextureFilter {
LINEAR_MIPMAP_LINEAR = 9987
}

export enum TextureRepeat {
export const enum TextureRepeat {
REPEAT = 10497,
CLAMP = 33071,
REPEAT_MIRROR = 33648
Expand Down
4 changes: 3 additions & 1 deletion packages/webgl/src/fbo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { assert } from "@thi.ng/api";
import { FboOpts, IFbo } from "./api/buffers";
import { GL_COLOR_ATTACHMENT0_WEBGL, GL_MAX_COLOR_ATTACHMENTS_WEBGL } from "./api/constants";
import { ITexture } from "./api/texture";
import { error } from "./error";
import { RBO } from "./rbo";
import { isGL2Context } from "./utils";

const GL_COLOR_ATTACHMENT0_WEBGL = 0x8ce0;
const GL_MAX_COLOR_ATTACHMENTS_WEBGL = 0x8cdf;

/**
* WebGL framebuffer wrapper w/ automatic detection & support for
* multiple render targets (color attachments) and optional depth
Expand Down
2 changes: 1 addition & 1 deletion packages/webgl/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./api/blend";
export * from "./api/buffers";
export * from "./api/canvas";
export * from "./api/constants";
export * from "./api/ext";
export * from "./api/glsl";
export * from "./api/gpgpu";
Expand Down
36 changes: 36 additions & 0 deletions packages/webgl/src/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,39 @@ export const FX_SHADER_SPEC: ShaderSpec = {
state: { depth: false },
ext: {}
};

export type GPGPUSize = number | [number, number];
export interface ShaderPipelineOpts {
size: GPGPUSize;
gl: WebGLRenderingContext | WebGL2RenderingContext;
// canvas?: HTMLCanvasElement;
version?: 1 | 2;
textures: IObjectOf<TextureOpts>;
passes: ShaderPass;
}

export interface ShaderPass {
inputs: string[];
outputs: string[];
size?: [number, number];
uniforms?: ShaderUniformSpecs;
fn: ShaderFn;
}

// export const shaderPipeline = (opts: ShaderPipelineOpts) => {
// let width: number, height: number;
// const gl = opts.gl;
// if (isNumber(opts.size)) {
// width = height = ceilPow2(Math.ceil(Math.sqrt(opts.size / 4)));
// } else {
// [width, height] = opts.size;
// }
// const textures = Object.keys(opts.textures).reduce((acc, id) => {
// const tex = opts.textures[id];
// const format = TEX_FORMATS[tex.format];
// acc[id] = _texture(gl, {
// width,
// height
// });
// }, {});
// };

0 comments on commit c8898a0

Please sign in to comment.