xbintsc 0.7.0 → 0.7.6

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 (323) hide show
  1. package/dist/src/ast/common.d.ts +10 -0
  2. package/dist/src/checker/source.js +4 -0
  3. package/dist/src/checker/source.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/main.js +15 -3
  6. package/dist/src/cli/main.js.map +1 -1
  7. package/dist/src/codegen/generator/calls/access.js +4 -11
  8. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  9. package/dist/src/codegen/generator/calls/element-inline.d.ts +42 -0
  10. package/dist/src/codegen/generator/calls/element-inline.js +388 -0
  11. package/dist/src/codegen/generator/calls/element-inline.js.map +1 -0
  12. package/dist/src/codegen/generator/calls/invocation.js +82 -1
  13. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  14. package/dist/src/codegen/generator/calls.d.ts +3 -1
  15. package/dist/src/codegen/generator/calls.js +3 -1
  16. package/dist/src/codegen/generator/calls.js.map +1 -1
  17. package/dist/src/codegen/generator/debug.js +20 -3
  18. package/dist/src/codegen/generator/debug.js.map +1 -1
  19. package/dist/src/codegen/generator/expressions/assignment.js +10 -2
  20. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  21. package/dist/src/codegen/generator/expressions/global-values.d.ts +19 -0
  22. package/dist/src/codegen/generator/expressions/global-values.js +49 -0
  23. package/dist/src/codegen/generator/expressions/global-values.js.map +1 -0
  24. package/dist/src/codegen/generator/expressions/primary.d.ts +0 -2
  25. package/dist/src/codegen/generator/expressions/primary.js +4 -24
  26. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  27. package/dist/src/codegen/generator/expressions.d.ts +2 -1
  28. package/dist/src/codegen/generator/expressions.js +2 -1
  29. package/dist/src/codegen/generator/expressions.js.map +1 -1
  30. package/dist/src/codegen/generator/layout.d.ts +65 -0
  31. package/dist/src/codegen/generator/layout.js +50 -0
  32. package/dist/src/codegen/generator/layout.js.map +1 -0
  33. package/dist/src/codegen/generator/state.js +4 -0
  34. package/dist/src/codegen/generator/state.js.map +1 -1
  35. package/dist/src/diagnostics/diagnostic.js +1 -1
  36. package/dist/src/diagnostics/diagnostic.js.map +1 -1
  37. package/dist/src/diagnostics/source-text.d.ts +37 -9
  38. package/dist/src/diagnostics/source-text.js +124 -23
  39. package/dist/src/diagnostics/source-text.js.map +1 -1
  40. package/dist/src/diagnostics/source.d.ts +16 -6
  41. package/dist/src/diagnostics/source.js +25 -4
  42. package/dist/src/diagnostics/source.js.map +1 -1
  43. package/dist/src/driver/build-key.d.ts +14 -0
  44. package/dist/src/driver/build-key.js +6 -1
  45. package/dist/src/driver/build-key.js.map +1 -1
  46. package/dist/src/driver/bundler/concatenate.js +11 -4
  47. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  48. package/dist/src/driver/bundler/graph.js +2 -2
  49. package/dist/src/driver/bundler/graph.js.map +1 -1
  50. package/dist/src/driver/bundler/merge.js +11 -5
  51. package/dist/src/driver/bundler/merge.js.map +1 -1
  52. package/dist/src/driver/bundler/reexport-resolution.js +3 -0
  53. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  54. package/dist/src/driver/compiler.js +13 -3
  55. package/dist/src/driver/compiler.js.map +1 -1
  56. package/dist/src/driver/frontend.js +3 -4
  57. package/dist/src/driver/frontend.js.map +1 -1
  58. package/dist/src/driver/runtime-objects.d.ts +1 -1
  59. package/dist/src/driver/runtime-objects.js +4 -0
  60. package/dist/src/driver/runtime-objects.js.map +1 -1
  61. package/dist/src/driver/toolchain.js +28 -5
  62. package/dist/src/driver/toolchain.js.map +1 -1
  63. package/dist/src/driver/version.d.ts +1 -1
  64. package/dist/src/driver/version.js +1 -1
  65. package/dist/src/extensions/gui/html.js +42 -10
  66. package/dist/src/extensions/gui/html.js.map +1 -1
  67. package/dist/src/extensions/gui/index.js +48 -0
  68. package/dist/src/extensions/gui/index.js.map +1 -1
  69. package/dist/src/extensions/registry.d.ts +20 -0
  70. package/dist/src/extensions/registry.js.map +1 -1
  71. package/dist/src/parser/expressions.js +2 -2
  72. package/dist/src/parser/expressions.js.map +1 -1
  73. package/dist/src/parser/helpers.d.ts +8 -0
  74. package/dist/src/parser/helpers.js +21 -0
  75. package/dist/src/parser/helpers.js.map +1 -1
  76. package/dist/tests/cli/main.test.js +9 -0
  77. package/dist/tests/cli/main.test.js.map +1 -1
  78. package/dist/tests/codegen/element-layout.test.d.ts +19 -0
  79. package/dist/tests/codegen/element-layout.test.js +80 -0
  80. package/dist/tests/codegen/element-layout.test.js.map +1 -0
  81. package/dist/tests/codegen/expressions.test.js +25 -0
  82. package/dist/tests/codegen/expressions.test.js.map +1 -1
  83. package/dist/tests/codegen/llvm.test.js +13 -0
  84. package/dist/tests/codegen/llvm.test.js.map +1 -1
  85. package/dist/tests/diagnostics/diagnostic.test.js +9 -0
  86. package/dist/tests/diagnostics/diagnostic.test.js.map +1 -1
  87. package/dist/tests/diagnostics/source-text.test.d.ts +1 -0
  88. package/dist/tests/diagnostics/source-text.test.js +50 -0
  89. package/dist/tests/diagnostics/source-text.test.js.map +1 -0
  90. package/dist/tests/diagnostics/source.test.js +17 -0
  91. package/dist/tests/diagnostics/source.test.js.map +1 -1
  92. package/dist/tests/driver/build-key.test.d.ts +1 -0
  93. package/dist/tests/driver/build-key.test.js +49 -0
  94. package/dist/tests/driver/build-key.test.js.map +1 -0
  95. package/dist/tests/driver/toolchain.test.js +8 -1
  96. package/dist/tests/driver/toolchain.test.js.map +1 -1
  97. package/dist/tests/driver/usage.test.js +2 -1
  98. package/dist/tests/driver/usage.test.js.map +1 -1
  99. package/dist/tests/e2e/async-await.test.js +16 -0
  100. package/dist/tests/e2e/async-await.test.js.map +1 -1
  101. package/dist/tests/e2e/compile-builtins.test.js +29 -0
  102. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  103. package/dist/tests/e2e/compile-modules.test.js +13 -0
  104. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  105. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  106. package/dist/tests/e2e/compile-prototype-chain.test.d.ts +8 -0
  107. package/dist/tests/e2e/compile-prototype-chain.test.js +24 -0
  108. package/dist/tests/e2e/compile-prototype-chain.test.js.map +1 -0
  109. package/dist/tests/e2e/differential-language.test.js +25 -0
  110. package/dist/tests/e2e/differential-language.test.js.map +1 -1
  111. package/dist/tests/e2e/differential-values.test.js +59 -0
  112. package/dist/tests/e2e/differential-values.test.js.map +1 -1
  113. package/dist/tests/e2e/gc.test.js +22 -0
  114. package/dist/tests/e2e/gc.test.js.map +1 -1
  115. package/dist/tests/e2e/gui-audio-extra.test.d.ts +12 -0
  116. package/dist/tests/e2e/gui-audio-extra.test.js +185 -0
  117. package/dist/tests/e2e/gui-audio-extra.test.js.map +1 -0
  118. package/dist/tests/e2e/gui-browser.test.js +35 -2
  119. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  120. package/dist/tests/e2e/gui-dom.test.js +88 -0
  121. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  122. package/dist/tests/e2e/gui-webgl-declarations.test.d.ts +13 -0
  123. package/dist/tests/e2e/gui-webgl-declarations.test.js +136 -0
  124. package/dist/tests/e2e/gui-webgl-declarations.test.js.map +1 -0
  125. package/dist/tests/e2e/gui-webgl-texture-units.test.d.ts +12 -0
  126. package/dist/tests/e2e/gui-webgl-texture-units.test.js +86 -0
  127. package/dist/tests/e2e/gui-webgl-texture-units.test.js.map +1 -0
  128. package/dist/tests/e2e/gui-webgl2-blit.test.js +6 -2
  129. package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -1
  130. package/dist/tests/e2e/gui-webgl2-msaa-float.test.d.ts +12 -0
  131. package/dist/tests/e2e/gui-webgl2-msaa-float.test.js +157 -0
  132. package/dist/tests/e2e/gui-webgl2-msaa-float.test.js.map +1 -0
  133. package/dist/tests/e2e/gui-webgl2-msaa.test.d.ts +10 -0
  134. package/dist/tests/e2e/gui-webgl2-msaa.test.js +344 -0
  135. package/dist/tests/e2e/gui-webgl2-msaa.test.js.map +1 -0
  136. package/dist/tests/e2e/gui-webgl2-target-orientation.test.d.ts +15 -0
  137. package/dist/tests/e2e/gui-webgl2-target-orientation.test.js +287 -0
  138. package/dist/tests/e2e/gui-webgl2-target-orientation.test.js.map +1 -0
  139. package/dist/tests/e2e/gui-webgl2.test.js +59 -1
  140. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
  141. package/dist/tests/e2e/hang-watchdog.test.d.ts +10 -0
  142. package/dist/tests/e2e/hang-watchdog.test.js +50 -0
  143. package/dist/tests/e2e/hang-watchdog.test.js.map +1 -0
  144. package/dist/tests/e2e/method-call-cache.test.d.ts +5 -0
  145. package/dist/tests/e2e/method-call-cache.test.js +65 -0
  146. package/dist/tests/e2e/method-call-cache.test.js.map +1 -0
  147. package/dist/tests/e2e/performance-now.test.d.ts +9 -0
  148. package/dist/tests/e2e/performance-now.test.js +36 -0
  149. package/dist/tests/e2e/performance-now.test.js.map +1 -0
  150. package/dist/tests/e2e/property-ic.test.js +33 -0
  151. package/dist/tests/e2e/property-ic.test.js.map +1 -1
  152. package/dist/tests/e2e/shape-interning.test.d.ts +16 -0
  153. package/dist/tests/e2e/shape-interning.test.js +55 -0
  154. package/dist/tests/e2e/shape-interning.test.js.map +1 -0
  155. package/dist/tests/e2e/string-utf16.test.js +9 -0
  156. package/dist/tests/e2e/string-utf16.test.js.map +1 -1
  157. package/dist/tests/e2e/typed-array-views.test.js +26 -0
  158. package/dist/tests/e2e/typed-array-views.test.js.map +1 -1
  159. package/dist/tests/e2e/url-search-params.test.js +19 -0
  160. package/dist/tests/e2e/url-search-params.test.js.map +1 -1
  161. package/dist/tests/extensions/gui.test.js +16 -1
  162. package/dist/tests/extensions/gui.test.js.map +1 -1
  163. package/dist/tests/helpers.js +1 -0
  164. package/dist/tests/helpers.js.map +1 -1
  165. package/dist/tests/parser/expressions.test.js +36 -0
  166. package/dist/tests/parser/expressions.test.js.map +1 -1
  167. package/doc/ai/contributing.md +8 -5
  168. package/doc/ai/extensions.md +12 -3
  169. package/doc/ai/language-support.md +2 -1
  170. package/doc/ai/troubleshooting.md +14 -0
  171. package/doc/ai/zh-CN/contributing.md +6 -3
  172. package/doc/ai/zh-CN/extensions.md +10 -2
  173. package/doc/ai/zh-CN/language-support.md +2 -1
  174. package/doc/ai/zh-CN/troubleshooting.md +12 -0
  175. package/doc/gui/api.md +26 -4
  176. package/doc/gui/dom.md +8 -0
  177. package/doc/gui/known-issues.md +35 -6
  178. package/doc/gui/runtime.md +16 -1
  179. package/doc/gui/webgl.md +97 -26
  180. package/doc/gui-scripts.md +2 -1
  181. package/doc/gui.md +8 -5
  182. package/doc/implemented/checker.md +9 -0
  183. package/doc/implemented/codegen.md +7 -0
  184. package/doc/implemented/parser.md +2 -1
  185. package/doc/implemented/runtime-core.md +9 -2
  186. package/doc/implemented/stdlib.md +10 -2
  187. package/doc/implemented/typed-arrays.md +18 -2
  188. package/doc/zh-CN/gui/api.md +19 -5
  189. package/doc/zh-CN/gui/dom.md +6 -0
  190. package/doc/zh-CN/gui/known-issues.md +24 -5
  191. package/doc/zh-CN/gui/runtime.md +13 -1
  192. package/doc/zh-CN/gui/webgl.md +20 -7
  193. package/doc/zh-CN/gui-scripts.md +2 -1
  194. package/doc/zh-CN/gui.md +4 -1
  195. package/doc/zh-CN/implemented/checker.md +7 -0
  196. package/doc/zh-CN/implemented/codegen.md +7 -0
  197. package/doc/zh-CN/implemented/parser.md +2 -1
  198. package/doc/zh-CN/implemented/runtime-core.md +9 -2
  199. package/doc/zh-CN/implemented/stdlib.md +6 -2
  200. package/doc/zh-CN/implemented/typed-arrays.md +14 -2
  201. package/package.json +7 -7
  202. package/runtime/ext_gui/audio.cpp +39 -3
  203. package/runtime/ext_gui/audio_extra.cpp +353 -0
  204. package/runtime/ext_gui/audio_extra.h +47 -0
  205. package/runtime/ext_gui/audio_fft.h +76 -0
  206. package/runtime/ext_gui/audio_internal.h +21 -0
  207. package/runtime/ext_gui/audio_mixer.cpp +46 -1
  208. package/runtime/ext_gui/audio_mixer.h +89 -1
  209. package/runtime/ext_gui/audio_mixer_extra.cpp +126 -0
  210. package/runtime/ext_gui/audio_nodes.cpp +242 -0
  211. package/runtime/ext_gui/audio_nodes_internal.h +81 -0
  212. package/runtime/ext_gui/audio_nodes_spatial.cpp +375 -0
  213. package/runtime/ext_gui/browser.cpp +9 -0
  214. package/runtime/ext_gui/browser_screen.cpp +73 -0
  215. package/runtime/ext_gui/dom_api_canvas.cpp +9 -1
  216. package/runtime/ext_gui/dom_api_internal.h +3 -0
  217. package/runtime/ext_gui/dom_api_node.cpp +21 -0
  218. package/runtime/ext_gui/dom_api_node_read.cpp +29 -0
  219. package/runtime/ext_gui/dom_api_proto.cpp +3 -0
  220. package/runtime/ext_gui/dom_api_webgl2.cpp +16 -11
  221. package/runtime/ext_gui/dom_api_webgl2_msaa.cpp +64 -0
  222. package/runtime/ext_gui/dom_api_webgl_compressed.cpp +2 -2
  223. package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +68 -11
  224. package/runtime/ext_gui/dom_api_webgl_methods.h +3 -0
  225. package/runtime/ext_gui/dom_api_webgl_state.cpp +4 -2
  226. package/runtime/ext_gui/dom_api_webgl_texture.cpp +6 -6
  227. package/runtime/ext_gui/gui.cpp +6 -1
  228. package/runtime/ext_gui/gui_engine.h +2 -0
  229. package/runtime/ext_gui/gui_events.cpp +13 -8
  230. package/runtime/ext_gui/gui_webgl.cpp +3 -2
  231. package/runtime/ext_gui/style_ua.cpp +4 -0
  232. package/runtime/ext_gui/warnings.cpp +1 -1
  233. package/runtime/ext_gui/webgl.h +102 -7
  234. package/runtime/ext_gui/webgl_api.h +3 -2
  235. package/runtime/ext_gui/webgl_blit.cpp +109 -15
  236. package/runtime/ext_gui/webgl_blit_color.cpp +264 -0
  237. package/runtime/ext_gui/webgl_blit_depth.cpp +26 -8
  238. package/runtime/ext_gui/webgl_buffer.cpp +20 -170
  239. package/runtime/ext_gui/webgl_context.cpp +117 -169
  240. package/runtime/ext_gui/webgl_draw.cpp +116 -5
  241. package/runtime/ext_gui/webgl_draw_buffers.cpp +21 -6
  242. package/runtime/ext_gui/webgl_draw_setup.cpp +1 -0
  243. package/runtime/ext_gui/webgl_draw_state.h +2 -0
  244. package/runtime/ext_gui/webgl_feature.cpp +2 -2
  245. package/runtime/ext_gui/webgl_feature.h +4 -3
  246. package/runtime/ext_gui/webgl_framebuffer.cpp +346 -0
  247. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +78 -47
  248. package/runtime/ext_gui/webgl_objects.h +16 -2
  249. package/runtime/ext_gui/webgl_pipeline.cpp +1 -1
  250. package/runtime/ext_gui/webgl_state.cpp +45 -36
  251. package/runtime/ext_gui/webgl_targets.cpp +440 -0
  252. package/runtime/ext_gui/webgl_texture.cpp +81 -25
  253. package/runtime/ext_gui/window.cpp +8 -0
  254. package/runtime/rt.h +27 -27
  255. package/runtime/rt_binary.h +44 -0
  256. package/runtime/rt_internal.h +52 -60
  257. package/runtime/rt_object.h +83 -0
  258. package/runtime/rt_profile.h +45 -0
  259. package/runtime/xt_alloc.c +88 -133
  260. package/runtime/xt_array_buffer.c +99 -71
  261. package/runtime/xt_builtins.c +3 -1
  262. package/runtime/xt_constructors.c +1 -0
  263. package/runtime/xt_containers/access.inc +96 -11
  264. package/runtime/xt_containers/arrays.inc +2 -1
  265. package/runtime/xt_containers/cstr_cache.inc +56 -0
  266. package/runtime/xt_containers/method_cache.inc +121 -0
  267. package/runtime/xt_containers/object_keys.inc +58 -0
  268. package/runtime/xt_containers/objects.inc +47 -79
  269. package/runtime/xt_containers/shapes.inc +153 -8
  270. package/runtime/xt_containers.c +4 -0
  271. package/runtime/xt_gc_release.c +70 -0
  272. package/runtime/xt_gc_stack.c +83 -0
  273. package/runtime/xt_gc_verify.c +1 -0
  274. package/runtime/xt_io/console.inc +66 -0
  275. package/runtime/xt_io/exception.inc +32 -6
  276. package/runtime/xt_io/watchdog.inc +101 -0
  277. package/runtime/xt_io.c +1 -0
  278. package/runtime/xt_loop.c +23 -3
  279. package/runtime/xt_pool.c +151 -0
  280. package/runtime/xt_profile.c +126 -0
  281. package/runtime/xt_stdlib/dispatch.inc +7 -1
  282. package/runtime/xt_stdlib/math.inc +272 -57
  283. package/runtime/xt_stdlib2/json_stringify.inc +1 -2
  284. package/runtime/xt_stdlib2/regexp-match.inc +17 -0
  285. package/runtime/xt_storage/indexed_db.c +4 -1
  286. package/runtime/xt_structured_clone.c +2 -1
  287. package/runtime/xt_typed_array/construction.inc +10 -0
  288. package/runtime/xt_typed_array/elements.inc +43 -23
  289. package/runtime/xt_url.c +50 -0
  290. package/runtime/xt_values/constructors.inc +4 -1
  291. package/runtime/xt_values/strings.inc +3 -1
  292. package/src/ast/common.ts +10 -0
  293. package/src/checker/source.ts +3 -0
  294. package/src/cli/main.ts +14 -3
  295. package/src/codegen/generator/calls/access.ts +5 -10
  296. package/src/codegen/generator/calls/element-inline.ts +433 -0
  297. package/src/codegen/generator/calls/invocation.ts +87 -1
  298. package/src/codegen/generator/calls.ts +5 -0
  299. package/src/codegen/generator/debug.ts +21 -3
  300. package/src/codegen/generator/expressions/assignment.ts +11 -2
  301. package/src/codegen/generator/expressions/global-values.ts +57 -0
  302. package/src/codegen/generator/expressions/primary.ts +4 -27
  303. package/src/codegen/generator/expressions.ts +3 -0
  304. package/src/codegen/generator/layout.ts +54 -0
  305. package/src/codegen/generator/state.ts +5 -0
  306. package/src/diagnostics/diagnostic.ts +1 -1
  307. package/src/diagnostics/source-text.ts +118 -22
  308. package/src/diagnostics/source.ts +29 -7
  309. package/src/driver/build-key.ts +21 -1
  310. package/src/driver/bundler/concatenate.ts +12 -5
  311. package/src/driver/bundler/graph.ts +2 -2
  312. package/src/driver/bundler/merge.ts +11 -4
  313. package/src/driver/bundler/reexport-resolution.ts +2 -0
  314. package/src/driver/compiler.ts +14 -3
  315. package/src/driver/frontend.ts +3 -4
  316. package/src/driver/runtime-objects.ts +4 -0
  317. package/src/driver/toolchain.ts +28 -5
  318. package/src/driver/version.ts +1 -1
  319. package/src/extensions/gui/html.ts +41 -11
  320. package/src/extensions/gui/index.ts +50 -1
  321. package/src/extensions/registry.ts +20 -0
  322. package/src/parser/expressions.ts +2 -2
  323. package/src/parser/helpers.ts +22 -0
