xbintsc 0.7.8 → 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 (116) 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/context.d.ts +4 -3
  4. package/dist/src/codegen/generator/context.js +6 -5
  5. package/dist/src/codegen/generator/context.js.map +1 -1
  6. package/dist/src/codegen/generator/expressions/assignment.js +3 -3
  7. package/dist/src/driver/config.d.ts +1 -1
  8. package/dist/src/driver/options.d.ts +1 -1
  9. package/dist/src/driver/version.d.ts +1 -1
  10. package/dist/src/driver/version.js +1 -1
  11. package/dist/src/extensions/gui/features.d.ts +14 -6
  12. package/dist/src/extensions/gui/features.js +34 -6
  13. package/dist/src/extensions/gui/features.js.map +1 -1
  14. package/dist/src/extensions/gui/index.js +3 -3
  15. package/dist/tests/codegen/llvm.test.js +1 -1
  16. package/dist/tests/e2e/gui-audio.test.js +24 -0
  17. package/dist/tests/e2e/gui-audio.test.js.map +1 -1
  18. package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
  19. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
  20. package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
  21. package/dist/tests/e2e/gui-font-face.test.js +99 -7
  22. package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
  23. package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
  24. package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
  25. package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
  26. package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
  27. package/dist/tests/e2e/gui-initial-url.test.js +49 -0
  28. package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
  29. package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
  30. package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
  31. package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
  32. package/dist/tests/extensions/gui-features.test.js +27 -2
  33. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  34. package/doc/ai/extensions.md +10 -4
  35. package/doc/ai/zh-CN/extensions.md +8 -3
  36. package/doc/gui/api.md +35 -2
  37. package/doc/gui/build.md +34 -15
  38. package/doc/gui/known-issues.md +1 -1
  39. package/doc/gui/rendering.md +45 -8
  40. package/doc/gui/runtime.md +13 -0
  41. package/doc/gui/webgl.md +11 -2
  42. package/doc/gui.md +9 -4
  43. package/doc/implemented/codegen.md +3 -2
  44. package/doc/zh-CN/gui/api.md +25 -2
  45. package/doc/zh-CN/gui/build.md +20 -7
  46. package/doc/zh-CN/gui/known-issues.md +1 -1
  47. package/doc/zh-CN/gui/rendering.md +33 -8
  48. package/doc/zh-CN/gui/runtime.md +9 -0
  49. package/doc/zh-CN/gui/webgl.md +3 -2
  50. package/doc/zh-CN/gui.md +10 -5
  51. package/doc/zh-CN/implemented/codegen.md +3 -2
  52. package/package.json +7 -7
  53. package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
  54. package/runtime/ext_gui/audio_mixer.cpp +84 -163
  55. package/runtime/ext_gui/audio_mixer.h +59 -3
  56. package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
  57. package/runtime/ext_gui/audio_param_apply.cpp +160 -0
  58. package/runtime/ext_gui/browser.cpp +6 -19
  59. package/runtime/ext_gui/browser_location.cpp +78 -0
  60. package/runtime/ext_gui/css_sheet.cpp +4 -2
  61. package/runtime/ext_gui/document_animation.cpp +7 -0
  62. package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
  63. package/runtime/ext_gui/dom_api_webgl.cpp +31 -0
  64. package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
  65. package/runtime/ext_gui/dom_api_webgl_shader.cpp +19 -4
  66. package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -3
  67. package/runtime/ext_gui/gui.cpp +100 -1
  68. package/runtime/ext_gui/gui_events.cpp +8 -2
  69. package/runtime/ext_gui/perf.cpp +18 -6
  70. package/runtime/ext_gui/perf.h +37 -0
  71. package/runtime/ext_gui/style.cpp +27 -10
  72. package/runtime/ext_gui/style.h +10 -1
  73. package/runtime/ext_gui/text.cpp +4 -0
  74. package/runtime/ext_gui/text_font_bytes.cpp +117 -0
  75. package/runtime/ext_gui/text_font_bytes.h +65 -0
  76. package/runtime/ext_gui/text_font_db.cpp +5 -0
  77. package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
  78. package/runtime/ext_gui/text_font_sfnt.h +5 -2
  79. package/runtime/ext_gui/text_hb.cpp +34 -5
  80. package/runtime/ext_gui/text_stb.cpp +7 -30
  81. package/runtime/ext_gui/text_webfont.cpp +217 -0
  82. package/runtime/ext_gui/warnings.cpp +26 -0
  83. package/runtime/ext_gui/warnings.h +8 -0
  84. package/runtime/ext_gui/webfont_bridge.c +23 -0
  85. package/runtime/ext_gui/webgl.h +34 -39
  86. package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
  87. package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
  88. package/runtime/ext_gui/webgl_draw.cpp +72 -12
  89. package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
  90. package/runtime/ext_gui/webgl_draw_state.h +13 -2
  91. package/runtime/ext_gui/webgl_objects.h +4 -0
  92. package/runtime/ext_gui/webgl_program.h +64 -0
  93. package/runtime/ext_gui/webgl_state.cpp +7 -0
  94. package/runtime/ext_gui/webgl_uniform.cpp +78 -0
  95. package/runtime/ext_gui/window.cpp +6 -0
  96. package/runtime/rt_object.h +6 -0
  97. package/runtime/rt_profile.h +6 -0
  98. package/runtime/xt_containers/access.inc +2 -47
  99. package/runtime/xt_containers/ic_get.inc +70 -0
  100. package/runtime/xt_containers/method_cache.inc +24 -10
  101. package/runtime/xt_containers/objects.inc +12 -1
  102. package/runtime/xt_containers/shapes.inc +78 -0
  103. package/runtime/xt_containers.c +1 -0
  104. package/runtime/xt_profile.c +3 -0
  105. package/scripts/build-gui.ts +43 -49
  106. package/scripts/gui-engine-build.ts +58 -0
  107. package/scripts/gui-vendor-source.ts +53 -0
  108. package/scripts/gui-webfont-build.ts +138 -0
  109. package/src/cli/help.ts +1 -1
  110. package/src/codegen/generator/context.ts +6 -6
  111. package/src/codegen/generator/expressions/assignment.ts +3 -3
  112. package/src/driver/config.ts +1 -1
  113. package/src/driver/options.ts +1 -1
  114. package/src/driver/version.ts +1 -1
  115. package/src/extensions/gui/features.ts +34 -6
  116. package/src/extensions/gui/index.ts +3 -3
