xbintsc 0.6.12 → 0.6.17

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 (189) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/type-infer.js +1 -1
  3. package/dist/src/checker/type-infer.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/codegen/generator/calls/literals.js +4 -1
  6. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  7. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  8. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  10. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  12. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  13. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  14. package/dist/src/driver/bundler/commonjs.js +1 -1
  15. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  16. package/dist/src/driver/bundler/symbols.js +6 -0
  17. package/dist/src/driver/bundler/symbols.js.map +1 -1
  18. package/dist/src/driver/version.d.ts +1 -1
  19. package/dist/src/driver/version.js +1 -1
  20. package/dist/src/extensions/gui/features.d.ts +2 -0
  21. package/dist/src/extensions/gui/features.js +24 -0
  22. package/dist/src/extensions/gui/features.js.map +1 -1
  23. package/dist/src/extensions/gui/index.js +39 -0
  24. package/dist/src/extensions/gui/index.js.map +1 -1
  25. package/dist/src/extensions/node/index.js +2 -0
  26. package/dist/src/extensions/node/index.js.map +1 -1
  27. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  28. package/dist/src/extensions/node/readline/index.js +27 -0
  29. package/dist/src/extensions/node/readline/index.js.map +1 -0
  30. package/dist/src/parser/declarations/functions.js +6 -0
  31. package/dist/src/parser/declarations/functions.js.map +1 -1
  32. package/dist/src/parser/literals.js +1 -1
  33. package/dist/src/parser/literals.js.map +1 -1
  34. package/dist/tests/e2e/compile-modules.test.js +22 -0
  35. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  36. package/dist/tests/e2e/date-parse.test.js +17 -0
  37. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  38. package/dist/tests/e2e/gui-browser.test.js +61 -0
  39. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  40. package/dist/tests/e2e/gui-css-coverage.test.js +39 -0
  41. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  42. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  43. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-dom.test.js +34 -0
  45. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  46. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  47. package/dist/tests/e2e/gui-errors.test.js +68 -0
  48. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  49. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  50. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  51. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  52. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  53. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  54. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  55. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  56. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  57. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  58. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  59. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  60. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  61. package/dist/tests/e2e/harness.d.ts +4 -0
  62. package/dist/tests/e2e/harness.js +2 -0
  63. package/dist/tests/e2e/harness.js.map +1 -1
  64. package/dist/tests/e2e/readline.test.d.ts +7 -0
  65. package/dist/tests/e2e/readline.test.js +73 -0
  66. package/dist/tests/e2e/readline.test.js.map +1 -0
  67. package/dist/tests/extensions/gui-features.test.js +18 -1
  68. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  69. package/doc/ai/extensions.md +7 -1
  70. package/doc/ai/zh-CN/extensions.md +6 -1
  71. package/doc/gui/api.md +15 -0
  72. package/doc/gui/dom.md +7 -2
  73. package/doc/gui/known-issues.md +2 -1
  74. package/doc/gui/layout.md +5 -1
  75. package/doc/gui/webgpu.md +121 -0
  76. package/doc/gui.md +12 -1
  77. package/doc/implemented/node/readline.md +34 -0
  78. package/doc/implemented/node.md +1 -0
  79. package/doc/unimplemented/node/modules.md +2 -3
  80. package/doc/unimplemented/node.md +2 -2
  81. package/doc/zh-CN/gui/api.md +11 -0
  82. package/doc/zh-CN/gui/dom.md +6 -2
  83. package/doc/zh-CN/gui/known-issues.md +2 -1
  84. package/doc/zh-CN/gui/layout.md +4 -1
  85. package/doc/zh-CN/gui/webgpu.md +111 -0
  86. package/doc/zh-CN/gui.md +10 -1
  87. package/doc/zh-CN/implemented/node/readline.md +33 -0
  88. package/doc/zh-CN/implemented/node.md +1 -0
  89. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  90. package/doc/zh-CN/unimplemented/node.md +2 -2
  91. package/llms.txt +1 -0
  92. package/package.json +7 -7
  93. package/runtime/ext_gui/browser.cpp +10 -0
  94. package/runtime/ext_gui/browser_abort.cpp +2 -2
  95. package/runtime/ext_gui/browser_loop.cpp +1 -0
  96. package/runtime/ext_gui/browser_media.cpp +1 -1
  97. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  98. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  99. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  100. package/runtime/ext_gui/dom_api_events.cpp +1 -1
  101. package/runtime/ext_gui/dom_api_internal.h +2 -0
  102. package/runtime/ext_gui/dom_api_node.cpp +18 -12
  103. package/runtime/ext_gui/dom_api_node_read.cpp +27 -0
  104. package/runtime/ext_gui/dom_api_proto.cpp +2 -0
  105. package/runtime/ext_gui/error_report.cpp +166 -0
  106. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  107. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  108. package/runtime/ext_gui/gui.cpp +16 -0
  109. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  110. package/runtime/ext_gui/gui_engine.h +13 -0
  111. package/runtime/ext_gui/gui_events.cpp +4 -1
  112. package/runtime/ext_gui/layout_inline.cpp +46 -14
  113. package/runtime/ext_gui/paint.cpp +22 -0
  114. package/runtime/ext_gui/renderer_image.cpp +6 -0
  115. package/runtime/ext_gui/script.cpp +1 -1
  116. package/runtime/ext_gui/style.cpp +1 -0
  117. package/runtime/ext_gui/style.h +13 -0
  118. package/runtime/ext_gui/style_print.cpp +99 -0
  119. package/runtime/ext_gui/style_text.cpp +79 -0
  120. package/runtime/ext_gui/webgl_glsl.h +8 -0
  121. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  122. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  123. package/runtime/ext_gui/webgl_msl.h +4 -0
  124. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  125. package/runtime/ext_gui/webgpu.h +371 -0
  126. package/runtime/ext_gui/webgpu_api.h +42 -0
  127. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  128. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  129. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  130. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  131. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  132. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  133. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  134. package/runtime/ext_gui/webgpu_convert.cpp +309 -0
  135. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  136. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  137. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  138. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  139. package/runtime/ext_gui/webgpu_pass.h +33 -0
  140. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  141. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  142. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  143. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  144. package/runtime/ext_gui/webgpu_shader.cpp +242 -0
  145. package/runtime/ext_gui/wgsl.h +99 -0
  146. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  147. package/runtime/ext_gui/wgsl_ast.h +233 -0
  148. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  149. package/runtime/ext_gui/wgsl_emit.h +128 -0
  150. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  151. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  152. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  153. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  154. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  155. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  156. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  157. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  158. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  159. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  160. package/runtime/ext_gui/wgsl_parser.h +77 -0
  161. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  162. package/runtime/ext_gui/window.cpp +22 -8
  163. package/runtime/ext_gui/window_emit.cpp +1 -1
  164. package/runtime/ext_node/fs/parts/platform.inc +2 -0
  165. package/runtime/ext_node/readline/readline.c +572 -0
  166. package/runtime/rt.h +15 -0
  167. package/runtime/rt_internal.h +3 -2
  168. package/runtime/xt_io/exception.inc +29 -6
  169. package/runtime/xt_loop.c +36 -1
  170. package/runtime/xt_promise.c +26 -2
  171. package/runtime/xt_stdlib2/date.inc +2 -2
  172. package/runtime/xt_typed_array/elements.inc +0 -9
  173. package/runtime/xt_url.c +0 -6
  174. package/runtime/xt_url_object.c +1 -1
  175. package/src/ast/expressions.ts +6 -0
  176. package/src/checker/type-infer.ts +1 -1
  177. package/src/codegen/generator/calls/literals.ts +4 -1
  178. package/src/codegen/generator/expressions/assignment.ts +3 -1
  179. package/src/codegen/generator/expressions/operators.ts +2 -6
  180. package/src/codegen/generator/expressions/primary.ts +58 -6
  181. package/src/driver/bundler/commonjs.ts +1 -1
  182. package/src/driver/bundler/symbols.ts +6 -0
  183. package/src/driver/version.ts +1 -1
  184. package/src/extensions/gui/features.ts +24 -0
  185. package/src/extensions/gui/index.ts +39 -0
  186. package/src/extensions/node/index.ts +2 -0
  187. package/src/extensions/node/readline/index.ts +31 -0
  188. package/src/parser/declarations/functions.ts +6 -0
  189. package/src/parser/literals.ts +1 -1