@@ -0,0 +1,440 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGL render targets, resolves and render passes.
3
+ *
4
+ * A context renders either into the multisampled default drawing buffer (which
5
+ * every pass resolves into the composited colour texture), into a
6
+ * single-sampled default target, or into a user framebuffer whose colour and
7
+ * depth attachments may themselves be multisampled. This unit owns the target
8
+ * lookup, the scratch resolve texture, `readPixels`'s resolved source and the
9
+ * render pass itself; lifetime and object tables stay in `webgl_context.cpp`.
10
+ */
11
+
12
+ #include "webgl.h"
13
+
14
+ #include <algorithm>
15
+ #include <cstring>
16
+
17
+ #include "gui_internal.h"
18
+ #include "perf.h"
19
+
20
+ namespace xtgui {
21
+
22
+ namespace {
23
+
24
+ /** `XT_GUI_TRACE=1` prints the WebGL resolve/blit/render-pass sequence and the
25
+ * SDL error of the first failing call, for diagnosing a lost Vulkan device. */
26
+ bool trace_enabled() {
27
+ static int value = -1;
28
+ if (value < 0) {
29
+ const char *env = SDL_getenv("XT_GUI_TRACE");
30
+ value = (env != nullptr && env[0] != '\0' && env[0] != '0') ? 1 : 0;
31
+ }
32
+ return value != 0;
33
+ }
34
+
35
+ } // namespace
36
+
37
+ /* -- passes -------------------------------------------------------------- */
38
+
39
+ SDL_GPUTexture *WebGLContext::target_color() { return draw_color_texture(0); }
40
+
41
+ SDL_GPUTexture *WebGLContext::color_attachment(uint32_t id, int index, int *format,
42
+ int *samples, int *width, int *height) {
43
+ *format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
44
+ *samples = 1;
45
+ if (index < 0 || index >= kWebGLMaxDrawBuffers) return nullptr;
46
+ if (id == 0) {
47
+ ensure_default_target();
48
+ *width = width_;
49
+ *height = height_;
50
+ return color_;
51
+ }
52
+ WebGLFramebuffer *framebuffer_ = framebuffer(id);
53
+ if (framebuffer_ == nullptr) return nullptr;
54
+ *width = framebuffer_->width;
55
+ *height = framebuffer_->height;
56
+ if (framebuffer_->color_textures[index] != 0) {
57
+ WebGLTexture *texture_ = texture(framebuffer_->color_textures[index]);
58
+ if (texture_ == nullptr) return nullptr;
59
+ /* The attachment may have been bound before its storage was defined (three.js
60
+ * re-binds a render target's texture on every resize, then allocates it with
61
+ * `texImage2D`). Resolve the size from the attachment instead of the size
62
+ * cached when it was attached, or the framebuffer stays 0x0 and incomplete
63
+ * and every draw into it is silently dropped. */
64
+ if (texture_->width > 0 && texture_->height > 0) {
65
+ framebuffer_->width = texture_->width;
66
+ framebuffer_->height = texture_->height;
67
+ *width = texture_->width;
68
+ *height = texture_->height;
69
+ }
70
+ *format = (int)texture_->format;
71
+ *samples = texture_->samples;
72
+ return ensure_texture_gpu(texture_, true);
73
+ }
74
+ if (framebuffer_->color_renderbuffers[index] != 0) {
75
+ WebGLRenderbuffer *renderbuffer_ = renderbuffer(framebuffer_->color_renderbuffers[index]);
76
+ if (renderbuffer_ == nullptr) return nullptr;
77
+ if (renderbuffer_->width > 0 && renderbuffer_->height > 0) {
78
+ framebuffer_->width = renderbuffer_->width;
79
+ framebuffer_->height = renderbuffer_->height;
80
+ *width = renderbuffer_->width;
81
+ *height = renderbuffer_->height;
82
+ }
83
+ *format = (int)renderbuffer_->format;
84
+ *samples = renderbuffer_->samples;
85
+ return renderbuffer_->gpu;
86
+ }
87
+ return nullptr;
88
+ }
89
+
90
+ SDL_GPUTexture *WebGLContext::framebuffer_color(uint32_t id) {
91
+ int format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
92
+ int samples = 1;
93
+ int width = 0;
94
+ int height = 0;
95
+ return color_attachment(id, 0, &format, &samples, &width, &height);
96
+ }
97
+
98
+ void WebGLContext::framebuffer_size(uint32_t id, int *width, int *height) {
99
+ if (id == 0) {
100
+ *width = width_;
101
+ *height = height_;
102
+ return;
103
+ }
104
+ WebGLFramebuffer *framebuffer_ = framebuffer(id);
105
+ if (framebuffer_ == nullptr) {
106
+ *width = 0;
107
+ *height = 0;
108
+ return;
109
+ }
110
+ /* Read the attachment's real size: a target re-bound after a resize only knows
111
+ * it once its storage exists, not from the size cached at attach time. This
112
+ * must not allocate anything, so it reads the records directly. */
113
+ for (int index = 0; index < kWebGLMaxDrawBuffers; index++) {
114
+ if (framebuffer_->color_textures[index] != 0) {
115
+ WebGLTexture *texture_ = texture(framebuffer_->color_textures[index]);
116
+ if (texture_ != nullptr && texture_->width > 0 && texture_->height > 0) {
117
+ framebuffer_->width = texture_->width;
118
+ framebuffer_->height = texture_->height;
119
+ break;
120
+ }
121
+ }
122
+ if (framebuffer_->color_renderbuffers[index] != 0) {
123
+ WebGLRenderbuffer *renderbuffer_ = renderbuffer(framebuffer_->color_renderbuffers[index]);
124
+ if (renderbuffer_ != nullptr && renderbuffer_->width > 0 && renderbuffer_->height > 0) {
125
+ framebuffer_->width = renderbuffer_->width;
126
+ framebuffer_->height = renderbuffer_->height;
127
+ break;
128
+ }
129
+ }
130
+ }
131
+ *width = framebuffer_->width;
132
+ *height = framebuffer_->height;
133
+ }
134
+
135
+ SDL_GPUTexture *WebGLContext::target_depth() {
136
+ return framebuffer_depth(bound_framebuffer_);
137
+ }
138
+
139
+ SDL_GPUTexture *WebGLContext::framebuffer_depth(uint32_t id) {
140
+ if (id == 0) {
141
+ ensure_default_target();
142
+ return depth_;
143
+ }
144
+ WebGLFramebuffer *framebuffer = this->framebuffer(id);
145
+ if (framebuffer == nullptr) return nullptr;
146
+ if (framebuffer->depth_renderbuffer != 0) {
147
+ WebGLRenderbuffer *renderbuffer_ = renderbuffer(framebuffer->depth_renderbuffer);
148
+ return renderbuffer_ != nullptr ? renderbuffer_->gpu : nullptr;
149
+ }
150
+ if (framebuffer->depth_texture != 0) {
151
+ WebGLTexture *texture = this->texture(framebuffer->depth_texture);
152
+ if (texture == nullptr) return nullptr;
153
+ texture->as_depth_target = true;
154
+ return ensure_texture_gpu(texture, true);
155
+ }
156
+ if (framebuffer->depth == nullptr && framebuffer->width > 0 && framebuffer->height > 0 &&
157
+ guidev::g_device != nullptr) {
158
+ SDL_GPUTextureCreateInfo info;
159
+ memset(&info, 0, sizeof(info));
160
+ info.type = SDL_GPU_TEXTURETYPE_2D;
161
+ info.format = SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT;
162
+ info.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET;
163
+ info.width = (Uint32)framebuffer->width;
164
+ info.height = (Uint32)framebuffer->height;
165
+ info.layer_count_or_depth = 1;
166
+ info.num_levels = 1;
167
+ /* The auto depth buffer must match the colour attachment's sample count or
168
+ * the render pass is invalid. */
169
+ info.sample_count = webgl_sample_count(framebuffer_samples(id));
170
+ framebuffer->depth = SDL_CreateGPUTexture(guidev::g_device, &info);
171
+ }
172
+ return framebuffer->depth;
173
+ }
174
+
175
+ int WebGLContext::target_color_format() {
176
+ int format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
177
+ int samples = 1;
178
+ int width = 0;
179
+ int height = 0;
180
+ return color_attachment(bound_framebuffer_, 0, &format, &samples, &width, &height) != nullptr
181
+ ? format
182
+ : SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
183
+ }
184
+
185
+ int WebGLContext::target_depth_format() {
186
+ if (bound_framebuffer_ != 0) {
187
+ WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
188
+ if (framebuffer != nullptr) {
189
+ if (framebuffer->depth_renderbuffer != 0) {
190
+ WebGLRenderbuffer *renderbuffer_ = renderbuffer(framebuffer->depth_renderbuffer);
191
+ if (renderbuffer_ != nullptr) return (int)renderbuffer_->format;
192
+ }
193
+ if (framebuffer->depth_texture != 0) {
194
+ WebGLTexture *texture = this->texture(framebuffer->depth_texture);
195
+ if (texture != nullptr) return (int)texture->format;
196
+ }
197
+ }
198
+ }
199
+ return SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT;
200
+ }
201
+
202
+ int WebGLContext::draw_color_samples(int slot) {
203
+ if (bound_framebuffer_ == 0) return default_samples_;
204
+ WebGLFramebuffer *framebuffer_ = framebuffer(bound_framebuffer_);
205
+ if (framebuffer_ == nullptr) return 1;
206
+ const int index = framebuffer_->draw_buffers[slot] - GL_COLOR_ATTACHMENT0;
207
+ if (index < 0 || index >= kWebGLMaxDrawBuffers) return 1;
208
+ int format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
209
+ int samples = 1;
210
+ int width = 0;
211
+ int height = 0;
212
+ return color_attachment(bound_framebuffer_, index, &format, &samples, &width, &height) !=
213
+ nullptr
214
+ ? samples
215
+ : 1;
216
+ }
217
+
218
+ int WebGLContext::framebuffer_samples(uint32_t id) {
219
+ if (id == 0) return default_samples_;
220
+ WebGLFramebuffer *framebuffer_ = framebuffer(id);
221
+ if (framebuffer_ == nullptr) return 1;
222
+ for (int index = 0; index < kWebGLMaxDrawBuffers; index++) {
223
+ int format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
224
+ int samples = 1;
225
+ int width = 0;
226
+ int height = 0;
227
+ if (color_attachment(id, index, &format, &samples, &width, &height) != nullptr) return samples;
228
+ }
229
+ return 1;
230
+ }
231
+
232
+ int WebGLContext::target_samples() {
233
+ if (bound_framebuffer_ == 0) return default_samples_;
234
+ for (int slot = 0; slot < kWebGLMaxDrawBuffers; slot++) {
235
+ if (draw_color_texture(slot) != nullptr) return draw_color_samples(slot);
236
+ }
237
+ return 1;
238
+ }
239
+
240
+ SDL_GPUTexture *WebGLContext::ensure_resolve_target(int width, int height,
241
+ SDL_GPUTextureFormat format) {
242
+ if (guidev::g_device == nullptr || width <= 0 || height <= 0) return nullptr;
243
+ if (resolve_texture_ != nullptr && resolve_width_ == width && resolve_height_ == height &&
244
+ resolve_format_ == format) {
245
+ return resolve_texture_;
246
+ }
247
+ if (resolve_texture_ != nullptr) {
248
+ submit();
249
+ SDL_WaitForGPUIdle(guidev::g_device);
250
+ SDL_ReleaseGPUTexture(guidev::g_device, resolve_texture_);
251
+ resolve_texture_ = nullptr;
252
+ }
253
+ SDL_GPUTextureCreateInfo info;
254
+ memset(&info, 0, sizeof(info));
255
+ info.type = SDL_GPU_TEXTURETYPE_2D;
256
+ info.format = format;
257
+ info.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
258
+ info.width = (Uint32)width;
259
+ info.height = (Uint32)height;
260
+ info.layer_count_or_depth = 1;
261
+ info.num_levels = 1;
262
+ info.sample_count = SDL_GPU_SAMPLECOUNT_1;
263
+ resolve_texture_ = SDL_CreateGPUTexture(guidev::g_device, &info);
264
+ if (resolve_texture_ == nullptr) {
265
+ set_error(GL_OUT_OF_MEMORY);
266
+ return nullptr;
267
+ }
268
+ resolve_width_ = width;
269
+ resolve_height_ = height;
270
+ resolve_format_ = format;
271
+ return resolve_texture_;
272
+ }
273
+
274
+ bool WebGLContext::resolve_color_pass(SDL_GPUTexture *source, SDL_GPUTexture *destination) {
275
+ if (source == nullptr || destination == nullptr || guidev::g_device == nullptr) return false;
276
+ end_pass();
277
+ ensure_command();
278
+ if (cmd_ == nullptr) return false;
279
+ SDL_GPUColorTargetInfo info;
280
+ memset(&info, 0, sizeof(info));
281
+ info.texture = source;
282
+ info.load_op = SDL_GPU_LOADOP_LOAD;
283
+ /* Resolve on store, and keep the multisampled contents: the source is a
284
+ * framebuffer a later draw may still read. */
285
+ info.store_op = SDL_GPU_STOREOP_RESOLVE_AND_STORE;
286
+ info.resolve_texture = destination;
287
+ info.resolve_mip_level = 0;
288
+ info.resolve_layer = 0;
289
+ SDL_GPURenderPass *pass = SDL_BeginGPURenderPass(cmd_, &info, 1, nullptr);
290
+ if (pass == nullptr) {
291
+ if (trace_enabled()) {
292
+ fprintf(stderr, "xt_gui: resolve pass failed: %s\n", SDL_GetError());
293
+ }
294
+ set_error(GL_INVALID_OPERATION);
295
+ return false;
296
+ }
297
+ SDL_EndGPURenderPass(pass);
298
+ /* The pass was not opened through `begin_pass`, so the prepared-draw cache
299
+ * must bind its pipeline again in the next pass. */
300
+ last_setup_ = nullptr;
301
+ last_pipeline_ = nullptr;
302
+ return true;
303
+ }
304
+
305
+ SDL_GPUTexture *WebGLContext::read_color_texture(uint32_t id, int *width, int *height,
306
+ int *format) {
307
+ if (width == nullptr || height == nullptr || format == nullptr) return nullptr;
308
+ if (id == 0) {
309
+ ensure_default_target();
310
+ *width = width_;
311
+ *height = height_;
312
+ *format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
313
+ return color_;
314
+ }
315
+ WebGLFramebuffer *framebuffer_ = framebuffer(id);
316
+ if (framebuffer_ == nullptr) return nullptr;
317
+ int index = read_buffer_ - GL_COLOR_ATTACHMENT0;
318
+ if (index < 0 || index >= kWebGLMaxDrawBuffers) index = 0;
319
+ int samples = 1;
320
+ SDL_GPUTexture *attachment = color_attachment(id, index, format, &samples, width, height);
321
+ if (attachment == nullptr) return nullptr;
322
+ if (trace_enabled()) {
323
+ fprintf(stderr,
324
+ "xt_gui: read_color fb=%u index=%d samples=%d format=%d %dx%d samples_fb=%d\n", id,
325
+ index, samples, *format, *width, *height, framebuffer_samples(id));
326
+ }
327
+ if (samples <= 1) return attachment;
328
+ SDL_GPUTexture *resolved =
329
+ ensure_resolve_target(*width, *height, (SDL_GPUTextureFormat)*format);
330
+ if (resolved == nullptr) return nullptr;
331
+ if (!resolve_color_pass(attachment, resolved)) return nullptr;
332
+ return resolved;
333
+ }
334
+
335
+ void WebGLContext::end_pass() {
336
+ if (pass_ != nullptr && cmd_ != nullptr) {
337
+ SDL_EndGPURenderPass(pass_);
338
+ pass_ = nullptr;
339
+ }
340
+ }
341
+
342
+ void WebGLContext::begin_pass(bool clear_color, bool clear_depth, bool clear_stencil) {
343
+ if (guidev::g_device == nullptr) return;
344
+ if (pass_ != nullptr && pass_targets_default_ == (bound_framebuffer_ == 0) &&
345
+ pass_framebuffer_ == bound_framebuffer_ && !clear_color && !clear_depth &&
346
+ !clear_stencil) {
347
+ return;
348
+ }
349
+ end_pass();
350
+ ensure_command();
351
+ if (cmd_ == nullptr) return;
352
+ SDL_GPUTexture *depth = target_depth();
353
+ SDL_GPUColorTargetInfo color_infos[kWebGLMaxDrawBuffers];
354
+ int color_count = 0;
355
+ int pass_samples = 1;
356
+ for (int slot = 0; slot < kWebGLMaxDrawBuffers; slot++) {
357
+ SDL_GPUTexture *color = draw_color_texture(slot);
358
+ if (color == nullptr) continue;
359
+ const int samples = draw_color_samples(slot);
360
+ if (color_count > 0 && samples != pass_samples) {
361
+ /* Vulkan requires every attachment of a pass to share its sample count;
362
+ * the framebuffer is incomplete, so the draw cannot run. */
363
+ set_error(GL_INVALID_FRAMEBUFFER_OPERATION);
364
+ return;
365
+ }
366
+ pass_samples = samples;
367
+ SDL_GPUColorTargetInfo &color_info = color_infos[color_count++];
368
+ memset(&color_info, 0, sizeof(color_info));
369
+ color_info.texture = color;
370
+ color_info.layer_or_depth_plane = (Uint32)draw_color_layer(slot);
371
+ color_info.clear_color.r = clear_color_[0];
372
+ color_info.clear_color.g = clear_color_[1];
373
+ color_info.clear_color.b = clear_color_[2];
374
+ color_info.clear_color.a = clear_color_[3];
375
+ color_info.load_op = clear_color ? SDL_GPU_LOADOP_CLEAR : SDL_GPU_LOADOP_LOAD;
376
+ color_info.store_op = SDL_GPU_STOREOP_STORE;
377
+ /* The default framebuffer resolves its multisampled colour target into the
378
+ * texture the page composites, at the end of every pass. */
379
+ if (bound_framebuffer_ == 0 && slot == 0 && color_msaa_ != nullptr && color_ != nullptr) {
380
+ color_info.resolve_texture = color_;
381
+ color_info.store_op = SDL_GPU_STOREOP_RESOLVE_AND_STORE;
382
+ }
383
+ }
384
+ if (color_count == 0) {
385
+ set_error(GL_INVALID_FRAMEBUFFER_OPERATION);
386
+ return;
387
+ }
388
+ if (depth != nullptr) {
389
+ int depth_samples = 1;
390
+ if (bound_framebuffer_ == 0) {
391
+ depth_samples = default_samples_;
392
+ } else {
393
+ WebGLFramebuffer *framebuffer_ = framebuffer(bound_framebuffer_);
394
+ if (framebuffer_ != nullptr && framebuffer_->depth_renderbuffer != 0) {
395
+ WebGLRenderbuffer *renderbuffer_ = renderbuffer(framebuffer_->depth_renderbuffer);
396
+ if (renderbuffer_ != nullptr) depth_samples = renderbuffer_->samples;
397
+ } else if (framebuffer_ != nullptr && framebuffer_->depth_texture != 0) {
398
+ WebGLTexture *texture_ = texture(framebuffer_->depth_texture);
399
+ if (texture_ != nullptr) depth_samples = texture_->samples;
400
+ } else {
401
+ /* `framebuffer_depth` just created the auto depth buffer with the
402
+ * colour attachment's sample count. */
403
+ depth_samples = pass_samples;
404
+ }
405
+ }
406
+ if (depth_samples != pass_samples) {
407
+ set_error(GL_INVALID_FRAMEBUFFER_OPERATION);
408
+ return;
409
+ }
410
+ }
411
+
412
+ SDL_GPUDepthStencilTargetInfo depth_info;
413
+ memset(&depth_info, 0, sizeof(depth_info));
414
+ depth_info.texture = depth;
415
+ depth_info.clear_depth = clear_depth_;
416
+ depth_info.load_op = (clear_depth && depth != nullptr) ? SDL_GPU_LOADOP_CLEAR
417
+ : SDL_GPU_LOADOP_LOAD;
418
+ depth_info.store_op = SDL_GPU_STOREOP_STORE;
419
+ depth_info.clear_stencil = (Uint8)(clear_stencil_ & 0xFF);
420
+ depth_info.stencil_load_op = (clear_stencil && depth != nullptr) ? SDL_GPU_LOADOP_CLEAR
421
+ : SDL_GPU_LOADOP_LOAD;
422
+ depth_info.stencil_store_op = SDL_GPU_STOREOP_STORE;
423
+
424
+ pass_ = SDL_BeginGPURenderPass(cmd_, color_infos, (Uint32)color_count,
425
+ depth != nullptr ? &depth_info : nullptr);
426
+ if (pass_ == nullptr) {
427
+ if (trace_enabled()) {
428
+ fprintf(stderr, "xt_gui: begin_pass failed (fb=%u colors=%d samples=%d depth=%p): %s\n",
429
+ bound_framebuffer_, color_count, pass_samples, (void *)depth, SDL_GetError());
430
+ }
431
+ set_error(GL_INVALID_OPERATION);
432
+ return;
433
+ }
434
+ g_perf_frame.webglPasses += 1;
435
+ pass_serial_ += 1;
436
+ pass_targets_default_ = bound_framebuffer_ == 0;
437
+ pass_framebuffer_ = bound_framebuffer_;
438
+ }
439
+
440
+ } // namespace xtgui
@@ -109,6 +109,12 @@ bool WebGLContext::tex_image_format(uint32_t id, int width, int height,
109
109
  set_error(GL_INVALID_VALUE);
110
110
  return false;
111
111
  }
