xbintsc 0.6.13 → 0.6.18

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.
Files changed (79) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/driver/version.d.ts +1 -1
  3. package/dist/src/driver/version.js +1 -1
  4. package/dist/src/extensions/gui/features.d.ts +2 -0
  5. package/dist/src/extensions/gui/features.js +24 -0
  6. package/dist/src/extensions/gui/features.js.map +1 -1
  7. package/dist/src/extensions/gui/index.js +39 -0
  8. package/dist/src/extensions/gui/index.js.map +1 -1
  9. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  10. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  11. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  14. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  16. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  17. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  18. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  19. package/dist/tests/extensions/gui-features.test.js +18 -1
  20. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  21. package/doc/ai/extensions.md +7 -1
  22. package/doc/ai/zh-CN/extensions.md +6 -1
  23. package/doc/gui/webgpu.md +121 -0
  24. package/doc/gui.md +12 -1
  25. package/doc/zh-CN/gui/webgpu.md +111 -0
  26. package/doc/zh-CN/gui.md +10 -1
  27. package/llms.txt +1 -0
  28. package/package.json +7 -7
  29. package/runtime/ext_gui/browser.cpp +10 -0
  30. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  31. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  32. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  33. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  34. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  35. package/runtime/ext_gui/renderer_image.cpp +6 -0
  36. package/runtime/ext_gui/webgl_glsl.h +8 -0
  37. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  38. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  39. package/runtime/ext_gui/webgl_msl.h +4 -0
  40. package/runtime/ext_gui/webgpu.h +371 -0
  41. package/runtime/ext_gui/webgpu_api.h +42 -0
  42. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  43. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  44. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  45. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  46. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  47. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  48. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  49. package/runtime/ext_gui/webgpu_convert.cpp +313 -0
  50. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  51. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  52. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  53. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  54. package/runtime/ext_gui/webgpu_pass.h +33 -0
  55. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  56. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  57. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  58. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  59. package/runtime/ext_gui/webgpu_shader.cpp +246 -0
  60. package/runtime/ext_gui/wgsl.h +99 -0
  61. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  62. package/runtime/ext_gui/wgsl_ast.h +233 -0
  63. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  64. package/runtime/ext_gui/wgsl_emit.h +128 -0
  65. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  66. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  67. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  68. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  69. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  70. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  71. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  72. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  73. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  74. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  75. package/runtime/ext_gui/wgsl_parser.h +77 -0
  76. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  77. package/src/driver/version.ts +1 -1
  78. package/src/extensions/gui/features.ts +24 -0
  79. package/src/extensions/gui/index.ts +39 -0
