xbintsc 0.7.7 → 0.7.9

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 (160) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/cli/help.js +1 -1
  3. package/dist/src/codegen/generator/calls/invocation.js +6 -4
  4. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  5. package/dist/src/codegen/generator/context.d.ts +4 -3
  6. package/dist/src/codegen/generator/context.js +6 -5
  7. package/dist/src/codegen/generator/context.js.map +1 -1
  8. package/dist/src/codegen/generator/expressions/assignment.js +3 -3
  9. package/dist/src/driver/config.d.ts +1 -1
  10. package/dist/src/driver/options.d.ts +1 -1
  11. package/dist/src/driver/version.d.ts +1 -1
  12. package/dist/src/driver/version.js +1 -1
  13. package/dist/src/extensions/gui/features.d.ts +14 -6
  14. package/dist/src/extensions/gui/features.js +34 -6
  15. package/dist/src/extensions/gui/features.js.map +1 -1
  16. package/dist/src/extensions/gui/index.js +3 -3
  17. package/dist/tests/codegen/llvm.test.js +1 -1
  18. package/dist/tests/e2e/gui-audio.test.js +24 -0
  19. package/dist/tests/e2e/gui-audio.test.js.map +1 -1
  20. package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
  21. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
  22. package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
  23. package/dist/tests/e2e/gui-font-face.test.js +99 -7
  24. package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
  25. package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
  26. package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
  27. package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
  28. package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
  29. package/dist/tests/e2e/gui-initial-url.test.js +49 -0
  30. package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
  31. package/dist/tests/e2e/gui-layout.test.js +85 -0
  32. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  33. package/dist/tests/e2e/gui-webgl-depth-init.test.d.ts +13 -0
  34. package/dist/tests/e2e/gui-webgl-depth-init.test.js +144 -0
  35. package/dist/tests/e2e/gui-webgl-depth-init.test.js.map +1 -0
  36. package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
  37. package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
  38. package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
  39. package/dist/tests/e2e/gui-webgl.test.js +49 -0
  40. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  41. package/dist/tests/e2e/gui-webgpu-attachment.test.d.ts +19 -0
  42. package/dist/tests/e2e/gui-webgpu-attachment.test.js +258 -0
  43. package/dist/tests/e2e/gui-webgpu-attachment.test.js.map +1 -0
  44. package/dist/tests/e2e/method-call-cache.test.js +27 -0
  45. package/dist/tests/e2e/method-call-cache.test.js.map +1 -1
  46. package/dist/tests/extensions/gui-features.test.js +27 -2
  47. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  48. package/doc/ai/extensions.md +10 -4
  49. package/doc/ai/zh-CN/extensions.md +8 -3
  50. package/doc/gui/animation.md +1 -1
  51. package/doc/gui/api.md +35 -2
  52. package/doc/gui/build.md +34 -15
  53. package/doc/gui/dom.md +3 -2
  54. package/doc/gui/known-issues.md +1 -1
  55. package/doc/gui/layout.md +9 -3
  56. package/doc/gui/rendering.md +45 -8
  57. package/doc/gui/runtime.md +13 -0
  58. package/doc/gui/webgl.md +29 -3
  59. package/doc/gui/webgpu.md +20 -0
  60. package/doc/gui.md +9 -4
  61. package/doc/implemented/codegen.md +4 -3
  62. package/doc/zh-CN/gui/animation.md +1 -1
  63. package/doc/zh-CN/gui/api.md +25 -2
  64. package/doc/zh-CN/gui/build.md +20 -7
  65. package/doc/zh-CN/gui/dom.md +3 -2
  66. package/doc/zh-CN/gui/known-issues.md +1 -1
  67. package/doc/zh-CN/gui/layout.md +7 -4
  68. package/doc/zh-CN/gui/rendering.md +33 -8
  69. package/doc/zh-CN/gui/runtime.md +9 -0
  70. package/doc/zh-CN/gui/webgl.md +7 -4
  71. package/doc/zh-CN/gui/webgpu.md +18 -0
  72. package/doc/zh-CN/gui.md +10 -5
  73. package/doc/zh-CN/implemented/codegen.md +4 -3
  74. package/package.json +7 -7
  75. package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
  76. package/runtime/ext_gui/audio_mixer.cpp +84 -163
  77. package/runtime/ext_gui/audio_mixer.h +59 -3
  78. package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
  79. package/runtime/ext_gui/audio_param_apply.cpp +160 -0
  80. package/runtime/ext_gui/browser.cpp +6 -19
  81. package/runtime/ext_gui/browser_location.cpp +78 -0
  82. package/runtime/ext_gui/css.cpp +1 -0
  83. package/runtime/ext_gui/css.h +4 -0
  84. package/runtime/ext_gui/css_sheet.cpp +4 -2
  85. package/runtime/ext_gui/document_animation.cpp +7 -0
  86. package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
  87. package/runtime/ext_gui/dom_api_webgl.cpp +39 -2
  88. package/runtime/ext_gui/dom_api_webgl_extensions.cpp +134 -0
  89. package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
  90. package/runtime/ext_gui/dom_api_webgl_shader.cpp +28 -4
  91. package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -103
  92. package/runtime/ext_gui/gui.cpp +100 -1
  93. package/runtime/ext_gui/gui_events.cpp +8 -2
  94. package/runtime/ext_gui/layout_flex.cpp +5 -2
  95. package/runtime/ext_gui/layout_flow.cpp +24 -1
  96. package/runtime/ext_gui/layout_flow_internal.h +11 -2
  97. package/runtime/ext_gui/media_query.cpp +3 -0
  98. package/runtime/ext_gui/perf.cpp +18 -6
  99. package/runtime/ext_gui/perf.h +37 -0
  100. package/runtime/ext_gui/style.cpp +28 -10
  101. package/runtime/ext_gui/style.h +10 -1
  102. package/runtime/ext_gui/style_length.cpp +4 -0
  103. package/runtime/ext_gui/style_print.cpp +7 -1
  104. package/runtime/ext_gui/style_values.cpp +6 -5
  105. package/runtime/ext_gui/text.cpp +4 -0
  106. package/runtime/ext_gui/text_font_bytes.cpp +117 -0
  107. package/runtime/ext_gui/text_font_bytes.h +65 -0
  108. package/runtime/ext_gui/text_font_db.cpp +5 -0
  109. package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
  110. package/runtime/ext_gui/text_font_sfnt.h +5 -2
  111. package/runtime/ext_gui/text_hb.cpp +34 -5
  112. package/runtime/ext_gui/text_stb.cpp +7 -30
  113. package/runtime/ext_gui/text_webfont.cpp +217 -0
  114. package/runtime/ext_gui/warnings.cpp +26 -0
  115. package/runtime/ext_gui/warnings.h +8 -0
  116. package/runtime/ext_gui/webfont_bridge.c +23 -0
  117. package/runtime/ext_gui/webgl.h +36 -39
  118. package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
  119. package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
  120. package/runtime/ext_gui/webgl_context.cpp +2 -0
  121. package/runtime/ext_gui/webgl_draw.cpp +72 -12
  122. package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
  123. package/runtime/ext_gui/webgl_draw_state.h +13 -2
  124. package/runtime/ext_gui/webgl_framebuffer.cpp +8 -0
  125. package/runtime/ext_gui/webgl_objects.h +19 -0
  126. package/runtime/ext_gui/webgl_program.h +64 -0
  127. package/runtime/ext_gui/webgl_state.cpp +7 -0
  128. package/runtime/ext_gui/webgl_targets.cpp +31 -7
  129. package/runtime/ext_gui/webgl_uniform.cpp +78 -0
  130. package/runtime/ext_gui/webgpu.h +10 -0
  131. package/runtime/ext_gui/webgpu_buffer.cpp +3 -1
  132. package/runtime/ext_gui/webgpu_canvas.cpp +1 -0
  133. package/runtime/ext_gui/webgpu_command.cpp +51 -11
  134. package/runtime/ext_gui/webgpu_convert.cpp +7 -2
  135. package/runtime/ext_gui/webgpu_pass.cpp +12 -0
  136. package/runtime/ext_gui/webgpu_queue.cpp +3 -0
  137. package/runtime/ext_gui/webgpu_resources.cpp +1 -0
  138. package/runtime/ext_gui/window.cpp +6 -0
  139. package/runtime/rt_object.h +6 -0
  140. package/runtime/rt_profile.h +6 -0
  141. package/runtime/xt_containers/access.inc +2 -47
  142. package/runtime/xt_containers/ic_get.inc +70 -0
  143. package/runtime/xt_containers/method_cache.inc +42 -11
  144. package/runtime/xt_containers/objects.inc +12 -1
  145. package/runtime/xt_containers/shapes.inc +78 -0
  146. package/runtime/xt_containers.c +1 -0
  147. package/runtime/xt_profile.c +3 -0
  148. package/scripts/build-gui.ts +43 -49
  149. package/scripts/gui-engine-build.ts +58 -0
  150. package/scripts/gui-vendor-source.ts +53 -0
  151. package/scripts/gui-webfont-build.ts +138 -0
  152. package/src/cli/help.ts +1 -1
  153. package/src/codegen/generator/calls/invocation.ts +6 -4
  154. package/src/codegen/generator/context.ts +6 -6
  155. package/src/codegen/generator/expressions/assignment.ts +3 -3
  156. package/src/driver/config.ts +1 -1
  157. package/src/driver/options.ts +1 -1
  158. package/src/driver/version.ts +1 -1
  159. package/src/extensions/gui/features.ts +34 -6
  160. package/src/extensions/gui/index.ts +3 -3
