xbintsc 0.5.38 → 0.5.41

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 (117) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/codegen/generator/module.js +23 -7
  3. package/dist/src/codegen/generator/module.js.map +1 -1
  4. package/dist/src/driver/version.d.ts +1 -1
  5. package/dist/src/driver/version.js +1 -1
  6. package/dist/src/extensions/gui/html.js +9 -2
  7. package/dist/src/extensions/gui/html.js.map +1 -1
  8. package/dist/tests/codegen/closures.test.js +7 -0
  9. package/dist/tests/codegen/closures.test.js.map +1 -1
  10. package/dist/tests/e2e/compile-objects-classes.test.js +31 -0
  11. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-canvas-state.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-canvas-state.test.js +297 -0
  14. package/dist/tests/e2e/gui-canvas-state.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-draw-perf.test.d.ts +22 -0
  16. package/dist/tests/e2e/gui-draw-perf.test.js +405 -0
  17. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -0
  18. package/dist/tests/e2e/gui-perf.test.d.ts +23 -0
  19. package/dist/tests/e2e/gui-perf.test.js +283 -0
  20. package/dist/tests/e2e/gui-perf.test.js.map +1 -0
  21. package/dist/tests/e2e/gui-script-import-order.test.js +9 -1
  22. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  23. package/dist/tests/e2e/gui-webgl.test.js +4 -4
  24. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  25. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.d.ts +8 -0
  26. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +336 -0
  27. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -0
  28. package/dist/tests/extensions/gui.test.js +23 -0
  29. package/dist/tests/extensions/gui.test.js.map +1 -1
  30. package/doc/gui/api.md +28 -2
  31. package/doc/gui/canvas.md +110 -27
  32. package/doc/gui/known-issues.md +32 -10
  33. package/doc/gui/rendering.md +77 -0
  34. package/doc/gui/runtime.md +18 -3
  35. package/doc/gui/webgl.md +38 -19
  36. package/doc/gui.md +33 -1
  37. package/doc/zh-CN/gui/api.md +25 -2
  38. package/doc/zh-CN/gui/canvas.md +95 -26
  39. package/doc/zh-CN/gui/known-issues.md +21 -4
  40. package/doc/zh-CN/gui/rendering.md +59 -0
  41. package/doc/zh-CN/gui/runtime.md +13 -3
  42. package/doc/zh-CN/gui/webgl.md +27 -5
  43. package/doc/zh-CN/gui.md +25 -2
  44. package/package.json +7 -7
  45. package/runtime/ext_gui/browser.cpp +3 -1
  46. package/runtime/ext_gui/canvas.cpp +135 -89
  47. package/runtime/ext_gui/canvas.h +116 -0
  48. package/runtime/ext_gui/canvas_clip.cpp +143 -0
  49. package/runtime/ext_gui/canvas_composite.cpp +241 -0
  50. package/runtime/ext_gui/canvas_coverage.cpp +241 -0
  51. package/runtime/ext_gui/canvas_gradient.cpp +41 -4
  52. package/runtime/ext_gui/canvas_image_data.cpp +4 -0
  53. package/runtime/ext_gui/canvas_internal.h +34 -9
  54. package/runtime/ext_gui/canvas_raster.cpp +207 -178
  55. package/runtime/ext_gui/canvas_shadow.cpp +161 -0
  56. package/runtime/ext_gui/canvas_stroke.cpp +37 -9
  57. package/runtime/ext_gui/canvas_style.cpp +59 -0
  58. package/runtime/ext_gui/canvas_text.cpp +140 -0
  59. package/runtime/ext_gui/css.h +7 -0
  60. package/runtime/ext_gui/css_sheet.cpp +22 -0
  61. package/runtime/ext_gui/document.cpp +25 -1
  62. package/runtime/ext_gui/document.h +8 -0
  63. package/runtime/ext_gui/dom_api_canvas.cpp +16 -1
  64. package/runtime/ext_gui/dom_api_canvas_image.cpp +9 -0
  65. package/runtime/ext_gui/dom_api_canvas_state.cpp +217 -0
  66. package/runtime/ext_gui/dom_api_internal.h +36 -37
  67. package/runtime/ext_gui/dom_api_offscreen.cpp +213 -0
  68. package/runtime/ext_gui/dom_api_proto.cpp +18 -0
  69. package/runtime/ext_gui/dom_api_webgl.cpp +19 -9
  70. package/runtime/ext_gui/dom_api_webgl_internal.h +58 -0
  71. package/runtime/ext_gui/dom_api_webgl_source.cpp +18 -0
  72. package/runtime/ext_gui/gradient_values.cpp +12 -1
  73. package/runtime/ext_gui/gui.cpp +5 -0
  74. package/runtime/ext_gui/gui_canvas.cpp +4 -0
  75. package/runtime/ext_gui/gui_engine.h +30 -0
  76. package/runtime/ext_gui/gui_events.cpp +182 -58
  77. package/runtime/ext_gui/gui_media.cpp +3 -0
  78. package/runtime/ext_gui/image.cpp +69 -0
  79. package/runtime/ext_gui/image_bitmap_browser.cpp +11 -0
  80. package/runtime/ext_gui/layout.cpp +4 -0
  81. package/runtime/ext_gui/paint.cpp +19 -0
  82. package/runtime/ext_gui/paint_background.cpp +18 -1
  83. package/runtime/ext_gui/perf.cpp +171 -0
  84. package/runtime/ext_gui/perf.h +127 -0
  85. package/runtime/ext_gui/renderer.cpp +109 -41
  86. package/runtime/ext_gui/renderer.h +33 -9
  87. package/runtime/ext_gui/renderer_image.cpp +135 -65
  88. package/runtime/ext_gui/renderer_internal.h +15 -3
  89. package/runtime/ext_gui/renderer_text.cpp +24 -12
  90. package/runtime/ext_gui/style.cpp +101 -30
  91. package/runtime/ext_gui/style.h +6 -3
  92. package/runtime/ext_gui/style_print.cpp +7 -1
  93. package/runtime/ext_gui/text.cpp +23 -4
  94. package/runtime/ext_gui/text.h +29 -0
  95. package/runtime/ext_gui/text_cache.cpp +118 -0
  96. package/runtime/ext_gui/text_glyph_cache.cpp +100 -0
  97. package/runtime/ext_gui/webgl.h +48 -146
  98. package/runtime/ext_gui/webgl_blit.cpp +12 -1
  99. package/runtime/ext_gui/webgl_blit_depth.cpp +270 -0
  100. package/runtime/ext_gui/webgl_buffer.cpp +59 -1
  101. package/runtime/ext_gui/webgl_buffer_objects.cpp +106 -9
  102. package/runtime/ext_gui/webgl_clear.cpp +3 -0
  103. package/runtime/ext_gui/webgl_context.cpp +29 -3
  104. package/runtime/ext_gui/webgl_draw.cpp +75 -270
  105. package/runtime/ext_gui/webgl_draw_buffers.cpp +1 -0
  106. package/runtime/ext_gui/webgl_draw_setup.cpp +321 -0
  107. package/runtime/ext_gui/webgl_draw_state.h +130 -0
  108. package/runtime/ext_gui/webgl_glsl_scan.cpp +3 -8
  109. package/runtime/ext_gui/webgl_objects.h +148 -0
  110. package/runtime/ext_gui/webgl_pipeline.cpp +8 -8
  111. package/runtime/ext_gui/webgl_program_internal.h +35 -0
  112. package/runtime/ext_gui/webgl_shader.cpp +22 -30
  113. package/runtime/ext_gui/webgl_webgl2.cpp +36 -0
  114. package/runtime/ext_gui/window.cpp +56 -1
  115. package/src/codegen/generator/module.ts +22 -7
  116. package/src/driver/version.ts +1 -1
  117. package/src/extensions/gui/html.ts +9 -2
