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,452 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL module setup, resource assignment and declarations.
3
+ *
4
+ * Reachability matters: a module usually holds both the vertex and fragment
5
+ * entry point, and only the resources the requested stage actually touches may
6
+ * become SDL_GPU bindings, or a fragment-only texture would force the vertex
7
+ * stage to bind a texture the program never supplied.
8
+ */
9
+
10
+ #include "wgsl_emit.h"
11
+
12
+ #include <algorithm>
13
+ #include <cstdlib>
14
+
15
+ namespace xtgui {
16
+ namespace wgsl {
17
+ namespace {
18
+
19
+ bool is_swizzle(const std::string &name) {
20
+ if (name.empty() || name.size() > 4) return false;
21
+ for (char c : name) {
22
+ if (std::string("xyzwrgba").find(c) == std::string::npos) return false;
23
+ }
24
+ return true;
25
+ }
26
+
27
+ bool is_texture(const TypePtr &type) {
28
+ return type != nullptr && (type->kind == Kind::Texture || type->kind == Kind::StorageTexture);
29
+ }
30
+
31
+ bool is_sampler(const TypePtr &type) {
32
+ return type != nullptr && (type->kind == Kind::Sampler || type->kind == Kind::SamplerComparison);
33
+ }
34
+
35
+ bool texture_used_as_store(const GlobalDecl &decl) {
36
+ return decl.type != nullptr && decl.type->kind == Kind::StorageTexture && decl.type->access != 1;
37
+ }
38
+
39
+ bool storage_written(const GlobalDecl &decl) {
40
+ if (decl.access == "write" || decl.access == "read_write") return true;
41
+ return decl.type != nullptr && decl.type->access != 1;
42
+ }
43
+
44
+ } // namespace
45
+
46
+ /* -- shared helpers ------------------------------------------------------- */
47
+
48
+ void Emitter::push_scope() { scopes_.emplace_back(); }
49
+
50
+ void Emitter::pop_scope() {
51
+ if (!scopes_.empty()) scopes_.pop_back();
52
+ }
53
+
54
+ void Emitter::declare(const std::string &name, const TypePtr &type) {
55
+ if (scopes_.empty()) push_scope();
56
+ Local local;
57
+ local.name = "xg_" + name + "_" + std::to_string(local_counter_++);
58
+ local.type = type;
59
+ scopes_.back()[name] = local;
60
+ }
61
+
62
+ const Emitter::Local *Emitter::lookup_local(const std::string &name) const {
63
+ for (auto scope = scopes_.rbegin(); scope != scopes_.rend(); ++scope) {
64
+ auto found = scope->find(name);
65
+ if (found != scope->end()) return &found->second;
66
+ }
67
+ return nullptr;
68
+ }
69
+
70
+ const GlobalDecl *Emitter::find_global(const std::string &name) const {
71
+ auto found = globals_.find(name);
72
+ return found == globals_.end() ? nullptr : found->second;
73
+ }
74
+
75
+ const FuncDecl *Emitter::find_function(const std::string &name) const {
76
+ auto found = functions_.find(name);
77
+ return found == functions_.end() ? nullptr : found->second;
78
+ }
79
+
80
+ const StructDecl *Emitter::struct_of(const TypePtr &type) const {
81
+ return type != nullptr && type->kind == Kind::Struct ? type->strct : nullptr;
82
+ }
83
+
84
+ std::string Emitter::member_name(const std::string &name) const {
85
+ if (is_swizzle(name)) return name;
86
+ return "xg_" + name;
87
+ }
88
+
89
+ std::string Emitter::global_expr(const std::string &name) const {
90
+ auto found = global_expr_.find(name);
91
+ return found == global_expr_.end() ? "xg_" + name : found->second;
92
+ }
93
+
94
+ int Emitter::pair_slot_for(const std::string &texture, const std::string &sampler) const {
95
+ for (const Pair &pair : pairs_) {
96
+ if (pair.texture == texture && (sampler.empty() || pair.sampler == sampler)) return pair.slot;
97
+ }
98
+ return -1;
99
+ }
100
+
101
+ int Emitter::first_pair_for(const std::string &texture) const {
102
+ return pair_slot_for(texture, std::string());
103
+ }
104
+
105
+ /* -- reachability --------------------------------------------------------- */
106
+
107
+ void Emitter::collect_expr(const Expr &node, bool &loads, bool &samples) {
108
+ if (node.kind == Expr::K::Ident) {
109
+ if (globals_.count(node.text) != 0) reachable_.insert(node.text);
110
+ return;
111
+ }
112
+ if (node.kind == Expr::K::Call) {
113
+ const std::string &callee = node.text;
114
+ if (callee.compare(0, 5, ".") != 0 && callee.compare(0, 13, "textureSample") == 0) {
115
+ samples = true;
116
+ if (node.args.size() >= 2 && node.args[0]->kind == Expr::K::Ident &&
117
+ node.args[1]->kind == Expr::K::Ident) {
118
+ const std::string &texture = node.args[0]->text;
119
+ const std::string &sampler = node.args[1]->text;
120
+ bool exists = false;
121
+ for (const Pair &pair : pairs_) {
122
+ if (pair.texture == texture && pair.sampler == sampler) exists = true;
123
+ }
124
+ if (!exists) pairs_.push_back({texture, sampler, -1});
125
+ reachable_.insert(texture);
126
+ reachable_.insert(sampler);
127
+ }
128
+ } else if (callee == "textureLoad" || callee == "textureDimensions" ||
129
+ callee == "textureNumLevels" || callee == "textureNumLayers") {
130
+ loads = true;
131
+ if (!node.args.empty() && node.args[0]->kind == Expr::K::Ident) {
132
+ load_only_textures_.insert(node.args[0]->text);
133
+ reachable_.insert(node.args[0]->text);
134
+ }
135
+ } else if (callee == "textureStore") {
136
+ if (!node.args.empty() && node.args[0]->kind == Expr::K::Ident) {
137
+ reachable_.insert(node.args[0]->text);
138
+ }
139
+ } else if (const FuncDecl *function = find_function(callee)) {
140
+ if (reachable_.count("fn:" + callee) == 0) collect_function(function);
141
+ }
142
+ for (const ExprPtr &arg : node.args) {
143
+ if (arg != nullptr) collect_expr(*arg, loads, samples);
144
+ }
145
+ return;
146
+ }
147
+ for (const ExprPtr &arg : node.args) {
148
+ if (arg != nullptr) collect_expr(*arg, loads, samples);
149
+ }
150
+ }
151
+
152
+ void Emitter::collect_stmt(const Stmt &node, bool &loads, bool &samples) {
153
+ if (node.expr != nullptr) collect_expr(*node.expr, loads, samples);
154
+ if (node.value != nullptr) collect_expr(*node.value, loads, samples);
155
+ for (const StmtPtr &child : node.body) {
156
+ if (child != nullptr) collect_stmt(*child, loads, samples);
157
+ }
158
+ for (const StmtPtr &child : node.otherwise) {
159
+ if (child != nullptr) collect_stmt(*child, loads, samples);
160
+ }
161
+ if (node.init != nullptr) collect_stmt(*node.init, loads, samples);
162
+ if (node.cond != nullptr) collect_stmt(*node.cond, loads, samples);
163
+ if (node.update != nullptr) collect_stmt(*node.update, loads, samples);
164
+ if (node.continuing != nullptr) collect_stmt(*node.continuing, loads, samples);
165
+ for (const StmtPtr &child : node.case_bodies) {
166
+ if (child != nullptr) collect_stmt(*child, loads, samples);
167
+ }
168
+ }
169
+
170
+ void Emitter::collect_function(const FuncDecl *func) {
171
+ if (func == nullptr || visited_functions_.count(func->name) != 0) return;
172
+ visited_functions_.insert(func->name);
173
+ reachable_.insert("fn:" + func->name);
174
+ bool loads = false;
175
+ bool samples = false;
176
+ for (const StmtPtr &statement : func->body) {
177
+ if (statement != nullptr) collect_stmt(*statement, loads, samples);
178
+ }
179
+ }
180
+
181
+ bool Emitter::collect_reachable(const FuncDecl &entry) {
182
+ collect_function(&entry);
183
+ return true;
184
+ }
185
+
186
+ /* -- slot assignment ------------------------------------------------------ */
187
+
188
+ bool Emitter::assign_resources(CompileInfo *info) {
189
+ const bool compute = stage_ == Stage::Compute;
190
+ read_set_ = compute ? 0 : (stage_ == Stage::Fragment ? 2 : 0);
191
+ write_set_ = 1;
192
+ uniform_set_ = compute ? 2 : (stage_ == Stage::Fragment ? 3 : 1);
193
+
194
+ auto by_binding = [&](const GlobalDecl *left, const GlobalDecl *right) {
195
+ if (left->group != right->group) return left->group < right->group;
196
+ return left->binding < right->binding;
197
+ };
198
+ std::vector<const GlobalDecl *> textures;
199
+ std::vector<const GlobalDecl *> storage_buffers;
200
+ std::vector<const GlobalDecl *> storage_textures;
201
+ std::vector<const GlobalDecl *> uniforms;
202
+ for (const auto &entry_global : globals_) {
203
+ const GlobalDecl *decl = entry_global.second;
204
+ if (reachable_.count(decl->name) == 0) continue;
205
+ if (is_texture(decl->type) && decl->type->kind == Kind::Texture) textures.push_back(decl);
206
+ else if (decl->space == Space::Storage) {
207
+ if (decl->type != nullptr && decl->type->kind == Kind::Struct) storage_buffers.push_back(decl);
208
+ else if (decl->type != nullptr && decl->type->kind == Kind::Array) storage_buffers.push_back(decl);
209
+ else if (decl->type != nullptr && decl->type->kind == Kind::StorageTexture) {
210
+ storage_textures.push_back(decl);
211
+ } else storage_buffers.push_back(decl);
212
+ } else if (decl->space == Space::Uniform) uniforms.push_back(decl);
213
+ }
214
+ std::sort(textures.begin(), textures.end(), by_binding);
215
+ std::sort(storage_buffers.begin(), storage_buffers.end(), by_binding);
216
+ std::sort(storage_textures.begin(), storage_textures.end(), by_binding);
217
+ std::sort(uniforms.begin(), uniforms.end(), by_binding);
218
+
219
+ /* Sampler slots: one per (texture, sampler) pair. Ensure every reachable
220
+ * texture has at least one slot so textureLoad-only textures can be bound. */
221
+ for (const GlobalDecl *decl : textures) {
222
+ bool any = false;
223
+ for (const Pair &pair : pairs_) {
224
+ if (pair.texture == decl->name) any = true;
225
+ }
226
+ if (!any) pairs_.push_back({decl->name, std::string(), -1});
227
+ }
228
+ std::sort(pairs_.begin(), pairs_.end(), [&](const Pair &left, const Pair &right) {
229
+ const GlobalDecl *a = find_global(left.texture);
230
+ const GlobalDecl *b = find_global(right.texture);
231
+ if (a != nullptr && b != nullptr) {
232
+ if (a->group != b->group) return a->group < b->group;
233
+ if (a->binding != b->binding) return a->binding < b->binding;
234
+ }
235
+ const GlobalDecl *as = left.sampler.empty() ? nullptr : find_global(left.sampler);
236
+ const GlobalDecl *bs = right.sampler.empty() ? nullptr : find_global(right.sampler);
237
+ if (as == nullptr && bs == nullptr) return left.sampler < right.sampler;
238
+ if (as == nullptr) return true;
239
+ if (bs == nullptr) return false;
240
+ if (as->group != bs->group) return as->group < bs->group;
241
+ return as->binding < bs->binding;
242
+ });
243
+ if (pairs_.size() > 16) {
244
+ info->message = "at most 16 sampled textures can be used by one stage";
245
+ return false;
246
+ }
247
+ for (size_t i = 0; i < pairs_.size(); i++) {
248
+ pairs_[i].slot = (int)i;
249
+ TextureSlot slot;
250
+ const GlobalDecl *texture = find_global(pairs_[i].texture);
251
+ if (texture == nullptr) continue;
252
+ slot.texture.group = texture->group;
253
+ slot.texture.binding = texture->binding;
254
+ if (!pairs_[i].sampler.empty()) {
255
+ const GlobalDecl *sampler = find_global(pairs_[i].sampler);
256
+ if (sampler != nullptr) {
257
+ slot.has_sampler = true;
258
+ slot.sampler.group = sampler->group;
259
+ slot.sampler.binding = sampler->binding;
260
+ }
261
+ }
262
+ out_->samplers.push_back(slot);
263
+ Resource resource;
264
+ resource.kind = Resource::Kind::Texture;
265
+ resource.slot = (int)i;
266
+ resources_[pairs_[i].texture] = resource;
267
+ if (!pairs_[i].sampler.empty()) {
268
+ Resource sampler_resource;
269
+ sampler_resource.kind = Resource::Kind::Sampler;
270
+ sampler_resource.slot = (int)i;
271
+ resources_[pairs_[i].sampler] = sampler_resource;
272
+ }
273
+ }
274
+
275
+ const int pair_count = (int)pairs_.size();
276
+ for (size_t i = 0; i < storage_textures.size(); i++) {
277
+ const GlobalDecl *decl = storage_textures[i];
278
+ Resource resource;
279
+ if (texture_used_as_store(*decl)) {
280
+ resource.kind = Resource::Kind::StorageTextureWrite;
281
+ resource.slot = (int)i;
282
+ out_->write_storage_textures.push_back({decl->group, decl->binding});
283
+ } else {
284
+ resource.kind = Resource::Kind::StorageTextureRead;
285
+ resource.slot = (int)i;
286
+ out_->storage_textures.push_back({decl->group, decl->binding});
287
+ }
288
+ resources_[decl->name] = resource;
289
+ }
290
+ int read_storage_buffers = 0;
291
+ int write_storage_buffers = 0;
292
+ int read_binding = pair_count + (int)out_->storage_textures.size();
293
+ for (const GlobalDecl *decl : storage_buffers) {
294
+ Resource resource;
295
+ if (storage_written(*decl)) {
296
+ resource.kind = Resource::Kind::StorageWrite;
297
+ resource.slot = write_storage_buffers++;
298
+ out_->write_storage_buffers.push_back({decl->group, decl->binding});
299
+ } else {
300
+ resource.kind = Resource::Kind::StorageRead;
301
+ resource.slot = read_storage_buffers++;
302
+ resource.binding = read_binding + read_storage_buffers - 1;
303
+ out_->storage_buffers.push_back({decl->group, decl->binding});
304
+ }
305
+ resources_[decl->name] = resource;
306
+ }
307
+ if (uniforms.size() > 4) {
308
+ info->message = "at most 4 uniform buffers can be used by one stage";
309
+ return false;
310
+ }
311
+ for (size_t i = 0; i < uniforms.size(); i++) {
312
+ Resource resource;
313
+ resource.kind = Resource::Kind::Uniform;
314
+ resource.slot = (int)i;
315
+ resources_[uniforms[i]->name] = resource;
316
+ out_->uniform_buffers.push_back({uniforms[i]->group, uniforms[i]->binding});
317
+ }
318
+ return true;
319
+ }
320
+
321
+ /* -- declarations --------------------------------------------------------- */
322
+
323
+ std::string Emitter::block_body(const Type &type, bool storage) const {
324
+ std::string body;
325
+ if (type.kind == Kind::Struct && type.strct != nullptr) {
326
+ for (const Member &member : type.strct->members) {
327
+ if (member.type == nullptr) continue;
328
+ body += " " + glsl_decl(*member.type, member_name(member.name)) + ";\n";
329
+ }
330
+ return body;
331
+ }
332
+ if (type.kind == Kind::Array && type.elem != nullptr) {
333
+ if (storage && type.length == 0) {
334
+ body += " " + glsl_type(*type.elem) + " xg_data[];\n";
335
+ } else {
336
+ body += " " + glsl_decl(type, "xg_data") + ";\n";
337
+ }
338
+ return body;
339
+ }
340
+ body += " " + glsl_decl(type, "xg_value") + ";\n";
341
+ return body;
342
+ }
343
+
344
+ bool Emitter::emit_declarations(CompileInfo *info) {
345
+ /* Struct definitions first: blocks and function signatures use them. */
346
+ for (const StructDecl &decl : module_.structs) {
347
+ structs_text_ += "struct " + decl.name + " {\n";
348
+ for (const Member &member : decl.members) {
349
+ if (member.type == nullptr) continue;
350
+ structs_text_ += " " + glsl_decl(*member.type, member_name(member.name)) + ";\n";
351
+ }
352
+ structs_text_ += "};\n";
353
+ }
354
+
355
+ for (const auto &entry : globals_) {
356
+ const GlobalDecl *decl = entry.second;
357
+ const std::string name = "xg_" + decl->name;
358
+ if (decl->type == nullptr) {
359
+ if (decl->init == nullptr) continue;
360
+ /* `const positions = array<vec2f, 3>(…);` has no written type. */
361
+ TypePtr inferred = type_of(*decl->init);
362
+ if (inferred == nullptr || inferred->kind == Kind::Unknown) continue;
363
+ const_cast<GlobalDecl *>(decl)->type = inferred;
364
+ }
365
+ const bool used = reachable_.count(decl->name) != 0;
366
+
367
+ /* Textures and samplers are resources regardless of the address space the
368
+ * parser defaulted them to (a module-scope `var tex: texture_2d<f32>;`
369
+ * omits `var<…>` entirely). */
370
+ if (is_sampler(decl->type)) continue;
371
+ if (decl->type->kind == Kind::Texture) {
372
+ if (!used) continue;
373
+ const int slot = first_pair_for(decl->name);
374
+ if (slot < 0) continue;
375
+ const bool depth = decl->type->depth;
376
+ globals_text_ += "layout(set = " + std::to_string(read_set_) + ", binding = " +
377
+ std::to_string(slot) + ") uniform " +
378
+ (depth ? "sampler2DShadow" : "sampler2D") + " xg_tex" +
379
+ std::to_string(slot) + ";\n";
380
+ global_expr_[decl->name] = "xg_tex" + std::to_string(slot);
381
+ continue;
382
+ }
383
+ if (decl->type->kind == Kind::StorageTexture) {
384
+ if (!used) continue;
385
+ Resource resource = resources_[decl->name];
386
+ const bool write = resource.kind == Resource::Kind::StorageTextureWrite;
387
+ const int set = write ? write_set_ : read_set_;
388
+ const int binding = write ? resource.slot : (int)pairs_.size() + resource.slot;
389
+ std::string image = "image2D";
390
+ switch (decl->type->dim) {
391
+ case TextureDim::D2Array: image = "image2DArray"; break;
392
+ case TextureDim::D3: image = "image3D"; break;
393
+ default: break;
394
+ }
395
+ globals_text_ += "layout(" + image_format_glsl(decl->type->format) + ", set = " +
396
+ std::to_string(set) + ", binding = " + std::to_string(binding) +
397
+ ") uniform " + (write ? "writeonly " : "readonly ") + image + " " + name +
398
+ ";\n";
399
+ global_expr_[decl->name] = name;
400
+ continue;
401
+ }
402
+
403
+ if (decl->space == Space::None) {
404
+ globals_text_ += std::string("const ") + glsl_decl(*decl->type, name);
405
+ if (decl->init != nullptr) globals_text_ += " = " + expr(*decl->init);
406
+ globals_text_ += ";\n";
407
+ global_expr_[decl->name] = name;
408
+ continue;
409
+ }
410
+ if (decl->space == Space::Private) {
411
+ globals_text_ += glsl_decl(*decl->type, name);
412
+ if (decl->init != nullptr) globals_text_ += " = " + expr(*decl->init);
413
+ globals_text_ += ";\n";
414
+ global_expr_[decl->name] = name;
415
+ continue;
416
+ }
417
+ if (decl->space == Space::Workgroup) {
418
+ globals_text_ += std::string("shared ") + glsl_decl(*decl->type, name) + ";\n";
419
+ global_expr_[decl->name] = name;
420
+ continue;
421
+ }
422
+ if (!used) continue;
423
+
424
+ if (decl->space == Space::Uniform) {
425
+ Resource resource = resources_[decl->name];
426
+ globals_text_ += "layout(std140, set = " + std::to_string(uniform_set_) +
427
+ ", binding = " + std::to_string(resource.slot) + ") uniform " + name +
428
+ "_block {\n" + block_body(*decl->type, false) + "} " + name + ";\n";
429
+ if (decl->type->kind == Kind::Struct) global_expr_[decl->name] = name;
430
+ else if (decl->type->kind == Kind::Array) global_expr_[decl->name] = name + ".xg_data";
431
+ else global_expr_[decl->name] = name + ".xg_value";
432
+ continue;
433
+ }
434
+ if (decl->space == Space::Storage) {
435
+ Resource resource = resources_[decl->name];
436
+ const bool write = resource.kind == Resource::Kind::StorageWrite;
437
+ const int set = write ? write_set_ : read_set_;
438
+ const int binding = write ? resource.slot : resource.binding;
439
+ globals_text_ += "layout(std430, set = " + std::to_string(set) +
440
+ ", binding = " + std::to_string(binding) + ") buffer " + name +
441
+ "_block {\n" + block_body(*decl->type, true) + "} " + name + ";\n";
442
+ if (decl->type->kind == Kind::Struct) global_expr_[decl->name] = name;
443
+ else if (decl->type->kind == Kind::Array) global_expr_[decl->name] = name + ".xg_data";
444
+ else global_expr_[decl->name] = name + ".xg_value";
445
+ }
446
+ }
447
+ (void)info;
448
+ return true;
449
+ }
450
+
451
+ } // namespace wgsl
452
+ } // namespace xtgui
@@ -0,0 +1,128 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL -> GLSL emitter (shared declarations).
3
+ *
4
+ * Emission runs in three passes:
5
+ * 1. mark the globals and functions reachable from the requested entry point,
6
+ * so a module that holds several stages only declares what this stage uses;
7
+ * 2. assign SDL_GPU binding slots to the reachable resources and pair every
8
+ * sampled texture with the sampler(s) it is sampled with;
9
+ * 3. emit structs, resource declarations, functions and the entry wrapper.
10
+ */
11
+ #ifndef XT_GUI_WGSL_EMIT_H
12
+ #define XT_GUI_WGSL_EMIT_H
13
+
14
+ #include <map>
15
+ #include <set>
16
+ #include <string>
17
+ #include <vector>
18
+
19
+ #include "wgsl.h"
20
+ #include "wgsl_ast.h"
21
+
22
+ namespace xtgui {
23
+ namespace wgsl {
24
+
25
+ class Emitter {
26
+ public:
27
+ Emitter(const Module &module, Stage stage, StageCode *out)
28
+ : module_(module), stage_(stage), out_(out) {}
29
+
30
+ bool run(const std::string &entry_point, CompileInfo *info);
31
+
32
+ /* -- expression/statement emission (wgsl_emit_expr.cpp, wgsl_emit_stmt.cpp) */
33
+ std::string expr(const Expr &node);
34
+ std::string call_expr(const Expr &node);
35
+ std::string statement(const Stmt &node, int indent);
36
+ TypePtr type_of(const Expr &node);
37
+
38
+ /* -- helpers shared by the emission units --------------------------------- */
39
+ struct Local {
40
+ std::string name;
41
+ TypePtr type;
42
+ };
43
+ void push_scope();
44
+ void pop_scope();
45
+ void declare(const std::string &name, const TypePtr &type);
46
+ const Local *lookup_local(const std::string &name) const;
47
+ const GlobalDecl *find_global(const std::string &name) const;
48
+ const FuncDecl *find_function(const std::string &name) const;
49
+ const StructDecl *struct_of(const TypePtr &type) const;
50
+ /** `xg_`-prefixed member name; swizzle letters pass through untouched. */
51
+ std::string member_name(const std::string &name) const;
52
+ /** The GLSL expression that names a global (`xg_u`, `xg_u.xg_data`, …). */
53
+ std::string global_expr(const std::string &name) const;
54
+ /** The sampler slot a texture uses together with `sampler` ("" = any). */
55
+ int pair_slot_for(const std::string &texture, const std::string &sampler) const;
56
+ int first_pair_for(const std::string &texture) const;
57
+ /** GLSL builtin variable for a WGSL `@builtin(name)` ("" when unknown). */
58
+ std::string builtin_variable(const std::string &name, Stage stage) const;
59
+ /** Emit `target(expr)` only when the GLSL type would otherwise be wrong. */
60
+ std::string cast_to(const Type &target, const std::string &value) const;
61
+ std::string emit_user_function(const FuncDecl &func);
62
+ bool emit_entry(const FuncDecl &entry, std::string *text);
63
+
64
+ const Module &module_;
65
+ Stage stage_;
66
+ StageCode *out_;
67
+ std::string structs_text_;
68
+ std::string globals_text_;
69
+ std::string functions_text_;
70
+ std::string entry_text_;
71
+
72
+ void fail(const std::string &message) {
73
+ if (!failed_) failed_ = true;
74
+ error_ = message;
75
+ }
76
+ bool failed_ = false;
77
+ std::string error_;
78
+
79
+ private:
80
+ struct Resource {
81
+ enum class Kind {
82
+ Uniform,
83
+ StorageRead,
84
+ StorageWrite,
85
+ StorageTextureRead,
86
+ StorageTextureWrite,
87
+ Texture,
88
+ Sampler
89
+ } kind = Kind::Uniform;
90
+ int slot = -1;
91
+ int binding = -1;
92
+ };
93
+ struct Pair {
94
+ std::string texture;
95
+ std::string sampler; // empty for a texture used only by textureLoad
96
+ int slot = -1;
97
+ };
98
+
99
+ bool collect_reachable(const FuncDecl &entry);
100
+ void collect_function(const FuncDecl *func);
101
+ void collect_stmt(const Stmt &node, bool &loads, bool &samples);
102
+ void collect_expr(const Expr &node, bool &loads, bool &samples);
103
+ bool assign_resources(CompileInfo *info);
104
+ bool emit_declarations(CompileInfo *info);
105
+ std::string block_body(const Type &type, bool storage) const;
106
+ std::string storage_declaration(const GlobalDecl &decl, int set, int binding,
107
+ const std::string &api_name) const;
108
+
109
+ std::map<std::string, const FuncDecl *> functions_;
110
+ std::map<std::string, const GlobalDecl *> globals_;
111
+ std::map<std::string, Resource> resources_;
112
+ std::vector<Pair> pairs_;
113
+ std::map<std::string, std::string> global_expr_;
114
+ std::vector<std::map<std::string, Local>> scopes_;
115
+ std::vector<std::string> continuing_stack_;
116
+ std::set<std::string> reachable_;
117
+ std::set<std::string> visited_functions_;
118
+ std::set<std::string> load_only_textures_;
119
+ int local_counter_ = 0;
120
+ int read_set_ = 0;
121
+ int write_set_ = 1;
122
+ int uniform_set_ = 2;
123
+ };
124
+
125
+ } // namespace wgsl
126
+ } // namespace xtgui
127
+
128
+ #endif /* XT_GUI_WGSL_EMIT_H */