@@ -20,6 +20,7 @@
20
20
  #include <vector>
21
21
 
22
22
  #include "gui_internal.h"
23
+ #include "perf.h"
23
24
 
24
25
  namespace xtgui {
25
26
  namespace {
@@ -239,14 +240,17 @@ void WebGLContext::clear_draw_cache() {
239
240
  const WebGLDrawSetup *WebGLContext::prepare_draw(const WebGLDrawKey &key) {
240
241
  auto found = draw_setups_.find(key);
241
242
  if (found != draw_setups_.end()) return &found->second;
243
+ g_perf_frame.webglSetupMisses += 1;
242
244
  WebGLProgram *program_ = program(key.program);
243
245
  if (program_ == nullptr) {
244
246
  set_error(GL_INVALID_OPERATION);
245
247
  return nullptr;
246
248
  }
247
249
  /* A page that cycles through many states would otherwise grow the table
248
- * without bound; clearing only costs one rebuild per live state. */
249
- if (draw_setups_.size() >= 256) clear_draw_cache();
250
+ * without bound; clearing only costs one rebuild per live state. The keys are
251
+ * stable across frames now (VAO identity + revision), so this is a safety
252
+ * valve rather than a per-frame event. */
253
+ if (draw_setups_.size() >= 4096) clear_draw_cache();
250
254
 
251
255
  WebGLDrawSetup setup;
252
256
  setup.attributes.reserve(program_->draw_attribs.size());
@@ -342,6 +346,8 @@ const WebGLDrawSetup *WebGLContext::prepare_draw(const WebGLDrawKey &key) {
342
346
  WebGLDrawKey pipeline_key = key;
343
347
  pipeline_key.gpu_version = 0;
344
348
  pipeline_key.element_buffer = 0;
349
+ pipeline_key.attribs_vao = 0;
350
+ pipeline_key.attribs_revision = 0;
345
351
  pipeline_key.attribs_version = vertex_layout_hash(setup.attributes, setup.buffers);
346
352
  pipeline_key.framebuffer_version = 0;
347
353
  auto cached = program_->pipeline_cache.find(pipeline_key);
@@ -70,6 +70,14 @@ struct WebGLDrawKey {
70
70
  int mode = 0; // GL primitive mode
71
71
  int index_size = 0; // 0 = drawArrays, otherwise index element bytes
72
72
  uint32_t element_buffer = 0; // bound GL_ELEMENT_ARRAY_BUFFER, 0 = none
73
+ /** Identity of the vertex layout: the bound VAO plus that VAO's revision
74
+ * (bumped only when its attribute state actually changes), so the prepared
75
+ * draw survives across frames. For the default VAO (`attribs_vao == 0`) the
76
+ * revision is the context's global attrib counter. */
77
+ uint32_t attribs_vao = 0;
78
+ uint64_t attribs_revision = 0;
79
+ /** Content hash of the resolved vertex layout. Only the pipeline cache uses
80
+ * it (a pipeline depends on the layout, not on which VAO supplies it). */
73
81
  uint64_t attribs_version = 0;
74
82
  uint64_t gpu_version = 0;
75
83
  uint64_t framebuffer_version = 0;
@@ -78,8 +86,9 @@ struct WebGLDrawKey {
78
86
 
79
87
  bool operator==(const WebGLDrawKey &other) const {
80
88
  return program == other.program && mode == other.mode && index_size == other.index_size &&
81
- element_buffer == other.element_buffer && attribs_version == other.attribs_version &&
82
- gpu_version == other.gpu_version &&
89
+ element_buffer == other.element_buffer && attribs_vao == other.attribs_vao &&
90
+ attribs_revision == other.attribs_revision &&
91
+ attribs_version == other.attribs_version && gpu_version == other.gpu_version &&
83
92
  framebuffer_version == other.framebuffer_version && state_hash == other.state_hash &&
84
93
  state_hash2 == other.state_hash2;
85
94
  }
@@ -97,6 +106,8 @@ struct WebGLDrawKeyHash {
97
106
  mix((uint64_t)(uint32_t)key.mode);
98
107
  mix((uint64_t)(uint32_t)key.index_size);
99
108
  mix(key.element_buffer);
109
+ mix(key.attribs_vao);
110
+ mix(key.attribs_revision);
100
111
  mix(key.attribs_version);
101
112
  mix(key.gpu_version);
102
113
  mix(key.framebuffer_version);
@@ -84,6 +84,10 @@ bool WebGLContext::framebuffer_texture(uint32_t framebuffer_id, uint32_t texture
84
84
  SDL_ReleaseGPUTexture(guidev::g_device, record_framebuffer->depth);
85
85
  record_framebuffer->depth = nullptr;
86
86
  }
87
+ /* A never-allocated depth texture starts at undefined contents; its first
88
+ * pass must clear it to WebGL's 1. An existing texture keeps its contents,
89
+ * like a browser. */
90
+ const bool fresh_depth = record != nullptr && record->gpu == nullptr;
87
91
  if (record != nullptr) {
88
92
  record->as_depth_target = true;
89
93
  ensure_texture_gpu(record, true);
@@ -94,6 +98,7 @@ bool WebGLContext::framebuffer_texture(uint32_t framebuffer_id, uint32_t texture
94
98
  record_framebuffer->height = record->height;
95
99
  }
96
100
  }
101
+ record_framebuffer->depth_clear_pending = fresh_depth;
97
102
  return true;
98
103
  }
99
104
  const int color_index = attachment - GL_COLOR_ATTACHMENT0;
@@ -330,6 +335,9 @@ bool WebGLContext::framebuffer_renderbuffer(uint32_t framebuffer_id, int attachm
330
335
  framebuffer_->width = record->width;
331
336
  framebuffer_->height = record->height;
332
337
  }
338
+ /* A depth renderbuffer has undefined contents until cleared; WebGL starts
339
+ * it at 1, so the first pass that uses it clears depth. */
340
+ framebuffer_->depth_clear_pending = record != nullptr && record->depth;
333
341
  return true;
334
342
  }
335
343
  const int index = attachment - GL_COLOR_ATTACHMENT0;
@@ -26,6 +26,14 @@ constexpr int kWebGLMaxAttribs = 16;
26
26
  constexpr int kWebGLMaxTextureUnits = 16;
27
27
  constexpr int kWebGLMaxDrawBuffers = 4;
28
28
 
29
+ /** `KHR_parallel_shader_compile`'s `COMPLETION_STATUS_KHR`: the pname
30
+ * `getShaderParameter` and `getProgramParameter` accept to ask whether the
31
+ * translation finished. Shader compilation and linking are synchronous in this
32
+ * engine (see `WebGLContext::compile_shader`/`link_program`), so the answer is
33
+ * always `true` and the extension is only advertised to stop capability probes
34
+ * from warning. */
35
+ constexpr int kWebGLCompletionStatusKHR = 0x91B1;
36
+
29
37
  struct WebGLBuffer {
30
38
  uint32_t id = 0;
31
39
  std::vector<uint8_t> data;
@@ -108,6 +116,13 @@ struct WebGLFramebuffer {
108
116
  int width = 0;
109
117
  int height = 0;
110
118
  SDL_GPUTexture *depth = nullptr; // owned when no renderbuffer/texture supplies one
119
+ /** The depth attachment has no WebGL-visible contents yet. A WebGL depth
120
+ * buffer starts cleared to 1, and a browser therefore renders depth-tested
121
+ * geometry into a fresh render target without the page clearing it first —
122
+ * three.js's PMREMGenerator relies on exactly that. The GPU attachment's
123
+ * contents are undefined (often 0), so the first pass that uses this
124
+ * framebuffer must clear depth even when the page did not ask for a clear. */
125
+ bool depth_clear_pending = false;
111
126
  };
112
127
 
113
128
  /** Per-attribute array state. */
@@ -135,6 +150,10 @@ struct WebGLVertexArray {
135
150
  uint32_t id = 0;
136
151
  WebGLAttribState attribs[kWebGLMaxAttribs];
137
152
  uint32_t element_buffer = 0;
153
+ /** Bumped whenever an attribute of this VAO changes while it is bound. The
154
+ * prepared-draw cache keys on it, so binding the same VAO on a later frame is
155
+ * a cache hit even though the context's global attrib counter keeps moving. */
156
+ uint64_t revision = 0;
138
157
  };
139
158
 
140
159
  /** A WebGL 2 sampler object, bound to a texture unit with `bindSampler`. */
@@ -0,0 +1,64 @@
1
+ /*
2
+ * xbintsc GUI engine — linked-program records.
3
+ *
4
+ * Split out of `webgl.h` to keep that header within the repository's file-length
5
+ * budget. A program owns its reflected uniforms, the std140 block the page's
6
+ * `uniform*` calls fill, and the pipeline cache keyed by draw state.
7
+ */
8
+ #ifndef XT_GUI_WEBGL_PROGRAM_H
9
+ #define XT_GUI_WEBGL_PROGRAM_H
10
+
11
+ #include <cstdint>
12
+ #include <string>
13
+ #include <unordered_map>
14
+ #include <vector>
15
+
16
+ #include <SDL3/SDL.h>
17
+
18
+ #include "webgl_draw_state.h"
19
+ #include "webgl_glsl.h"
20
+ #include "webgl_uniform_block.h"
21
+
22
+ namespace xtgui {
23
+
24
+ /** One active uniform of a linked program, with its std140 home. */
25
+ struct WebGLUniform {
26
+ std::string name;
27
+ GlslBlockMember layout;
28
+ bool sampler = false;
29
+ /** `sampler2DShadow`/`samplerCubeShadow`/`sampler2DArrayShadow`: SDL_GPU
30
+ * binds a comparison sampler, and the GLSL `texture()` call performs the
31
+ * hardware depth compare. */
32
+ bool compare = false;
33
+ int unit = -1;
34
+ };
35
+
36
+ struct WebGLProgram {
37
+ uint32_t id = 0;
38
+ uint32_t vs = 0;
39
+ uint32_t fs = 0;
40
+ bool linked = false;
41
+ std::string log;
42
+ std::vector<WebGLUniform> uniforms;
43
+ std::vector<WebGLUniformBlock> blocks;
44
+ std::vector<GlslUniformDecl> attribs;
45
+ std::vector<WebGLDrawAttribPlan> draw_attribs;
46
+ std::unordered_map<std::string, int> attrib_locations; // name -> location
47
+ std::unordered_map<std::string, int> attrib_bindings; // bindAttribLocation overrides
48
+ std::unordered_map<std::string, int> uniform_index; // name -> uniforms[] index
49
+ std::vector<int> vs_sampler_uniforms; // local binding -> uniforms[] index
50
+ std::vector<int> fs_sampler_uniforms;
51
+ std::vector<uint8_t> block; // std140 uniform data
52
+ int block_size = 0;
53
+ bool block_dirty = true;
54
+ bool vs_has_block = false;
55
+ bool fs_has_block = false;
56
+ SDL_GPUShader *vs_shader = nullptr;
57
+ SDL_GPUShader *fs_shader = nullptr;
58
+ std::vector<SDL_GPUGraphicsPipeline *> pipelines;
59
+ std::unordered_map<WebGLDrawKey, SDL_GPUGraphicsPipeline *, WebGLDrawKeyHash> pipeline_cache;
60
+ };
61
+
62
+ } // namespace xtgui
63
+
64
+ #endif /* XT_GUI_WEBGL_PROGRAM_H */
@@ -162,6 +162,13 @@ void WebGLContext::scissor(int x, int y, int w, int h) {
162
162
  scissor_box_[3] = h;
163
163
  }
164
164
 
165
+ void WebGLContext::depth_range(float near, float far) {
166
+ /* GL clamps both ends into [0, 1] and keeps a reversed range (`near > far`)
167
+ * as given; only the mapping changes, so no validation error is raised. */
168
+ depth_range_near_ = std::clamp(near, 0.0f, 1.0f);
169
+ depth_range_far_ = std::clamp(far, 0.0f, 1.0f);
170
+ }
171
+
165
172
  void WebGLContext::clear(int mask) {
166
173
  if (guidev::g_device == nullptr) return;
167
174
  const bool color_bit = (mask & GL_COLOR_BUFFER_BIT) != 0;
@@ -168,6 +168,7 @@ SDL_GPUTexture *WebGLContext::framebuffer_depth(uint32_t id) {
168
168
  * the render pass is invalid. */
169
169
  info.sample_count = webgl_sample_count(framebuffer_samples(id));
170
170
  framebuffer->depth = SDL_CreateGPUTexture(guidev::g_device, &info);
171
+ if (framebuffer->depth != nullptr) framebuffer->depth_clear_pending = true;
171
172
  }
172
173
  return framebuffer->depth;
173
174
  }
@@ -341,15 +342,27 @@ void WebGLContext::end_pass() {
341
342
 
342
343
  void WebGLContext::begin_pass(bool clear_color, bool clear_depth, bool clear_stencil) {
343
344
  if (guidev::g_device == nullptr) return;
345
+ /* A framebuffer whose depth attachment has never been used needs a depth
346
+ * clear even if the page asked for none; see
347
+ * `WebGLFramebuffer::depth_clear_pending`. */
348
+ auto depth_pending = [this]() {
349
+ if (bound_framebuffer_ == 0) return default_depth_clear_pending_;
350
+ WebGLFramebuffer *framebuffer_ = framebuffer(bound_framebuffer_);
351
+ return framebuffer_ != nullptr && framebuffer_->depth_clear_pending;
352
+ };
344
353
  if (pass_ != nullptr && pass_targets_default_ == (bound_framebuffer_ == 0) &&
345
354
  pass_framebuffer_ == bound_framebuffer_ && !clear_color && !clear_depth &&
346
- !clear_stencil) {
355
+ !clear_stencil && !depth_pending()) {
347
356
  return;
348
357
  }
349
358
  end_pass();
350
359
  ensure_command();
351
360
  if (cmd_ == nullptr) return;
352
361
  SDL_GPUTexture *depth = target_depth();
362
+ /* `target_depth` may have just created the auto depth buffer, which sets the
363
+ * pending flag; read it after that call. */
364
+ const bool pending_depth_clear = depth != nullptr && depth_pending();
365
+ const bool clear_depth_now = clear_depth || pending_depth_clear;
353
366
  SDL_GPUColorTargetInfo color_infos[kWebGLMaxDrawBuffers];
354
367
  int color_count = 0;
355
368
  int pass_samples = 1;
@@ -412,13 +425,16 @@ void WebGLContext::begin_pass(bool clear_color, bool clear_depth, bool clear_ste
412
425
  SDL_GPUDepthStencilTargetInfo depth_info;
413
426
  memset(&depth_info, 0, sizeof(depth_info));
414
427
  depth_info.texture = depth;
415
- depth_info.clear_depth = clear_depth_;
416
- depth_info.load_op = (clear_depth && depth != nullptr) ? SDL_GPU_LOADOP_CLEAR
417
- : SDL_GPU_LOADOP_LOAD;
428
+ /* A fresh depth attachment is cleared to WebGL's initial value (1, stencil
429
+ * 0) even when the page renders without asking for a clear. */
430
+ depth_info.clear_depth = clear_depth ? clear_depth_ : 1.0f;
431
+ depth_info.load_op = (clear_depth_now && depth != nullptr) ? SDL_GPU_LOADOP_CLEAR
432
+ : SDL_GPU_LOADOP_LOAD;
418
433
  depth_info.store_op = SDL_GPU_STOREOP_STORE;
419
- depth_info.clear_stencil = (Uint8)(clear_stencil_ & 0xFF);
420
- depth_info.stencil_load_op = (clear_stencil && depth != nullptr) ? SDL_GPU_LOADOP_CLEAR
421
- : SDL_GPU_LOADOP_LOAD;
434
+ depth_info.clear_stencil = (Uint8)(clear_stencil ? clear_stencil_ : 0);
435
+ depth_info.stencil_load_op = ((clear_stencil || pending_depth_clear) && depth != nullptr)
436
+ ? SDL_GPU_LOADOP_CLEAR
437
+ : SDL_GPU_LOADOP_LOAD;
422
438
  depth_info.stencil_store_op = SDL_GPU_STOREOP_STORE;
423
439
 
424
440
  pass_ = SDL_BeginGPURenderPass(cmd_, color_infos, (Uint32)color_count,
@@ -431,6 +447,14 @@ void WebGLContext::begin_pass(bool clear_color, bool clear_depth, bool clear_ste
431
447
  set_error(GL_INVALID_OPERATION);
432
448
  return;
433
449
  }
450
+ if (pending_depth_clear) {
451
+ /* The attachment now holds defined contents; later passes may LOAD it. */
452
+ if (bound_framebuffer_ == 0) {
453
+ default_depth_clear_pending_ = false;
454
+ } else if (WebGLFramebuffer *framebuffer_ = framebuffer(bound_framebuffer_)) {
455
+ framebuffer_->depth_clear_pending = false;
456
+ }
457
+ }
434
458
  g_perf_frame.webglPasses += 1;
435
459
  pass_serial_ += 1;
436
460
  pass_targets_default_ = bound_framebuffer_ == 0;
@@ -12,6 +12,7 @@
12
12
  #include <vector>
13
13
 
14
14
  #include "webgl_program_internal.h"
15
+ #include "perf.h"
15
16
 
16
17
  namespace xtgui {
17
18
  namespace webgl_detail {
@@ -33,6 +34,7 @@ int member_bytes(const GlslBlockMember &member) {
33
34
 
34
35
  void WebGLContext::uniform_floats(uint32_t id, int location, int components, const double *values,
35
36
  int count) {
37
+ PerfScopeActive perf_scope(&g_perf_frame_ms.webgl_uniform);
36
38
  WebGLProgram *record = program(id);
37
39
  if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
38
40
  set_error(GL_INVALID_OPERATION);
@@ -73,8 +75,58 @@ void WebGLContext::uniform_floats(uint32_t id, int location, int components, con
73
75
  record->block_dirty = true;
74
76
  }
75
77
 
78
+ /** `Float32Array`-backed variant of `uniform_floats`: the caller already has
79
+ * the elements as single-precision floats, so this skips the `double` staging
80
+ * vector (and the per-element `xt_to_number`) the plain-array path needs. */
81
+ void WebGLContext::uniform_floats_raw(uint32_t id, int location, int components,
82
+ const float *values, int count) {
83
+ PerfScopeActive perf_scope(&g_perf_frame_ms.webgl_uniform);
84
+ WebGLProgram *record = program(id);
85
+ if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
86
+ set_error(GL_INVALID_OPERATION);
87
+ return;
88
+ }
89
+ WebGLUniform &uniform = record->uniforms[location];
90
+ if (uniform.sampler) {
91
+ uniform.unit = (int)values[0];
92
+ return;
93
+ }
94
+ const GlslBlockMember &member = uniform.layout;
95
+ if (record->block_size <= 0 ||
96
+ member.offset + webgl_detail::member_bytes(member) > record->block_size) {
97
+ return;
98
+ }
99
+ if (count <= 0) count = 1;
100
+ if (member.is_matrix) {
101
+ for (int element = 0; element < count; element++) {
102
+ const int base = member.offset + element * member.array_stride;
103
+ for (int column = 0; column < member.columns; column++) {
104
+ for (int row = 0; row < member.rows; row++) {
105
+ const float value = values[(element * member.columns + column) * member.rows + row];
106
+ const int at = base + column * member.matrix_stride + row * 4;
107
+ if (at + 4 <= (int)record->block.size()) memcpy(&record->block[at], &value, 4);
108
+ }
109
+ }
110
+ }
111
+ record->block_dirty = true;
112
+ return;
113
+ }
114
+ const int components_to_write = std::min(components, member.components);
115
+ for (int element = 0; element < count; element++) {
116
+ const int base = member.offset + element * (member.array_stride > 0 ? member.array_stride
117
+ : member.components * 4);
118
+ for (int component = 0; component < components_to_write; component++) {
119
+ const float value = values[(size_t)element * components + component];
120
+ const int at = base + component * 4;
121
+ if (at + 4 <= (int)record->block.size()) memcpy(&record->block[at], &value, 4);
122
+ }
123
+ }
124
+ record->block_dirty = true;
125
+ }
126
+
76
127
  void WebGLContext::uniform_ints(uint32_t id, int location, int components, const int *values,
77
128
  int count) {
129
+ PerfScopeActive perf_scope(&g_perf_frame_ms.webgl_uniform);
78
130
  WebGLProgram *record = program(id);
79
131
  if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
80
132
  set_error(GL_INVALID_OPERATION);
@@ -103,6 +155,7 @@ void WebGLContext::uniform_ints(uint32_t id, int location, int components, const
103
155
  }
104
156
 
105
157
  void WebGLContext::uniform_matrix(uint32_t id, int location, int dimensions, const double *values) {
158
+ PerfScopeActive perf_scope(&g_perf_frame_ms.webgl_uniform);
106
159
  WebGLProgram *record = program(id);
107
160
  if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
108
161
  set_error(GL_INVALID_OPERATION);
@@ -125,6 +178,31 @@ void WebGLContext::uniform_matrix(uint32_t id, int location, int dimensions, con
125
178
  record->block_dirty = true;
126
179
  }
127
180
 
181
+ void WebGLContext::uniform_matrix_raw(uint32_t id, int location, int dimensions,
182
+ const float *values) {
183
+ PerfScopeActive perf_scope(&g_perf_frame_ms.webgl_uniform);
184
+ WebGLProgram *record = program(id);
185
+ if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
186
+ set_error(GL_INVALID_OPERATION);
187
+ return;
188
+ }
189
+ WebGLUniform &uniform = record->uniforms[location];
190
+ const GlslBlockMember &member = uniform.layout;
191
+ if (!member.is_matrix) {
192
+ uniform_floats_raw(id, location, dimensions * dimensions, values, 1);
193
+ return;
194
+ }
195
+ const int base = member.offset;
196
+ for (int column = 0; column < member.columns; column++) {
197
+ for (int row = 0; row < member.rows; row++) {
198
+ const float value = values[(size_t)column * member.rows + row];
199
+ const int at = base + column * member.matrix_stride + row * 4;
200
+ if (at + 4 <= (int)record->block.size()) memcpy(&record->block[at], &value, 4);
201
+ }
202
+ }
203
+ record->block_dirty = true;
204
+ }
205
+
128
206
  bool WebGLContext::uniform_values(uint32_t id, int location, std::vector<double> *out) {
129
207
  WebGLProgram *record = program(id);
130
208
  if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) return false;
@@ -100,6 +100,10 @@ struct Buffer : Object {
100
100
  struct Texture : Object {
101
101
  SDL_GPUTexture *gpu = nullptr;
102
102
  SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
103
+ /** The WebGPU format string the page asked for. Promise formats such as
104
+ * `depth24plus-stencil8` do not round-trip through the SDL format, and
105
+ * `texture.format` must keep reporting the name the page used. */
106
+ std::string format_name;
103
107
  uint32_t width = 0;
104
108
  uint32_t height = 0;
105
109
  uint32_t depth = 1;
@@ -109,6 +113,12 @@ struct Texture : Object {
109
113
  std::string dimension = "2d";
110
114
  /** Non-zero for a `<canvas>` backbuffer: the compositor samples this. */
111
115
  uint64_t canvas_id = 0;
116
+ /** WebGPU conceptually initialises every resource to zero. A texture that no
117
+ * pass or copy has written yet therefore reads as zeros, and the first pass
118
+ * that LOADs it clears it in `beginRenderPass` (depth 0, stencil 0,
119
+ * transparent black) instead of exposing uninitialised GPU memory. Tracked
120
+ * for the whole texture: the engine ignores view subresources. */
121
+ bool initialized = false;
112
122
  bool external = false;
113
123
  bool destroyed = false;
114
124
  };
@@ -207,7 +207,9 @@ xt_value texture_dimension(Object &object) {
207
207
  }
208
208
 
209
209
  xt_value texture_format(Object &object) {
210
- return xt_string_from_cstr(Convert::texture_format_name(static_cast<Texture &>(object).format));
210
+ const Texture &texture = static_cast<Texture &>(object);
211
+ if (!texture.format_name.empty()) return xt_string_from_cstr(texture.format_name.c_str());
212
+ return xt_string_from_cstr(Convert::texture_format_name(texture.format));
211
213
  }
212
214
 
213
215
  xt_value texture_usage(Object &object) {
@@ -73,6 +73,7 @@ uint32_t ensure_backbuffer(CanvasContext &context) {
73
73
  texture->device_id = context.device;
74
74
  texture->gpu = gpu;
75
75
  texture->format = context.format;
76
+ texture->format_name = context.format_name;
76
77
  texture->width = context.width;
77
78
  texture->height = context.height;
78
79
  texture->depth = 1;
@@ -186,11 +186,12 @@ xt_value begin_render_pass(Object &object, int argc, xt_value *argv) {
186
186
  xt_value view = prop_object(item, "view");
187
187
  const uint32_t view_id = (uint32_t)value_id(view);
188
188
  std::shared_ptr<Object> view_object = get_object(view_id, Kind::TextureView);
189
+ Texture *native = nullptr;
189
190
  if (view_object != nullptr) {
190
191
  std::shared_ptr<Object> texture_object =
191
192
  get_object(static_cast<TextureView *>(view_object.get())->texture, Kind::Texture);
192
193
  if (texture_object != nullptr) {
193
- Texture *native = static_cast<Texture *>(texture_object.get());
194
+ native = static_cast<Texture *>(texture_object.get());
194
195
  target.texture = native->gpu;
195
196
  if (target_width == 0) {
196
197
  target_width = native->width;
@@ -203,8 +204,18 @@ xt_value begin_render_pass(Object &object, int argc, xt_value *argv) {
203
204
  target.clear_color.g = (float)prop_number(clear, "g", 0.0);
204
205
  target.clear_color.b = (float)prop_number(clear, "b", 0.0);
205
206
  target.clear_color.a = (float)prop_number(clear, "a", 1.0);
206
- target.load_op = prop_string(item, "loadOp") == "load" ? SDL_GPU_LOADOP_LOAD
207
- : SDL_GPU_LOADOP_CLEAR;
207
+ bool load = prop_string(item, "loadOp") == "load";
208
+ /* WebGPU initialises every resource to zero, so the first pass that
209
+ * LOADs a colour texture nobody has written sees transparent black,
210
+ * not whatever the driver left in the allocation. */
211
+ if (load && native != nullptr && !native->initialized) {
212
+ load = false;
213
+ target.clear_color.r = 0.0f;
214
+ target.clear_color.g = 0.0f;
215
+ target.clear_color.b = 0.0f;
216
+ target.clear_color.a = 0.0f;
217
+ }
218
+ target.load_op = load ? SDL_GPU_LOADOP_LOAD : SDL_GPU_LOADOP_CLEAR;
208
219
  target.store_op = prop_string(item, "storeOp") == "discard" ? SDL_GPU_STOREOP_DONT_CARE
209
220
  : SDL_GPU_STOREOP_STORE;
210
221
  xt_value resolve = prop_object(item, "resolveTarget");
@@ -220,7 +231,13 @@ xt_value begin_render_pass(Object &object, int argc, xt_value *argv) {
220
231
  }
221
232
  }
222
233
  }
223
- target.cycle = true;
234
+ /* The pass defines the texture's contents from now on: it either clears
235
+ * it or loads contents that were already defined. */
236
+ if (native != nullptr) native->initialized = true;
237
+ /* SDL discards a cycled texture's contents (it rotates to another
238
+ * internal allocation), which is only legal when no load op is LOAD —
239
+ * cycling a LOAD target would silently throw the previous pass away. */
240
+ target.cycle = !load;
224
241
  targets.push_back(target);
225
242
  }
226
243
  }
@@ -230,24 +247,43 @@ xt_value begin_render_pass(Object &object, int argc, xt_value *argv) {
230
247
  if (XT_IS_OBJECT(depth_value)) {
231
248
  xt_value view = prop_object(depth_value, "view");
232
249
  std::shared_ptr<Object> view_object = get_object((uint32_t)value_id(view), Kind::TextureView);
250
+ Texture *native = nullptr;
233
251
  if (view_object != nullptr) {
234
252
  std::shared_ptr<Object> texture_object =
235
253
  get_object(static_cast<TextureView *>(view_object.get())->texture, Kind::Texture);
236
254
  if (texture_object != nullptr) {
237
- depth.texture = static_cast<Texture *>(texture_object.get())->gpu;
255
+ native = static_cast<Texture *>(texture_object.get());
256
+ depth.texture = native->gpu;
238
257
  has_depth = true;
239
258
  }
240
259
  }
241
260
  depth.clear_depth = (float)prop_number(depth_value, "depthClearValue", 1.0);
242
- depth.load_op = prop_string(depth_value, "depthLoadOp") == "load" ? SDL_GPU_LOADOP_LOAD
243
- : SDL_GPU_LOADOP_CLEAR;
261
+ depth.clear_stencil = (Uint8)prop_u64(depth_value, "stencilClearValue", 0);
262
+ bool depth_load = prop_string(depth_value, "depthLoadOp") == "load";
263
+ bool stencil_load = prop_string(depth_value, "stencilLoadOp") == "load";
264
+ /* WebGPU zero-initialises resources: a first pass that LOADs a depth or
265
+ * stencil texture must see 0 (not the driver's leftovers), because zero is
266
+ * what the specification's initial contents are. */
267
+ const bool fresh = native != nullptr && !native->initialized;
268
+ if (depth_load && fresh) {
269
+ depth_load = false;
270
+ depth.clear_depth = 0.0f;
271
+ }
272
+ if (stencil_load && fresh) {
273
+ stencil_load = false;
274
+ depth.clear_stencil = 0;
275
+ }
276
+ depth.load_op = depth_load ? SDL_GPU_LOADOP_LOAD : SDL_GPU_LOADOP_CLEAR;
277
+ depth.stencil_load_op = stencil_load ? SDL_GPU_LOADOP_LOAD : SDL_GPU_LOADOP_CLEAR;
244
278
  depth.store_op = prop_string(depth_value, "depthStoreOp") == "discard"
245
279
  ? SDL_GPU_STOREOP_DONT_CARE
246
280
  : SDL_GPU_STOREOP_STORE;
247
- depth.clear_stencil = (Uint8)prop_u64(depth_value, "stencilClearValue", 0);
248
- depth.stencil_load_op = SDL_GPU_LOADOP_CLEAR;
249
- depth.stencil_store_op = SDL_GPU_STOREOP_STORE;
250
- depth.cycle = true;
281
+ depth.stencil_store_op = prop_string(depth_value, "stencilStoreOp") == "discard"
282
+ ? SDL_GPU_STOREOP_DONT_CARE
283
+ : SDL_GPU_STOREOP_STORE;
284
+ if (native != nullptr) native->initialized = true;
285
+ /* SDL refuses to cycle a target with a LOAD load op for either aspect. */
286
+ depth.cycle = !depth_load && !stencil_load;
251
287
  }
252
288
  SDL_GPUCommandBuffer *cmd = encoder_cmd(encoder);
253
289
  SDL_GPURenderPass *pass =
@@ -355,6 +391,8 @@ xt_value copy_buffer_to_texture(Object &object, int argc, xt_value *argv) {
355
391
  (uint32_t)prop_u64(origin, "x", 0), (uint32_t)prop_u64(origin, "y", 0),
356
392
  (uint32_t)prop_u64(origin, "z", 0), width, height, depth, stride, data,
357
393
  source_offset);
394
+ /* Copies count as writes: a later LOAD must keep them. */
395
+ texture->initialized = true;
358
396
  return XT_UNDEFINED;
359
397
  }
360
398
 
@@ -413,6 +451,8 @@ xt_value copy_texture_to_texture(Object &object, int argc, xt_value *argv) {
413
451
  (Uint32)prop_u64(size, "height", 1),
414
452
  (Uint32)prop_u64(size, "depthOrArrayLayers", 1), true);
415
453
  SDL_EndGPUCopyPass(pass);
454
+ /* The destination now holds defined texels; a later LOAD keeps them. */
455
+ to->initialized = true;
416
456
  return XT_UNDEFINED;
417
457
  }
418
458
 
@@ -134,9 +134,12 @@ bool Convert::texture_format(const std::string &name, SDL_GPUTextureFormat *out)
134
134
  /* WebGPU's `depth24plus` is a promise format: the implementation picks a
135
135
  * depth of at least 24 bits. Metal has no 24-bit-only depth format (its
136
136
  * `D24_UNORM` is `MTLPixelFormatInvalid`), so use 32-bit float, which is
137
- * ≥24 bits and supported by every backend (matching Dawn on Metal). */
137
+ * ≥24 bits and supported by every backend (matching Dawn on Metal). The
138
+ * stencil variant has the same promise, and Metal's 24-bit stencil
139
+ * format is not available on every device (Apple GPUs reject it), so it
140
+ * also uses the 32-bit float + stencil8 format. */
138
141
  {"depth24plus", SDL_GPU_TEXTUREFORMAT_D32_FLOAT},
139
- {"depth24plus-stencil8", SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT},
142
+ {"depth24plus-stencil8", SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT},
140
143
  {"depth32float", SDL_GPU_TEXTUREFORMAT_D32_FLOAT},
141
144
  {"depth32float-stencil8", SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT},
142
145
  };
@@ -159,7 +162,9 @@ const char *Convert::texture_format_name(SDL_GPUTextureFormat format) {
159
162
  case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT: return "rgba32float";
160
163
  case SDL_GPU_TEXTUREFORMAT_D16_UNORM: return "depth16unorm";
161
164
  case SDL_GPU_TEXTUREFORMAT_D24_UNORM: return "depth24plus";
165
+ case SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT: return "depth24plus-stencil8";
162
166
  case SDL_GPU_TEXTUREFORMAT_D32_FLOAT: return "depth32float";
167
+ case SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT: return "depth32float-stencil8";
163
168
  default: return "rgba8unorm";
164
169
  }
165
170
  }
@@ -23,6 +23,17 @@ SDL_GPUTexture *texture_of(uint32_t texture_id) {
23
23
  return static_cast<Texture *>(object.get())->gpu;
24
24
  }
25
25
 
26
+ /* A writable storage binding may `textureStore` into the texture, so the
27
+ * texture no longer counts as untouched: a later pass that LOADs it must keep
28
+ * what the shader stored instead of zero-initialising the whole texture. */
29
+ void mark_storage_texture_written(uint32_t view_id) {
30
+ std::shared_ptr<Object> view = get_object(view_id, Kind::TextureView);
31
+ if (view == nullptr) return;
32
+ std::shared_ptr<Object> texture =
33
+ get_object(static_cast<TextureView *>(view.get())->texture, Kind::Texture);
34
+ if (texture != nullptr) static_cast<Texture *>(texture.get())->initialized = true;
35
+ }
36
+
26
37
  SDL_GPUBuffer *buffer_of(uint32_t buffer_id) {
27
38
  std::shared_ptr<Object> object = get_object(buffer_id, Kind::Buffer);
28
39
  if (object == nullptr) return nullptr;
@@ -218,6 +229,7 @@ bool bind_compute_resources(ComputePassEncoder &pass, ComputePipeline &pipeline,
218
229
  /* Keep the previous contents: a WebGPU storage texture may be read too. */
219
230
  texture.cycle = false;
220
231
  textures.push_back(texture);
232
+ if (entry != nullptr) mark_storage_texture_written(entry->view);
221
233
  }
222
234
  std::vector<SDL_GPUStorageBufferReadWriteBinding> buffers;
223
235
  for (const wgsl::Binding &binding : pipeline.stage.code.write_storage_buffers) {
@@ -186,6 +186,9 @@ xt_value queue_write_texture(Object &object, int argc, xt_value *argv) {
186
186
  SDL_EndGPUCopyPass(pass);
187
187
  SDL_SubmitGPUCommandBuffer(cmd);
188
188
  SDL_ReleaseGPUTransferBuffer(gpu_device(), transfer);
189
+ /* The texture holds defined texels now, so a later LOAD keeps them instead
190
+ * of zero-initialising the whole texture. */
191
+ texture.initialized = true;
189
192
  return XT_UNDEFINED;
190
193
  }
191
194
 
@@ -105,6 +105,7 @@ xt_value device_create_texture(Object &object, int argc, xt_value *argv) {
105
105
  texture->kind = Kind::Texture;
106
106
  texture->device_id = dev.id;
107
107
  texture->format = format;
108
+ texture->format_name = format_name;
108
109
  texture->width = width;
109
110
  texture->height = height;
110
111
  texture->depth = depth;