@plasius/gpu-worker 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/CHANGELOG.md ADDED
@@ -0,0 +1,74 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on **[Keep a Changelog](https://keepachangelog.com/en/1.1.0/)**, and this project adheres to **[Semantic Versioning](https://semver.org/spec/v2.0.0.html)**.
6
+
7
+ ---
8
+
9
+ ## [Unreleased]
10
+
11
+ - **Added**
12
+ - (placeholder)
13
+
14
+ - **Changed**
15
+ - (placeholder)
16
+
17
+ - **Fixed**
18
+ - (placeholder)
19
+
20
+ - **Security**
21
+ - (placeholder)
22
+
23
+ ## [0.1.0] - 2026-01-22
24
+
25
+ - **Added**
26
+ - (placeholder)
27
+
28
+ - **Changed**
29
+ - (placeholder)
30
+
31
+ - **Fixed**
32
+ - (placeholder)
33
+
34
+ - **Security**
35
+ - (placeholder)
36
+
37
+ ## [0.1.0-beta.1] - 2026-01-22
38
+
39
+ - **Added**
40
+ - Unit tests for WGSL loading/assembly with coverage output for CI.
41
+
42
+ - **Changed**
43
+ - Build outputs now ship as ESM and CJS bundles with the WGSL asset in `dist/`.
44
+
45
+ - **Fixed**
46
+ - CJS builds no longer warn on `import.meta` when resolving `worker.wgsl`.
47
+
48
+ - **Security**
49
+ - (placeholder)
50
+
51
+ ## [0.1.0-beta.1]
52
+
53
+ - **Added**
54
+ - Initial beta release with lock-free GPU job queue integration.
55
+ - WGSL worker module and helper utilities.
56
+ - Ray tracing demo.
57
+
58
+ ---
59
+
60
+ ## Release process (maintainers)
61
+
62
+ 1. Update `CHANGELOG.md` under **Unreleased** with user-visible changes.
63
+ 2. Bump version in `package.json` following SemVer (major/minor/patch).
64
+ 3. Move entries from **Unreleased** to a new version section with the current date.
65
+ 4. Tag the release in Git (`vX.Y.Z`) and push tags.
66
+ 5. Publish to npm (via CI/CD or `npm publish`).
67
+
68
+ > Tip: Use Conventional Commits in PR titles/bodies to make changelog updates easier.
69
+
70
+ ---
71
+
72
+ [Unreleased]: https://github.com/Plasius-LTD/gpu-worker/compare/v0.1.0...HEAD
73
+ [0.1.0-beta.1]: https://github.com/Plasius-LTD/gpu-worker/releases/tag/v0.1.0-beta.1
74
+ [0.1.0]: https://github.com/Plasius-LTD/gpu-worker/releases/tag/v0.1.0
package/LICENSE ADDED
@@ -0,0 +1,203 @@
1
+ Copyright 2025 Plasius LTD
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
179
+
180
+ APPENDIX: How to apply the Apache License to your work.
181
+
182
+ To apply the Apache License to your work, attach the following
183
+ boilerplate notice, with the fields enclosed by brackets "[]"
184
+ replaced with your own identifying information. (Don't include
185
+ the brackets!) The text should be enclosed in the appropriate
186
+ comment syntax for the file format. We also recommend that a
187
+ file or class name and description of purpose be included on the
188
+ same "printed page" as the copyright notice for easier
189
+ identification within third-party archives.
190
+
191
+ Copyright [yyyy] [name of copyright owner]
192
+
193
+ Licensed under the Apache License, Version 2.0 (the "License");
194
+ you may not use this file except in compliance with the License.
195
+ You may obtain a copy of the License at
196
+
197
+ http://www.apache.org/licenses/LICENSE-2.0
198
+
199
+ Unless required by applicable law or agreed to in writing, software
200
+ distributed under the License is distributed on an "AS IS" BASIS,
201
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202
+ See the License for the specific language governing permissions and
203
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # @plasius/gpu-worker
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@plasius/gpu-worker)](https://www.npmjs.com/package/@plasius/gpu-worker)
4
+ [![CI](https://github.com/Plasius-LTD/gpu-worker/actions/workflows/ci.yml/badge.svg)](https://github.com/Plasius-LTD/gpu-worker/actions/workflows/ci.yml)
5
+ [![license](https://img.shields.io/github/license/Plasius-LTD/gpu-worker)](./LICENSE)
6
+
7
+ A WebGPU worker runtime that builds on `@plasius/gpu-lock-free-queue` to schedule WGSL workloads like ray tracing, physics, and acoustics.
8
+
9
+ Apache-2.0. ESM + CJS builds. WGSL assets are published in `dist/`.
10
+
11
+ ## Install
12
+ ```
13
+ npm install @plasius/gpu-worker
14
+ ```
15
+
16
+ ## Usage
17
+ ```js
18
+ import { assembleWorkerWgsl, loadWorkerWgsl } from "@plasius/gpu-worker";
19
+
20
+ const workerWgsl = await loadWorkerWgsl();
21
+ const shaderCode = await assembleWorkerWgsl(workerWgsl);
22
+ // Pass shaderCode to device.createShaderModule({ code: shaderCode })
23
+ ```
24
+
25
+ ## What this is
26
+ - A minimal GPU worker layer that combines a lock-free queue with user WGSL jobs.
27
+ - A helper to assemble WGSL modules with queue helpers included.
28
+ - A reference job format for fixed-size job dispatch (u32 indices).
29
+
30
+ ## Demo
31
+ The demo enqueues ray tracing tile jobs on the GPU and renders a simple scene. Install
32
+ dependencies first so the lock-free queue package is available for the browser import map.
33
+
34
+ ```
35
+ npm install
36
+ python3 -m http.server
37
+ ```
38
+
39
+ Then open `http://localhost:8000/demo/`.
40
+
41
+ ## Build Outputs
42
+
43
+ `npm run build` emits `dist/index.js`, `dist/index.cjs`, and `dist/worker.wgsl`.
44
+
45
+ ## Files
46
+ - `demo/index.html`: Loads the ray tracing demo.
47
+ - `demo/main.js`: WebGPU setup, enqueue, and ray tracing kernel.
48
+ - `src/worker.wgsl`: Worker entry points that dequeue jobs and run a ray tracer.
49
+ - `src/index.js`: Helper functions to load/assemble WGSL.
50
+
51
+ ## Job shape
52
+ Jobs are `u32` indices into a fixed workload array (tiles, particles, etc). Keep job data fixed-size; use indices into a separate payload buffer for variable payloads.
package/dist/index.cjs ADDED
@@ -0,0 +1,56 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.js
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ assembleWorkerWgsl: () => assembleWorkerWgsl,
23
+ loadWorkerWgsl: () => loadWorkerWgsl,
24
+ workerWgslUrl: () => workerWgslUrl
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_gpu_lock_free_queue = require("@plasius/gpu-lock-free-queue");
28
+ var workerWgslUrl = (() => {
29
+ if (false) {
30
+ return new URL("./worker.wgsl", void 0);
31
+ }
32
+ if (typeof __filename !== "undefined" && typeof require !== "undefined") {
33
+ const { pathToFileURL } = require("url");
34
+ return new URL("./worker.wgsl", pathToFileURL(__filename));
35
+ }
36
+ const base = typeof process !== "undefined" && process.cwd ? `file://${process.cwd()}/` : "file:///";
37
+ return new URL("./worker.wgsl", base);
38
+ })();
39
+ async function loadWorkerWgsl() {
40
+ const response = await fetch(workerWgslUrl);
41
+ return response.text();
42
+ }
43
+ async function assembleWorkerWgsl(workerWgsl) {
44
+ const queueWgsl = await (0, import_gpu_lock_free_queue.loadQueueWgsl)();
45
+ const body = workerWgsl ?? await loadWorkerWgsl();
46
+ return `${queueWgsl}
47
+
48
+ ${body}`;
49
+ }
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ assembleWorkerWgsl,
53
+ loadWorkerWgsl,
54
+ workerWgslUrl
55
+ });
56
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.js"],"sourcesContent":["import { loadQueueWgsl } from \"@plasius/gpu-lock-free-queue\";\n\nexport const workerWgslUrl = (() => {\n if (typeof __IMPORT_META_URL__ !== \"undefined\") {\n return new URL(\"./worker.wgsl\", __IMPORT_META_URL__);\n }\n if (typeof __filename !== \"undefined\" && typeof require !== \"undefined\") {\n const { pathToFileURL } = require(\"node:url\");\n return new URL(\"./worker.wgsl\", pathToFileURL(__filename));\n }\n const base =\n typeof process !== \"undefined\" && process.cwd\n ? `file://${process.cwd()}/`\n : \"file:///\";\n return new URL(\"./worker.wgsl\", base);\n})();\n\nexport async function loadWorkerWgsl() {\n const response = await fetch(workerWgslUrl);\n return response.text();\n}\n\nexport async function assembleWorkerWgsl(workerWgsl) {\n const queueWgsl = await loadQueueWgsl();\n const body = workerWgsl ?? (await loadWorkerWgsl());\n return `${queueWgsl}\\n\\n${body}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iCAA8B;AAEvB,IAAM,iBAAiB,MAAM;AAClC,MAAI,OAA4C;AAC9C,WAAO,IAAI,IAAI,iBAAiB,MAAmB;AAAA,EACrD;AACA,MAAI,OAAO,eAAe,eAAe,OAAO,YAAY,aAAa;AACvE,UAAM,EAAE,cAAc,IAAI,QAAQ,KAAU;AAC5C,WAAO,IAAI,IAAI,iBAAiB,cAAc,UAAU,CAAC;AAAA,EAC3D;AACA,QAAM,OACJ,OAAO,YAAY,eAAe,QAAQ,MACtC,UAAU,QAAQ,IAAI,CAAC,MACvB;AACN,SAAO,IAAI,IAAI,iBAAiB,IAAI;AACtC,GAAG;AAEH,eAAsB,iBAAiB;AACrC,QAAM,WAAW,MAAM,MAAM,aAAa;AAC1C,SAAO,SAAS,KAAK;AACvB;AAEA,eAAsB,mBAAmB,YAAY;AACnD,QAAM,YAAY,UAAM,0CAAc;AACtC,QAAM,OAAO,cAAe,MAAM,eAAe;AACjD,SAAO,GAAG,SAAS;AAAA;AAAA,EAAO,IAAI;AAChC;","names":[]}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/index.js
9
+ import { loadQueueWgsl } from "@plasius/gpu-lock-free-queue";
10
+ var workerWgslUrl = (() => {
11
+ if (typeof import.meta.url !== "undefined") {
12
+ return new URL("./worker.wgsl", import.meta.url);
13
+ }
14
+ if (typeof __filename !== "undefined" && typeof __require !== "undefined") {
15
+ const { pathToFileURL } = __require("url");
16
+ return new URL("./worker.wgsl", pathToFileURL(__filename));
17
+ }
18
+ const base = typeof process !== "undefined" && process.cwd ? `file://${process.cwd()}/` : "file:///";
19
+ return new URL("./worker.wgsl", base);
20
+ })();
21
+ async function loadWorkerWgsl() {
22
+ const response = await fetch(workerWgslUrl);
23
+ return response.text();
24
+ }
25
+ async function assembleWorkerWgsl(workerWgsl) {
26
+ const queueWgsl = await loadQueueWgsl();
27
+ const body = workerWgsl ?? await loadWorkerWgsl();
28
+ return `${queueWgsl}
29
+
30
+ ${body}`;
31
+ }
32
+ export {
33
+ assembleWorkerWgsl,
34
+ loadWorkerWgsl,
35
+ workerWgslUrl
36
+ };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.js"],"sourcesContent":["import { loadQueueWgsl } from \"@plasius/gpu-lock-free-queue\";\n\nexport const workerWgslUrl = (() => {\n if (typeof __IMPORT_META_URL__ !== \"undefined\") {\n return new URL(\"./worker.wgsl\", __IMPORT_META_URL__);\n }\n if (typeof __filename !== \"undefined\" && typeof require !== \"undefined\") {\n const { pathToFileURL } = require(\"node:url\");\n return new URL(\"./worker.wgsl\", pathToFileURL(__filename));\n }\n const base =\n typeof process !== \"undefined\" && process.cwd\n ? `file://${process.cwd()}/`\n : \"file:///\";\n return new URL(\"./worker.wgsl\", base);\n})();\n\nexport async function loadWorkerWgsl() {\n const response = await fetch(workerWgslUrl);\n return response.text();\n}\n\nexport async function assembleWorkerWgsl(workerWgsl) {\n const queueWgsl = await loadQueueWgsl();\n const body = workerWgsl ?? (await loadWorkerWgsl());\n return `${queueWgsl}\\n\\n${body}`;\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,qBAAqB;AAEvB,IAAM,iBAAiB,MAAM;AAClC,MAAI,OAAO,oBAAwB,aAAa;AAC9C,WAAO,IAAI,IAAI,iBAAiB,eAAmB;AAAA,EACrD;AACA,MAAI,OAAO,eAAe,eAAe,OAAO,cAAY,aAAa;AACvE,UAAM,EAAE,cAAc,IAAI,UAAQ,KAAU;AAC5C,WAAO,IAAI,IAAI,iBAAiB,cAAc,UAAU,CAAC;AAAA,EAC3D;AACA,QAAM,OACJ,OAAO,YAAY,eAAe,QAAQ,MACtC,UAAU,QAAQ,IAAI,CAAC,MACvB;AACN,SAAO,IAAI,IAAI,iBAAiB,IAAI;AACtC,GAAG;AAEH,eAAsB,iBAAiB;AACrC,QAAM,WAAW,MAAM,MAAM,aAAa;AAC1C,SAAO,SAAS,KAAK;AACvB;AAEA,eAAsB,mBAAmB,YAAY;AACnD,QAAM,YAAY,MAAM,cAAc;AACtC,QAAM,OAAO,cAAe,MAAM,eAAe;AACjD,SAAO,GAAG,SAAS;AAAA;AAAA,EAAO,IAAI;AAChC;","names":[]}
@@ -0,0 +1,134 @@
1
+ struct RenderParams {
2
+ width: u32,
3
+ height: u32,
4
+ tile_size: u32,
5
+ _pad0: u32,
6
+ camera_pos: vec4<f32>,
7
+ camera_target: vec4<f32>,
8
+ fov_y: f32,
9
+ aspect: f32,
10
+ time: f32,
11
+ _pad1: f32,
12
+ };
13
+
14
+ @group(1) @binding(0) var<storage, read_write> framebuffer: array<u32>;
15
+ @group(1) @binding(1) var<uniform> render: RenderParams;
16
+
17
+ fn hit_sphere(center: vec3<f32>, radius: f32, origin: vec3<f32>, dir: vec3<f32>) -> f32 {
18
+ let oc = origin - center;
19
+ let a = dot(dir, dir);
20
+ let b = 2.0 * dot(oc, dir);
21
+ let c = dot(oc, oc) - radius * radius;
22
+ let disc = b * b - 4.0 * a * c;
23
+ if (disc < 0.0) {
24
+ return -1.0;
25
+ }
26
+ let sq = sqrt(disc);
27
+ let t0 = (-b - sq) / (2.0 * a);
28
+ if (t0 > 0.001) {
29
+ return t0;
30
+ }
31
+ let t1 = (-b + sq) / (2.0 * a);
32
+ if (t1 > 0.001) {
33
+ return t1;
34
+ }
35
+ return -1.0;
36
+ }
37
+
38
+ fn background(dir: vec3<f32>) -> vec3<f32> {
39
+ let t = 0.5 * (dir.y + 1.0);
40
+ return mix(vec3<f32>(0.04, 0.05, 0.08), vec3<f32>(0.65, 0.78, 0.92), t);
41
+ }
42
+
43
+ fn shade_point(pos: vec3<f32>, normal: vec3<f32>, base: vec3<f32>) -> vec3<f32> {
44
+ let light_dir = normalize(vec3<f32>(0.6 + 0.2 * sin(render.time), 0.9, 0.3));
45
+ let diff = max(dot(normal, light_dir), 0.0);
46
+ let ambient = 0.2;
47
+ return base * (ambient + diff * 0.8);
48
+ }
49
+
50
+ fn pack_color(color: vec3<f32>) -> u32 {
51
+ let c = clamp(color, vec3<f32>(0.0), vec3<f32>(1.0));
52
+ let r = u32(round(c.x * 255.0));
53
+ let g = u32(round(c.y * 255.0));
54
+ let b = u32(round(c.z * 255.0));
55
+ return (255u << 24) | (b << 16) | (g << 8) | r;
56
+ }
57
+
58
+ @compute @workgroup_size(64)
59
+ fn raytrace_main(@builtin(global_invocation_id) gid: vec3<u32>) {
60
+ let idx = gid.x;
61
+ if (idx >= params.job_count) {
62
+ return;
63
+ }
64
+
65
+ let ok = dequeue(idx);
66
+ if (ok == 0u) {
67
+ return;
68
+ }
69
+
70
+ let job = output_jobs[idx];
71
+ let tiles_x = (render.width + render.tile_size - 1u) / render.tile_size;
72
+ let tile_x = job % tiles_x;
73
+ let tile_y = job / tiles_x;
74
+ let start_x = tile_x * render.tile_size;
75
+ let start_y = tile_y * render.tile_size;
76
+
77
+ let forward = normalize(render.camera_target.xyz - render.camera_pos.xyz);
78
+ let right = normalize(cross(forward, vec3<f32>(0.0, 1.0, 0.0)));
79
+ let up = normalize(cross(right, forward));
80
+ let tan_half = tan(0.5 * render.fov_y);
81
+
82
+ for (var y: u32 = 0u; y < render.tile_size; y = y + 1u) {
83
+ let py = start_y + y;
84
+ if (py >= render.height) {
85
+ continue;
86
+ }
87
+ for (var x: u32 = 0u; x < render.tile_size; x = x + 1u) {
88
+ let px = start_x + x;
89
+ if (px >= render.width) {
90
+ continue;
91
+ }
92
+
93
+ let u = (f32(px) + 0.5) / f32(render.width);
94
+ let v = (f32(py) + 0.5) / f32(render.height);
95
+ let ndc = vec2<f32>(u * 2.0 - 1.0, 1.0 - v * 2.0);
96
+ let dir = normalize(forward + ndc.x * render.aspect * tan_half * right + ndc.y * tan_half * up);
97
+
98
+ let origin = render.camera_pos.xyz;
99
+ var color = background(dir);
100
+
101
+ let t1 = hit_sphere(vec3<f32>(0.0, 1.0, 0.0), 1.0, origin, dir);
102
+ let t2 = hit_sphere(vec3<f32>(-1.6, 0.6, -0.5), 0.6, origin, dir);
103
+ let t3 = hit_sphere(vec3<f32>(0.0, -1000.0, 0.0), 999.0, origin, dir);
104
+
105
+ var t_hit = -1.0;
106
+ var base = vec3<f32>(0.0);
107
+ var center = vec3<f32>(0.0);
108
+ if (t1 > 0.0) {
109
+ t_hit = t1;
110
+ base = vec3<f32>(0.86, 0.42, 0.32);
111
+ center = vec3<f32>(0.0, 1.0, 0.0);
112
+ }
113
+ if (t2 > 0.0 && (t_hit < 0.0 || t2 < t_hit)) {
114
+ t_hit = t2;
115
+ base = vec3<f32>(0.2, 0.7, 0.9);
116
+ center = vec3<f32>(-1.6, 0.6, -0.5);
117
+ }
118
+ if (t3 > 0.0 && (t_hit < 0.0 || t3 < t_hit)) {
119
+ t_hit = t3;
120
+ base = vec3<f32>(0.32, 0.3, 0.26);
121
+ center = vec3<f32>(0.0, -1000.0, 0.0);
122
+ }
123
+
124
+ if (t_hit > 0.0) {
125
+ let pos = origin + t_hit * dir;
126
+ let normal = normalize(pos - center);
127
+ color = shade_point(pos, normal, base);
128
+ }
129
+
130
+ let pixel_index = py * render.width + px;
131
+ framebuffer[pixel_index] = pack_color(color);
132
+ }
133
+ }
134
+ }
@@ -0,0 +1,2 @@
1
+ type,github_handle,full_name,company,email,date,approver
2
+ corporate,Zephod111r,Phillip Hounslow,Plasius LTD,zephod@plasius.co.uk,2025-09-12,Phillip Hounslow (Maintainer)
package/legal/CLA.md ADDED
@@ -0,0 +1,22 @@
1
+ # Contributor License Agreements (CLA)
2
+
3
+ To protect the intellectual property of this project and ensure clarity of rights, all contributors must sign a Contributor License Agreement (CLA) before their first contribution.
4
+
5
+ ## Which CLA should I sign?
6
+
7
+ - **Individual CLA**: If you are contributing personally and not on behalf of an employer, sign the [Individual CLA](INDIVIDUAL_CLA.md).
8
+ - **Corporate CLA**: If you are contributing as part of your work for a company, the company should sign the [Corporate CLA](CORPORATE_CLA.md).
9
+
10
+ ## How to sign
11
+
12
+ 1. Download the appropriate CLA file (Individual or Corporate).
13
+ 2. Fill in the required details, sign, and date it.
14
+ 3. Email a PDF copy of the signed document to **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)** with subject: `CLA – Individual` or `CLA – Corporate`.
15
+
16
+ ## Registry
17
+
18
+ All signed CLAs are logged internally in the CLA registry (`CLA-REGISTRY.csv`).
19
+
20
+ ## Questions?
21
+
22
+ If you have any questions about which CLA to sign or how the process works, please email **[contributors@plasius.co.uk](mailtocontributors@plasius.co.uk)**.
@@ -0,0 +1,57 @@
1
+ # Corporate Contributor License Agreement (CLA)
2
+
3
+ ## Purpose
4
+
5
+ This Corporate Contributor License Agreement ("Agreement") is intended to protect the intellectual property rights of the contributors and the project, ensure clear licensing terms for contributions, and maintain trust within the community. By signing this Agreement, the corporation agrees to the terms that facilitate the use, distribution, and modification of contributions under the project's licensing framework.
6
+
7
+ ## Agreement
8
+
9
+ 1. **Representation of Authority**
10
+ The undersigned individual represents and warrants that they have the full legal authority to enter into this Agreement on behalf of the corporation named below ("Corporation") and to grant the rights contained herein.
11
+
12
+ 2. **Grant of Copyright License**
13
+ The Corporation hereby grants to the project maintainers and users a perpetual, worldwide, non-exclusive, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute the contributions submitted to the project.
14
+
15
+ 3. **Grant of Patent License**
16
+ The Corporation hereby grants to the project maintainers and users a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under any patent claims that are necessarily infringed by the contributions to make, use, sell, offer for sale, import, and otherwise dispose of the contributions or derivative works thereof.
17
+
18
+ 4. **Warranties and Representations**
19
+ The Corporation represents and warrants that:
20
+
21
+ - The contributions are the original work of the Corporation or that the Corporation has sufficient rights to grant the licenses herein.
22
+ - The submission of the contributions does not violate any agreements or rights of third parties.
23
+
24
+ 5. **No Revocation**
25
+ This license is granted on a perpetual basis and cannot be revoked, provided that the terms of this Agreement are met.
26
+
27
+ 6. **Governing Law**
28
+ This Agreement shall be governed by and construed in accordance with the laws of the United Kingdom, without regard to its conflict of laws principles.
29
+
30
+ 7. **Execution**
31
+
32
+ This Agreement is effective upon signature by the authorized representative of the Corporation. Please sign and date this document, then email a scanned PDF copy to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
33
+
34
+ ---
35
+
36
+ ### **@plasius/gpu-worker**
37
+
38
+ **Corporation Legal Name:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
39
+
40
+ **Authorized Representative:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
41
+
42
+ **Title:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
43
+
44
+ **Email:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
45
+
46
+ **Date:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
47
+
48
+ **Signature:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
49
+
50
+ ---
51
+
52
+ ## How to Sign
53
+
54
+ - Download this file as a template.
55
+ - Fill in the Corporation’s legal name, authorized representative, title, email, date, and provide a signature.
56
+ - Sign and date the document.
57
+ - Send a scanned copy of the signed Agreement to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
@@ -0,0 +1,91 @@
1
+ # Individual Contributor License Agreement (CLA)
2
+
3
+ **Project:** @plasius/gpu-worker (Plasius LTD)
4
+ **Version:** 1.0 — 2025‑09‑12
5
+ **Contact:** [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)
6
+
7
+ ---
8
+
9
+ ## 1. Definitions
10
+
11
+ - **"You"** (or **"Contributor"**) means the individual signing this CLA and submitting Contributions to the Project.
12
+ - **"Contribution"** means any original work of authorship, including code, documentation, data, designs, or feedback that You submit to the Project in any form (e.g., pull request, issue comment, email, file upload).
13
+ - **"Project"** means the @plasius/gpu-worker repositories and related materials owned or managed by **Plasius LTD**.
14
+
15
+ ## 2. Copyright License Grant
16
+
17
+ You hereby grant to Plasius LTD a **perpetual, worldwide, non‑exclusive, transferable, sublicensable, royalty‑free, irrevocable** copyright license to:
18
+
19
+ - use, reproduce, publicly display, publicly perform, modify, create derivative works of, and
20
+ - distribute Contributions in source and object form,
21
+ - and to **sublicense** these rights under any terms Plasius LTD chooses, including proprietary or open‑source licenses.
22
+
23
+ ## 3. Patent License Grant
24
+
25
+ You hereby grant to Plasius LTD and its sublicensees a **perpetual, worldwide, non‑exclusive, transferable, royalty‑free, irrevocable** patent license to **make, have made, use, offer to sell, sell, import, and otherwise transfer** the Contribution and derivative works thereof, where such license applies only to patent claims that You **own or control** and that would be infringed by Your Contribution or its combination with the Project.
26
+
27
+ ## 4. Moral Rights & Attribution
28
+
29
+ To the maximum extent permitted by applicable law, You **waive** and agree not to assert any moral rights (e.g., rights of attribution or integrity) in or to the Contribution against Plasius LTD. Plasius LTD may, but is not required to, credit You.
30
+
31
+ ## 5. Representations & Warranties
32
+
33
+ You represent that:
34
+
35
+ 1. **Originality / Rights:** Each Contribution is Your original creation, or You have sufficient rights to submit it and grant the licenses above.
36
+ 2. **No Confidential Info:** Contributions **do not** include confidential information or trade secrets of any third party.
37
+ 3. **No Infringement:** To the best of Your knowledge, Contributions do not infringe any third‑party IP rights.
38
+ 4. **Employment / Contractor Status:** If Your employer or a third party might claim rights in Your Contribution, You have obtained **written permission** to make the Contribution and grant these licenses (attach or reference below), or Your Contribution is made **outside the scope** of your employment and without using your employer’s confidential information or resources.
39
+ 5. **Compliance:** You will follow the Project’s policies (e.g., Code of Conduct, Security Policy) and applicable laws.
40
+
41
+ ## 6. Third‑Party Code
42
+
43
+ If Your Contribution includes code, data, or other material from a third party, You will **identify the material and its license** in the pull request or submission, and ensure it is **compatible** with the Project’s licensing model. You will not submit material subject to terms that require the Project to disclose proprietary source code (e.g., certain copyleft obligations) unless the Project has **pre‑approved** such inclusion in writing.
44
+
45
+ ## 7. Scope & Duration
46
+
47
+ - This CLA covers **all past and future** Contributions You submit to the Project, unless and until You provide written notice to **revoke** it.
48
+ - Revocation is **not retroactive**: rights granted for prior Contributions remain in effect.
49
+
50
+ ## 8. Disclaimer
51
+
52
+ THE CONTRIBUTION IS PROVIDED “AS IS” WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON‑INFRINGEMENT.
53
+
54
+ ## 9. Governing Law & Jurisdiction
55
+
56
+ This CLA is governed by the **laws of England and Wales**, and the courts of England and Wales shall have **exclusive jurisdiction** over any dispute arising out of or relating to it.
57
+
58
+ ## 10. Entire Agreement
59
+
60
+ This CLA is the entire agreement between You and Plasius LTD regarding Contributions. It supersedes any prior discussions relating to Contributions. If any provision is held unenforceable, the remaining provisions remain in full force.
61
+
62
+ ---
63
+
64
+ ## 11. Contributor Information & Signature
65
+
66
+ By signing below, You agree to the terms of this CLA for Your Contributions to the Project.
67
+
68
+ **Full Name:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
69
+
70
+ **Email:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
71
+
72
+ **GitHub Handle:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
73
+
74
+ **Address (optional):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
75
+
76
+ **Employer (if applicable):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
77
+
78
+ **If employed:** ☐ I confirm Contributions are made outside the scope of employment **or** ☐ I have attached my employer’s written permission.
79
+
80
+ **Signature:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
81
+
82
+ **Date (YYYY‑MM‑DD):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
83
+
84
+ _Electronic signatures are accepted. You may type your name in the Signature field and email a PDF copy._
85
+
86
+ **Submission:** Please email the signed CLA to **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)** with subject line: `CLA – Individual – <GitHubHandle>`.
87
+
88
+ **(Optional) Attachments / Notes:**
89
+
90
+ - Employer permission letter (if required)
91
+ - Third‑party license disclosures (if any)
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@plasius/gpu-worker",
3
+ "version": "0.1.0",
4
+ "description": "WebGPU worker runtime with a lock-free job queue for WGSL workloads.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "private": false,
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "files": [
11
+ "dist",
12
+ "src",
13
+ "README.md",
14
+ "CHANGELOG.md",
15
+ "LICENSE",
16
+ "legal"
17
+ ],
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs"
22
+ },
23
+ "./worker.wgsl": "./dist/worker.wgsl",
24
+ "./package.json": "./package.json"
25
+ },
26
+ "scripts": {
27
+ "build": "tsup && cp src/worker.wgsl dist/worker.wgsl",
28
+ "demo": "python3 -m http.server",
29
+ "test": "npm run test:unit",
30
+ "test:unit": "node --test",
31
+ "test:e2e": "npx playwright install chromium && playwright test",
32
+ "test:coverage": "c8 --reporter=lcov --reporter=text node --test"
33
+ },
34
+ "keywords": [
35
+ "webgpu",
36
+ "worker",
37
+ "queue",
38
+ "wgsl",
39
+ "gpu",
40
+ "compute",
41
+ "raytracing",
42
+ "physics",
43
+ "acoustics",
44
+ "plasius"
45
+ ],
46
+ "author": "Plasius LTD <development@plasius.co.uk>",
47
+ "license": "Apache-2.0",
48
+ "dependencies": {
49
+ "@plasius/gpu-lock-free-queue": "^0.1.1-beta.1"
50
+ },
51
+ "devDependencies": {
52
+ "@playwright/test": "^1.57.0",
53
+ "c8": "^10.1.3",
54
+ "tsup": "^8.5.0",
55
+ "typescript": "^5.9.3"
56
+ },
57
+ "repository": {
58
+ "type": "git",
59
+ "url": "git+https://github.com/Plasius-LTD/gpu-worker.git"
60
+ },
61
+ "bugs": {
62
+ "url": "https://github.com/Plasius-LTD/gpu-worker/issues"
63
+ },
64
+ "homepage": "https://github.com/Plasius-LTD/gpu-worker#readme",
65
+ "publishConfig": {
66
+ "access": "public"
67
+ },
68
+ "funding": [
69
+ {
70
+ "type": "patreon",
71
+ "url": "https://www.patreon.com/c/plasiusltd/membership"
72
+ },
73
+ {
74
+ "type": "github",
75
+ "url": "https://github.com/sponsors/Plasius-LTD"
76
+ }
77
+ ]
78
+ }
package/src/index.js ADDED
@@ -0,0 +1,27 @@
1
+ import { loadQueueWgsl } from "@plasius/gpu-lock-free-queue";
2
+
3
+ export const workerWgslUrl = (() => {
4
+ if (typeof __IMPORT_META_URL__ !== "undefined") {
5
+ return new URL("./worker.wgsl", __IMPORT_META_URL__);
6
+ }
7
+ if (typeof __filename !== "undefined" && typeof require !== "undefined") {
8
+ const { pathToFileURL } = require("node:url");
9
+ return new URL("./worker.wgsl", pathToFileURL(__filename));
10
+ }
11
+ const base =
12
+ typeof process !== "undefined" && process.cwd
13
+ ? `file://${process.cwd()}/`
14
+ : "file:///";
15
+ return new URL("./worker.wgsl", base);
16
+ })();
17
+
18
+ export async function loadWorkerWgsl() {
19
+ const response = await fetch(workerWgslUrl);
20
+ return response.text();
21
+ }
22
+
23
+ export async function assembleWorkerWgsl(workerWgsl) {
24
+ const queueWgsl = await loadQueueWgsl();
25
+ const body = workerWgsl ?? (await loadWorkerWgsl());
26
+ return `${queueWgsl}\n\n${body}`;
27
+ }
@@ -0,0 +1,134 @@
1
+ struct RenderParams {
2
+ width: u32,
3
+ height: u32,
4
+ tile_size: u32,
5
+ _pad0: u32,
6
+ camera_pos: vec4<f32>,
7
+ camera_target: vec4<f32>,
8
+ fov_y: f32,
9
+ aspect: f32,
10
+ time: f32,
11
+ _pad1: f32,
12
+ };
13
+
14
+ @group(1) @binding(0) var<storage, read_write> framebuffer: array<u32>;
15
+ @group(1) @binding(1) var<uniform> render: RenderParams;
16
+
17
+ fn hit_sphere(center: vec3<f32>, radius: f32, origin: vec3<f32>, dir: vec3<f32>) -> f32 {
18
+ let oc = origin - center;
19
+ let a = dot(dir, dir);
20
+ let b = 2.0 * dot(oc, dir);
21
+ let c = dot(oc, oc) - radius * radius;
22
+ let disc = b * b - 4.0 * a * c;
23
+ if (disc < 0.0) {
24
+ return -1.0;
25
+ }
26
+ let sq = sqrt(disc);
27
+ let t0 = (-b - sq) / (2.0 * a);
28
+ if (t0 > 0.001) {
29
+ return t0;
30
+ }
31
+ let t1 = (-b + sq) / (2.0 * a);
32
+ if (t1 > 0.001) {
33
+ return t1;
34
+ }
35
+ return -1.0;
36
+ }
37
+
38
+ fn background(dir: vec3<f32>) -> vec3<f32> {
39
+ let t = 0.5 * (dir.y + 1.0);
40
+ return mix(vec3<f32>(0.04, 0.05, 0.08), vec3<f32>(0.65, 0.78, 0.92), t);
41
+ }
42
+
43
+ fn shade_point(pos: vec3<f32>, normal: vec3<f32>, base: vec3<f32>) -> vec3<f32> {
44
+ let light_dir = normalize(vec3<f32>(0.6 + 0.2 * sin(render.time), 0.9, 0.3));
45
+ let diff = max(dot(normal, light_dir), 0.0);
46
+ let ambient = 0.2;
47
+ return base * (ambient + diff * 0.8);
48
+ }
49
+
50
+ fn pack_color(color: vec3<f32>) -> u32 {
51
+ let c = clamp(color, vec3<f32>(0.0), vec3<f32>(1.0));
52
+ let r = u32(round(c.x * 255.0));
53
+ let g = u32(round(c.y * 255.0));
54
+ let b = u32(round(c.z * 255.0));
55
+ return (255u << 24) | (b << 16) | (g << 8) | r;
56
+ }
57
+
58
+ @compute @workgroup_size(64)
59
+ fn raytrace_main(@builtin(global_invocation_id) gid: vec3<u32>) {
60
+ let idx = gid.x;
61
+ if (idx >= params.job_count) {
62
+ return;
63
+ }
64
+
65
+ let ok = dequeue(idx);
66
+ if (ok == 0u) {
67
+ return;
68
+ }
69
+
70
+ let job = output_jobs[idx];
71
+ let tiles_x = (render.width + render.tile_size - 1u) / render.tile_size;
72
+ let tile_x = job % tiles_x;
73
+ let tile_y = job / tiles_x;
74
+ let start_x = tile_x * render.tile_size;
75
+ let start_y = tile_y * render.tile_size;
76
+
77
+ let forward = normalize(render.camera_target.xyz - render.camera_pos.xyz);
78
+ let right = normalize(cross(forward, vec3<f32>(0.0, 1.0, 0.0)));
79
+ let up = normalize(cross(right, forward));
80
+ let tan_half = tan(0.5 * render.fov_y);
81
+
82
+ for (var y: u32 = 0u; y < render.tile_size; y = y + 1u) {
83
+ let py = start_y + y;
84
+ if (py >= render.height) {
85
+ continue;
86
+ }
87
+ for (var x: u32 = 0u; x < render.tile_size; x = x + 1u) {
88
+ let px = start_x + x;
89
+ if (px >= render.width) {
90
+ continue;
91
+ }
92
+
93
+ let u = (f32(px) + 0.5) / f32(render.width);
94
+ let v = (f32(py) + 0.5) / f32(render.height);
95
+ let ndc = vec2<f32>(u * 2.0 - 1.0, 1.0 - v * 2.0);
96
+ let dir = normalize(forward + ndc.x * render.aspect * tan_half * right + ndc.y * tan_half * up);
97
+
98
+ let origin = render.camera_pos.xyz;
99
+ var color = background(dir);
100
+
101
+ let t1 = hit_sphere(vec3<f32>(0.0, 1.0, 0.0), 1.0, origin, dir);
102
+ let t2 = hit_sphere(vec3<f32>(-1.6, 0.6, -0.5), 0.6, origin, dir);
103
+ let t3 = hit_sphere(vec3<f32>(0.0, -1000.0, 0.0), 999.0, origin, dir);
104
+
105
+ var t_hit = -1.0;
106
+ var base = vec3<f32>(0.0);
107
+ var center = vec3<f32>(0.0);
108
+ if (t1 > 0.0) {
109
+ t_hit = t1;
110
+ base = vec3<f32>(0.86, 0.42, 0.32);
111
+ center = vec3<f32>(0.0, 1.0, 0.0);
112
+ }
113
+ if (t2 > 0.0 && (t_hit < 0.0 || t2 < t_hit)) {
114
+ t_hit = t2;
115
+ base = vec3<f32>(0.2, 0.7, 0.9);
116
+ center = vec3<f32>(-1.6, 0.6, -0.5);
117
+ }
118
+ if (t3 > 0.0 && (t_hit < 0.0 || t3 < t_hit)) {
119
+ t_hit = t3;
120
+ base = vec3<f32>(0.32, 0.3, 0.26);
121
+ center = vec3<f32>(0.0, -1000.0, 0.0);
122
+ }
123
+
124
+ if (t_hit > 0.0) {
125
+ let pos = origin + t_hit * dir;
126
+ let normal = normalize(pos - center);
127
+ color = shade_point(pos, normal, base);
128
+ }
129
+
130
+ let pixel_index = py * render.width + px;
131
+ framebuffer[pixel_index] = pack_color(color);
132
+ }
133
+ }
134
+ }