@@ -39,6 +39,7 @@
39
39
  #include "webgl_draw_state.h"
40
40
  #include "webgl_glsl.h"
41
41
  #include "webgl_objects.h"
42
+ #include "webgl_program.h"
42
43
  #include "webgl_uniform_block.h"
43
44
 
44
45
  namespace xtgui {
@@ -78,44 +79,6 @@ struct WebGLShader {
78
79
  int attachments = 0;
79
80
  };
80
81
 
81
- /** One active uniform of a linked program, with its std140 home. */
82
- struct WebGLUniform {
83
- std::string name;
84
- GlslBlockMember layout;
85
- bool sampler = false;
86
- /** `sampler2DShadow`/`samplerCubeShadow`/`sampler2DArrayShadow`: SDL_GPU
87
- * binds a comparison sampler, and the GLSL `texture()` call performs the
88
- * hardware depth compare. */
89
- bool compare = false;
90
- int unit = -1;
91
- };
92
-
93
- struct WebGLProgram {
94
- uint32_t id = 0;
95
- uint32_t vs = 0;
96
- uint32_t fs = 0;
97
- bool linked = false;
98
- std::string log;
99
- std::vector<WebGLUniform> uniforms;
100
- std::vector<WebGLUniformBlock> blocks;
101
- std::vector<GlslUniformDecl> attribs;
102
- std::vector<WebGLDrawAttribPlan> draw_attribs;
103
- std::unordered_map<std::string, int> attrib_locations; // name -> location
104
- std::unordered_map<std::string, int> attrib_bindings; // bindAttribLocation overrides
105
- std::unordered_map<std::string, int> uniform_index; // name -> uniforms[] index
106
- std::vector<int> vs_sampler_uniforms; // local binding -> uniforms[] index
107
- std::vector<int> fs_sampler_uniforms;
108
- std::vector<uint8_t> block; // std140 uniform data
109
- int block_size = 0;
110
- bool block_dirty = true;
111
- bool vs_has_block = false;
112
- bool fs_has_block = false;
113
- SDL_GPUShader *vs_shader = nullptr;
114
- SDL_GPUShader *fs_shader = nullptr;
115
- std::vector<SDL_GPUGraphicsPipeline *> pipelines;
116
- std::unordered_map<WebGLDrawKey, SDL_GPUGraphicsPipeline *, WebGLDrawKeyHash> pipeline_cache;
117
- };
118
-
119
82
  class WebGLContext {
120
83
  public:
121
84
  WebGLContext(uint64_t id, int width, int height, bool webgl2, bool antialias);
@@ -168,6 +131,12 @@ class WebGLContext {
168
131
  void color_mask(bool r, bool g, bool b, bool a);
169
132
  void depth_mask(bool write) { state_.depth_write = write; }
170
133
  void depth_func(int func) { state_.depth_func = func; }
134
+ /** `glDepthRange`: maps clip-space depth into `[near, far]` of the depth
135
+ * buffer for the test and the write alike. Not pipeline state — the draw path
136
+ * applies it through the SDL_GPU viewport's depth bounds. */
137
+ void depth_range(float near, float far);
138
+ float depth_range_near() const { return depth_range_near_; }
139
+ float depth_range_far() const { return depth_range_far_; }
171
140
  void stencil_func(int func, int ref, uint32_t mask);
172
141
  void stencil_func_separate(int face, int func, int ref, uint32_t mask);
173
142
  void stencil_op(int fail, int zfail, int zpass);
@@ -216,11 +185,16 @@ class WebGLContext {
216
185
  int attrib_location(uint32_t program, const std::string &name);
217
186
  void bind_attrib_location(uint32_t program, int index, const std::string &name);
218
187
 
219
- /* Uniform uploads; `location` indexes the program's uniform table. */
188
+ /* Uniform uploads; `location` indexes the program's uniform table. The `_raw`
189
+ * variants take the `Float32Array` bytes directly, skipping the per-element
190
+ * conversion through `double` that `uniform*{f,mat}` need for plain arrays. */
220
191
  void uniform_floats(uint32_t program, int location, int components, const double *values,
221
192
  int count);
193
+ void uniform_floats_raw(uint32_t program, int location, int components, const float *values,
194
+ int count);
222
195
  void uniform_ints(uint32_t program, int location, int components, const int *values, int count);
223
196
  void uniform_matrix(uint32_t program, int location, int dimensions, const double *values);
197
+ void uniform_matrix_raw(uint32_t program, int location, int dimensions, const float *values);
224
198
  bool uniform_values(uint32_t program, int location, std::vector<double> *out);
225
199
 
226
200
  /* -- buffers and vertex attributes ------------------------------------ */
@@ -452,6 +426,10 @@ class WebGLContext {
452
426
  void draw(int mode, int first, int count, int index_type, int offset, int instances);
453
427
  void save_vertex_array();
454
428
  void load_vertex_array(const WebGLVertexArray *array);
429
+ /** Bump the layout version the prepared-draw cache depends on. A named VAO
430
+ * owns a revision of its own (see `WebGLVertexArray::revision`); the default
431
+ * VAO falls back to the context-wide counter. */
432
+ void note_attrib_state_change();
455
433
  SDL_GPUSampler *sampler_gpu(WebGLSampler *sampler);
456
434
  uint32_t buffer_for_target(int target) const;
457
435
  void release_program_pipelines(WebGLProgram *program);
@@ -487,6 +465,20 @@ class WebGLContext {
487
465
  std::unordered_map<std::string, SDL_GPUGraphicsPipeline *> internal_pipelines_;
488
466
  /** Scratch for the sampler bindings of one draw. */
489
467
  std::vector<SDL_GPUTextureSamplerBinding> sampler_bindings_;
468
+ /* The dynamic state last recorded into a render pass. Repeat draws skip the
469
+ * corresponding SDL command while the pass is the same; a pass split (an
470
+ * upload, a target change) invalidates everything because these bindings do
471
+ * not survive the new pass. */
472
+ uint64_t last_viewport_pass_ = 0;
473
+ SDL_GPUViewport last_viewport_{};
474
+ uint64_t last_scissor_pass_ = 0;
475
+ SDL_Rect last_scissor_{};
476
+ uint64_t last_blend_pass_ = 0;
477
+ SDL_FColor last_blend_{};
478
+ uint64_t last_vertex_sampler_pass_ = 0;
479
+ uint64_t last_fragment_sampler_pass_ = 0;
480
+ std::vector<SDL_GPUTextureSamplerBinding> last_vertex_samplers_;
481
+ std::vector<SDL_GPUTextureSamplerBinding> last_fragment_samplers_;
490
482
 
491
483
  SDL_GPUTexture *color_ = nullptr;
492
484
  SDL_GPUTexture *depth_ = nullptr;
@@ -530,6 +522,9 @@ class WebGLContext {
530
522
  int viewport_[4] = {0, 0, 0, 0};
531
523
  int scissor_box_[4] = {0, 0, 0, 0};
532
524
  bool scissor_test_ = false;
525
+ /** Depth range (`glDepthRange`); see `depth_range`. */
526
+ float depth_range_near_ = 0.0f;
527
+ float depth_range_far_ = 1.0f;
533
528
 
534
529
  std::unordered_map<uint32_t, std::unique_ptr<WebGLShader>> shaders_;
535
530
  std::unordered_map<uint32_t, std::unique_ptr<WebGLProgram>> programs_;
@@ -217,6 +217,11 @@ bool WebGLContext::draw_color_quad(SDL_GPUTexture *source, bool source_top_down,
217
217
  viewport.min_depth = 0.0f;
218
218
  viewport.max_depth = 1.0f;
219
219
  SDL_SetGPUViewport(pass_, &viewport);
220
+ /* This helper replaces the pass's viewport without going through the draw
221
+ * path's cache, so record what the pass now has: the next user draw has to
222
+ * re-record its own depth range instead of trusting a stale "unchanged". */
223
+ last_viewport_ = viewport;
224
+ last_viewport_pass_ = pass_serial_;
220
225
  SDL_Rect scissor;
221
226
  scissor.x = x0;
222
227
  /* The rectangle covers the rows between the two corners; for a top-down
@@ -287,13 +287,23 @@ uint32_t WebGLContext::buffer_size(uint32_t id) {
287
287
 
288
288
  /* -- vertex attributes --------------------------------------------------- */
289
289
 
290
+ void WebGLContext::note_attrib_state_change() {
291
+ attribs_version_ += 1;
292
+ /* A named VAO's layout is what the prepared-draw cache keys on, so bump the
293
+ * revision of the *bound* array. The default VAO uses `attribs_version_`. */
294
+ if (bound_vertex_array_ != 0) {
295
+ WebGLVertexArray *array = vertex_array(bound_vertex_array_);
296
+ if (array != nullptr) array->revision += 1;
297
+ }
298
+ }
299
+
290
300
  void WebGLContext::enable_attrib(int index, bool enabled) {
291
301
  if (index < 0 || index >= kWebGLMaxAttribs) {
292
302
  set_error(GL_INVALID_VALUE);
293
303
  return;
294
304
  }
295
305
  attribs_[index].enabled = enabled;
296
- attribs_version_ += 1;
306
+ note_attrib_state_change();
297
307
  }
298
308
 
299
309
  bool WebGLContext::attrib_enabled(int index) const {
@@ -314,7 +324,7 @@ void WebGLContext::attrib_pointer(int index, int size, int type, bool normalized
314
324
  attrib.integer = false;
315
325
  attrib.stride = stride > 0 ? stride : 0;
316
326
  attrib.offset = offset;
317
- attribs_version_ += 1;
327
+ note_attrib_state_change();
318
328
  }
319
329
 
320
330
  void WebGLContext::attrib_ipointer(int index, int size, int type, int stride, int offset) {
@@ -330,7 +340,7 @@ void WebGLContext::attrib_ipointer(int index, int size, int type, int stride, in
330
340
  attrib.integer = true;
331
341
  attrib.stride = stride > 0 ? stride : 0;
332
342
  attrib.offset = offset;
333
- attribs_version_ += 1;
343
+ note_attrib_state_change();
334
344
  }
335
345
 
336
346
  void WebGLContext::attrib_divisor(int index, int divisor) {
@@ -342,7 +352,7 @@ void WebGLContext::attrib_divisor(int index, int divisor) {
342
352
  * rates WebGL programs use — per vertex (0) and per instance (1) — map onto
343
353
  * the backend. A larger divisor is clamped. */
344
354
  attribs_[index].divisor = divisor > 0 ? 1 : 0;
345
- attribs_version_ += 1;
355
+ note_attrib_state_change();
346
356
  }
347
357
 
348
358
  void WebGLContext::attrib_value(int index, int components, const float *values) {
@@ -350,7 +360,11 @@ void WebGLContext::attrib_value(int index, int components, const float *values)
350
360
  for (int i = 0; i < 4; i++) attribs_[index].current[i] = i < components ? values[i] : 0.0f;
351
361
  if (components < 4) attribs_[index].current[3] = 1.0f;
352
362
  attribs_[index].constant_dirty = true;
353
- attribs_version_ += 1;
363
+ /* The constant-attribute buffer is shared by every VAO that inherits this
364
+ * attribute state, and it is recreated on upload, so a prepared draw holding
365
+ * the old pointer must be invalidated too. */
366
+ gpu_version_ += 1;
367
+ note_attrib_state_change();
354
368
  }
355
369
 
356
370
  void WebGLContext::attrib_state(int index, int *size, int *type, int *stride, bool *normalized,
@@ -167,6 +167,7 @@ void WebGLContext::draw_range_elements(int mode, int start, int end, int count,
167
167
  void WebGLContext::draw(int mode, int first, int count, int index_type, int offset,
168
168
  int instances) {
169
169
  if (count <= 0) return;
170
+ PerfScopeActive perf_scope(&g_perf_frame_ms.webgl_draw);
170
171
  g_perf_frame.webglDraws += 1;
171
172
  WebGLProgram *program_ = program(current_program_);
172
173
  if (program_ == nullptr || !program_->linked || program_->vs_shader == nullptr) {
@@ -284,9 +285,21 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
284
285
  key.mode = draw_mode;
285
286
  key.index_size = index_size;
286
287
  key.element_buffer = !needs_expand && index_size > 0 ? bound_element_ : 0;
287
- key.attribs_version = attribs_version_;
288
+ /* The vertex layout is identified by the bound VAO and its revision, not by
289
+ * the context-wide counter that every VAO bind and framebuffer switch moves.
290
+ * Keying on the counter rebuilt every prepared draw once per frame (three.js
291
+ * binds a VAO per geometry), which was the dominant per-draw cost. */
292
+ key.attribs_vao = bound_vertex_array_;
293
+ const WebGLVertexArray *bound_vao =
294
+ bound_vertex_array_ != 0 ? vertex_array(bound_vertex_array_) : nullptr;
295
+ key.attribs_revision = bound_vao != nullptr ? bound_vao->revision : attribs_version_;
288
296
  key.gpu_version = gpu_version_;
289
- key.framebuffer_version = framebuffer_version_;
297
+ /* `framebuffer_version_` is a monotonic counter bumped by every framebuffer
298
+ * switch, not a content hash. Keying the prepared-draw cache on it rebuilt
299
+ * every setup every frame in a multi-pass scene (the attachment formats it
300
+ * stood in for are already part of `state_hash`, and the bindings do not
301
+ * depend on the target), so the setup identity ignores it. */
302
+ key.framebuffer_version = 0;
290
303
  draw_state_hash(&key.state_hash, &key.state_hash2);
291
304
 
292
305
  const WebGLDrawSetup *setup = prepare_draw(key);
@@ -315,9 +328,20 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
315
328
  viewport.y = (float)(viewport_[1] + viewport_h);
316
329
  viewport.h = (float)-viewport_h;
317
330
  }
318
- viewport.min_depth = 0.0f;
319
- viewport.max_depth = 1.0f;
320
- SDL_SetGPUViewport(pass_, &viewport);
331
+ viewport.min_depth = depth_range_near_;
332
+ viewport.max_depth = depth_range_far_;
333
+ /* Dynamic state is per pass: a pass split loses it, so the comparison is
334
+ * scoped by `pass_serial_`. Repeating the same value is otherwise a no-op
335
+ * command that a multi-hundred-draw frame records once per draw. */
336
+ if (last_viewport_pass_ != pass_serial_ || last_viewport_.x != viewport.x ||
337
+ last_viewport_.y != viewport.y || last_viewport_.w != viewport.w ||
338
+ last_viewport_.h != viewport.h || last_viewport_.min_depth != viewport.min_depth ||
339
+ last_viewport_.max_depth != viewport.max_depth) {
340
+ SDL_SetGPUViewport(pass_, &viewport);
341
+ last_viewport_ = viewport;
342
+ last_viewport_pass_ = pass_serial_;
343
+ g_perf_frame.webglStateBinds += 1;
344
+ }
321
345
 
322
346
  SDL_Rect scissor;
323
347
  scissor.x = 0;
@@ -342,7 +366,14 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
342
366
  if (scissor.y + scissor.h > target_height) scissor.h = target_height - scissor.y;
343
367
  if (scissor.w < 0) scissor.w = 0;
344
368
  if (scissor.h < 0) scissor.h = 0;
345
- SDL_SetGPUScissor(pass_, &scissor);
369
+ if (last_scissor_pass_ != pass_serial_ || last_scissor_.x != scissor.x ||
370
+ last_scissor_.y != scissor.y || last_scissor_.w != scissor.w ||
371
+ last_scissor_.h != scissor.h) {
372
+ SDL_SetGPUScissor(pass_, &scissor);
373
+ last_scissor_ = scissor;
374
+ last_scissor_pass_ = pass_serial_;
375
+ g_perf_frame.webglStateBinds += 1;
376
+ }
346
377
 
347
378
  /* Bindings belong to the render pass, so a setup reused across an upload (a
348
379
  * new pass) must be bound again even though nothing about it changed. */
@@ -376,7 +407,14 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
376
407
  constants.g = blend_color_[1];
377
408
  constants.b = blend_color_[2];
378
409
  constants.a = blend_color_[3];
379
- SDL_SetGPUBlendConstants(pass_, constants);
410
+ if (last_blend_pass_ != pass_serial_ || last_blend_.r != constants.r ||
411
+ last_blend_.g != constants.g || last_blend_.b != constants.b ||
412
+ last_blend_.a != constants.a) {
413
+ SDL_SetGPUBlendConstants(pass_, constants);
414
+ last_blend_ = constants;
415
+ last_blend_pass_ = pass_serial_;
416
+ g_perf_frame.webglStateBinds += 1;
417
+ }
380
418
  }
381
419
  /* User uniform blocks live in the same std140 block; refresh their members
382
420
  * from the CPU-side buffer bound to each block's binding point. */
@@ -450,12 +488,34 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
450
488
  texture_binding.sampler = sampler;
451
489
  sampler_bindings_.push_back(texture_binding);
452
490
  }
453
- if (vertex_stage) {
454
- SDL_BindGPUVertexSamplers(pass_, 0, sampler_bindings_.data(),
455
- (Uint32)sampler_bindings_.size());
456
- } else {
457
- SDL_BindGPUFragmentSamplers(pass_, 0, sampler_bindings_.data(),
491
+ /* A sampler set only has to be re-bound when it changes or the pass was
492
+ * split (bindings do not survive a new pass). three.js repeats the same
493
+ * material/texture set across many objects, so this removes one descriptor
494
+ * write per draw in the common case. */
495
+ uint64_t &bound_pass = vertex_stage ? last_vertex_sampler_pass_ : last_fragment_sampler_pass_;
496
+ std::vector<SDL_GPUTextureSamplerBinding> &cached =
497
+ vertex_stage ? last_vertex_samplers_ : last_fragment_samplers_;
498
+ bool unchanged = bound_pass == pass_serial_ && cached.size() == sampler_bindings_.size();
499
+ if (unchanged) {
500
+ for (size_t i = 0; i < cached.size(); i++) {
501
+ if (cached[i].texture != sampler_bindings_[i].texture ||
502
+ cached[i].sampler != sampler_bindings_[i].sampler) {
503
+ unchanged = false;
504
+ break;
505
+ }
506
+ }
507
+ }
508
+ if (!unchanged) {
509
+ if (vertex_stage) {
510
+ SDL_BindGPUVertexSamplers(pass_, 0, sampler_bindings_.data(),
458
511
  (Uint32)sampler_bindings_.size());
512
+ } else {
513
+ SDL_BindGPUFragmentSamplers(pass_, 0, sampler_bindings_.data(),
514
+ (Uint32)sampler_bindings_.size());
515
+ }
516
+ cached = sampler_bindings_;
517
+ bound_pass = pass_serial_;
518
+ g_perf_frame.webglSamplerBinds += 1;
459
519
  }
460
520
  };
461
521
  bind_samplers(program_->vs_sampler_uniforms, true);
@@ -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);
@@ -150,6 +150,10 @@ struct WebGLVertexArray {
150
150
  uint32_t id = 0;
151
151
  WebGLAttribState attribs[kWebGLMaxAttribs];
152
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;
153
157
  };
154
158
 
155
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;
@@ -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;
@@ -423,11 +423,14 @@ static xt_value win_stats(xt_value self, xt_value env, int32_t argc, xt_value *a
423
423
  * cumulative (diff two calls to get the work one frame did). */
424
424
  const xtgui::PerfTimings &ms = xtgui::g_perf_last;
425
425
  xt_object_set(object, xt_string_from_cstr("frameMs"), xt_number(ms.frame));
426
+ xt_object_set(object, xt_string_from_cstr("scriptMs"), xt_number(ms.script));
426
427
  xt_object_set(object, xt_string_from_cstr("restyleMs"), xt_number(ms.restyle));
427
428
  xt_object_set(object, xt_string_from_cstr("layoutMs"), xt_number(ms.layout));
428
429
  xt_object_set(object, xt_string_from_cstr("paintMs"), xt_number(ms.paint));
429
430
  xt_object_set(object, xt_string_from_cstr("uploadMs"), xt_number(ms.upload));
430
431
  xt_object_set(object, xt_string_from_cstr("drawMs"), xt_number(ms.draw));
432
+ xt_object_set(object, xt_string_from_cstr("webglDrawMs"), xt_number(ms.webgl_draw));
433
+ xt_object_set(object, xt_string_from_cstr("webglUniformMs"), xt_number(ms.webgl_uniform));
431
434
  const xtgui::PerfCounters &counters = xtgui::g_perf_total;
432
435
  auto setCounter = [&object](const char *name, uint64_t value) {
433
436
  xt_object_set(object, xt_string_from_cstr(name), xt_number((double)value));
@@ -463,6 +466,9 @@ static xt_value win_stats(xt_value self, xt_value env, int32_t argc, xt_value *a
463
466
  setCounter("webglPasses", counters.webglPasses);
464
467
  setCounter("webglPipelineMisses", counters.webglPipelineMisses);
465
468
  setCounter("webglBufferUploadBytes", counters.webglBufferUploadBytes);
469
+ setCounter("webglSetupMisses", counters.webglSetupMisses);
470
+ setCounter("webglSamplerBinds", counters.webglSamplerBinds);
471
+ setCounter("webglStateBinds", counters.webglStateBinds);
466
472
  return object;
467
473
  }
468
474
 
@@ -26,12 +26,18 @@ typedef struct {
26
26
  * (parent, key) transition and never freed, so an object's shape pointer is a
27
27
  * stable layout identity. Shape keys are rooted by a collector provider in
28
28
  * xt_containers/shapes.inc. */
29
+ struct xt_shape_index;
29
30
  typedef struct xt_shape {
30
31
  xt_value key;
31
32
  uint32_t count;
32
33
  struct xt_shape *parent;
33
34
  struct xt_shape *next; /* hash-bucket chain */
34
35
  struct xt_shape *all_next; /* every shape, for the GC root provider */
36
+ /* Lazily built key -> property-index table for this layout (see
37
+ * `xt_shape_lookup`). NULL until the shape is first looked up in a
38
+ * property-heavy object. The chain order and the object's property order are
39
+ * the same by construction, so the index is a property position too. */
40
+ struct xt_shape_index *index;
35
41
  } xt_shape;
36
42
 
37
43
  typedef struct {
@@ -25,6 +25,12 @@ enum {
25
25
  XT_PROFILE_STRING_CSTR,
26
26
  XT_PROFILE_TYPED_READ,
27
27
  XT_PROFILE_TYPED_WRITE,
28
+ /* Prepared inline-cache outcome: a hit skips the property lookup, an early
29
+ * out is a receiver/key the cache cannot describe (arrays, host views,
30
+ * symbol arithmetic), a miss performs the lookup and refills. */
31
+ XT_PROFILE_IC_HIT,
32
+ XT_PROFILE_IC_EARLY,
33
+ XT_PROFILE_IC_MISS,
28
34
  XT_PROFILE_COUNT
29
35
  };
30
36