112
+ if (record->samples > 1) {
113
+ /* `texImage2D` cannot redefine a multisampled texture; it is allocated by
114
+ * `texStorage2DMultisample` and is a render target only. */
115
+ set_error(GL_INVALID_OPERATION);
116
+ return false;
117
+ }
112
118
  end_pass();
113
119
  ensure_command();
114
120
  if (cmd_ == nullptr) return false;
@@ -126,11 +132,15 @@ bool WebGLContext::tex_image_format(uint32_t id, int width, int height,
126
132
  record->target = layer >= 0 ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
127
133
  record->compressed = false;
128
134
  record->has_mipmaps = false;
135
+ record->samples = 1;
129
136
  if (record->sampler != nullptr) {
130
137
  SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
131
138
  record->sampler = nullptr;
132
139
  }
133
140
  }
141
+ /* `texImage2D` always defines a single-sampled texture; a multisampled one
142
+ * can only be created by `texStorage2DMultisample`. */
143
+ record->samples = 1;
134
144
  if (ensure_texture_gpu(record, record->as_target || record->as_depth_target) == nullptr) {
135
145
  return false;
136
146
  }
@@ -154,6 +164,11 @@ bool WebGLContext::tex_sub_image_format(uint32_t id, int x, int y, int width, in
154
164
  set_error(GL_INVALID_OPERATION);
155
165
  return false;
156
166
  }
