xbintsc 0.6.13 → 0.6.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/driver/version.d.ts +1 -1
  3. package/dist/src/driver/version.js +1 -1
  4. package/dist/src/extensions/gui/features.d.ts +2 -0
  5. package/dist/src/extensions/gui/features.js +24 -0
  6. package/dist/src/extensions/gui/features.js.map +1 -1
  7. package/dist/src/extensions/gui/index.js +39 -0
  8. package/dist/src/extensions/gui/index.js.map +1 -1
  9. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  10. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  11. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  14. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  16. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  17. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  18. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  19. package/dist/tests/extensions/gui-features.test.js +18 -1
  20. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  21. package/doc/ai/extensions.md +7 -1
  22. package/doc/ai/zh-CN/extensions.md +6 -1
  23. package/doc/gui/webgpu.md +121 -0
  24. package/doc/gui.md +12 -1
  25. package/doc/zh-CN/gui/webgpu.md +111 -0
  26. package/doc/zh-CN/gui.md +10 -1
  27. package/llms.txt +1 -0
  28. package/package.json +7 -7
  29. package/runtime/ext_gui/browser.cpp +10 -0
  30. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  31. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  32. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  33. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  34. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  35. package/runtime/ext_gui/renderer_image.cpp +6 -0
  36. package/runtime/ext_gui/webgl_glsl.h +8 -0
  37. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  38. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  39. package/runtime/ext_gui/webgl_msl.h +4 -0
  40. package/runtime/ext_gui/webgpu.h +371 -0
  41. package/runtime/ext_gui/webgpu_api.h +42 -0
  42. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  43. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  44. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  45. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  46. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  47. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  48. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  49. package/runtime/ext_gui/webgpu_convert.cpp +309 -0
  50. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  51. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  52. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  53. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  54. package/runtime/ext_gui/webgpu_pass.h +33 -0
  55. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  56. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  57. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  58. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  59. package/runtime/ext_gui/webgpu_shader.cpp +242 -0
  60. package/runtime/ext_gui/wgsl.h +99 -0
  61. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  62. package/runtime/ext_gui/wgsl_ast.h +233 -0
  63. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  64. package/runtime/ext_gui/wgsl_emit.h +128 -0
  65. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  66. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  67. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  68. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  69. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  70. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  71. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  72. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  73. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  74. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  75. package/runtime/ext_gui/wgsl_parser.h +77 -0
  76. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  77. package/src/driver/version.ts +1 -1
  78. package/src/extensions/gui/features.ts +24 -0
  79. package/src/extensions/gui/index.ts +39 -0