@@ -0,0 +1,121 @@
1
+ # WebGPU (`navigator.gpu` on a `<canvas>`)
2
+
3
+ > Part of the [GUI extension](../gui.md); the engine overview, locked
4
+ > decisions and roadmap live there.
5
+
6
+ A page can create a WebGPU device and draw into a `<canvas>`:
7
+
8
+ ```ts
9
+ const canvas: any = document.getElementById("scene");
10
+ const context: any = canvas.getContext("webgpu");
11
+ const gpu: any = (navigator as any).gpu;
12
+ const adapter = await gpu.requestAdapter();
13
+ const device = await adapter.requestDevice();
14
+ context.configure({ device, format: gpu.getPreferredCanvasFormat() });
15
+
16
+ const module = device.createShaderModule({ code: WGSL });
17
+ // … pipeline, bind group, command encoder, render pass …
18
+ ```
19
+
20
+ The implementation sits behind the optional **`webgpu` link feature** (which
21
+ requires `gpu`, the bundled glslang). A page that never touches WebGPU links
22
+ none of it: `getContext("webgpu")` answers `null`, `navigator.gpu` is
23
+ `undefined`, and the `GPU…` host types do not exist, so a `typeof` feature test
24
+ takes its fallback path.
25
+
26
+ ## How a frame reaches the screen
27
+
28
+ - `context.getCurrentTexture()` returns a `GPUTexture` whose SDL_GPU texture is
29
+ also the canvas's colour texture. The page's image pipeline samples it
30
+ directly (no CPU readback), exactly like the WebGL path.
31
+ - Vertex buffers are recorded per slot and bound together at draw time
32
+ (`SDL_BindGPUVertexBuffers` starts at slot 0). This SDL_GPU build's Vulkan
33
+ backend mishandles separate `firstSlot` bindings, so binding slot 1 on its own
34
+ would overwrite slot 0 and break `stepMode: "instance"`.
35
+ - `context.configure()` creates the backbuffer at the canvas's `width` /
36
+ `height`; the `canvas.width` / `canvas.height` setters recreate it.
37
+ - Queue submissions are immediate SDL_GPU command buffer submissions. A
38
+ completed render pass is visible to the compositor on the next frame.
39
+ - Buffers used as **uniform buffers** keep a CPU mirror: SDL_GPU takes uniform
40
+ data through `SDL_Push…UniformData`, not a bound buffer. `writeBuffer`
41
+ updates both the mirror and (when the buffer has GPU usage) the SDL buffer.
42
+
43
+ ## WGSL
44
+
45
+ WGSL is translated to Vulkan-dialect GLSL, then compiled by the same bundled
46
+ glslang that serves WebGL; on Metal the SPIR-V is cross-compiled to MSL by the
47
+ bundled SPIRV-Cross. The front end (`runtime/ext_gui/wgsl_*.cpp`) supports:
48
+
49
+ - `struct`, `alias`, `const`, `override`, module-scope `var` in `private`,
50
+ `workgroup`, `uniform` and `storage` address spaces;
51
+ - `@vertex`, `@fragment`, `@compute` entry points with `@location`,
52
+ `@builtin(position|vertex_index|instance_index|front_facing|frag_depth|
53
+ sample_index|sample_mask|local_invocation_id|global_invocation_id|
54
+ workgroup_id|num_workgroups|local_invocation_index)`, `@interpolate`,
55
+ `@group`/`@binding` and `@workgroup_size`;
56
+ - the scalar/vector/matrix/array constructors, swizzles, `let`/`var`,
57
+ `if`/`else`, `for`, `while`, `loop`/`continuing`, `switch`, `break`,
58
+ `continue`, `return`, `discard`, and the usual math/texture builtins;
59
+ - `texture_2d`/`texture_2d_array`/`texture_3d`/`texture_cube` (and the depth
60
+ variants), `sampler`, `sampler_comparison`, `texture_storage_2d`, plus
61
+ `textureSample*`, `textureLoad`, `textureStore`, `textureDimensions` and
62
+ `textureGather`.
63
+
64
+ A texture and a sampler are separate WGSL bindings but SDL_GPU binds them as a
65
+ pair, so the translator analyses the `textureSample` call sites and emits one
66
+ combined sampler per (texture, sampler) pair. A texture used only through
67
+ `textureLoad` gets a slot with a default sampler.
68
+
69
+ ## Supported API
70
+
71
+ Implemented interfaces: `GPU`, `GPUAdapter`, `GPUAdapterInfo`, `GPUDevice`,
72
+ `GPUQueue`, `GPUBuffer`, `GPUTexture`, `GPUTextureView`, `GPUSampler`,
73
+ `GPUShaderModule`, `GPUBindGroupLayout`, `GPUPipelineLayout`, `GPUBindGroup`,
74
+ `GPURenderPipeline`, `GPUComputePipeline`, `GPUCommandEncoder`,
75
+ `GPURenderPassEncoder`, `GPUComputePassEncoder`, `GPURenderBundleEncoder`,
76
+ `GPURenderBundle`, `GPUQuerySet`, `GPUCanvasContext`, `GPUCompilationInfo`,
77
+ `GPUCompilationMessage` and the `GPUBufferUsage` / `GPUTextureUsage` /
78
+ `GPUShaderStage` / `GPUMapMode` / `GPUColorWrite` namespaces.
79
+
80
+ That includes device creation, error scopes (`pushErrorScope` /
81
+ `popErrorScope`) and `uncapturederror`, buffer mapping
82
+ (`mapAsync`/`getMappedRange`/`unmap`), `queue.writeBuffer`/`writeTexture`,
83
+ render and compute pipelines (including the `*Async` variants), bind groups,
84
+ render/compute passes, draws (indexed, indirect, instanced), compute dispatches
85
+ (direct and indirect), `copyBufferToBuffer`, `copyBufferToTexture`,
86
+ `copyTextureToBuffer`, `copyTextureToTexture`, `clearBuffer` and a
87
+ basic render-bundle replay.
88
+
89
+ ## Not implemented
90
+
91
+ These parts of the specification are accepted by the API but report a
92
+ validation error (or do nothing observable) because SDL_GPU has no equivalent:
93
+
94
+ - **Queries**: `GPUQuerySet` exists, but `beginOcclusionQuery`,
95
+ `endOcclusionQuery`, `resolveQuerySet` and `writeTimestamp` record a
96
+ validation error; the SDL_GPU build has no query API.
97
+ - **`copyExternalImageToTexture` / `importExternalTexture`**: DOM image/canvas
98
+ sources are not bridged into the WebGPU queue.
99
+ - **Atomics** (`atomic<u32>` and the `atomic*` builtins) are rejected by the
100
+ WGSL front end.
101
+ - **Storage-texture writes in fragment stages**, MSAA resolve targets beyond
102
+ SDL's color-target resolve, and non-`2d` canvas contexts.
103
+ - **Pointers** in user functions (`ptr<…>` parameters and `&`/`*` on locals).
104
+ - A pipeline layout uses at most **4 uniform buffers per stage** and **16
105
+ sampled textures per stage** (SDL_GPU's per-stage limits); a shader that
106
+ exceeds them fails validation with an explanatory message.
107
+ - `GPUTexture.createView()` accepts its descriptor but SDL_GPU binds a whole
108
+ texture, so `baseMipLevel` / `arrayLayerCount` selection is not represented.
109
+ - A bind group applies the first dynamic offset to every dynamic entry; the
110
+ common one-offset-per-group pattern works.
111
+
112
+ Direct3D 12 is not usable: SDL_GPU's D3D12 backend consumes DXIL, which this
113
+ build has no compiler for. On Windows the GUI engine picks Vulkan when it is
114
+ available (the same rule as WebGL).
115
+
116
+ ## Testing
117
+
118
+ `tests/e2e/gui-webgpu.test.ts` draws a textured triangle with a WGSL
119
+ vertex/fragment pair, reads the canvas back through `copyTextureToBuffer`,
120
+ runs a compute shader over a storage buffer and checks an error scope and a
121
+ shader compilation message. Demo: `examples/gui/webgpu-triangle`.
package/doc/gui.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # xbintsc GUI extension (self-hosted HTML/CSS renderer)
2
2
 
3
- Status: **M22** — features (M1–M15) are complete: HTML parsing, CSS selector
3
+ Status: **M23** — features (M1–M15) are complete: HTML parsing, CSS selector
4
4
  matching, the cascade, computed styles and layout (block, inline, Flexbox and
5
5
  CSS positioning) are
6
6
  in place, and the engine *paints*: it builds a display list of rectangles, images
@@ -415,6 +415,17 @@ engine were Rust.
415
415
  `skewX()`/`skewY()`, `background-size` tiling for `url()` layers and
416
416
  document CSS on SVG children (`fill`/`stroke`/…) are now honoured.
417
417
  - E2E: `tests/e2e/gui-css-coverage.test.ts`.
418
+ 23. **M23 — WebGPU** ✅
419
+ - `navigator.gpu`, `getContext("webgpu")` and the `GPU…` object model are
420
+ implemented behind the optional **`webgpu` link feature** (requires
421
+ `gpu`), with a WGSL → Vulkan GLSL front end compiled by the bundled
422
+ glslang and cross-compiled to MSL on Metal by SPIRV-Cross.
423
+ - The canvas backbuffer is shared with the page compositor (no readback);
424
+ buffers, textures, samplers, bind groups, render/compute pipelines,
425
+ command encoders, render/compute passes, draws, dispatches, copies and
426
+ buffer mapping are implemented; queries/atomics/DOM image uploads report
427
+ validation errors (see [WebGPU](./gui/webgpu.md)).
428
+ - E2E: `tests/e2e/gui-webgpu.test.ts`; demo: `examples/gui/webgpu-triangle`.
418
429
 
419
430
  ## Progress log
420
431
 
@@ -0,0 +1,111 @@
1
+ # WebGPU(`<canvas>` 上的 `navigator.gpu`)
2
+
3
+ > 属于 [GUI 扩展](../gui.md);引擎总览、既定决策与路线图见该页。
4
+
5
+ 页面可以创建 WebGPU 设备并绘制到 `<canvas>`:
6
+
7
+ ```ts
8
+ const canvas: any = document.getElementById("scene");
9
+ const context: any = canvas.getContext("webgpu");
10
+ const gpu: any = (navigator as any).gpu;
11
+ const adapter = await gpu.requestAdapter();
12
+ const device = await adapter.requestDevice();
13
+ context.configure({ device, format: gpu.getPreferredCanvasFormat() });
14
+
15
+ const module = device.createShaderModule({ code: WGSL });
16
+ // … pipeline、bind group、command encoder、render pass …
17
+ ```
18
+
19
+ 实现位于可选的 **`webgpu` 链接特性**中(依赖 `gpu`,即内置的 glslang)。
20
+ 完全不用 WebGPU 的页面不会链接其中任何代码:`getContext("webgpu")` 返回
21
+ `null`,`navigator.gpu` 为 `undefined`,`GPU…` 宿主类型也不存在,因此
22
+ `typeof` 探测会走回退路径。
23
+
24
+ ## 一帧如何到达屏幕
25
+
26
+ - `context.getCurrentTexture()` 返回的 `GPUTexture` 同时就是画布的颜色纹理,
27
+ 页面图像管线直接采样它(不经 CPU 回读),与 WebGL 路径一致。
28
+ - 多路顶点缓冲按槽位记录,在 draw 时从 slot 0 起一次性绑定
29
+ (`SDL_BindGPUVertexBuffers`)。这版 SDL_GPU 的 Vulkan 后端对分开的
30
+ `firstSlot` 绑定处理有误,单独绑定 slot 1 会覆盖 slot 0,使
31
+ `stepMode: "instance"` 失效。
32
+ - `context.configure()` 按画布的 `width`/`height` 创建后备缓冲;
33
+ `canvas.width`/`canvas.height` 的赋值会重建它。
34
+ - 队列提交即提交 SDL_GPU 命令缓冲。已结束的 render pass 在下一帧对合成器可见。
35
+ - 作为**uniform buffer**使用的缓冲区保留一份 CPU 镜像:SDL_GPU 通过
36
+ `SDL_Push…UniformData` 接收 uniform 数据而非绑定缓冲;`writeBuffer`
37
+ 同时更新镜像与(有 GPU 用途时的)SDL 缓冲。
38
+
39
+ ## WGSL
40
+
41
+ WGSL 先翻译为 Vulkan 方言的 GLSL,再交给与 WebGL 相同的内置 glslang 编译;
42
+ 在 Metal 上再由内置 SPIRV-Cross 转成 MSL。前端
43
+ (`runtime/ext_gui/wgsl_*.cpp`)支持:
44
+
45
+ - `struct`、`alias`、`const`、`override`,以及 `private`、`workgroup`、
46
+ `uniform`、`storage` 地址空间的模块级 `var`;
47
+ - `@vertex`、`@fragment`、`@compute` 入口点,以及 `@location`、
48
+ `@builtin(position|vertex_index|instance_index|front_facing|frag_depth|
49
+ sample_index|sample_mask|local_invocation_id|global_invocation_id|
50
+ workgroup_id|num_workgroups|local_invocation_index)`、`@interpolate`、
51
+ `@group`/`@binding`、`@workgroup_size`;
52
+ - 标量/向量/矩阵/数组构造器、swizzle、`let`/`var`、`if`/`else`、`for`、
53
+ `while`、`loop`/`continuing`、`switch`、`break`、`continue`、`return`、
54
+ `discard`,以及常用数学/纹理内建函数;
55
+ - `texture_2d`/`texture_2d_array`/`texture_3d`/`texture_cube`(含 depth 变体)、
56
+ `sampler`、`sampler_comparison`、`texture_storage_2d`,以及
57
+ `textureSample*`、`textureLoad`、`textureStore`、`textureDimensions`、
58
+ `textureGather`。
59
+
60
+ WGSL 中纹理与采样器是两个独立绑定,而 SDL_GPU 成对绑定,因此翻译器会分析
61
+ `textureSample` 调用点,为每个(纹理, 采样器)对生成一个组合采样器;仅通过
62
+ `textureLoad` 使用的纹理也会获得一个带默认采样器的槽位。
63
+
64
+ ## 已实现的 API
65
+
66
+ 已实现接口:`GPU`、`GPUAdapter`、`GPUAdapterInfo`、`GPUDevice`、`GPUQueue`、
67
+ `GPUBuffer`、`GPUTexture`、`GPUTextureView`、`GPUSampler`、`GPUShaderModule`、
68
+ `GPUBindGroupLayout`、`GPUPipelineLayout`、`GPUBindGroup`、
69
+ `GPURenderPipeline`、`GPUComputePipeline`、`GPUCommandEncoder`、
70
+ `GPURenderPassEncoder`、`GPUComputePassEncoder`、`GPURenderBundleEncoder`、
71
+ `GPURenderBundle`、`GPUQuerySet`、`GPUCanvasContext`、`GPUCompilationInfo`、
72
+ `GPUCompilationMessage`,以及 `GPUBufferUsage`/`GPUTextureUsage`/
73
+ `GPUShaderStage`/`GPUMapMode`/`GPUColorWrite` 命名空间。
74
+
75
+ 包括设备创建、错误作用域(`pushErrorScope`/`popErrorScope`)与
76
+ `uncapturederror`、缓冲区映射(`mapAsync`/`getMappedRange`/`unmap`)、
77
+ `queue.writeBuffer`/`writeTexture`、render/compute pipeline(含 `*Async`)、
78
+ bind group、render/compute pass、绘制(索引、间接、实例化)、compute dispatch
79
+ (直接与间接)、`copyBufferToBuffer`、`copyBufferToTexture`、
80
+ `copyTextureToBuffer`、`copyTextureToTexture`、`clearBuffer`,以及基础的
81
+ render bundle 回放。
82
+
83
+ ## 未实现
84
+
85
+ 以下规范内容可以调用,但会记录一条校验错误(或无可观察行为),因为
86
+ SDL_GPU 没有对应能力:
87
+
88
+ - **查询**:`GPUQuerySet` 存在,但 `beginOcclusionQuery`、
89
+ `endOcclusionQuery`、`resolveQuerySet`、`writeTimestamp` 会记录校验错误;
90
+ 该 SDL_GPU 构建没有查询 API。
91
+ - **`copyExternalImageToTexture` / `importExternalTexture`**:DOM 图像/画布来源
92
+ 不会桥接到 WebGPU 队列。
93
+ - **原子操作**(`atomic<u32>` 与 `atomic*` 内建)被 WGSL 前端拒绝。
94
+ - **片元阶段的存储纹理写入**、超出 SDL 颜色目标 resolve 的 MSAA、以及非
95
+ `2d` 画布上下文。
96
+ - 用户函数中的**指针**(`ptr<…>` 参数、局部变量的 `&`/`*`)。
97
+ - 每个阶段最多 **4 个 uniform buffer**、**16 个采样纹理**(SDL_GPU 每阶段
98
+ 限制);超出的着色器会得到带说明的校验失败。
99
+ - `GPUTexture.createView()` 接受描述对象,但 SDL_GPU 绑定整张纹理,因此
100
+ `baseMipLevel`/`arrayLayerCount` 选择不会被表示。
101
+ - 每个 bind group 只把第一个动态偏移应用到所有动态项;常见的“每组一个偏移”
102
+ 用法可以正常工作。
103
+
104
+ Direct3D 12 不可用:SDL_GPU 的 D3D12 后端只接受 DXIL,本构建没有对应编译器。
105
+ Windows 上 GUI 引擎在可用时选择 Vulkan(与 WebGL 相同的规则)。
106
+
107
+ ## 测试
108
+
109
+ `tests/e2e/gui-webgpu.test.ts` 用 WGSL 顶点/片元着色器绘制带纹理的三角形,
110
+ 通过 `copyTextureToBuffer` 回读画布,对存储缓冲运行 compute shader,并检查
111
+ 错误作用域与着色器编译消息。示例:`examples/gui/webgpu-triangle`。
package/doc/zh-CN/gui.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > 语言 / Language:[English](../gui.md) | **简体中文**
4
4
 
5
- 状态:**M22** —— 功能(M1–M15)已全部完成:HTML 解析、CSS 选择器匹配、层叠(cascade)、计算样式与布局(块级、行内、Flexbox 与 CSS 定位)均已就位,引擎还会**绘制**:它构建一个由矩形、图像与排版后的文本 run 组成的显示列表,并通过 SDL_GPU 渲染。输入事件会经过命中测试并投递给原生 TS 处理器,`:hover`/`:focus` 会被动态匹配,`<img>` 依据其固有尺寸确定大小并从纹理绘制——位图(PNG/JPEG/…)与 SVG 文件皆可,后者按绘制尺寸光栅化,因此矢量图保持清晰——CSS transition 会为绘制属性做动画。**M16** 让内联 `<svg>` 子树获得同样的处理:子树被序列化回 SVG 文档,并按绘制设备尺寸光栅化,因此图标不再需要单独的文件。**M8** 增加了交互式 DOM:具有稳定标识的元素句柄、变更操作(`appendChild`、`textContent`、`classList`、`style` 等)以及支持捕获/冒泡的元素级事件。**M9** 会提前编译 `<script>` 主体(内联与 `<script src>`)——参见 `gui-scripts.md`。**M10** 增加 `requestAnimationFrame` 以及若干 DOM 辅助方法。**M11** 增加 `position: relative`/`absolute`/`fixed` 及其偏移,`examples/gui/pelican-bike`(一个可玩的 2D 游戏)就构建在它之上。**M12** 明确了 DOM 节点生命周期(哪些变更释放、哪些保留),并让引擎内存有界:参见[节点生命周期](./gui/dom.md)与[内存与 GC](./gui/runtime.md)。**M13** 真正播放
5
+ 状态:**M23** —— 功能(M1–M15)已全部完成:HTML 解析、CSS 选择器匹配、层叠(cascade)、计算样式与布局(块级、行内、Flexbox 与 CSS 定位)均已就位,引擎还会**绘制**:它构建一个由矩形、图像与排版后的文本 run 组成的显示列表,并通过 SDL_GPU 渲染。输入事件会经过命中测试并投递给原生 TS 处理器,`:hover`/`:focus` 会被动态匹配,`<img>` 依据其固有尺寸确定大小并从纹理绘制——位图(PNG/JPEG/…)与 SVG 文件皆可,后者按绘制尺寸光栅化,因此矢量图保持清晰——CSS transition 会为绘制属性做动画。**M16** 让内联 `<svg>` 子树获得同样的处理:子树被序列化回 SVG 文档,并按绘制设备尺寸光栅化,因此图标不再需要单独的文件。**M8** 增加了交互式 DOM:具有稳定标识的元素句柄、变更操作(`appendChild`、`textContent`、`classList`、`style` 等)以及支持捕获/冒泡的元素级事件。**M9** 会提前编译 `<script>` 主体(内联与 `<script src>`)——参见 `gui-scripts.md`。**M10** 增加 `requestAnimationFrame` 以及若干 DOM 辅助方法。**M11** 增加 `position: relative`/`absolute`/`fixed` 及其偏移,`examples/gui/pelican-bike`(一个可玩的 2D 游戏)就构建在它之上。**M12** 明确了 DOM 节点生命周期(哪些变更释放、哪些保留),并让引擎内存有界:参见[节点生命周期](./gui/dom.md)与[内存与 GC](./gui/runtime.md)。**M13** 真正播放
6
6
  `<video>`/`<audio>`——可选的 **`media` 链接特性**用 vendored 的最小 FFmpeg 在主线程解码,
7
7
  视频帧走图像纹理路径、音频进入 SDL 流,DOM 媒体 API 暴露
8
8
  `play`/`pause`/`currentTime`/… 与媒体事件。编译器会从程序里判断是否需要它
@@ -327,6 +327,15 @@
327
327
  文档 CSS(`fill`/`stroke`/…)均已生效。
328
328
  - e2e:`tests/e2e/gui-css-coverage.test.ts`。
329
329
 
330
+ 23. **M23 — WebGPU** ✅
331
+ - `navigator.gpu`、`getContext("webgpu")` 与整套 `GPU…` 对象模型实现在可选的
332
+ **`webgpu` 链接特性**(依赖 `gpu`)中;WGSL 先翻译为 Vulkan GLSL,由内置
333
+ glslang 编译,Metal 上再由 SPIRV-Cross 转成 MSL。
334
+ - 画布后备缓冲与页面合成器共享(无回读);缓冲、纹理、采样器、bind group、
335
+ render/compute pipeline、命令编码器、render/compute pass、绘制、dispatch、
336
+ 拷贝与缓冲映射均已实现;查询/原子/DOM 图像上传会记录校验错误
337
+ (见 [WebGPU](./gui/webgpu.md))。
338
+ - e2e:`tests/e2e/gui-webgpu.test.ts`;示例:`examples/gui/webgpu-triangle`。
330
339
  ## 进度日志
331
340
 
332
341
  - **M1** ✅ 通用 `xt_loop_poll`/`xt_loop_set_main`;`gui` 扩展骨架。
package/llms.txt CHANGED
@@ -77,6 +77,7 @@
77
77
  - [doc/gui/runtime.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui/runtime.md): event loop integration, the native bridge and memory
78
78
  - [doc/gui/canvas.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui/canvas.md): `<canvas>` and the 2D context
79
79
  - [doc/gui/webgl.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui/webgl.md): WebGL 1.0 on the GPU — GLSL ES to SPIR-V, textures, framebuffers and readback
80
+ - [doc/gui/webgpu.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui/webgpu.md): WebGPU — navigator.gpu, the GPU object model and WGSL shaders over SDL_GPU
80
81
  - [doc/gui/build.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui/build.md): building `gui.a` and running the examples
81
82
  - [doc/gui/known-issues.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui/known-issues.md): known issues and open questions
82
83
  - [doc/gui-scripts.md](https://raw.githubusercontent.com/zy445566/xbintsc/main/doc/gui-scripts.md): the GUI scripting surface
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xbintsc",
3
- "version": "0.6.13",
3
+ "version": "0.6.18",
4
4
  "description": "xbintsc - a TypeScript binary compiler (TypeScript -> LLVM IR -> native binary)",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -72,12 +72,12 @@
72
72
  "vitest": "^2.1.8"
73
73
  },
74
74
  "optionalDependencies": {
75
- "@xbintsc/linux-x64": "0.6.13",
76
- "@xbintsc/linux-arm64": "0.6.13",
77
- "@xbintsc/darwin-x64": "0.6.13",
78
- "@xbintsc/darwin-arm64": "0.6.13",
79
- "@xbintsc/win32-x64": "0.6.13",
80
- "@xbintsc/win32-arm64": "0.6.13"
75
+ "@xbintsc/linux-x64": "0.6.18",
76
+ "@xbintsc/linux-arm64": "0.6.18",
77
+ "@xbintsc/darwin-x64": "0.6.18",
78
+ "@xbintsc/darwin-arm64": "0.6.18",
79
+ "@xbintsc/win32-x64": "0.6.18",
80
+ "@xbintsc/win32-arm64": "0.6.18"
81
81
  },
82
82
  "engines": {
83
83
  "node": ">=22"
@@ -19,6 +19,7 @@
19
19
 
20
20
  #include "gui_engine.h"
21
21
  #include "dom_api_internal.h"
22
+ #include "webgpu_api.h"
22
23
 
23
24
  #include <cstdio>
24
25
  #include <string>
@@ -214,6 +215,11 @@ xt_value navigator_build(void) {
214
215
  set_number(nav, "maxTouchPoints", 0);
215
216
  set_prop(nav, "onLine", XT_TRUE);
216
217
  set_prop(nav, "webdriver", XT_FALSE);
218
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
219
+ set_prop(nav, "gpu", webgpu->navigator_gpu());
220
+ } else {
221
+ set_prop(nav, "gpu", XT_UNDEFINED);
222
+ }
217
223
  set_method(nav, "vibrate", (void *)noop);
218
224
  xt_value languages = xt_array_new(0, NULL);
219
225
  xt_array_push(languages, string_of("en-US"));
@@ -533,6 +539,10 @@ xt_value xt_browser_type(int32_t argc, xt_value *argv) {
533
539
  name == "DOMParser") {
534
540
  return XT_UNDEFINED;
535
541
  }
542
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
543
+ xt_value type = webgpu->host_type(name.c_str());
544
+ if (!XT_IS_UNDEFINED(type)) return type;
545
+ }
536
546
  return type_value(name);
537
547
  }
