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.
- package/dist/src/ast/expressions.d.ts +6 -0
- package/dist/src/checker/type-infer.js +1 -1
- package/dist/src/checker/type-infer.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/calls/literals.js +4 -1
- package/dist/src/codegen/generator/calls/literals.js.map +1 -1
- package/dist/src/codegen/generator/expressions/assignment.js +3 -1
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
- package/dist/src/codegen/generator/expressions/operators.js +2 -6
- package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
- package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
- package/dist/src/codegen/generator/expressions/primary.js +44 -7
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
- package/dist/src/driver/bundler/commonjs.js +1 -1
- package/dist/src/driver/bundler/commonjs.js.map +1 -1
- package/dist/src/driver/bundler/symbols.js +6 -0
- package/dist/src/driver/bundler/symbols.js.map +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/src/extensions/node/index.js +2 -0
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/readline/index.d.ts +12 -0
- package/dist/src/extensions/node/readline/index.js +27 -0
- package/dist/src/extensions/node/readline/index.js.map +1 -0
- package/dist/src/parser/declarations/functions.js +6 -0
- package/dist/src/parser/declarations/functions.js.map +1 -1
- package/dist/src/parser/literals.js +1 -1
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/e2e/compile-modules.test.js +22 -0
- package/dist/tests/e2e/compile-modules.test.js.map +1 -1
- package/dist/tests/e2e/date-parse.test.js +17 -0
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-browser.test.js +61 -0
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +39 -0
- package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-features.test.js +100 -0
- package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
- package/dist/tests/e2e/gui-dom.test.js +34 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
- package/dist/tests/e2e/gui-errors.test.js +68 -0
- package/dist/tests/e2e/gui-errors.test.js.map +1 -0
- 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-webgl2-shadow-sampler.test.js +7 -1
- package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +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/e2e/harness.d.ts +4 -0
- package/dist/tests/e2e/harness.js +2 -0
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/readline.test.d.ts +7 -0
- package/dist/tests/e2e/readline.test.js +73 -0
- package/dist/tests/e2e/readline.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/api.md +15 -0
- package/doc/gui/dom.md +7 -2
- package/doc/gui/known-issues.md +2 -1
- package/doc/gui/layout.md +5 -1
- package/doc/gui/webgpu.md +121 -0
- package/doc/gui.md +12 -1
- package/doc/implemented/node/readline.md +34 -0
- package/doc/implemented/node.md +1 -0
- package/doc/unimplemented/node/modules.md +2 -3
- package/doc/unimplemented/node.md +2 -2
- package/doc/zh-CN/gui/api.md +11 -0
- package/doc/zh-CN/gui/dom.md +6 -2
- package/doc/zh-CN/gui/known-issues.md +2 -1
- package/doc/zh-CN/gui/layout.md +4 -1
- package/doc/zh-CN/gui/webgpu.md +111 -0
- package/doc/zh-CN/gui.md +10 -1
- package/doc/zh-CN/implemented/node/readline.md +33 -0
- package/doc/zh-CN/implemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/node/modules.md +1 -2
- package/doc/zh-CN/unimplemented/node.md +2 -2
- package/llms.txt +1 -0
- package/package.json +7 -7
- package/runtime/ext_gui/browser.cpp +10 -0
- package/runtime/ext_gui/browser_abort.cpp +2 -2
- package/runtime/ext_gui/browser_loop.cpp +1 -0
- package/runtime/ext_gui/browser_media.cpp +1 -1
- 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/dom_api_canvas_image.cpp +2 -1
- package/runtime/ext_gui/dom_api_events.cpp +1 -1
- package/runtime/ext_gui/dom_api_internal.h +2 -0
- package/runtime/ext_gui/dom_api_node.cpp +18 -12
- package/runtime/ext_gui/dom_api_node_read.cpp +27 -0
- package/runtime/ext_gui/dom_api_proto.cpp +2 -0
- package/runtime/ext_gui/error_report.cpp +166 -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.cpp +16 -0
- package/runtime/ext_gui/gui_canvas.cpp +18 -0
- package/runtime/ext_gui/gui_engine.h +13 -0
- package/runtime/ext_gui/gui_events.cpp +4 -1
- package/runtime/ext_gui/layout_inline.cpp +46 -14
- package/runtime/ext_gui/paint.cpp +22 -0
- package/runtime/ext_gui/renderer_image.cpp +6 -0
- package/runtime/ext_gui/script.cpp +1 -1
- package/runtime/ext_gui/style.cpp +1 -0
- package/runtime/ext_gui/style.h +13 -0
- package/runtime/ext_gui/style_print.cpp +99 -0
- package/runtime/ext_gui/style_text.cpp +79 -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/webgl_texture.cpp +0 -8
- 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/runtime/ext_gui/window.cpp +22 -8
- package/runtime/ext_gui/window_emit.cpp +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +2 -0
- package/runtime/ext_node/readline/readline.c +572 -0
- package/runtime/rt.h +15 -0
- package/runtime/rt_internal.h +3 -2
- package/runtime/xt_io/exception.inc +29 -6
- package/runtime/xt_loop.c +36 -1
- package/runtime/xt_promise.c +26 -2
- package/runtime/xt_stdlib2/date.inc +2 -2
- package/runtime/xt_typed_array/elements.inc +0 -9
- package/runtime/xt_url.c +0 -6
- package/runtime/xt_url_object.c +1 -1
- package/src/ast/expressions.ts +6 -0
- package/src/checker/type-infer.ts +1 -1
- package/src/codegen/generator/calls/literals.ts +4 -1
- package/src/codegen/generator/expressions/assignment.ts +3 -1
- package/src/codegen/generator/expressions/operators.ts +2 -6
- package/src/codegen/generator/expressions/primary.ts +58 -6
- package/src/driver/bundler/commonjs.ts +1 -1
- package/src/driver/bundler/symbols.ts +6 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +24 -0
- package/src/extensions/gui/index.ts +39 -0
- package/src/extensions/node/index.ts +2 -0
- package/src/extensions/node/readline/index.ts +31 -0
- package/src/parser/declarations/functions.ts +6 -0
- package/src/parser/literals.ts +1 -1
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WebGPU prototype/method dispatch and host types.
|
|
3
|
+
*
|
|
4
|
+
* Every WebGPU interface is a prototype object whose members are closures that
|
|
5
|
+
* capture one index into `g_methods`. The dispatcher validates the receiver's
|
|
6
|
+
* kind, resolves the native object and calls the handler, so a method called
|
|
7
|
+
* on the wrong interface behaves like a browser's "Illegal invocation".
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "webgpu_bindings.h"
|
|
11
|
+
|
|
12
|
+
#include <map>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <vector>
|
|
15
|
+
|
|
16
|
+
namespace xtgui {
|
|
17
|
+
namespace webgpu {
|
|
18
|
+
namespace {
|
|
19
|
+
|
|
20
|
+
struct Method {
|
|
21
|
+
Kind kind = Kind::Gpu;
|
|
22
|
+
std::string name;
|
|
23
|
+
Handler handler = nullptr;
|
|
24
|
+
Getter getter = nullptr;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
std::vector<Method> g_methods;
|
|
28
|
+
std::map<int, xt_value> g_protos; // Kind -> prototype
|
|
29
|
+
std::map<int, xt_value> g_errors; // ErrorKind -> prototype
|
|
30
|
+
std::map<std::string, xt_value> g_types;
|
|
31
|
+
std::map<std::string, xt_value> g_namespaces;
|
|
32
|
+
xt_value g_gpu = XT_UNDEFINED;
|
|
33
|
+
xt_value g_device_lost = XT_UNDEFINED;
|
|
34
|
+
bool g_registered = false;
|
|
35
|
+
xt_value g_plain_proto = XT_UNDEFINED;
|
|
36
|
+
xt_value g_plain_ctor = XT_UNDEFINED;
|
|
37
|
+
|
|
38
|
+
xt_value host_ctor(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
39
|
+
(void)self;
|
|
40
|
+
(void)env;
|
|
41
|
+
(void)argc;
|
|
42
|
+
(void)argv;
|
|
43
|
+
throw_type_error("Illegal constructor");
|
|
44
|
+
return XT_UNDEFINED;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A constructor value whose `.prototype` is `proto` (for `instanceof`). */
|
|
48
|
+
xt_value constructor_for(const std::string &name, xt_value proto) {
|
|
49
|
+
xt_value name_value = xt_string_from_cstr(name.c_str());
|
|
50
|
+
xt_value ctor = xt_closure_new((void *)host_ctor, 1, &name_value);
|
|
51
|
+
xt_function_set_metadata(ctor, xt_string_from_cstr(name.c_str()), 0);
|
|
52
|
+
xt_function_set_prototype(ctor, proto);
|
|
53
|
+
xt_object_set(proto, xt_string_from_cstr("constructor"), ctor);
|
|
54
|
+
return ctor;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Kind kind_by_name(const std::string &name, bool *found) {
|
|
58
|
+
*found = true;
|
|
59
|
+
if (name == "GPU") return Kind::Gpu;
|
|
60
|
+
if (name == "GPUAdapter") return Kind::Adapter;
|
|
61
|
+
if (name == "GPUDevice") return Kind::Device;
|
|
62
|
+
if (name == "GPUQueue") return Kind::Queue;
|
|
63
|
+
if (name == "GPUBuffer") return Kind::Buffer;
|
|
64
|
+
if (name == "GPUTexture") return Kind::Texture;
|
|
65
|
+
if (name == "GPUTextureView") return Kind::TextureView;
|
|
66
|
+
if (name == "GPUSampler") return Kind::Sampler;
|
|
67
|
+
if (name == "GPUShaderModule") return Kind::ShaderModule;
|
|
68
|
+
if (name == "GPUBindGroupLayout") return Kind::BindGroupLayout;
|
|
69
|
+
if (name == "GPUPipelineLayout") return Kind::PipelineLayout;
|
|
70
|
+
if (name == "GPUBindGroup") return Kind::BindGroup;
|
|
71
|
+
if (name == "GPURenderPipeline") return Kind::RenderPipeline;
|
|
72
|
+
if (name == "GPUComputePipeline") return Kind::ComputePipeline;
|
|
73
|
+
if (name == "GPUCommandEncoder") return Kind::CommandEncoder;
|
|
74
|
+
if (name == "GPURenderPassEncoder") return Kind::RenderPassEncoder;
|
|
75
|
+
if (name == "GPUComputePassEncoder") return Kind::ComputePassEncoder;
|
|
76
|
+
if (name == "GPURenderBundleEncoder") return Kind::RenderBundleEncoder;
|
|
77
|
+
if (name == "GPURenderBundle") return Kind::RenderBundle;
|
|
78
|
+
if (name == "GPUQuerySet") return Kind::QuerySet;
|
|
79
|
+
if (name == "GPUCanvasContext") return Kind::CanvasContext;
|
|
80
|
+
*found = false;
|
|
81
|
+
return Kind::Gpu;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
xt_value dispatch(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
85
|
+
const int index = (int)xt_to_number(xt_closure_env(env, 0));
|
|
86
|
+
if (index < 0 || (size_t)index >= g_methods.size()) return XT_UNDEFINED;
|
|
87
|
+
const Method &method = g_methods[(size_t)index];
|
|
88
|
+
if (!XT_IS_OBJECT(self)) {
|
|
89
|
+
throw_type_error("Illegal invocation: " + method.name);
|
|
90
|
+
return XT_UNDEFINED;
|
|
91
|
+
}
|
|
92
|
+
xt_value kind_value = xt_object_get_cstr(self, "__xt_webgpu_kind");
|
|
93
|
+
if (!XT_IS_NUMBER(kind_value) || (Kind)xt_to_number(kind_value) != method.kind) {
|
|
94
|
+
throw_type_error("Illegal invocation: " + method.name);
|
|
95
|
+
return XT_UNDEFINED;
|
|
96
|
+
}
|
|
97
|
+
const uint32_t id = (uint32_t)xt_to_number(xt_object_get_cstr(self, "__xt_webgpu_id"));
|
|
98
|
+
std::shared_ptr<Object> object = get_object(id, method.kind);
|
|
99
|
+
if (object == nullptr) {
|
|
100
|
+
throw_type_error("This WebGPU object has been destroyed");
|
|
101
|
+
return XT_UNDEFINED;
|
|
102
|
+
}
|
|
103
|
+
if (method.getter != nullptr) return method.getter(*object);
|
|
104
|
+
return method.handler(*object, argc, argv);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void ensure_registered() {
|
|
108
|
+
if (g_registered) return;
|
|
109
|
+
g_registered = true;
|
|
110
|
+
register_gpu_methods();
|
|
111
|
+
register_resource_methods();
|
|
112
|
+
register_buffer_methods();
|
|
113
|
+
register_pipeline_methods();
|
|
114
|
+
register_queue_methods();
|
|
115
|
+
register_command_methods();
|
|
116
|
+
register_render_pass_methods();
|
|
117
|
+
register_compute_pass_methods();
|
|
118
|
+
register_canvas_methods();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
xt_value namespace_object(const char *name) {
|
|
122
|
+
auto found = g_namespaces.find(name);
|
|
123
|
+
if (found != g_namespaces.end()) return found->second;
|
|
124
|
+
xt_value object = xt_object_new();
|
|
125
|
+
g_namespaces[name] = object;
|
|
126
|
+
xt_gc_add_root(&g_namespaces[name]);
|
|
127
|
+
return object;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
void set_number_prop(xt_value object, const char *name, double value) {
|
|
131
|
+
xt_object_set(object, xt_string_from_cstr(name), xt_number(value));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
xt_value gpu_feature_has(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
135
|
+
(void)self;
|
|
136
|
+
(void)env;
|
|
137
|
+
(void)argc;
|
|
138
|
+
(void)argv;
|
|
139
|
+
return XT_FALSE;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
} // namespace
|
|
143
|
+
|
|
144
|
+
void add_method(Kind kind, const char *name, Handler handler) {
|
|
145
|
+
Method method;
|
|
146
|
+
method.kind = kind;
|
|
147
|
+
method.name = name;
|
|
148
|
+
method.handler = handler;
|
|
149
|
+
g_methods.push_back(std::move(method));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
void add_getter(Kind kind, const char *name, Getter getter) {
|
|
153
|
+
Method method;
|
|
154
|
+
method.kind = kind;
|
|
155
|
+
method.name = name;
|
|
156
|
+
method.getter = getter;
|
|
157
|
+
g_methods.push_back(std::move(method));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
xt_value proto_for(Kind kind) {
|
|
161
|
+
ensure_registered();
|
|
162
|
+
auto found = g_protos.find((int)kind);
|
|
163
|
+
if (found != g_protos.end()) return found->second;
|
|
164
|
+
xt_value proto = xt_object_new();
|
|
165
|
+
g_protos[(int)kind] = proto;
|
|
166
|
+
xt_gc_add_root(&g_protos[(int)kind]);
|
|
167
|
+
for (size_t i = 0; i < g_methods.size(); i++) {
|
|
168
|
+
const Method &method = g_methods[i];
|
|
169
|
+
if (method.kind != kind) continue;
|
|
170
|
+
xt_value index = xt_number((double)i);
|
|
171
|
+
xt_value closure = xt_closure_new((void *)dispatch, 1, &index);
|
|
172
|
+
if (method.getter != nullptr) {
|
|
173
|
+
xt_object_define_getter(proto, xt_string_from_cstr(method.name.c_str()), closure);
|
|
174
|
+
} else {
|
|
175
|
+
xt_object_set(proto, xt_string_from_cstr(method.name.c_str()), closure);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return proto;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
xt_value error_proto(int kind) {
|
|
182
|
+
ensure_registered();
|
|
183
|
+
auto found = g_errors.find(kind);
|
|
184
|
+
if (found != g_errors.end()) return found->second;
|
|
185
|
+
xt_value proto = xt_object_new();
|
|
186
|
+
g_errors[kind] = proto;
|
|
187
|
+
xt_gc_add_root(&g_errors[kind]);
|
|
188
|
+
return proto;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
xt_value device_lost_proto() {
|
|
192
|
+
if (XT_IS_OBJECT(g_device_lost)) return g_device_lost;
|
|
193
|
+
g_device_lost = xt_object_new();
|
|
194
|
+
xt_gc_add_root(&g_device_lost);
|
|
195
|
+
return g_device_lost;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
xt_value listener_list(Device &device) {
|
|
199
|
+
if (!XT_IS_OBJECT(device.js)) return xt_array_new(0, nullptr);
|
|
200
|
+
xt_value list = xt_object_get_cstr(device.js, "__xt_webgpu_listeners");
|
|
201
|
+
if (!XT_IS_ARRAY(list)) {
|
|
202
|
+
list = xt_array_new(0, nullptr);
|
|
203
|
+
xt_object_set(device.js, xt_string_from_cstr("__xt_webgpu_listeners"), list);
|
|
204
|
+
}
|
|
205
|
+
return list;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
void dispatch_uncaptured_error(Device &device, xt_value error) {
|
|
209
|
+
if (!XT_IS_OBJECT(device.js)) return;
|
|
210
|
+
xt_value list = listener_list(device);
|
|
211
|
+
const uint32_t count = (uint32_t)xt_to_number(xt_array_length(list));
|
|
212
|
+
if (count == 0) return;
|
|
213
|
+
xt_value event = xt_object_new();
|
|
214
|
+
xt_object_set(event, xt_string_from_cstr("type"), xt_string_from_cstr("uncapturederror"));
|
|
215
|
+
xt_object_set(event, xt_string_from_cstr("error"), error);
|
|
216
|
+
for (uint32_t i = 0; i < count; i++) {
|
|
217
|
+
xt_value listener = xt_array_get(list, xt_number((double)i));
|
|
218
|
+
if (XT_IS_FUNCTION(listener)) xt_call_with_this(listener, device.js, 1, &event);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
void throw_type_error(const std::string &message) {
|
|
223
|
+
xt_value text = xt_string_from_cstr(message.c_str());
|
|
224
|
+
xt_throw(xt_error_ctor_by_name(xt_string_from_cstr("TypeError"), 1, &text));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
xt_value validation(Device &device, const std::string &message, xt_value fallback) {
|
|
228
|
+
record_error(device, ErrorKind::Validation, message);
|
|
229
|
+
return fallback;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
std::shared_ptr<Device> device_for(const Object &object, std::string *error) {
|
|
233
|
+
std::shared_ptr<Object> found = get_object(object.device_id, Kind::Device);
|
|
234
|
+
if (found == nullptr) {
|
|
235
|
+
if (error != nullptr) *error = "the WebGPU device is no longer available";
|
|
236
|
+
return nullptr;
|
|
237
|
+
}
|
|
238
|
+
return std::static_pointer_cast<Device>(found);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
xt_value handle_of(uint32_t id) {
|
|
242
|
+
std::shared_ptr<Object> object = get_object(id);
|
|
243
|
+
if (object == nullptr) return XT_UNDEFINED;
|
|
244
|
+
return wrap_object(object, proto_for(object->kind));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
xt_value resolved(xt_value value) { return xt_promise_resolve(value); }
|
|
248
|
+
|
|
249
|
+
/* -- host types ----------------------------------------------------------- */
|
|
250
|
+
|
|
251
|
+
xt_value host_type(const std::string &name) {
|
|
252
|
+
ensure_registered();
|
|
253
|
+
auto cached = g_types.find(name);
|
|
254
|
+
if (cached != g_types.end()) return cached->second;
|
|
255
|
+
|
|
256
|
+
if (name == "GPUValidationError") {
|
|
257
|
+
g_types[name] = constructor_for(name, error_proto(0));
|
|
258
|
+
xt_gc_add_root(&g_types[name]);
|
|
259
|
+
return g_types[name];
|
|
260
|
+
}
|
|
261
|
+
if (name == "GPUOutOfMemoryError") {
|
|
262
|
+
g_types[name] = constructor_for(name, error_proto(1));
|
|
263
|
+
xt_gc_add_root(&g_types[name]);
|
|
264
|
+
return g_types[name];
|
|
265
|
+
}
|
|
266
|
+
if (name == "GPUInternalError") {
|
|
267
|
+
g_types[name] = constructor_for(name, error_proto(2));
|
|
268
|
+
xt_gc_add_root(&g_types[name]);
|
|
269
|
+
return g_types[name];
|
|
270
|
+
}
|
|
271
|
+
if (name == "GPUDeviceLostInfo") {
|
|
272
|
+
g_types[name] = constructor_for(name, device_lost_proto());
|
|
273
|
+
xt_gc_add_root(&g_types[name]);
|
|
274
|
+
return g_types[name];
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (name == "GPUBufferUsage") {
|
|
278
|
+
xt_value ns = namespace_object("GPUBufferUsage");
|
|
279
|
+
const struct { const char *key; double value; } kValues[] = {
|
|
280
|
+
{"MAP_READ", 1}, {"MAP_WRITE", 2}, {"COPY_SRC", 4}, {"COPY_DST", 8},
|
|
281
|
+
{"INDEX", 16}, {"VERTEX", 32}, {"UNIFORM", 64}, {"STORAGE", 128},
|
|
282
|
+
{"INDIRECT", 256}, {"QUERY_RESOLVE", 512}};
|
|
283
|
+
for (const auto &value : kValues) set_number_prop(ns, value.key, value.value);
|
|
284
|
+
g_types[name] = ns;
|
|
285
|
+
return ns;
|
|
286
|
+
}
|
|
287
|
+
if (name == "GPUTextureUsage") {
|
|
288
|
+
xt_value ns = namespace_object("GPUTextureUsage");
|
|
289
|
+
const struct { const char *key; double value; } kValues[] = {
|
|
290
|
+
{"COPY_SRC", 1}, {"COPY_DST", 2}, {"TEXTURE_BINDING", 4},
|
|
291
|
+
{"STORAGE_BINDING", 8}, {"RENDER_ATTACHMENT", 16}};
|
|
292
|
+
for (const auto &value : kValues) set_number_prop(ns, value.key, value.value);
|
|
293
|
+
g_types[name] = ns;
|
|
294
|
+
return ns;
|
|
295
|
+
}
|
|
296
|
+
if (name == "GPUShaderStage") {
|
|
297
|
+
xt_value ns = namespace_object("GPUShaderStage");
|
|
298
|
+
set_number_prop(ns, "VERTEX", 1);
|
|
299
|
+
set_number_prop(ns, "FRAGMENT", 2);
|
|
300
|
+
set_number_prop(ns, "COMPUTE", 4);
|
|
301
|
+
g_types[name] = ns;
|
|
302
|
+
return ns;
|
|
303
|
+
}
|
|
304
|
+
if (name == "GPUMapMode") {
|
|
305
|
+
xt_value ns = namespace_object("GPUMapMode");
|
|
306
|
+
set_number_prop(ns, "READ", 1);
|
|
307
|
+
set_number_prop(ns, "WRITE", 2);
|
|
308
|
+
g_types[name] = ns;
|
|
309
|
+
return ns;
|
|
310
|
+
}
|
|
311
|
+
if (name == "GPUColorWrite") {
|
|
312
|
+
xt_value ns = namespace_object("GPUColorWrite");
|
|
313
|
+
set_number_prop(ns, "RED", 1);
|
|
314
|
+
set_number_prop(ns, "GREEN", 2);
|
|
315
|
+
set_number_prop(ns, "BLUE", 4);
|
|
316
|
+
set_number_prop(ns, "ALPHA", 8);
|
|
317
|
+
set_number_prop(ns, "ALL", 15);
|
|
318
|
+
g_types[name] = ns;
|
|
319
|
+
return ns;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
bool found = false;
|
|
323
|
+
const Kind kind = kind_by_name(name, &found);
|
|
324
|
+
if (!found) {
|
|
325
|
+
/* A class without native state (GPUAdapterInfo, GPUCompilationInfo, …):
|
|
326
|
+
* a constructor over a plain prototype keeps `instanceof` meaningful. */
|
|
327
|
+
if (name.compare(0, 3, "GPU") != 0) return XT_UNDEFINED;
|
|
328
|
+
if (!XT_IS_OBJECT(g_plain_proto)) {
|
|
329
|
+
g_plain_proto = xt_object_new();
|
|
330
|
+
xt_gc_add_root(&g_plain_proto);
|
|
331
|
+
g_plain_ctor = constructor_for("GPUObject", g_plain_proto);
|
|
332
|
+
xt_gc_add_root(&g_plain_ctor);
|
|
333
|
+
}
|
|
334
|
+
xt_value ctor = xt_closure_new((void *)host_ctor, 0, nullptr);
|
|
335
|
+
xt_function_set_prototype(ctor, g_plain_proto);
|
|
336
|
+
g_types[name] = ctor;
|
|
337
|
+
xt_gc_add_root(&g_types[name]);
|
|
338
|
+
return ctor;
|
|
339
|
+
}
|
|
340
|
+
xt_value proto = proto_for(kind);
|
|
341
|
+
g_types[name] = constructor_for(name, proto);
|
|
342
|
+
xt_gc_add_root(&g_types[name]);
|
|
343
|
+
return g_types[name];
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
xt_value navigator_gpu() {
|
|
347
|
+
ensure_registered();
|
|
348
|
+
if (XT_IS_OBJECT(g_gpu)) return g_gpu;
|
|
349
|
+
auto gpu = std::make_shared<Object>();
|
|
350
|
+
gpu->kind = Kind::Gpu;
|
|
351
|
+
const uint32_t id = add_object(gpu);
|
|
352
|
+
g_gpu = wrap_object(gpu, proto_for(Kind::Gpu));
|
|
353
|
+
xt_gc_add_root(&g_gpu);
|
|
354
|
+
xt_value features = xt_object_new();
|
|
355
|
+
xt_value has = xt_closure_new((void *)gpu_feature_has, 0, nullptr);
|
|
356
|
+
xt_object_set(features, xt_string_from_cstr("has"), has);
|
|
357
|
+
xt_object_set(features, xt_string_from_cstr("size"), xt_number(0));
|
|
358
|
+
xt_object_set(g_gpu, xt_string_from_cstr("wgslLanguageFeatures"), features);
|
|
359
|
+
(void)id;
|
|
360
|
+
return g_gpu;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
} // namespace webgpu
|
|
364
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — interface between the WebGPU runtime and its JS bindings.
|
|
3
|
+
*/
|
|
4
|
+
#ifndef XT_GUI_WEBGPU_BINDINGS_H
|
|
5
|
+
#define XT_GUI_WEBGPU_BINDINGS_H
|
|
6
|
+
|
|
7
|
+
#include "webgpu.h"
|
|
8
|
+
|
|
9
|
+
namespace xtgui {
|
|
10
|
+
namespace webgpu {
|
|
11
|
+
|
|
12
|
+
using Handler = xt_value (*)(Object &, int argc, xt_value *argv);
|
|
13
|
+
using Getter = xt_value (*)(Object &);
|
|
14
|
+
|
|
15
|
+
/** Register a prototype member (called once from the unit that owns it). */
|
|
16
|
+
void add_method(Kind kind, const char *name, Handler handler);
|
|
17
|
+
void add_getter(Kind kind, const char *name, Getter getter);
|
|
18
|
+
|
|
19
|
+
void register_gpu_methods();
|
|
20
|
+
void register_resource_methods();
|
|
21
|
+
void register_buffer_methods();
|
|
22
|
+
void register_pipeline_methods();
|
|
23
|
+
void register_queue_methods();
|
|
24
|
+
void register_command_methods();
|
|
25
|
+
void register_canvas_methods();
|
|
26
|
+
void register_render_pass_methods();
|
|
27
|
+
void register_compute_pass_methods();
|
|
28
|
+
|
|
29
|
+
/** The prototype of one WebGPU interface. */
|
|
30
|
+
xt_value proto_for(Kind kind);
|
|
31
|
+
/** `GPUValidationError` / `GPUOutOfMemoryError` / `GPUInternalError`. */
|
|
32
|
+
xt_value error_proto(int kind);
|
|
33
|
+
xt_value device_lost_proto();
|
|
34
|
+
/** Fire `uncapturederror` on the device's listeners. */
|
|
35
|
+
void dispatch_uncaptured_error(Device &device, xt_value error);
|
|
36
|
+
|
|
37
|
+
/** Throw a JS TypeError from a binding. */
|
|
38
|
+
void throw_type_error(const std::string &message);
|
|
39
|
+
/** Record a validation error and return `fallback`. */
|
|
40
|
+
xt_value validation(Device &device, const std::string &message,
|
|
41
|
+
xt_value fallback = XT_UNDEFINED);
|
|
42
|
+
|
|
43
|
+
/** Resolve the device that owns `object`. */
|
|
44
|
+
std::shared_ptr<Device> device_for(const Object &object, std::string *error);
|
|
45
|
+
/** The JS handle for a registry id, or undefined. */
|
|
46
|
+
xt_value handle_of(uint32_t id);
|
|
47
|
+
/** A resolved promise, or a rejected one carrying a TypeError. */
|
|
48
|
+
xt_value resolved(xt_value value);
|
|
49
|
+
|
|
50
|
+
/** The JS device object's rooted listener list. */
|
|
51
|
+
xt_value listener_list(Device &device);
|
|
52
|
+
|
|
53
|
+
/** Parse the `entries` of a GPUBindGroup descriptor into a native bind group. */
|
|
54
|
+
bool parse_bind_group(Device &device, xt_value descriptor, BindGroup *out, std::string *error);
|
|
55
|
+
|
|
56
|
+
} // namespace webgpu
|
|
57
|
+
} // namespace xtgui
|
|
58
|
+
|
|
59
|
+
#endif /* XT_GUI_WEBGPU_BINDINGS_H */
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — `webgpu` link feature registration bridge.
|
|
3
|
+
*
|
|
4
|
+
* Compiled and linked only when the compiler detects that the program may use
|
|
5
|
+
* WebGPU. Its constructor calls `xt_webgpu_feature_register`, which installs
|
|
6
|
+
* `XtWebgpuApi` and references the WebGPU runtime, pulling it out of `gui.a`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
extern void xt_webgpu_feature_register(void);
|
|
10
|
+
|
|
11
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
12
|
+
__attribute__((constructor)) static void xt_webgpu_feature_boot(void) {
|
|
13
|
+
xt_webgpu_feature_register();
|
|
14
|
+
}
|
|
15
|
+
#elif defined(_MSC_VER)
|
|
16
|
+
void xt_webgpu_feature_boot(void);
|
|
17
|
+
#pragma section(".CRT$XCU", read)
|
|
18
|
+
__declspec(allocate(".CRT$XCU")) static void (*const xt_webgpu_feature_ctor)(void) =
|
|
19
|
+
xt_webgpu_feature_boot;
|
|
20
|
+
void xt_webgpu_feature_boot(void) { xt_webgpu_feature_register(); }
|
|
21
|
+
#endif
|