@@ -101,7 +101,26 @@ PaintClip clipForChildren(const LayoutBox *box, const PaintClip &parent) {
101
101
  void paintChildren(const LayoutBox *box, DisplayList &out, const PaintClip &clip,
102
102
  const paint_detail::FilterChain &chain, float viewportWidth,
103
103
  float viewportHeight) {
104
+ /* Fast path: with no stacking child the painter order *is* document order, so
105
+ * skip the temporary vector and the sort (they used to be allocated and run
106
+ * for every parent, every repaint). */
107
+ bool stacked = false;
108
+ for (const std::unique_ptr<LayoutBox> &child : box->children) {
109
+ if (child->display == Display::None || child->style == nullptr) continue;
110
+ if (zIndexOf(child.get()) != 0) {
111
+ stacked = true;
112
+ break;
113
+ }
114
+ }
115
+ if (!stacked) {
116
+ for (const std::unique_ptr<LayoutBox> &child : box->children) {
117
+ if (child->display == Display::None || child->style == nullptr) continue;
118
+ paintBox(child.get(), out, clip, chain, viewportWidth, viewportHeight);
119
+ }
120
+ return;
121
+ }
104
122
  std::vector<const LayoutBox *> children;
123
+ children.reserve(box->children.size());
105
124
  for (const std::unique_ptr<LayoutBox> &child : box->children) {
106
125
  if (child->display == Display::None || child->style == nullptr) continue;
107
126
  children.push_back(child.get());
@@ -200,6 +200,21 @@ void paintRadial(const LayoutBox &box, const XtStyle &style, const BackgroundLay
200
200
  addEllipse(out, cx, cy, 0.6f, 0.6f, center, clip, box.world);
201
201
  }
202
202
 
203
+ void paintImage(const LayoutBox &box, const BackgroundLayer &layer, const FilterChain &chain,
204
+ const PaintClip &clip, DisplayList &out) {
205
+ if (layer.image_src.empty() || box.width <= 0.0f || box.height <= 0.0f) return;
206
+ /* A `background-image: url(...)` layer fills the box. (CSS `background-size`
207
+ * is not modelled, so the intrinsic-size/tiling default becomes a stretch.) */
208
+ PaintImage image;
209
+ image.x = box.x;
210
+ image.y = box.y;
211
+ image.width = box.width;
212
+ image.height = box.height;
213
+ image.src = layer.image_src;
214
+ image.transform = box.world;
215
+ addImage(out, image, clip, chain);
216
+ }
217
+
203
218
  } // namespace
204
219
 
205
220
  void paintBackgrounds(DisplayList &out, const LayoutBox &box, const XtStyle &style,
@@ -219,8 +234,10 @@ void paintBackgrounds(DisplayList &out, const LayoutBox &box, const XtStyle &sty
219
234
  const BackgroundLayer &layer = style.background_layers[i];
220
235
  if (layer.kind == BackgroundKind::Linear) {
221
236
  paintLinear(box, layer, chain, layerClip, out);
222
- } else {
237
+ } else if (layer.kind == BackgroundKind::Radial) {
223
238
  paintRadial(box, style, layer, chain, layerClip, viewportWidth, viewportHeight, out);
239
+ } else {
240
+ paintImage(box, layer, chain, layerClip, out);
224
241
  }
225
242
  }
226
243
  }
@@ -0,0 +1,171 @@
1
+ /*
2
+ * xbintsc GUI engine — frame timing implementation (see `perf.h`).
3
+ */
4
+
5
+ #include "perf.h"
6
+
7
+ #include <SDL3/SDL.h>
8
+
9
+ #include <cstdio>
10
+ #include <cstdlib>
11
+ #include <cstring>
12
+
13
+ namespace xtgui {
14
+
15
+ PerfCounters g_perf_frame;
16
+ PerfCounters g_perf_total;
17
+ PerfTimings g_perf_frame_ms;
18
+ PerfTimings g_perf_last;
19
+
20
+ namespace {
21
+
22
+ int g_perf_mode = -1; // -1 = not read yet, 0 = disabled, N = every N frames
23
+ uint64_t g_frame_start = 0;
24
+ /** Accumulator for `XT_GUI_PERF=<N>` (N > 1). */
25
+ PerfTimings g_agg_ms;
26
+ PerfCounters g_agg_counters;
27
+ uint64_t g_agg_frames = 0;
28
+
29
+ void readMode() {
30
+ if (g_perf_mode >= 0) return;
31
+ const char *value = SDL_getenv("XT_GUI_PERF");
32
+ if (value == nullptr || value[0] == '\0' || std::strcmp(value, "0") == 0) {
33
+ g_perf_mode = 0;
34
+ return;
35
+ }
36
+ int every = std::atoi(value);
37
+ g_perf_mode = every > 1 ? every : 1;
38
+ }
39
+
40
+ void addCounters(PerfCounters *target, const PerfCounters &value) {
41
+ target->frames += value.frames;
42
+ target->presents += value.presents;
43
+ target->exposes += value.exposes;
44
+ target->deferred += value.deferred;
45
+ target->starvedFrames += value.starvedFrames;
46
+ target->skippedFrames += value.skippedFrames;
47
+ target->restyles += value.restyles;
48
+ target->restyleNodes += value.restyleNodes;
49
+ target->rulesEvaluated += value.rulesEvaluated;
50
+ target->mediaEvaluated += value.mediaEvaluated;
51
+ target->layouts += value.layouts;
52
+ target->layoutBoxes += value.layoutBoxes;
53
+ target->textShapeCalls += value.textShapeCalls;
54
+ target->textShapeHits += value.textShapeHits;
55
+ target->paints += value.paints;
56
+ target->paintOps += value.paintOps;
57
+ target->drawRuns += value.drawRuns;
58
+ target->drawCalls += value.drawCalls;
59
+ target->uploadSubmits += value.uploadSubmits;
60
+ target->vertices += value.vertices;
61
+ target->syncNodes += value.syncNodes;
62
+ target->transitionFrames += value.transitionFrames;
63
+ target->canvasDraws += value.canvasDraws;
64
+ target->canvasUploads += value.canvasUploads;
65
+ target->canvasUploadBytes += value.canvasUploadBytes;
66
+ target->webglDraws += value.webglDraws;
67
+ target->webglPasses += value.webglPasses;
68
+ target->webglPipelineMisses += value.webglPipelineMisses;
69
+ target->webglBufferUploadBytes += value.webglBufferUploadBytes;
70
+ }
71
+
72
+ void addTimings(PerfTimings *target, const PerfTimings &value) {
73
+ target->frame += value.frame;
74
+ target->restyle += value.restyle;
75
+ target->layout += value.layout;
76
+ target->paint += value.paint;
77
+ target->upload += value.upload;
78
+ target->draw += value.draw;
79
+ }
80
+
81
+ void printLine(uint64_t frames, const PerfTimings &ms, const PerfCounters &counters) {
82
+ double divisor = frames > 0 ? (double)frames : 1.0;
83
+ std::fprintf(stderr,
84
+ "xt_gui perf frames=%llu ms=%.3f restyle=%.3f layout=%.3f paint=%.3f "
85
+ "upload=%.3f draw=%.3f restyles=%llu nodes=%llu rules=%llu media=%llu "
86
+ "layouts=%llu boxes=%llu text=%llu textHit=%llu ops=%llu runs=%llu "
87
+ "draws=%llu uploads=%llu verts=%llu sync=%llu presents=%llu exposes=%llu "
88
+ "deferred=%llu starved=%llu skip=%llu "
89
+ "cdraw=%llu cups=%llu cbytes=%llu gldraw=%llu passes=%llu gmiss=%llu "
90
+ "gbytes=%llu\n",
91
+ (unsigned long long)frames, ms.frame / divisor, ms.restyle / divisor,
92
+ ms.layout / divisor, ms.paint / divisor, ms.upload / divisor, ms.draw / divisor,
93
+ (unsigned long long)counters.restyles, (unsigned long long)counters.restyleNodes,
94
+ (unsigned long long)counters.rulesEvaluated,
95
+ (unsigned long long)counters.mediaEvaluated, (unsigned long long)counters.layouts,
96
+ (unsigned long long)counters.layoutBoxes,
97
+ (unsigned long long)counters.textShapeCalls,
98
+ (unsigned long long)counters.textShapeHits, (unsigned long long)counters.paintOps,
99
+ (unsigned long long)counters.drawRuns, (unsigned long long)counters.drawCalls,
100
+ (unsigned long long)counters.uploadSubmits, (unsigned long long)counters.vertices,
101
+ (unsigned long long)counters.syncNodes, (unsigned long long)counters.presents,
102
+ (unsigned long long)counters.exposes,
103
+ (unsigned long long)counters.deferred,
104
+ (unsigned long long)counters.starvedFrames,
105
+ (unsigned long long)counters.skippedFrames,
106
+ (unsigned long long)counters.canvasDraws,
107
+ (unsigned long long)counters.canvasUploads,
108
+ (unsigned long long)counters.canvasUploadBytes,
109
+ (unsigned long long)counters.webglDraws,
110
+ (unsigned long long)counters.webglPasses,
111
+ (unsigned long long)counters.webglPipelineMisses,
112
+ (unsigned long long)counters.webglBufferUploadBytes);
113
+ }
114
+
115
+ } // namespace
116
+
117
+ bool xt_perf_active() {
118
+ readMode();
119
+ return g_perf_mode != 0;
120
+ }
121
+
122
+ uint64_t xt_perf_counter() { return SDL_GetPerformanceCounter(); }
123
+
124
+ double xt_perf_ms_since(uint64_t start) {
125
+ uint64_t frequency = SDL_GetPerformanceFrequency();
126
+ if (frequency == 0 || start == 0) return 0.0;
127
+ return (double)(SDL_GetPerformanceCounter() - start) * 1000.0 / (double)frequency;
128
+ }
129
+
130
+ PerfScope::PerfScope(double *slot) {
131
+ /* Always time the phases: five counter pairs per frame are free, and
132
+ * `win.stats()` is then useful without the environment variable (which only
133
+ * controls printing). */
134
+ slot_ = slot;
135
+ if (slot_ != nullptr) start_ = SDL_GetPerformanceCounter();
136
+ }
137
+
138
+ PerfScope::~PerfScope() {
139
+ if (slot_ == nullptr) return;
140
+ *slot_ += xt_perf_ms_since(start_);
141
+ }
142
+
143
+ void xt_perf_frame_begin() {
144
+ g_perf_frame = PerfCounters();
145
+ g_perf_frame_ms = PerfTimings();
146
+ g_frame_start = SDL_GetPerformanceCounter();
147
+ }
148
+
149
+ void xt_perf_frame_end() {
150
+ g_perf_frame_ms.frame = xt_perf_ms_since(g_frame_start);
151
+ g_perf_last = g_perf_frame_ms;
152
+ g_perf_frame.frames = 1;
153
+ addCounters(&g_perf_total, g_perf_frame);
154
+
155
+ readMode();
156
+ if (g_perf_mode == 0) return;
157
+ if (g_perf_mode == 1) {
158
+ printLine(1, g_perf_frame_ms, g_perf_frame);
159
+ return;
160
+ }
161
+ addTimings(&g_agg_ms, g_perf_frame_ms);
162
+ addCounters(&g_agg_counters, g_perf_frame);
163
+ g_agg_frames += 1;
164
+ if (g_agg_frames < (uint64_t)g_perf_mode) return;
165
+ printLine(g_agg_frames, g_agg_ms, g_agg_counters);
166
+ g_agg_ms = PerfTimings();
167
+ g_agg_counters = PerfCounters();
168
+ g_agg_frames = 0;
169
+ }
170
+
171
+ } // namespace xtgui
@@ -0,0 +1,127 @@
1
+ /*
2
+ * xbintsc GUI engine — frame counters and phase timings.
3
+ *
4
+ * The engine is a single-threaded UI loop, so one process-wide record is
5
+ * enough. `xt_gui_render_window` brackets every frame with
6
+ * `xt_perf_frame_begin()` / `xt_perf_frame_end()`; the rest of the engine only
7
+ * bumps counters or wraps a phase in `PerfScope`. `win.stats()` exposes the
8
+ * last frame plus the cumulative totals, which is what
9
+ * `tests/e2e/gui-perf.test.ts` asserts on (wall-clock times are informational
10
+ * only, so the suite also runs on a software GPU in CI).
11
+ *
12
+ * Set `XT_GUI_PERF=1` to print one line per frame, or `XT_GUI_PERF=<N>` to
13
+ * print an aggregated line every N frames (average of the timings, sums of the
14
+ * counters). The counters and phase timings are always recorded (a few counter
15
+ * reads per frame), so `win.stats()` is useful without the variable; the
16
+ * environment variable only controls the stderr printing.
17
+ */
18
+ #ifndef XT_GUI_PERF_H
19
+ #define XT_GUI_PERF_H
20
+
21
+ #include <cstdint>
22
+
23
+ namespace xtgui {
24
+
25
+ /** Counters for one frame; the same layout is reused for cumulative totals. */
26
+ struct PerfCounters {
27
+ uint64_t frames = 0;
28
+ uint64_t presents = 0;
29
+ /** Presents the *window system* asked for (a map/expose/restore event), not
30
+ * the page. They are real work — the compositor lost the contents — but a
31
+ * quiet page must not be blamed for them: a window server sends its first
32
+ * `Expose` whenever it gets around to mapping the window, which on a busy CI
33
+ * runner can land long after the first frames. At most one is charged per
34
+ * frame (the events coalesce into a single present), so
35
+ * `presents - exposes - deferred` counts the presents only the page asked
36
+ * for. */
37
+ uint64_t exposes = 0;
38
+ /** Presents a starved frame had to defer: no swapchain image was available
39
+ * then (the GPU still held it, or the window was minimized), so the frame's
40
+ * paint was presented on a later frame. Those presents replay work built in
41
+ * an earlier frame, so they are not this frame's page work either. */
42
+ uint64_t deferred = 0;
43
+ /** Frames that asked to present and got no drawable target at all: SDL's
44
+ * non-blocking swapchain acquire had no image (the GPU still held it) or the
45
+ * device gave no command buffer. The invalidation stays pending (see
46
+ * `deferred`), so the content is presented on a later frame rather than lost;
47
+ * a climbing value means the compositor keeps refusing images and the window
48
+ * is not being updated. */
49
+ uint64_t starvedFrames = 0;
50
+ uint64_t skippedFrames = 0;
51
+ uint64_t restyles = 0;
52
+ uint64_t restyleNodes = 0;
53
+ uint64_t rulesEvaluated = 0;
54
+ uint64_t mediaEvaluated = 0;
55
+ uint64_t layouts = 0;
56
+ uint64_t layoutBoxes = 0;
57
+ uint64_t textShapeCalls = 0;
58
+ uint64_t textShapeHits = 0;
59
+ uint64_t paints = 0;
60
+ uint64_t paintOps = 0;
61
+ uint64_t drawRuns = 0;
62
+ uint64_t drawCalls = 0;
63
+ uint64_t uploadSubmits = 0;
64
+ uint64_t vertices = 0;
65
+ uint64_t syncNodes = 0;
66
+ uint64_t transitionFrames = 0;
67
+ /* Canvas 2D: CPU draws, texture uploads and the bytes they move. A canvas
68
+ * that is only redrawn in a small region should show a small `bytes` count. */
69
+ uint64_t canvasDraws = 0;
70
+ uint64_t canvasUploads = 0;
71
+ uint64_t canvasUploadBytes = 0;
72
+ /* WebGL: draw calls, render passes actually opened (a draw that has nothing
73
+ * to upload must not open a new pass), pipeline-cache misses and the bytes
74
+ * buffer updates copy to the GPU. */
75
+ uint64_t webglDraws = 0;
76
+ uint64_t webglPasses = 0;
77
+ uint64_t webglPipelineMisses = 0;
78
+ uint64_t webglBufferUploadBytes = 0;
79
+ };
80
+
81
+ /** Wall-clock phase timings in milliseconds. */
82
+ struct PerfTimings {
83
+ double frame = 0;
84
+ double restyle = 0;
85
+ double layout = 0;
86
+ double paint = 0;
87
+ double upload = 0;
88
+ double draw = 0;
89
+ };
90
+
91
+ /** The frame being recorded; reset by `xt_perf_frame_begin`. */
92
+ extern PerfCounters g_perf_frame;
93
+ /** Every frame since startup. */
94
+ extern PerfCounters g_perf_total;
95
+ /** Phase timings of the frame being recorded. */
96
+ extern PerfTimings g_perf_frame_ms;
97
+ /** Phase timings of the last completed frame. */
98
+ extern PerfTimings g_perf_last;
99
+
100
+ /** True when `XT_GUI_PERF` asks for timings/printing. Cached after first use. */
101
+ bool xt_perf_active();
102
+
103
+ /** Raw performance counter, for `PerfScope` / ad-hoc measurements. */
104
+ uint64_t xt_perf_counter();
105
+ /** Milliseconds elapsed since `start` (from `xt_perf_counter`). */
106
+ double xt_perf_ms_since(uint64_t start);
107
+
108
+ void xt_perf_frame_begin();
109
+ void xt_perf_frame_end();
110
+
111
+ /** Accumulate elapsed milliseconds into `*slot` while in scope. A no-op (beyond
112
+ * one branch) when `XT_GUI_PERF` is unset. */
113
+ class PerfScope {
114
+ public:
115
+ explicit PerfScope(double *slot);
116
+ ~PerfScope();
117
+ PerfScope(const PerfScope &) = delete;
118
+ PerfScope &operator=(const PerfScope &) = delete;
119
+
120
+ private:
121
+ double *slot_ = nullptr;
122
+ uint64_t start_ = 0;
123
+ };
124
+
125
+ } // namespace xtgui
126
+
127
+ #endif // XT_GUI_PERF_H
@@ -15,6 +15,7 @@
15
15
  #include <cstring>
16
16
 
17
17
  #include "paint.h"
18
+ #include "perf.h"
18
19
  #include "text.h"
19
20
 
20
21
  #include "renderer_shaders.h"
@@ -39,10 +40,32 @@ bool g_atlas_dirty = false;
39
40
  int g_atlas_dirty_x0 = 0, g_atlas_dirty_y0 = 0, g_atlas_dirty_x1 = 0, g_atlas_dirty_y1 = 0;
40
41
  bool g_atlas_full_logged = false;
41
42
 
43
+ std::vector<Vertex> g_shape_staging;
44
+ std::vector<TextVertex> g_text_staging;
45
+ std::vector<TextVertex> g_image_staging;
46
+
47
+ /** The command buffer an upload records into. With a batch, the frame's single
48
+ * command buffer is acquired lazily and submitted by `xt_gui_upload_end`;
49
+ * without one, each upload submits on its own. */
50
+ SDL_GPUCommandBuffer *uploadCommand(SDL_GPUDevice *device, XtGuiUploadBatch *batch) {
51
+ if (batch == nullptr) return SDL_AcquireGPUCommandBuffer(device);
52
+ if (batch->cmd == nullptr) batch->cmd = SDL_AcquireGPUCommandBuffer(batch->device);
53
+ return batch->cmd;
54
+ }
55
+
56
+ void uploadDone(SDL_GPUCommandBuffer *cmd, XtGuiUploadBatch *batch) {
57
+ if (batch != nullptr) {
58
+ batch->recorded += 1;
59
+ return;
60
+ }
61
+ SDL_SubmitGPUCommandBuffer(cmd);
62
+ g_perf_frame.uploadSubmits += 1;
63
+ }
64
+
42
65
  /** Upload `count` vertices (of `stride` bytes) into `geometry`, reusing or
43
66
  * growing its GPU buffer. Does nothing when `count` is zero. */
44
67
  void uploadVertices(SDL_GPUDevice *device, XtGuiGeometry *geometry, const void *data,
45
- uint32_t count, uint32_t stride) {
68
+ uint32_t count, uint32_t stride, XtGuiUploadBatch *batch) {
46
69
  geometry->vertex_count = count;
47
70
  if (count == 0) return;
48
71
  uint32_t bytes = count * stride;
@@ -61,29 +84,37 @@ void uploadVertices(SDL_GPUDevice *device, XtGuiGeometry *geometry, const void *
61
84
  return;
62
85
  }
63
86
 
64
- SDL_GPUTransferBufferCreateInfo transfer_info;
65
- memset(&transfer_info, 0, sizeof(transfer_info));
66
- transfer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
67
- transfer_info.size = bytes;
68
- SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(device, &transfer_info);
69
- if (transfer == nullptr) {
87
+ /* Reuse the geometry's staging buffer: a repaint used to create and release a
88
+ * transfer buffer for every vertex array, every frame. */
89
+ if (geometry->staging == nullptr || geometry->staging_capacity < bytes) {
90
+ if (geometry->staging != nullptr) SDL_ReleaseGPUTransferBuffer(device, geometry->staging);
91
+ SDL_GPUTransferBufferCreateInfo transfer_info;
92
+ memset(&transfer_info, 0, sizeof(transfer_info));
93
+ transfer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
94
+ transfer_info.size = bytes;
95
+ geometry->staging = SDL_CreateGPUTransferBuffer(device, &transfer_info);
96
+ geometry->staging_capacity = geometry->staging != nullptr ? bytes : 0;
97
+ }
98
+ if (geometry->staging == nullptr) {
70
99
  fprintf(stderr, "xt_gui: transfer buffer creation failed: %s\n", SDL_GetError());
71
100
  geometry->vertex_count = 0;
72
101
  return;
73
102
  }
74
- void *mapped = SDL_MapGPUTransferBuffer(device, transfer, false);
103
+ /* `cycle` rotates SDL's internal allocation while the previous frame's copy
104
+ * may still be in flight: the staging buffer is reused every frame, so the
105
+ * non-cycling map could overwrite data the GPU has not read yet. */
106
+ void *mapped = SDL_MapGPUTransferBuffer(device, geometry->staging, true);
75
107
  std::memcpy(mapped, data, bytes);
76
- SDL_UnmapGPUTransferBuffer(device, transfer);
108
+ SDL_UnmapGPUTransferBuffer(device, geometry->staging);
77
109
 
78
- SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(device);
110
+ SDL_GPUCommandBuffer *cmd = uploadCommand(device, batch);
79
111
  if (cmd == nullptr) {
80
- SDL_ReleaseGPUTransferBuffer(device, transfer);
81
112
  geometry->vertex_count = 0;
82
113
  return;
83
114
  }
84
115
  SDL_GPUCopyPass *copy = SDL_BeginGPUCopyPass(cmd);
85
116
  SDL_GPUTransferBufferLocation source;
86
- source.transfer_buffer = transfer;
117
+ source.transfer_buffer = geometry->staging;
87
118
  source.offset = 0;
88
119
  SDL_GPUBufferRegion destination;
89
120
  destination.buffer = geometry->buffer;
@@ -91,13 +122,26 @@ void uploadVertices(SDL_GPUDevice *device, XtGuiGeometry *geometry, const void *
91
122
  destination.size = bytes;
92
123
  SDL_UploadToGPUBuffer(copy, &source, &destination, false);
93
124
  SDL_EndGPUCopyPass(copy);
94
- SDL_SubmitGPUCommandBuffer(cmd);
95
- SDL_ReleaseGPUTransferBuffer(device, transfer);
125
+ uploadDone(cmd, batch);
126
+ g_perf_frame.vertices += count;
96
127
  }
97
128
 
98
129
  } // namespace renderer_detail
99
130
  } // namespace xtgui
100
131
 
132
+ void xt_gui_upload_begin(XtGuiUploadBatch *batch, SDL_GPUDevice *device) {
133
+ batch->device = device;
134
+ batch->cmd = nullptr;
135
+ batch->recorded = 0;
136
+ }
137
+
138
+ void xt_gui_upload_end(XtGuiUploadBatch *batch) {
139
+ if (batch == nullptr || batch->cmd == nullptr) return;
140
+ SDL_SubmitGPUCommandBuffer(batch->cmd);
141
+ batch->cmd = nullptr;
142
+ xtgui::g_perf_frame.uploadSubmits += 1;
143
+ }
144
+
101
145
  bool xt_gui_renderer_ensure(SDL_GPUDevice *device, SDL_Window *reference) {
102
146
  using namespace xtgui::renderer_detail;
103
147
  if (g_pipeline != nullptr) return true;
@@ -238,15 +282,21 @@ void xt_gui_geometry_destroy(SDL_GPUDevice *device, XtGuiGeometry *geometry) {
238
282
  SDL_ReleaseGPUBuffer(device, geometry->buffer);
239
283
  geometry->buffer = nullptr;
240
284
  }
285
+ if (geometry->staging != nullptr) {
286
+ SDL_ReleaseGPUTransferBuffer(device, geometry->staging);
287
+ geometry->staging = nullptr;
288
+ }
289
+ geometry->staging_capacity = 0;
241
290
  geometry->capacity_bytes = 0;
242
291
  geometry->vertex_count = 0;
243
292
  geometry->dirty = true;
244
293
  }
245
294
 
246
295
  void xt_gui_geometry_upload(SDL_GPUDevice *device, XtGuiGeometry *geometry,
247
- const xtgui::DisplayList &list) {
296
+ const xtgui::DisplayList &list, XtGuiUploadBatch *batch) {
248
297
  using namespace xtgui::renderer_detail;
249
- std::vector<Vertex> verts;
298
+ std::vector<Vertex> &verts = g_shape_staging;
299
+ verts.clear();
250
300
  verts.reserve(list.rects.size() * 6);
251
301
  geometry->op_counts.assign(list.rects.size(), 0);
252
302
  /* Walk painter order so each rectangle carries its op's clip path. */
@@ -256,7 +306,7 @@ void xt_gui_geometry_upload(SDL_GPUDevice *device, XtGuiGeometry *geometry,
256
306
  pushShape(verts, list.rects[op.index], op.clip, list);
257
307
  geometry->op_counts[op.index] = (uint32_t)verts.size() - before;
258
308
  }
259
- uploadVertices(device, geometry, verts.data(), (uint32_t)verts.size(), sizeof(Vertex));
309
+ uploadVertices(device, geometry, verts.data(), (uint32_t)verts.size(), sizeof(Vertex), batch);
260
310
  }
261
311
 
262
312
  void xt_gui_build_runs(XtGuiGeometry *shapes, XtGuiGeometry *texts, XtGuiGeometry *images,
@@ -310,6 +360,35 @@ void xt_gui_renderer_draw(SDL_GPUCommandBuffer *cmd, SDL_GPURenderPass *pass,
310
360
 
311
361
  int target_width = (int)(viewport_width * pixel_density + 0.5f);
312
362
  int target_height = (int)(viewport_height * pixel_density + 0.5f);
363
+ /* A run list alternates pipelines (background, text, background, …), so the
364
+ * same scissor rectangle and the same pipeline were re-bound for every run.
365
+ * Track what is bound and only issue the state change when it actually
366
+ * changes: a 1,000-row list has hundreds of runs. */
367
+ SDL_Rect bound_scissor;
368
+ bound_scissor.x = -1;
369
+ bound_scissor.y = -1;
370
+ bound_scissor.w = -1;
371
+ bound_scissor.h = -1;
372
+ int bound_pipeline = -1;
373
+ SDL_GPUTexture *bound_texture = nullptr;
374
+ auto bindPipeline = [&](int pipeline, SDL_GPUBuffer *buffer, SDL_GPUGraphicsPipeline *gpu) {
375
+ if (bound_pipeline == pipeline) return;
376
+ SDL_BindGPUGraphicsPipeline(pass, gpu);
377
+ SDL_GPUBufferBinding binding;
378
+ binding.buffer = buffer;
379
+ binding.offset = 0;
380
+ SDL_BindGPUVertexBuffers(pass, 0, &binding, 1);
381
+ bound_pipeline = pipeline;
382
+ bound_texture = nullptr; // a new pipeline invalidates the bound sampler
383
+ };
384
+ auto bindTexture = [&](SDL_GPUTexture *texture) {
385
+ if (bound_texture == texture) return;
386
+ SDL_GPUTextureSamplerBinding texture_binding;
387
+ texture_binding.texture = texture;
388
+ texture_binding.sampler = g_sampler;
389
+ SDL_BindGPUFragmentSamplers(pass, 0, &texture_binding, 1);
390
+ bound_texture = texture;
391
+ };
313
392
  for (const XtGuiDrawRun &run : shapes->runs) {
314
393
  SDL_Rect scissor;
315
394
  scissor.x = 0;
@@ -330,42 +409,31 @@ void xt_gui_renderer_draw(SDL_GPUCommandBuffer *cmd, SDL_GPURenderPass *pass,
330
409
  scissor.w = x1 > x0 ? x1 - x0 : 0;
331
410
  scissor.h = y1 > y0 ? y1 - y0 : 0;
332
411
  }
333
- SDL_SetGPUScissor(pass, &scissor);
412
+ if (scissor.x != bound_scissor.x || scissor.y != bound_scissor.y ||
413
+ scissor.w != bound_scissor.w || scissor.h != bound_scissor.h) {
414
+ SDL_SetGPUScissor(pass, &scissor);
415
+ bound_scissor = scissor;
416
+ }
334
417
 
335
418
  if (run.pipeline == XtGuiDrawRun::Shape) {
336
419
  if (g_pipeline == nullptr || shapes->buffer == nullptr) continue;
337
- SDL_BindGPUGraphicsPipeline(pass, g_pipeline);
338
- SDL_GPUBufferBinding binding;
339
- binding.buffer = shapes->buffer;
340
- binding.offset = 0;
341
- SDL_BindGPUVertexBuffers(pass, 0, &binding, 1);
420
+ bindPipeline(XtGuiDrawRun::Shape, shapes->buffer, g_pipeline);
342
421
  SDL_DrawGPUPrimitives(pass, run.count, 1, run.first, 0);
422
+ xtgui::g_perf_frame.drawCalls += 1;
343
423
  } else if (run.pipeline == XtGuiDrawRun::Text) {
344
424
  if (!g_text_ok || g_atlas_texture == nullptr || texts->buffer == nullptr) continue;
345
- SDL_BindGPUGraphicsPipeline(pass, g_text_pipeline);
346
- SDL_GPUBufferBinding binding;
347
- binding.buffer = texts->buffer;
348
- binding.offset = 0;
349
- SDL_BindGPUVertexBuffers(pass, 0, &binding, 1);
350
- SDL_GPUTextureSamplerBinding atlas_binding;
351
- atlas_binding.texture = g_atlas_texture;
352
- atlas_binding.sampler = g_sampler;
353
- SDL_BindGPUFragmentSamplers(pass, 0, &atlas_binding, 1);
425
+ bindPipeline(XtGuiDrawRun::Text, texts->buffer, g_text_pipeline);
426
+ bindTexture(g_atlas_texture);
354
427
  SDL_DrawGPUPrimitives(pass, run.count, 1, run.first, 0);
428
+ xtgui::g_perf_frame.drawCalls += 1;
355
429
  } else {
356
430
  if (g_image_pipeline == nullptr || g_sampler == nullptr || images->buffer == nullptr ||
357
431
  run.texture == nullptr)
358
432
  continue;
359
- SDL_BindGPUGraphicsPipeline(pass, g_image_pipeline);
360
- SDL_GPUBufferBinding binding;
361
- binding.buffer = images->buffer;
362
- binding.offset = 0;
363
- SDL_BindGPUVertexBuffers(pass, 0, &binding, 1);
364
- SDL_GPUTextureSamplerBinding texture_binding;
365
- texture_binding.texture = run.texture;
366
- texture_binding.sampler = g_sampler;
367
- SDL_BindGPUFragmentSamplers(pass, 0, &texture_binding, 1);
433
+ bindPipeline(XtGuiDrawRun::Image, images->buffer, g_image_pipeline);
434
+ bindTexture(run.texture);
368
435
  SDL_DrawGPUPrimitives(pass, run.count, 1, run.first, 0);
436
+ xtgui::g_perf_frame.drawCalls += 1;
369
437
  }
370
438
  }
371
439