Skip to content

Commit

Permalink
feat(webgl): update texture config, split api.ts into mult files
Browse files Browse the repository at this point in the history
- TextureOpts.format now refers to internal format, with
  base format & type inferred from TEX_FORMATS decls
- add preliminary 3D texture support
  • Loading branch information
postspectacular committed Jul 14, 2019
1 parent d064894 commit 052552f
Show file tree
Hide file tree
Showing 30 changed files with 1,390 additions and 1,249 deletions.
1,153 changes: 0 additions & 1,153 deletions packages/webgl/src/api.ts

This file was deleted.

59 changes: 59 additions & 0 deletions packages/webgl/src/api/buffers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { IBind, IRelease } from "@thi.ng/api";
import { ITexture } from "./texture";

export type IndexBufferData = Uint16Array | Uint32Array;

export interface IWebGLBuffer<T> extends IBind<void>, IRelease {
set(data: T, mode?: GLenum): void;
setChunk(data: T, offset: number): void;
}

export interface IConfigure<T> {
configure(opts: T): boolean;
}

export interface IFbo
extends IBind<void>,
IConfigure<Partial<FboOpts>>,
IRelease {}

export interface IRenderBuffer extends IBind<void>, IRelease {
buffer: WebGLRenderbuffer;
format: GLenum;
width: number;
height: number;
}

export interface IndexBufferSpec {
/**
* Backing `WebGLBuffer` instance. Usually this will be
* auto-initialized by `makeBuffersInSpec()`
*/
buffer?: IWebGLBuffer<IndexBufferData>;
/**
* Raw attribute data from which `buffer` will be initialized
*/
data: IndexBufferData;
}

export interface FboOpts {
/**
* Array of Texture instances to be used as color attachments.
* Multiple attachments are only allowed if the `webgl_draw_buffers`
* extension is available. The texture at `[0]` will be mapped to
* `COLOR_ATTACHMENT0` (or `COLOR_ATTACHMENT0_WEBGL`), other indices
* are mapped to their respective attachment IDs.
*/
tex: ITexture[];
/**
* Optional pre-instantiated `RenderBuffer` to be used as depth
* buffer for this FBO.
*/
depth?: ITexture | IRenderBuffer;
}

export interface RboOpts {
format?: number;
width: number;
height: number;
}
13 changes: 13 additions & 0 deletions packages/webgl/src/api/canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { WebGLExtensionMap } from "./ext";

export interface WeblGLCanvasOpts {
canvas: string | HTMLCanvasElement;
parent: HTMLElement;
opts: Partial<WebGLContextAttributes>;
version: 1 | 2;
width: number;
height: number;
autoScale: boolean;
onContextLost: EventListener;
ext: (keyof WebGLExtensionMap)[];
}
9 changes: 9 additions & 0 deletions packages/webgl/src/api/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Tuple } from "@thi.ng/api";

export const GL_COLOR_ATTACHMENT0_WEBGL = 0x8ce0;
export const GL_MAX_COLOR_ATTACHMENTS_WEBGL = 0x8cdf;
export const GL_RGBA = 0x1908;
export const GL_RGBA32F = 0x8814;

// [SRC_ALPHA, ONE_MINUS_SRC_ALPHA]
export const DEFAULT_BLEND: Tuple<GLenum, 2> = [0x302, 0x303];
36 changes: 36 additions & 0 deletions packages/webgl/src/api/ext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const GL_EXT_INFO = {
WEBGL_draw_buffers: {
gl: true,
alias: "GL_EXT_draw_buffers"
},
OES_standard_derivatives: {
gl: true,
alias: "GL_OES_standard_derivatives"
}
};

export interface WebGLExtensionMap {
EXT_blend_minmax: EXT_blend_minmax;
EXT_color_buffer_float: WEBGL_color_buffer_float;
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;
}
62 changes: 62 additions & 0 deletions packages/webgl/src/api/glsl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Tuple } from "@thi.ng/api";
import { Type } from "@thi.ng/shader-ast";

export type GLSL = Type;

export type GLVec = number[] | Float32Array;
export type GLVec2 = Tuple<number, 2> | Float32Array;
export type GLVec3 = Tuple<number, 3> | Float32Array;
export type GLVec4 = Tuple<number, 4> | Float32Array;

export type GLIntVec = number[] | Int32Array;
export type GLUintVec = number[] | Uint32Array;
export type GLIntVec2 = Tuple<number, 2> | Int32Array;
export type GLIntVec3 = Tuple<number, 3> | Int32Array;
export type GLIntVec4 = Tuple<number, 4> | Int32Array;

export type GLMat2 = Tuple<number, 4> | Float32Array;
export type GLMat3 = Tuple<number, 9> | Float32Array;
export type GLMat4 = Tuple<number, 16> | Float32Array;
export type GLMat23 = Tuple<number, 6> | Float32Array;
export type GLMat24 = Tuple<number, 8> | Float32Array;
export type GLMat34 = Tuple<number, 12> | Float32Array;

