xbintsc 0.6.13 → 0.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/driver/version.d.ts +1 -1
  3. package/dist/src/driver/version.js +1 -1
  4. package/dist/src/extensions/gui/features.d.ts +2 -0
  5. package/dist/src/extensions/gui/features.js +24 -0
  6. package/dist/src/extensions/gui/features.js.map +1 -1
  7. package/dist/src/extensions/gui/index.js +39 -0
  8. package/dist/src/extensions/gui/index.js.map +1 -1
  9. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  10. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  11. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  14. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  16. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  17. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  18. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  19. package/dist/tests/extensions/gui-features.test.js +18 -1
  20. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  21. package/doc/ai/extensions.md +7 -1
  22. package/doc/ai/zh-CN/extensions.md +6 -1
  23. package/doc/gui/webgpu.md +121 -0
  24. package/doc/gui.md +12 -1
  25. package/doc/zh-CN/gui/webgpu.md +111 -0
  26. package/doc/zh-CN/gui.md +10 -1
  27. package/llms.txt +1 -0
  28. package/package.json +7 -7
  29. package/runtime/ext_gui/browser.cpp +10 -0
  30. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  31. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  32. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  33. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  34. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  35. package/runtime/ext_gui/renderer_image.cpp +6 -0
  36. package/runtime/ext_gui/webgl_glsl.h +8 -0
  37. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  38. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  39. package/runtime/ext_gui/webgl_msl.h +4 -0
  40. package/runtime/ext_gui/webgpu.h +371 -0
  41. package/runtime/ext_gui/webgpu_api.h +42 -0
  42. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  43. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  44. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  45. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  46. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  47. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  48. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  49. package/runtime/ext_gui/webgpu_convert.cpp +313 -0
  50. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  51. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  52. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  53. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  54. package/runtime/ext_gui/webgpu_pass.h +33 -0
  55. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  56. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  57. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  58. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  59. package/runtime/ext_gui/webgpu_shader.cpp +246 -0
  60. package/runtime/ext_gui/wgsl.h +99 -0
  61. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  62. package/runtime/ext_gui/wgsl_ast.h +233 -0
  63. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  64. package/runtime/ext_gui/wgsl_emit.h +128 -0
  65. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  66. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  67. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  68. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  69. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  70. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  71. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  72. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  73. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  74. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  75. package/runtime/ext_gui/wgsl_parser.h +77 -0
  76. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  77. package/src/driver/version.ts +1 -1
  78. package/src/extensions/gui/features.ts +24 -0
  79. package/src/extensions/gui/index.ts +39 -0