@@ -0,0 +1,74 @@
1
+ /*
2
+ * xbintsc GUI engine — `webgpu` link feature registration.
3
+ *
4
+ * This translation unit is the only core-side place that references the WebGPU
5
+ * context/DOM bindings. It is pulled out of `gui.a` only when
6
+ * `webgpu_bridge.c` is linked, and installs `XtWebgpuApi` for the engine.
7
+ * Without it the table is null, `getContext("webgpu")` answers null and
8
+ * `navigator.gpu` is undefined, while the rest of the page works normally.
9
+ */
10
+
11
+ #include "webgpu_api.h"
12
+
13
+ #include "webgpu.h"
14
+ #include "webgpu_bindings.h"
15
+
16
+ namespace {
17
+
18
+ const XtWebgpuApi *g_api = nullptr;
19
+
20
+ bool canvas_get(XtGuiWindow *win, const void *node, uint32_t *id) {
21
+ return xtgui::webgpu::canvas_context(win, node, id);
22
+ }
23
+
24
+ bool canvas_find(XtGuiWindow *win, const void *node, uint32_t *id) {
25
+ return xtgui::webgpu::canvas_find(win, node, id);
26
+ }
27
+
28
+ void canvas_forget(XtGuiWindow *win, const void *node) {
29
+ xtgui::webgpu::canvas_forget(win, node);
30
+ }
31
+
32
+ void shutdown(XtGuiWindow *win) { xtgui::webgpu::canvas_shutdown(win); }
33
+
34
+ void info(uint32_t id, float *width, float *height, uint64_t *canvas_id) {
35
+ if (!xtgui::webgpu::canvas_info(id, width, height, canvas_id)) {
36
+ *width = 0.0f;
37
+ *height = 0.0f;
38
+ *canvas_id = 0;
39
+ }
40
+ }
41
+
42
+ void resize(uint32_t id, int width, int height) {
43
+ xtgui::webgpu::canvas_resize(id, width, height);
44
+ }
45
+
46
+ xt_value handle(xt_value self, uint32_t id) {
47
+ return xtgui::webgpu::context_handle(self, id);
48
+ }
49
+
50
+ xt_value proto() { return xtgui::webgpu::context_proto(); }
51
+
52
+ xt_value navigator_gpu() { return xtgui::webgpu::navigator_gpu(); }
53
+
54
+ xt_value host_type(const char *name) { return xtgui::webgpu::host_type(name); }
55
+
56
+ void *color_texture(uint64_t canvas_id) {
57
+ return xtgui::webgpu::canvas_color_texture(canvas_id);
58
+ }
59
+
60
+ const XtWebgpuApi kWebgpuApi = {
61
+ canvas_get, canvas_find, canvas_forget, shutdown, info,
62
+ resize, handle, proto, navigator_gpu, host_type,
63
+ color_texture,
64
+ };
65
+
66
+ } // namespace
67
+
68
+ void xt_webgpu_api_install(const XtWebgpuApi *api) { g_api = api; }
69
+
70
+ const XtWebgpuApi *xt_webgpu_api() { return g_api; }
71
+
72
+ extern "C" void xt_webgpu_feature_register(void) {
73
+ xt_webgpu_api_install(&kWebgpuApi);
74
+ }
@@ -0,0 +1,125 @@
1
+ /*
2
+ * xbintsc GUI engine — the WebGPU object registry, errors and JS handles.
3
+ */
4
+
5
+ #include "webgpu.h"
6
+
7
+ #include <algorithm>
8
+
9
+ #include "gui_internal.h"
10
+ #include "webgpu_bindings.h"
11
+
12
+ namespace xtgui {
13
+ namespace webgpu {
14
+ namespace {
15
+
16
+ std::unordered_map<uint32_t, std::shared_ptr<Object>> g_objects;
17
+ uint32_t g_next_id = 1;
18
+ std::unordered_map<uint32_t, uint32_t> g_queues; // device id -> queue id
19
+
20
+ ErrorKind kind_of_scope(int filter) {
21
+ if (filter == 1) return ErrorKind::OutOfMemory;
22
+ if (filter == 2) return ErrorKind::Internal;
23
+ return ErrorKind::Validation;
24
+ }
25
+
26
+ } // namespace
27
+
28
+ uint32_t add_object(std::shared_ptr<Object> object) {
29
+ const uint32_t id = g_next_id++;
30
+ object->id = id;
31
+ g_objects[id] = std::move(object);
32
+ return id;
33
+ }
34
+
35
+ std::shared_ptr<Object> get_object(uint32_t id) {
36
+ auto found = g_objects.find(id);
37
+ return found == g_objects.end() ? nullptr : found->second;
38
+ }
39
+
40
+ std::shared_ptr<Object> get_object(uint32_t id, Kind kind) {
41
+ std::shared_ptr<Object> object = get_object(id);
42
+ if (object == nullptr || object->kind != kind) return nullptr;
43
+ return object;
44
+ }
45
+
46
+ void remove_object(uint32_t id) { g_objects.erase(id); }
47
+
48
+ void device_children(uint32_t device_id, std::vector<uint32_t> *out) {
49
+ out->clear();
50
+ for (const auto &entry : g_objects) {
51
+ if (entry.second->device_id == device_id) out->push_back(entry.first);
52
+ }
53
+ }
54
+
55
+ SDL_GPUDevice *gpu_device() { return guidev::g_device; }
56
+
57
+ /* -- errors --------------------------------------------------------------- */
58
+
59
+ xt_value make_error(ErrorKind kind, const std::string &message) {
60
+ xt_value error = xt_object_new_with_proto(error_proto((int)kind));
61
+ xt_object_set(error, xt_string_from_cstr("message"), xt_string_from_cstr(message.c_str()));
62
+ const char *name = kind == ErrorKind::OutOfMemory
63
+ ? "GPUOutOfMemoryError"
64
+ : (kind == ErrorKind::Internal ? "GPUInternalError" : "GPUValidationError");
65
+ xt_object_set(error, xt_string_from_cstr("name"), xt_string_from_cstr(name));
66
+ xt_object_set(error, xt_string_from_cstr("__xt_webgpu_error"), xt_number((double)kind));
67
+ return error;
68
+ }
69
+
70
+ xt_value make_device_lost(const std::string &reason) {
71
+ xt_value info = xt_object_new_with_proto(device_lost_proto());
72
+ xt_object_set(info, xt_string_from_cstr("reason"), xt_string_from_cstr(reason.c_str()));
73
+ xt_object_set(info, xt_string_from_cstr("message"), xt_string_from_cstr(reason.c_str()));
74
+ return info;
75
+ }
76
+
77
+ void record_error(Device &device, ErrorKind kind, const std::string &message) {
78
+ for (auto scope = device.scopes.rbegin(); scope != device.scopes.rend(); ++scope) {
79
+ if (kind_of_scope(scope->filter) != kind) continue;
80
+ if (!scope->filled) {
81
+ scope->filled = true;
82
+ scope->kind = (int)kind;
83
+ scope->message = message;
84
+ }
85
+ return;
86
+ }
87
+ dispatch_uncaptured_error(device, make_error(kind, message));
88
+ }
89
+
90
+ /* -- JS handles ----------------------------------------------------------- */
91
+
92
+ xt_value wrap_object(const std::shared_ptr<Object> &object, xt_value proto) {
93
+ xt_value handle = xt_object_new_with_proto(proto);
94
+ xt_object_set(handle, xt_string_from_cstr("__xt_webgpu_id"), xt_number((double)object->id));
95
+ xt_object_set(handle, xt_string_from_cstr("__xt_webgpu_kind"), xt_number((double)object->kind));
96
+ if (object->kind == Kind::Device) {
97
+ auto *device = static_cast<Device *>(object.get());
98
+ device->js = handle;
99
+ xt_gc_add_root(&device->js);
100
+ }
101
+ return handle;
102
+ }
103
+
104
+ xt_value queue_object(uint32_t device_id) {
105
+ auto found = g_queues.find(device_id);
106
+ if (found != g_queues.end()) {
107
+ std::shared_ptr<Object> existing = get_object(found->second, Kind::Queue);
108
+ if (existing != nullptr) return wrap_object(existing, proto_for(Kind::Queue));
109
+ }
110
+ auto queue = std::make_shared<Object>();
111
+ queue->kind = Kind::Queue;
112
+ queue->device_id = device_id;
113
+ const uint32_t id = add_object(queue);
114
+ g_queues[device_id] = id;
115
+ return wrap_object(queue, proto_for(Kind::Queue));
116
+ }
117
+
118
+ xt_value device_object(uint32_t device_id) {
119
+ std::shared_ptr<Object> object = get_object(device_id, Kind::Device);
120
+ if (object == nullptr) return XT_UNDEFINED;
121
+ return static_cast<Device *>(object.get())->js;
122
+ }
123
+
124
+ } // namespace webgpu
125
+ } // namespace xtgui
@@ -0,0 +1,295 @@
1
+ /*
2
+ * xbintsc GUI engine — resolving WebGPU bind groups to SDL_GPU bindings.
3
+ */
4
+
5
+ #include "webgpu_pass.h"
6
+
7
+ #include <vector>
8
+
9
+ #include "webgpu_bindings.h"
10
+
11
+ namespace xtgui {
12
+ namespace webgpu {
13
+ namespace {
14
+
15
+ Device *device_of_pass(uint32_t id) {
16
+ std::shared_ptr<Object> object = get_object(id, Kind::Device);
17
+ return object == nullptr ? nullptr : static_cast<Device *>(object.get());
18
+ }
19
+
20
+ SDL_GPUTexture *texture_of(uint32_t texture_id) {
21
+ std::shared_ptr<Object> object = get_object(texture_id, Kind::Texture);
22
+ if (object == nullptr) return nullptr;
23
+ return static_cast<Texture *>(object.get())->gpu;
24
+ }
25
+
26
+ SDL_GPUBuffer *buffer_of(uint32_t buffer_id) {
27
+ std::shared_ptr<Object> object = get_object(buffer_id, Kind::Buffer);
28
+ if (object == nullptr) return nullptr;
29
+ Buffer *buffer = static_cast<Buffer *>(object.get());
30
+ return buffer->has_gpu ? buffer->gpu : nullptr;
31
+ }
32
+
33
+ SDL_GPUSampler *sampler_of(uint32_t sampler_id) {
34
+ std::shared_ptr<Object> object = get_object(sampler_id, Kind::Sampler);
35
+ if (object == nullptr) return nullptr;
36
+ return static_cast<Sampler *>(object.get())->gpu;
37
+ }
38
+
39
+ void bind_samplers(SDL_GPURenderPass *pass, const wgsl::StageCode &code,
40
+ const std::map<int, BindGroupSlot> &groups, bool vertex, Device *device) {
41
+ std::vector<SDL_GPUTextureSamplerBinding> bindings;
42
+ bindings.reserve(code.samplers.size());
43
+ for (const wgsl::TextureSlot &slot : code.samplers) {
44
+ SDL_GPUTexture *texture = nullptr;
45
+ SDL_GPUSampler *sampler = nullptr;
46
+ int dynamic = 0;
47
+ const BindGroupEntry *entry =
48
+ bound_entry(groups, slot.texture.group, slot.texture.binding, &dynamic);
49
+ if (entry != nullptr) texture = view_texture(entry->view);
50
+ if (slot.has_sampler) {
51
+ const BindGroupEntry *sampler_entry =
52
+ bound_entry(groups, slot.sampler.group, slot.sampler.binding, &dynamic);
53
+ if (sampler_entry != nullptr) sampler = sampler_of(sampler_entry->sampler);
54
+ }
55
+ if (texture == nullptr && device != nullptr) texture = fallback_texture(*device);
56
+ if (sampler == nullptr && device != nullptr) sampler = fallback_sampler(*device);
57
+ SDL_GPUTextureSamplerBinding binding = {};
58
+ binding.texture = texture;
59
+ binding.sampler = sampler;
60
+ bindings.push_back(binding);
61
+ }
62
+ if (bindings.empty()) return;
63
+ if (vertex) {
64
+ SDL_BindGPUVertexSamplers(pass, 0, bindings.data(), (Uint32)bindings.size());
65
+ } else {
66
+ SDL_BindGPUFragmentSamplers(pass, 0, bindings.data(), (Uint32)bindings.size());
67
+ }
68
+ }
69
+
70
+ void bind_storage_buffers(SDL_GPURenderPass *pass, const wgsl::StageCode &code,
71
+ const std::map<int, BindGroupSlot> &groups, bool vertex) {
72
+ std::vector<SDL_GPUBuffer *> buffers;
73
+ for (const wgsl::Binding &binding : code.storage_buffers) {
74
+ int dynamic = 0;
75
+ const BindGroupEntry *entry = bound_entry(groups, binding.group, binding.binding, &dynamic);
76
+ buffers.push_back(entry != nullptr ? buffer_of(entry->buffer) : nullptr);
77
+ }
78
+ if (buffers.empty()) return;
79
+ if (vertex) {
80
+ SDL_BindGPUVertexStorageBuffers(pass, 0, buffers.data(), (Uint32)buffers.size());
81
+ } else {
82
+ SDL_BindGPUFragmentStorageBuffers(pass, 0, buffers.data(), (Uint32)buffers.size());
83
+ }
84
+ }
85
+
86
+ void bind_storage_textures(SDL_GPURenderPass *pass, const wgsl::StageCode &code,
87
+ const std::map<int, BindGroupSlot> &groups, bool vertex) {
88
+ std::vector<SDL_GPUTexture *> textures;
89
+ for (const wgsl::Binding &binding : code.storage_textures) {
90
+ int dynamic = 0;
91
+ const BindGroupEntry *entry = bound_entry(groups, binding.group, binding.binding, &dynamic);
92
+ textures.push_back(entry != nullptr ? view_texture(entry->view) : nullptr);
93
+ }
94
+ if (textures.empty()) return;
95
+ if (vertex) {
96
+ SDL_BindGPUVertexStorageTextures(pass, 0, textures.data(), (Uint32)textures.size());
97
+ } else {
98
+ SDL_BindGPUFragmentStorageTextures(pass, 0, textures.data(), (Uint32)textures.size());
99
+ }
100
+ }
101
+
102
+ void push_uniforms(SDL_GPUCommandBuffer *command, const wgsl::StageCode &code,
103
+ const std::map<int, BindGroupSlot> &groups, bool vertex) {
104
+ for (size_t i = 0; i < code.uniform_buffers.size(); i++) {
105
+ int dynamic = 0;
106
+ const BindGroupEntry *entry = bound_entry(groups, code.uniform_buffers[i].group,
107
+ code.uniform_buffers[i].binding, &dynamic);
108
+ if (entry == nullptr) continue;
109
+ std::shared_ptr<Object> object = get_object(entry->buffer, Kind::Buffer);
110
+ if (object == nullptr) continue;
111
+ Buffer *buffer = static_cast<Buffer *>(object.get());
112
+ if (buffer->shadow.empty()) continue;
113
+ const uint64_t offset = entry->offset + (uint64_t)dynamic;
114
+ if (offset >= buffer->shadow.size()) continue;
115
+ uint64_t size = entry->size != 0 ? entry->size : buffer->shadow.size() - offset;
116
+ if (offset + size > buffer->shadow.size()) size = buffer->shadow.size() - offset;
117
+ if (size == 0) continue;
118
+ if (vertex) {
119
+ SDL_PushGPUVertexUniformData(command, (Uint32)i, buffer->shadow.data() + offset, (Uint32)size);
120
+ } else {
121
+ SDL_PushGPUFragmentUniformData(command, (Uint32)i, buffer->shadow.data() + offset,
122
+ (Uint32)size);
123
+ }
124
+ }
125
+ }
126
+
127
+ } // namespace
128
+
129
+ std::shared_ptr<BindGroup> bound_group(const std::map<int, BindGroupSlot> &groups, int index) {
130
+ auto found = groups.find(index);
131
+ if (found == groups.end()) return nullptr;
132
+ std::shared_ptr<Object> object = get_object(found->second.group, Kind::BindGroup);
133
+ if (object == nullptr) return nullptr;
134
+ return std::static_pointer_cast<BindGroup>(object);
135
+ }
136
+
137
+ const BindGroupEntry *bound_entry(const std::map<int, BindGroupSlot> &groups, int group,
138
+ int binding, int *dynamic_offset) {
139
+ if (dynamic_offset != nullptr) *dynamic_offset = 0;
140
+ auto found = groups.find(group);
141
+ if (found == groups.end()) return nullptr;
142
+ std::shared_ptr<BindGroup> bind_group = bound_group(groups, group);
143
+ if (bind_group == nullptr) return nullptr;
144
+ const BindGroupEntry *entry = bind_group->find(binding);
145
+ if (entry != nullptr && dynamic_offset != nullptr) {
146
+ /* Dynamic offsets are listed in the order of the layout's dynamic entries;
147
+ * the common case is one offset per group, which is what this maps. */
148
+ if (!found->second.dynamic_offsets.empty()) {
149
+ *dynamic_offset = (int)found->second.dynamic_offsets[0];
150
+ }
151
+ }
152
+ return entry;
153
+ }
154
+
155
+ SDL_GPUTexture *view_texture(uint32_t view_id) {
156
+ std::shared_ptr<Object> object = get_object(view_id, Kind::TextureView);
157
+ if (object == nullptr) return nullptr;
158
+ return texture_of(static_cast<TextureView *>(object.get())->texture);
159
+ }
160
+
161
+ SDL_GPUTexture *fallback_texture(Device &device) {
162
+ if (device.fallback_texture != nullptr) return device.fallback_texture;
163
+ SDL_GPUTextureCreateInfo info = {};
164
+ info.type = SDL_GPU_TEXTURETYPE_2D;
165
+ info.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
166
+ info.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER;
167
+ info.width = 1;
168
+ info.height = 1;
169
+ info.layer_count_or_depth = 1;
170
+ info.num_levels = 1;
171
+ info.sample_count = SDL_GPU_SAMPLECOUNT_1;
172
+ device.fallback_texture = SDL_CreateGPUTexture(gpu_device(), &info);
173
+ return device.fallback_texture;
174
+ }
175
+
176
+ SDL_GPUSampler *fallback_sampler(Device &device) {
177
+ if (device.fallback_sampler != nullptr) return device.fallback_sampler;
178
+ SDL_GPUSamplerCreateInfo info = {};
179
+ info.min_filter = SDL_GPU_FILTER_NEAREST;
180
+ info.mag_filter = SDL_GPU_FILTER_NEAREST;
181
+ info.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST;
182
+ info.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
183
+ info.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
184
+ info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
185
+ device.fallback_sampler = SDL_CreateGPUSampler(gpu_device(), &info);
186
+ return device.fallback_sampler;
187
+ }
188
+
189
+ bool bind_render_resources(RenderPassEncoder &pass, RenderPipeline &pipeline,
190
+ SDL_GPUCommandBuffer *command) {
191
+ Device *device = device_of_pass(pass.device_id);
192
+ if (command == nullptr || pass.pass == nullptr) return false;
193
+ bind_samplers(pass.pass, pipeline.vertex.code, pass.groups, true, device);
194
+ bind_storage_buffers(pass.pass, pipeline.vertex.code, pass.groups, true);
195
+ bind_storage_textures(pass.pass, pipeline.vertex.code, pass.groups, true);
196
+ push_uniforms(command, pipeline.vertex.code, pass.groups, true);
197
+ if (pipeline.has_fragment) {
198
+ bind_samplers(pass.pass, pipeline.fragment.code, pass.groups, false, device);
199
+ bind_storage_buffers(pass.pass, pipeline.fragment.code, pass.groups, false);
200
+ bind_storage_textures(pass.pass, pipeline.fragment.code, pass.groups, false);
201
+ push_uniforms(command, pipeline.fragment.code, pass.groups, false);
202
+ }
203
+ return true;
204
+ }
205
+
206
+ bool bind_compute_resources(ComputePassEncoder &pass, ComputePipeline &pipeline,
207
+ SDL_GPUCommandBuffer *command) {
208
+ Device *device = device_of_pass(pass.device_id);
209
+ if (command == nullptr) return false;
210
+ if (!pass.begun) {
211
+ /* SDL binds read-write storage resources when the pass begins, so they are
212
+ * collected from the bind groups set before the first dispatch. */
213
+ std::vector<SDL_GPUStorageTextureReadWriteBinding> textures;
214
+ for (const wgsl::Binding &binding : pipeline.stage.code.write_storage_textures) {
215
+ const BindGroupEntry *entry = bound_entry(pass.groups, binding.group, binding.binding, nullptr);
216
+ SDL_GPUStorageTextureReadWriteBinding texture = {};
217
+ texture.texture = entry != nullptr ? view_texture(entry->view) : nullptr;
218
+ /* Keep the previous contents: a WebGPU storage texture may be read too. */
219
+ texture.cycle = false;
220
+ textures.push_back(texture);
221
+ }
222
+ std::vector<SDL_GPUStorageBufferReadWriteBinding> buffers;
223
+ for (const wgsl::Binding &binding : pipeline.stage.code.write_storage_buffers) {
224
+ const BindGroupEntry *entry = bound_entry(pass.groups, binding.group, binding.binding, nullptr);
225
+ SDL_GPUStorageBufferReadWriteBinding buffer = {};
226
+ buffer.buffer = entry != nullptr ? buffer_of(entry->buffer) : nullptr;
227
+ /* Keep the previous contents: the shader may read before writing. */
228
+ buffer.cycle = false;
229
+ buffers.push_back(buffer);
230
+ }
231
+ pass.pass = SDL_BeginGPUComputePass(command, textures.empty() ? nullptr : textures.data(),
232
+ (Uint32)textures.size(),
233
+ buffers.empty() ? nullptr : buffers.data(),
234
+ (Uint32)buffers.size());
235
+ pass.begun = true;
236
+ }
237
+ if (pass.pass == nullptr) return false;
238
+
239
+ std::vector<SDL_GPUTextureSamplerBinding> samplers;
240
+ for (const wgsl::TextureSlot &slot : pipeline.stage.code.samplers) {
241
+ SDL_GPUTexture *texture = nullptr;
242
+ SDL_GPUSampler *sampler = nullptr;
243
+ const BindGroupEntry *entry = bound_entry(pass.groups, slot.texture.group, slot.texture.binding,
244
+ nullptr);
245
+ if (entry != nullptr) texture = view_texture(entry->view);
246
+ if (slot.has_sampler) {
247
+ const BindGroupEntry *sampler_entry =
248
+ bound_entry(pass.groups, slot.sampler.group, slot.sampler.binding, nullptr);
249
+ if (sampler_entry != nullptr) sampler = sampler_of(sampler_entry->sampler);
250
+ }
251
+ if (texture == nullptr && device != nullptr) texture = fallback_texture(*device);
252
+ if (sampler == nullptr && device != nullptr) sampler = fallback_sampler(*device);
253
+ SDL_GPUTextureSamplerBinding binding = {};
254
+ binding.texture = texture;
255
+ binding.sampler = sampler;
256
+ samplers.push_back(binding);
257
+ }
258
+ if (!samplers.empty()) {
259
+ SDL_BindGPUComputeSamplers(pass.pass, 0, samplers.data(), (Uint32)samplers.size());
260
+ }
261
+ if (!pipeline.stage.code.storage_textures.empty()) {
262
+ std::vector<SDL_GPUTexture *> textures;
263
+ for (const wgsl::Binding &binding : pipeline.stage.code.storage_textures) {
264
+ const BindGroupEntry *entry = bound_entry(pass.groups, binding.group, binding.binding, nullptr);
265
+ textures.push_back(entry != nullptr ? view_texture(entry->view) : nullptr);
266
+ }
267
+ SDL_BindGPUComputeStorageTextures(pass.pass, 0, textures.data(), (Uint32)textures.size());
268
+ }
269
+ if (!pipeline.stage.code.storage_buffers.empty()) {
270
+ std::vector<SDL_GPUBuffer *> buffers;
271
+ for (const wgsl::Binding &binding : pipeline.stage.code.storage_buffers) {
272
+ const BindGroupEntry *entry = bound_entry(pass.groups, binding.group, binding.binding, nullptr);
273
+ buffers.push_back(entry != nullptr ? buffer_of(entry->buffer) : nullptr);
274
+ }
275
+ SDL_BindGPUComputeStorageBuffers(pass.pass, 0, buffers.data(), (Uint32)buffers.size());
276
+ }
277
+ for (size_t i = 0; i < pipeline.stage.code.uniform_buffers.size(); i++) {
278
+ const BindGroupEntry *entry = bound_entry(pass.groups, pipeline.stage.code.uniform_buffers[i].group,
279
+ pipeline.stage.code.uniform_buffers[i].binding, nullptr);
280
+ if (entry == nullptr) continue;
281
+ std::shared_ptr<Object> object = get_object(entry->buffer, Kind::Buffer);
282
+ if (object == nullptr) continue;
283
+ Buffer *buffer = static_cast<Buffer *>(object.get());
284
+ if (buffer->shadow.empty()) continue;
285
+ uint64_t offset = entry->offset;
286
+ if (offset >= buffer->shadow.size()) continue;
287
+ uint64_t size = entry->size != 0 ? entry->size : buffer->shadow.size() - offset;
288
+ if (offset + size > buffer->shadow.size()) size = buffer->shadow.size() - offset;
289
+ SDL_PushGPUComputeUniformData(command, (Uint32)i, buffer->shadow.data() + offset, (Uint32)size);
290
+ }
291
+ return true;
292
+ }
293
+
294
+ } // namespace webgpu
295
+ } // namespace xtgui
@@ -0,0 +1,33 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGPU pass resource binding.
3
+ *
4
+ * SDL_GPU binds by flat slot (sampler N, storage buffer N, uniform slot N), so
5
+ * each draw/dispatch walks the pipeline's translated stage resource lists and
6
+ * resolves the matching WebGPU bind-group entries.
7
+ */
8
+ #ifndef XT_GUI_WEBGPU_PASS_H
9
+ #define XT_GUI_WEBGPU_PASS_H
10
+
11
+ #include "webgpu.h"
12
+
13
+ namespace xtgui {
14
+ namespace webgpu {
15
+
16
+ std::shared_ptr<BindGroup> bound_group(const std::map<int, BindGroupSlot> &groups, int index);
17
+ const BindGroupEntry *bound_entry(const std::map<int, BindGroupSlot> &groups, int group,
18
+ int binding, int *dynamic_offset);
19
+ SDL_GPUTexture *view_texture(uint32_t view_id);
20
+ SDL_GPUTexture *fallback_texture(Device &device);
21
+ SDL_GPUSampler *fallback_sampler(Device &device);
22
+
23
+ /** Bind every resource the vertex and fragment stages use. */
24
+ bool bind_render_resources(RenderPassEncoder &pass, RenderPipeline &pipeline,
25
+ SDL_GPUCommandBuffer *command);
26
+ /** Bind a compute stage's read resources; begins the SDL pass if needed. */
27
+ bool bind_compute_resources(ComputePassEncoder &pass, ComputePipeline &pipeline,
28
+ SDL_GPUCommandBuffer *command);
29
+
30
+ } // namespace webgpu
31
+ } // namespace xtgui
32
+
33
+ #endif /* XT_GUI_WEBGPU_PASS_H */