export type GLSLScalarType =
| "bool"
| "float"
| "int"
| "uint"
| "sampler2D"
| "samplerCube";

export type GLSLArrayType =
| "bool[]"
| "int[]"
| "uint[]"
| "float[]"
| "bvec2[]"
| "bvec3[]"
| "bvec4[]"
| "ivec2[]"
| "ivec3[]"
| "ivec4[]"
| "uvec2[]"
| "uvec3[]"
| "uvec4[]"
| "vec2[]"
| "vec3[]"
| "vec4[]"
| "mat2[]"
| "mat3[]"
| "mat4[]"
// | "mat2x3[]"
// | "mat2x4[]"
// | "mat3x2[]"
// | "mat3x4[]"
// | "mat4x2[]"
// | "mat4x3[]"
| "sampler2D[]"
| "sampler3D[]"
| "samplerCube[]";

export type GLSLExtensionBehavior = "require" | "warn" | boolean;
38 changes: 38 additions & 0 deletions packages/webgl/src/api/gpgpu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TypedArray } from "@thi.ng/api";
import {
ShaderFn,
ShaderSpec,
ShaderUniformSpecs,
UniformValues
} from "./shader";
import { ITexture, TextureOpts } from "./texture";

export interface GPGPUOpts {
size: number;
inputs?: number | GPGPUTextureConfig[];
outputs?: number | GPGPUTextureConfig[];
gl?: WebGLRenderingContext;
version?: 1 | 2;
}

export interface GPGPUTextureConfig
extends Partial<
// Pick<TextureOpts, "internalFormat" | "format" | "type" | "flip">
Pick<TextureOpts, "format" | "type" | "flip">
> {
stride: number;
}

export interface GPGPUJobConfig {
shader: ShaderSpec;
src: string | ShaderFn;
uniforms: ShaderUniformSpecs;
inputs: number;
outputs?: number;
}

export interface GPGPUJobExecOpts {
inputs: (ITexture | TypedArray)[];
outputs?: number[];
uniforms?: UniformValues;
}
7 changes: 7 additions & 0 deletions packages/webgl/src/api/material.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { GLVec3 } from "./glsl";

export interface Material {
ambientCol: GLVec3;
diffuseCol: GLVec3;
specularCol: GLVec3;
}
98 changes: 98 additions & 0 deletions packages/webgl/src/api/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { IObjectOf } from "@thi.ng/api";
import { AttribPool } from "@thi.ng/vector-pools";
import { IndexBufferSpec, IWebGLBuffer } from "./buffers";
import { AttribBufferData, IShader, UniformValues } from "./shader";
import { ITexture } from "./texture";

export type ModelAttributeSpecs = IObjectOf<ModelAttributeSpec>;

export interface ModelSpec {
/**
* Initialized `IShader` instance
*/
shader: IShader;
/**
* GLSL attribute declarations
*/
attribs: ModelAttributeSpecs;
/**
* Geometry attributes given as `AttribPool` instance.
*/
attribPool?: AttribPool;
/**
* GLSL uniform value overrides
*/
uniforms?: UniformValues;
/**
* Buffer spec for indexed geometry
*/
indices?: IndexBufferSpec;
/**
* Array of initialized `ITexture` instances.
* Each non-null item will be auto-bound to its respective texture unit,
* each time the model is drawn via `draw()`
*/
textures?: ITexture[];
/**
* Extra configuration for instanced geometry
*/
instances?: InstancingSpec;
/**
* WebGL draw mode. Defaults to `TRIANGLES`
*/
mode?: GLenum;
/**
* Number of vertices/indices to draw
*/
num: number;
}

/**
* Data specification of a single WebGL attribute
*/
export interface ModelAttributeSpec {
/**
* Backing `WebGLArrayBuffer` instance. Usually this will be
* auto-initialized by `compileBuffers()`
*/
buffer?: IWebGLBuffer<AttribBufferData>;
/**
* Raw attribute data from which `buffer` will be initialized
*/
data?: AttribBufferData;
/**
* Attribute element size (in component values, not bytes).
* Default: 3
*/
size?: number;
/**
* Auto-normalization flag when writing buffer data.
* Default: false
*/
normalized?: boolean;
/**
* Byte offset of 1st attrib component.
* Default: 0
*/
offset?: number;
/**
* Attribute stride in bytes.
* Default: 0 = densely packed
*/
stride?: number;
/**
* Attribute's WebGL data type.
* Default: gl.FLOAT
*/
type?: GLenum;
/**
* Only used for instanced attributes.
* See: https://www.khronos.org/registry/OpenGL/extensions/ANGLE/ANGLE_instanced_arrays.txt
*/
divisor?: number;
}

export interface InstancingSpec {
attribs: IObjectOf<ModelAttributeSpec>;
num: number;
}
Loading

0 comments on commit 052552f

Please sign in to comment.