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
@@ -0,0 +1,217 @@
1
+ /*
2
+ * xbintsc GUI engine — WOFF and WOFF2 decompression for author `@font-face`
3
+ * sources.
4
+ *
5
+ * Only `webfont_bridge.c` references `xt_webfont_feature_register`, so this
6
+ * translation unit (and the miniz/Brotli/woff2 members it calls) is extracted
7
+ * from `gui.a` only when the `webfonts` link feature is active.
8
+ *
9
+ * WOFF (W3C WOFF 1.0) is the sfnt table directory with each table individually
10
+ * zlib-compressed, so this unit rebuilds a plain sfnt from the decompressed
11
+ * tables. WOFF2 (W3C WOFF 2.0) stores one Brotli stream of
12
+ * *transformed* tables (glyf/loca and hmtx are re-encoded); reconstructing that
13
+ * is the woff2 reference decoder's job, and its output is already a complete
14
+ * sfnt file.
15
+ */
16
+
17
+ #include "text_font_bytes.h"
18
+
19
+ #include <cstdint>
20
+ #include <cstring>
21
+ #include <string>
22
+ #include <vector>
23
+
24
+ #include "miniz_tinfl.h"
25
+ #include <woff2/decode.h>
26
+
27
+ namespace xtgui {
28
+ namespace {
29
+
30
+ constexpr uint32_t tagOf(char a, char b, char c, char d) {
31
+ return ((uint32_t)(unsigned char)a << 24) | ((uint32_t)(unsigned char)b << 16) |
32
+ ((uint32_t)(unsigned char)c << 8) | (uint32_t)(unsigned char)d;
33
+ }
34
+
35
+ constexpr uint32_t kTagWoff = tagOf('w', 'O', 'F', 'F');
36
+ constexpr uint32_t kTagWoff2 = tagOf('w', 'O', 'F', '2');
37
+
38
+ /** Decompression-bomb guard: a real font is far below this. WOFF declares the
39
+ * uncompressed size up front, and the rebuilt table set is checked against the
40
+ * same bound. */
41
+ constexpr size_t kMaxSfntSize = 64u * 1024u * 1024u;
42
+
43
+ uint16_t be16(const unsigned char *data) {
44
+ return (uint16_t)((uint16_t)data[0] << 8 | data[1]);
45
+ }
46
+
47
+ uint32_t be32(const unsigned char *data) {
48
+ return ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) | ((uint32_t)data[2] << 8) |
49
+ (uint32_t)data[3];
50
+ }
51
+
52
+ void put16(unsigned char *data, uint16_t value) {
53
+ data[0] = (unsigned char)(value >> 8);
54
+ data[1] = (unsigned char)value;
55
+ }
56
+
57
+ void put32(unsigned char *data, uint32_t value) {
58
+ data[0] = (unsigned char)(value >> 24);
59
+ data[1] = (unsigned char)(value >> 16);
60
+ data[2] = (unsigned char)(value >> 8);
61
+ data[3] = (unsigned char)value;
62
+ }
63
+
64
+ size_t paddedSize(size_t length) { return (length + 3u) & ~(size_t)3u; }
65
+
66
+ /** One WOFF table directory entry. */
67
+ struct WoffTable {
68
+ uint32_t tag = 0;
69
+ uint32_t offset = 0;
70
+ uint32_t compressed_length = 0;
71
+ uint32_t original_length = 0;
72
+ uint32_t checksum = 0;
73
+ };
74
+
75
+ /** Inflate one WOFF table. WOFF compresses each table with zlib (RFC 1950),
76
+ * not a raw DEFLATE stream, so the 2-byte header and Adler-32 trailer are part
77
+ * of the input. */
78
+ bool inflateTable(const unsigned char *source, size_t source_length, uint32_t original_length,
79
+ std::vector<unsigned char> *out) {
80
+ size_t inflated = 0;
81
+ void *heap = tinfl_decompress_mem_to_heap(source, source_length, &inflated,
82
+ TINFL_FLAG_PARSE_ZLIB_HEADER);
83
+ if (heap == nullptr) return false;
84
+ const bool ok = inflated == original_length;
85
+ if (ok) out->assign((const unsigned char *)heap, (const unsigned char *)heap + inflated);
86
+ MZ_FREE(heap);
87
+ return ok;
88
+ }
89
+
90
+ /** Rebuild a plain sfnt file from the WOFF table set. Every table keeps the
91
+ * checksum the WOFF directory recorded, and the data is written 4-byte aligned
92
+ * in directory order, which is what an sfnt reader expects. */
93
+ bool buildSfnt(uint32_t flavor, const std::vector<WoffTable> &tables,
94
+ const std::vector<std::vector<unsigned char>> &data,
95
+ std::vector<unsigned char> *out) {
96
+ const size_t count = tables.size();
97
+ if (count == 0 || count > 0x0fffu) return false;
98
+ size_t total = 12 + count * 16;
99
+ for (size_t i = 0; i < count; i++) {
100
+ if (data[i].size() != tables[i].original_length) return false;
101
+ total += paddedSize(data[i].size());
102
+ if (total > kMaxSfntSize) return false;
103
+ }
104
+
105
+ out->assign(total, 0);
106
+ unsigned char *sfnt = out->data();
107
+ put32(sfnt, flavor);
108
+ put16(sfnt + 4, (uint16_t)count);
109
+ int entry_selector = 0;
110
+ while (((size_t)1 << (entry_selector + 1)) <= count) entry_selector++;
111
+ const uint16_t search_range = (uint16_t)(((size_t)1 << entry_selector) * 16);
112
+ put16(sfnt + 6, search_range);
113
+ put16(sfnt + 8, (uint16_t)entry_selector);
114
+ put16(sfnt + 10, (uint16_t)(count * 16 - search_range));
115
+
116
+ size_t offset = 12 + count * 16;
117
+ for (size_t i = 0; i < count; i++) {
118
+ unsigned char *record = sfnt + 12 + i * 16;
119
+ put32(record, tables[i].tag);
120
+ put32(record + 4, tables[i].checksum);
121
+ put32(record + 8, (uint32_t)offset);
122
+ put32(record + 12, tables[i].original_length);
123
+ if (!data[i].empty()) std::memcpy(sfnt + offset, data[i].data(), data[i].size());
124
+ offset += paddedSize(data[i].size());
125
+ }
126
+ return true;
127
+ }
128
+
129
+ bool decodeWoff(const std::vector<unsigned char> &source, std::vector<unsigned char> *out) {
130
+ /* Fixed 44-byte header, then one 20-byte record per table. */
131
+ constexpr size_t kHeaderSize = 44;
132
+ constexpr size_t kRecordSize = 20;
133
+ const unsigned char *bytes = source.data();
134
+ if (source.size() < kHeaderSize) return false;
135
+
136
+ const uint32_t flavor = be32(bytes + 4);
137
+ const uint32_t declared_length = be32(bytes + 8);
138
+ const uint16_t count = be16(bytes + 12);
139
+ const uint32_t total_sfnt_size = be32(bytes + 16);
140
+ if (count == 0 || count > 0x0fffu) return false;
141
+ if (total_sfnt_size == 0 || total_sfnt_size > kMaxSfntSize) return false;
142
+ if (declared_length != 0 && declared_length > source.size()) return false;
143
+ if (kHeaderSize + (size_t)count * kRecordSize > source.size()) return false;
144
+
145
+ std::vector<WoffTable> tables(count);
146
+ std::vector<std::vector<unsigned char>> data(count);
147
+ for (size_t i = 0; i < count; i++) {
148
+ const unsigned char *record = bytes + kHeaderSize + i * kRecordSize;
149
+ WoffTable &table = tables[i];
150
+ table.tag = be32(record);
151
+ table.offset = be32(record + 4);
152
+ table.compressed_length = be32(record + 8);
153
+ table.original_length = be32(record + 12);
154
+ table.checksum = be32(record + 16);
155
+ if (table.original_length == 0 || table.compressed_length == 0) return false;
156
+ if (table.original_length > kMaxSfntSize) return false;
157
+ /* A table is stored uncompressed when its compressed size is not smaller. */
158
+ if (table.compressed_length > table.original_length) return false;
159
+ if (table.offset > source.size() ||
160
+ table.compressed_length > source.size() - table.offset) {
161
+ return false;
162
+ }
163
+ if (table.compressed_length == table.original_length) {
164
+ data[i].assign(bytes + table.offset, bytes + table.offset + table.compressed_length);
165
+ continue;
166
+ }
167
+ if (!inflateTable(bytes + table.offset, table.compressed_length, table.original_length,
168
+ &data[i])) {
169
+ return false;
170
+ }
171
+ }
172
+ return buildSfnt(flavor, tables, data, out);
173
+ }
174
+
175
+ bool decodeWoff2(const std::vector<unsigned char> &source, std::vector<unsigned char> *out) {
176
+ if (source.size() < 48) return false;
177
+ std::string decoded;
178
+ woff2::WOFF2StringOut writer(&decoded);
179
+ if (!woff2::ConvertWOFF2ToTTF(source.data(), source.size(), &writer)) return false;
180
+ out->assign(decoded.begin(), decoded.end());
181
+ return true;
182
+ }
183
+
184
+ /** The sfnt version tags a rebuilt container may start with. WOFF/WOFF2 cannot
185
+ * hold a collection, but accepting `ttcf` keeps a decoder bug from turning into
186
+ * a "corrupt font" report when the source really was one. */
187
+ bool looksLikeSfnt(const std::vector<unsigned char> &bytes) {
188
+ if (bytes.size() < 4) return false;
189
+ const uint32_t tag = be32(bytes.data());
190
+ return tag == 0x00010000u || tag == tagOf('t', 'r', 'u', 'e') ||
191
+ tag == tagOf('t', 'y', 'p', '1') || tag == tagOf('O', 'T', 'T', 'O') ||
192
+ tag == tagOf('t', 't', 'c', 'f');
193
+ }
194
+
195
+ bool decode(const std::vector<unsigned char> &source, std::vector<unsigned char> *out) {
196
+ if (source.size() < 4) return false;
197
+ const uint32_t tag = be32(source.data());
198
+ if (tag == kTagWoff) {
199
+ if (!decodeWoff(source, out)) return false;
200
+ } else if (tag == kTagWoff2) {
201
+ if (!decodeWoff2(source, out)) return false;
202
+ } else {
203
+ return false;
204
+ }
205
+ return looksLikeSfnt(*out);
206
+ }
207
+
208
+ } // namespace
209
+ } // namespace xtgui
210
+
211
+ /** Entry point referenced by `webfont_bridge.c`; it installs the decoder and,
212
+ * through that reference, makes the linker extract this object (and the
213
+ * miniz/Brotli/woff2 members) from `gui.a`. */
214
+ extern "C" void xt_webfont_feature_register(void) {
215
+ static const xtgui::WebfontDecoder kDecoder = {xtgui::decode};
216
+ xtgui::xt_font_webfont_install(&kDecoder);
217
+ }
@@ -178,6 +178,22 @@ void warnSelectorOnce(const std::string &selector) {
178
178
  truncated(selector, 80).c_str());