538
548
 
@@ -9,6 +9,7 @@
9
9
  */
10
10
 
11
11
  #include "dom_api_internal.h"
12
+ #include "webgpu_api.h"
12
13
 
13
14
  #include <string>
14
15
  #include <vector>
@@ -86,11 +87,31 @@ METHOD(node_get_context) {
86
87
  if (value_truthy(xt_object_get_cstr(self, "__xt_gui_offscreen"))) return XT_NULL;
87
88
  XtGuiWindow *win = nullptr;
88
89
  xtgui::Node *node = resolve_node(self, &win);
90
+ if (mode == "webgpu") {
91
+ const XtWebgpuApi *webgpu = xt_webgpu_api();
92
+ if (webgpu == nullptr) return XT_NULL;
93
+ if (node == nullptr) return XT_NULL;
94
+ if (!node->isElement() || !node->isTag("canvas")) return XT_NULL;
95
+ if (xt_gui_canvas_find(win, node) != nullptr) return XT_NULL;
96
+ if (const XtWebglApi *webgl = xt_webgl_api()) {
97
+ if (webgl->find(win, node) != nullptr) return XT_NULL;
98
+ }
99
+ xt_value cached = xt_object_get_cstr(self, "__xt_gui_webgpu");
100
+ if (XT_IS_OBJECT(cached)) return cached;
101
+ uint32_t context_id = 0;
102
+ if (!webgpu->canvas_get(win, node, &context_id)) return XT_NULL;
103
+ xt_value context = webgpu->handle(self, context_id);
104
+ xt_object_set(self, xt_string_from_cstr("__xt_gui_webgpu"), context);
105
+ return context;
106
+ }
89
107
  if (mode == "webgl" || mode == "experimental-webgl" || mode == "webgl2") {
90
108
  const bool webgl2 = mode == "webgl2";
91
109
  const XtWebglApi *webgl = xt_webgl_api();
92
110
  if (webgl == nullptr) return XT_NULL;
93
111
  if (node == nullptr) return XT_NULL;
112
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
113
+ if (XT_IS_OBJECT(xt_object_get_cstr(self, "__xt_gui_webgpu"))) return XT_NULL;
114
+ }
94
115
  if (!node->isElement() || !node->isTag("canvas")) return XT_NULL;
95
116
  if (xt_gui_canvas_find(win, node) != nullptr) return XT_NULL; // already a 2D canvas
96
117
  xt_value cached = xt_object_get_cstr(self, "__xt_gui_gl");
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  #include "dom_api_internal.h"
11
+ #include "webgpu_api.h"
11
12
 
12
13
  #include <cmath>
13
14
  #include <string>
@@ -290,6 +291,17 @@ METHOD(node_width_get) {
290
291
  xtgui::Node *node = resolve_node(self, &win);
291
292
  if (node == nullptr || !node->isElement()) return xt_number(0);
292
293
  if (node->isTag("canvas")) {
294
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
295
+ xt_value context = xt_object_get_cstr(self, "__xt_gui_webgpu");
296
+ if (XT_IS_OBJECT(context)) {
297
+ float width = 0.0f;
298
+ float height = 0.0f;
299
+ uint64_t id = 0;
300
+ webgpu->info((uint32_t)xt_to_number(xt_object_get_cstr(context, "__xt_webgpu_id")),
301
+ &width, &height, &id);
302
+ return xt_number(width);
303
+ }
304
+ }
293
305
  if (const XtWebglApi *webgl = xt_webgl_api()) {
294
306
  if (void *context = webgl->find(win, node)) {
295
307
  float width = 0.0f;
@@ -316,6 +328,25 @@ METHOD(node_width_set) {
316
328
  if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
317
329
  double value = canvas_number_arg(argc, argv, 0, 0.0);
318
330
  if (node->isTag("canvas")) {
331
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
332
+ xt_value context = xt_object_get_cstr(self, "__xt_gui_webgpu");
333
+ if (XT_IS_OBJECT(context)) {
334
+ uint32_t context_id =
335
+ (uint32_t)xt_to_number(xt_object_get_cstr(context, "__xt_webgpu_id"));
336
+ float width = 0.0f;
337
+ float height = 0.0f;
338
+ uint64_t canvas_id = 0;
339
+ webgpu->info(context_id, &width, &height, &canvas_id);
340
+ webgpu->resize(context_id, (int)value, (int)height);
341
+ mark_dirty(win);
342
+ if (win->document != nullptr) {
343
+ webgpu->info(context_id, &width, &height, &canvas_id);
344
+ win->document->setCanvasInfo(node, width, height, canvas_id);
345
+ }
346
+ set_attribute(win, node, "width", std::to_string((long long)value));
347
+ return XT_UNDEFINED;
348
+ }
349
+ }
319
350
  if (const XtWebglApi *webgl = xt_webgl_api()) {
320
351
  if (void *context = webgl->find(win, node)) {
321
352
  float width = 0.0f;
@@ -367,6 +398,17 @@ METHOD(node_height_get) {
367
398
  xtgui::Node *node = resolve_node(self, &win);
368
399
  if (node == nullptr || !node->isElement()) return xt_number(0);
369
400
  if (node->isTag("canvas")) {
401
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
402
+ xt_value context = xt_object_get_cstr(self, "__xt_gui_webgpu");
403
+ if (XT_IS_OBJECT(context)) {
404
+ float width = 0.0f;
405
+ float height = 0.0f;
406
+ uint64_t id = 0;
407
+ webgpu->info((uint32_t)xt_to_number(xt_object_get_cstr(context, "__xt_webgpu_id")),
408
+ &width, &height, &id);
409
+ return xt_number(height);
410
+ }
411
+ }
370
412
  if (const XtWebglApi *webgl = xt_webgl_api()) {
371
413
  if (void *context = webgl->find(win, node)) {
372
414
  float width = 0.0f;
@@ -392,6 +434,25 @@ METHOD(node_height_set) {
392
434
  if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
393
435
  double value = canvas_number_arg(argc, argv, 0, 0.0);
394
436
  if (node->isTag("canvas")) {
437
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
438
+ xt_value context = xt_object_get_cstr(self, "__xt_gui_webgpu");
439
+ if (XT_IS_OBJECT(context)) {
440
+ uint32_t context_id =
441
+ (uint32_t)xt_to_number(xt_object_get_cstr(context, "__xt_webgpu_id"));
442
+ float width = 0.0f;
443
+ float height = 0.0f;
444
+ uint64_t canvas_id = 0;
445
+ webgpu->info(context_id, &width, &height, &canvas_id);
446
+ webgpu->resize(context_id, (int)width, (int)value);
447
+ mark_dirty(win);
448
+ if (win->document != nullptr) {
449
+ webgpu->info(context_id, &width, &height, &canvas_id);
450
+ win->document->setCanvasInfo(node, width, height, canvas_id);
451
+ }
452
+ set_attribute(win, node, "height", std::to_string((long long)value));
453
+ return XT_UNDEFINED;
454
+ }
455
+ }
395
456
  if (const XtWebglApi *webgl = xt_webgl_api()) {
396
457
  if (void *context = webgl->find(win, node)) {
397
458
  float width = 0.0f;
@@ -18,9 +18,11 @@ const xtgui::GpuShaderApi kGpuShaderApi = {
18
18
  xtgui::scan_glsl,
19
19
  xtgui::rewrite_glsl,
20
20
  xtgui::compile_glsl,
21
+ xtgui::compile_glsl_stage,
21
22
  xtgui::reflect_uniform_block,
22
23
  xtgui::reflect_attributes,
23
24
  xtgui::cross_compile_msl,
25
+ xtgui::cross_compile_msl_stage,
24
26
  };
25
27
 
26
28
  } // namespace
@@ -31,12 +31,18 @@ struct GpuShaderApi {
31
31
  const GlslLinkLayout &layout, const GlslShaderInfo &info);
32
32
  bool (*compile_glsl)(const std::string &source, bool fragment,
33
33
  std::vector<uint32_t> *spirv, std::string *log);
34
+ /** Stage-explicit compile (0 vertex, 1 fragment, 2 compute) for WebGPU. */
35
+ bool (*compile_glsl_stage)(const std::string &source, int stage,
36
+ std::vector<uint32_t> *spirv, std::string *log);
34
37
  bool (*reflect_uniform_block)(const std::vector<uint32_t> &spirv,
35
38
  std::vector<GlslBlockMember> *out);
36
39
  bool (*reflect_attributes)(const std::vector<uint32_t> &spirv,
37
40
  std::unordered_map<std::string, int> *out);
38
41
  bool (*cross_compile_msl)(const std::vector<uint32_t> &spirv, bool fragment,
39
42
  std::string *source, std::string *entry, std::string *log);
43
+ /** Stage-explicit MSL cross-compile for WebGPU compute. */
44
+ bool (*cross_compile_msl_stage)(const std::vector<uint32_t> &spirv, int stage,
45
+ std::string *source, std::string *entry, std::string *log);
40
46
  };
41
47
 
42
48
  /** Install the table. Called by the `gpu` feature bridge before `main`. */
@@ -14,6 +14,7 @@
14
14
  #include "canvas.h"
15
15
  #include "document.h"
16
16
  #include "gui_engine.h"
17
+ #include "webgpu_api.h"
17
18
  #include "gui_internal.h"
18
19
  #include "perf.h"
19
20
 
@@ -90,6 +91,7 @@ void xt_gui_canvas_sync(XtGuiWindow *win) {
90
91
  std::vector<const xtgui::Node *> nodes;
91
92
  collectCanvases(win->document->root(), &nodes);
92
93
  const XtWebglApi *webgl = xt_webgl_api();
94
+ const XtWebgpuApi *webgpu = xt_webgpu_api();
93
95
  for (const xtgui::Node *node : nodes) {
94
96
  if (webgl != nullptr) {
95
97
  if (void *context = webgl->find(win, node)) {
@@ -101,6 +103,20 @@ void xt_gui_canvas_sync(XtGuiWindow *win) {
101
103
  continue;
102
104
  }
103
105
  }
106
+ /* A WebGPU context publishes its colour texture through the same canvas
107
+ * table: the paint pipeline samples that texture directly, so it needs a
108
+ * non-zero canvas id exactly like a WebGL or 2D canvas. */
109
+ if (webgpu != nullptr) {
110
+ uint32_t context = 0;
111
+ if (webgpu->canvas_find(win, node, &context)) {
112
+ float width = 0.0f;
113
+ float height = 0.0f;
114
+ uint64_t id = 0;
115
+ webgpu->info(context, &width, &height, &id);
116
+ win->document->setCanvasInfo(node, width, height, id);
117
+ continue;
118
+ }
119
+ }
104
120
  xtgui::Canvas2D *canvas = xt_gui_canvas_find(win, node);
105
121
  if (canvas != nullptr) {
106
122
  win->document->setCanvasInfo(node, (float)canvas->width(), (float)canvas->height(),
@@ -145,6 +161,8 @@ void xt_gui_canvas_shutdown(XtGuiWindow *win) {
145
161
  if (win == nullptr) return;
146
162
  const XtWebglApi *webgl = xt_webgl_api();
147
163
  if (webgl != nullptr) webgl->shutdown(win);
164
+ const XtWebgpuApi *webgpu = xt_webgpu_api();
165
+ if (webgpu != nullptr) webgpu->shutdown(win);
148
166
  for (auto &entry : win->canvases) releaseCanvasTexture(entry.second.get());
149
167
  win->canvases.clear();
150
168
  for (auto &entry : win->offscreen_canvases) releaseCanvasTexture(entry.second.get());
@@ -16,6 +16,7 @@
16
16
  #include "perf.h"
17
17
  #include "svg.h"
18
18
  #include "webgl_api.h"
19
+ #include "webgpu_api.h"
19
20
 
20
21
  #include "renderer_shaders.h"
21
22
 
@@ -158,6 +159,11 @@ static SDL_GPUTexture *ensureMediaTexture(SDL_GPUDevice *device, uint64_t media_
158
159
  * directly and nothing is copied through the CPU. */
159
160
  static SDL_GPUTexture *ensureCanvasTexture(SDL_GPUDevice *device, uint64_t canvas_id,
160
161
  XtGuiUploadBatch *batch) {
162
+ if (const XtWebgpuApi *webgpu = xt_webgpu_api()) {
163
+ if (void *texture = webgpu->color_texture(canvas_id)) {
164
+ return static_cast<SDL_GPUTexture *>(texture);
165
+ }
166
+ }
161
167
  if (const XtWebglApi *webgl = xt_webgl_api()) {
162
168
  if (void *texture = webgl->color_texture(canvas_id)) {
163
169
  return static_cast<SDL_GPUTexture *>(texture);
@@ -140,6 +140,14 @@ std::string rewrite_glsl(const std::string &source, bool fragment, const GlslLin
140
140
  bool compile_glsl(const std::string &source, bool fragment, std::vector<uint32_t> *spirv,
141
141
  std::string *log);
142
142
 
143
+ /**
144
+ * The same compiler for an explicit stage: 0 = vertex, 1 = fragment,
145
+ * 2 = compute. WebGL always passes vertex/fragment; WebGPU compute pipelines
146
+ * need the third.
147
+ */
148
+ bool compile_glsl_stage(const std::string &source, int stage, std::vector<uint32_t> *spirv,
149
+ std::string *log);
150
+
143
151
  /** One std140 block member, reflected from SPIR-V. */
144
152
  struct GlslBlockMember {
145
153
  std::string name;
@@ -340,18 +340,19 @@ void reflect_struct_members(const Module &module, uint32_t struct_id, int base,
340
340
 
341
341
  } // namespace
342
342
 
343
- bool compile_glsl(const std::string &source, bool fragment, std::vector<uint32_t> *spirv,
344
- std::string *log) {
343
+ bool compile_glsl_stage(const std::string &source, int stage_index, std::vector<uint32_t> *spirv,
344
+ std::string *log) {
345
345
  static const bool initialized = glslang::InitializeProcess();
346
346
  if (!initialized) {
347
347
  *log = "glslang initialization failed";
348
348
  return false;
349
349
  }
350
- const EShLanguage stage = fragment ? EShLangFragment : EShLangVertex;
350
+ EShLanguage stage = EShLangVertex;
351
+ if (stage_index == 1) stage = EShLangFragment;
352
+ else if (stage_index == 2) stage = EShLangCompute;
351
353
  glslang::TShader shader(stage);
352
354
  if (getenv("XT_WEBGL_DUMP_GLSL") != nullptr) {
353
- fprintf(stderr, "--- GLSL %s ---\n%s\n---\n", fragment ? "fragment" : "vertex",
354
- source.c_str());
355
+ fprintf(stderr, "--- GLSL stage %d ---\n%s\n---\n", stage_index, source.c_str());
355
356
  }
356
357
  const char *text = source.c_str();
357
358
  shader.setStrings(&text, 1);
@@ -376,6 +377,11 @@ bool compile_glsl(const std::string &source, bool fragment, std::vector<uint32_t
376
377
  return !spirv->empty();
377
378
  }
378
379
 
380
+ bool compile_glsl(const std::string &source, bool fragment, std::vector<uint32_t> *spirv,
381
+ std::string *log) {
382
+ return compile_glsl_stage(source, fragment ? 1 : 0, spirv, log);
383
+ }
384
+
379
385
  bool reflect_uniform_block(const std::vector<uint32_t> &spirv, std::vector<GlslBlockMember> *out) {
380
386
  out->clear();
381
387
  Module module;