@derschmale/tiny-helix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/BindGroup.d.ts +121 -0
- package/dist/BlendMode.d.ts +21 -0
- package/dist/Buffer.d.ts +66 -0
- package/dist/BufferDataWriter.d.ts +67 -0
- package/dist/CommandEncoder.d.ts +50 -0
- package/dist/ComputePass.d.ts +37 -0
- package/dist/ComputePipeline.d.ts +18 -0
- package/dist/Mesh.d.ts +177 -0
- package/dist/Pipeline.d.ts +47 -0
- package/dist/RenderPass.d.ts +101 -0
- package/dist/RenderPipeline.d.ts +77 -0
- package/dist/RenderTarget.d.ts +44 -0
- package/dist/Renderer.d.ts +44 -0
- package/dist/Sampler.d.ts +31 -0
- package/dist/Shader.d.ts +77 -0
- package/dist/Texture.d.ts +55 -0
- package/dist/TinyHelix.d.ts +163 -0
- package/dist/UniformBuffer.d.ts +84 -0
- package/dist/WebGPUContext.d.ts +70 -0
- package/dist/buffers/Buffer.d.ts +79 -0
- package/dist/buffers/BufferDataWriter.d.ts +68 -0
- package/dist/buffers/IBuffer.d.ts +4 -0
- package/dist/buffers/UniformBuffer.d.ts +111 -0
- package/dist/enums.d.ts +158 -0
- package/dist/index.d.ts +24 -0
- package/dist/main.js +2 -0
- package/dist/main.js.map +1 -0
- package/dist/tiny-helix.debug.js +3351 -0
- package/dist/tiny-helix.debug.js.map +1 -0
- package/dist/tiny-helix.esm.debug.js +3386 -0
- package/dist/tiny-helix.esm.debug.js.map +1 -0
- package/dist/tiny-helix.esm.js +2 -0
- package/dist/tiny-helix.esm.js.map +1 -0
- package/dist/tiny-helix.js +2 -0
- package/dist/tiny-helix.js.map +1 -0
- package/dist/utils/IndexedCollection.d.ts +4 -0
- package/dist/utils/float32ToFloat16.d.ts +5 -0
- package/dist/utils/mapUndefined.d.ts +6 -0
- package/dist/utils/padArrayBuffer.d.ts +10 -0
- package/package.json +54 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { WebGPUContext } from "./WebGPUContext";
|
|
2
|
+
import { Buffer } from "./Buffer";
|
|
3
|
+
export declare enum BaseType {
|
|
4
|
+
Float16 = 0,
|
|
5
|
+
Float32 = 1,
|
|
6
|
+
Uint = 2,
|
|
7
|
+
Sint = 3,
|
|
8
|
+
Boolean = 4
|
|
9
|
+
}
|
|
10
|
+
export type UBOFieldData = number[] | Float32Array | Uint32Array | Int32Array;
|
|
11
|
+
declare enum ContainerType {
|
|
12
|
+
Scalar = 0,
|
|
13
|
+
Vec = 1,
|
|
14
|
+
Matrix2 = 2,// a matrix of 2 vec[numElements]<baseType>
|
|
15
|
+
Matrix3 = 3,
|
|
16
|
+
Matrix4 = 4,
|
|
17
|
+
Array = 5
|
|
18
|
+
}
|
|
19
|
+
declare class MemberType {
|
|
20
|
+
baseType: BaseType;
|
|
21
|
+
containerType: ContainerType;
|
|
22
|
+
numElements: number;
|
|
23
|
+
constructor(baseType: BaseType, containerType: ContainerType, numElements: number);
|
|
24
|
+
get size(): number;
|
|
25
|
+
get alignment(): number;
|
|
26
|
+
}
|
|
27
|
+
interface Member {
|
|
28
|
+
type: MemberType;
|
|
29
|
+
offset: number;
|
|
30
|
+
}
|
|
31
|
+
export declare class UniformBufferLayoutBuilder {
|
|
32
|
+
private _size;
|
|
33
|
+
private _members;
|
|
34
|
+
withSize(size: number): this;
|
|
35
|
+
pushScalar(baseType: BaseType, name: string): this;
|
|
36
|
+
pushVec2(baseType: BaseType, name: string): this;
|
|
37
|
+
pushVec3(baseType: BaseType, name: string): this;
|
|
38
|
+
pushVec4(baseType: BaseType, name: string): this;
|
|
39
|
+
pushMatrix(baseType: BaseType, name: string, numRows: number, numCols: number): this;
|
|
40
|
+
build(): UniformBufferLayout;
|
|
41
|
+
private _pushField;
|
|
42
|
+
}
|
|
43
|
+
export declare class UniformBufferLayout {
|
|
44
|
+
readonly size: number;
|
|
45
|
+
private _members;
|
|
46
|
+
constructor(size: number, members: Map<string, Member>);
|
|
47
|
+
_writeScalar(name: string, value: number, target: DataView): void;
|
|
48
|
+
_writeVec(name: string, values: UBOFieldData, target: DataView): void;
|
|
49
|
+
_writeMatrix(name: string, values: UBOFieldData, target: DataView): void;
|
|
50
|
+
private _getMemberOrThrow;
|
|
51
|
+
}
|
|
52
|
+
export declare class UniformBuffer {
|
|
53
|
+
private _buffer;
|
|
54
|
+
private _layout;
|
|
55
|
+
private _dataInvalid;
|
|
56
|
+
private _data;
|
|
57
|
+
/**
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
constructor(layout: UniformBufferLayout, ctx: WebGPUContext);
|
|
61
|
+
/**
|
|
62
|
+
* Sets the value of a scalar field.
|
|
63
|
+
* @param name Name of the scalar field.
|
|
64
|
+
* @param value Value to set.
|
|
65
|
+
*/
|
|
66
|
+
setScalar(name: string, value: number): this;
|
|
67
|
+
/**
|
|
68
|
+
* Sets the values for a vector field.
|
|
69
|
+
* @param name Name of the vector field.
|
|
70
|
+
* @param values Array of values to set.
|
|
71
|
+
*/
|
|
72
|
+
setVec(name: string, values: number[]): this;
|
|
73
|
+
/**
|
|
74
|
+
* Sets the values for a matrix field.
|
|
75
|
+
* @param name Name of the matrix field.
|
|
76
|
+
* @param values Array of values to set. The values are expected to be in column-major order.
|
|
77
|
+
*/
|
|
78
|
+
setMatrix(name: string, values: UBOFieldData): this;
|
|
79
|
+
/**
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
_getBuffer(ctx: WebGPUContext): Buffer;
|
|
83
|
+
}
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/// <reference types="@webgpu/types" />
|
|
2
|
+
import { ColorSpace, TextureFormat } from "./enums";
|
|
3
|
+
/**
|
|
4
|
+
* Options for initializing WebGPU context
|
|
5
|
+
*/
|
|
6
|
+
export interface WebGPUContextOptions {
|
|
7
|
+
/** The canvas element to render to */
|
|
8
|
+
canvas?: HTMLCanvasElement;
|
|
9
|
+
/** Power preference for adapter selection */
|
|
10
|
+
powerPreference?: GPUPowerPreference;
|
|
11
|
+
/** Required features for the device */
|
|
12
|
+
requiredFeatures?: GPUFeatureName[];
|
|
13
|
+
/** Required limits for the device */
|
|
14
|
+
requiredLimits?: Record<string, number>;
|
|
15
|
+
colorSpace?: ColorSpace;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Manages WebGPU adapter, device, and context initialization
|
|
19
|
+
*/
|
|
20
|
+
export declare class WebGPUContext {
|
|
21
|
+
private _adapter;
|
|
22
|
+
private _device;
|
|
23
|
+
private _context;
|
|
24
|
+
private _format;
|
|
25
|
+
private _canvas;
|
|
26
|
+
private _colorSpace;
|
|
27
|
+
/**
|
|
28
|
+
* Gets the WebGPU adapter. Throws if not initialized.
|
|
29
|
+
*/
|
|
30
|
+
get adapter(): GPUAdapter;
|
|
31
|
+
/**
|
|
32
|
+
* Gets the WebGPU device. Throws if not initialized.
|
|
33
|
+
*/
|
|
34
|
+
get device(): GPUDevice;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the GPU canvas context. Throws if no canvas was configured.
|
|
37
|
+
*/
|
|
38
|
+
get context(): GPUCanvasContext;
|
|
39
|
+
/**
|
|
40
|
+
* Gets the preferred texture format used by the configured canvas/context.
|
|
41
|
+
*/
|
|
42
|
+
get format(): TextureFormat;
|
|
43
|
+
/**
|
|
44
|
+
* Gets the color space used by the configured canvas/context.
|
|
45
|
+
*/
|
|
46
|
+
get colorSpace(): ColorSpace;
|
|
47
|
+
/**
|
|
48
|
+
* Gets the configured canvas element. Throws if none was provided during initialization.
|
|
49
|
+
*/
|
|
50
|
+
get canvas(): HTMLCanvasElement;
|
|
51
|
+
/**
|
|
52
|
+
* Checks if WebGPU is supported in the current environment.
|
|
53
|
+
*/
|
|
54
|
+
static isSupported(): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Initializes the WebGPU context and device.
|
|
57
|
+
* @param options - Configuration options such as canvas and adapter preferences
|
|
58
|
+
* @throws Error if WebGPU is not supported or initialization fails
|
|
59
|
+
*/
|
|
60
|
+
initialize(options?: WebGPUContextOptions): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Internal helper to fetch the current swapchain texture.
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
getCurrentTexture(): GPUTexture;
|
|
66
|
+
/**
|
|
67
|
+
* Destroys the WebGPU context and releases resources
|
|
68
|
+
*/
|
|
69
|
+
destroy(): void;
|
|
70
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/// <reference types="@webgpu/types" />
|
|
2
|
+
import { WebGPUContext } from "../WebGPUContext";
|
|
3
|
+
import { IBuffer } from "./IBuffer";
|
|
4
|
+
/**
|
|
5
|
+
* Buffer usage flags re-exported from the WebGPU API for convenience.
|
|
6
|
+
* Use these when constructing buffers with `BufferBuilder.withUsage()`.
|
|
7
|
+
*/
|
|
8
|
+
export declare enum BufferUsage {
|
|
9
|
+
MapRead,
|
|
10
|
+
MapWrite,
|
|
11
|
+
CopySrc,
|
|
12
|
+
CopyDst,
|
|
13
|
+
Index,
|
|
14
|
+
Vertex,
|
|
15
|
+
Uniform,
|
|
16
|
+
Storage,
|
|
17
|
+
Indirect,
|
|
18
|
+
QueryResolve
|
|
19
|
+
}
|
|
20
|
+
export declare enum TextureUsage {
|
|
21
|
+
CopySrc,
|
|
22
|
+
CopyDst,
|
|
23
|
+
TextureBinding,
|
|
24
|
+
StorageBinding,
|
|
25
|
+
RenderAttachment
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Builder for creating GPU-backed buffers.
|
|
29
|
+
*
|
|
30
|
+
* Example:
|
|
31
|
+
* const buf = new BufferBuilder(ctx)
|
|
32
|
+
* .withUsage(BufferUsage.Vertex | BufferUsage.CopyDst)
|
|
33
|
+
* .withData(new Float32Array([...]).buffer)
|
|
34
|
+
* .build();
|
|
35
|
+
*/
|
|
36
|
+
export declare class BufferBuilder {
|
|
37
|
+
private _ctx;
|
|
38
|
+
private _data?;
|
|
39
|
+
private _size;
|
|
40
|
+
private _keepData;
|
|
41
|
+
private _usage;
|
|
42
|
+
constructor(ctx: WebGPUContext);
|
|
43
|
+
/**
|
|
44
|
+
* Add usage flags for the GPU buffer.
|
|
45
|
+
*/
|
|
46
|
+
withUsage(usage: BufferUsage): this;
|
|
47
|
+
/**
|
|
48
|
+
* Provide initial data for the buffer. If `keepOnCPU` is true the original
|
|
49
|
+
* ArrayBuffer is stored in the resulting `Buffer.data` field for readback or
|
|
50
|
+
* reuse.
|
|
51
|
+
*/
|
|
52
|
+
withData(data: ArrayBufferLike, keepOnCPU?: boolean): this;
|
|
53
|
+
/**
|
|
54
|
+
* Specify a size for the buffer, used when not providing data.
|
|
55
|
+
*/
|
|
56
|
+
withSize(size: number): this;
|
|
57
|
+
/**
|
|
58
|
+
* Create the GPU buffer and upload any provided data.
|
|
59
|
+
*/
|
|
60
|
+
build(): Buffer;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Lightweight wrapper around a GPUBuffer. Exposes the original CPU-side data
|
|
64
|
+
* (when kept) and the underlying GPU buffer for low-level interop.
|
|
65
|
+
*/
|
|
66
|
+
export declare class Buffer implements IBuffer {
|
|
67
|
+
readonly _inner: GPUBuffer;
|
|
68
|
+
readonly data?: ArrayBufferLike;
|
|
69
|
+
/**
|
|
70
|
+
* Create a Buffer from an existing GPUBuffer.
|
|
71
|
+
* @param inner The underlying GPUBuffer.
|
|
72
|
+
* @param data Optional CPU-side copy of the buffer contents.
|
|
73
|
+
*/
|
|
74
|
+
constructor(inner: GPUBuffer, data?: ArrayBufferLike);
|
|
75
|
+
/** Size of the GPU buffer in bytes. */
|
|
76
|
+
get size(): number;
|
|
77
|
+
_uploadData(ctx: WebGPUContext): void;
|
|
78
|
+
_getBufferResource(): GPUBindingResource;
|
|
79
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { IndexedCollection } from "../utils/IndexedCollection";
|
|
2
|
+
/**
|
|
3
|
+
* Utility for incrementally building binary data into an ArrayBuffer.
|
|
4
|
+
* Provides typed push helpers for common numeric types used in vertex/index buffers.
|
|
5
|
+
*/
|
|
6
|
+
export declare class BufferDataWriter {
|
|
7
|
+
private _buffer;
|
|
8
|
+
private _view;
|
|
9
|
+
private _offset;
|
|
10
|
+
/** Create a new writer with the given total byte size. */
|
|
11
|
+
constructor(byteSize: number);
|
|
12
|
+
/** Push an unsigned 8-bit integer. */
|
|
13
|
+
pushUint8(value: number): this;
|
|
14
|
+
/** Push two unsigned 8-bit integers. */
|
|
15
|
+
pushUint8x2(x: number, y: number): this;
|
|
16
|
+
/** Push four unsigned 8-bit integers. */
|
|
17
|
+
pushUint8x4(x: number, y: number, z: number, w: number): this;
|
|
18
|
+
/** Push a signed 8-bit integer. */
|
|
19
|
+
pushSint8(value: number): this;
|
|
20
|
+
/** Push two signed 8-bit integers. */
|
|
21
|
+
pushSint8x2(x: number, y: number): this;
|
|
22
|
+
/** Push four signed 8-bit integers. */
|
|
23
|
+
pushSint8x4(x: number, y: number, z: number, w: number): this;
|
|
24
|
+
/** Push an unsigned 16-bit integer (little-endian). */
|
|
25
|
+
pushUint16(value: number): this;
|
|
26
|
+
/** Push two unsigned 16-bit integers (little-endian). */
|
|
27
|
+
pushUint16x2(x: number, y: number): this;
|
|
28
|
+
/** Push four unsigned 16-bit integers (little-endian). */
|
|
29
|
+
pushUint16x4(x: number, y: number, z: number, w: number): this;
|
|
30
|
+
/** Push a signed 16-bit integer (little-endian). */
|
|
31
|
+
pushSint16(value: number): this;
|
|
32
|
+
/** Push two signed 16-bit integers (little-endian). */
|
|
33
|
+
pushSint16x2(x: number, y: number): this;
|
|
34
|
+
/** Push four signed 16-bit integers (little-endian). */
|
|
35
|
+
pushSint16x4(x: number, y: number, z: number, w: number): this;
|
|
36
|
+
/** Push a 16-bit float (little-endian). */
|
|
37
|
+
pushFloat16(value: number): this;
|
|
38
|
+
/** Push two 16-bit floats (little-endian). */
|
|
39
|
+
pushFloat16x2(x: number, y: number): this;
|
|
40
|
+
/** Push four 16-bit floats (little-endian). */
|
|
41
|
+
pushFloat16x4(x: number, y: number, z: number, w: number): this;
|
|
42
|
+
/** Push a 32-bit float (little-endian). */
|
|
43
|
+
pushFloat32(value: number): this;
|
|
44
|
+
/** Push two 32-bit floats (little-endian). */
|
|
45
|
+
pushFloat32x2(arr: [number, number] | IndexedCollection): this;
|
|
46
|
+
/** Push four 32-bit floats (little-endian). */
|
|
47
|
+
pushFloat32x3(arr: [number, number, number] | IndexedCollection): this;
|
|
48
|
+
/** Push an 8-bit float (little-endian). */
|
|
49
|
+
pushFloat32x4(arr: [number, number, number, number] | IndexedCollection): this;
|
|
50
|
+
/** Push an unsigned 32-bit integer (little-endian). */
|
|
51
|
+
pushUint32(value: number): this;
|
|
52
|
+
/** Push two unsigned 32-bit integers (little-endian). */
|
|
53
|
+
pushUint32x2(arr: [number, number] | Uint32Array): this;
|
|
54
|
+
/** Push three unsigned 32-bit integers (little-endian). */
|
|
55
|
+
pushUint32x3(arr: [number, number, number] | Uint32Array): this;
|
|
56
|
+
/** Push four unsigned 32-bit integers (little-endian). */
|
|
57
|
+
pushUint32x4(arr: [number, number, number, number] | Uint32Array): this;
|
|
58
|
+
/** Push a signed 32-bit integer (little-endian). */
|
|
59
|
+
pushSint32(value: number): this;
|
|
60
|
+
/** Push two signed 32-bit integers (little-endian). */
|
|
61
|
+
pushSint32x2(arr: [number, number] | Int32Array): this;
|
|
62
|
+
/** Push three signed 32-bit integers (little-endian). */
|
|
63
|
+
pushSint32x3(arr: [number, number, number] | Int32Array): this;
|
|
64
|
+
/** Push four signed 32-bit integers (little-endian). */
|
|
65
|
+
pushSint32x4(arr: [number, number, number, number] | Int32Array): this;
|
|
66
|
+
/** Returns the underlying ArrayBuffer containing all written data. */
|
|
67
|
+
get arrayBuffer(): ArrayBuffer;
|
|
68
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/// <reference types="@webgpu/types" />
|
|
2
|
+
import { WebGPUContext } from "../WebGPUContext";
|
|
3
|
+
import { IBuffer } from "./IBuffer";
|
|
4
|
+
import { IndexedCollection } from "../utils/IndexedCollection";
|
|
5
|
+
/**
|
|
6
|
+
* Primitive base types supported by the uniform buffer helpers.
|
|
7
|
+
* These correspond to WGSL scalar types used inside UBOs.
|
|
8
|
+
*/
|
|
9
|
+
export declare enum BaseType {
|
|
10
|
+
Float16 = 0,
|
|
11
|
+
Float32 = 1,
|
|
12
|
+
Uint = 2,
|
|
13
|
+
Sint = 3,
|
|
14
|
+
Boolean = 4
|
|
15
|
+
}
|
|
16
|
+
declare enum ContainerType {
|
|
17
|
+
Scalar = 0,
|
|
18
|
+
Vec = 1,
|
|
19
|
+
Matrix = 2,// a matrix of 2 vec[numElements]<baseType>
|
|
20
|
+
Array = 3
|
|
21
|
+
}
|
|
22
|
+
declare class MemberType {
|
|
23
|
+
baseType: BaseType;
|
|
24
|
+
containerType: ContainerType;
|
|
25
|
+
numRows: number;
|
|
26
|
+
numCols: number;
|
|
27
|
+
constructor(baseType: BaseType, containerType: ContainerType, numRows: number, numCols: number);
|
|
28
|
+
get size(): number;
|
|
29
|
+
get alignment(): number;
|
|
30
|
+
}
|
|
31
|
+
interface Member {
|
|
32
|
+
type: MemberType;
|
|
33
|
+
offset: number;
|
|
34
|
+
}
|
|
35
|
+
export declare class UniformBufferLayoutBuilder {
|
|
36
|
+
private _size;
|
|
37
|
+
private _members;
|
|
38
|
+
/**
|
|
39
|
+
* Reserve an explicit size for the resulting UBO layout. If not set the
|
|
40
|
+
* builder will compute a size that fits all declared members.
|
|
41
|
+
*/
|
|
42
|
+
withSize(size: number): this;
|
|
43
|
+
pushScalar(baseType: BaseType, name: string): this;
|
|
44
|
+
pushVec2(baseType: BaseType, name: string): this;
|
|
45
|
+
pushVec3(baseType: BaseType, name: string): this;
|
|
46
|
+
pushVec4(baseType: BaseType, name: string): this;
|
|
47
|
+
pushMatrix(baseType: BaseType, numRows: number, numCols: number, name: string): this;
|
|
48
|
+
/**
|
|
49
|
+
* Finalize and return a `UniformBufferLayout` describing member offsets and total size.
|
|
50
|
+
*/
|
|
51
|
+
build(): UniformBufferLayout;
|
|
52
|
+
private _pushField;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Describes the layout of a uniform buffer including member offsets and total size.
|
|
56
|
+
* Use `UniformBufferLayoutBuilder` to construct instances.
|
|
57
|
+
*/
|
|
58
|
+
export declare class UniformBufferLayout {
|
|
59
|
+
readonly size: number;
|
|
60
|
+
private _members;
|
|
61
|
+
constructor(size: number, members: Map<string, Member>);
|
|
62
|
+
/**
|
|
63
|
+
* Create typed DataViews for each member of the layout.
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
_createDataViews(target: ArrayBuffer): Map<string, [Member, IndexedCollection]>;
|
|
67
|
+
private _getMemberOrThrow;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* GPU-backed uniform buffer wrapper providing typed setters and an upload method.
|
|
71
|
+
* Use {@link TinyHelix.createUniformBuffer} to create instances.
|
|
72
|
+
*/
|
|
73
|
+
export declare class UniformBuffer implements IBuffer {
|
|
74
|
+
private _buffer;
|
|
75
|
+
private _data;
|
|
76
|
+
private _ctx;
|
|
77
|
+
private _dataViews;
|
|
78
|
+
/**
|
|
79
|
+
* Create a new UniformBuffer with the given layout and WebGPU context.
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
constructor(layout: UniformBufferLayout, ctx: WebGPUContext);
|
|
83
|
+
/**
|
|
84
|
+
* Sets the value of a scalar field.
|
|
85
|
+
* @param name Name of the scalar field.
|
|
86
|
+
* @param value Value to set.
|
|
87
|
+
*/
|
|
88
|
+
setScalar(name: string, value: number): this;
|
|
89
|
+
/**
|
|
90
|
+
* Sets the values for a vector field.
|
|
91
|
+
* @param name Name of the vector field.
|
|
92
|
+
* @param values Array of values to set.
|
|
93
|
+
*/
|
|
94
|
+
setVec(name: string, values: number[] | IndexedCollection): this;
|
|
95
|
+
/**
|
|
96
|
+
* Sets the values for a matrix field.
|
|
97
|
+
* @param name Name of the matrix field.
|
|
98
|
+
* @param values Array of values to set. The values are expected to be in column-major order.
|
|
99
|
+
*/
|
|
100
|
+
setMatrix(name: string, values: number[] | IndexedCollection): this;
|
|
101
|
+
/**
|
|
102
|
+
* Upload the current buffer contents to the GPU.
|
|
103
|
+
*/
|
|
104
|
+
upload(): this;
|
|
105
|
+
/**
|
|
106
|
+
* Internal accessor returning the underlying Buffer wrapper.
|
|
107
|
+
* @internal
|
|
108
|
+
*/
|
|
109
|
+
_getBufferResource(): GPUBindingResource;
|
|
110
|
+
}
|
|
111
|
+
export {};
|
package/dist/enums.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
export declare enum ColorSpace {
|
|
2
|
+
sRGB = "srgb",
|
|
3
|
+
DisplayP3 = "display-p3"
|
|
4
|
+
}
|
|
5
|
+
export declare enum FilterMode {
|
|
6
|
+
Nearest = "nearest",
|
|
7
|
+
Linear = "linear"
|
|
8
|
+
}
|
|
9
|
+
export declare enum AddressMode {
|
|
10
|
+
ClampToEdge = "clamp-to-edge",
|
|
11
|
+
Repeat = "repeat",
|
|
12
|
+
MirrorRepeat = "mirror-repeat"
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Face culling modes used by the render pipeline primitive state.
|
|
16
|
+
*/
|
|
17
|
+
export declare enum CullMode {
|
|
18
|
+
None = "none",
|
|
19
|
+
Front = "front",
|
|
20
|
+
Back = "back"
|
|
21
|
+
}
|
|
22
|
+
export declare enum TextureFormat {
|
|
23
|
+
R8Unorm = "r8unorm",
|
|
24
|
+
R8Snorm = "r8snorm",
|
|
25
|
+
R8Uint = "r8uint",
|
|
26
|
+
R8Sint = "r8sint",
|
|
27
|
+
R16Unorm = "r16unorm",
|
|
28
|
+
R16Snorm = "r16snorm",
|
|
29
|
+
R16Uint = "r16uint",
|
|
30
|
+
R16Sint = "r16sint",
|
|
31
|
+
R16Float = "r16float",
|
|
32
|
+
RG8Unorm = "rg8unorm",
|
|
33
|
+
RG8Snorm = "rg8snorm",
|
|
34
|
+
RG8Uint = "rg8uint",
|
|
35
|
+
RG8Sint = "rg8sint",
|
|
36
|
+
R32Uint = "r32uint",
|
|
37
|
+
R32Sint = "r32sint",
|
|
38
|
+
R32Float = "r32float",
|
|
39
|
+
RG16Unorm = "rg16unorm",
|
|
40
|
+
RG16Snorm = "rg16snorm",
|
|
41
|
+
RG16Uint = "rg16uint",
|
|
42
|
+
RG16Sint = "rg16sint",
|
|
43
|
+
RG16Float = "rg16float",
|
|
44
|
+
Rgba8Unorm = "rgba8unorm",
|
|
45
|
+
Rgba8UnormSrgb = "rgba8unorm-srgb",
|
|
46
|
+
Rgba8Snorm = "rgba8snorm",
|
|
47
|
+
Rgba8Uint = "rgba8uint",
|
|
48
|
+
Rgba8Sint = "rgba8sint",
|
|
49
|
+
Bgra8Unorm = "bgra8unorm",
|
|
50
|
+
Bgra8UnormSrgb = "bgra8unorm-srgb",
|
|
51
|
+
Rgb9e5Ufloat = "rgb9e5ufloat",
|
|
52
|
+
Rgb10a2Uint = "rgb10a2uint",
|
|
53
|
+
Rgb10a2Unorm = "rgb10a2unorm",
|
|
54
|
+
Rg11b10Ufloat = "rg11b10ufloat",
|
|
55
|
+
RG32Uint = "rg32uint",
|
|
56
|
+
RG32Sint = "rg32sint",
|
|
57
|
+
RG32Float = "rg32float",
|
|
58
|
+
Rgba16Unorm = "rgba16unorm",
|
|
59
|
+
Rgba16Snorm = "rgba16snorm",
|
|
60
|
+
Rgba16Uint = "rgba16uint",
|
|
61
|
+
Rgba16Sint = "rgba16sint",
|
|
62
|
+
Rgba16Float = "rgba16float",
|
|
63
|
+
Rgba32Uint = "rgba32uint",
|
|
64
|
+
Rgba32Sint = "rgba32sint",
|
|
65
|
+
Rgba32Float = "rgba32float",
|
|
66
|
+
Stencil8 = "stencil8",
|
|
67
|
+
Depth16Unorm = "depth16unorm",
|
|
68
|
+
Depth24Plus = "depth24plus",
|
|
69
|
+
Depth24PlusStencil8 = "depth24plus-stencil8",
|
|
70
|
+
Depth32Float = "depth32float",
|
|
71
|
+
Depth32FloatStencil8 = "depth32float-stencil8",
|
|
72
|
+
Bc1RgbaUnorm = "bc1-rgba-unorm",
|
|
73
|
+
Bc1RgbaUnormSrgb = "bc1-rgba-unorm-srgb",
|
|
74
|
+
Bc2RgbaUnorm = "bc2-rgba-unorm",
|
|
75
|
+
Bc2RgbaUnormSrgb = "bc2-rgba-unorm-srgb",
|
|
76
|
+
Bc3RgbaUnorm = "bc3-rgba-unorm",
|
|
77
|
+
Bc3RgbaUnormSrgb = "bc3-rgba-unorm-srgb",
|
|
78
|
+
Bc4RUnorm = "bc4-r-unorm",
|
|
79
|
+
Bc4RSnorm = "bc4-r-snorm",
|
|
80
|
+
Bc5RgUnorm = "bc5-rg-unorm",
|
|
81
|
+
Bc5RgSnorm = "bc5-rg-snorm",
|
|
82
|
+
Bc6hRgbUfloat = "bc6h-rgb-ufloat",
|
|
83
|
+
Bc6hRgbFloat = "bc6h-rgb-float",
|
|
84
|
+
Bc7RgbaUnorm = "bc7-rgba-unorm",
|
|
85
|
+
Bc7RgbaUnormSrgb = "bc7-rgba-unorm-srgb",
|
|
86
|
+
Etc2Rgb8Unorm = "etc2-rgb8unorm",
|
|
87
|
+
Etc2Rgb8UnormSrgb = "etc2-rgb8unorm-srgb",
|
|
88
|
+
Etc2Rgb8A1Unorm = "etc2-rgb8a1unorm",
|
|
89
|
+
Etc2Rgb8A1UnormSrgb = "etc2-rgb8a1unorm-srgb",
|
|
90
|
+
Etc2Rgba8Unorm = "etc2-rgba8unorm",
|
|
91
|
+
Etc2Rgba8UnormSrgb = "etc2-rgba8unorm-srgb",
|
|
92
|
+
EacR11Unorm = "eac-r11unorm",
|
|
93
|
+
EacR11Snorm = "eac-r11snorm",
|
|
94
|
+
EacRg11Unorm = "eac-rg11unorm",
|
|
95
|
+
EacRg11Snorm = "eac-rg11snorm",
|
|
96
|
+
Astc4x4Unorm = "astc-4x4-unorm",
|
|
97
|
+
Astc4x4UnormSrgb = "astc-4x4-unorm-srgb",
|
|
98
|
+
Astc5x4Unorm = "astc-5x4-unorm",
|
|
99
|
+
Astc5x4UnormSrgb = "astc-5x4-unorm-srgb",
|
|
100
|
+
Astc5x5Unorm = "astc-5x5-unorm",
|
|
101
|
+
Astc5x5UnormSrgb = "astc-5x5-unorm-srgb",
|
|
102
|
+
Astc6x5Unorm = "astc-6x5-unorm",
|
|
103
|
+
Astc6x5UnormSrgb = "astc-6x5-unorm-srgb",
|
|
104
|
+
Astc6x6Unorm = "astc-6x6-unorm",
|
|
105
|
+
Astc6x6UnormSrgb = "astc-6x6-unorm-srgb",
|
|
106
|
+
Astc8x5Unorm = "astc-8x5-unorm",
|
|
107
|
+
Astc8x5UnormSrgb = "astc-8x5-unorm-srgb",
|
|
108
|
+
Astc8x6Unorm = "astc-8x6-unorm",
|
|
109
|
+
Astc8x6UnormSrgb = "astc-8x6-unorm-srgb",
|
|
110
|
+
Astc8x8Unorm = "astc-8x8-unorm",
|
|
111
|
+
Astc8x8UnormSrgb = "astc-8x8-unorm-srgb",
|
|
112
|
+
Astc10x5Unorm = "astc-10x5-unorm",
|
|
113
|
+
Astc10x5UnormSrgb = "astc-10x5-unorm-srgb",
|
|
114
|
+
Astc10x6Unorm = "astc-10x6-unorm",
|
|
115
|
+
Astc10x6UnormSrgb = "astc-10x6-unorm-srgb",
|
|
116
|
+
Astc10x8Unorm = "astc-10x8-unorm",
|
|
117
|
+
Astc10x8UnormSrgb = "astc-10x8-unorm-srgb",
|
|
118
|
+
Astc10x10Unorm = "astc-10x10-unorm",
|
|
119
|
+
Astc10x10UnormSrgb = "astc-10x10-unorm-srgb",
|
|
120
|
+
Astc12x10Unorm = "astc-12x10-unorm",
|
|
121
|
+
Astc12x10UnormSrgb = "astc-12x10-unorm-srgb",
|
|
122
|
+
Astc12x12Unorm = "astc-12x12-unorm",
|
|
123
|
+
Astc12x12UnormSrgb = "astc-12x12-unorm-srgb"
|
|
124
|
+
}
|
|
125
|
+
export declare enum BlendFactor {
|
|
126
|
+
Zero = "zero",
|
|
127
|
+
One = "one",
|
|
128
|
+
SrcColor = "src",
|
|
129
|
+
OneMinusSrcColor = "one-minus-src",
|
|
130
|
+
SrcAlpha = "src-alpha",
|
|
131
|
+
OneMinusSrcAlpha = "one-minus-src-alpha",
|
|
132
|
+
DstColor = "dst",
|
|
133
|
+
OneMinusDstColor = "one-minus-dst",
|
|
134
|
+
DstAlpha = "dst-alpha",
|
|
135
|
+
OneMinusDstAlpha = "one-minus-dst-alpha",
|
|
136
|
+
SrcAlphaSaturated = "src-alpha-saturated",
|
|
137
|
+
Constant = "constant",
|
|
138
|
+
OneMinusConstant = "one-minus-constant",
|
|
139
|
+
Src1Color = "src1",
|
|
140
|
+
OneMinusSrc1Color = "one-minus-src1",
|
|
141
|
+
Src1Alpha = "src1-alpha",
|
|
142
|
+
OneMinusSrc1Alpha = "one-minus-src1-alpha"
|
|
143
|
+
}
|
|
144
|
+
export declare enum CompareFunction {
|
|
145
|
+
Never = "never",
|
|
146
|
+
Less = "less",
|
|
147
|
+
Equal = "equal",
|
|
148
|
+
LessEqual = "less-equal",
|
|
149
|
+
Greater = "greater",
|
|
150
|
+
NotEqual = "not-equal",
|
|
151
|
+
GreaterEqual = "greater-equal",
|
|
152
|
+
Always = "always"
|
|
153
|
+
}
|
|
154
|
+
export declare enum StorageAccess {
|
|
155
|
+
ReadWrite = "read-write",
|
|
156
|
+
Read = "read-only",
|
|
157
|
+
Write = "write-only"
|
|
158
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tiny-helix: A TypeScript library for WebGPU
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
export { TinyHelix, type TinyHelixOptions } from "./TinyHelix";
|
|
6
|
+
export { WebGPUContext, type WebGPUContextOptions } from "./WebGPUContext";
|
|
7
|
+
import { TinyHelix } from "./TinyHelix";
|
|
8
|
+
export default TinyHelix;
|
|
9
|
+
export * from "./BindGroup";
|
|
10
|
+
export * from "./BlendMode";
|
|
11
|
+
export * from "./CommandEncoder";
|
|
12
|
+
export * from "./ComputePass";
|
|
13
|
+
export * from "./ComputePipeline";
|
|
14
|
+
export * from "./enums";
|
|
15
|
+
export * from "./Mesh";
|
|
16
|
+
export * from "./RenderPass";
|
|
17
|
+
export * from "./RenderPipeline";
|
|
18
|
+
export * from "./RenderTarget";
|
|
19
|
+
export * from "./Sampler";
|
|
20
|
+
export * from "./Shader";
|
|
21
|
+
export * from "./Texture";
|
|
22
|
+
export * from "./buffers/Buffer";
|
|
23
|
+
export * from "./buffers/UniformBuffer";
|
|
24
|
+
export * from "./buffers/BufferDataWriter";
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
(()=>{"use strict";var n,t,i,r,o,x,e;!function(n){n.None="none",n.Front="front",n.Back="back"}(n||(n={})),(e=t||(t={}))[e.MapRead=GPUBufferUsage.MAP_READ]="MapRead",e[e.MapWrite=GPUBufferUsage.MAP_WRITE]="MapWrite",e[e.CopySrc=GPUBufferUsage.COPY_SRC]="CopySrc",e[e.CopyDst=GPUBufferUsage.COPY_DST]="CopyDst",e[e.Index=GPUBufferUsage.INDEX]="Index",e[e.Vertex=GPUBufferUsage.VERTEX]="Vertex",e[e.Uniform=GPUBufferUsage.UNIFORM]="Uniform",e[e.Storage=GPUBufferUsage.STORAGE]="Storage",e[e.Indirect=GPUBufferUsage.INDIRECT]="Indirect",e[e.QueryResolve=GPUBufferUsage.QUERY_RESOLVE]="QueryResolve",function(n){n.Uint16="uint16",n.Uint32="uint32"}(i||(i={})),function(n){n.TriangleList="triangle-list",n.TriangleStrip="triangle-strip",n.LineList="line-list",n.LineStrip="line-strip",n.PointList="point-list"}(r||(r={})),function(n){n.Clockwise="cw",n.CounterClockwise="ccw"}(o||(o={})),function(n){n.Uint8="uint8",n.Uint8x2="uint8x2",n.Uint8x4="uint8x4",n.Sint8="sint8",n.Sint8x2="sint8x2",n.Sint8x4="sint8x4",n.Unorm8="unorm8",n.Unorm8x2="unorm8x2",n.Unorm8x4="unorm8x4",n.Snorm8="snorm8",n.Snorm8x2="snorm8x2",n.Snorm8x4="snorm8x4",n.Uint16="uint16",n.Uint16x2="uint16x2",n.Uint16x4="uint16x4",n.Sint16="sint16",n.Sint16x2="sint16x2",n.Sint16x4="sint16x4",n.Unorm16="unorm16",n.Unorm16x2="unorm16x2",n.Unorm16x4="unorm16x4",n.Snorm16="snorm16",n.Snorm16x2="snorm16x2",n.Snorm16x4="snorm16x4",n.Float16="float16",n.Float16x2="float16x2",n.Float16x4="float16x4",n.Float32="float32",n.Float32x2="float32x2",n.Float32x3="float32x3",n.Float32x4="float32x4",n.Uint32="uint32",n.Uint32x2="uint32x2",n.Uint32x3="uint32x3",n.Uint32x4="uint32x4",n.Sint32="sint32",n.Sint32x2="sint32x2",n.Sint32x3="sint32x3",n.Sint32x4="sint32x4",n.Unorm10_10_10_2="unorm10-10-10-2",n.Unorm8x4_bgra="unorm8x4-bgra"}(x||(x={}))})();
|
|
2
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","mappings":"mBAKA,IAAYA,ECHA,ECEAC,EAoBAC,EAQAC,EAKAC,EDnCAC,GDGZ,SAAYL,GACR,cACA,gBACA,aACH,CAJD,CAAYA,IAAAA,EAAQ,MCHRK,EAAA,MAAW,KACnB,UAAUC,eAAeC,UAAQ,UACjC,aAAWD,eAAeE,WAAS,WACnC,YAAUF,eAAeG,UAAQ,UACjC,YAAUH,eAAeI,UAAQ,UACjC,UAAQJ,eAAeK,OAAK,QAC5B,WAASL,eAAeM,QAAM,SAC9B,YAAUN,eAAeO,SAAO,UAChC,YAAUP,eAAeQ,SAAO,UAChC,aAAWR,eAAeS,UAAQ,WAClC,iBAAeT,eAAeU,eAAa,eCR/C,SAAYf,GACR,kBACA,iBACH,CAHD,CAAYA,IAAAA,EAAW,KAoBvB,SAAYC,GACR,+BACA,iCACA,uBACA,yBACA,wBACH,CAND,CAAYA,IAAAA,EAAY,KAQxB,SAAYC,GACR,iBACA,wBACH,CAHD,CAAYA,IAAAA,EAAS,KAKrB,SAAYC,GACR,gBACA,oBACA,oBACA,gBACA,oBACA,oBACA,kBACA,sBACA,sBACA,kBACA,sBACA,sBACA,kBACA,sBACA,sBACA,kBACA,sBACA,sBACA,oBACA,wBACA,wBACA,oBACA,wBACA,wBACA,oBACA,wBACA,wBACA,oBACA,wBACA,wBACA,wBACA,kBACA,sBACA,sBACA,sBACA,kBACA,sBACA,sBACA,sBACA,oCACA,+BACH,CA1CD,CAAYA,IAAAA,EAAY,I","sources":["./src/RenderPipeline.ts","./src/Buffer.ts","./src/Mesh.ts"],"sourcesContent":["import {Shader} from \"./Shader\";\r\nimport {mapUndefined} from \"./utils/mapUndefined\";\r\nimport {WebGPUContext} from \"./WebGPUContext\";\r\nimport {Mesh} from \"./Mesh\";\r\n\r\nexport enum CullMode {\r\n None = 'none',\r\n Front = 'front',\r\n Back = 'back'\r\n}\r\n\r\nexport class RenderPipelineBuilder {\r\n private _shader?: Shader;\r\n private _colorTargets: GPUColorTargetState[] = [];\r\n private _defaultColorState: GPUColorTargetState;\r\n private _ctx: WebGPUContext;\r\n private _vertexEntry: string | undefined = undefined;\r\n private _fragmentEntry: string | undefined = undefined;\r\n private _label: string | undefined = undefined;\r\n private _overrideConstants: Record<string, number> = {};\r\n private _cullMode: CullMode = CullMode.Back;\r\n private _mesh: Mesh | undefined = undefined;\r\n\r\n constructor(ctx: WebGPUContext) {\r\n this._ctx = ctx;\r\n this._defaultColorState = {\r\n format: ctx.format,\r\n blend: undefined,\r\n writeMask: GPUColorWrite.ALL\r\n }\r\n }\r\n\r\n withLabel(label: string): this {\r\n this._label = label;\r\n return this;\r\n }\r\n\r\n withCullMode(value: CullMode): this {\r\n this._cullMode = value;\r\n return this;\r\n }\r\n\r\n withMesh(mesh: Mesh): this {\r\n this._mesh = mesh;\r\n return this;\r\n }\r\n\r\n withShader(shader: Shader): this {\r\n this._shader = shader;\r\n return this;\r\n }\r\n\r\n withVertexShader(entry: string): this {\r\n this._vertexEntry = entry;\r\n return this;\r\n }\r\n\r\n withFragmentShader(entry: string): this {\r\n this._fragmentEntry = entry;\r\n return this;\r\n }\r\n\r\n withColorTarget(format: GPUTextureFormat): this {\r\n this._colorTargets.push({\r\n format,\r\n blend: undefined,\r\n writeMask: GPUColorWrite.ALL\r\n });\r\n return this;\r\n }\r\n\r\n withOverrideConstant(id: string, value: number): this {\r\n this._overrideConstants[id] = value;\r\n return this;\r\n }\r\n\r\n withBlendMode(): this {\r\n this.lastColorTarget.blend = undefined;\r\n return this;\r\n }\r\n\r\n build(): RenderPipeline {\r\n if (!this._shader) {\r\n throw new Error(\"Shader not specified. Use withShader() to set the shader to use.\");\r\n }\r\n\r\n const shader = this._shader!;\r\n\r\n if (this._vertexEntry === undefined) {\r\n throw new Error(\"Shader does not contain a vertex entry point. Use ShaderBuilder.withVertexShader() to set one.\");\r\n }\r\n\r\n const colorTargets = this._colorTargets.length ? this._colorTargets : [this._defaultColorState];\r\n const primitive: GPUPrimitiveState | undefined = mapUndefined(this._mesh, mesh => ({\r\n topology: mesh.topology,\r\n frontFace: mesh.frontFace,\r\n cullMode: this._cullMode,\r\n }));\r\n\r\n const buffers = mapUndefined(this._mesh, mesh => {\r\n const buffers: GPUVertexBufferLayout[] = [];\r\n for (let i = 0; i < mesh.numStreams; ++i) {\r\n const attributes = mesh.getStreamAttributes(i)\r\n .filter(attr => shader.hasVertexAttribute(attr.name))\r\n .map(attr => ({\r\n shaderLocation: shader.getVertexAttributeLocation(attr.name)!,\r\n offset: attr.offset,\r\n format: attr.format,\r\n }));\r\n\r\n buffers.push({\r\n arrayStride: mesh.getStreamStride(i),\r\n stepMode: 'vertex',\r\n attributes\r\n });\r\n }\r\n return buffers;\r\n });\r\n\r\n const desc: GPURenderPipelineDescriptor = {\r\n label: this._label,\r\n layout: shader.layout ?? \"auto\",\r\n primitive,\r\n vertex: {\r\n buffers,\r\n module: shader.inner,\r\n entryPoint: this._vertexEntry!,\r\n constants: this._overrideConstants\r\n },\r\n fragment: mapUndefined(this._fragmentEntry, entry => ({\r\n module: shader.inner,\r\n entryPoint: entry,\r\n targets: colorTargets,\r\n constants: this._overrideConstants\r\n }))\r\n }\r\n\r\n const inner = this._ctx.device.createRenderPipeline(desc);\r\n\r\n return new RenderPipeline(inner, shader);\r\n }\r\n\r\n private get lastColorTarget(): GPUColorTargetState {\r\n return this._colorTargets.length ? this._colorTargets[this._colorTargets.length - 1] : this._defaultColorState;\r\n }\r\n}\r\n\r\n/**\r\n * Lightweight wrapper around GPURenderPipeline. Provides an internal accessor for low-level interop.\r\n */\r\nexport class RenderPipeline {\r\n private _inner: GPURenderPipeline;\r\n private _shader: Shader;\r\n\r\n constructor(inner: GPURenderPipeline, shader: Shader) {\r\n this._inner = inner;\r\n this._shader = shader;\r\n }\r\n\r\n /**\r\n * Internal accessor for the underlying GPURenderPipeline. Not intended for public use.\r\n * @internal\r\n */\r\n get inner(): GPURenderPipeline {\r\n return this._inner;\r\n }\r\n\r\n public getVertexAttributeLocation(name: string): number | undefined {\r\n return this._shader.getVertexAttributeLocation(name);\r\n }\r\n}","import {WebGPUContext} from \"./WebGPUContext\";\r\n\r\nexport enum BufferUsage {\r\n MapRead = GPUBufferUsage.MAP_READ,\r\n MapWrite = GPUBufferUsage.MAP_WRITE,\r\n CopySrc = GPUBufferUsage.COPY_SRC,\r\n CopyDst = GPUBufferUsage.COPY_DST,\r\n Index = GPUBufferUsage.INDEX,\r\n Vertex = GPUBufferUsage.VERTEX,\r\n Uniform = GPUBufferUsage.UNIFORM,\r\n Storage = GPUBufferUsage.STORAGE,\r\n Indirect = GPUBufferUsage.INDIRECT,\r\n QueryResolve = GPUBufferUsage.QUERY_RESOLVE\r\n}\r\n\r\nexport class BufferBuilder {\r\n private _ctx: WebGPUContext;\r\n private _data?: ArrayBufferLike;\r\n private _size = 0;\r\n private _keepData = false;\r\n private _usage: number = 0;\r\n\r\n constructor(ctx: WebGPUContext) {\r\n this._ctx = ctx;\r\n }\r\n\r\n withUsage(usage: BufferUsage): this\r\n {\r\n this._usage |= usage;\r\n return this;\r\n }\r\n\r\n withData(data: ArrayBufferLike, keepOnCPU: boolean = false) {\r\n this._data = data;\r\n this._keepData = keepOnCPU;\r\n return this;\r\n }\r\n\r\n build(): Buffer {\r\n const data = this._keepData? this._data : undefined;\r\n const buffer = this._ctx.device.createBuffer({\r\n size: this._size,\r\n usage: this._usage\r\n });\r\n if (this._data) {\r\n this._ctx.device.queue.writeBuffer(\r\n buffer,\r\n 0,\r\n this._data,\r\n 0,\r\n this._data.byteLength\r\n );\r\n }\r\n return new Buffer(buffer, data);\r\n }\r\n}\r\n\r\nexport class Buffer {\r\n private _data?: ArrayBufferLike;\r\n private _inner: GPUBuffer;\r\n\r\n constructor(inner: GPUBuffer, data?: ArrayBufferLike) {\r\n this._inner = inner;\r\n this._data = data;\r\n }\r\n\r\n get inner(): GPUBuffer { return this._inner; }\r\n get data(): ArrayBufferLike | undefined { return this._data; }\r\n}","import {Buffer, BufferBuilder, BufferUsage} from \"./Buffer\";\r\nimport {WebGPUContext} from \"./WebGPUContext\";\r\nimport {mapUndefined} from \"./utils/mapUndefined\";\r\n\r\nexport enum IndexFormat {\r\n Uint16 = 'uint16',\r\n Uint32 = 'uint32'\r\n}\r\n\r\nclass IndexBuffer {\r\n buffer: Buffer;\r\n format: IndexFormat;\r\n count: number;\r\n\r\n constructor(data: Uint16Array | Uint32Array, ctx: WebGPUContext, keepData: boolean = false) {\r\n this.format = data instanceof Uint16Array ? IndexFormat.Uint16 : IndexFormat.Uint32;\r\n this.count = data.length;\r\n this.buffer = new BufferBuilder(ctx)\r\n .withData(data.buffer, keepData)\r\n .withUsage(BufferUsage.CopyDst | BufferUsage.Index)\r\n .build();\r\n }\r\n}\r\n\r\nexport enum MeshTopology {\r\n TriangleList = 'triangle-list',\r\n TriangleStrip = 'triangle-strip',\r\n LineList = 'line-list',\r\n LineStrip = 'line-strip',\r\n PointList = 'point-list'\r\n}\r\n\r\nexport enum FrontFace {\r\n Clockwise = 'cw',\r\n CounterClockwise = 'ccw'\r\n}\r\n\r\nexport enum VertexFormat {\r\n Uint8 = 'uint8',\r\n Uint8x2 = 'uint8x2',\r\n Uint8x4 = 'uint8x4',\r\n Sint8 = 'sint8',\r\n Sint8x2 = 'sint8x2',\r\n Sint8x4 = 'sint8x4',\r\n Unorm8 = 'unorm8',\r\n Unorm8x2 = 'unorm8x2',\r\n Unorm8x4 = 'unorm8x4',\r\n Snorm8 = 'snorm8',\r\n Snorm8x2 = 'snorm8x2',\r\n Snorm8x4 = 'snorm8x4',\r\n Uint16 = 'uint16',\r\n Uint16x2 = 'uint16x2',\r\n Uint16x4 = 'uint16x4',\r\n Sint16 = 'sint16',\r\n Sint16x2 = 'sint16x2',\r\n Sint16x4 = 'sint16x4',\r\n Unorm16 = 'unorm16',\r\n Unorm16x2 = 'unorm16x2',\r\n Unorm16x4 = 'unorm16x4',\r\n Snorm16 = 'snorm16',\r\n Snorm16x2 = 'snorm16x2',\r\n Snorm16x4 = 'snorm16x4',\r\n Float16 = 'float16',\r\n Float16x2 = 'float16x2',\r\n Float16x4 = 'float16x4',\r\n Float32 = 'float32',\r\n Float32x2 = 'float32x2',\r\n Float32x3 = 'float32x3',\r\n Float32x4 = 'float32x4',\r\n Uint32 = 'uint32',\r\n Uint32x2 = 'uint32x2',\r\n Uint32x3 = 'uint32x3',\r\n Uint32x4 = 'uint32x4',\r\n Sint32 = 'sint32',\r\n Sint32x2 = 'sint32x2',\r\n Sint32x3 = 'sint32x3',\r\n Sint32x4 = 'sint32x4',\r\n Unorm10_10_10_2 = 'unorm10-10-10-2',\r\n Unorm8x4_bgra = 'unorm8x4-bgra'\r\n}\r\n\r\nfunction vertexFormatByteSize(format: VertexFormat): number {\r\n switch (format) {\r\n case VertexFormat.Uint8:\r\n case VertexFormat.Sint8:\r\n case VertexFormat.Unorm8:\r\n case VertexFormat.Snorm8:\r\n return 1;\r\n case VertexFormat.Uint8x2:\r\n case VertexFormat.Sint8x2:\r\n case VertexFormat.Unorm8x2:\r\n case VertexFormat.Snorm8x2:\r\n case VertexFormat.Uint16:\r\n case VertexFormat.Sint16:\r\n case VertexFormat.Unorm16:\r\n case VertexFormat.Snorm16:\r\n case VertexFormat.Float16:\r\n return 2;\r\n case VertexFormat.Uint8x4:\r\n case VertexFormat.Sint8x4:\r\n case VertexFormat.Unorm8x4:\r\n case VertexFormat.Snorm8x4:\r\n case VertexFormat.Uint16x2:\r\n case VertexFormat.Sint16x2:\r\n case VertexFormat.Unorm16x2:\r\n case VertexFormat.Snorm16x2:\r\n case VertexFormat.Float16x2:\r\n case VertexFormat.Float32:\r\n case VertexFormat.Uint32:\r\n case VertexFormat.Sint32:\r\n case VertexFormat.Unorm10_10_10_2:\r\n case VertexFormat.Unorm8x4_bgra:\r\n return 4;\r\n case VertexFormat.Uint16x4:\r\n case VertexFormat.Sint16x4:\r\n case VertexFormat.Unorm16x4:\r\n case VertexFormat.Snorm16x4:\r\n case VertexFormat.Float16x4:\r\n case VertexFormat.Float32x2:\r\n case VertexFormat.Uint32x2:\r\n case VertexFormat.Sint32x2:\r\n return 8;\r\n case VertexFormat.Float32x3:\r\n case VertexFormat.Uint32x3:\r\n case VertexFormat.Sint32x3:\r\n return 12;\r\n\r\n case VertexFormat.Float32x4:\r\n case VertexFormat.Uint32x4:\r\n case VertexFormat.Sint32x4:\r\n return 16;\r\n }\r\n}\r\n\r\nexport type VertexAttribute = {\r\n name: string;\r\n format: VertexFormat;\r\n offset: number;\r\n}\r\n\r\nclass VertexStream {\r\n stride: number = 0;\r\n attributes: VertexAttribute[] = [];\r\n buffer?: Buffer;\r\n\r\n pushAttribute(name: string, format: VertexFormat) {\r\n\r\n this.attributes.push({name, format, offset: this.stride});\r\n this.stride += vertexFormatByteSize(format);\r\n }\r\n}\r\n\r\nexport class MeshBuilder {\r\n private _topology: MeshTopology = MeshTopology.TriangleList;\r\n private _frontFace: FrontFace = FrontFace.CounterClockwise;\r\n private _streams: VertexStream[] = [new VertexStream()];\r\n private _ctx: WebGPUContext;\r\n private _indexBuffer?: IndexBuffer;\r\n private _numVertices: number = 0;\r\n\r\n constructor(ctx: WebGPUContext) {\r\n this._ctx = ctx;\r\n }\r\n\r\n pushStream(data: ArrayBufferLike, keepOnCPU: boolean = false): this {\r\n const stream = this._streams[this._streams.length - 1];\r\n stream.buffer = new BufferBuilder(this._ctx)\r\n .withData(data, keepOnCPU)\r\n .withUsage(BufferUsage.Vertex | BufferUsage.CopyDst)\r\n .build();\r\n\r\n const numVertices = data.byteLength / stream.stride;\r\n console.assert(this._streams.length == 0 || numVertices == this._numVertices, \"All vertex buffers must have the same number of vertices.\")\r\n this._numVertices = numVertices;\r\n\r\n this._streams.push(new VertexStream());\r\n return this;\r\n }\r\n\r\n pushAttribute(name: string, format: VertexFormat): this {\r\n const streamIndex = this._streams.length - 1;\r\n const stream = this._streams[streamIndex];\r\n stream.pushAttribute(name, format);\r\n return this;\r\n }\r\n\r\n withFrontFace(value: FrontFace): this {\r\n this._frontFace = value;\r\n return this;\r\n }\r\n\r\n withTopology(value: MeshTopology): this {\r\n this._topology = value;\r\n return this;\r\n }\r\n\r\n withIndexData(data: Uint16Array | Uint32Array, keepOnCPU: boolean = false): this {\r\n this._indexBuffer = mapUndefined(data, data => new IndexBuffer(data, this._ctx, keepOnCPU));\r\n return this;\r\n }\r\n\r\n build(): Mesh {\r\n // remove the last stream, it's empty\r\n this._streams.pop();\r\n\r\n return new Mesh(\r\n this._topology,\r\n this._frontFace,\r\n this._numVertices,\r\n this._streams,\r\n this._indexBuffer,\r\n );\r\n }\r\n}\r\n\r\nexport class Mesh {\r\n private _topology: MeshTopology;\r\n private _frontFace: FrontFace;\r\n private _streams: VertexStream[] = [];\r\n private _indexBuffer?: IndexBuffer;\r\n private _numVertices: number;\r\n\r\n /**\r\n * @internal\r\n */\r\n constructor(topology: MeshTopology, frontFace: FrontFace, numVertices: number, streams: VertexStream[], indexBuffer?: IndexBuffer) {\r\n this._topology = topology;\r\n this._frontFace = frontFace;\r\n this._streams = streams;\r\n this._numVertices = numVertices;\r\n this._indexBuffer = indexBuffer;\r\n }\r\n\r\n get topology(): MeshTopology {\r\n return this._topology;\r\n }\r\n\r\n get frontFace(): FrontFace {\r\n return this._frontFace;\r\n }\r\n\r\n get numStreams() {\r\n return this._streams.length;\r\n }\r\n\r\n get numVertices() {\r\n return this._numVertices;\r\n }\r\n\r\n getVertexBuffer(streamIndex: number): Buffer {\r\n return this._streams[streamIndex].buffer!;\r\n }\r\n\r\n getStreamStride(streamIndex: number): number {\r\n return this._streams[streamIndex].stride;\r\n }\r\n\r\n getStreamAttributes(streamIndex: number): VertexAttribute[] {\r\n return this._streams[streamIndex].attributes;\r\n }\r\n\r\n get indexBuffer(): Buffer | undefined {\r\n return this._indexBuffer?.buffer;\r\n }\r\n\r\n get indexFormat(): IndexFormat {\r\n return this._indexBuffer?.format ?? IndexFormat.Uint16;\r\n }\r\n\r\n get numIndices(): number {\r\n return this._indexBuffer?.count ?? 0;\r\n }\r\n}"],"names":["CullMode","IndexFormat","MeshTopology","FrontFace","VertexFormat","BufferUsage","GPUBufferUsage","MAP_READ","MAP_WRITE","COPY_SRC","COPY_DST","INDEX","VERTEX","UNIFORM","STORAGE","INDIRECT","QUERY_RESOLVE"],"ignoreList":[],"sourceRoot":""}
|