@@ -0,0 +1,315 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGPU buffer, texture, sampler, shader-module, bind
3
+ * group, command-encoder and query-set creation.
4
+ *
5
+ * These are `GPUDevice` factory methods; they live here rather than in
6
+ * `webgpu_device.cpp` so each unit stays within the repository's size budget.
7
+ */
8
+
9
+ #include <cstring>
10
+
11
+ #include "webgpu_bindings.h"
12
+ #include "wgsl.h"
13
+
14
+ namespace xtgui {
15
+ namespace webgpu {
16
+ namespace {
17
+
18
+ Device &device(Object &object) { return static_cast<Device &>(object); }
19
+
20
+ uint32_t sdl_buffer_usage(uint32_t usage) {
21
+ uint32_t flags = 0;
22
+ if ((usage & 32) != 0) flags |= SDL_GPU_BUFFERUSAGE_VERTEX;
23
+ if ((usage & 16) != 0) flags |= SDL_GPU_BUFFERUSAGE_INDEX;
24
+ if ((usage & 256) != 0) flags |= SDL_GPU_BUFFERUSAGE_INDIRECT;
25
+ if ((usage & 128) != 0) {
26
+ flags |= SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ |
27
+ SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ |
28
+ SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE;
29
+ }
30
+ return flags;
31
+ }
32
+
33
+ uint32_t sdl_texture_usage(uint32_t usage, SDL_GPUTextureFormat format) {
34
+ return Convert::texture_usage(usage, format);
35
+ }
36
+
37
+ SDL_GPUTextureType texture_type(const std::string &dimension, uint32_t depth) {
38
+ if (dimension == "3d") return SDL_GPU_TEXTURETYPE_3D;
39
+ if (dimension == "cube") return SDL_GPU_TEXTURETYPE_CUBE;
40
+ if (dimension == "cube-array") return SDL_GPU_TEXTURETYPE_CUBE_ARRAY;
41
+ if (depth > 1) return SDL_GPU_TEXTURETYPE_2D_ARRAY;
42
+ return SDL_GPU_TEXTURETYPE_2D;
43
+ }
44
+ xt_value device_create_buffer(Object &object, int argc, xt_value *argv) {
45
+ Device &dev = device(object);
46
+ xt_value descriptor = arg_object(argc, argv, 0);
47
+ if (!XT_IS_OBJECT(descriptor)) {
48
+ throw_type_error("createBuffer needs a descriptor");
49
+ return XT_UNDEFINED;
50
+ }
51
+ const uint64_t size = prop_u64(descriptor, "size", 0);
52
+ const uint32_t usage = (uint32_t)prop_u64(descriptor, "usage", 0);
53
+ if (size == 0) return validation(dev, "createBuffer: size must be greater than 0");
54
+ if (usage == 0) return validation(dev, "createBuffer: usage must not be empty");
55
+ auto buffer = std::make_shared<Buffer>();
56
+ buffer->kind = Kind::Buffer;
57
+ buffer->device_id = dev.id;
58
+ buffer->size = size;
59
+ buffer->usage = usage;
60
+ const bool mapped_at_creation = prop_bool(descriptor, "mappedAtCreation", false);
61
+
62
+ uint32_t flags = sdl_buffer_usage(usage);
63
+ const bool needs_gpu = flags != 0 || (usage & 1) != 0 || (usage & 512) != 0;
64
+ if (needs_gpu) {
65
+ if (flags == 0) flags = SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ;
66
+ SDL_GPUBufferCreateInfo info = {};
67
+ info.usage = flags;
68
+ info.size = (Uint32)size;
69
+ buffer->gpu = SDL_CreateGPUBuffer(gpu_device(), &info);
70
+ if (buffer->gpu == nullptr) {
71
+ return validation(dev, std::string("createBuffer failed: ") + SDL_GetError());
72
+ }
73
+ buffer->has_gpu = true;
74
+ }
75
+ if ((usage & 64) != 0 || mapped_at_creation || (usage & 1) != 0) {
76
+ buffer->has_shadow = true;
77
+ buffer->shadow.assign((size_t)size, 0);
78
+ }
79
+ if (mapped_at_creation) {
80
+ buffer->mapped = buffer->shadow;
81
+ buffer->map_state = 2;
82
+ }
83
+ return handle_of(add_object(buffer));
84
+ }
85
+
86
+ xt_value device_create_texture(Object &object, int argc, xt_value *argv) {
87
+ Device &dev = device(object);
88
+ xt_value descriptor = arg_object(argc, argv, 0);
89
+ if (!XT_IS_OBJECT(descriptor)) {
90
+ throw_type_error("createTexture needs a descriptor");
91
+ return XT_UNDEFINED;
92
+ }
93
+ xt_value size_value = prop_object(descriptor, "size");
94
+ const uint32_t width = (uint32_t)prop_u64(size_value, "width", 1);
95
+ const uint32_t height = (uint32_t)prop_u64(size_value, "height", 1);
96
+ const uint32_t depth = (uint32_t)prop_u64(size_value, "depthOrArrayLayers", 1);
97
+ const std::string format_name = prop_string(descriptor, "format");
98
+ const uint32_t usage = (uint32_t)prop_u64(descriptor, "usage", 0);
99
+ SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
100
+ if (!Convert::texture_format(format_name, &format)) {
101
+ return validation(dev, "createTexture: unsupported format '" + format_name + "'");
102
+ }
103
+ if (width == 0 || height == 0) return validation(dev, "createTexture: size must not be zero");
104
+ auto texture = std::make_shared<Texture>();
105
+ texture->kind = Kind::Texture;
106
+ texture->device_id = dev.id;
107
+ texture->format = format;
108
+ texture->width = width;
109
+ texture->height = height;
110
+ texture->depth = depth;
111
+ texture->mips = (uint32_t)prop_u64(descriptor, "mipLevelCount", 1);
112
+ texture->samples = (uint32_t)prop_u64(descriptor, "sampleCount", 1);
113
+ texture->usage = usage;
114
+ texture->dimension = prop_string(descriptor, "dimension");
115
+ if (texture->dimension.empty()) texture->dimension = "2d";
116
+
117
+ SDL_GPUTextureCreateInfo info = {};
118
+ info.type = texture_type(texture->dimension, depth);
119
+ info.format = format;
120
+ info.usage = sdl_texture_usage(usage, format);
121
+ if (info.usage == 0) info.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER;
122
+ info.width = width;
123
+ info.height = height;
124
+ info.layer_count_or_depth = depth;
125
+ info.num_levels = texture->mips;
126
+ info.sample_count = texture->samples >= 8 ? SDL_GPU_SAMPLECOUNT_8
127
+ : texture->samples >= 4 ? SDL_GPU_SAMPLECOUNT_4
128
+ : texture->samples >= 2 ? SDL_GPU_SAMPLECOUNT_2
129
+ : SDL_GPU_SAMPLECOUNT_1;
130
+ texture->gpu = SDL_CreateGPUTexture(gpu_device(), &info);
131
+ if (texture->gpu == nullptr) {
132
+ return validation(dev, std::string("createTexture failed: ") + SDL_GetError());
133
+ }
134
+ return handle_of(add_object(texture));
135
+ }
136
+
137
+ xt_value device_create_sampler(Object &object, int argc, xt_value *argv) {
138
+ Device &dev = device(object);
139
+ xt_value descriptor = arg_object(argc, argv, 0);
140
+ if (!XT_IS_OBJECT(descriptor)) {
141
+ throw_type_error("createSampler needs a descriptor");
142
+ return XT_UNDEFINED;
143
+ }
144
+ SDL_GPUSamplerCreateInfo info = {};
145
+ info.min_filter = Convert::filter(prop_string(descriptor, "minFilter"));
146
+ info.mag_filter = Convert::filter(prop_string(descriptor, "magFilter"));
147
+ info.mipmap_mode = Convert::mipmap_mode(prop_string(descriptor, "mipmapFilter"));
148
+ info.address_mode_u = Convert::address_mode(prop_string(descriptor, "addressModeU"));
149
+ info.address_mode_v = Convert::address_mode(prop_string(descriptor, "addressModeV"));
150
+ info.address_mode_w = Convert::address_mode(prop_string(descriptor, "addressModeW"));
151
+ info.mip_lod_bias = 0.0f;
152
+ info.min_lod = (float)prop_number(descriptor, "lodMinClamp", 0.0);
153
+ info.max_lod = (float)prop_number(descriptor, "lodMaxClamp", 1000.0);
154
+ info.max_anisotropy = (float)prop_number(descriptor, "maxAnisotropy", 1.0);
155
+ info.enable_anisotropy = info.max_anisotropy > 1.0f;
156
+ info.compare_op = Convert::compare(prop_string(descriptor, "compare"));
157
+ info.enable_compare = !prop_string(descriptor, "compare").empty();
158
+ auto sampler = std::make_shared<Sampler>();
159
+ sampler->kind = Kind::Sampler;
160
+ sampler->device_id = dev.id;
161
+ sampler->compare = info.enable_compare;
162
+ sampler->gpu = SDL_CreateGPUSampler(gpu_device(), &info);
163
+ if (sampler->gpu == nullptr) {
164
+ return validation(dev, std::string("createSampler failed: ") + SDL_GetError());
165
+ }
166
+ return handle_of(add_object(sampler));
167
+ }
168
+
169
+ xt_value device_create_shader_module(Object &object, int argc, xt_value *argv) {
170
+ Device &dev = device(object);
171
+ xt_value descriptor = arg_object(argc, argv, 0);
172
+ if (!XT_IS_OBJECT(descriptor)) {
173
+ throw_type_error("createShaderModule needs a descriptor");
174
+ return XT_UNDEFINED;
175
+ }
176
+ auto module = std::make_shared<ShaderModule>();
177
+ module->kind = Kind::ShaderModule;
178
+ module->device_id = dev.id;
179
+ module->source = prop_string(descriptor, "code");
180
+ if (module->source.empty()) return validation(dev, "createShaderModule: code must not be empty");
181
+ wgsl::CompileInfo info;
182
+ if (!wgsl::check(module->source, &info)) {
183
+ module->valid = false;
184
+ module->error = info.message;
185
+ module->error_line = info.line;
186
+ module->error_column = info.column;
187
+ record_error(dev, ErrorKind::Validation, info.message);
188
+ }
189
+ return handle_of(add_object(module));
190
+ }
191
+
192
+ xt_value device_create_bind_group_layout(Object &object, int argc, xt_value *argv) {
193
+ Device &dev = device(object);
194
+ xt_value descriptor = arg_object(argc, argv, 0);
195
+ if (!XT_IS_OBJECT(descriptor)) {
196
+ throw_type_error("createBindGroupLayout needs a descriptor");
197
+ return XT_UNDEFINED;
198
+ }
199
+ auto layout = std::make_shared<BindGroupLayout>();
200
+ layout->kind = Kind::BindGroupLayout;
201
+ layout->device_id = dev.id;
202
+ std::string error;
203
+ if (!build_bind_group_layout(dev, descriptor, layout, &error)) {
204
+ return validation(dev, error);
205
+ }
206
+ return handle_of(add_object(layout));
207
+ }
208
+
209
+ xt_value device_create_pipeline_layout(Object &object, int argc, xt_value *argv) {
210
+ Device &dev = device(object);
211
+ xt_value descriptor = arg_object(argc, argv, 0);
212
+ if (!XT_IS_OBJECT(descriptor)) {
213
+ throw_type_error("createPipelineLayout needs a descriptor");
214
+ return XT_UNDEFINED;
215
+ }
216
+ xt_value layouts = prop_object(descriptor, "bindGroupLayouts");
217
+ auto pipeline_layout = std::make_shared<PipelineLayout>();
218
+ pipeline_layout->kind = Kind::PipelineLayout;
219
+ pipeline_layout->device_id = dev.id;
220
+ if (XT_IS_ARRAY(layouts)) {
221
+ const uint32_t count = (uint32_t)xt_to_number(xt_array_length(layouts));
222
+ for (uint32_t i = 0; i < count; i++) {
223
+ xt_value item = xt_array_get(layouts, xt_number((double)i));
224
+ const uint32_t id = (uint32_t)xt_to_number(xt_object_get_cstr(item, "__xt_webgpu_id"));
225
+ std::shared_ptr<Object> layout = get_object(id, Kind::BindGroupLayout);
226
+ if (layout == nullptr) return validation(dev, "createPipelineLayout: invalid layout");
227
+ pipeline_layout->layouts.push_back(id);
228
+ }
229
+ }
230
+ return handle_of(add_object(pipeline_layout));
231
+ }
232
+
233
+ xt_value device_create_bind_group(Object &object, int argc, xt_value *argv) {
234
+ Device &dev = device(object);
235
+ xt_value descriptor = arg_object(argc, argv, 0);
236
+ if (!XT_IS_OBJECT(descriptor)) {
237
+ throw_type_error("createBindGroup needs a descriptor");
238
+ return XT_UNDEFINED;
239
+ }
240
+ auto group = std::make_shared<BindGroup>();
241
+ group->kind = Kind::BindGroup;
242
+ group->device_id = dev.id;
243
+ xt_value layout_value = prop_object(descriptor, "layout");
244
+ if (XT_IS_OBJECT(layout_value)) {
245
+ group->layout =
246
+ (uint32_t)xt_to_number(xt_object_get_cstr(layout_value, "__xt_webgpu_id"));
247
+ }
248
+ std::string error;
249
+ if (!parse_bind_group(dev, descriptor, group.get(), &error)) {
250
+ return validation(dev, error);
251
+ }
252
+ return handle_of(add_object(group));
253
+ }
254
+
255
+ xt_value device_create_command_encoder(Object &object, int argc, xt_value *argv) {
256
+ (void)argc;
257
+ (void)argv;
258
+ Device &dev = device(object);
259
+ auto encoder = std::make_shared<CommandEncoder>();
260
+ encoder->kind = Kind::CommandEncoder;
261
+ encoder->device_id = dev.id;
262
+ return handle_of(add_object(encoder));
263
+ }
264
+
265
+ xt_value device_create_query_set(Object &object, int argc, xt_value *argv) {
266
+ Device &dev = device(object);
267
+ xt_value descriptor = arg_object(argc, argv, 0);
268
+ if (!XT_IS_OBJECT(descriptor)) {
269
+ throw_type_error("createQuerySet needs a descriptor");
270
+ return XT_UNDEFINED;
271
+ }
272
+ auto query = std::make_shared<QuerySet>();
273
+ query->kind = Kind::QuerySet;
274
+ query->device_id = dev.id;
275
+ query->type = prop_string(descriptor, "type") == "timestamp" ? 1 : 0;
276
+ query->count = (uint32_t)prop_u64(descriptor, "count", 1);
277
+ return handle_of(add_object(query));
278
+ }
279
+
280
+ xt_value device_import_external_texture(Object &object, int argc, xt_value *argv) {
281
+ Device &dev = device(object);
282
+ (void)argc;
283
+ (void)argv;
284
+ return validation(dev, "importExternalTexture: external image sources are not supported");
285
+ }
286
+
287
+ xt_value device_create_render_bundle_encoder(Object &object, int argc, xt_value *argv) {
288
+ (void)argc;
289
+ (void)argv;
290
+ Device &dev = device(object);
291
+ auto bundle = std::make_shared<RenderBundleEncoder>();
292
+ bundle->kind = Kind::RenderBundleEncoder;
293
+ bundle->device_id = dev.id;
294
+ return handle_of(add_object(bundle));
295
+ }
296
+
297
+
298
+ } // namespace
299
+
300
+ void register_resource_methods() {
301
+ add_method(Kind::Device, "createBuffer", device_create_buffer);
302
+ add_method(Kind::Device, "createTexture", device_create_texture);
303
+ add_method(Kind::Device, "createSampler", device_create_sampler);
304
+ add_method(Kind::Device, "createShaderModule", device_create_shader_module);
305
+ add_method(Kind::Device, "createBindGroupLayout", device_create_bind_group_layout);
306
+ add_method(Kind::Device, "createPipelineLayout", device_create_pipeline_layout);
307
+ add_method(Kind::Device, "createBindGroup", device_create_bind_group);
308
+ add_method(Kind::Device, "createCommandEncoder", device_create_command_encoder);
309
+ add_method(Kind::Device, "createQuerySet", device_create_query_set);
310
+ add_method(Kind::Device, "createRenderBundleEncoder", device_create_render_bundle_encoder);
311
+ add_method(Kind::Device, "importExternalTexture", device_import_external_texture);
312
+ }
313
+
314
+ } // namespace webgpu
315
+ } // namespace xtgui
@@ -0,0 +1,242 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGPU shader compilation and bind-group layouts.
3
+ *
4
+ * WGSL is translated to GLSL first (see `wgsl_*.cpp`); this unit turns that
5
+ * GLSL into the bytecode the SDL_GPU backend consumes (SPIR-V on Vulkan, MSL
6
+ * on Metal) and derives the SDL shader's resource counts from the translated
7
+ * binding list.
8
+ */
9
+
10
+ #include "webgpu.h"
11
+
12
+ #include <algorithm>
13
+ #include <cstring>
14
+
15
+ #include "gpu_shader_api.h"
16
+
17
+ namespace xtgui {
18
+ namespace webgpu {
19
+ namespace {
20
+
21
+ std::string stage_key(int stage, const std::string &entry) {
22
+ return std::to_string(stage) + ":" + entry;
23
+ }
24
+
25
+ const char *stage_glsl(int stage) {
26
+ if (stage == 1) return "fragment";
27
+ if (stage == 2) return "compute";
28
+ return "vertex";
29
+ }
30
+
31
+ bool build_stage_code(Device &device, ShaderModule &module, const std::string &entry, int stage,
32
+ StageResources *resources, bool create_shader, std::string *error) {
33
+ if (!module.valid) {
34
+ *error = module.error;
35
+ return false;
36
+ }
37
+ std::string key = stage_key(stage, entry);
38
+ wgsl::StageCode code;
39
+ auto cached = module.stages.find(key);
40
+ if (cached != module.stages.end()) {
41
+ code = cached->second;
42
+ } else {
43
+ wgsl::CompileInfo info;
44
+ if (!wgsl::translate(module.source, entry, (wgsl::Stage)stage, &code, &info)) {
45
+ *error = info.message;
46
+ return false;
47
+ }
48
+ module.stages[key] = code;
49
+ }
50
+
51
+ const GpuShaderApi *api = gpu_shader_api();
52
+ if (api == nullptr) {
53
+ *error = "shader translation is not linked (the 'gpu' link feature is inactive)";
54
+ return false;
55
+ }
56
+ std::vector<uint32_t> spirv;
57
+ std::string log;
58
+ if (!api->compile_glsl_stage(code.glsl, stage, &spirv, &log)) {
59
+ *error = std::string("GLSL (") + stage_glsl(stage) + ") compilation failed: " + log;
60
+ return false;
61
+ }
62
+ resources->code = code;
63
+ resources->spirv = spirv;
64
+ resources->entry = "main";
65
+ resources->msl.clear();
66
+
67
+ const SDL_GPUShaderFormat formats = SDL_GetGPUShaderFormats(gpu_device());
68
+ if ((formats & SDL_GPU_SHADERFORMAT_MSL) != 0) {
69
+ std::string source;
70
+ std::string msl_entry;
71
+ if (!api->cross_compile_msl_stage(spirv, stage, &source, &msl_entry, &log)) {
72
+ *error = "MSL cross-compilation failed: " + log;
73
+ return false;
74
+ }
75
+ resources->msl = std::move(source);
76
+ resources->entry = msl_entry;
77
+ resources->format = SDL_GPU_SHADERFORMAT_MSL;
78
+ } else if ((formats & SDL_GPU_SHADERFORMAT_SPIRV) != 0) {
79
+ resources->format = SDL_GPU_SHADERFORMAT_SPIRV;
80
+ } else {
81
+ *error = "this GPU backend consumes neither Metal nor Vulkan shaders (Direct3D 12 is not supported)";
82
+ return false;
83
+ }
84
+
85
+ if (!create_shader) return true;
86
+ if (resources->format != SDL_GPU_SHADERFORMAT_SPIRV) {
87
+ *error = "graphics pipelines need SPIR-V on this backend";
88
+ return false;
89
+ }
90
+ SDL_GPUShaderCreateInfo info = {};
91
+ info.code = reinterpret_cast<const Uint8 *>(resources->spirv.data());
92
+ info.code_size = resources->spirv.size() * sizeof(uint32_t);
93
+ info.entrypoint = resources->entry.c_str();
94
+ info.format = resources->format;
95
+ info.stage = stage == 1 ? SDL_GPU_SHADERSTAGE_FRAGMENT : SDL_GPU_SHADERSTAGE_VERTEX;
96
+ info.num_samplers = (Uint32)code.samplers.size();
97
+ info.num_storage_textures = (Uint32)code.storage_textures.size();
98
+ info.num_storage_buffers = (Uint32)code.storage_buffers.size();
99
+ info.num_uniform_buffers = (Uint32)code.uniform_buffers.size();
100
+ resources->shader = SDL_CreateGPUShader(gpu_device(), &info);
101
+ if (resources->shader == nullptr) {
102
+ *error = std::string("SDL_CreateGPUShader failed: ") + SDL_GetError();
103
+ return false;
104
+ }
105
+ (void)device;
106
+ return true;
107
+ }
108
+
109
+ int stage_visibility(int stage) { return stage == 1 ? 2 : (stage == 2 ? 4 : 1); }
110
+
111
+ } // namespace
112
+
113
+ bool translate_stage(Device &device, ShaderModule &module, const std::string &entry, int stage,
114
+ wgsl::StageCode *code, std::string *error) {
115
+ (void)device;
116
+ if (!module.valid) {
117
+ *error = module.error;
118
+ return false;
119
+ }
120
+ const std::string key = stage_key(stage, entry);
121
+ auto cached = module.stages.find(key);
122
+ if (cached != module.stages.end()) {
123
+ *code = cached->second;
124
+ return true;
125
+ }
126
+ wgsl::CompileInfo info;
127
+ if (!wgsl::translate(module.source, entry, (wgsl::Stage)stage, code, &info)) {
128
+ *error = info.message;
129
+ return false;
130
+ }
131
+ module.stages[key] = *code;
132
+ return true;
133
+ }
134
+
135
+ bool compile_shader(Device &device, ShaderModule &module, const std::string &entry, int stage,
136
+ StageResources *resources, std::string *error) {
137
+ return build_stage_code(device, module, entry, stage, resources, true, error);
138
+ }
139
+
140
+ bool compile_shader_code(Device &device, ShaderModule &module, const std::string &entry, int stage,
141
+ StageResources *resources, std::string *error) {
142
+ return build_stage_code(device, module, entry, stage, resources, false, error);
143
+ }
144
+
145
+ std::vector<uint32_t> make_auto_layouts(
146
+ Device &device, const std::vector<std::pair<int, wgsl::StageCode>> &stages) {
147
+ struct Entry {
148
+ LayoutEntry layout;
149
+ bool set = false;
150
+ };
151
+ std::map<int, std::map<int, Entry>> groups;
152
+ int max_group = 0;
153
+ auto add = [&](int stage, int group, int binding, const char *type, const char *buffer_type) {
154
+ if (group < 0) group = 0;
155
+ if (group > max_group) max_group = group;
156
+ Entry &entry = groups[group][binding];
157
+ entry.set = true;
158
+ entry.layout.binding = binding;
159
+ entry.layout.visibility |= stage_visibility(stage);
160
+ entry.layout.type = type;
161
+ if (buffer_type != nullptr) entry.layout.buffer_type = buffer_type;
162
+ };
163
+ for (const auto &stage : stages) {
164
+ const int index = stage.first;
165
+ const wgsl::StageCode &code = stage.second;
166
+ for (const wgsl::TextureSlot &slot : code.samplers) {
167
+ add(index, slot.texture.group, slot.texture.binding, "texture", nullptr);
168
+ if (slot.has_sampler) add(index, slot.sampler.group, slot.sampler.binding, "sampler", nullptr);
169
+ }
170
+ for (const wgsl::Binding &binding : code.storage_textures) {
171
+ add(index, binding.group, binding.binding, "storage-texture", nullptr);
172
+ }
173
+ for (const wgsl::Binding &binding : code.write_storage_textures) {
174
+ add(index, binding.group, binding.binding, "storage-texture", nullptr);
175
+ }
176
+ for (const wgsl::Binding &binding : code.storage_buffers) {
177
+ add(index, binding.group, binding.binding, "buffer", "read-only-storage");
178
+ }
179
+ for (const wgsl::Binding &binding : code.write_storage_buffers) {
180
+ add(index, binding.group, binding.binding, "buffer", "storage");
181
+ }
182
+ for (const wgsl::Binding &binding : code.uniform_buffers) {
183
+ add(index, binding.group, binding.binding, "buffer", "uniform");
184
+ }
185
+ }
186
+ std::vector<uint32_t> ids;
187
+ for (int group = 0; group <= max_group; group++) {
188
+ auto layout = std::make_shared<BindGroupLayout>();
189
+ layout->kind = Kind::BindGroupLayout;
190
+ layout->device_id = device.id;
191
+ auto found = groups.find(group);
192
+ if (found != groups.end()) {
193
+ for (const auto &entry : found->second) {
194
+ if (entry.second.set) layout->entries.push_back(entry.second.layout);
195
+ }
196
+ }
197
+ ids.push_back(add_object(layout));
198
+ }
199
+ return ids;
200
+ }
201
+ bool build_bind_group_layout(Device &device, xt_value descriptor,
202
+ const std::shared_ptr<BindGroupLayout> &layout, std::string *error) {
203
+ (void)device;
204
+ xt_value entries = prop_object(descriptor, "entries");
205
+ if (!XT_IS_ARRAY(entries)) {
206
+ *error = "layout.entries must be an array";
207
+ return false;
208
+ }
209
+ const int count = (int)xt_to_number(xt_array_length(entries));
210
+ for (int i = 0; i < count; i++) {
211
+ xt_value item = xt_get(entries, xt_number((double)i));
212
+ if (!XT_IS_OBJECT(item)) continue;
213
+ LayoutEntry entry;
214
+ entry.binding = (int)prop_u64(item, "binding", 0);
215
+ entry.visibility = (int)prop_number(item, "visibility", 7);
216
+ xt_value buffer = xt_object_get_cstr(item, "buffer");
217
+ xt_value texture = xt_object_get_cstr(item, "texture");
218
+ xt_value storage = xt_object_get_cstr(item, "storageTexture");
219
+ xt_value sampler = xt_object_get_cstr(item, "sampler");
220
+ if (XT_IS_OBJECT(buffer)) {
221
+ entry.type = "buffer";
222
+ entry.buffer_type = prop_string(buffer, "type");
223
+ if (entry.buffer_type.empty()) entry.buffer_type = "uniform";
224
+ entry.has_dynamic_offset = prop_bool(buffer, "hasDynamicOffset", false);
225
+ entry.min_binding_size = prop_u64(buffer, "minBindingSize", 0);
226
+ } else if (XT_IS_OBJECT(storage)) {
227
+ entry.type = "storage-texture";
228
+ } else if (XT_IS_OBJECT(texture)) {
229
+ entry.type = "texture";
230
+ } else if (XT_IS_OBJECT(sampler)) {
231
+ entry.type = "sampler";
232
+ } else {
233
+ *error = "layout entry " + std::to_string(i) + " has no resource type";
234
+ return false;
235
+ }
236
+ layout->entries.push_back(entry);
237
+ }
238
+ return true;
239
+ }
240
+
241
+ } // namespace webgpu
242
+ } // namespace xtgui
@@ -0,0 +1,99 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL front end for WebGPU.
3
+ *
4
+ * WebGPU programs supply WGSL at run time. The engine has exactly one shader
5
+ * compiler (glslang, behind the optional `gpu` link feature) and it consumes
6
+ * GLSL, so this module translates the WGSL subset WebGPU programs use into
7
+ * Vulkan-dialect GLSL ES, and then reuses the WebGL path: glslang produces
8
+ * SPIR-V for Vulkan and SPIRV-Cross produces MSL for Metal.
9
+ *
10
+ * A translation is requested per *entry point*, because a WGSL module may hold
11
+ * the vertex and fragment stages (and several entry points) at once while a
12
+ * graphics pipeline consumes one pair. The result also reports where the
13
+ * module's resources live, in the slot order SDL_GPU binds them, so the
14
+ * pipeline can bind a WebGPU bind group without re-reflecting the SPIR-V.
15
+ */
16
+ #ifndef XT_GUI_WGSL_H
17
+ #define XT_GUI_WGSL_H
18
+
19
+ #include <cstdint>
20
+ #include <string>
21
+ #include <vector>
22
+
23
+ namespace xtgui {
24
+ namespace wgsl {
25
+
26
+ enum class Stage { Vertex = 0, Fragment = 1, Compute = 2 };
27
+
28
+ /** A resource reference as it appeared in WGSL: `@group(g) @binding(b)`. */
29
+ struct Binding {
30
+ int group = 0;
31
+ int binding = 0;
32
+ bool operator==(const Binding &other) const {
33
+ return group == other.group && binding == other.binding;
34
+ }
35
+ };
36
+
37
+ /**
38
+ * One SDL_GPU sampler slot. WGSL declares textures and samplers separately,
39
+ * but SDL_GPU binds them as pairs, so the translator pairs every texture with
40
+ * each sampler it is sampled with (`textureSample(t, s, …)`). `has_sampler` is
41
+ * false for `textureLoad`-only textures, which SDL still needs a sampler for;
42
+ * the runtime supplies a default one.
43
+ */
44
+ struct TextureSlot {
45
+ Binding texture;
46
+ bool has_sampler = false;
47
+ Binding sampler;
48
+ };
49
+
50
+ /** The translated GLSL for one entry point plus the bindings it uses. */
51
+ struct StageCode {
52
+ std::string glsl;
53
+ /** Name of the GLSL entry point (`main`). */
54
+ std::string entry = "main";
55
+
56
+ /** SDL sampler slots, in binding order. */
57
+ std::vector<TextureSlot> samplers;
58
+ /** Read-only storage textures, in SDL binding order. */
59
+ std::vector<Binding> storage_textures;
60
+ /** Read-only storage buffers, in SDL binding order. */
61
+ std::vector<Binding> storage_buffers;
62
+ /** Write storage textures (compute only). */
63
+ std::vector<Binding> write_storage_textures;
64
+ /** Read-write storage buffers (compute only). */
65
+ std::vector<Binding> write_storage_buffers;
66
+ /** Uniform buffers, in SDL binding order (at most 4 per stage). */
67
+ std::vector<Binding> uniform_buffers;
68
+
69
+ /** WGSL `@workgroup_size`, for compute pipelines; 0 for other stages. */
70
+ int workgroup_size[3] = {0, 0, 0};
71
+ };
72
+
73
+ /** Set by a failed translation; the text is shown by `getCompilationInfo`. */
74
+ struct CompileInfo {
75
+ std::string message;
76
+ int line = 0;
77
+ int column = 0;
78
+ };
79
+
80
+ /**
81
+ * Translate `entry_point` of a WGSL `source` for `stage` into GLSL.
82
+ *
83
+ * Returns false and fills `info` when the source does not parse, the entry
84
+ * point is missing, or it is declared for another stage. The caller reports
85
+ * the message through `GPUCompilationMessage`.
86
+ */
87
+ bool translate(const std::string &source, const std::string &entry_point, Stage stage,
88
+ StageCode *out, CompileInfo *info);
89
+
90
+ /**
91
+ * Check that `source` parses at all (used by `getCompilationInfo`); every
92
+ * entry point is validated but nothing is emitted.
93
+ */
94
+ bool check(const std::string &source, CompileInfo *info);
95
+
96
+ } // namespace wgsl
97
+ } // namespace xtgui
98
+
99
+ #endif /* XT_GUI_WGSL_H */