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
@@ -25,6 +25,82 @@
25
25
  namespace xtgui {
26
26
  namespace {
27
27
 
28
+ /** `XT_GUI_TRACE=1` records the last draws of each submitted command buffer, so
29
+ * the buffer a lost Vulkan device was executing can be inspected. */
30
+ bool trace_enabled() {
31
+ static int value = -1;
32
+ if (value < 0) {
33
+ const char *env = SDL_getenv("XT_GUI_TRACE");
34
+ value = (env != nullptr && env[0] != '\0' && env[0] != '0') ? 1 : 0;
35
+ }
36
+ return value != 0;
37
+ }
38
+
39
+ struct DrawTrace {
40
+ uint32_t program;
41
+ uint32_t framebuffer;
42
+ int mode;
43
+ int first;
44
+ int count;
45
+ int instances;
46
+ int samples;
47
+ int color_format;
48
+ int depth_format;
49
+ };
50
+
51
+ constexpr int kTraceDraws = 64;
52
+ DrawTrace g_draw_trace[kTraceDraws];
53
+ int g_draw_trace_count = 0;
54
+ DrawTrace g_submitted_trace[kTraceDraws];
55
+ int g_submitted_trace_count = 0;
56
+
57
+ void trace_draw(const WebGLContext *context, uint32_t program, int mode, int first, int count,
58
+ int instances, int samples, int color_format, int depth_format) {
59
+ if (!trace_enabled()) return;
60
+ if (g_draw_trace_count >= kTraceDraws) {
61
+ memmove(g_draw_trace, g_draw_trace + 1, sizeof(DrawTrace) * (kTraceDraws - 1));
62
+ g_draw_trace_count = kTraceDraws - 1;
63
+ }
64
+ DrawTrace &entry = g_draw_trace[g_draw_trace_count++];
65
+ entry.program = program;
66
+ entry.framebuffer = context != nullptr ? context->bound_framebuffer_id() : 0;
67
+ entry.mode = mode;
68
+ entry.first = first;
69
+ entry.count = count;
70
+ entry.instances = instances;
71
+ entry.samples = samples;
72
+ entry.color_format = color_format;
73
+ entry.depth_format = depth_format;
74
+ (void)context;
75
+ }
76
+
77
+ } // namespace
78
+
79
+ } // namespace xtgui
80
+
81
+ extern "C" void xt_gui_trace_submit(int submitted) {
82
+ if (!xtgui::trace_enabled()) return;
83
+ if (submitted) {
84
+ memcpy(xtgui::g_submitted_trace, xtgui::g_draw_trace,
85
+ sizeof(xtgui::DrawTrace) * (size_t)xtgui::g_draw_trace_count);
86
+ xtgui::g_submitted_trace_count = xtgui::g_draw_trace_count;
87
+ xtgui::g_draw_trace_count = 0;
88
+ return;
89
+ }
90
+ fprintf(stderr, "xt_gui: last submitted buffer had %d draws:\n",
91
+ xtgui::g_submitted_trace_count);
92
+ for (int i = 0; i < xtgui::g_submitted_trace_count; i++) {
93
+ const xtgui::DrawTrace &d = xtgui::g_submitted_trace[i];
94
+ fprintf(stderr,
95
+ " draw prog=%u mode=%d first=%d count=%d inst=%d samples=%d fmt=%d dfmt=%d fbo=%u\n",
96
+ d.program, d.mode, d.first, d.count, d.instances, d.samples, d.color_format,
97
+ d.depth_format, d.framebuffer);
98
+ }
99
+ }
100
+
101
+ namespace xtgui {
102
+ namespace {
103
+
28
104
  int type_size(int type) {
29
105
  switch (type) {
30
106
  case GL_BYTE:
@@ -181,6 +257,7 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
181
257
  bound_framebuffer_ != 0 ? framebuffer(bound_framebuffer_) : nullptr;
182
258
  const int target_width = framebuffer_ != nullptr ? framebuffer_->width : width_;
183
259
  const int target_height = framebuffer_ != nullptr ? framebuffer_->height : height_;
260
+ const bool top_down = webgl_target_top_down(bound_framebuffer_);
184
261
  /* A pipeline bakes in the attachment formats; float colour targets, MRT
185
262
  * layouts and depth textures therefore need their own pipeline variants. */
186
263
  state_.color_target_count = 0;
@@ -190,6 +267,17 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
190
267
  }
191
268
  if (state_.color_target_count == 0) state_.color_target_count = 1;
192
269
  state_.depth_format = target_depth_format();
270
+ state_.sample_count = target_samples();
271
+
272
+ /* The viewport of a bottom-up target is flipped (see below), which also
273
+ * mirrors the winding the rasteriser sees. Flip the GL front-face comparison
274
+ * for such a target while its pipeline is prepared; the draw hash carries the
275
+ * effective value, so the two orientations get distinct pipelines, and the
276
+ * page's logical state is restored right after. */
277
+ const int requested_front_face = state_.front_face;
278
+ if (!top_down) {
279
+ state_.front_face = requested_front_face == GL_CW ? GL_CCW : GL_CW;
280
+ }
193
281
 
194
282
  WebGLDrawKey key;
195
283
  key.program = current_program_;
@@ -202,17 +290,31 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
202
290
  draw_state_hash(&key.state_hash, &key.state_hash2);
203
291
 
204
292
  const WebGLDrawSetup *setup = prepare_draw(key);
293
+ state_.front_face = requested_front_face;
205
294
  if (setup == nullptr) return;
206
295
 
207
296
  begin_pass(false, false, false);
208
297
  if (pass_ == nullptr) return;
209
298
 
299
+ /* GL window y counts from the target's bottom edge while SDL_GPU's viewport
300
+ * is top-down. The composited drawing buffer stores GL top at row 0, so a
301
+ * positive-height viewport at `height - (y + h)` reproduces GL. A user
302
+ * framebuffer stores GL row y at storage row y (shaders sample it with v = 0
303
+ * at the bottom), which needs the opposite y direction: SDL's Vulkan backend
304
+ * negates a viewport height and Metal flips a negative one, so passing the
305
+ * viewport from `y + h` down to `y` renders GL rows in place. */
210
306
  SDL_GPUViewport viewport;
211
307
  memset(&viewport, 0, sizeof(viewport));
212
308
  viewport.x = (float)viewport_[0];
213
- viewport.y = (float)(target_height - (viewport_[1] + viewport_[3]));
214
309
  viewport.w = (float)std::max(1, viewport_[2]);
215
- viewport.h = (float)std::max(1, viewport_[3]);
310
+ const int viewport_h = std::max(1, viewport_[3]);
311
+ if (top_down) {
312
+ viewport.y = (float)webgl_target_rect_y(true, target_height, viewport_[1], viewport_h);
313
+ viewport.h = (float)viewport_h;
314
+ } else {
315
+ viewport.y = (float)(viewport_[1] + viewport_h);
316
+ viewport.h = (float)-viewport_h;
317
+ }
216
318
  viewport.min_depth = 0.0f;
217
319
  viewport.max_depth = 1.0f;
218
320
  SDL_SetGPUViewport(pass_, &viewport);
@@ -224,7 +326,7 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
224
326
  scissor.h = target_height;
225
327
  if (scissor_test_ && scissor_box_[2] > 0 && scissor_box_[3] > 0) {
226
328
  scissor.x = scissor_box_[0];
227
- scissor.y = target_height - (scissor_box_[1] + scissor_box_[3]);
329
+ scissor.y = webgl_target_rect_y(top_down, target_height, scissor_box_[1], scissor_box_[3]);
228
330
  scissor.w = scissor_box_[2];
229
331
  scissor.h = scissor_box_[3];
230
332
  }
@@ -325,8 +427,13 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
325
427
  texture_id = bound_array_textures_[unit];
326
428
  }
327
429
  WebGLTexture *sampled = texture(texture_id);
328
- SDL_GPUTexture *gpu = sampled != nullptr ? ensure_texture_gpu(sampled, sampled->as_target)
329
- : nullptr;
430
+ /* A multisampled texture is not sampleable: binding one makes the
431
+ * texture incomplete, which reads as the fallback texel, exactly like a
432
+ * browser's incomplete texture. */
433
+ SDL_GPUTexture *gpu =
434
+ sampled != nullptr && sampled->samples <= 1
435
+ ? ensure_texture_gpu(sampled, sampled->as_target)
436
+ : nullptr;
330
437
  WebGLSampler *sampler_object = this->sampler(bound_samplers_[unit]);
331
438
  SDL_GPUSampler *sampler = nullptr;
332
439
  if (sampler_object != nullptr) {
@@ -355,9 +462,13 @@ void WebGLContext::draw(int mode, int first, int count, int index_type, int offs
355
462
  bind_samplers(program_->fs_sampler_uniforms, false);
356
463
 
357
464
  if (index_size > 0) {
465
+ trace_draw(this, current_program_, draw_mode, first, count, instances, state_.sample_count,
466
+ state_.color_target_count > 0 ? state_.color_formats[0] : 0, state_.depth_format);
358
467
  SDL_DrawGPUIndexedPrimitives(pass_, (Uint32)count, (Uint32)instances, (Uint32)first_index, 0,
359
468
  0);
360
469
  } else {
470
+ trace_draw(this, current_program_, draw_mode, first, count, instances, state_.sample_count,
471
+ state_.color_target_count > 0 ? state_.color_formats[0] : 0, state_.depth_format);
361
472
  SDL_DrawGPUPrimitives(pass_, (Uint32)count, (Uint32)instances, (Uint32)first, 0);
362
473
  }
363
474
  }
@@ -17,16 +17,23 @@ SDL_GPUTexture *WebGLContext::draw_color_texture(int slot) {
17
17
  if (slot < 0 || slot >= kWebGLMaxDrawBuffers) return nullptr;
18
18
  if (bound_framebuffer_ == 0) {
19
19
  ensure_default_target();
20
- return slot == 0 && default_draw_buffers_[0] == GL_BACK ? color_ : nullptr;
20
+ return slot == 0 && default_draw_buffers_[0] == GL_BACK ? color_msaa_ != nullptr ? color_msaa_ : color_
21
+ : nullptr;
21
22
  }
22
23
  WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
23
24
  if (framebuffer == nullptr) return nullptr;
24
25
  const int buffer = framebuffer->draw_buffers[slot];
25
26
  const int index = buffer - GL_COLOR_ATTACHMENT0;
26
27
  if (index < 0 || index >= kWebGLMaxDrawBuffers) return nullptr;
27
- if (framebuffer->color_textures[index] == 0) return nullptr;
28
- WebGLTexture *texture = this->texture(framebuffer->color_textures[index]);
29
- return texture != nullptr ? ensure_texture_gpu(texture, true) : nullptr;
28
+ if (framebuffer->color_textures[index] != 0) {
29
+ WebGLTexture *texture = this->texture(framebuffer->color_textures[index]);
30
+ return texture != nullptr ? ensure_texture_gpu(texture, true) : nullptr;
31
+ }
32
+ if (framebuffer->color_renderbuffers[index] != 0) {
33
+ WebGLRenderbuffer *renderbuffer = this->renderbuffer(framebuffer->color_renderbuffers[index]);
34
+ return renderbuffer != nullptr ? renderbuffer->gpu : nullptr;
35
+ }
36
+ return nullptr;
30
37
  }
31
38
 
32
39
  int WebGLContext::draw_color_format(int slot) {
@@ -37,8 +44,16 @@ int WebGLContext::draw_color_format(int slot) {
37
44
  if (index < 0 || index >= kWebGLMaxDrawBuffers) {
38
45
  return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
39
46
  }
40
- WebGLTexture *texture = this->texture(framebuffer->color_textures[index]);
41
- return texture != nullptr ? (int)texture->format : SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
47
+ if (framebuffer->color_textures[index] != 0) {
48
+ WebGLTexture *texture = this->texture(framebuffer->color_textures[index]);
49
+ return texture != nullptr ? (int)texture->format : SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
50
+ }
51
+ if (framebuffer->color_renderbuffers[index] != 0) {
52
+ WebGLRenderbuffer *renderbuffer = this->renderbuffer(framebuffer->color_renderbuffers[index]);
53
+ return renderbuffer != nullptr ? (int)renderbuffer->format
54
+ : SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
55
+ }
56
+ return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
42
57
  }
43
58
 
44
59
  int WebGLContext::draw_color_layer(int slot) {
@@ -223,6 +223,7 @@ void WebGLContext::draw_state_hash(uint64_t *first, uint64_t *second) const {
223
223
  mix((uint64_t)(uint32_t)state_.color_formats[i]);
224
224
  }
225
225
  mix((uint64_t)(uint32_t)state_.depth_format);
226
+ mix((uint64_t)(uint32_t)state_.sample_count);
226
227
 
227
228
  *first = h1;
228
229
  *second = h2;
@@ -54,6 +54,8 @@ struct WebGLDrawState {
54
54
  SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM};
55
55
  int color_target_count = 1;
56
56
  int depth_format = SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT;
57
+ /** Samples per texel of the render targets; a pipeline bakes it in. */
58
+ int sample_count = 1;
57
59
  };
58
60
 
59
61
  /**
@@ -16,8 +16,8 @@
16
16
 
17
17
  namespace {
18
18
 
19
- void *get(XtGuiWindow *win, const void *node, bool webgl2) {
20
- return xt_gui_webgl_get(win, static_cast<const xtgui::Node *>(node), webgl2);
19
+ void *get(XtGuiWindow *win, const void *node, bool webgl2, bool antialias) {
20
+ return xt_gui_webgl_get(win, static_cast<const xtgui::Node *>(node), webgl2, antialias);
21
21
  }
22
22
 
23
23
  void *find(XtGuiWindow *win, const void *node) {
@@ -13,9 +13,10 @@
13
13
  #include "webgl.h"
14
14
 
15
15
  /** The WebGL context of a `<canvas>`, creating it on first use. `webgl2`
16
- * selects the WebGL 2 API/prototype; a canvas keeps the first context kind it
17
- * asked for. */
18
- xtgui::WebGLContext *xt_gui_webgl_get(XtGuiWindow *win, const xtgui::Node *node, bool webgl2);
16
+ * selects the WebGL 2 API/prototype and `antialias` the context-creation
17
+ * attribute; a canvas keeps the first context kind it asked for. */
18
+ xtgui::WebGLContext *xt_gui_webgl_get(XtGuiWindow *win, const xtgui::Node *node, bool webgl2,
19
+ bool antialias = true);
19
20
  /** The existing WebGL context of `node`, or null (no side effects). */
20
21
  xtgui::WebGLContext *xt_gui_webgl_find(XtGuiWindow *win, const xtgui::Node *node);
21
22
  /** Drop a node's WebGL context (called when its handle is forgotten). */
@@ -0,0 +1,346 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGL framebuffer and renderbuffer objects.
3
+ *
4
+ * A framebuffer is a set of up to four colour attachments (texture or
5
+ * renderbuffer), an optional depth/stencil attachment and the draw-buffer
6
+ * table, plus an auto-created depth buffer when the page attaches none.
7
+ * Renderbuffers are engine-owned render targets so a page can ask for a
8
+ * multisampled colour or depth buffer without a texture. Completeness
9
+ * (dimensions and sample counts) is reported with the exact GL status enum.
10
+ */
11
+
12
+ #include "webgl.h"
13
+
14
+ #include <cstdio>
15
+ #include <cstring>
16
+
17
+ #include "gui_internal.h"
18
+
19
+ namespace xtgui {
20
+ namespace {
21
+
22
+ /** `XT_GUI_TRACE=1` explains an incomplete framebuffer. */
23
+ bool trace_enabled() {
24
+ static int value = -1;
25
+ if (value < 0) {
26
+ const char *env = SDL_getenv("XT_GUI_TRACE");
27
+ value = (env != nullptr && env[0] != '\0' && env[0] != '0') ? 1 : 0;
28
+ }
29
+ return value != 0;
30
+ }
31
+
32
+ } // namespace
33
+
34
+ /* -- framebuffers -------------------------------------------------------- */
35
+
36
+ uint32_t WebGLContext::create_framebuffer() {
37
+ auto record = std::make_unique<WebGLFramebuffer>();
38
+ record->id = next_object_id_++;
39
+ const uint32_t id = record->id;
40
+ framebuffers_[id] = std::move(record);
41
+ return id;
42
+ }
43
+
44
+ void WebGLContext::delete_framebuffer(uint32_t id) {
45
+ auto found = framebuffers_.find(id);
46
+ if (found == framebuffers_.end()) return;
47
+ if (bound_framebuffer_ == id) bind_framebuffer(GL_FRAMEBUFFER, 0);
48
+ if (read_framebuffer_ == id) read_framebuffer_ = 0;
49
+ if (found->second->depth != nullptr && found->second->depth_renderbuffer == 0 &&
50
+ guidev::g_device != nullptr) {
51
+ submit(); SDL_WaitForGPUIdle(guidev::g_device);
52
+ SDL_ReleaseGPUTexture(guidev::g_device, found->second->depth);
53
+ }
54
+ framebuffers_.erase(found);
55
+ }
56
+
57
+ void WebGLContext::bind_framebuffer(int target, uint32_t id) {
58
+ if (id != 0 && framebuffers_.find(id) == framebuffers_.end()) return;
59
+ if (target == GL_READ_FRAMEBUFFER) {
60
+ read_framebuffer_ = id;
61
+ read_buffer_ = id == 0 ? GL_BACK : GL_COLOR_ATTACHMENT0;
62
+ return;
63
+ }
64
+ if (target != GL_FRAMEBUFFER && target != GL_DRAW_FRAMEBUFFER) return;
65
+ if (id != bound_framebuffer_) end_pass();
66
+ bound_framebuffer_ = id;
67
+ framebuffer_version_ += 1;
68
+ if (target == GL_FRAMEBUFFER) read_framebuffer_ = id;
69
+ read_buffer_ = id == 0 ? GL_BACK : GL_COLOR_ATTACHMENT0;
70
+ }
71
+
72
+ bool WebGLContext::framebuffer_texture(uint32_t framebuffer_id, uint32_t texture_id,
73
+ int attachment) {
74
+ framebuffer_version_ += 1;
75
+ WebGLFramebuffer *record_framebuffer = framebuffer(framebuffer_id);
76
+ WebGLTexture *record = texture(texture_id);
77
+ if (record_framebuffer == nullptr) return false;
78
+ if (texture_id != 0 && record == nullptr) return false;
79
+ if (attachment == GL_DEPTH_ATTACHMENT || attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
80
+ record_framebuffer->depth_texture = texture_id;
81
+ record_framebuffer->depth_renderbuffer = 0;
82
+ if (record_framebuffer->depth != nullptr) {
83
+ submit(); SDL_WaitForGPUIdle(guidev::g_device);
84
+ SDL_ReleaseGPUTexture(guidev::g_device, record_framebuffer->depth);
85
+ record_framebuffer->depth = nullptr;
86
+ }
87
+ if (record != nullptr) {
88
+ record->as_depth_target = true;
89
+ ensure_texture_gpu(record, true);
90
+ /* Storage may not exist yet; color_attachment and framebuffer_size re-read
91
+ * the real size once it does, so never clobber a known size with 0. */
92
+ if (record->width > 0 && record->height > 0) {
93
+ record_framebuffer->width = record->width;
94
+ record_framebuffer->height = record->height;
95
+ }
96
+ }
97
+ return true;
98
+ }
99
+ const int color_index = attachment - GL_COLOR_ATTACHMENT0;
100
+ if (color_index < 0 || color_index >= kWebGLMaxDrawBuffers) return false;
101
+ record_framebuffer->color_textures[color_index] = texture_id;
102
+ record_framebuffer->color_renderbuffers[color_index] = 0;
103
+ if (record != nullptr) {
104
+ if (record->width > 0 && record->height > 0) {
105
+ record_framebuffer->width = record->width;
106
+ record_framebuffer->height = record->height;
107
+ }
108
+ ensure_texture_gpu(record, true);
109
+ }
110
+ return true;
111
+ }
112
+
113
+ bool WebGLContext::framebuffer_texture_layer(uint32_t framebuffer_id, uint32_t texture_id,
114
+ int attachment, int layer) {
115
+ if (!framebuffer_texture(framebuffer_id, texture_id, attachment)) return false;
116
+ const int index = attachment - GL_COLOR_ATTACHMENT0;
117
+ WebGLFramebuffer *record = framebuffer(framebuffer_id);
118
+ if (record != nullptr && index >= 0 && index < kWebGLMaxDrawBuffers) {
119
+ record->color_layers[index] = layer;
120
+ }
121
+ /* SDL_GPU 3.2 depth-stencil targets have no layer selector; the depth
122
+ * attachment still binds, without per-layer rendering. */
123
+ return true;
124
+ }
125
+
126
+ int WebGLContext::framebuffer_status(uint32_t id) {
127
+ WebGLFramebuffer *framebuffer_ = framebuffer(id);
128
+ if (framebuffer_ == nullptr) return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
129
+ auto incomplete = [&](int status, const char *why, int slot, uint32_t attachment) {
130
+ if (trace_enabled()) {
131
+ WebGLTexture *attachment_texture = attachment != 0 ? texture(attachment) : nullptr;
132
+ fprintf(stderr,
133
+ "xt_gui: fb %u incomplete (%d): %s slot=%d attachment=%u texture=%p texsize=%dx%d "
134
+ "texgpu=%p fbsize=%dx%d\n",
135
+ id, status, why, slot, attachment, (void *)attachment_texture,
136
+ attachment_texture != nullptr ? attachment_texture->width : -1,
137
+ attachment_texture != nullptr ? attachment_texture->height : -1,
138
+ attachment_texture != nullptr ? (void *)attachment_texture->gpu : nullptr,
139
+ framebuffer_->width, framebuffer_->height);
140
+ }
141
+ return status;
142
+ };
143
+ bool attached = false;
144
+ int width = 0;
145
+ int height = 0;
146
+ int samples = 1;
147
+ if (trace_enabled()) {
148
+ fprintf(stderr,
149
+ "xt_gui: fb %u status: color=[%u,%u,%u,%u] rb=[%u,%u,%u,%u] depth=%u/%u size=%dx%d\n",
150
+ id, framebuffer_->color_textures[0], framebuffer_->color_textures[1],
151
+ framebuffer_->color_textures[2], framebuffer_->color_textures[3],
152
+ framebuffer_->color_renderbuffers[0], framebuffer_->color_renderbuffers[1],
153
+ framebuffer_->color_renderbuffers[2], framebuffer_->color_renderbuffers[3],
154
+ framebuffer_->depth_texture, framebuffer_->depth_renderbuffer, framebuffer_->width,
155
+ framebuffer_->height);
156
+ }
157
+ for (int index = 0; index < kWebGLMaxDrawBuffers; index++) {
158
+ if (framebuffer_->color_textures[index] == 0 &&
159
+ framebuffer_->color_renderbuffers[index] == 0) {
160
+ continue;
161
+ }
162
+ int format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
163
+ int attachment_samples = 1;
164
+ int attachment_width = 0;
165
+ int attachment_height = 0;
166
+ if (color_attachment(id, index, &format, &attachment_samples, &attachment_width,
167
+ &attachment_height) == nullptr) {
168
+ return incomplete(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT, "colour attachment unresolved",
169
+ index, framebuffer_->color_textures[index]);
170
+ }
171
+ if (!attached) {
172
+ attached = true;
173
+ width = attachment_width;
174
+ height = attachment_height;
175
+ samples = attachment_samples;
176
+ } else if (attachment_samples != samples) {
177
+ return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
178
+ } else if (attachment_width != width || attachment_height != height) {
179
+ return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
180
+ }
181
+ }
182
+ if (framebuffer_->depth_renderbuffer != 0 || framebuffer_->depth_texture != 0 ||
183
+ framebuffer_->depth != nullptr) {
184
+ int depth_samples = 1;
185
+ int depth_width = framebuffer_->width;
186
+ int depth_height = framebuffer_->height;
187
+ if (framebuffer_->depth_renderbuffer != 0) {
188
+ WebGLRenderbuffer *renderbuffer_ = renderbuffer(framebuffer_->depth_renderbuffer);
189
+ if (renderbuffer_ == nullptr) return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
190
+ depth_samples = renderbuffer_->samples;
191
+ depth_width = renderbuffer_->width;
192
+ depth_height = renderbuffer_->height;
193
+ } else if (framebuffer_->depth_texture != 0) {
194
+ WebGLTexture *texture_ = texture(framebuffer_->depth_texture);
195
+ if (texture_ == nullptr) return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
196
+ depth_samples = texture_->samples;
197
+ depth_width = texture_->width;
198
+ depth_height = texture_->height;
199
+ }
200
+ if (!attached) {
201
+ attached = true;
202
+ width = depth_width;
203
+ height = depth_height;
204
+ samples = depth_samples;
205
+ } else if (depth_samples != samples) {
206
+ return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
207
+ } else if (depth_width != width || depth_height != height) {
208
+ return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
209
+ }
210
+ }
211
+ if (!attached) return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
212
+ if (width <= 0 || height <= 0) return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
213
+ return GL_FRAMEBUFFER_COMPLETE;
214
+ }
215
+
216
+ uint32_t WebGLContext::create_renderbuffer() {
217
+ auto record = std::make_unique<WebGLRenderbuffer>();
218
+ record->id = next_object_id_++;
219
+ const uint32_t id = record->id;
220
+ renderbuffers_[id] = std::move(record);
221
+ return id;
222
+ }
223
+
224
+ void WebGLContext::delete_renderbuffer(uint32_t id) {
225
+ auto found = renderbuffers_.find(id);
226
+ if (found == renderbuffers_.end()) return;
227
+ if (bound_renderbuffer_ == id) bound_renderbuffer_ = 0;
228
+ for (auto &entry : framebuffers_) {
229
+ if (entry.second->depth_renderbuffer == id) {
230
+ entry.second->depth_renderbuffer = 0;
231
+ entry.second->depth = nullptr;
232
+ }
233
+ for (uint32_t &color : entry.second->color_renderbuffers) {
234
+ if (color == id) color = 0;
235
+ }
236
+ }
237
+ if (found->second->gpu != nullptr && guidev::g_device != nullptr) {
238
+ submit(); SDL_WaitForGPUIdle(guidev::g_device);
239
+ SDL_ReleaseGPUTexture(guidev::g_device, found->second->gpu);
240
+ }
241
+ renderbuffers_.erase(found);
242
+ }
243
+
244
+ bool WebGLContext::renderbuffer_storage(uint32_t id, int width, int height, int internalformat,
245
+ int samples) {
246
+ WebGLRenderbuffer *record = renderbuffer(id);
247
+ if (record == nullptr) return false;
248
+ if (width <= 0 || height <= 0) {
249
+ set_error(GL_INVALID_VALUE);
250
+ return false;
251
+ }
252
+ if (samples != 0 && samples != 1 && samples != 2 && samples != kWebGLMaxSamples) {
253
+ set_error(GL_INVALID_VALUE);
254
+ return false;
255
+ }
256
+ const bool depth = internalformat == GL_DEPTH_COMPONENT ||
257
+ internalformat == GL_DEPTH_COMPONENT16 ||
258
+ internalformat == GL_DEPTH_COMPONENT24 ||
259
+ internalformat == GL_DEPTH_COMPONENT32F ||
260
+ internalformat == GL_DEPTH_STENCIL ||
261
+ internalformat == GL_DEPTH24_STENCIL8 ||
262
+ internalformat == GL_DEPTH32F_STENCIL8 ||
263
+ internalformat == GL_STENCIL_INDEX8;
264
+ /* One depth-stencil format serves every depth renderbuffer, so a pipeline
265
+ * built for the default framebuffer also renders into a user one. */
266
+ const SDL_GPUTextureFormat format =
267
+ depth ? SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT : webgl_storage_format(internalformat);
268
+ int granted_samples = samples > 1 ? samples : 1;
269
+ if (granted_samples > 1 &&
270
+ (!multisample_supported_ ||
271
+ !SDL_GPUTextureSupportsSampleCount(guidev::g_device, format,
272
+ webgl_sample_count(granted_samples)))) {
273
+ /* The backend cannot multisample this target. Degrade to the same
274
+ * single-sampled storage a plain `renderbufferStorage` would allocate, so a
275
+ * page that asks for MSAA still renders (without antialiasing) instead of
276
+ * losing the attachment; `RENDERBUFFER_SAMPLES` then reports 0. */
277
+ granted_samples = 1;
278
+ }
279
+ if (record->gpu != nullptr) {
280
+ submit(); SDL_WaitForGPUIdle(guidev::g_device);
281
+ SDL_ReleaseGPUTexture(guidev::g_device, record->gpu);
282
+ record->gpu = nullptr;
283
+ }
284
+ record->width = width;
285
+ record->height = height;
286
+ record->internalformat = internalformat;
287
+ record->samples = granted_samples;
288
+ record->depth = depth;
289
+ record->format = format;
290
+ SDL_GPUTextureCreateInfo info;
291
+ memset(&info, 0, sizeof(info));
292
+ info.type = SDL_GPU_TEXTURETYPE_2D;
293
+ info.format = format;
294
+ /* A single-sampled colour renderbuffer is also a valid blit source, so it
295
+ * carries SAMPLER usage; a multisampled one cannot be sampled and is
296
+ * resolved through a render pass instead. */
297
+ info.usage = depth ? SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET
298
+ : (SDL_GPU_TEXTUREUSAGE_COLOR_TARGET |
299
+ (record->samples > 1 ? 0 : SDL_GPU_TEXTUREUSAGE_SAMPLER));
300
+ info.width = (Uint32)width;
301
+ info.height = (Uint32)height;
302
+ info.layer_count_or_depth = 1;
303
+ info.num_levels = 1;
304
+ info.sample_count = webgl_sample_count(record->samples);
305
+ record->gpu = SDL_CreateGPUTexture(guidev::g_device, &info);
306
+ if (record->gpu == nullptr) {
307
+ set_error(GL_OUT_OF_MEMORY);
308
+ return false;
309
+ }
310
+ return true;
311
+ }
312
+
313
+ bool WebGLContext::framebuffer_renderbuffer(uint32_t framebuffer_id, int attachment,
314
+ uint32_t renderbuffer_id) {
315
+ framebuffer_version_ += 1;
316
+ WebGLFramebuffer *framebuffer_ = framebuffer(framebuffer_id);
317
+ WebGLRenderbuffer *record = renderbuffer(renderbuffer_id);
318
+ if (framebuffer_ == nullptr) return false;
319
+ if (renderbuffer_id != 0 && record == nullptr) return false;
320
+ if (attachment == GL_DEPTH_ATTACHMENT || attachment == GL_DEPTH_STENCIL_ATTACHMENT ||
321
+ attachment == GL_STENCIL_ATTACHMENT) {
322
+ framebuffer_->depth_renderbuffer = renderbuffer_id;
323
+ framebuffer_->depth_texture = 0;
324
+ if (framebuffer_->depth != nullptr) {
325
+ submit(); SDL_WaitForGPUIdle(guidev::g_device);
326
+ SDL_ReleaseGPUTexture(guidev::g_device, framebuffer_->depth);
327
+ framebuffer_->depth = nullptr;
328
+ }
329
+ if (record != nullptr && record->width > 0 && record->height > 0) {
330
+ framebuffer_->width = record->width;
331
+ framebuffer_->height = record->height;
332
+ }
333
+ return true;
334
+ }
335
+ const int index = attachment - GL_COLOR_ATTACHMENT0;
336
+ if (index < 0 || index >= kWebGLMaxDrawBuffers) return false;
337
+ framebuffer_->color_renderbuffers[index] = renderbuffer_id;
338
+ framebuffer_->color_textures[index] = 0;
339
+ if (record != nullptr && record->width > 0 && record->height > 0) {
340
+ framebuffer_->width = record->width;
341
+ framebuffer_->height = record->height;
342
+ }
343
+ return true;
344
+ }
345
+
346
+ } // namespace xtgui