179
179
  }
180
180
 
181
+ void warnWebfontUnlinkedOnce(const std::string &path) {
182
+ static SeenNames seen;
183
+ if (!firstUse(&seen, path)) return;
184
+ std::fprintf(stderr,
185
+ "xt_gui: warning: '%s' is a WOFF/WOFF2 font and this program was built without "
186
+ "the 'webfonts' link feature; rebuild with --feature webfonts\n",
187
+ truncated(path, 120).c_str());
188
+ }
189
+
190
+ void warnWebfontInvalidOnce(const std::string &path) {
191
+ static SeenNames seen;
192
+ if (!firstUse(&seen, path)) return;
193
+ std::fprintf(stderr, "xt_gui: warning: cannot decode the WOFF/WOFF2 font '%s'; it is ignored\n",
194
+ truncated(path, 120).c_str());
195
+ }
196
+
181
197
  void warnPseudoClassOnce(const std::string &name) {
182
198
  static SeenNames seen;
183
199
  if (!firstUse(&seen, name)) return;
@@ -270,6 +286,16 @@ void xt_warn_unsupported_selector(const std::string &selector) {
270
286
  warnSelectorOnce(text);
271
287
  }
272
288
 
289
+ void xt_warn_webfont_unlinked(const std::string &path) {
290
+ if (warningsSuppressed() || path.empty()) return;
291
+ warnWebfontUnlinkedOnce(path);
292
+ }
293
+
294
+ void xt_warn_webfont_invalid(const std::string &path) {
295
+ if (warningsSuppressed() || path.empty()) return;
296
+ warnWebfontInvalidOnce(path);
297
+ }
298
+
273
299
  void xt_warn_unsupported_pseudo_class(const std::string &name) {
274
300
  if (warningsSuppressed() || name.empty()) return;
275
301
  warnPseudoClassOnce(hasUpperAscii(name) ? lowerAscii(name) : name);
@@ -36,6 +36,14 @@ void xt_warn_unsupported_at_rule(const std::string &name);
36
36
  /** Report an ignored `@font-face` descriptor once per descriptor name. */
37
37
  void xt_warn_unsupported_descriptor(const std::string &name);
38
38
 
39
+ /** Report a `.woff`/`.woff2` source that cannot be decoded because the program
40
+ * was built without the `webfonts` link feature, once per path. */
41
+ void xt_warn_webfont_unlinked(const std::string &path);
42
+
43
+ /** Report a `.woff`/`.woff2` source whose container cannot be decoded, once per
44
+ * path. The source is skipped like an unreadable file. */
45
+ void xt_warn_webfont_invalid(const std::string &path);
46
+
39
47
  /** Report a selector the parser had to drop. */
40
48
  void xt_warn_unsupported_selector(const std::string &selector);
41
49
 
@@ -0,0 +1,23 @@
1
+ /*
2
+ * xbintsc GUI engine — `webfonts` link feature registration bridge.
3
+ *
4
+ * Compiled and linked only when the compiler detects that the program may use a
5
+ * WOFF/WOFF2 `@font-face` source. Its constructor installs the decompressor and
6
+ * references it, which pulls `text_webfont.cpp` (and the miniz/Brotli/woff2
7
+ * members it calls) out of `gui.a`; without it a compressed font source is
8
+ * skipped and the diagnostic names the missing feature.
9
+ */
10
+
11
+ extern void xt_webfont_feature_register(void);
12
+
13
+ #if defined(__GNUC__) || defined(__clang__)
14
+ __attribute__((constructor)) static void xt_webfont_feature_boot(void) {
15
+ xt_webfont_feature_register();
16
+ }
17
+ #elif defined(_MSC_VER)
18
+ void xt_webfont_feature_boot(void);
19
+ #pragma section(".CRT$XCU", read)
20
+ __declspec(allocate(".CRT$XCU")) static void (*const xt_webfont_feature_ctor)(void) =
21
+ xt_webfont_feature_boot;
22
+ void xt_webfont_feature_boot(void) { xt_webfont_feature_register(); }
23
+ #endif
@@ -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,9 +465,25 @@ 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;
485
+ /** A fresh drawing buffer's depth attachment still needs its initial clear. */
486
+ bool default_depth_clear_pending_ = false;
493
487
  /** The multisampled colour target the default framebuffer draws into; every
494
488
  * pass resolves it into `color_`, which the page composites. */
495
489
  SDL_GPUTexture *color_msaa_ = nullptr;
@@ -528,6 +522,9 @@ class WebGLContext {
528
522
  int viewport_[4] = {0, 0, 0, 0};
529
523
  int scissor_box_[4] = {0, 0, 0, 0};
530
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;
531
528
 
532
529
  std::unordered_map<uint32_t, std::unique_ptr<WebGLShader>> shaders_;
533
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,
@@ -281,6 +281,7 @@ void WebGLContext::ensure_default_target() {
281
281
  if (default_samples_ <= 1) info.usage |= SDL_GPU_TEXTUREUSAGE_SAMPLER;
282
282
  info.sample_count = webgl_sample_count(default_samples_);
283
283
  depth_ = SDL_CreateGPUTexture(guidev::g_device, &info);
284
+ if (depth_ != nullptr) default_depth_clear_pending_ = true;
284
285
  if (depth_ == nullptr && default_samples_ > 1) {
285
286
  /* Do not mix a multisampled colour target with a depth-less pass: fall
286
287
  * back to the single-sampled drawing buffer instead. */
@@ -290,6 +291,7 @@ void WebGLContext::ensure_default_target() {
290
291
  info.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
291
292
  info.sample_count = SDL_GPU_SAMPLECOUNT_1;
292
293
  depth_ = SDL_CreateGPUTexture(guidev::g_device, &info);
294
+ if (depth_ != nullptr) default_depth_clear_pending_ = true;
293
295
  }
294
296
  if (depth_ == nullptr) {
295
297
  set_error(GL_OUT_OF_MEMORY);
@@ -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);