@@ -0,0 +1,293 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGPU buffer, texture, texture-view and sampler
3
+ * instance methods (creation lives in `webgpu_device.cpp`).
4
+ */
5
+
6
+ #include <cstring>
7
+
8
+ #include "webgpu_bindings.h"
9
+
10
+ namespace xtgui {
11
+ namespace webgpu {
12
+ namespace {
13
+
14
+ xt_value label_of(Object &object) { return xt_string_from_cstr(object.label.c_str()); }
15
+
16
+ /* -- buffer --------------------------------------------------------------- */
17
+
18
+ xt_value buffer_size(Object &object) { return xt_number((double)static_cast<Buffer &>(object).size); }
19
+
20
+ xt_value buffer_usage(Object &object) { return xt_number((double)static_cast<Buffer &>(object).usage); }
21
+
22
+ xt_value buffer_map_state(Object &object) {
23
+ static const char *const kNames[] = {"unmapped", "pending", "mapped"};
24
+ const int state = static_cast<Buffer &>(object).map_state;
25
+ return xt_string_from_cstr(kNames[state >= 0 && state <= 2 ? state : 0]);
26
+ }
27
+
28
+ bool upload_buffer(Buffer &buffer, uint64_t offset, const uint8_t *data, uint64_t size) {
29
+ if (!buffer.has_gpu || size == 0) return true;
30
+ SDL_GPUTransferBufferCreateInfo info = {};
31
+ info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
32
+ info.size = (Uint32)size;
33
+ SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(gpu_device(), &info);
34
+ if (transfer == nullptr) return false;
35
+ void *mapped = SDL_MapGPUTransferBuffer(gpu_device(), transfer, false);
36
+ if (mapped != nullptr) {
37
+ memcpy(mapped, data, (size_t)size);
38
+ SDL_UnmapGPUTransferBuffer(gpu_device(), transfer);
39
+ }
40
+ SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(gpu_device());
41
+ SDL_GPUCopyPass *pass = SDL_BeginGPUCopyPass(cmd);
42
+ SDL_GPUTransferBufferLocation source = {};
43
+ source.transfer_buffer = transfer;
44
+ source.offset = 0;
45
+ SDL_GPUBufferRegion destination = {};
46
+ destination.buffer = buffer.gpu;
47
+ destination.offset = (Uint32)offset;
48
+ destination.size = (Uint32)size;
49
+ SDL_UploadToGPUBuffer(pass, &source, &destination, true);
50
+ SDL_EndGPUCopyPass(pass);
51
+ SDL_SubmitGPUCommandBuffer(cmd);
52
+ SDL_ReleaseGPUTransferBuffer(gpu_device(), transfer);
53
+ return true;
54
+ }
55
+
56
+ xt_value buffer_map_async(Object &object, int argc, xt_value *argv) {
57
+ Buffer &buffer = static_cast<Buffer &>(object);
58
+ std::string error;
59
+ std::shared_ptr<Device> device = device_for(object, &error);
60
+ if (device == nullptr) return xt_promise_reject(make_error(ErrorKind::Validation, error));
61
+ const std::string mode = arg_string(argc, argv, 0);
62
+ const uint64_t offset = arg_u64(argc, argv, 1);
63
+ uint64_t size = arg_u64(argc, argv, 2);
64
+ if (size == 0) size = buffer.size > offset ? buffer.size - offset : 0;
65
+ if (offset + size > buffer.size) {
66
+ return xt_promise_reject(make_error(ErrorKind::Validation, "mapAsync range is out of bounds"));
67
+ }
68
+ if (buffer.map_state != 0) {
69
+ return xt_promise_reject(make_error(ErrorKind::Validation, "buffer is already mapped"));
70
+ }
71
+ if ((mode == "READ" && (buffer.usage & 1) == 0) ||
72
+ (mode == "WRITE" && (buffer.usage & 2) == 0)) {
73
+ return xt_promise_reject(
74
+ make_error(ErrorKind::Validation, "mapAsync mode does not match the buffer usage"));
75
+ }
76
+ buffer.map_mode = mode == "WRITE" ? 2 : 1;
77
+ buffer.map_state = 1;
78
+ if (!buffer.has_shadow) buffer.shadow.assign((size_t)buffer.size, 0);
79
+ if (!buffer.has_gpu) {
80
+ buffer.map_state = 2;
81
+ return resolved(XT_UNDEFINED);
82
+ }
83
+ if (buffer.map_mode == 2) {
84
+ /* Nothing to read: the upload happens on unmap. */
85
+ buffer.map_state = 2;
86
+ return resolved(XT_UNDEFINED);
87
+ }
88
+ SDL_GPUTransferBufferCreateInfo info = {};
89
+ info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD;
90
+ info.size = (Uint32)size;
91
+ SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(gpu_device(), &info);
92
+ if (transfer == nullptr) {
93
+ buffer.map_state = 0;
94
+ return xt_promise_reject(make_error(ErrorKind::Validation, "mapAsync readback failed"));
95
+ }
96
+ SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(gpu_device());
97
+ SDL_GPUCopyPass *pass = SDL_BeginGPUCopyPass(cmd);
98
+ SDL_GPUBufferRegion region = {};
99
+ region.buffer = buffer.gpu;
100
+ region.offset = (Uint32)offset;
101
+ region.size = (Uint32)size;
102
+ SDL_GPUTransferBufferLocation location = {};
103
+ location.transfer_buffer = transfer;
104
+ location.offset = 0;
105
+ SDL_DownloadFromGPUBuffer(pass, &region, &location);
106
+ SDL_EndGPUCopyPass(pass);
107
+ SDL_GPUFence *fence = SDL_SubmitGPUCommandBufferAndAcquireFence(cmd);
108
+ if (fence != nullptr) {
109
+ SDL_GPUFence *fences[1] = {fence};
110
+ SDL_WaitForGPUFences(gpu_device(), true, fences, 1);
111
+ SDL_ReleaseGPUFence(gpu_device(), fence);
112
+ }
113
+ void *mapped = SDL_MapGPUTransferBuffer(gpu_device(), transfer, false);
114
+ if (mapped != nullptr) {
115
+ memcpy(buffer.shadow.data() + offset, mapped, (size_t)size);
116
+ SDL_UnmapGPUTransferBuffer(gpu_device(), transfer);
117
+ }
118
+ SDL_ReleaseGPUTransferBuffer(gpu_device(), transfer);
119
+ buffer.map_state = 2;
120
+ return resolved(XT_UNDEFINED);
121
+ }
122
+
123
+ xt_value buffer_get_mapped_range(Object &object, int argc, xt_value *argv) {
124
+ Buffer &buffer = static_cast<Buffer &>(object);
125
+ if (buffer.map_state != 2) {
126
+ std::string error;
127
+ std::shared_ptr<Device> device = device_for(object, &error);
128
+ if (device != nullptr) {
129
+ validation(*device, "getMappedRange: the buffer is not mapped");
130
+ }
131
+ return XT_UNDEFINED;
132
+ }
133
+ const uint64_t offset = arg_u64(argc, argv, 0);
134
+ uint64_t size = arg_u64(argc, argv, 1);
135
+ if (size == 0) size = buffer.shadow.size() > offset ? buffer.shadow.size() - offset : 0;
136
+ if (offset + size > buffer.shadow.size()) return XT_UNDEFINED;
137
+ xt_value array = xt_array_buffer_alloc((uint32_t)size);
138
+ xt_array_buffer_copy_bytes_in(array, 0, buffer.shadow.data() + offset, (uint32_t)size);
139
+ buffer.mapped_array = array;
140
+ buffer.mapped_offset = offset;
141
+ buffer.mapped_size = size;
142
+ xt_gc_add_root(&buffer.mapped_array);
143
+ return array;
144
+ }
145
+
146
+ xt_value buffer_unmap(Object &object, int argc, xt_value *argv) {
147
+ (void)argc;
148
+ (void)argv;
149
+ Buffer &buffer = static_cast<Buffer &>(object);
150
+ if (buffer.map_state != 2) return XT_UNDEFINED;
151
+ if (buffer.map_mode == 2 && buffer.map_state == 2) {
152
+ /* A mapped WRITE range is a copy of the shadow; take the edits back. */
153
+ if (XT_IS_OBJECT(buffer.mapped_array) && buffer.mapped_size > 0 &&
154
+ buffer.mapped_offset + buffer.mapped_size <= buffer.shadow.size()) {
155
+ xt_array_buffer_copy_bytes_out(buffer.mapped_array, 0,
156
+ buffer.shadow.data() + buffer.mapped_offset,
157
+ (uint32_t)buffer.mapped_size);
158
+ upload_buffer(buffer, buffer.mapped_offset, buffer.shadow.data() + buffer.mapped_offset,
159
+ buffer.mapped_size);
160
+ } else if (!buffer.shadow.empty()) {
161
+ upload_buffer(buffer, 0, buffer.shadow.data(), buffer.shadow.size());
162
+ }
163
+ }
164
+ buffer.map_state = 0;
165
+ buffer.map_mode = 0;
166
+ buffer.mapped_array = XT_UNDEFINED;
167
+ return XT_UNDEFINED;
168
+ }
169
+
170
+ xt_value buffer_destroy(Object &object, int argc, xt_value *argv) {
171
+ (void)argc;
172
+ (void)argv;
173
+ Buffer &buffer = static_cast<Buffer &>(object);
174
+ if (buffer.destroyed) return XT_UNDEFINED;
175
+ if (buffer.gpu != nullptr) SDL_ReleaseGPUBuffer(gpu_device(), buffer.gpu);
176
+ buffer.gpu = nullptr;
177
+ buffer.has_gpu = false;
178
+ buffer.destroyed = true;
179
+ buffer.map_state = 0;
180
+ return XT_UNDEFINED;
181
+ }
182
+
183
+ /* -- texture -------------------------------------------------------------- */
184
+
185
+ xt_value texture_width(Object &object) {
186
+ return xt_number((double)static_cast<Texture &>(object).width);
187
+ }
188
+
189
+ xt_value texture_height(Object &object) {
190
+ return xt_number((double)static_cast<Texture &>(object).height);
191
+ }
192
+
193
+ xt_value texture_depth(Object &object) {
194
+ return xt_number((double)static_cast<Texture &>(object).depth);
195
+ }
196
+
197
+ xt_value texture_mips(Object &object) {
198
+ return xt_number((double)static_cast<Texture &>(object).mips);
199
+ }
200
+
201
+ xt_value texture_samples(Object &object) {
202
+ return xt_number((double)static_cast<Texture &>(object).samples);
203
+ }
204
+
205
+ xt_value texture_dimension(Object &object) {
206
+ return xt_string_from_cstr(static_cast<Texture &>(object).dimension.c_str());
207
+ }
208
+
209
+ xt_value texture_format(Object &object) {
210
+ return xt_string_from_cstr(Convert::texture_format_name(static_cast<Texture &>(object).format));
211
+ }
212
+
213
+ xt_value texture_usage(Object &object) {
214
+ return xt_number((double)static_cast<Texture &>(object).usage);
215
+ }
216
+
217
+ xt_value texture_create_view(Object &object, int argc, xt_value *argv) {
218
+ (void)argc;
219
+ (void)argv;
220
+ Texture &texture = static_cast<Texture &>(object);
221
+ if (texture.destroyed) {
222
+ std::string error;
223
+ std::shared_ptr<Device> device = device_for(object, &error);
224
+ if (device != nullptr) validation(*device, "createView: the texture was destroyed");
225
+ return XT_UNDEFINED;
226
+ }
227
+ auto view = std::make_shared<TextureView>();
228
+ view->kind = Kind::TextureView;
229
+ view->device_id = texture.device_id;
230
+ view->texture = texture.id;
231
+ return handle_of(add_object(view));
232
+ }
233
+
234
+ xt_value texture_destroy(Object &object, int argc, xt_value *argv) {
235
+ (void)argc;
236
+ (void)argv;
237
+ Texture &texture = static_cast<Texture &>(object);
238
+ if (texture.destroyed) return XT_UNDEFINED;
239
+ if (texture.gpu != nullptr && !texture.external) {
240
+ SDL_ReleaseGPUTexture(gpu_device(), texture.gpu);
241
+ }
242
+ texture.gpu = nullptr;
243
+ texture.destroyed = true;
244
+ return XT_UNDEFINED;
245
+ }
246
+
247
+ /* -- registration --------------------------------------------------------- */
248
+
249
+ } // namespace
250
+
251
+ void register_buffer_methods() {
252
+ add_getter(Kind::Buffer, "size", buffer_size);
253
+ add_getter(Kind::Buffer, "usage", buffer_usage);
254
+ add_getter(Kind::Buffer, "mapState", buffer_map_state);
255
+ add_getter(Kind::Buffer, "label", label_of);
256
+ add_method(Kind::Buffer, "mapAsync", buffer_map_async);
257
+ add_method(Kind::Buffer, "getMappedRange", buffer_get_mapped_range);
258
+ add_method(Kind::Buffer, "unmap", buffer_unmap);
259
+ add_method(Kind::Buffer, "destroy", buffer_destroy);
260
+
261
+ add_getter(Kind::Texture, "width", texture_width);
262
+ add_getter(Kind::Texture, "height", texture_height);
263
+ add_getter(Kind::Texture, "depthOrArrayLayers", texture_depth);
264
+ add_getter(Kind::Texture, "mipLevelCount", texture_mips);
265
+ add_getter(Kind::Texture, "sampleCount", texture_samples);
266
+ add_getter(Kind::Texture, "dimension", texture_dimension);
267
+ add_getter(Kind::Texture, "format", texture_format);
268
+ add_getter(Kind::Texture, "usage", texture_usage);
269
+ add_getter(Kind::Texture, "label", label_of);
270
+ add_method(Kind::Texture, "createView", texture_create_view);
271
+ add_method(Kind::Texture, "destroy", texture_destroy);
272
+
273
+ add_getter(Kind::TextureView, "label", label_of);
274
+ add_getter(Kind::Sampler, "label", label_of);
275
+ add_getter(Kind::QuerySet, "label", label_of);
276
+ add_getter(Kind::QuerySet, "type", [](Object &object) -> xt_value {
277
+ return xt_string_from_cstr(static_cast<QuerySet &>(object).type == 1 ? "timestamp"
278
+ : "occlusion");
279
+ });
280
+ add_getter(Kind::QuerySet, "count", [](Object &object) -> xt_value {
281
+ return xt_number((double)static_cast<QuerySet &>(object).count);
282
+ });
283
+ add_method(Kind::QuerySet, "destroy", [](Object &object, int argc, xt_value *argv) -> xt_value {
284
+ (void)argc;
285
+ (void)argv;
286
+ (void)object;
287
+ return XT_UNDEFINED;
288
+ });
289
+ add_getter(Kind::RenderBundle, "label", label_of);
290
+ }
291
+
292
+ } // namespace webgpu
293
+ } // namespace xtgui
@@ -0,0 +1,305 @@
1
+ /*
2
+ * xbintsc GUI engine — `<canvas>` WebGPU contexts and their backbuffer.
3
+ *
4
+ * A context owns one SDL_GPU texture that both the WebGPU render pass and the
5
+ * page compositor use: the paint pipeline samples the same texture through the
6
+ * `XtWebgpuApi::color_texture` bridge, so no pixels pass through the CPU.
7
+ */
8
+
9
+ #include <unordered_map>
10
+
11
+ #include "gui_internal.h"
12
+ #include "webgpu_bindings.h"
13
+ #include "webgpu.h"
14
+
15
+ namespace xtgui {
16
+ namespace webgpu {
17
+ namespace {
18
+
19
+ std::unordered_map<uint64_t, uint32_t> g_by_canvas; // canvas id -> context id
20
+ std::unordered_map<XtGuiWindow *,
21
+ std::unordered_map<const xtgui::Node *, uint32_t>>
22
+ g_contexts;
23
+ uint64_t g_next_canvas = 1;
24
+
25
+ uint64_t next_canvas_id() { return (2ULL << 32) | g_next_canvas++; }
26
+
27
+ bool canvas_node(const Node *node) {
28
+ return node != nullptr && node->isElement() && node->isTag("canvas");
29
+ }
30
+
31
+ uint32_t context_id_of(xt_value self) {
32
+ xt_value value = xt_object_get_cstr(self, "__xt_webgpu_id");
33
+ return XT_IS_NUMBER(value) ? (uint32_t)xt_to_number(value) : 0;
34
+ }
35
+
36
+ SDL_GPUTexture *create_backbuffer(CanvasContext &context) {
37
+ SDL_GPUTextureCreateInfo info = {};
38
+ info.type = SDL_GPU_TEXTURETYPE_2D;
39
+ info.format = context.format;
40
+ info.usage = Convert::texture_usage(context.usage, context.format) |
41
+ SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
42
+ info.width = context.width > 0 ? context.width : 1;
43
+ info.height = context.height > 0 ? context.height : 1;
44
+ info.layer_count_or_depth = 1;
45
+ info.num_levels = 1;
46
+ info.sample_count = SDL_GPU_SAMPLECOUNT_1;
47
+ return SDL_CreateGPUTexture(gpu_device(), &info);
48
+ }
49
+
50
+ /* Drop the old backbuffer texture object before creating a new one. */
51
+ void release_backbuffer(CanvasContext &context) {
52
+ if (context.texture == 0) return;
53
+ std::shared_ptr<Object> object = get_object(context.texture, Kind::Texture);
54
+ if (object != nullptr) {
55
+ Texture *texture = static_cast<Texture *>(object.get());
56
+ if (texture->gpu != nullptr) SDL_ReleaseGPUTexture(gpu_device(), texture->gpu);
57
+ texture->gpu = nullptr;
58
+ texture->destroyed = true;
59
+ }
60
+ remove_object(context.texture);
61
+ context.texture = 0;
62
+ }
63
+
64
+ uint32_t ensure_backbuffer(CanvasContext &context) {
65
+ if (context.texture != 0) {
66
+ std::shared_ptr<Object> existing = get_object(context.texture, Kind::Texture);
67
+ if (existing != nullptr) return context.texture;
68
+ }
69
+ SDL_GPUTexture *gpu = create_backbuffer(context);
70
+ if (gpu == nullptr) return 0;
71
+ auto texture = std::make_shared<Texture>();
72
+ texture->kind = Kind::Texture;
73
+ texture->device_id = context.device;
74
+ texture->gpu = gpu;
75
+ texture->format = context.format;
76
+ texture->width = context.width;
77
+ texture->height = context.height;
78
+ texture->depth = 1;
79
+ texture->mips = 1;
80
+ texture->samples = 1;
81
+ texture->usage = context.usage;
82
+ texture->dimension = "2d";
83
+ texture->canvas_id = context.canvas_id;
84
+ texture->external = true;
85
+ context.texture = add_object(texture);
86
+ return context.texture;
87
+ }
88
+
89
+ /* -- context methods ------------------------------------------------------ */
90
+
91
+ xt_value context_configure(Object &object, int argc, xt_value *argv) {
92
+ CanvasContext &context = static_cast<CanvasContext &>(object);
93
+ xt_value descriptor = arg_object(argc, argv, 0);
94
+ if (!XT_IS_OBJECT(descriptor)) {
95
+ throw_type_error("configure needs a descriptor");
96
+ return XT_UNDEFINED;
97
+ }
98
+ const uint32_t device_id = (uint32_t)xt_to_number(
99
+ xt_object_get_cstr(prop_object(descriptor, "device"), "__xt_webgpu_id"));
100
+ std::shared_ptr<Object> device = get_object(device_id, Kind::Device);
101
+ if (device == nullptr) {
102
+ throw_type_error("configure: device is not a GPUDevice");
103
+ return XT_UNDEFINED;
104
+ }
105
+ const std::string format = prop_string(descriptor, "format");
106
+ SDL_GPUTextureFormat sdl_format = SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM;
107
+ if (!format.empty() && !Convert::texture_format(format, &sdl_format)) {
108
+ return validation(static_cast<Device &>(*device), "configure: unsupported format '" + format + "'");
109
+ }
110
+ if (context.texture != 0 &&
111
+ (context.format != sdl_format || context.width == 0 || context.height == 0)) {
112
+ release_backbuffer(context);
113
+ }
114
+ context.device = device_id;
115
+ context.format = sdl_format;
116
+ context.format_name = format.empty() ? "bgra8unorm" : format;
117
+ context.usage = (uint32_t)prop_u64(descriptor, "usage", 16);
118
+ context.alpha_mode = prop_string(descriptor, "alphaMode");
119
+ if (context.alpha_mode.empty()) context.alpha_mode = "opaque";
120
+ context.configured = true;
121
+ release_backbuffer(context);
122
+ if (ensure_backbuffer(context) == 0) {
123
+ return validation(static_cast<Device &>(*device), "configure: the backbuffer could not be created");
124
+ }
125
+ return XT_UNDEFINED;
126
+ }
127
+
128
+ xt_value context_unconfigure(Object &object, int argc, xt_value *argv) {
129
+ (void)argc;
130
+ (void)argv;
131
+ CanvasContext &context = static_cast<CanvasContext &>(object);
132
+ release_backbuffer(context);
133
+ context.configured = false;
134
+ return XT_UNDEFINED;
135
+ }
136
+
137
+ xt_value context_get_current_texture(Object &object, int argc, xt_value *argv) {
138
+ (void)argc;
139
+ (void)argv;
140
+ CanvasContext &context = static_cast<CanvasContext &>(object);
141
+ if (!context.configured) {
142
+ std::shared_ptr<Device> device = device_for(object, nullptr);
143
+ if (device != nullptr) {
144
+ return validation(*device, "getCurrentTexture: the canvas context is not configured");
145
+ }
146
+ return XT_UNDEFINED;
147
+ }
148
+ const uint32_t texture = ensure_backbuffer(context);
149
+ if (texture == 0) return XT_UNDEFINED;
150
+ return handle_of(texture);
151
+ }
152
+
153
+ xt_value context_get_configuration(Object &object, int argc, xt_value *argv) {
154
+ (void)argc;
155
+ (void)argv;
156
+ CanvasContext &context = static_cast<CanvasContext &>(object);
157
+ if (!context.configured) return XT_UNDEFINED;
158
+ xt_value result = xt_object_new();
159
+ xt_object_set(result, xt_string_from_cstr("device"), device_object(context.device));
160
+ xt_object_set(result, xt_string_from_cstr("format"),
161
+ xt_string_from_cstr(context.format_name.c_str()));
162
+ xt_object_set(result, xt_string_from_cstr("usage"), xt_number((double)context.usage));
163
+ xt_object_set(result, xt_string_from_cstr("alphaMode"),
164
+ xt_string_from_cstr(context.alpha_mode.c_str()));
165
+ return result;
166
+ }
167
+
168
+ xt_value context_canvas(Object &object) {
169
+ CanvasContext &context = static_cast<CanvasContext &>(object);
170
+ return context.canvas_js == XT_UNDEFINED ? XT_NULL : context.canvas_js;
171
+ }
172
+
173
+ xt_value context_label(Object &object) { return xt_string_from_cstr(object.label.c_str()); }
174
+
175
+ } // namespace
176
+
177
+ /* -- feature entry points ------------------------------------------------- */
178
+
179
+ bool canvas_context(XtGuiWindow *win, const void *node_ptr, uint32_t *out_id) {
180
+ const Node *node = static_cast<const Node *>(node_ptr);
181
+ if (win == nullptr || !canvas_node(node)) return false;
182
+ auto found = g_contexts.find(win);
183
+ if (found != g_contexts.end()) {
184
+ auto entry = found->second.find(node);
185
+ if (entry != found->second.end()) {
186
+ *out_id = entry->second;
187
+ return true;
188
+ }
189
+ }
190
+ float width = 300.0f;
191
+ float height = 150.0f;
192
+ if (const std::string *attribute = node->attr("width")) xtgui::xt_css_parse_number(*attribute, &width);
193
+ if (const std::string *attribute = node->attr("height")) xtgui::xt_css_parse_number(*attribute, &height);
194
+ auto context = std::make_shared<CanvasContext>();
195
+ context->kind = Kind::CanvasContext;
196
+ context->device_id = 0;
197
+ context->canvas_id = next_canvas_id();
198
+ context->width = (uint32_t)(width + 0.5f);
199
+ context->height = (uint32_t)(height + 0.5f);
200
+ const uint32_t id = add_object(context);
201
+ g_contexts[win][node] = id;
202
+ g_by_canvas[context->canvas_id] = id;
203
+ if (win->document != nullptr) {
204
+ win->document->setCanvasInfo(node, width, height, context->canvas_id);
205
+ }
206
+ *out_id = id;
207
+ return true;
208
+ }
209
+
210
+ bool canvas_find(XtGuiWindow *win, const void *node_ptr, uint32_t *out_id) {
211
+ const Node *node = static_cast<const Node *>(node_ptr);
212
+ auto found = g_contexts.find(win);
213
+ if (found == g_contexts.end()) return false;
214
+ auto entry = found->second.find(node);
215
+ if (entry == found->second.end()) return false;
216
+ *out_id = entry->second;
217
+ return true;
218
+ }
219
+
220
+ void canvas_forget(XtGuiWindow *win, const void *node_ptr) {
221
+ const Node *node = static_cast<const Node *>(node_ptr);
222
+ auto found = g_contexts.find(win);
223
+ if (found == g_contexts.end()) return;
224
+ auto entry = found->second.find(node);
225
+ if (entry == found->second.end()) return;
226
+ std::shared_ptr<Object> object = get_object(entry->second, Kind::CanvasContext);
227
+ if (object != nullptr) {
228
+ CanvasContext *context = static_cast<CanvasContext *>(object.get());
229
+ g_by_canvas.erase(context->canvas_id);
230
+ release_backbuffer(*context);
231
+ remove_object(context->id);
232
+ }
233
+ found->second.erase(entry);
234
+ }
235
+
236
+ void canvas_shutdown(XtGuiWindow *win) {
237
+ auto found = g_contexts.find(win);
238
+ if (found == g_contexts.end()) return;
239
+ for (const auto &entry : found->second) {
240
+ std::shared_ptr<Object> object = get_object(entry.second, Kind::CanvasContext);
241
+ if (object == nullptr) continue;
242
+ CanvasContext *context = static_cast<CanvasContext *>(object.get());
243
+ g_by_canvas.erase(context->canvas_id);
244
+ release_backbuffer(*context);
245
+ remove_object(context->id);
246
+ }
247
+ g_contexts.erase(win);
248
+ }
249
+
250
+ void canvas_resize(uint32_t id, int width, int height) {
251
+ std::shared_ptr<Object> object = get_object(id, Kind::CanvasContext);
252
+ if (object == nullptr) return;
253
+ CanvasContext *context = static_cast<CanvasContext *>(object.get());
254
+ if ((uint32_t)width == context->width && (uint32_t)height == context->height) return;
255
+ context->width = (uint32_t)width;
256
+ context->height = (uint32_t)height;
257
+ if (context->configured) {
258
+ release_backbuffer(*context);
259
+ ensure_backbuffer(*context);
260
+ }
261
+ }
262
+
263
+ void *canvas_color_texture(uint64_t canvas_id) {
264
+ auto found = g_by_canvas.find(canvas_id);
265
+ if (found == g_by_canvas.end()) return nullptr;
266
+ std::shared_ptr<Object> object = get_object(found->second, Kind::CanvasContext);
267
+ if (object == nullptr) return nullptr;
268
+ CanvasContext *context = static_cast<CanvasContext *>(object.get());
269
+ if (context->texture == 0) return nullptr;
270
+ std::shared_ptr<Object> texture = get_object(context->texture, Kind::Texture);
271
+ return texture == nullptr ? nullptr : static_cast<Texture *>(texture.get())->gpu;
272
+ }
273
+
274
+ bool canvas_info(uint32_t id, float *width, float *height, uint64_t *canvas_id) {
275
+ std::shared_ptr<Object> object = get_object(id, Kind::CanvasContext);
276
+ if (object == nullptr) return false;
277
+ CanvasContext *context = static_cast<CanvasContext *>(object.get());
278
+ *width = (float)context->width;
279
+ *height = (float)context->height;
280
+ *canvas_id = context->canvas_id;
281
+ return true;
282
+ }
283
+
284
+ xt_value context_proto() { return proto_for(Kind::CanvasContext); }
285
+
286
+ xt_value context_handle(xt_value self, uint32_t id) {
287
+ std::shared_ptr<Object> object = get_object(id, Kind::CanvasContext);
288
+ if (object == nullptr) return XT_NULL;
289
+ CanvasContext *context = static_cast<CanvasContext *>(object.get());
290
+ context->canvas_js = self;
291
+ xt_gc_add_root(&context->canvas_js);
292
+ return wrap_object(object, context_proto());
293
+ }
294
+
295
+ void register_canvas_methods() {
296
+ add_getter(Kind::CanvasContext, "canvas", context_canvas);
297
+ add_getter(Kind::CanvasContext, "label", context_label);
298
+ add_method(Kind::CanvasContext, "configure", context_configure);
299
+ add_method(Kind::CanvasContext, "unconfigure", context_unconfigure);
300
+ add_method(Kind::CanvasContext, "getCurrentTexture", context_get_current_texture);
301
+ add_method(Kind::CanvasContext, "getConfiguration", context_get_configuration);
302
+ }
303
+
304
+ } // namespace webgpu
305
+ } // namespace xtgui