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.
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/features.d.ts +2 -0
- package/dist/src/extensions/gui/features.js +24 -0
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +39 -0
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
- package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
- package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
- package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
- package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
- package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +1 -1
- package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
- package/dist/tests/e2e/gui-webgpu.test.js +402 -0
- package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
- package/dist/tests/extensions/gui-features.test.js +18 -1
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/doc/ai/extensions.md +7 -1
- package/doc/ai/zh-CN/extensions.md +6 -1
- package/doc/gui/webgpu.md +121 -0
- package/doc/gui.md +12 -1
- package/doc/zh-CN/gui/webgpu.md +111 -0
- package/doc/zh-CN/gui.md +10 -1
- package/llms.txt +1 -0
- package/package.json +7 -7
- package/runtime/ext_gui/browser.cpp +10 -0
- package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
- package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
- package/runtime/ext_gui/gpu_feature.cpp +2 -0
- package/runtime/ext_gui/gpu_shader_api.h +6 -0
- package/runtime/ext_gui/gui_canvas.cpp +18 -0
- package/runtime/ext_gui/renderer_image.cpp +6 -0
- package/runtime/ext_gui/webgl_glsl.h +8 -0
- package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
- package/runtime/ext_gui/webgl_msl.cpp +20 -9
- package/runtime/ext_gui/webgl_msl.h +4 -0
- package/runtime/ext_gui/webgpu.h +371 -0
- package/runtime/ext_gui/webgpu_api.h +42 -0
- package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
- package/runtime/ext_gui/webgpu_bindings.h +59 -0
- package/runtime/ext_gui/webgpu_bridge.c +21 -0
- package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
- package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
- package/runtime/ext_gui/webgpu_command.cpp +510 -0
- package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
- package/runtime/ext_gui/webgpu_convert.cpp +309 -0
- package/runtime/ext_gui/webgpu_device.cpp +370 -0
- package/runtime/ext_gui/webgpu_feature.cpp +74 -0
- package/runtime/ext_gui/webgpu_objects.cpp +125 -0
- package/runtime/ext_gui/webgpu_pass.cpp +295 -0
- package/runtime/ext_gui/webgpu_pass.h +33 -0
- package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
- package/runtime/ext_gui/webgpu_queue.cpp +222 -0
- package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
- package/runtime/ext_gui/webgpu_resources.cpp +315 -0
- package/runtime/ext_gui/webgpu_shader.cpp +242 -0
- package/runtime/ext_gui/wgsl.h +99 -0
- package/runtime/ext_gui/wgsl_api.cpp +137 -0
- package/runtime/ext_gui/wgsl_ast.h +233 -0
- package/runtime/ext_gui/wgsl_emit.cpp +452 -0
- package/runtime/ext_gui/wgsl_emit.h +128 -0
- package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
- package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
- package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
- package/runtime/ext_gui/wgsl_entry.cpp +189 -0
- package/runtime/ext_gui/wgsl_infer.cpp +171 -0
- package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
- package/runtime/ext_gui/wgsl_lexer.h +32 -0
- package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
- package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
- package/runtime/ext_gui/wgsl_parser.cpp +457 -0
- package/runtime/ext_gui/wgsl_parser.h +77 -0
- package/runtime/ext_gui/wgsl_type.cpp +232 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +24 -0
- package/src/extensions/gui/index.ts +39 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WGSL calls, texture operations and constructors.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#include "wgsl_emit.h"
|
|
10
|
+
|
|
11
|
+
namespace xtgui {
|
|
12
|
+
namespace wgsl {
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
std::string ident_of(const ExprPtr &node) {
|
|
16
|
+
return node != nullptr && node->kind == Expr::K::Ident ? node->text : std::string();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const char *compare_function(const std::string &name) {
|
|
20
|
+
if (name == "textureSampleCompare" || name == "textureSampleCompareLevel") return "texture";
|
|
21
|
+
return nullptr;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
std::string coord_width(int width) {
|
|
25
|
+
if (width <= 1) return "int";
|
|
26
|
+
if (width == 2) return "ivec2";
|
|
27
|
+
return "ivec3";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
bool is_pass_through_builtin(const std::string &name) {
|
|
31
|
+
static const char *const kNames[] = {
|
|
32
|
+
"abs", "acos", "acosh", "asin", "asinh", "atan",
|
|
33
|
+
"atanh", "ceil", "clamp", "cos", "cosh", "cross",
|
|
34
|
+
"degrees", "determinant", "distance", "dot", "exp", "exp2",
|
|
35
|
+
"faceForward", "floor", "fma", "fract", "fwidth", "ldexp",
|
|
36
|
+
"length", "log", "log2", "max", "min", "mix",
|
|
37
|
+
"modf", "normalize", "pow", "radians", "reflect", "refract",
|
|
38
|
+
"roundEven", "sign", "sin", "sinh", "smoothstep", "sqrt",
|
|
39
|
+
"step", "tan", "tanh", "transpose", "trunc", "inverse",
|
|
40
|
+
"bitCount", "bitfieldReverse", "findMSB", "findLSB", "bitfieldExtract",
|
|
41
|
+
"bitfieldInsert", "packHalf2x16", "unpackHalf2x16", "packUnorm2x16"};
|
|
42
|
+
for (const char *candidate : kNames) {
|
|
43
|
+
if (name == candidate) return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
} // namespace
|
|
49
|
+
|
|
50
|
+
std::string Emitter::call_expr(const Expr &node) {
|
|
51
|
+
std::vector<std::string> args;
|
|
52
|
+
args.reserve(node.args.size());
|
|
53
|
+
for (const ExprPtr &arg : node.args) args.push_back(arg != nullptr ? expr(*arg) : "0");
|
|
54
|
+
auto joined = [&](size_t from) {
|
|
55
|
+
std::string text;
|
|
56
|
+
for (size_t i = from; i < args.size(); i++) {
|
|
57
|
+
if (i > from) text += ", ";
|
|
58
|
+
text += args[i];
|
|
59
|
+
}
|
|
60
|
+
return text;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/* Method call: `array.length()`. */
|
|
64
|
+
if (!node.text.empty() && node.text[0] == '.') {
|
|
65
|
+
if (node.text == ".length") {
|
|
66
|
+
const TypePtr base = node.args.empty() ? nullptr : type_of(*node.args[0]);
|
|
67
|
+
if (base != nullptr && base->kind == Kind::Array) {
|
|
68
|
+
if (base->length > 0) return std::to_string(base->length);
|
|
69
|
+
return "uint(" + args[0] + ".length())";
|
|
70
|
+
}
|
|
71
|
+
return std::to_string(base != nullptr ? base->width : 1);
|
|
72
|
+
}
|
|
73
|
+
fail("unsupported WGSL method '" + node.text + "'");
|
|
74
|
+
return "0";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/* Type constructors (vec3f(…), mat4x4f(…), S(…)). */
|
|
78
|
+
if (TypePtr constructed = type_by_name(module_, node.text)) {
|
|
79
|
+
return glsl_type(*constructed) + "(" + joined(0) + ")";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* User functions. */
|
|
83
|
+
if (const FuncDecl *function = find_function(node.text)) {
|
|
84
|
+
return "xg_" + function->name + "(" + joined(0) + ")";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const std::string &name = node.text;
|
|
88
|
+
|
|
89
|
+
/* -- textures ---------------------------------------------------------- */
|
|
90
|
+
if (name.compare(0, 13, "textureSample") == 0) {
|
|
91
|
+
if (node.args.size() < 3) {
|
|
92
|
+
fail("textureSample needs a texture, a sampler and coordinates");
|
|
93
|
+
return "vec4(0.0)";
|
|
94
|
+
}
|
|
95
|
+
const std::string texture = ident_of(node.args[0]);
|
|
96
|
+
const std::string sampler = ident_of(node.args[1]);
|
|
97
|
+
const int slot = pair_slot_for(texture, sampler);
|
|
98
|
+
if (slot < 0) {
|
|
99
|
+
fail("textureSample uses a texture or sampler that is not a module-level resource");
|
|
100
|
+
return "vec4(0.0)";
|
|
101
|
+
}
|
|
102
|
+
const std::string pair = "xg_tex" + std::to_string(slot);
|
|
103
|
+
const GlobalDecl *decl = find_global(texture);
|
|
104
|
+
const TypePtr type = decl != nullptr ? decl->type : nullptr;
|
|
105
|
+
const TextureDim dim = type != nullptr ? type->dim : TextureDim::D2;
|
|
106
|
+
std::string coords = args[2];
|
|
107
|
+
if (dim == TextureDim::D2Array && args.size() >= 4) {
|
|
108
|
+
coords = "vec3(" + coords + ", float(" + args[3] + "))";
|
|
109
|
+
}
|
|
110
|
+
if (name == "textureSampleCompare" || name == "textureSampleCompareLevel") {
|
|
111
|
+
/* WGSL (t, s, coords, reference); GLSL compares against a vec3/vec4. */
|
|
112
|
+
if (args.size() < 4) {
|
|
113
|
+
fail("textureSampleCompare needs a depth reference argument");
|
|
114
|
+
return "1.0";
|
|
115
|
+
}
|
|
116
|
+
const std::string combined =
|
|
117
|
+
dim == TextureDim::Cube
|
|
118
|
+
? "vec4(" + args[2] + ", " + args[3] + ")"
|
|
119
|
+
: "vec3(" + args[2] + ", " + args[3] + ")";
|
|
120
|
+
if (name == "textureSampleCompareLevel") {
|
|
121
|
+
return "textureLod(" + pair + ", " + combined + ", 0.0)";
|
|
122
|
+
}
|
|
123
|
+
return "texture(" + pair + ", " + combined + ")";
|
|
124
|
+
}
|
|
125
|
+
if (name == "textureSampleLevel") {
|
|
126
|
+
if (args.size() < 4) {
|
|
127
|
+
fail("textureSampleLevel needs a level argument");
|
|
128
|
+
return "vec4(0.0)";
|
|
129
|
+
}
|
|
130
|
+
return "textureLod(" + pair + ", " + coords + ", " + args[3] + ")";
|
|
131
|
+
}
|
|
132
|
+
if (name == "textureSampleBias") {
|
|
133
|
+
if (args.size() < 4) {
|
|
134
|
+
fail("textureSampleBias needs a bias argument");
|
|
135
|
+
return "vec4(0.0)";
|
|
136
|
+
}
|
|
137
|
+
return "texture(" + pair + ", " + coords + ", " + args[3] + ")";
|
|
138
|
+
}
|
|
139
|
+
if (name == "textureSampleGrad") {
|
|
140
|
+
if (args.size() < 5) {
|
|
141
|
+
fail("textureSampleGrad needs two gradient arguments");
|
|
142
|
+
return "vec4(0.0)";
|
|
143
|
+
}
|
|
144
|
+
return "textureGrad(" + pair + ", " + coords + ", " + args[3] + ", " + args[4] + ")";
|
|
145
|
+
}
|
|
146
|
+
return "texture(" + pair + ", " + coords + ")";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (name == "textureLoad") {
|
|
150
|
+
if (node.args.size() < 2) {
|
|
151
|
+
fail("textureLoad needs a texture and coordinates");
|
|
152
|
+
return "vec4(0.0)";
|
|
153
|
+
}
|
|
154
|
+
const std::string texture = ident_of(node.args[0]);
|
|
155
|
+
const GlobalDecl *decl = find_global(texture);
|
|
156
|
+
if (decl != nullptr && decl->type != nullptr && decl->type->kind == Kind::StorageTexture) {
|
|
157
|
+
return "imageLoad(" + global_expr(texture) + ", " + coord_width(
|
|
158
|
+
decl->type->dim == TextureDim::D3 || decl->type->dim == TextureDim::D2Array ? 3 : 2) +
|
|
159
|
+
"(" + args[1] + "))";
|
|
160
|
+
}
|
|
161
|
+
const int slot = first_pair_for(texture);
|
|
162
|
+
if (slot < 0) {
|
|
163
|
+
fail("textureLoad uses a texture that is not a module-level resource");
|
|
164
|
+
return "vec4(0.0)";
|
|
165
|
+
}
|
|
166
|
+
const TextureDim dim = decl != nullptr && decl->type != nullptr ? decl->type->dim : TextureDim::D2;
|
|
167
|
+
const int width = (dim == TextureDim::D3 || dim == TextureDim::D2Array) ? 3 : 2;
|
|
168
|
+
const std::string level = args.size() >= 3 ? "int(" + args[2] + ")" : "0";
|
|
169
|
+
return "texelFetch(xg_tex" + std::to_string(slot) + ", " + coord_width(width) + "(" + args[1] +
|
|
170
|
+
"), " + level + ")";
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (name == "textureStore") {
|
|
174
|
+
if (node.args.size() < 3) {
|
|
175
|
+
fail("textureStore needs a texture, coordinates and a value");
|
|
176
|
+
return "";
|
|
177
|
+
}
|
|
178
|
+
const std::string texture = ident_of(node.args[0]);
|
|
179
|
+
const GlobalDecl *decl = find_global(texture);
|
|
180
|
+
const int width = decl != nullptr && decl->type != nullptr &&
|
|
181
|
+
(decl->type->dim == TextureDim::D3 ||
|
|
182
|
+
decl->type->dim == TextureDim::D2Array)
|
|
183
|
+
? 3
|
|
184
|
+
: 2;
|
|
185
|
+
return "imageStore(" + global_expr(texture) + ", " + coord_width(width) + "(" + args[1] + "), " +
|
|
186
|
+
args[2] + ");\n";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (name == "textureDimensions") {
|
|
190
|
+
const std::string texture = ident_of(node.args[0]);
|
|
191
|
+
const int slot = first_pair_for(texture);
|
|
192
|
+
if (slot < 0) {
|
|
193
|
+
fail("textureDimensions uses a texture that is not a module-level resource");
|
|
194
|
+
return "uvec2(0u)";
|
|
195
|
+
}
|
|
196
|
+
const std::string level = args.size() >= 2 ? "int(" + args[1] + ")" : "0";
|
|
197
|
+
const GlobalDecl *decl = find_global(texture);
|
|
198
|
+
const TextureDim dim = decl != nullptr && decl->type != nullptr ? decl->type->dim : TextureDim::D2;
|
|
199
|
+
const std::string size = "textureSize(xg_tex" + std::to_string(slot) + ", " + level + ")";
|
|
200
|
+
if (dim == TextureDim::D1) return "uint(" + size + ")";
|
|
201
|
+
if (dim == TextureDim::D3) return "uvec3(" + size + ")";
|
|
202
|
+
return "uvec2(" + size + ")";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (name == "textureNumLevels") {
|
|
206
|
+
const int slot = first_pair_for(ident_of(node.args[0]));
|
|
207
|
+
if (slot < 0) return "0u";
|
|
208
|
+
return "textureQueryLevels(xg_tex" + std::to_string(slot) + ")";
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (name == "textureNumLayers") {
|
|
212
|
+
const int slot = first_pair_for(ident_of(node.args[0]));
|
|
213
|
+
if (slot < 0) return "0u";
|
|
214
|
+
return "textureQueryLevels(xg_tex" + std::to_string(slot) + ")";
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (name == "textureGather") {
|
|
218
|
+
if (node.args.size() < 4) {
|
|
219
|
+
fail("textureGather needs a component, texture, sampler and coordinates");
|
|
220
|
+
return "vec4(0.0)";
|
|
221
|
+
}
|
|
222
|
+
const int slot = pair_slot_for(ident_of(node.args[1]), ident_of(node.args[2]));
|
|
223
|
+
if (slot < 0) return "vec4(0.0)";
|
|
224
|
+
return "textureGather(xg_tex" + std::to_string(slot) + ", " + args[3] + ", " + args[0] + ")";
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/* -- casts, select and the small spelling differences ------------------ */
|
|
228
|
+
if (name == "select") {
|
|
229
|
+
if (node.args.size() < 3) return "0";
|
|
230
|
+
const TypePtr condition = type_of(*node.args[2]);
|
|
231
|
+
if (condition != nullptr && condition->kind == Kind::Vector) {
|
|
232
|
+
return "mix(" + args[0] + ", " + args[1] + ", " + args[2] + ")";
|
|
233
|
+
}
|
|
234
|
+
return "(" + args[2] + " ? " + args[1] + " : " + args[0] + ")";
|
|
235
|
+
}
|
|
236
|
+
if (name == "saturate") return "clamp(" + args[0] + ", 0.0, 1.0)";
|
|
237
|
+
if (name == "atan2") return "atan(" + joined(0) + ")";
|
|
238
|
+
if (name == "inverseSqrt") return "inversesqrt(" + joined(0) + ")";
|
|
239
|
+
if (name == "round") return "roundEven(" + joined(0) + ")";
|
|
240
|
+
if (name == "countOneBits") return "bitCount(" + joined(0) + ")";
|
|
241
|
+
if (name == "reverseBits") return "bitfieldReverse(" + joined(0) + ")";
|
|
242
|
+
if (name == "firstLeadingBit") return "findMSB(" + joined(0) + ")";
|
|
243
|
+
if (name == "firstTrailingBit") return "findLSB(" + joined(0) + ")";
|
|
244
|
+
if (name == "extractBits") return "bitfieldExtract(" + joined(0) + ")";
|
|
245
|
+
if (name == "insertBits") return "bitfieldInsert(" + joined(0) + ")";
|
|
246
|
+
if (name == "dpdx") return "dFdx(" + joined(0) + ")";
|
|
247
|
+
if (name == "dpdy") return "dFdy(" + joined(0) + ")";
|
|
248
|
+
if (name == "fwidth") return "fwidth(" + joined(0) + ")";
|
|
249
|
+
if (name == "workgroupBarrier" || name == "storageBarrier") return "barrier()";
|
|
250
|
+
if (name == "textureBarrier") return "textureBarrier()";
|
|
251
|
+
if (name == "f32" || name == "f16") return "float(" + joined(0) + ")";
|
|
252
|
+
if (name == "i32") return "int(" + joined(0) + ")";
|
|
253
|
+
if (name == "u32") return "uint(" + joined(0) + ")";
|
|
254
|
+
if (name == "bool") return "bool(" + joined(0) + ")";
|
|
255
|
+
|
|
256
|
+
if (name == "arrayLength") {
|
|
257
|
+
const Expr &operand = *node.args[0];
|
|
258
|
+
if (operand.kind == Expr::K::Unary && !operand.args.empty() &&
|
|
259
|
+
operand.args[0]->kind == Expr::K::Ident) {
|
|
260
|
+
const GlobalDecl *decl = find_global(operand.args[0]->text);
|
|
261
|
+
if (decl != nullptr && decl->type != nullptr && decl->type->kind == Kind::Array) {
|
|
262
|
+
if (decl->type->length > 0) return std::to_string(decl->type->length);
|
|
263
|
+
return "uint(" + expr(*operand.args[0]) + ".length())";
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return "0u";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (name.compare(0, 6, "atomic") == 0) {
|
|
270
|
+
fail("WGSL atomics are not supported by the WebGPU backend");
|
|
271
|
+
return "0u";
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (is_pass_through_builtin(name)) return name + "(" + joined(0) + ")";
|
|
275
|
+
|
|
276
|
+
fail("unsupported WGSL function '" + name + "'");
|
|
277
|
+
return name + "(" + joined(0) + ")";
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
} // namespace wgsl
|
|
281
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WGSL expression emission.
|
|
3
|
+
*
|
|
4
|
+
* WGSL and GLSL agree on most arithmetic, so the emitter only rewrites where
|
|
5
|
+
* the spelling or the operand order genuinely differs: vector comparisons,
|
|
6
|
+
* float `%`, texture sampling (SDL_GPU binds a texture and a sampler as one
|
|
7
|
+
* pair, so the pair's combined sampler replaces both arguments), array
|
|
8
|
+
* constructors and bitcasts.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
#include <cctype>
|
|
13
|
+
#include <cstdlib>
|
|
14
|
+
|
|
15
|
+
#include "wgsl_emit.h"
|
|
16
|
+
|
|
17
|
+
namespace xtgui {
|
|
18
|
+
namespace wgsl {
|
|
19
|
+
namespace {
|
|
20
|
+
|
|
21
|
+
std::string float_literal(const std::string &text) {
|
|
22
|
+
std::string value = text;
|
|
23
|
+
if (!value.empty()) {
|
|
24
|
+
const char last = value.back();
|
|
25
|
+
if (last == 'f' || last == 'F' || last == 'h' || last == 'H') value.pop_back();
|
|
26
|
+
}
|
|
27
|
+
if (value.find('.') == std::string::npos && value.find('e') == std::string::npos &&
|
|
28
|
+
value.find('E') == std::string::npos) {
|
|
29
|
+
value += ".0";
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
std::string int_literal(const std::string &text) {
|
|
35
|
+
if (!text.empty() && (text.back() == 'i' || text.back() == 'I')) return text.substr(0, text.size() - 1);
|
|
36
|
+
return text;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
std::string zero_for(Scalar scalar) {
|
|
40
|
+
switch (scalar) {
|
|
41
|
+
case Scalar::Bool: return "false";
|
|
42
|
+
case Scalar::I32: return "0";
|
|
43
|
+
case Scalar::U32: return "0u";
|
|
44
|
+
default: return "0.0";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const char *comparison_function(const std::string &op) {
|
|
49
|
+
if (op == "<") return "lessThan";
|
|
50
|
+
if (op == "<=") return "lessThanEqual";
|
|
51
|
+
if (op == ">") return "greaterThan";
|
|
52
|
+
if (op == ">=") return "greaterThanEqual";
|
|
53
|
+
return nullptr;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
bool is_pass_through_builtin(const std::string &name) {
|
|
57
|
+
static const char *const kNames[] = {
|
|
58
|
+
"abs", "acos", "acosh", "asin", "asinh", "atan",
|
|
59
|
+
"atanh", "ceil", "clamp", "cos", "cosh", "cross",
|
|
60
|
+
"degrees", "determinant", "distance", "dot", "exp", "exp2",
|
|
61
|
+
"faceForward", "floor", "fma", "fract", "fwidth", "ldexp",
|
|
62
|
+
"length", "log", "log2", "max", "min", "mix",
|
|
63
|
+
"modf", "normalize", "pow", "radians", "reflect", "refract",
|
|
64
|
+
"roundEven", "sign", "sin", "sinh", "smoothstep", "sqrt",
|
|
65
|
+
"step", "tan", "tanh", "transpose", "trunc", "inverse",
|
|
66
|
+
"bitCount", "bitfieldReverse", "findMSB", "findLSB", "bitfieldExtract",
|
|
67
|
+
"bitfieldInsert", "packHalf2x16", "unpackHalf2x16", "packUnorm2x16"};
|
|
68
|
+
for (const char *candidate : kNames) {
|
|
69
|
+
if (name == candidate) return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
std::string ident_of(const ExprPtr &node) {
|
|
75
|
+
return node != nullptr && node->kind == Expr::K::Ident ? node->text : std::string();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
} // namespace
|
|
79
|
+
|
|
80
|
+
std::string Emitter::cast_to(const Type &target, const std::string &value) const {
|
|
81
|
+
return glsl_type(target) + "(" + value + ")";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
std::string Emitter::expr(const Expr &node) {
|
|
85
|
+
switch (node.kind) {
|
|
86
|
+
case Expr::K::Int: return int_literal(node.text);
|
|
87
|
+
case Expr::K::Float: return float_literal(node.text);
|
|
88
|
+
case Expr::K::Bool: return node.text;
|
|
89
|
+
case Expr::K::Ident: {
|
|
90
|
+
if (const Local *local = lookup_local(node.text)) return local->name;
|
|
91
|
+
if (const GlobalDecl *global = find_global(node.text)) {
|
|
92
|
+
if (global->type != nullptr && global->type->kind == Kind::Sampler) return "";
|
|
93
|
+
if (global->type != nullptr && global->type->kind == Kind::Texture) {
|
|
94
|
+
return global_expr(node.text);
|
|
95
|
+
}
|
|
96
|
+
return global_expr(node.text);
|
|
97
|
+
}
|
|
98
|
+
return "xg_" + node.text;
|
|
99
|
+
}
|
|
100
|
+
case Expr::K::Unary: {
|
|
101
|
+
if (node.args.empty()) return "0";
|
|
102
|
+
if (node.text == "&" || node.text == "*") {
|
|
103
|
+
fail("WGSL pointers are not supported by the WebGPU backend");
|
|
104
|
+
return expr(*node.args[0]);
|
|
105
|
+
}
|
|
106
|
+
return "(" + node.text + expr(*node.args[0]) + ")";
|
|
107
|
+
}
|
|
108
|
+
case Expr::K::Binary: {
|
|
109
|
+
if (node.args.size() < 2) return "0";
|
|
110
|
+
const std::string left = expr(*node.args[0]);
|
|
111
|
+
const std::string right = expr(*node.args[1]);
|
|
112
|
+
const TypePtr left_type = type_of(*node.args[0]);
|
|
113
|
+
const TypePtr right_type = type_of(*node.args[1]);
|
|
114
|
+
const int width = left_type != nullptr && left_type->width > 1
|
|
115
|
+
? left_type->width
|
|
116
|
+
: (right_type != nullptr ? right_type->width : 1);
|
|
117
|
+
if (width > 1 && (node.text == "<" || node.text == "<=" || node.text == ">" ||
|
|
118
|
+
node.text == ">=")) {
|
|
119
|
+
return std::string(comparison_function(node.text)) + "(" + left + ", " + right + ")";
|
|
120
|
+
}
|
|
121
|
+
if (node.text == "%" && left_type != nullptr && left_type->is_numeric()) {
|
|
122
|
+
/* GLSL `%` is integer-only; WGSL defines float remainder as `mod`. */
|
|
123
|
+
if (left_type->scalar == Scalar::F32) return "mod(" + left + ", " + right + ")";
|
|
124
|
+
return "(" + left + " % " + right + ")";
|
|
125
|
+
}
|
|
126
|
+
return "(" + left + " " + node.text + " " + right + ")";
|
|
127
|
+
}
|
|
128
|
+
case Expr::K::Member: {
|
|
129
|
+
if (node.args.empty()) return "0";
|
|
130
|
+
const std::string base = expr(*node.args[0]);
|
|
131
|
+
const TypePtr base_type = type_of(*node.args[0]);
|
|
132
|
+
if (base_type != nullptr && base_type->kind == Kind::Struct) {
|
|
133
|
+
return base + "." + member_name(node.member);
|
|
134
|
+
}
|
|
135
|
+
if (node.member == "length") {
|
|
136
|
+
/* A fixed-size array's length is known statically. */
|
|
137
|
+
if (base_type != nullptr && base_type->kind == Kind::Array) {
|
|
138
|
+
return std::to_string(base_type->length);
|
|
139
|
+
}
|
|
140
|
+
return "uint(" + base + ".length())";
|
|
141
|
+
}
|
|
142
|
+
return base + "." + node.member;
|
|
143
|
+
}
|
|
144
|
+
case Expr::K::Index: {
|
|
145
|
+
if (node.args.size() < 2) return "0";
|
|
146
|
+
return expr(*node.args[0]) + "[" + expr(*node.args[1]) + "]";
|
|
147
|
+
}
|
|
148
|
+
case Expr::K::ArrayCtor: {
|
|
149
|
+
if (node.type == nullptr || node.type->elem == nullptr) return "0";
|
|
150
|
+
const std::string type = glsl_type(*node.type);
|
|
151
|
+
std::string body;
|
|
152
|
+
for (size_t i = 0; i < node.args.size(); i++) {
|
|
153
|
+
if (i > 0) body += ", ";
|
|
154
|
+
body += expr(*node.args[i]);
|
|
155
|
+
}
|
|
156
|
+
if (node.args.empty()) {
|
|
157
|
+
if (node.type->elem->kind == Kind::Struct) {
|
|
158
|
+
fail("a zero-initialised array of structs is not supported; list its elements");
|
|
159
|
+
return type;
|
|
160
|
+
}
|
|
161
|
+
for (int i = 0; i < node.type->length; i++) {
|
|
162
|
+
if (i > 0) body += ", ";
|
|
163
|
+
body += zero_for(node.type->elem->scalar);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return type + "(" + body + ")";
|
|
167
|
+
}
|
|
168
|
+
case Expr::K::Bitcast: {
|
|
169
|
+
const TypePtr source = node.args.empty() ? nullptr : type_of(*node.args[0]);
|
|
170
|
+
const TypePtr target = node.type;
|
|
171
|
+
if (source == nullptr || target == nullptr) return "0";
|
|
172
|
+
const std::string value = node.args.empty() ? "0" : expr(*node.args[0]);
|
|
173
|
+
const bool source_float = source->scalar == Scalar::F32;
|
|
174
|
+
const bool target_float = target->scalar == Scalar::F32;
|
|
175
|
+
if (source_float && !target_float) {
|
|
176
|
+
return (target->scalar == Scalar::U32 ? "floatBitsToUint" : "floatBitsToInt") + std::string("(") +
|
|
177
|
+
value + ")";
|
|
178
|
+
}
|
|
179
|
+
if (!source_float && target_float) {
|
|
180
|
+
return (source->scalar == Scalar::U32 ? "uintBitsToFloat" : "intBitsToFloat") + std::string("(") +
|
|
181
|
+
value + ")";
|
|
182
|
+
}
|
|
183
|
+
return cast_to(*target, value);
|
|
184
|
+
}
|
|
185
|
+
case Expr::K::Call: return call_expr(node);
|
|
186
|
+
default: break;
|
|
187
|
+
}
|
|
188
|
+
return "0";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
} // namespace wgsl
|
|
192
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WGSL statement emission.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include <string>
|
|
6
|
+
|
|
7
|
+
#include "wgsl_emit.h"
|
|
8
|
+
|
|
9
|
+
namespace xtgui {
|
|
10
|
+
namespace wgsl {
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
std::string spaces(int indent) { return std::string((size_t)indent * 2, ' '); }
|
|
14
|
+
|
|
15
|
+
std::string zero_value(const Type &type) {
|
|
16
|
+
if (type.kind == Kind::Scalar) {
|
|
17
|
+
switch (type.scalar) {
|
|
18
|
+
case Scalar::Bool: return "false";
|
|
19
|
+
case Scalar::I32: return "0";
|
|
20
|
+
case Scalar::U32: return "0u";
|
|
21
|
+
default: return "0.0";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (type.kind == Kind::Vector) {
|
|
25
|
+
return glsl_type(type) + "(" + zero_value(*make_scalar(type.scalar)) + ")";
|
|
26
|
+
}
|
|
27
|
+
return std::string();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
std::string assignment_text(Emitter *emitter, const Stmt &node) {
|
|
31
|
+
if (node.expr == nullptr) return std::string();
|
|
32
|
+
const std::string left = emitter->expr(*node.expr);
|
|
33
|
+
if (node.op == "++" || node.op == "--") return left + node.op + ";";
|
|
34
|
+
if (node.value == nullptr) return std::string();
|
|
35
|
+
return left + " " + node.op + " " + emitter->expr(*node.value) + ";";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
} // namespace
|
|
39
|
+
|
|
40
|
+
std::string Emitter::statement(const Stmt &node, int indent) {
|
|
41
|
+
const std::string pad = spaces(indent);
|
|
42
|
+
switch (node.kind) {
|
|
43
|
+
case Stmt::K::Block: {
|
|
44
|
+
std::string text = pad + "{\n";
|
|
45
|
+
push_scope();
|
|
46
|
+
for (const StmtPtr &child : node.body) {
|
|
47
|
+
if (child != nullptr) text += statement(*child, indent + 1);
|
|
48
|
+
}
|
|
49
|
+
pop_scope();
|
|
50
|
+
text += pad + "}\n";
|
|
51
|
+
return text;
|
|
52
|
+
}
|
|
53
|
+
case Stmt::K::Var: {
|
|
54
|
+
TypePtr type = node.type;
|
|
55
|
+
if (type == nullptr && node.expr != nullptr) type = type_of(*node.expr);
|
|
56
|
+
if (type == nullptr || type->kind == Kind::Unknown) {
|
|
57
|
+
fail("cannot infer the type of '" + node.name + "'");
|
|
58
|
+
return std::string();
|
|
59
|
+
}
|
|
60
|
+
declare(node.name, type);
|
|
61
|
+
const Local *local = lookup_local(node.name);
|
|
62
|
+
std::string text = pad + glsl_decl(*type, local->name);
|
|
63
|
+
if (node.expr != nullptr) {
|
|
64
|
+
text += " = " + expr(*node.expr);
|
|
65
|
+
} else {
|
|
66
|
+
const std::string zero = zero_value(*type);
|
|
67
|
+
if (!zero.empty()) text += " = " + zero;
|
|
68
|
+
}
|
|
69
|
+
return text + ";\n";
|
|
70
|
+
}
|
|
71
|
+
case Stmt::K::Assign: {
|
|
72
|
+
const std::string text = assignment_text(this, node);
|
|
73
|
+
return text.empty() ? std::string() : pad + text + "\n";
|
|
74
|
+
}
|
|
75
|
+
case Stmt::K::Expr:
|
|
76
|
+
return node.expr == nullptr ? std::string() : pad + expr(*node.expr) + ";\n";
|
|
77
|
+
case Stmt::K::If: {
|
|
78
|
+
std::string text = pad + "if (" + (node.expr != nullptr ? expr(*node.expr) : "false") + ") ";
|
|
79
|
+
if (node.body.empty() || node.body[0] == nullptr) {
|
|
80
|
+
text += "{\n" + pad + "}\n";
|
|
81
|
+
} else {
|
|
82
|
+
text += statement(*node.body[0], indent);
|
|
83
|
+
if (!text.empty() && text.back() == '\n') text.pop_back();
|
|
84
|
+
text += "\n";
|
|
85
|
+
}
|
|
86
|
+
for (const StmtPtr &otherwise : node.otherwise) {
|
|
87
|
+
text += pad + "else ";
|
|
88
|
+
if (otherwise != nullptr) {
|
|
89
|
+
text += statement(*otherwise, indent);
|
|
90
|
+
if (!text.empty() && text.back() == '\n') text.pop_back();
|
|
91
|
+
text += "\n";
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return text;
|
|
95
|
+
}
|
|
96
|
+
case Stmt::K::For: {
|
|
97
|
+
std::string init;
|
|
98
|
+
std::string cond;
|
|
99
|
+
std::string update;
|
|
100
|
+
push_scope();
|
|
101
|
+
if (node.init != nullptr) {
|
|
102
|
+
if (node.init->kind == Stmt::K::Var) {
|
|
103
|
+
init = statement(*node.init, 0);
|
|
104
|
+
while (!init.empty() && (init.back() == '\n' || init.back() == ';')) init.pop_back();
|
|
105
|
+
} else {
|
|
106
|
+
init = assignment_text(this, *node.init);
|
|
107
|
+
if (!init.empty() && init.back() == ';') init.pop_back();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (node.cond != nullptr && node.cond->expr != nullptr) cond = expr(*node.cond->expr);
|
|
111
|
+
if (node.update != nullptr) {
|
|
112
|
+
update = assignment_text(this, *node.update);
|
|
113
|
+
if (!update.empty() && update.back() == ';') update.pop_back();
|
|
114
|
+
}
|
|
115
|
+
std::string text = pad + "for (" + init + "; " + cond + "; " + update + ") ";
|
|
116
|
+
continuing_stack_.push_back(std::string());
|
|
117
|
+
if (!node.body.empty() && node.body[0] != nullptr) {
|
|
118
|
+
text += statement(*node.body[0], indent);
|
|
119
|
+
if (!text.empty() && text.back() == '\n') text.pop_back();
|
|
120
|
+
text += "\n";
|
|
121
|
+
}
|
|
122
|
+
continuing_stack_.pop_back();
|
|
123
|
+
pop_scope();
|
|
124
|
+
return text;
|
|
125
|
+
}
|
|
126
|
+
case Stmt::K::While: {
|
|
127
|
+
std::string text = pad + "while (" + (node.expr != nullptr ? expr(*node.expr) : "false") + ") ";
|
|
128
|
+
if (!node.body.empty() && node.body[0] != nullptr) {
|
|
129
|
+
text += statement(*node.body[0], indent);
|
|
130
|
+
if (!text.empty() && text.back() == '\n') text.pop_back();
|
|
131
|
+
text += "\n";
|
|
132
|
+
}
|
|
133
|
+
return text;
|
|
134
|
+
}
|
|
135
|
+
case Stmt::K::Loop: {
|
|
136
|
+
std::string continuing;
|
|
137
|
+
if (node.continuing != nullptr) continuing = statement(*node.continuing, indent + 1);
|
|
138
|
+
continuing_stack_.push_back(continuing);
|
|
139
|
+
std::string body;
|
|
140
|
+
for (const StmtPtr &child : node.body) {
|
|
141
|
+
if (child != nullptr) body += statement(*child, indent + 1);
|
|
142
|
+
}
|
|
143
|
+
continuing_stack_.pop_back();
|
|
144
|
+
std::string text = pad + "for (;;) {\n" + body;
|
|
145
|
+
if (!continuing.empty()) text += continuing;
|
|
146
|
+
text += pad + "}\n";
|
|
147
|
+
return text;
|
|
148
|
+
}
|
|
149
|
+
case Stmt::K::Break: return pad + "break;\n";
|
|
150
|
+
case Stmt::K::Continue: {
|
|
151
|
+
std::string text;
|
|
152
|
+
if (!continuing_stack_.empty()) text = continuing_stack_.back();
|
|
153
|
+
return text + pad + "continue;\n";
|
|
154
|
+
}
|
|
155
|
+
case Stmt::K::Return:
|
|
156
|
+
if (node.expr == nullptr) return pad + "return;\n";
|
|
157
|
+
return pad + "return " + expr(*node.expr) + ";\n";
|
|
158
|
+
case Stmt::K::Discard: return pad + "discard;\n";
|
|
159
|
+
case Stmt::K::Switch: {
|
|
160
|
+
std::string text = pad + "switch (" + (node.expr != nullptr ? expr(*node.expr) : "0") + ") {\n";
|
|
161
|
+
for (size_t i = 0; i < node.cases.size(); i++) {
|
|
162
|
+
if (node.cases[i] == nullptr) {
|
|
163
|
+
text += pad + " default: {\n";
|
|
164
|
+
} else {
|
|
165
|
+
text += pad + " case " + expr(*node.cases[i]) + ": {\n";
|
|
166
|
+
}
|
|
167
|
+
if (i < node.case_bodies.size() && node.case_bodies[i] != nullptr) {
|
|
168
|
+
if (node.case_bodies[i]->kind == Stmt::K::Block) {
|
|
169
|
+
for (const StmtPtr &child : node.case_bodies[i]->body) {
|
|
170
|
+
if (child != nullptr) text += statement(*child, indent + 2);
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
text += statement(*node.case_bodies[i], indent + 2);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
text += pad + " break;\n";
|
|
177
|
+
text += pad + " }\n";
|
|
178
|
+
}
|
|
179
|
+
return text + pad + "}\n";
|
|
180
|
+
}
|
|
181
|
+
default: break;
|
|
182
|
+
}
|
|
183
|
+
return std::string();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
std::string Emitter::emit_user_function(const FuncDecl &func) {
|
|
187
|
+
push_scope();
|
|
188
|
+
std::string params;
|
|
189
|
+
for (size_t i = 0; i < func.params.size(); i++) {
|
|
190
|
+
const Param ¶m = func.params[i];
|
|
191
|
+
if (i > 0) params += ", ";
|
|
192
|
+
declare(param.name, param.type);
|
|
193
|
+
const Local *local = lookup_local(param.name);
|
|
194
|
+
params += glsl_decl(*param.type, local->name);
|
|
195
|
+
}
|
|
196
|
+
Type void_type;
|
|
197
|
+
void_type.kind = Kind::Void;
|
|
198
|
+
std::string text = glsl_type(func.ret != nullptr ? *func.ret : void_type) + " xg_" + func.name +
|
|
199
|
+
"(" + params + ") {\n";
|
|
200
|
+
for (const StmtPtr &statement_node : func.body) {
|
|
201
|
+
if (statement_node != nullptr) text += statement(*statement_node, 1);
|
|
202
|
+
}
|
|
203
|
+
text += "}\n";
|
|
204
|
+
pop_scope();
|
|
205
|
+
return text;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
} // namespace wgsl
|
|
209
|
+
} // namespace xtgui
|