167
+ if (record->samples > 1) {
168
+ /* A multisampled texture has no uploadable levels. */
169
+ set_error(GL_INVALID_OPERATION);
170
+ return false;
171
+ }
157
172
  if (x < 0 || y < 0 || width <= 0 || height <= 0 || x + width > record->width ||
158
173
  y + height > record->height) {
159
174
  set_error(GL_INVALID_VALUE);
@@ -196,33 +211,24 @@ bool WebGLContext::generate_mipmap(uint32_t id) {
196
211
 
197
212
  bool WebGLContext::read_pixels(int x, int y, int width, int height, uint8_t *out) {
198
213
  if (out == nullptr || width <= 0 || height <= 0) return false;
199
- if (target_color_format() != (int)SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM) {
214
+ if (bound_framebuffer_ != 0 && framebuffer_samples(bound_framebuffer_) > 1) {
215
+ /* ES 3.0 §4.3.1: reading a multisampled framebuffer directly is an error.
216
+ * The page resolves it with `blitFramebuffer` first. The default
217
+ * framebuffer is exempt because its resolve target is always current. */
218
+ set_error(GL_INVALID_OPERATION);
219
+ return false;
220
+ }
221
+ int target_width = 0;
222
+ int target_height = 0;
223
+ int target_format = 0;
224
+ SDL_GPUTexture *target = read_color_texture(bound_framebuffer_, &target_width, &target_height,
225
+ &target_format);
226
+ if (target_format != (int)SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM) {
200
227
  /* The only readback this surface offers is RGBA8; a float/half colour
201
228
  * target is read by rendering it into an RGBA8 target instead. */
202
229
  set_error(GL_INVALID_OPERATION);
203
230
  return false;
204
231
  }
205
- SDL_GPUTexture *target = target_color();
206
- if (bound_framebuffer_ != 0) {
207
- WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
208
- if (framebuffer != nullptr) {
209
- int index = read_buffer_ - GL_COLOR_ATTACHMENT0;
210
- if (index < 0 || index >= kWebGLMaxDrawBuffers ||
211
- framebuffer->color_textures[index] == 0) {
212
- index = 0;
213
- }
214
- WebGLTexture *read_texture = texture(framebuffer->color_textures[index]);
215
- target = read_texture != nullptr ? ensure_texture_gpu(read_texture, true) : nullptr;
216
- }
217
- }
218
- int target_width = bound_framebuffer_ == 0 ? width_ : 0;
219
- int target_height = bound_framebuffer_ == 0 ? height_ : 0;
220
- if (bound_framebuffer_ != 0) {
221
- WebGLFramebuffer *framebuffer_ = framebuffer(bound_framebuffer_);
222
- if (framebuffer_ == nullptr) return false;
223
- target_width = framebuffer_->width;
224
- target_height = framebuffer_->height;
225
- }
226
232
  if (target == nullptr || target_width <= 0 || target_height <= 0) return false;
227
233
  /* Clamp to the target; pixels outside are transparent black. */
228
234
  const int px = std::max(0, x);
@@ -249,9 +255,12 @@ bool WebGLContext::read_pixels(int x, int y, int width, int height, uint8_t *out
249
255
  SDL_GPUTextureRegion region;
250
256
  memset(&region, 0, sizeof(region));
251
257
  region.texture = target;
252
- /* The framebuffer is stored top-down; GL's y counts from the bottom. */
258
+ /* `readPixels`' y counts from GL's bottom edge; where that lands in storage
259
+ * depends on the target's convention (the drawing buffer is top-down, a user
260
+ * framebuffer is bottom-up). */
261
+ const bool top_down = webgl_target_top_down(bound_framebuffer_);
253
262
  region.x = (Uint32)px;
254
- region.y = (Uint32)(target_height - (py + ph));
263
+ region.y = (Uint32)webgl_target_rect_y(top_down, target_height, py, ph);
255
264
  region.w = (Uint32)pw;
256
265
  region.h = (Uint32)ph;
257
266
  region.d = 1;
@@ -277,7 +286,10 @@ bool WebGLContext::read_pixels(int x, int y, int width, int height, uint8_t *out
277
286
  return false;
278
287
  }
279
288
  for (int row = 0; row < ph; row++) {
280
- const uint8_t *source = mapped + (size_t)(ph - 1 - row) * pw * 4;
289
+ /* A top-down target stores GL's bottom row last, so the downloaded rows are
290
+ * reversed; a bottom-up user framebuffer is already in GL order. */
291
+ const int source_row = top_down ? ph - 1 - row : row;
292
+ const uint8_t *source = mapped + (size_t)source_row * pw * 4;
281
293
  uint8_t *line = out + ((size_t)(py - y + row) * width + (px - x)) * 4;
282
294
  memcpy(line, source, (size_t)pw * 4);
283
295
  }
@@ -310,6 +322,7 @@ bool WebGLContext::tex_storage_format(uint32_t id, int levels, int width, int he
310
322
  record->target = GL_TEXTURE_2D;
311
323
  record->compressed = false;
312
324
  record->has_mipmaps = false;
325
+ record->samples = 1;
313
326
  if (record->sampler != nullptr && guidev::g_device != nullptr) {
314
327
  SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
315
328
  record->sampler = nullptr;
@@ -318,6 +331,49 @@ bool WebGLContext::tex_storage_format(uint32_t id, int levels, int width, int he
318
331
  return ensure_texture_gpu(record, record->as_target || record->as_depth_target) != nullptr;
319
332
  }
320
333
 
334
+ bool WebGLContext::tex_storage_2d_multisample(uint32_t id, int samples,
335
+ SDL_GPUTextureFormat format, int width,
336
+ int height) {
337
+ WebGLTexture *record = texture(id);
338
+ if (record == nullptr) return false;
339
+ if (samples < 2 || samples > kWebGLMaxSamples) {
340
+ set_error(GL_INVALID_VALUE);
341
+ return false;
342
+ }
343
+ int granted_samples = samples;
344
+ if (!multisample_supported_ ||
345
+ !SDL_GPUTextureSupportsSampleCount(guidev::g_device, format,
346
+ webgl_sample_count(samples))) {
347
+ /* A backend without MSAA cannot resolve a multisampled texture; allocate
348
+ * the single-sampled storage the page can still attach and resolve. */
349
+ granted_samples = 1;
350
+ }
351
+ if (width <= 0 || height <= 0) {
352
+ set_error(GL_INVALID_VALUE);
353
+ return false;
354
+ }
355
+ end_pass();
356
+ ensure_command();
357
+ if (record->gpu != nullptr) {
358
+ submit();
359
+ SDL_WaitForGPUIdle(guidev::g_device);
360
+ SDL_ReleaseGPUTexture(guidev::g_device, record->gpu);
361
+ record->gpu = nullptr;
362
+ record->usage = 0;
363
+ }
364
+ record->width = width;
365
+ record->height = height;
366
+ record->format = format;
367
+ record->layers = 1;
368
+ record->target = GL_TEXTURE_2D;
369
+ record->compressed = false;
370
+ record->has_mipmaps = false;
371
+ record->samples = granted_samples;
372
+ record->as_target = true;
373
+ record->defined = true;
374
+ return ensure_texture_gpu(record, true) != nullptr;
375
+ }
376
+
321
377
  bool WebGLContext::tex_storage_2d(uint32_t id, int levels, int width, int height) {
322
378
  return tex_storage_format(id, levels, width, height,
323
379
  SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
@@ -345,6 +345,14 @@ static xt_value win_request_animation_frame(xt_value self, xt_value env, int32_t
345
345
  if (!XT_IS_FUNCTION(fn)) return xt_number(0);
346
346
  int id = win->next_animation_frame_id++;
347
347
  win->animation_frames.push_back(XtGuiAnimationFrame{id, fn});
348
+ /* The pump stops once a frame leaves the queue empty, so every request has
349
+ * to restart it — including one made through the *window method*, which is
350
+ * how three.js's `setAnimationLoop` asks (`self.requestAnimationFrame`).
351
+ * Only the bare global did this before, so a page that booted with no frame
352
+ * pending when the pump drained its queue never rendered again: the last
353
+ * presented frame stayed on screen (the Saltline page froze on its final
354
+ * "准备就绪" boot image). */
355
+ xt_browser_schedule_frame();
348
356
  return xt_number((double)id);
349
357
  }
350
358