xbintsc 0.6.13 → 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 (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 +309 -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 +242 -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,137 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL translation entry points.
3
+ */
4
+
5
+ #include "wgsl.h"
6
+
7
+ #include <cstdlib>
8
+ #include <string>
9
+
10
+ #include "wgsl_ast.h"
11
+ #include "wgsl_emit.h"
12
+ #include "wgsl_lexer.h"
13
+ #include "wgsl_parser.h"
14
+
15
+ namespace xtgui {
16
+ namespace wgsl {
17
+
18
+ namespace {
19
+
20
+ bool parse_source(const std::string &source, Module *module, CompileInfo *info) {
21
+ std::vector<Token> tokens;
22
+ std::string error;
23
+ if (!tokenize(source, &tokens, &error)) {
24
+ info->message = error;
25
+ const size_t colon = error.find(':');
26
+ if (colon != std::string::npos) {
27
+ info->line = std::atoi(error.c_str());
28
+ const size_t second = error.find(':', colon + 1);
29
+ if (second != std::string::npos) info->column = std::atoi(error.c_str() + colon + 1);
30
+ }
31
+ return false;
32
+ }
33
+ Parser parser(tokens, module);
34
+ if (!parser.parse_module()) {
35
+ info->message = parser.error();
36
+ info->line = parser.error_line();
37
+ info->column = parser.error_column();
38
+ return false;
39
+ }
40
+ return true;
41
+ }
42
+
43
+ std::string stage_name(Stage stage) {
44
+ switch (stage) {
45
+ case Stage::Vertex: return "vertex";
46
+ case Stage::Fragment: return "fragment";
47
+ default: return "compute";
48
+ }
49
+ }
50
+
51
+ } // namespace
52
+
53
+ bool Emitter::run(const std::string &entry_point, CompileInfo *info) {
54
+ for (const FuncDecl &function : module_.funcs) functions_[function.name] = &function;
55
+ for (const GlobalDecl &global : module_.globals) globals_[global.name] = &global;
56
+
57
+ const FuncDecl *entry = find_function(entry_point);
58
+ if (entry == nullptr) {
59
+ info->message = "entry point '" + entry_point + "' was not declared in this shader module";
60
+ return false;
61
+ }
62
+ if (stage_name(stage_) != entry->stage) {
63
+ info->message = "entry point '" + entry_point + "' is a @" + entry->stage +
64
+ " entry point, not @" + stage_name(stage_);
65
+ return false;
66
+ }
67
+ if (!entry->is_entry) {
68
+ info->message = "the function '" + entry_point + "' is not an entry point";
69
+ return false;
70
+ }
71
+
72
+ collect_reachable(*entry);
73
+ if (!assign_resources(info)) return false;
74
+ if (!emit_declarations(info)) return false;
75
+
76
+ /* Prototypes first: WGSL allows a function to call one declared later. */
77
+ std::vector<const FuncDecl *> reachable;
78
+ for (const std::string &name : visited_functions_) {
79
+ if (name == entry_point) continue;
80
+ const FuncDecl *function = find_function(name);
81
+ if (function != nullptr) reachable.push_back(function);
82
+ }
83
+ for (const FuncDecl *function : reachable) {
84
+ Type void_type;
85
+ void_type.kind = Kind::Void;
86
+ functions_text_ += glsl_type(function->ret != nullptr ? *function->ret : void_type) + " xg_" +
87
+ function->name + "(";
88
+ for (size_t i = 0; i < function->params.size(); i++) {
89
+ if (i > 0) functions_text_ += ", ";
90
+ functions_text_ += glsl_decl(*function->params[i].type,
91
+ "xg_" + function->params[i].name + "_p");
92
+ }
93
+ functions_text_ += ");\n";
94
+ }
95
+ for (const FuncDecl *function : reachable) {
96
+ functions_text_ += emit_user_function(*function);
97
+ }
98
+
99
+ if (!emit_entry(*entry, &entry_text_)) return false;
100
+ if (failed_) {
101
+ info->message = error_;
102
+ return false;
103
+ }
104
+
105
+ out_->glsl = "#version 450\n";
106
+ out_->glsl += structs_text_;
107
+ out_->glsl += globals_text_;
108
+ out_->glsl += functions_text_;
109
+ out_->glsl += entry_text_;
110
+ out_->entry = "main";
111
+ return true;
112
+ }
113
+
114
+ bool translate(const std::string &source, const std::string &entry_point, Stage stage,
115
+ StageCode *out, CompileInfo *info) {
116
+ if (out == nullptr || info == nullptr) return false;
117
+ *out = StageCode();
118
+ info->message.clear();
119
+ info->line = 0;
120
+ info->column = 0;
121
+ Module module;
122
+ if (!parse_source(source, &module, info)) return false;
123
+ Emitter emitter(module, stage, out);
124
+ if (!emitter.run(entry_point, info)) return false;
125
+ return true;
126
+ }
127
+
128
+ bool check(const std::string &source, CompileInfo *info) {
129
+ info->message.clear();
130
+ info->line = 0;
131
+ info->column = 0;
132
+ Module module;
133
+ return parse_source(source, &module, info);
134
+ }
135
+
136
+ } // namespace wgsl
137
+ } // namespace xtgui
@@ -0,0 +1,233 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL abstract syntax tree.
3
+ *
4
+ * The tree is deliberately small: it models the WGSL a WebGPU renderer writes
5
+ * (entry points, structs, resources, functions, the usual statements) and
6
+ * nothing else. Types are shared because a struct member and a variable can
7
+ * name the same struct, and because array element types nest.
8
+ */
9
+ #ifndef XT_GUI_WGSL_AST_H
10
+ #define XT_GUI_WGSL_AST_H
11
+
12
+ #include <deque>
13
+ #include <memory>
14
+ #include <string>
15
+ #include <vector>
16
+
17
+ namespace xtgui {
18
+ namespace wgsl {
19
+
20
+ enum class Scalar { Bool, I32, U32, F32 };
21
+ enum class Kind {
22
+ Unknown,
23
+ Void,
24
+ Scalar,
25
+ Vector,
26
+ Matrix,
27
+ Array,
28
+ Struct,
29
+ Atomic,
30
+ Ptr,
31
+ Sampler,
32
+ SamplerComparison,
33
+ Texture, // sampled texture
34
+ StorageTexture // write/read storage texture
35
+ };
36
+
37
+ enum class TextureDim { D1, D2, D2Array, D3, Cube, CubeArray, Multisampled2D };
38
+
39
+ enum class ImageFormat {
40
+ Unknown,
41
+ Rgba8Unorm,
42
+ Rgba8Snorm,
43
+ Rgba8Uint,
44
+ Rgba8Sint,
45
+ Rgba16Float,
46
+ Rgba32Float,
47
+ R32Float,
48
+ R32Uint,
49
+ R32Sint,
50
+ Rgba16Uint,
51
+ Rgba16Sint,
52
+ Bgra8Unorm
53
+ };
54
+
55
+ struct StructDecl;
56
+ struct Type;
57
+ using TypePtr = std::shared_ptr<Type>;
58
+
59
+ struct Type {
60
+ Kind kind = Kind::Unknown;
61
+ Scalar scalar = Scalar::F32;
62
+ /** Vector width (1 for scalars). */
63
+ int width = 1;
64
+ /** Matrix columns/rows. */
65
+ int cols = 1;
66
+ int rows = 1;
67
+ TypePtr elem;
68
+ /** Array length; 0 means a runtime-sized array. */
69
+ int length = 0;
70
+ const StructDecl *strct = nullptr;
71
+ /** Texture shape and storage format. */
72
+ TextureDim dim = TextureDim::D2;
73
+ ImageFormat format = ImageFormat::Unknown;
74
+ bool depth = false;
75
+ /** 1 = read, 2 = write, 3 = read_write (storage textures/buffers). */
76
+ int access = 1;
77
+
78
+ bool is_scalar() const { return kind == Kind::Scalar; }
79
+ bool is_vector() const { return kind == Kind::Vector; }
80
+ bool is_numeric() const { return kind == Kind::Scalar || kind == Kind::Vector; }
81
+ };
82
+
83
+ enum class Space { None, Private, Workgroup, Uniform, Storage };
84
+
85
+ struct Attribute {
86
+ std::string name;
87
+ std::vector<int> ints;
88
+ std::vector<std::string> words;
89
+ };
90
+
91
+ struct Member {
92
+ std::string name;
93
+ TypePtr type;
94
+ int location = -1;
95
+ std::string builtin;
96
+ std::string interpolate;
97
+ int align = -1;
98
+ int size = -1;
99
+ };
100
+
101
+ struct StructDecl {
102
+ std::string name;
103
+ std::vector<Member> members;
104
+ };
105
+
106
+ struct Param {
107
+ std::string name;
108
+ TypePtr type;
109
+ int location = -1;
110
+ std::string builtin;
111
+ std::string interpolate;
112
+ };
113
+
114
+ /** A literal, name, operator, call, member access, index or array ctor. */
115
+ struct Expr;
116
+ using ExprPtr = std::unique_ptr<Expr>;
117
+ struct Expr {
118
+ enum class K {
119
+ Int,
120
+ Float,
121
+ Bool,
122
+ Ident,
123
+ Unary,
124
+ Binary,
125
+ Call,
126
+ Member,
127
+ Index,
128
+ ArrayCtor,
129
+ Bitcast
130
+ } kind = K::Ident;
131
+ std::string text; // literal spelling, identifier, operator or callee
132
+ std::string member; // `.member`
133
+ std::vector<ExprPtr> args;
134
+ TypePtr type; // array constructor / bitcast target type
135
+ int line = 0;
136
+ };
137
+
138
+ struct Stmt;
139
+ using StmtPtr = std::unique_ptr<Stmt>;
140
+ struct Stmt {
141
+ enum class K {
142
+ Block,
143
+ Var,
144
+ Assign,
145
+ Expr,
146
+ If,
147
+ For,
148
+ While,
149
+ Loop,
150
+ Break,
151
+ Continue,
152
+ Return,
153
+ Discard,
154
+ Switch
155
+ } kind = K::Block;
156
+
157
+ std::vector<StmtPtr> body; // Block / Loop / If-then / SwitchCases
158
+ std::vector<StmtPtr> otherwise; // else branch
159
+ StmtPtr init, cond, update; // For
160
+ ExprPtr expr; // If/While/For cond, Return, Expr, Assign
161
+ ExprPtr value; // Assign value
162
+ std::string op; // Assign operator (`=`, `+=`, …)
163
+ std::string name; // Var name
164
+ TypePtr type; // Var type
165
+ Space space = Space::None; // Var address space
166
+ std::string access; // storage access (`read`, `write`, `read_write`)
167
+ bool is_const = false; // `let`/`const`
168
+ StmtPtr continuing; // Loop `continuing` block
169
+ std::vector<ExprPtr> cases; // Switch case selectors (null = default)
170
+ std::vector<StmtPtr> case_bodies; // parallel to cases
171
+ int line = 0;
172
+ };
173
+
174
+ struct FuncDecl {
175
+ std::string name;
176
+ std::vector<Param> params;
177
+ TypePtr ret;
178
+ std::vector<StmtPtr> body;
179
+ bool is_entry = false;
180
+ std::string stage; // `vertex`, `fragment` or `compute`
181
+ std::string ret_builtin;
182
+ int ret_location = -1;
183
+ std::string ret_interpolate;
184
+ int workgroup[3] = {1, 1, 1};
185
+ bool has_workgroup = false;
186
+ };
187
+
188
+ struct GlobalDecl {
189
+ std::string name;
190
+ TypePtr type;
191
+ Space space = Space::Private;
192
+ std::string access;
193
+ int group = -1;
194
+ int binding = -1;
195
+ ExprPtr init;
196
+ int line = 0;
197
+ };
198
+
199
+ struct Module {
200
+ std::vector<std::string> enables;
201
+ /* A deque: types hold `StructDecl*`, and the address must survive later
202
+ * struct declarations in the same module. */
203
+ std::deque<StructDecl> structs;
204
+ std::vector<GlobalDecl> globals;
205
+ std::vector<FuncDecl> funcs;
206
+ std::vector<std::pair<std::string, TypePtr>> aliases;
207
+ };
208
+
209
+ /** Parse `source`; on failure `error` receives a `line:col: message`. */
210
+ bool parse_module(const std::string &source, Module *out, std::string *error);
211
+
212
+ /* -- type helpers (wgsl_type.cpp) ----------------------------------------- */
213
+
214
+ TypePtr make_scalar(Scalar scalar);
215
+ TypePtr make_vector(Scalar scalar, int width);
216
+ TypePtr make_matrix(Scalar scalar, int cols, int rows);
217
+ TypePtr make_array(TypePtr elem, int length);
218
+ std::string scalar_glsl(Scalar scalar);
219
+ /** The GLSL base type (`float`, `vec3`, `mat4`, `S`, `sampler2D`, …). */
220
+ std::string glsl_type(const Type &type);
221
+ /** A declaration: `vec3 name`, `float name[4]`. */
222
+ std::string glsl_decl(const Type &type, const std::string &name);
223
+ /** Human-readable WGSL type name, for diagnostics. */
224
+ std::string wgsl_type(const Type &type);
225
+ /** The constructed type named `name` (`vec3f`, `mat4x4f`, `S`), or null. */
226
+ TypePtr type_by_name(const Module &module, const std::string &name);
227
+ /** GLSL image format for a storage texture's `rgba8unorm`-style suffix. */
228
+ std::string image_format_glsl(ImageFormat format);
229
+
230
+ } // namespace wgsl
231
+ } // namespace xtgui
232
+
233
+ #endif /* XT_GUI_WGSL_AST_H */