@ghostty-webgpu/shaders 0.0.0-stage → 0.0.3

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.
@@ -0,0 +1,131 @@
1
+ import type { ShaderUniformField, ShaderVector3, ShaderVector4 } from './types.js';
2
+ declare const UNIFORM_DEFINITIONS: readonly [{
3
+ readonly name: "iResolution";
4
+ readonly type: "vec3";
5
+ readonly count: 1;
6
+ }, {
7
+ readonly name: "iTime";
8
+ readonly type: "float";
9
+ readonly count: 1;
10
+ }, {
11
+ readonly name: "iTimeDelta";
12
+ readonly type: "float";
13
+ readonly count: 1;
14
+ }, {
15
+ readonly name: "iFrameRate";
16
+ readonly type: "float";
17
+ readonly count: 1;
18
+ }, {
19
+ readonly name: "iFrame";
20
+ readonly type: "int";
21
+ readonly count: 1;
22
+ }, {
23
+ readonly name: "iChannelTime";
24
+ readonly type: "float";
25
+ readonly count: 4;
26
+ }, {
27
+ readonly name: "iChannelResolution";
28
+ readonly type: "vec3";
29
+ readonly count: 4;
30
+ }, {
31
+ readonly name: "iMouse";
32
+ readonly type: "vec4";
33
+ readonly count: 1;
34
+ }, {
35
+ readonly name: "iDate";
36
+ readonly type: "vec4";
37
+ readonly count: 1;
38
+ }, {
39
+ readonly name: "iSampleRate";
40
+ readonly type: "float";
41
+ readonly count: 1;
42
+ }, {
43
+ readonly name: "iCurrentCursor";
44
+ readonly type: "vec4";
45
+ readonly count: 1;
46
+ }, {
47
+ readonly name: "iPreviousCursor";
48
+ readonly type: "vec4";
49
+ readonly count: 1;
50
+ }, {
51
+ readonly name: "iCurrentCursorColor";
52
+ readonly type: "vec4";
53
+ readonly count: 1;
54
+ }, {
55
+ readonly name: "iPreviousCursorColor";
56
+ readonly type: "vec4";
57
+ readonly count: 1;
58
+ }, {
59
+ readonly name: "iCurrentCursorStyle";
60
+ readonly type: "int";
61
+ readonly count: 1;
62
+ }, {
63
+ readonly name: "iPreviousCursorStyle";
64
+ readonly type: "int";
65
+ readonly count: 1;
66
+ }, {
67
+ readonly name: "iCursorVisible";
68
+ readonly type: "int";
69
+ readonly count: 1;
70
+ }, {
71
+ readonly name: "iTimeCursorChange";
72
+ readonly type: "float";
73
+ readonly count: 1;
74
+ }, {
75
+ readonly name: "iTimeFocus";
76
+ readonly type: "float";
77
+ readonly count: 1;
78
+ }, {
79
+ readonly name: "iFocus";
80
+ readonly type: "int";
81
+ readonly count: 1;
82
+ }, {
83
+ readonly name: "iPalette";
84
+ readonly type: "vec3";
85
+ readonly count: 256;
86
+ }, {
87
+ readonly name: "iBackgroundColor";
88
+ readonly type: "vec3";
89
+ readonly count: 1;
90
+ }, {
91
+ readonly name: "iForegroundColor";
92
+ readonly type: "vec3";
93
+ readonly count: 1;
94
+ }, {
95
+ readonly name: "iCursorColor";
96
+ readonly type: "vec3";
97
+ readonly count: 1;
98
+ }, {
99
+ readonly name: "iCursorText";
100
+ readonly type: "vec3";
101
+ readonly count: 1;
102
+ }, {
103
+ readonly name: "iSelectionForegroundColor";
104
+ readonly type: "vec3";
105
+ readonly count: 1;
106
+ }, {
107
+ readonly name: "iSelectionBackgroundColor";
108
+ readonly type: "vec3";
109
+ readonly count: 1;
110
+ }];
111
+ interface UniformTypeValues {
112
+ readonly float: number;
113
+ readonly int: number;
114
+ readonly vec3: ShaderVector3;
115
+ readonly vec4: ShaderVector4;
116
+ }
117
+ interface UniformCountValues<T> {
118
+ readonly 1: T;
119
+ readonly 4: readonly [T, T, T, T];
120
+ readonly 256: readonly T[];
121
+ }
122
+ type UniformValue<T extends (typeof UNIFORM_DEFINITIONS)[number]> = UniformCountValues<UniformTypeValues[T['type']]>[T['count']];
123
+ export type ShaderUniformValues = {
124
+ readonly [Field in (typeof UNIFORM_DEFINITIONS)[number] as Field['name']]: UniformValue<Field>;
125
+ };
126
+ export declare const SHADER_UNIFORM_FIELDS: readonly ShaderUniformField[];
127
+ export declare const SHADER_UNIFORM_BYTE_LENGTH: number;
128
+ /** Packs the shared std140 ABI. Omitted values are zero; iResolution.z defaults to one. */
129
+ export declare function packShaderUniforms(values?: Partial<ShaderUniformValues>, output?: ArrayBuffer): ArrayBuffer;
130
+ export {};
131
+ //# sourceMappingURL=uniforms.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uniforms.d.ts","sourceRoot":"","sources":["../src/uniforms.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAclF,QAAA,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4BvB,CAAA;AAEF,UAAU,iBAAiB;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;IAC5B,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;CAC7B;AAED,UAAU,kBAAkB,CAAC,CAAC;IAC5B,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAA;IACb,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;IACjC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,CAAA;CAC3B;AAED,KAAK,YAAY,CAAC,CAAC,SAAS,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,IAAI,kBAAkB,CACpF,iBAAiB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAC7B,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;AAEb,MAAM,MAAM,mBAAmB,GAAG;IAChC,QAAQ,EAAE,KAAK,IAAI,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC;CAC/F,CAAA;AAuBD,eAAO,MAAM,qBAAqB,+BAAkB,CAAA;AAUpD,eAAO,MAAM,0BAA0B,QAAsB,CAAA;AA4D7D,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAChC,MAAM,GAAE,OAAO,CAAC,mBAAmB,CAAM,EACzC,MAAM,GAAE,WAAyD,GAChE,WAAW,CAgBb"}
@@ -0,0 +1,127 @@
1
+ function defineUniforms(fields) {
2
+ return fields;
3
+ }
4
+ const UNIFORM_DEFINITIONS = defineUniforms([
5
+ { name: 'iResolution', type: 'vec3', count: 1 },
6
+ { name: 'iTime', type: 'float', count: 1 },
7
+ { name: 'iTimeDelta', type: 'float', count: 1 },
8
+ { name: 'iFrameRate', type: 'float', count: 1 },
9
+ { name: 'iFrame', type: 'int', count: 1 },
10
+ { name: 'iChannelTime', type: 'float', count: 4 },
11
+ { name: 'iChannelResolution', type: 'vec3', count: 4 },
12
+ { name: 'iMouse', type: 'vec4', count: 1 },
13
+ { name: 'iDate', type: 'vec4', count: 1 },
14
+ { name: 'iSampleRate', type: 'float', count: 1 },
15
+ { name: 'iCurrentCursor', type: 'vec4', count: 1 },
16
+ { name: 'iPreviousCursor', type: 'vec4', count: 1 },
17
+ { name: 'iCurrentCursorColor', type: 'vec4', count: 1 },
18
+ { name: 'iPreviousCursorColor', type: 'vec4', count: 1 },
19
+ { name: 'iCurrentCursorStyle', type: 'int', count: 1 },
20
+ { name: 'iPreviousCursorStyle', type: 'int', count: 1 },
21
+ { name: 'iCursorVisible', type: 'int', count: 1 },
22
+ { name: 'iTimeCursorChange', type: 'float', count: 1 },
23
+ { name: 'iTimeFocus', type: 'float', count: 1 },
24
+ { name: 'iFocus', type: 'int', count: 1 },
25
+ { name: 'iPalette', type: 'vec3', count: 256 },
26
+ { name: 'iBackgroundColor', type: 'vec3', count: 1 },
27
+ { name: 'iForegroundColor', type: 'vec3', count: 1 },
28
+ { name: 'iCursorColor', type: 'vec3', count: 1 },
29
+ { name: 'iCursorText', type: 'vec3', count: 1 },
30
+ { name: 'iSelectionForegroundColor', type: 'vec3', count: 1 },
31
+ { name: 'iSelectionBackgroundColor', type: 'vec3', count: 1 },
32
+ ]);
33
+ const COMPONENTS = { float: 1, int: 1, vec3: 3, vec4: 4 };
34
+ function align(offset, alignment) {
35
+ return Math.ceil(offset / alignment) * alignment;
36
+ }
37
+ function uniformFields() {
38
+ let end = 0;
39
+ return Object.freeze(UNIFORM_DEFINITIONS.map((definition) => {
40
+ const components = COMPONENTS[definition.type];
41
+ const array = definition.count > 1;
42
+ const alignment = array || components >= 3 ? 16 : 4;
43
+ const offset = align(end, alignment);
44
+ const stride = array ? 16 : components * 4;
45
+ end = offset + stride * definition.count;
46
+ return Object.freeze({ ...definition, offset, stride });
47
+ }));
48
+ }
49
+ export const SHADER_UNIFORM_FIELDS = uniformFields();
50
+ function uniformByteLength() {
51
+ let end = 0;
52
+ for (const field of SHADER_UNIFORM_FIELDS) {
53
+ end = Math.max(end, field.offset + field.stride * field.count);
54
+ }
55
+ return align(end, 16);
56
+ }
57
+ export const SHADER_UNIFORM_BYTE_LENGTH = uniformByteLength();
58
+ function checkedNumber(value, integer, name) {
59
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
60
+ throw new TypeError(`${name} must be a finite number`);
61
+ }
62
+ if (!integer && !Number.isFinite(Math.fround(value))) {
63
+ throw new RangeError(`${name} must fit in a 32-bit float`);
64
+ }
65
+ if (integer && (!Number.isInteger(value) || value < -2147483648 || value > 2147483647)) {
66
+ throw new RangeError(`${name} must be a signed 32-bit integer`);
67
+ }
68
+ return value;
69
+ }
70
+ function writeValue(view, field, value, index) {
71
+ const offset = field.offset + field.stride * index;
72
+ const components = COMPONENTS[field.type];
73
+ if (components === 1) {
74
+ const number = checkedNumber(value, field.type === 'int', field.name);
75
+ if (field.type === 'int')
76
+ view.setInt32(offset, number, true);
77
+ if (field.type === 'float')
78
+ view.setFloat32(offset, number, true);
79
+ return;
80
+ }
81
+ if (!Array.isArray(value) || value.length !== components) {
82
+ throw new TypeError(`${field.name} must contain ${components} numbers per vector`);
83
+ }
84
+ for (let component = 0; component < components; component += 1) {
85
+ const number = checkedNumber(value[component], false, field.name);
86
+ view.setFloat32(offset + component * 4, number, true);
87
+ }
88
+ }
89
+ function writeField(view, field, value) {
90
+ if (field.count === 1)
91
+ return writeValue(view, field, value, 0);
92
+ if (!Array.isArray(value) || value.length !== field.count) {
93
+ throw new TypeError(`${field.name} must contain ${field.count} entries`);
94
+ }
95
+ for (let index = 0; index < field.count; index += 1) {
96
+ writeValue(view, field, value[index], index);
97
+ }
98
+ }
99
+ const UNIFORM_NAMES = new Set(SHADER_UNIFORM_FIELDS.map((field) => field.name));
100
+ const DEFAULT_VALUES = { iResolution: [0, 0, 1] };
101
+ function validateValues(values) {
102
+ if (values === null || typeof values !== 'object' || Array.isArray(values)) {
103
+ throw new TypeError('Shader uniform values must be an object');
104
+ }
105
+ for (const name of Object.keys(values)) {
106
+ if (!UNIFORM_NAMES.has(name))
107
+ throw new TypeError(`Unknown shader uniform: ${name}`);
108
+ }
109
+ }
110
+ /** Packs the shared std140 ABI. Omitted values are zero; iResolution.z defaults to one. */
111
+ export function packShaderUniforms(values = {}, output = new ArrayBuffer(SHADER_UNIFORM_BYTE_LENGTH)) {
112
+ validateValues(values);
113
+ if (!(output instanceof ArrayBuffer) || output.byteLength !== SHADER_UNIFORM_BYTE_LENGTH) {
114
+ throw new RangeError(`Shader uniform output must be a ${SHADER_UNIFORM_BYTE_LENGTH}-byte ArrayBuffer`);
115
+ }
116
+ new Uint8Array(output).fill(0);
117
+ const view = new DataView(output);
118
+ for (const field of SHADER_UNIFORM_FIELDS) {
119
+ const value = Reflect.get(values, field.name);
120
+ const effectiveValue = value === undefined ? Reflect.get(DEFAULT_VALUES, field.name) : value;
121
+ if (effectiveValue === undefined)
122
+ continue;
123
+ writeField(view, field, effectiveValue);
124
+ }
125
+ return output;
126
+ }
127
+ //# source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AA0B,mBAAmB,CACjF,CAAA;IACH,CAAC;IACD,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAC9B,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAA;IACjC,KAAK,MAAM,KAAK,IAAI,qBAAqB,EAAE,CAAC;QAC1C,MAAM,KAAK,GAAY,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,CAAA;QACtD,MAAM,cAAc,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;QAC5F,IAAI,cAAc,KAAK,SAAS;YAAE,SAAQ;QAC1C,UAAU,CAAC,IAAI,EAAE,KAAK,EAAE,cAAc,CAAC,CAAA;IACzC,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -0,0 +1,5 @@
1
+ /** Draw three vertices. Channel textures use row zero as the logical bottom row. */
2
+ export declare const FULLSCREEN_VERTEX_WGSL = "struct VertexOutput {\n @builtin(position) position: vec4<f32>,\n @location(0) uv: vec2<f32>,\n}\n\n@vertex\nfn main(@builtin(vertex_index) index: u32) -> VertexOutput {\n let uv = vec2<f32>(f32((index << 1u) & 2u), f32(index & 2u));\n var output: VertexOutput;\n output.position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);\n output.uv = uv;\n return output;\n}\n";
3
+ /** Draw three vertices with no vertex buffers. Matches the WGSL vertex output. */
4
+ export declare const FULLSCREEN_VERTEX_GLSL = "#version 300 es\nprecision highp float;\nout vec2 _vs2fs_location0;\n\nvoid main() {\n vec2 uv = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));\n gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0);\n _vs2fs_location0 = uv;\n}\n";
5
+ //# sourceMappingURL=vertices.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vertices.d.ts","sourceRoot":"","sources":["../src/vertices.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,eAAO,MAAM,sBAAsB,kXAalC,CAAA;AAED,kFAAkF;AAClF,eAAO,MAAM,sBAAsB,mPASlC,CAAA"}
@@ -0,0 +1,27 @@
1
+ /** Draw three vertices. Channel textures use row zero as the logical bottom row. */
2
+ export const FULLSCREEN_VERTEX_WGSL = `struct VertexOutput {
3
+ @builtin(position) position: vec4<f32>,
4
+ @location(0) uv: vec2<f32>,
5
+ }
6
+
7
+ @vertex
8
+ fn main(@builtin(vertex_index) index: u32) -> VertexOutput {
9
+ let uv = vec2<f32>(f32((index << 1u) & 2u), f32(index & 2u));
10
+ var output: VertexOutput;
11
+ output.position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);
12
+ output.uv = uv;
13
+ return output;
14
+ }
15
+ `;
16
+ /** Draw three vertices with no vertex buffers. Matches the WGSL vertex output. */
17
+ export const FULLSCREEN_VERTEX_GLSL = `#version 300 es
18
+ precision highp float;
19
+ out vec2 _vs2fs_location0;
20
+
21
+ void main() {
22
+ vec2 uv = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
23
+ gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0);
24
+ _vs2fs_location0 = uv;
25
+ }
26
+ `;
27
+ //# sourceMappingURL=vertices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vertices.js","sourceRoot":"","sources":["../src/vertices.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,MAAM,CAAC,MAAM,sBAAsB,GAAG;;;;;;;;;;;;;CAarC,CAAA;AAED,kFAAkF;AAClF,MAAM,CAAC,MAAM,sBAAsB,GAAG;;;;;;;;;CASrC,CAAA"}
@@ -0,0 +1,61 @@
1
+ # Shader bindings
2
+
3
+ The addon accepts compiled fragment programs through `CompiledShader`. It supplies a fullscreen triangle with no vertex buffers. Fragment entry defaults to `main`; WGSL can override it with `entryPoint`. The fragment input is `@location(0) vec2<f32>` in WGSL or `in vec2 _vs2fs_location0` in GLSL. UV `(0, 0)` is the bottom-left of the terminal content. Fragment output is a color at location zero. Output is opaque.
4
+
5
+ ## Source texture
6
+
7
+ WebGPU group zero bindings are:
8
+
9
+ | Binding | Resource |
10
+ | ------- | ---------------------------------------- |
11
+ | 0 | Uniform buffer, 4496 bytes |
12
+ | 1 | Terminal `texture_2d<f32>`, RGBA8 |
13
+ | 2 | Filtering sampler, linear, clamp-to-edge |
14
+
15
+ Texture row zero is the logical bottom row on both backends. Sampling at UV `(0, 0)` reads the bottom-left terminal pixel. Direct WGSL position builtins still use WebGPU's native top-origin coordinates. The vertex UV is the shared bottom-origin coordinate.
16
+
17
+ A hand-authored compiled WGSL pass can use only the resources it needs:
18
+
19
+ ```ts
20
+ const compiled = {
21
+ wgsl: `
22
+ @group(0) @binding(1) var terminalTexture: texture_2d<f32>;
23
+ @group(0) @binding(2) var terminalSampler: sampler;
24
+ @fragment fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
25
+ return textureSample(terminalTexture, terminalSampler, uv);
26
+ }`,
27
+ }
28
+ ```
29
+
30
+ For GLSL ES 3.00, `webgl.uniformBlock` names the std140 block and `webgl.channel0` names its `sampler2D`. They default to `GhosttyUniforms` and `iChannel0`. Use `null` for a resource your program omits, for example `webgl: { uniformBlock: null, channel0: null }` for a constant-color fragment. Full compiler output carries its generated names. The sampler uses texture unit zero, and the uniform block uses buffer binding zero.
31
+
32
+ ## Uniform ABI
33
+
34
+ `SHADER_UNIFORM_FIELDS` exposes names, types, byte offsets, counts, and strides. `SHADER_UNIFORM_BYTE_LENGTH` is 4496. `packShaderUniforms(values, output?)` packs this little-endian std140 layout. Arrays use 16-byte strides, including scalar arrays. WGSL represents `iChannelTime` as four `vec4<f32>` values with each time in `.x`. Unused bytes and omitted values are zero, except `iResolution.z`, which defaults to one.
35
+
36
+ | Field | Type/count | Byte offset |
37
+ | --------------------------------------------------------- | -------------------- | ------------- |
38
+ | iResolution | vec3 | 0 |
39
+ | iTime, iTimeDelta, iFrameRate | float each | 12, 16, 20 |
40
+ | iFrame | int | 24 |
41
+ | iChannelTime | float[4], stride 16 | 32 |
42
+ | iChannelResolution | vec3[4], stride 16 | 96 |
43
+ | iMouse, iDate | vec4 each | 160, 176 |
44
+ | iSampleRate | float | 192 |
45
+ | iCurrentCursor, iPreviousCursor | vec4 each | 208, 224 |
46
+ | iCurrentCursorColor, iPreviousCursorColor | vec4 each | 240, 256 |
47
+ | iCurrentCursorStyle, iPreviousCursorStyle, iCursorVisible | int each | 272, 276, 280 |
48
+ | iTimeCursorChange, iTimeFocus | float each | 284, 288 |
49
+ | iFocus | int | 292 |
50
+ | iPalette | vec3[256], stride 16 | 304 |
51
+ | iBackgroundColor, iForegroundColor | vec3 each | 4400, 4416 |
52
+ | iCursorColor, iCursorText | vec3 each | 4432, 4448 |
53
+ | iSelectionForegroundColor, iSelectionBackgroundColor | vec3 each | 4464, 4480 |
54
+
55
+ Resolution uses backing pixels. Time is active elapsed seconds; delta is time since the last submitted effect frame. `iFrame` starts at zero and advances only on submission. Channel zero has the terminal dimensions/time; other channels and sample rate are zero. There are no additional channel textures.
56
+
57
+ Mouse `.xy` is the current bottom-origin backing-pixel position. `.zw` holds the press origin, positive while pressed and negative after release. Date is local year, zero-based month, day of month, and seconds since midnight.
58
+
59
+ Cursor rectangles are `(left, top, width, height)` in bottom-origin pixels. The previous rectangle/color/style changes when the current one changes. Styles are block `0`, bar `2`, underline `3`. Visibility reflects the last submitted terminal cursor paint. Focus is `1` when the terminal contains the active element. Cursor/focus timestamps use effect time. Theme colors and the 256-entry palette use normalized RGB; cursor colors include alpha one.
60
+
61
+ The uniform packing and bounded-frame scheduling are adapted from the MIT-licensed `ghostty-web-shaderc` runtime and showcase at revision `331a7ee`.
package/docs/usage.md ADDED
@@ -0,0 +1,46 @@
1
+ # Usage and lifecycle
2
+
3
+ Install the addon separately from `ghostty-webgpu`. Terminal core has no dependency on this package.
4
+
5
+ ```ts
6
+ import { createShaderAddon } from '@ghostty-webgpu/shaders'
7
+
8
+ const handle = terminal.use(
9
+ createShaderAddon(compiled, {
10
+ paused: false,
11
+ onError: (error) => console.error(error.code, error.message),
12
+ }),
13
+ )
14
+ await handle.api.ready
15
+ await handle.api.setShader(nextCompiled)
16
+ handle.api.setPaused(true)
17
+ handle.dispose()
18
+ ```
19
+
20
+ `compiled` is an object containing nonempty `wgsl`, `glsl`, or both. These are already compiled fragment programs. A full `CompiledGhosttyShader` object from `ghostty-web-shaderc` is structurally accepted. Partial output may omit `schemaVersion`, `uniformLayout`, `entryPoint`, and `webgl`. If supplied, the schema and layout must be `1` and `ghostty-v1`. See [bindings](bindings.md) for custom authoring.
21
+
22
+ Compile in your build or with a compiler you load yourself. This package neither resolves nor loads a compiler, parser, WASM, or Three.js. Its packed entry can be imported without a terminal runtime installed. The terminal peer supplies TypeScript types and the host extension lifecycle.
23
+
24
+ ## Backend selection
25
+
26
+ With both languages, the addon prefers WebGL2 when the terminal selected WebGL2, and WebGPU otherwise. If a WebGPU adapter/device is unavailable and GLSL is present, it uses WebGL2. WGSL-only output selects WebGPU; GLSL-only output selects WebGL2. Canvas2D source terminals work with either output backend.
27
+
28
+ `backend: 'webgpu' | 'webgl2'` makes selection strict. Missing language output or unavailable browser capability rejects `ready` with a `ShaderAddonError`. Backend selection happens once per installation. Replacements must supply that backend's language.
29
+
30
+ ## Installation and replacement
31
+
32
+ Installation works before or after `terminal.open()`. `ready` waits for opening, output device initialization, and a valid shader. It rejects if initialization fails or the addon is disposed first.
33
+
34
+ `setShader` resolves `true` when its program becomes current. A superseded request or disposal resolves `false`. Requests are serialized, and only the newest can become current. Invalid replacements reject while keeping the last valid program and pixels. An invalid initial program rejects `ready`, removes the addon canvas, and calls `onError`.
35
+
36
+ Main-thread WebGPU, WebGL2, and both Canvas2D modes are supported. A custom factory must forward the internal options to a built-in renderer. Independent renderers using only public options reject with code `backend`. DOM renderers reject with code `backend`; runtime fallback to DOM removes the overlay and reports the same error, including while paused. Worker terminals reject with code `worker`: their asynchronous snapshots arrive after the non-preserved source drawing buffer can clear. Use a main-thread terminal for this addon.
37
+
38
+ ## Capture and ownership
39
+
40
+ The addon inserts a `pointer-events: none`, `aria-hidden` overlay into `terminal.element`, before the terminal's input/composition elements. It follows the source content box, padding, backing dimensions, and DPR. Source canvas, focus, keyboard input, selection, and accessibility nodes stay owned by the terminal. Renderer recovery can replace the source canvas; the next submitted frame retargets capture and geometry.
41
+
42
+ A private synchronous image notification copies each genuine source draw into an owned transparent Canvas2D staging canvas. The replacing copy removes erased pixels, including transparent backgrounds and partial glyph alpha. Accepted publications that do not draw retain the previous snapshot, so WebGL2 buffers cleared after presentation do not blank the effect. The effect output remains opaque; staging is not flattened into the theme background. Installation calls the public `clearTextureAtlas()` once to guarantee a fresh draw even for an unchanged, already-presented terminal. The output then uploads that snapshot. This copy has a cost proportional to terminal pixel area; this package makes no hardware performance claim.
43
+
44
+ At most one output draw is submitted per animation frame and one unfinished effect frame is retained. WebGPU queue completion and WebGL2 fences gate uploads/draws. Busy output retains the latest snapshot and elapsed time. Pause and document visibility freeze effect time. Terminal changes can still redraw while paused; hidden documents resume with current terminal state.
45
+
46
+ `handle.dispose()` removes only the overlay and owned resources/listeners. It leaves the terminal usable. Terminal disposal also disposes the addon. Device/context loss rejects pending installation, removes the overlay, and calls `onError`; reinstall to create fresh output resources. Original terminal pixels remain underneath on disposal or failure.
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@ghostty-webgpu/shaders",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.3",
4
+ "description": "Optional compiled shader effects for ghostty-webgpu terminals",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/ShaulLavo/ghostty-webgpu.git",
9
+ "directory": "packages/shaders"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "docs",
14
+ "README.md",
15
+ "LICENSE"
16
+ ],
17
+ "type": "module",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ }
24
+ },
25
+ "publishConfig": {
26
+ "access": "public",
27
+ "provenance": true
28
+ },
29
+ "scripts": {
30
+ "build": "tsc -p tsconfig.build.json",
31
+ "typecheck": "tsc --noEmit",
32
+ "lint": "oxlint src scripts",
33
+ "format": "oxfmt --write .",
34
+ "format:check": "oxfmt --check .",
35
+ "test": "vitest run",
36
+ "test:browser": "bun run --cwd ../.. test:browser packages/shaders/src/tests/",
37
+ "test:package": "node scripts/package-smoke.mjs",
38
+ "verify": "bun run build && bun run typecheck && bun run lint && bun run format:check && bun run test && bun run test:package"
39
+ },
40
+ "devDependencies": {
41
+ "@webgpu/types": "0.1.74",
42
+ "oxfmt": "0.70.0",
43
+ "oxlint": "1.85.0",
44
+ "typescript": "7.0.2",
45
+ "vitest": "5.0.2"
46
+ },
47
+ "peerDependencies": {
48
+ "ghostty-webgpu": "^0.3.32"
49
+ },
50
+ "engines": {
51
+ "node": ">=20"
52
+ },
53
+ "packageManager": "bun@1.4.4"
54
+ }