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,463 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WebGPU shader modules, bind groups and pipelines.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include <cstring>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
#include "webgpu_bindings.h"
|
|
9
|
+
#include "wgsl.h"
|
|
10
|
+
|
|
11
|
+
namespace xtgui {
|
|
12
|
+
namespace webgpu {
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
uint32_t handle_id(xt_value value) {
|
|
16
|
+
if (!XT_IS_OBJECT(value)) return 0;
|
|
17
|
+
return (uint32_t)xt_to_number(xt_object_get_cstr(value, "__xt_webgpu_id"));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
xt_value label_of(Object &object) { return xt_string_from_cstr(object.label.c_str()); }
|
|
21
|
+
|
|
22
|
+
SDL_GPUStencilOp stencil_op(const std::string &name) {
|
|
23
|
+
if (name == "zero") return SDL_GPU_STENCILOP_ZERO;
|
|
24
|
+
if (name == "replace") return SDL_GPU_STENCILOP_REPLACE;
|
|
25
|
+
if (name == "invert") return SDL_GPU_STENCILOP_INVERT;
|
|
26
|
+
if (name == "increment-clamp") return SDL_GPU_STENCILOP_INCREMENT_AND_CLAMP;
|
|
27
|
+
if (name == "decrement-clamp") return SDL_GPU_STENCILOP_DECREMENT_AND_CLAMP;
|
|
28
|
+
if (name == "increment-wrap") return SDL_GPU_STENCILOP_INCREMENT_AND_WRAP;
|
|
29
|
+
if (name == "decrement-wrap") return SDL_GPU_STENCILOP_DECREMENT_AND_WRAP;
|
|
30
|
+
return SDL_GPU_STENCILOP_KEEP;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void fill_stencil(xt_value value, SDL_GPUStencilOpState *state) {
|
|
34
|
+
if (!XT_IS_OBJECT(value)) {
|
|
35
|
+
state->compare_op = SDL_GPU_COMPAREOP_ALWAYS;
|
|
36
|
+
state->fail_op = SDL_GPU_STENCILOP_KEEP;
|
|
37
|
+
state->depth_fail_op = SDL_GPU_STENCILOP_KEEP;
|
|
38
|
+
state->pass_op = SDL_GPU_STENCILOP_KEEP;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
state->compare_op = Convert::compare(prop_string(value, "compare"));
|
|
42
|
+
state->fail_op = stencil_op(prop_string(value, "failOp"));
|
|
43
|
+
state->depth_fail_op = stencil_op(prop_string(value, "depthFailOp"));
|
|
44
|
+
state->pass_op = stencil_op(prop_string(value, "passOp"));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void fill_blend(xt_value target, SDL_GPUColorTargetBlendState *state, uint32_t write_mask) {
|
|
48
|
+
xt_value blend = prop_object(target, "blend");
|
|
49
|
+
if (!XT_IS_OBJECT(blend)) {
|
|
50
|
+
state->enable_blend = false;
|
|
51
|
+
state->color_write_mask = write_mask;
|
|
52
|
+
state->enable_color_write_mask = write_mask != 15;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
xt_value color = prop_object(blend, "color");
|
|
56
|
+
xt_value alpha = prop_object(blend, "alpha");
|
|
57
|
+
state->enable_blend = true;
|
|
58
|
+
state->src_color_blendfactor = Convert::blend_factor(prop_string(color, "srcFactor"));
|
|
59
|
+
state->dst_color_blendfactor = Convert::blend_factor(prop_string(color, "dstFactor"));
|
|
60
|
+
state->color_blend_op = Convert::blend_op(prop_string(color, "operation"));
|
|
61
|
+
state->src_alpha_blendfactor = Convert::blend_factor(prop_string(alpha, "srcFactor"));
|
|
62
|
+
state->dst_alpha_blendfactor = Convert::blend_factor(prop_string(alpha, "dstFactor"));
|
|
63
|
+
state->alpha_blend_op = Convert::blend_op(prop_string(alpha, "operation"));
|
|
64
|
+
state->color_write_mask = write_mask;
|
|
65
|
+
state->enable_color_write_mask = write_mask != 15;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
bool build_color_targets(xt_value targets, std::vector<SDL_GPUColorTargetDescription> *out,
|
|
69
|
+
std::string *error) {
|
|
70
|
+
if (!XT_IS_ARRAY(targets)) return true;
|
|
71
|
+
const uint32_t count = (uint32_t)xt_to_number(xt_array_length(targets));
|
|
72
|
+
for (uint32_t i = 0; i < count; i++) {
|
|
73
|
+
xt_value target = xt_array_get(targets, xt_number((double)i));
|
|
74
|
+
const std::string format = prop_string(target, "format");
|
|
75
|
+
SDL_GPUColorTargetDescription description = {};
|
|
76
|
+
if (!Convert::texture_format(format, &description.format)) {
|
|
77
|
+
*error = "unsupported color target format '" + format + "'";
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
fill_blend(target, &description.blend_state, (uint32_t)prop_u64(target, "writeMask", 15));
|
|
81
|
+
out->push_back(description);
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
bool make_pipeline_layout(Device &device, xt_value descriptor,
|
|
87
|
+
const std::vector<std::pair<int, wgsl::StageCode>> &stages,
|
|
88
|
+
uint32_t *layout_id, std::string *error) {
|
|
89
|
+
xt_value layout = prop_object(descriptor, "layout");
|
|
90
|
+
if (XT_IS_OBJECT(layout)) {
|
|
91
|
+
const uint32_t id = handle_id(layout);
|
|
92
|
+
if (get_object(id, Kind::PipelineLayout) == nullptr) {
|
|
93
|
+
*error = "the pipeline layout is not a GPUPipelineLayout";
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
*layout_id = id;
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
auto pipeline_layout = std::make_shared<PipelineLayout>();
|
|
100
|
+
pipeline_layout->kind = Kind::PipelineLayout;
|
|
101
|
+
pipeline_layout->device_id = device.id;
|
|
102
|
+
pipeline_layout->layouts = make_auto_layouts(device, stages);
|
|
103
|
+
*layout_id = add_object(pipeline_layout);
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
bool build_vertex_input(xt_value vertex, SDL_GPUVertexInputState *state,
|
|
108
|
+
std::vector<SDL_GPUVertexBufferDescription> *buffers,
|
|
109
|
+
std::vector<SDL_GPUVertexAttribute> *attributes, std::string *error) {
|
|
110
|
+
xt_value list = prop_object(vertex, "buffers");
|
|
111
|
+
if (!XT_IS_ARRAY(list)) return true;
|
|
112
|
+
const uint32_t count = (uint32_t)xt_to_number(xt_array_length(list));
|
|
113
|
+
for (uint32_t i = 0; i < count; i++) {
|
|
114
|
+
xt_value buffer = xt_array_get(list, xt_number((double)i));
|
|
115
|
+
SDL_GPUVertexBufferDescription description = {};
|
|
116
|
+
description.slot = i;
|
|
117
|
+
description.pitch = (Uint32)prop_u64(buffer, "arrayStride", 0);
|
|
118
|
+
description.input_rate = prop_string(buffer, "stepMode") == "instance"
|
|
119
|
+
? SDL_GPU_VERTEXINPUTRATE_INSTANCE
|
|
120
|
+
: SDL_GPU_VERTEXINPUTRATE_VERTEX;
|
|
121
|
+
buffers->push_back(description);
|
|
122
|
+
xt_value attrs = prop_object(buffer, "attributes");
|
|
123
|
+
if (!XT_IS_ARRAY(attrs)) continue;
|
|
124
|
+
const uint32_t attr_count = (uint32_t)xt_to_number(xt_array_length(attrs));
|
|
125
|
+
for (uint32_t a = 0; a < attr_count; a++) {
|
|
126
|
+
xt_value attribute = xt_array_get(attrs, xt_number((double)a));
|
|
127
|
+
SDL_GPUVertexAttribute entry = {};
|
|
128
|
+
entry.location = (Uint32)prop_u64(attribute, "shaderLocation", 0);
|
|
129
|
+
entry.buffer_slot = i;
|
|
130
|
+
entry.offset = (Uint32)prop_u64(attribute, "offset", 0);
|
|
131
|
+
const std::string format = prop_string(attribute, "format");
|
|
132
|
+
if (!Convert::vertex_format(format, &entry.format)) {
|
|
133
|
+
*error = "unsupported vertex format '" + format + "'";
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
attributes->push_back(entry);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
state->vertex_buffer_descriptions = buffers->data();
|
|
140
|
+
state->num_vertex_buffers = (Uint32)buffers->size();
|
|
141
|
+
state->vertex_attributes = attributes->data();
|
|
142
|
+
state->num_vertex_attributes = (Uint32)attributes->size();
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/* -- bind groups ---------------------------------------------------------- */
|
|
147
|
+
|
|
148
|
+
bool parse_bind_group_impl(Device &device, xt_value descriptor, BindGroup *out, std::string *error) {
|
|
149
|
+
std::shared_ptr<Object> layout_object =
|
|
150
|
+
get_object(handle_id(prop_object(descriptor, "layout")), Kind::BindGroupLayout);
|
|
151
|
+
if (layout_object == nullptr) {
|
|
152
|
+
*error = "createBindGroup: layout is not a GPUBindGroupLayout";
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
BindGroupLayout *layout = static_cast<BindGroupLayout *>(layout_object.get());
|
|
156
|
+
out->layout = layout->id;
|
|
157
|
+
xt_value entries = prop_object(descriptor, "entries");
|
|
158
|
+
if (!XT_IS_ARRAY(entries)) {
|
|
159
|
+
*error = "createBindGroup: entries must be an array";
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
const uint32_t count = (uint32_t)xt_to_number(xt_array_length(entries));
|
|
163
|
+
for (uint32_t i = 0; i < count; i++) {
|
|
164
|
+
xt_value item = xt_array_get(entries, xt_number((double)i));
|
|
165
|
+
BindGroupEntry entry;
|
|
166
|
+
entry.binding = (int)prop_u64(item, "binding", 0);
|
|
167
|
+
const LayoutEntry *expected = nullptr;
|
|
168
|
+
for (const LayoutEntry &candidate : layout->entries) {
|
|
169
|
+
if (candidate.binding == entry.binding) expected = &candidate;
|
|
170
|
+
}
|
|
171
|
+
if (expected == nullptr) {
|
|
172
|
+
*error = "createBindGroup: binding " + std::to_string(entry.binding) + " is not in the layout";
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
xt_value resource = prop_object(item, "resource");
|
|
176
|
+
xt_value buffer = prop_object(resource, "buffer");
|
|
177
|
+
xt_value sampler = prop_object(resource, "sampler");
|
|
178
|
+
xt_value view = prop_object(resource, "textureView");
|
|
179
|
+
/* A WebGPU bind-group entry names the sampler/view object directly; accept
|
|
180
|
+
* the `{ sampler }` / `{ textureView }` wrappers too. */
|
|
181
|
+
if (XT_IS_OBJECT(resource) && !XT_IS_OBJECT(buffer) && !XT_IS_OBJECT(sampler) &&
|
|
182
|
+
!XT_IS_OBJECT(view)) {
|
|
183
|
+
const xt_value kind_value = xt_object_get_cstr(resource, "__xt_webgpu_kind");
|
|
184
|
+
const int kind = XT_IS_NUMBER(kind_value) ? (int)xt_to_number(kind_value) : 0;
|
|
185
|
+
if (kind == (int)Kind::Sampler) sampler = resource;
|
|
186
|
+
else if (kind == (int)Kind::TextureView) view = resource;
|
|
187
|
+
}
|
|
188
|
+
if (XT_IS_OBJECT(buffer)) {
|
|
189
|
+
std::shared_ptr<Object> object = get_object(handle_id(buffer), Kind::Buffer);
|
|
190
|
+
if (object == nullptr) {
|
|
191
|
+
*error = "createBindGroup: resource.buffer is not a GPUBuffer";
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
entry.buffer = object->id;
|
|
195
|
+
entry.offset = prop_u64(resource, "offset", 0);
|
|
196
|
+
entry.size = prop_u64(resource, "size", 0);
|
|
197
|
+
} else if (XT_IS_OBJECT(sampler)) {
|
|
198
|
+
std::shared_ptr<Object> object = get_object(handle_id(sampler), Kind::Sampler);
|
|
199
|
+
if (object == nullptr) {
|
|
200
|
+
*error = "createBindGroup: resource.sampler is not a GPUSampler";
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
entry.sampler = object->id;
|
|
204
|
+
} else if (XT_IS_OBJECT(view)) {
|
|
205
|
+
std::shared_ptr<Object> object = get_object(handle_id(view), Kind::TextureView);
|
|
206
|
+
if (object == nullptr) {
|
|
207
|
+
*error = "createBindGroup: resource.textureView is not a GPUTextureView";
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
entry.view = object->id;
|
|
211
|
+
} else {
|
|
212
|
+
*error = "createBindGroup: entry " + std::to_string(entry.binding) + " has no resource";
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
out->entries.push_back(entry);
|
|
216
|
+
}
|
|
217
|
+
(void)device;
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/* -- shader modules ------------------------------------------------------- */
|
|
222
|
+
|
|
223
|
+
xt_value shader_compilation_info(Object &object, int argc, xt_value *argv) {
|
|
224
|
+
(void)argc;
|
|
225
|
+
(void)argv;
|
|
226
|
+
ShaderModule &module = static_cast<ShaderModule &>(object);
|
|
227
|
+
xt_value info = xt_object_new();
|
|
228
|
+
xt_value messages = xt_array_new(0, nullptr);
|
|
229
|
+
if (!module.valid) {
|
|
230
|
+
xt_value message = xt_object_new();
|
|
231
|
+
xt_object_set(message, xt_string_from_cstr("message"), xt_string_from_cstr(module.error.c_str()));
|
|
232
|
+
xt_object_set(message, xt_string_from_cstr("type"), xt_string_from_cstr("error"));
|
|
233
|
+
xt_object_set(message, xt_string_from_cstr("lineNum"), xt_number(module.error_line));
|
|
234
|
+
xt_object_set(message, xt_string_from_cstr("linePos"), xt_number(module.error_column));
|
|
235
|
+
xt_object_set(message, xt_string_from_cstr("offset"), xt_number(0));
|
|
236
|
+
xt_object_set(message, xt_string_from_cstr("length"), xt_number(0));
|
|
237
|
+
xt_array_push(messages, message);
|
|
238
|
+
}
|
|
239
|
+
xt_object_set(info, xt_string_from_cstr("messages"), messages);
|
|
240
|
+
return resolved(info);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/* -- pipelines ------------------------------------------------------------ */
|
|
244
|
+
|
|
245
|
+
xt_value create_render_pipeline(Object &object, int argc, xt_value *argv) {
|
|
246
|
+
Device &device = static_cast<Device &>(object);
|
|
247
|
+
xt_value descriptor = arg_object(argc, argv, 0);
|
|
248
|
+
if (!XT_IS_OBJECT(descriptor)) {
|
|
249
|
+
throw_type_error("createRenderPipeline needs a descriptor");
|
|
250
|
+
return XT_UNDEFINED;
|
|
251
|
+
}
|
|
252
|
+
xt_value vertex = prop_object(descriptor, "vertex");
|
|
253
|
+
std::shared_ptr<Object> vertex_module =
|
|
254
|
+
get_object(handle_id(prop_object(vertex, "module")), Kind::ShaderModule);
|
|
255
|
+
if (vertex_module == nullptr) {
|
|
256
|
+
return validation(device, "createRenderPipeline: vertex.module is not a GPUShaderModule");
|
|
257
|
+
}
|
|
258
|
+
auto pipeline = std::make_shared<RenderPipeline>();
|
|
259
|
+
pipeline->kind = Kind::RenderPipeline;
|
|
260
|
+
pipeline->device_id = device.id;
|
|
261
|
+
std::string error;
|
|
262
|
+
if (!compile_shader(device, *static_cast<ShaderModule *>(vertex_module.get()),
|
|
263
|
+
prop_string(vertex, "entryPoint"), 0, &pipeline->vertex, &error)) {
|
|
264
|
+
return validation(device, error);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
std::vector<std::pair<int, wgsl::StageCode>> stages;
|
|
268
|
+
stages.emplace_back(0, pipeline->vertex.code);
|
|
269
|
+
std::vector<SDL_GPUColorTargetDescription> targets;
|
|
270
|
+
xt_value fragment = prop_object(descriptor, "fragment");
|
|
271
|
+
if (XT_IS_OBJECT(fragment)) {
|
|
272
|
+
std::shared_ptr<Object> fragment_module =
|
|
273
|
+
get_object(handle_id(prop_object(fragment, "module")), Kind::ShaderModule);
|
|
274
|
+
if (fragment_module == nullptr) {
|
|
275
|
+
return validation(device, "createRenderPipeline: fragment.module is not a GPUShaderModule");
|
|
276
|
+
}
|
|
277
|
+
if (!compile_shader(device, *static_cast<ShaderModule *>(fragment_module.get()),
|
|
278
|
+
prop_string(fragment, "entryPoint"), 1, &pipeline->fragment, &error)) {
|
|
279
|
+
return validation(device, error);
|
|
280
|
+
}
|
|
281
|
+
pipeline->has_fragment = true;
|
|
282
|
+
stages.emplace_back(1, pipeline->fragment.code);
|
|
283
|
+
if (!build_color_targets(prop_object(fragment, "targets"), &targets, &error)) {
|
|
284
|
+
return validation(device, error);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (!make_pipeline_layout(device, descriptor, stages, &pipeline->layout, &error)) {
|
|
288
|
+
return validation(device, error);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
std::vector<SDL_GPUVertexBufferDescription> buffers;
|
|
292
|
+
std::vector<SDL_GPUVertexAttribute> attributes;
|
|
293
|
+
SDL_GPUVertexInputState input = {};
|
|
294
|
+
if (!build_vertex_input(vertex, &input, &buffers, &attributes, &error)) {
|
|
295
|
+
return validation(device, error);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
xt_value primitive = prop_object(descriptor, "primitive");
|
|
299
|
+
xt_value depth = prop_object(descriptor, "depthStencil");
|
|
300
|
+
xt_value multisample = prop_object(descriptor, "multisample");
|
|
301
|
+
const std::string depth_format_name = prop_string(depth, "format");
|
|
302
|
+
|
|
303
|
+
SDL_GPUGraphicsPipelineCreateInfo info = {};
|
|
304
|
+
info.vertex_shader = pipeline->vertex.shader;
|
|
305
|
+
info.fragment_shader = pipeline->has_fragment ? pipeline->fragment.shader : nullptr;
|
|
306
|
+
info.vertex_input_state = input;
|
|
307
|
+
info.primitive_type = Convert::topology(prop_string(primitive, "topology"));
|
|
308
|
+
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
|
|
309
|
+
const std::string cull = prop_string(primitive, "cullMode");
|
|
310
|
+
info.rasterizer_state.cull_mode = cull == "front" ? SDL_GPU_CULLMODE_FRONT
|
|
311
|
+
: cull == "back" ? SDL_GPU_CULLMODE_BACK
|
|
312
|
+
: SDL_GPU_CULLMODE_NONE;
|
|
313
|
+
info.rasterizer_state.front_face = prop_string(primitive, "frontFace") == "cw"
|
|
314
|
+
? SDL_GPU_FRONTFACE_CLOCKWISE
|
|
315
|
+
: SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
|
|
316
|
+
info.rasterizer_state.enable_depth_bias = prop_bool(depth, "depthBias", 0) != 0;
|
|
317
|
+
info.rasterizer_state.depth_bias_constant_factor =
|
|
318
|
+
(float)prop_number(depth, "depthBiasConstantFactor", 0.0);
|
|
319
|
+
info.rasterizer_state.depth_bias_clamp = (float)prop_number(depth, "depthBiasClamp", 0.0);
|
|
320
|
+
info.rasterizer_state.depth_bias_slope_factor =
|
|
321
|
+
(float)prop_number(depth, "depthBiasSlopeScale", 0.0);
|
|
322
|
+
info.rasterizer_state.enable_depth_clip = true;
|
|
323
|
+
const int samples = (int)prop_u64(multisample, "count", 1);
|
|
324
|
+
info.multisample_state.sample_count = samples >= 8 ? SDL_GPU_SAMPLECOUNT_8
|
|
325
|
+
: samples >= 4 ? SDL_GPU_SAMPLECOUNT_4
|
|
326
|
+
: samples >= 2 ? SDL_GPU_SAMPLECOUNT_2
|
|
327
|
+
: SDL_GPU_SAMPLECOUNT_1;
|
|
328
|
+
info.multisample_state.sample_mask = 0;
|
|
329
|
+
info.multisample_state.enable_mask = false;
|
|
330
|
+
info.depth_stencil_state.enable_depth_test = false;
|
|
331
|
+
if (XT_IS_OBJECT(depth) && !depth_format_name.empty()) {
|
|
332
|
+
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
|
333
|
+
if (!Convert::texture_format(depth_format_name, &format)) {
|
|
334
|
+
return validation(device, "unsupported depth format '" + depth_format_name + "'");
|
|
335
|
+
}
|
|
336
|
+
info.target_info.has_depth_stencil_target = true;
|
|
337
|
+
info.target_info.depth_stencil_format = format;
|
|
338
|
+
const std::string compare = prop_string(depth, "depthCompare");
|
|
339
|
+
info.depth_stencil_state.compare_op = Convert::compare(compare);
|
|
340
|
+
info.depth_stencil_state.enable_depth_test = compare != "always" && !compare.empty();
|
|
341
|
+
info.depth_stencil_state.enable_depth_write = prop_bool(depth, "depthWriteEnabled", false);
|
|
342
|
+
info.depth_stencil_state.enable_stencil_test =
|
|
343
|
+
XT_IS_OBJECT(prop_object(depth, "stencilFront")) ||
|
|
344
|
+
XT_IS_OBJECT(prop_object(depth, "stencilBack"));
|
|
345
|
+
fill_stencil(prop_object(depth, "stencilFront"),
|
|
346
|
+
&info.depth_stencil_state.front_stencil_state);
|
|
347
|
+
fill_stencil(prop_object(depth, "stencilBack"), &info.depth_stencil_state.back_stencil_state);
|
|
348
|
+
info.depth_stencil_state.compare_mask = 0xFF;
|
|
349
|
+
info.depth_stencil_state.write_mask = 0xFF;
|
|
350
|
+
}
|
|
351
|
+
info.target_info.color_target_descriptions = targets.empty() ? nullptr : targets.data();
|
|
352
|
+
info.target_info.num_color_targets = (Uint32)targets.size();
|
|
353
|
+
|
|
354
|
+
pipeline->gpu = SDL_CreateGPUGraphicsPipeline(gpu_device(), &info);
|
|
355
|
+
if (pipeline->gpu == nullptr) {
|
|
356
|
+
return validation(device, std::string("createRenderPipeline failed: ") + SDL_GetError());
|
|
357
|
+
}
|
|
358
|
+
return handle_of(add_object(pipeline));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
xt_value create_compute_pipeline(Object &object, int argc, xt_value *argv) {
|
|
362
|
+
Device &device = static_cast<Device &>(object);
|
|
363
|
+
xt_value descriptor = arg_object(argc, argv, 0);
|
|
364
|
+
if (!XT_IS_OBJECT(descriptor)) {
|
|
365
|
+
throw_type_error("createComputePipeline needs a descriptor");
|
|
366
|
+
return XT_UNDEFINED;
|
|
367
|
+
}
|
|
368
|
+
xt_value compute = prop_object(descriptor, "compute");
|
|
369
|
+
std::shared_ptr<Object> module =
|
|
370
|
+
get_object(handle_id(prop_object(compute, "module")), Kind::ShaderModule);
|
|
371
|
+
if (module == nullptr) {
|
|
372
|
+
return validation(device, "createComputePipeline: compute.module is not a GPUShaderModule");
|
|
373
|
+
}
|
|
374
|
+
auto pipeline = std::make_shared<ComputePipeline>();
|
|
375
|
+
pipeline->kind = Kind::ComputePipeline;
|
|
376
|
+
pipeline->device_id = device.id;
|
|
377
|
+
std::string error;
|
|
378
|
+
if (!compile_shader_code(device, *static_cast<ShaderModule *>(module.get()),
|
|
379
|
+
prop_string(compute, "entryPoint"), 2, &pipeline->stage, &error)) {
|
|
380
|
+
return validation(device, error);
|
|
381
|
+
}
|
|
382
|
+
std::vector<std::pair<int, wgsl::StageCode>> stages;
|
|
383
|
+
stages.emplace_back(2, pipeline->stage.code);
|
|
384
|
+
if (!make_pipeline_layout(device, descriptor, stages, &pipeline->layout, &error)) {
|
|
385
|
+
return validation(device, error);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
SDL_GPUComputePipelineCreateInfo info = {};
|
|
389
|
+
if (pipeline->stage.format == SDL_GPU_SHADERFORMAT_MSL) {
|
|
390
|
+
info.code = reinterpret_cast<const Uint8 *>(pipeline->stage.msl.data());
|
|
391
|
+
info.code_size = pipeline->stage.msl.size();
|
|
392
|
+
} else {
|
|
393
|
+
info.code = reinterpret_cast<const Uint8 *>(pipeline->stage.spirv.data());
|
|
394
|
+
info.code_size = pipeline->stage.spirv.size() * sizeof(uint32_t);
|
|
395
|
+
}
|
|
396
|
+
info.entrypoint = pipeline->stage.entry.c_str();
|
|
397
|
+
info.format = pipeline->stage.format;
|
|
398
|
+
info.num_samplers = (Uint32)pipeline->stage.code.samplers.size();
|
|
399
|
+
info.num_readonly_storage_textures = (Uint32)pipeline->stage.code.storage_textures.size();
|
|
400
|
+
info.num_readonly_storage_buffers = (Uint32)pipeline->stage.code.storage_buffers.size();
|
|
401
|
+
info.num_readwrite_storage_textures =
|
|
402
|
+
(Uint32)pipeline->stage.code.write_storage_textures.size();
|
|
403
|
+
info.num_readwrite_storage_buffers = (Uint32)pipeline->stage.code.write_storage_buffers.size();
|
|
404
|
+
info.num_uniform_buffers = (Uint32)pipeline->stage.code.uniform_buffers.size();
|
|
405
|
+
info.threadcount_x = (Uint32)pipeline->stage.code.workgroup_size[0];
|
|
406
|
+
info.threadcount_y = (Uint32)pipeline->stage.code.workgroup_size[1];
|
|
407
|
+
info.threadcount_z = (Uint32)pipeline->stage.code.workgroup_size[2];
|
|
408
|
+
pipeline->gpu = SDL_CreateGPUComputePipeline(gpu_device(), &info);
|
|
409
|
+
if (pipeline->gpu == nullptr) {
|
|
410
|
+
return validation(device, std::string("createComputePipeline failed: ") + SDL_GetError());
|
|
411
|
+
}
|
|
412
|
+
return handle_of(add_object(pipeline));
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
xt_value pipeline_bind_group_layout(Object &object, int argc, xt_value *argv) {
|
|
416
|
+
const int index = (int)arg_number(argc, argv, 0);
|
|
417
|
+
uint32_t layout_id = 0;
|
|
418
|
+
if (object.kind == Kind::ComputePipeline) {
|
|
419
|
+
layout_id = static_cast<ComputePipeline &>(object).layout;
|
|
420
|
+
} else {
|
|
421
|
+
layout_id = static_cast<RenderPipeline &>(object).layout;
|
|
422
|
+
}
|
|
423
|
+
std::shared_ptr<Object> object_layout = get_object(layout_id, Kind::PipelineLayout);
|
|
424
|
+
if (object_layout == nullptr) return XT_UNDEFINED;
|
|
425
|
+
PipelineLayout *layout = static_cast<PipelineLayout *>(object_layout.get());
|
|
426
|
+
if (index < 0 || (size_t)index >= layout->layouts.size()) return XT_UNDEFINED;
|
|
427
|
+
return handle_of(layout->layouts[(size_t)index]);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
xt_value pipeline_async_render(Object &object, int argc, xt_value *argv) {
|
|
431
|
+
xt_value created = create_render_pipeline(object, argc, argv);
|
|
432
|
+
return resolved(created);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
xt_value pipeline_async_compute(Object &object, int argc, xt_value *argv) {
|
|
436
|
+
xt_value created = create_compute_pipeline(object, argc, argv);
|
|
437
|
+
return resolved(created);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
} // namespace
|
|
441
|
+
|
|
442
|
+
bool parse_bind_group(Device &device, xt_value descriptor, BindGroup *out, std::string *error) {
|
|
443
|
+
return parse_bind_group_impl(device, descriptor, out, error);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
void register_pipeline_methods() {
|
|
447
|
+
add_getter(Kind::ShaderModule, "label", label_of);
|
|
448
|
+
add_method(Kind::ShaderModule, "getCompilationInfo", shader_compilation_info);
|
|
449
|
+
add_getter(Kind::BindGroupLayout, "label", label_of);
|
|
450
|
+
add_getter(Kind::PipelineLayout, "label", label_of);
|
|
451
|
+
add_getter(Kind::BindGroup, "label", label_of);
|
|
452
|
+
add_getter(Kind::RenderPipeline, "label", label_of);
|
|
453
|
+
add_getter(Kind::ComputePipeline, "label", label_of);
|
|
454
|
+
add_method(Kind::RenderPipeline, "getBindGroupLayout", pipeline_bind_group_layout);
|
|
455
|
+
add_method(Kind::ComputePipeline, "getBindGroupLayout", pipeline_bind_group_layout);
|
|
456
|
+
add_method(Kind::Device, "createRenderPipeline", create_render_pipeline);
|
|
457
|
+
add_method(Kind::Device, "createComputePipeline", create_compute_pipeline);
|
|
458
|
+
add_method(Kind::Device, "createRenderPipelineAsync", pipeline_async_render);
|
|
459
|
+
add_method(Kind::Device, "createComputePipelineAsync", pipeline_async_compute);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
} // namespace webgpu
|
|
463
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WebGPU queue operations.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include <cstring>
|
|
6
|
+
|
|
7
|
+
#include "webgpu_bindings.h"
|
|
8
|
+
|
|
9
|
+
namespace xtgui {
|
|
10
|
+
namespace webgpu {
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
xt_value queue_label(Object &object) {
|
|
14
|
+
return xt_string_from_cstr(object.label.c_str());
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
xt_value queue_submit(Object &object, int argc, xt_value *argv) {
|
|
18
|
+
(void)object;
|
|
19
|
+
xt_value list = arg_object(argc, argv, 0);
|
|
20
|
+
if (!XT_IS_ARRAY(list)) {
|
|
21
|
+
throw_type_error("submit needs an array of command buffers");
|
|
22
|
+
return XT_UNDEFINED;
|
|
23
|
+
}
|
|
24
|
+
const uint32_t count = (uint32_t)xt_to_number(xt_array_length(list));
|
|
25
|
+
for (uint32_t i = 0; i < count; i++) {
|
|
26
|
+
xt_value item = xt_array_get(list, xt_number((double)i));
|
|
27
|
+
const uint32_t id = (uint32_t)xt_to_number(xt_object_get_cstr(item, "__xt_webgpu_id"));
|
|
28
|
+
std::shared_ptr<Object> command = get_object(id);
|
|
29
|
+
if (command == nullptr) continue;
|
|
30
|
+
if (command->kind == Kind::CommandBuffer) {
|
|
31
|
+
auto *buffer = static_cast<CommandBuffer *>(command.get());
|
|
32
|
+
if (buffer->cmd != nullptr) {
|
|
33
|
+
SDL_SubmitGPUCommandBuffer(buffer->cmd);
|
|
34
|
+
buffer->cmd = nullptr;
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (command->kind != Kind::CommandEncoder) continue;
|
|
39
|
+
auto *encoder = static_cast<CommandEncoder *>(command.get());
|
|
40
|
+
if (encoder->finished || encoder->cmd == nullptr) continue;
|
|
41
|
+
encoder->finished = true;
|
|
42
|
+
SDL_SubmitGPUCommandBuffer(encoder->cmd);
|
|
43
|
+
encoder->cmd = nullptr;
|
|
44
|
+
}
|
|
45
|
+
return XT_UNDEFINED;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
xt_value queue_write_buffer(Object &object, int argc, xt_value *argv) {
|
|
49
|
+
xt_value buffer_value = arg_object(argc, argv, 0);
|
|
50
|
+
const uint32_t buffer_id = (uint32_t)xt_to_number(
|
|
51
|
+
xt_object_get_cstr(buffer_value, "__xt_webgpu_id"));
|
|
52
|
+
std::shared_ptr<Object> buffer_object = get_object(buffer_id, Kind::Buffer);
|
|
53
|
+
if (buffer_object == nullptr) {
|
|
54
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
55
|
+
if (device != nullptr) return validation(*device, "writeBuffer: invalid buffer");
|
|
56
|
+
return XT_UNDEFINED;
|
|
57
|
+
}
|
|
58
|
+
Buffer &buffer = static_cast<Buffer &>(*buffer_object);
|
|
59
|
+
const uint64_t offset = arg_u64(argc, argv, 1);
|
|
60
|
+
std::vector<uint8_t> data;
|
|
61
|
+
if (!read_bytes(xt_arg(argc, argv, 2), &data)) {
|
|
62
|
+
throw_type_error("writeBuffer needs an ArrayBuffer or typed array");
|
|
63
|
+
return XT_UNDEFINED;
|
|
64
|
+
}
|
|
65
|
+
const uint64_t data_offset = arg_u64(argc, argv, 3);
|
|
66
|
+
uint64_t size = arg_u64(argc, argv, 4);
|
|
67
|
+
if (size == 0) size = data.size() > data_offset ? data.size() - data_offset : 0;
|
|
68
|
+
if (data_offset + size > data.size() || offset + size > buffer.size) {
|
|
69
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
70
|
+
if (device != nullptr) {
|
|
71
|
+
return validation(*device, "writeBuffer: the write range is out of bounds");
|
|
72
|
+
}
|
|
73
|
+
return XT_UNDEFINED;
|
|
74
|
+
}
|
|
75
|
+
if (buffer.has_shadow) {
|
|
76
|
+
memcpy(buffer.shadow.data() + offset, data.data() + data_offset, (size_t)size);
|
|
77
|
+
}
|
|
78
|
+
if (buffer.has_gpu) {
|
|
79
|
+
/* Upload now; WebGPU's write ordering is equivalent for our single device. */
|
|
80
|
+
SDL_GPUTransferBufferCreateInfo info = {};
|
|
81
|
+
info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
|
|
82
|
+
info.size = (Uint32)size;
|
|
83
|
+
SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(gpu_device(), &info);
|
|
84
|
+
if (transfer != nullptr) {
|
|
85
|
+
void *mapped = SDL_MapGPUTransferBuffer(gpu_device(), transfer, false);
|
|
86
|
+
if (mapped != nullptr) {
|
|
87
|
+
memcpy(mapped, data.data() + data_offset, (size_t)size);
|
|
88
|
+
SDL_UnmapGPUTransferBuffer(gpu_device(), transfer);
|
|
89
|
+
SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(gpu_device());
|
|
90
|
+
SDL_GPUCopyPass *pass = SDL_BeginGPUCopyPass(cmd);
|
|
91
|
+
SDL_GPUTransferBufferLocation source = {};
|
|
92
|
+
source.transfer_buffer = transfer;
|
|
93
|
+
source.offset = 0;
|
|
94
|
+
SDL_GPUBufferRegion region = {};
|
|
95
|
+
region.buffer = buffer.gpu;
|
|
96
|
+
region.offset = (Uint32)offset;
|
|
97
|
+
region.size = (Uint32)size;
|
|
98
|
+
SDL_UploadToGPUBuffer(pass, &source, ®ion, true);
|
|
99
|
+
SDL_EndGPUCopyPass(pass);
|
|
100
|
+
SDL_SubmitGPUCommandBuffer(cmd);
|
|
101
|
+
}
|
|
102
|
+
SDL_ReleaseGPUTransferBuffer(gpu_device(), transfer);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return XT_UNDEFINED;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
xt_value queue_write_texture(Object &object, int argc, xt_value *argv) {
|
|
109
|
+
xt_value destination = arg_object(argc, argv, 0);
|
|
110
|
+
std::vector<uint8_t> data;
|
|
111
|
+
if (!read_bytes(xt_arg(argc, argv, 1), &data)) {
|
|
112
|
+
throw_type_error("writeTexture needs an ArrayBuffer or typed array");
|
|
113
|
+
return XT_UNDEFINED;
|
|
114
|
+
}
|
|
115
|
+
xt_value layout = arg_object(argc, argv, 2);
|
|
116
|
+
xt_value size_value = arg_object(argc, argv, 3);
|
|
117
|
+
if (!XT_IS_OBJECT(destination) || !XT_IS_OBJECT(size_value)) {
|
|
118
|
+
throw_type_error("writeTexture needs a destination and a size");
|
|
119
|
+
return XT_UNDEFINED;
|
|
120
|
+
}
|
|
121
|
+
const uint32_t texture_id = (uint32_t)xt_to_number(
|
|
122
|
+
xt_object_get_cstr(prop_object(destination, "texture"), "__xt_webgpu_id"));
|
|
123
|
+
std::shared_ptr<Object> texture_object = get_object(texture_id, Kind::Texture);
|
|
124
|
+
if (texture_object == nullptr) {
|
|
125
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
126
|
+
if (device != nullptr) return validation(*device, "writeTexture: invalid texture");
|
|
127
|
+
return XT_UNDEFINED;
|
|
128
|
+
}
|
|
129
|
+
Texture &texture = static_cast<Texture &>(*texture_object);
|
|
130
|
+
const uint32_t width = (uint32_t)prop_u64(size_value, "width", 0);
|
|
131
|
+
const uint32_t height = (uint32_t)prop_u64(size_value, "height", 1);
|
|
132
|
+
const uint32_t depth = (uint32_t)prop_u64(size_value, "depthOrArrayLayers", 1);
|
|
133
|
+
xt_value origin = prop_object(destination, "origin");
|
|
134
|
+
const uint32_t x = (uint32_t)prop_u64(origin, "x", 0);
|
|
135
|
+
const uint32_t y = (uint32_t)prop_u64(origin, "y", 0);
|
|
136
|
+
const uint32_t z = (uint32_t)prop_u64(origin, "z", 0);
|
|
137
|
+
const uint32_t mip = (uint32_t)prop_u64(destination, "mipLevel", 0);
|
|
138
|
+
const uint32_t bytes_per_row = (uint32_t)prop_u64(layout, "bytesPerRow", width * 4);
|
|
139
|
+
const uint64_t data_offset = prop_u64(layout, "offset", 0);
|
|
140
|
+
const uint32_t bytes_per_pixel = Convert::bytes_per_pixel(texture.format);
|
|
141
|
+
const uint64_t row_bytes = (uint64_t)width * bytes_per_pixel;
|
|
142
|
+
if (row_bytes > bytes_per_row) {
|
|
143
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
144
|
+
if (device != nullptr) return validation(*device, "writeTexture: bytesPerRow is too small");
|
|
145
|
+
return XT_UNDEFINED;
|
|
146
|
+
}
|
|
147
|
+
const uint64_t needed = data_offset + (uint64_t)bytes_per_row * (height > 1 ? height - 1 : 0) +
|
|
148
|
+
row_bytes * (uint64_t)depth * (height > 1 ? 1 : height);
|
|
149
|
+
if (needed > data.size() + row_bytes) {
|
|
150
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
151
|
+
if (device != nullptr) return validation(*device, "writeTexture: the data is too short");
|
|
152
|
+
return XT_UNDEFINED;
|
|
153
|
+
}
|
|
154
|
+
SDL_GPUTransferBufferCreateInfo info = {};
|
|
155
|
+
info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
|
|
156
|
+
info.size = (Uint32)data.size();
|
|
157
|
+
SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(gpu_device(), &info);
|
|
158
|
+
if (transfer == nullptr) {
|
|
159
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
160
|
+
if (device != nullptr) return validation(*device, "writeTexture: transfer buffer failed");
|
|
161
|
+
return XT_UNDEFINED;
|
|
162
|
+
}
|
|
163
|
+
void *mapped = SDL_MapGPUTransferBuffer(gpu_device(), transfer, false);
|
|
164
|
+
if (mapped != nullptr) {
|
|
165
|
+
memcpy(mapped, data.data(), data.size());
|
|
166
|
+
SDL_UnmapGPUTransferBuffer(gpu_device(), transfer);
|
|
167
|
+
}
|
|
168
|
+
SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(gpu_device());
|
|
169
|
+
SDL_GPUCopyPass *pass = SDL_BeginGPUCopyPass(cmd);
|
|
170
|
+
SDL_GPUTextureTransferInfo source = {};
|
|
171
|
+
source.transfer_buffer = transfer;
|
|
172
|
+
source.offset = (Uint32)data_offset;
|
|
173
|
+
source.pixels_per_row = bytes_per_row / bytes_per_pixel;
|
|
174
|
+
source.rows_per_layer = height;
|
|
175
|
+
SDL_GPUTextureRegion region = {};
|
|
176
|
+
region.texture = texture.gpu;
|
|
177
|
+
region.mip_level = mip;
|
|
178
|
+
region.layer = z;
|
|
179
|
+
region.x = x;
|
|
180
|
+
region.y = y;
|
|
181
|
+
region.z = 0;
|
|
182
|
+
region.w = width;
|
|
183
|
+
region.h = height;
|
|
184
|
+
region.d = depth;
|
|
185
|
+
SDL_UploadToGPUTexture(pass, &source, ®ion, true);
|
|
186
|
+
SDL_EndGPUCopyPass(pass);
|
|
187
|
+
SDL_SubmitGPUCommandBuffer(cmd);
|
|
188
|
+
SDL_ReleaseGPUTransferBuffer(gpu_device(), transfer);
|
|
189
|
+
return XT_UNDEFINED;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
xt_value queue_copy_external_image(Object &object, int argc, xt_value *argv) {
|
|
193
|
+
(void)argc;
|
|
194
|
+
(void)argv;
|
|
195
|
+
std::shared_ptr<Device> device = device_for(object, nullptr);
|
|
196
|
+
if (device != nullptr) {
|
|
197
|
+
return validation(*device,
|
|
198
|
+
"copyExternalImageToTexture: DOM image sources are not supported by the WebGPU backend");
|
|
199
|
+
}
|
|
200
|
+
return XT_UNDEFINED;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
xt_value queue_on_submitted_work_done(Object &object, int argc, xt_value *argv) {
|
|
204
|
+
(void)object;
|
|
205
|
+
(void)argc;
|
|
206
|
+
(void)argv;
|
|
207
|
+
return resolved(XT_UNDEFINED);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
} // namespace
|
|
211
|
+
|
|
212
|
+
void register_queue_methods() {
|
|
213
|
+
add_getter(Kind::Queue, "label", queue_label);
|
|
214
|
+
add_method(Kind::Queue, "submit", queue_submit);
|
|
215
|
+
add_method(Kind::Queue, "writeBuffer", queue_write_buffer);
|
|
216
|
+
add_method(Kind::Queue, "writeTexture", queue_write_texture);
|
|
217
|
+
add_method(Kind::Queue, "copyExternalImageToTexture", queue_copy_external_image);
|
|
218
|
+
add_method(Kind::Queue, "onSubmittedWorkDone", queue_on_submitted_work_done);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
} // namespace webgpu
|
|
222
|
+
} // namespace xtgui
|