xbintsc 0.5.31 → 0.5.32

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 (181) hide show
  1. package/README.md +16 -10
  2. package/dist/src/binder/references.js +14 -1
  3. package/dist/src/binder/references.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/codegen/generator/module.d.ts +1 -0
  6. package/dist/src/codegen/generator/module.js +54 -0
  7. package/dist/src/codegen/generator/module.js.map +1 -1
  8. package/dist/src/codegen/generator/statements/variables.js +6 -4
  9. package/dist/src/codegen/generator/statements/variables.js.map +1 -1
  10. package/dist/src/driver/version.d.ts +1 -1
  11. package/dist/src/driver/version.js +1 -1
  12. package/dist/src/extensions/gui/css.d.ts +26 -0
  13. package/dist/src/extensions/gui/css.js +100 -0
  14. package/dist/src/extensions/gui/css.js.map +1 -0
  15. package/dist/src/extensions/gui/html.js +9 -2
  16. package/dist/src/extensions/gui/html.js.map +1 -1
  17. package/dist/src/extensions/gui/index.js +16 -2
  18. package/dist/src/extensions/gui/index.js.map +1 -1
  19. package/dist/src/extensions/gui/three.d.ts +16 -0
  20. package/dist/src/extensions/gui/three.js +22 -0
  21. package/dist/src/extensions/gui/three.js.map +1 -0
  22. package/dist/src/extensions/media/index.d.ts +6 -5
  23. package/dist/src/extensions/media/index.js +12 -14
  24. package/dist/src/extensions/media/index.js.map +1 -1
  25. package/dist/tests/e2e/compile-builtins.test.js +24 -0
  26. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  27. package/dist/tests/e2e/compile-objects-classes.test.js +23 -0
  28. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  29. package/dist/tests/e2e/gui-browser.test.js +1 -1
  30. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  31. package/dist/tests/e2e/gui-effects.test.d.ts +6 -0
  32. package/dist/tests/e2e/gui-effects.test.js +262 -0
  33. package/dist/tests/e2e/gui-effects.test.js.map +1 -0
  34. package/dist/tests/e2e/gui-example-webgl.test.d.ts +10 -0
  35. package/dist/tests/e2e/gui-example-webgl.test.js +75 -0
  36. package/dist/tests/e2e/gui-example-webgl.test.js.map +1 -0
  37. package/dist/tests/e2e/gui-example-webgl2.test.d.ts +8 -0
  38. package/dist/tests/e2e/gui-example-webgl2.test.js +72 -0
  39. package/dist/tests/e2e/gui-example-webgl2.test.js.map +1 -0
  40. package/dist/tests/e2e/gui-layout.test.js +48 -0
  41. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  42. package/dist/tests/e2e/gui-media.test.js +5 -4
  43. package/dist/tests/e2e/gui-media.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-webgl.test.d.ts +11 -0
  45. package/dist/tests/e2e/gui-webgl.test.js +179 -0
  46. package/dist/tests/e2e/gui-webgl.test.js.map +1 -0
  47. package/dist/tests/e2e/gui-webgl2.test.d.ts +10 -0
  48. package/dist/tests/e2e/gui-webgl2.test.js +238 -0
  49. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -0
  50. package/dist/tests/extensions/gui.test.js +41 -0
  51. package/dist/tests/extensions/gui.test.js.map +1 -1
  52. package/dist/tests/extensions/media.test.js +7 -3
  53. package/dist/tests/extensions/media.test.js.map +1 -1
  54. package/doc/ai/extensions.md +29 -9
  55. package/doc/ai/troubleshooting.md +12 -8
  56. package/doc/ai/zh-CN/extensions.md +26 -8
  57. package/doc/ai/zh-CN/troubleshooting.md +10 -6
  58. package/doc/gui/api.md +3 -2
  59. package/doc/gui/build.md +72 -16
  60. package/doc/gui/canvas.md +11 -3
  61. package/doc/gui/dom.md +33 -18
  62. package/doc/gui/known-issues.md +57 -24
  63. package/doc/gui/layout.md +18 -0
  64. package/doc/gui/media.md +17 -13
  65. package/doc/gui/rendering.md +50 -3
  66. package/doc/gui/webgl.md +256 -0
  67. package/doc/gui.md +108 -17
  68. package/doc/npm-package.md +6 -4
  69. package/doc/requirements.md +18 -9
  70. package/doc/zh-CN/gui/api.md +3 -2
  71. package/doc/zh-CN/gui/build.md +64 -15
  72. package/doc/zh-CN/gui/canvas.md +9 -2
  73. package/doc/zh-CN/gui/dom.md +20 -7
  74. package/doc/zh-CN/gui/known-issues.md +45 -19
  75. package/doc/zh-CN/gui/layout.md +14 -0
  76. package/doc/zh-CN/gui/media.md +15 -11
  77. package/doc/zh-CN/gui/rendering.md +41 -4
  78. package/doc/zh-CN/gui/webgl.md +115 -0
  79. package/doc/zh-CN/gui.md +89 -14
  80. package/doc/zh-CN/npm-package.md +9 -6
  81. package/doc/zh-CN/requirements.md +12 -6
  82. package/llms.txt +2 -0
  83. package/package.json +1 -1
  84. package/runtime/ext_gui/browser.cpp +39 -13
  85. package/runtime/ext_gui/canvas.cpp +3 -18
  86. package/runtime/ext_gui/canvas_internal.h +10 -0
  87. package/runtime/ext_gui/canvas_raster.cpp +60 -0
  88. package/runtime/ext_gui/clip_values.cpp +246 -0
  89. package/runtime/ext_gui/css.cpp +94 -0
  90. package/runtime/ext_gui/css.h +22 -1
  91. package/runtime/ext_gui/css_match.cpp +3 -0
  92. package/runtime/ext_gui/css_selector.cpp +9 -0
  93. package/runtime/ext_gui/css_sheet.cpp +89 -45
  94. package/runtime/ext_gui/document.cpp +4 -1
  95. package/runtime/ext_gui/document.h +2 -0
  96. package/runtime/ext_gui/dom.h +16 -0
  97. package/runtime/ext_gui/dom_api.cpp +1 -0
  98. package/runtime/ext_gui/dom_api_canvas.cpp +17 -80
  99. package/runtime/ext_gui/dom_api_canvas_extra.cpp +130 -1
  100. package/runtime/ext_gui/dom_api_canvas_pending.cpp +41 -0
  101. package/runtime/ext_gui/dom_api_internal.h +259 -0
  102. package/runtime/ext_gui/dom_api_webgl.cpp +376 -0
  103. package/runtime/ext_gui/dom_api_webgl2.cpp +434 -0
  104. package/runtime/ext_gui/dom_api_webgl2_draw.cpp +269 -0
  105. package/runtime/ext_gui/dom_api_webgl_buffer.cpp +212 -0
  106. package/runtime/ext_gui/dom_api_webgl_rt.h +30 -0
  107. package/runtime/ext_gui/dom_api_webgl_shader.cpp +443 -0
  108. package/runtime/ext_gui/dom_api_webgl_state.cpp +444 -0
  109. package/runtime/ext_gui/dom_api_webgl_texture.cpp +445 -0
  110. package/runtime/ext_gui/effect_values.cpp +312 -0
  111. package/runtime/ext_gui/gradient_values.cpp +476 -0
  112. package/runtime/ext_gui/grid_values.cpp +230 -0
  113. package/runtime/ext_gui/gui_canvas.cpp +50 -2
  114. package/runtime/ext_gui/gui_engine.h +27 -0
  115. package/runtime/ext_gui/gui_events.cpp +4 -1
  116. package/runtime/ext_gui/gui_media.cpp +1 -1
  117. package/runtime/ext_gui/gui_webgl.cpp +83 -0
  118. package/runtime/ext_gui/layout.cpp +21 -0
  119. package/runtime/ext_gui/layout_flow.cpp +81 -4
  120. package/runtime/ext_gui/layout_flow_internal.h +31 -0
  121. package/runtime/ext_gui/layout_grid.cpp +374 -0
  122. package/runtime/ext_gui/media.h +1 -1
  123. package/runtime/ext_gui/media_api.h +2 -2
  124. package/runtime/ext_gui/media_query.cpp +211 -0
  125. package/runtime/ext_gui/paint.cpp +111 -77
  126. package/runtime/ext_gui/paint.h +33 -3
  127. package/runtime/ext_gui/paint_background.cpp +229 -0
  128. package/runtime/ext_gui/paint_clip.cpp +268 -0
  129. package/runtime/ext_gui/paint_effects.cpp +324 -0
  130. package/runtime/ext_gui/paint_internal.h +85 -0
  131. package/runtime/ext_gui/renderer.cpp +8 -31
  132. package/runtime/ext_gui/renderer_clip.cpp +196 -0
  133. package/runtime/ext_gui/renderer_image.cpp +13 -8
  134. package/runtime/ext_gui/renderer_internal.h +15 -8
  135. package/runtime/ext_gui/renderer_text.cpp +6 -31
  136. package/runtime/ext_gui/style.cpp +343 -20
  137. package/runtime/ext_gui/style.h +144 -3
  138. package/runtime/ext_gui/style_print.cpp +134 -1
  139. package/runtime/ext_gui/style_values.cpp +152 -126
  140. package/runtime/ext_gui/style_values_internal.h +35 -0
  141. package/runtime/ext_gui/webgl.h +466 -0
  142. package/runtime/ext_gui/webgl2_constants.h +274 -0
  143. package/runtime/ext_gui/webgl_buffer.cpp +565 -0
  144. package/runtime/ext_gui/webgl_constants.h +317 -0
  145. package/runtime/ext_gui/webgl_context.cpp +466 -0
  146. package/runtime/ext_gui/webgl_draw.cpp +495 -0
  147. package/runtime/ext_gui/webgl_glsl.h +125 -0
  148. package/runtime/ext_gui/webgl_glsl_compile.cpp +431 -0
  149. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +349 -0
  150. package/runtime/ext_gui/webgl_glsl_scan.cpp +581 -0
  151. package/runtime/ext_gui/webgl_msl.cpp +62 -0
  152. package/runtime/ext_gui/webgl_msl.h +33 -0
  153. package/runtime/ext_gui/webgl_pipeline.cpp +274 -0
  154. package/runtime/ext_gui/webgl_program_internal.h +18 -0
  155. package/runtime/ext_gui/webgl_shader.cpp +582 -0
  156. package/runtime/ext_gui/webgl_texture.cpp +293 -0
  157. package/runtime/ext_gui/webgl_uniform.cpp +167 -0
  158. package/runtime/ext_gui/webgl_webgl2.cpp +224 -0
  159. package/runtime/ext_media/media.cpp +1 -1
  160. package/runtime/ext_media/media_bridge.c +9 -3
  161. package/runtime/rt_internal.h +1 -0
  162. package/runtime/xt_containers/access.inc +8 -0
  163. package/runtime/xt_regex/api.inc +2 -0
  164. package/runtime/xt_regex/matcher.inc +16 -3
  165. package/runtime/xt_regex/parser.inc +8 -2
  166. package/runtime/xt_regex/types.inc +3 -0
  167. package/runtime/xt_stdlib2/regexp-match.inc +60 -21
  168. package/runtime/xt_stdlib2/regexp.inc +1 -0
  169. package/runtime/xt_typed_array.c +1 -1
  170. package/scripts/build-gui.ts +90 -3
  171. package/scripts/build-media.ts +323 -58
  172. package/scripts/setup-vs-toolchain.ps1 +152 -0
  173. package/src/binder/references.ts +13 -1
  174. package/src/codegen/generator/module.ts +50 -0
  175. package/src/codegen/generator/statements/variables.ts +6 -3
  176. package/src/driver/version.ts +1 -1
  177. package/src/extensions/gui/css.ts +113 -0
  178. package/src/extensions/gui/html.ts +9 -2
  179. package/src/extensions/gui/index.ts +16 -2
  180. package/src/extensions/gui/three.ts +25 -0
  181. package/src/extensions/media/index.ts +13 -16
@@ -0,0 +1,293 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGL texture uploads, mipmaps and `readPixels`.
3
+ */
4
+
5
+ #include "webgl.h"
6
+
7
+ #include <algorithm>
8
+ #include <cstring>
9
+
10
+ #include "gui_internal.h"
11
+
12
+ namespace xtgui {
13
+ namespace {
14
+
15
+ /** Upload an RGBA8 rectangle into level 0 of `gpu`. `rgba` may be null, which
16
+ * uploads transparent black. */
17
+ bool upload_region(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, SDL_GPUTexture *gpu, int x,
18
+ int y, int width, int height, const uint8_t *rgba) {
19
+ const uint32_t bytes = (uint32_t)(width * height * 4);
20
+ if (bytes == 0) return true;
21
+ std::vector<uint8_t> zeros;
22
+ if (rgba == nullptr) {
23
+ zeros.assign(bytes, 0);
24
+ rgba = zeros.data();
25
+ }
26
+ SDL_GPUTransferBufferCreateInfo info;
27
+ memset(&info, 0, sizeof(info));
28
+ info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
29
+ info.size = bytes;
30
+ SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(device, &info);
31
+ if (transfer == nullptr) return false;
32
+ void *mapped = SDL_MapGPUTransferBuffer(device, transfer, false);
33
+ if (mapped == nullptr) {
34
+ SDL_ReleaseGPUTransferBuffer(device, transfer);
35
+ return false;
36
+ }
37
+ memcpy(mapped, rgba, bytes);
38
+ SDL_UnmapGPUTransferBuffer(device, transfer);
39
+ SDL_GPUTextureTransferInfo source;
40
+ memset(&source, 0, sizeof(source));
41
+ source.transfer_buffer = transfer;
42
+ source.pixels_per_row = (Uint32)width;
43
+ source.rows_per_layer = (Uint32)height;
44
+ SDL_GPUTextureRegion region;
45
+ memset(&region, 0, sizeof(region));
46
+ region.texture = gpu;
47
+ region.x = (Uint32)x;
48
+ region.y = (Uint32)y;
49
+ region.w = (Uint32)width;
50
+ region.h = (Uint32)height;
51
+ region.d = 1;
52
+ SDL_GPUCopyPass *copy = SDL_BeginGPUCopyPass(cmd);
53
+ SDL_UploadToGPUTexture(copy, &source, &region, false);
54
+ SDL_EndGPUCopyPass(copy);
55
+ SDL_ReleaseGPUTransferBuffer(device, transfer);
56
+ return true;
57
+ }
58
+
59
+ } // namespace
60
+
61
+ bool WebGLContext::tex_image_rgba(uint32_t id, int width, int height, const uint8_t *rgba) {
62
+ WebGLTexture *record = texture(id);
63
+ if (record == nullptr || width <= 0 || height <= 0) {
64
+ set_error(GL_INVALID_VALUE);
65
+ return false;
66
+ }
67
+ end_pass();
68
+ ensure_command();
69
+ if (cmd_ == nullptr) return false;
70
+ if (record->width != width || record->height != height) {
71
+ if (record->gpu != nullptr) {
72
+ SDL_WaitForGPUIdle(guidev::g_device);
73
+ SDL_ReleaseGPUTexture(guidev::g_device, record->gpu);
74
+ record->gpu = nullptr;
75
+ }
76
+ record->width = width;
77
+ record->height = height;
78
+ record->has_mipmaps = false;
79
+ if (record->sampler != nullptr) {
80
+ SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
81
+ record->sampler = nullptr;
82
+ }
83
+ }
84
+ if (ensure_texture_gpu(record, record->as_target) == nullptr) return false;
85
+ record->defined = true;
86
+ return upload_region(guidev::g_device, cmd_, record->gpu, 0, 0, width, height, rgba);
87
+ }
88
+
89
+ bool WebGLContext::tex_sub_image_rgba(uint32_t id, int x, int y, int width, int height,
90
+ const uint8_t *rgba) {
91
+ WebGLTexture *record = texture(id);
92
+ if (record == nullptr || !record->defined || record->gpu == nullptr) {
93
+ set_error(GL_INVALID_OPERATION);
94
+ return false;
95
+ }
96
+ if (x < 0 || y < 0 || width <= 0 || height <= 0 || x + width > record->width ||
97
+ y + height > record->height) {
98
+ set_error(GL_INVALID_VALUE);
99
+ return false;
100
+ }
101
+ end_pass();
102
+ ensure_command();
103
+ if (cmd_ == nullptr) return false;
104
+ return upload_region(guidev::g_device, cmd_, record->gpu, x, y, width, height, rgba);
105
+ }
106
+
107
+ bool WebGLContext::generate_mipmap(uint32_t id) {
108
+ WebGLTexture *record = texture(id);
109
+ if (record == nullptr || record->gpu == nullptr) {
110
+ set_error(GL_INVALID_OPERATION);
111
+ return false;
112
+ }
113
+ end_pass();
114
+ ensure_command();
115
+ if (cmd_ == nullptr || record->levels <= 1) return false;
116
+ SDL_GenerateMipmapsForGPUTexture(cmd_, record->gpu);
117
+ record->has_mipmaps = true;
118
+ if (record->sampler != nullptr) {
119
+ SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
120
+ record->sampler = nullptr;
121
+ }
122
+ return true;
123
+ }
124
+
125
+ bool WebGLContext::read_pixels(int x, int y, int width, int height, uint8_t *out) {
126
+ if (out == nullptr || width <= 0 || height <= 0) return false;
127
+ SDL_GPUTexture *target = target_color();
128
+ int target_width = bound_framebuffer_ == 0 ? width_ : 0;
129
+ int target_height = bound_framebuffer_ == 0 ? height_ : 0;
130
+ if (bound_framebuffer_ != 0) {
131
+ WebGLFramebuffer *framebuffer_ = framebuffer(bound_framebuffer_);
132
+ if (framebuffer_ == nullptr) return false;
133
+ target_width = framebuffer_->width;
134
+ target_height = framebuffer_->height;
135
+ }
136
+ if (target == nullptr || target_width <= 0 || target_height <= 0) return false;
137
+ /* Clamp to the target; pixels outside are transparent black. */
138
+ const int px = std::max(0, x);
139
+ const int py = std::max(0, y);
140
+ const int pw = std::min(width, target_width - px);
141
+ const int ph = std::min(height, target_height - py);
142
+ memset(out, 0, (size_t)width * height * 4);
143
+ if (pw <= 0 || ph <= 0) return true;
144
+
145
+ submit();
146
+ if (guidev::g_device == nullptr) return false;
147
+ SDL_GPUCommandBuffer *cmd = SDL_AcquireGPUCommandBuffer(guidev::g_device);
148
+ if (cmd == nullptr) return false;
149
+ const uint32_t bytes = (uint32_t)(pw * ph * 4);
150
+ SDL_GPUTransferBufferCreateInfo info;
151
+ memset(&info, 0, sizeof(info));
152
+ info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD;
153
+ info.size = bytes;
154
+ SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(guidev::g_device, &info);
155
+ if (transfer == nullptr) {
156
+ SDL_SubmitGPUCommandBuffer(cmd);
157
+ return false;
158
+ }
159
+ SDL_GPUTextureRegion region;
160
+ memset(&region, 0, sizeof(region));
161
+ region.texture = target;
162
+ /* The framebuffer is stored top-down; GL's y counts from the bottom. */
163
+ region.x = (Uint32)px;
164
+ region.y = (Uint32)(target_height - (py + ph));
165
+ region.w = (Uint32)pw;
166
+ region.h = (Uint32)ph;
167
+ region.d = 1;
168
+ SDL_GPUTextureTransferInfo destination;
169
+ memset(&destination, 0, sizeof(destination));
170
+ destination.transfer_buffer = transfer;
171
+ destination.pixels_per_row = (Uint32)pw;
172
+ destination.rows_per_layer = (Uint32)ph;
173
+ SDL_GPUCopyPass *copy = SDL_BeginGPUCopyPass(cmd);
174
+ SDL_DownloadFromGPUTexture(copy, &region, &destination);
175
+ SDL_EndGPUCopyPass(copy);
176
+ SDL_GPUFence *fence = SDL_SubmitGPUCommandBufferAndAcquireFence(cmd);
177
+ if (fence == nullptr) {
178
+ SDL_ReleaseGPUTransferBuffer(guidev::g_device, transfer);
179
+ return false;
180
+ }
181
+ SDL_WaitForGPUFences(guidev::g_device, true, &fence, 1);
182
+ SDL_ReleaseGPUFence(guidev::g_device, fence);
183
+ const uint8_t *mapped =
184
+ (const uint8_t *)SDL_MapGPUTransferBuffer(guidev::g_device, transfer, false);
185
+ if (mapped == nullptr) {
186
+ SDL_ReleaseGPUTransferBuffer(guidev::g_device, transfer);
187
+ return false;
188
+ }
189
+ for (int row = 0; row < ph; row++) {
190
+ const uint8_t *source = mapped + (size_t)(ph - 1 - row) * pw * 4;
191
+ uint8_t *line = out + ((size_t)(py - y + row) * width + (px - x)) * 4;
192
+ memcpy(line, source, (size_t)pw * 4);
193
+ }
194
+ SDL_UnmapGPUTransferBuffer(guidev::g_device, transfer);
195
+ SDL_ReleaseGPUTransferBuffer(guidev::g_device, transfer);
196
+ return true;
197
+ }
198
+
199
+ bool WebGLContext::tex_storage_2d(uint32_t id, int levels, int width, int height) {
200
+ (void)levels; // the backend always allocates the full mip chain
201
+ WebGLTexture *record = texture(id);
202
+ if (record == nullptr || width <= 0 || height <= 0) {
203
+ set_error(GL_INVALID_VALUE);
204
+ return false;
205
+ }
206
+ end_pass();
207
+ ensure_command();
208
+ if (record->gpu != nullptr) {
209
+ submit();
210
+ SDL_WaitForGPUIdle(guidev::g_device);
211
+ SDL_ReleaseGPUTexture(guidev::g_device, record->gpu);
212
+ record->gpu = nullptr;
213
+ }
214
+ record->width = width;
215
+ record->height = height;
216
+ record->has_mipmaps = false;
217
+ if (record->sampler != nullptr && guidev::g_device != nullptr) {
218
+ SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
219
+ record->sampler = nullptr;
220
+ }
221
+ record->defined = true;
222
+ return ensure_texture_gpu(record, record->as_target) != nullptr;
223
+ }
224
+
225
+ /* -- WebGL 2 clearBuffer* ------------------------------------------------ */
226
+
227
+ void WebGLContext::clear_buffer_float(int buffer, int drawbuffer, const float *values,
228
+ int count) {
229
+ if (buffer == GL_COLOR) {
230
+ if (drawbuffer != 0) return;
231
+ clear_color(values[0], count > 1 ? values[1] : 0.0f, count > 2 ? values[2] : 0.0f,
232
+ count > 3 ? values[3] : 1.0f);
233
+ clear(GL_COLOR_BUFFER_BIT);
234
+ return;
235
+ }
236
+ if (buffer == GL_DEPTH) {
237
+ clear_depth(values[0]);
238
+ clear(GL_DEPTH_BUFFER_BIT);
239
+ return;
240
+ }
241
+ if (buffer == GL_STENCIL) {
242
+ clear_stencil((int)values[0]);
243
+ clear(GL_STENCIL_BUFFER_BIT);
244
+ }
245
+ }
246
+
247
+ void WebGLContext::clear_buffer_int(int buffer, int drawbuffer, const int *values, int count) {
248
+ if (buffer == GL_STENCIL) {
249
+ clear_stencil(values[0]);
250
+ clear(GL_STENCIL_BUFFER_BIT);
251
+ return;
252
+ }
253
+ if (buffer == GL_COLOR) {
254
+ if (drawbuffer != 0) return;
255
+ clear_color((float)values[0], count > 1 ? (float)values[1] : 0.0f,
256
+ count > 2 ? (float)values[2] : 0.0f, count > 3 ? (float)values[3] : 1.0f);
257
+ clear(GL_COLOR_BUFFER_BIT);
258
+ }
259
+ }
260
+
261
+ void WebGLContext::clear_buffer_uint(int buffer, int drawbuffer, const unsigned int *values,
262
+ int count) {
263
+ if (buffer == GL_STENCIL) {
264
+ clear_stencil((int)values[0]);
265
+ clear(GL_STENCIL_BUFFER_BIT);
266
+ return;
267
+ }
268
+ if (buffer == GL_COLOR) {
269
+ if (drawbuffer != 0) return;
270
+ clear_color((float)values[0], count > 1 ? (float)values[1] : 0.0f,
271
+ count > 2 ? (float)values[2] : 0.0f, count > 3 ? (float)values[3] : 1.0f);
272
+ clear(GL_COLOR_BUFFER_BIT);
273
+ }
274
+ }
275
+
276
+ void WebGLContext::clear_buffer_fi(int buffer, int drawbuffer, float depth, int stencil) {
277
+ (void)drawbuffer;
278
+ if (buffer == GL_DEPTH_STENCIL) {
279
+ clear_depth(depth);
280
+ clear_stencil(stencil);
281
+ clear(GL_DEPTH_BUFFER_BIT);
282
+ return;
283
+ }
284
+ if (buffer == GL_DEPTH) {
285
+ clear_depth(depth);
286
+ clear(GL_DEPTH_BUFFER_BIT);
287
+ } else if (buffer == GL_STENCIL) {
288
+ clear_stencil(stencil);
289
+ clear(GL_STENCIL_BUFFER_BIT);
290
+ }
291
+ }
292
+
293
+ } // namespace xtgui
@@ -0,0 +1,167 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGL uniform uploads and readback.
3
+ *
4
+ * A program owns one std140 block; these functions write the values WebGL
5
+ * supplies into the reflected offsets and `uniform_values` reads them back for
6
+ * `getUniform`. Matrices are column-major and arrays stride by `array_stride`.
7
+ */
8
+
9
+ #include "webgl.h"
10
+
11
+ #include <cstring>
12
+ #include <vector>
13
+
14
+ #include "webgl_program_internal.h"
15
+
16
+ namespace xtgui {
17
+ namespace webgl_detail {
18
+
19
+ int member_bytes(const GlslBlockMember &member) {
20
+ if (member.is_array) {
21
+ const int stride = member.array_stride > 0 ? member.array_stride : 16;
22
+ return member.array_size * stride;
23
+ }
24
+ if (member.is_matrix) {
25
+ const int stride = member.matrix_stride > 0 ? member.matrix_stride : 16;
26
+ return member.columns * stride;
27
+ }
28
+ return member.components * 4;
29
+ }
30
+
31
+ } // namespace webgl_detail
32
+
33
+
34
+ void WebGLContext::uniform_floats(uint32_t id, int location, int components, const double *values,
35
+ int count) {
36
+ WebGLProgram *record = program(id);
37
+ if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
38
+ set_error(GL_INVALID_OPERATION);
39
+ return;
40
+ }
41
+ WebGLUniform &uniform = record->uniforms[location];
42
+ if (uniform.sampler) {
43
+ uniform.unit = (int)values[0];
44
+ return;
45
+ }
46
+ const GlslBlockMember &member = uniform.layout;
47
+ if (record->block_size <= 0 || member.offset + webgl_detail::member_bytes(member) > record->block_size) return;
48
+ if (count <= 0) count = 1;
49
+ if (member.is_matrix) {
50
+ for (int element = 0; element < count; element++) {
51
+ const int base = member.offset + element * member.array_stride;
52
+ for (int column = 0; column < member.columns; column++) {
53
+ for (int row = 0; row < member.rows; row++) {
54
+ const float value = (float)values[(element * member.columns + column) * member.rows + row];
55
+ const int at = base + column * member.matrix_stride + row * 4;
56
+ if (at + 4 <= (int)record->block.size()) memcpy(&record->block[at], &value, 4);
57
+ }
58
+ }
59
+ }
60
+ record->block_dirty = true;
61
+ return;
62
+ }
63
+ const int components_to_write = std::min(components, member.components);
64
+ for (int element = 0; element < count; element++) {
65
+ const int base = member.offset + element * (member.array_stride > 0 ? member.array_stride
66
+ : member.components * 4);
67
+ for (int component = 0; component < components_to_write; component++) {
68
+ const float value = (float)values[(size_t)element * components + component];
69
+ const int at = base + component * 4;
70
+ if (at + 4 <= (int)record->block.size()) memcpy(&record->block[at], &value, 4);
71
+ }
72
+ }
73
+ record->block_dirty = true;
74
+ }
75
+
76
+ void WebGLContext::uniform_ints(uint32_t id, int location, int components, const int *values,
77
+ int count) {
78
+ WebGLProgram *record = program(id);
79
+ if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
80
+ set_error(GL_INVALID_OPERATION);
81
+ return;
82
+ }
83
+ WebGLUniform &uniform = record->uniforms[location];
84
+ if (uniform.sampler) {
85
+ uniform.unit = values[0];
86
+ return;
87
+ }
88
+ const GlslBlockMember &member = uniform.layout;
89
+ if (record->block_size <= 0) return;
90
+ if (count <= 0) count = 1;
91
+ const int components_to_write = std::min(components, member.components);
92
+ for (int element = 0; element < count; element++) {
93
+ const int base = member.offset + element * (member.array_stride > 0 ? member.array_stride
94
+ : member.components * 4);
95
+ for (int component = 0; component < components_to_write; component++) {
96
+ const int at = base + component * 4;
97
+ if (at + 4 <= (int)record->block.size()) {
98
+ memcpy(&record->block[at], &values[(size_t)element * components + component], 4);
99
+ }
100
+ }
101
+ }
102
+ record->block_dirty = true;
103
+ }
104
+
105
+ void WebGLContext::uniform_matrix(uint32_t id, int location, int dimensions, const double *values) {
106
+ WebGLProgram *record = program(id);
107
+ if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) {
108
+ set_error(GL_INVALID_OPERATION);
109
+ return;
110
+ }
111
+ WebGLUniform &uniform = record->uniforms[location];
112
+ const GlslBlockMember &member = uniform.layout;
113
+ if (!member.is_matrix) {
114
+ uniform_floats(id, location, dimensions * dimensions, values, 1);
115
+ return;
116
+ }
117
+ const int base = member.offset;
118
+ for (int column = 0; column < member.columns; column++) {
119
+ for (int row = 0; row < member.rows; row++) {
120
+ const float value = (float)values[(size_t)column * member.rows + row];
121
+ const int at = base + column * member.matrix_stride + row * 4;
122
+ if (at + 4 <= (int)record->block.size()) memcpy(&record->block[at], &value, 4);
123
+ }
124
+ }
125
+ record->block_dirty = true;
126
+ }
127
+
128
+ bool WebGLContext::uniform_values(uint32_t id, int location, std::vector<double> *out) {
129
+ WebGLProgram *record = program(id);
130
+ if (record == nullptr || location < 0 || location >= (int)record->uniforms.size()) return false;
131
+ const WebGLUniform &uniform = record->uniforms[location];
132
+ out->clear();
133
+ if (uniform.sampler) {
134
+ out->push_back(uniform.unit);
135
+ return true;
136
+ }
137
+ const GlslBlockMember &member = uniform.layout;
138
+ const int elements = member.is_array ? member.array_size : 1;
139
+ if (member.is_matrix) {
140
+ for (int element = 0; element < elements; element++) {
141
+ for (int column = 0; column < member.columns; column++) {
142
+ for (int row = 0; row < member.rows; row++) {
143
+ float value = 0.0f;
144
+ const int at = member.offset + element * member.array_stride +
145
+ column * member.matrix_stride + row * 4;
146
+ if (at + 4 <= (int)record->block.size()) memcpy(&value, &record->block[at], 4);
147
+ out->push_back(value);
148
+ }
149
+ }
150
+ }
151
+ return true;
152
+ }
153
+ for (int element = 0; element < elements; element++) {
154
+ for (int component = 0; component < member.components; component++) {
155
+ float value = 0.0f;
156
+ const int at = member.offset + element * (member.array_stride > 0 ? member.array_stride
157
+ : member.components * 4) +
158
+ component * 4;
159
+ if (at + 4 <= (int)record->block.size()) memcpy(&value, &record->block[at], 4);
160
+ out->push_back(value);
161
+ }
162
+ }
163
+ return true;
164
+ }
165
+
166
+
167
+ } // namespace xtgui
@@ -0,0 +1,224 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGL 2 vertex-array objects, queries and syncs.
3
+ *
4
+ * SDL_GPU has no vertex-array-object or query concept, so a VAO is a saved copy
5
+ * of the attribute table (and its element buffer), restored into the live state
6
+ * when bound. Queries and syncs are opaque handles; fence syncs report as
7
+ * already signalled because every command buffer is submitted per frame.
8
+ */
9
+
10
+ #include "webgl.h"
11
+
12
+ #include <cstring>
13
+
14
+ #include "gui_internal.h"
15
+
16
+ namespace xtgui {
17
+ namespace {
18
+
19
+ SDL_GPUFilter filter_of(int gl_filter) {
20
+ return gl_filter == GL_NEAREST || gl_filter == GL_NEAREST_MIPMAP_NEAREST ||
21
+ gl_filter == GL_NEAREST_MIPMAP_LINEAR
22
+ ? SDL_GPU_FILTER_NEAREST
23
+ : SDL_GPU_FILTER_LINEAR;
24
+ }
25
+
26
+ SDL_GPUSamplerAddressMode address_of(int wrap) {
27
+ switch (wrap) {
28
+ case GL_CLAMP_TO_EDGE:
29
+ return SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
30
+ case GL_MIRRORED_REPEAT:
31
+ return SDL_GPU_SAMPLERADDRESSMODE_MIRRORED_REPEAT;
32
+ default:
33
+ return SDL_GPU_SAMPLERADDRESSMODE_REPEAT;
34
+ }
35
+ }
36
+
37
+ } // namespace
38
+
39
+ WebGLVertexArray *WebGLContext::vertex_array(uint32_t id) {
40
+ auto found = vertex_arrays_.find(id);
41
+ return found == vertex_arrays_.end() ? nullptr : found->second.get();
42
+ }
43
+
44
+ WebGLQuery *WebGLContext::query(uint32_t id) {
45
+ auto found = queries_.find(id);
46
+ return found == queries_.end() ? nullptr : found->second.get();
47
+ }
48
+
49
+ WebGLSync *WebGLContext::sync(uint32_t id) {
50
+ auto found = syncs_.find(id);
51
+ return found == syncs_.end() ? nullptr : found->second.get();
52
+ }
53
+
54
+ /* -- vertex arrays ------------------------------------------------------- */
55
+
56
+ uint32_t WebGLContext::create_vertex_array() {
57
+ auto record = std::make_unique<WebGLVertexArray>();
58
+ record->id = next_object_id_++;
59
+ const uint32_t id = record->id;
60
+ vertex_arrays_[id] = std::move(record);
61
+ return id;
62
+ }
63
+
64
+ void WebGLContext::delete_vertex_array(uint32_t id) {
65
+ if (id == 0) return;
66
+ if (bound_vertex_array_ == id) bind_vertex_array(0);
67
+ vertex_arrays_.erase(id);
68
+ }
69
+
70
+ void WebGLContext::save_vertex_array() {
71
+ WebGLVertexArray *target =
72
+ bound_vertex_array_ != 0 ? vertex_array(bound_vertex_array_) : &default_vertex_array_;
73
+ if (target == nullptr) return;
74
+ for (int i = 0; i < kWebGLMaxAttribs; i++) target->attribs[i] = attribs_[i];
75
+ target->element_buffer = bound_element_;
76
+ }
77
+
78
+ void WebGLContext::load_vertex_array(const WebGLVertexArray *array) {
79
+ for (int i = 0; i < kWebGLMaxAttribs; i++) attribs_[i] = array->attribs[i];
80
+ bound_element_ = array->element_buffer;
81
+ }
82
+
83
+ void WebGLContext::bind_vertex_array(uint32_t id) {
84
+ if (id != 0 && vertex_arrays_.find(id) == vertex_arrays_.end()) {
85
+ set_error(GL_INVALID_OPERATION);
86
+ return;
87
+ }
88
+ if (id == bound_vertex_array_) return;
89
+ save_vertex_array();
90
+ bound_vertex_array_ = id;
91
+ load_vertex_array(id != 0 ? vertex_array(id) : &default_vertex_array_);
92
+ }
93
+
94
+ bool WebGLContext::is_vertex_array(uint32_t id) const {
95
+ return vertex_arrays_.find(id) != vertex_arrays_.end();
96
+ }
97
+
98
+ /* -- queries and syncs --------------------------------------------------- */
99
+
100
+ uint32_t WebGLContext::create_query() {
101
+ auto record = std::make_unique<WebGLQuery>();
102
+ record->id = next_object_id_++;
103
+ const uint32_t id = record->id;
104
+ queries_[id] = std::move(record);
105
+ return id;
106
+ }
107
+
108
+ void WebGLContext::delete_query(uint32_t id) { queries_.erase(id); }
109
+
110
+ bool WebGLContext::is_query(uint32_t id) const {
111
+ return queries_.find(id) != queries_.end();
112
+ }
113
+
114
+ uint32_t WebGLContext::create_sync() {
115
+ auto record = std::make_unique<WebGLSync>();
116
+ record->id = next_object_id_++;
117
+ const uint32_t id = record->id;
118
+ syncs_[id] = std::move(record);
119
+ return id;
120
+ }
121
+
122
+ void WebGLContext::delete_sync(uint32_t id) { syncs_.erase(id); }
123
+
124
+ bool WebGLContext::is_sync(uint32_t id) const {
125
+ return syncs_.find(id) != syncs_.end();
126
+ }
127
+
128
+ uint32_t WebGLContext::uniform_block_index(uint32_t program, const std::string &name) const {
129
+ (void)program;
130
+ (void)name;
131
+ /* Uniform blocks are not exposed; answering INVALID_INDEX makes a library
132
+ * fall back to individual uniforms. */
133
+ return 0xFFFFFFFFu;
134
+ }
135
+
136
+ /* -- sampler objects ----------------------------------------------------- */
137
+
138
+ WebGLSampler *WebGLContext::sampler(uint32_t id) {
139
+ auto found = samplers_.find(id);
140
+ return found == samplers_.end() ? nullptr : found->second.get();
141
+ }
142
+
143
+ uint32_t WebGLContext::create_sampler() {
144
+ auto record = std::make_unique<WebGLSampler>();
145
+ record->id = next_object_id_++;
146
+ record->min_filter = GL_NEAREST_MIPMAP_LINEAR;
147
+ record->mag_filter = GL_LINEAR;
148
+ record->wrap_s = GL_REPEAT;
149
+ record->wrap_t = GL_REPEAT;
150
+ const uint32_t id = record->id;
151
+ samplers_[id] = std::move(record);
152
+ return id;
153
+ }
154
+
155
+ void WebGLContext::delete_sampler(uint32_t id) {
156
+ auto found = samplers_.find(id);
157
+ if (found == samplers_.end()) return;
158
+ for (uint32_t &bound : bound_samplers_) {
159
+ if (bound == id) bound = 0;
160
+ }
161
+ if (found->second->gpu != nullptr && guidev::g_device != nullptr) {
162
+ submit();
163
+ SDL_WaitForGPUIdle(guidev::g_device);
164
+ SDL_ReleaseGPUSampler(guidev::g_device, found->second->gpu);
165
+ }
166
+ samplers_.erase(found);
167
+ }
168
+
169
+ void WebGLContext::bind_sampler(int unit, uint32_t id) {
170
+ if (unit < 0 || unit >= kWebGLMaxTextureUnits) {
171
+ set_error(GL_INVALID_VALUE);
172
+ return;
173
+ }
174
+ if (id != 0 && samplers_.find(id) == samplers_.end()) {
175
+ set_error(GL_INVALID_OPERATION);
176
+ return;
177
+ }
178
+ bound_samplers_[unit] = id;
179
+ }
180
+
181
+ bool WebGLContext::sampler_parameter(uint32_t id, int pname, int value) {
182
+ WebGLSampler *record = sampler(id);
183
+ if (record == nullptr) return false;
184
+ if (pname == GL_TEXTURE_MIN_FILTER) record->min_filter = value;
185
+ else if (pname == GL_TEXTURE_MAG_FILTER) record->mag_filter = value;
186
+ else if (pname == GL_TEXTURE_WRAP_S) record->wrap_s = value;
187
+ else if (pname == GL_TEXTURE_WRAP_T) record->wrap_t = value;
188
+ if (record->gpu != nullptr && guidev::g_device != nullptr) {
189
+ SDL_ReleaseGPUSampler(guidev::g_device, record->gpu);
190
+ record->gpu = nullptr;
191
+ }
192
+ return true;
193
+ }
194
+
195
+ int WebGLContext::sampler_parameter_value(uint32_t id, int pname) {
196
+ WebGLSampler *record = sampler(id);
197
+ if (record == nullptr) return 0;
198
+ if (pname == GL_TEXTURE_MIN_FILTER) return record->min_filter;
199
+ if (pname == GL_TEXTURE_MAG_FILTER) return record->mag_filter;
200
+ if (pname == GL_TEXTURE_WRAP_S) return record->wrap_s;
201
+ if (pname == GL_TEXTURE_WRAP_T) return record->wrap_t;
202
+ return 0;
203
+ }
204
+
205
+ SDL_GPUSampler *WebGLContext::sampler_gpu(WebGLSampler *record) {
206
+ if (record == nullptr || guidev::g_device == nullptr) return nullptr;
207
+ if (record->gpu != nullptr) return record->gpu;
208
+ SDL_GPUSamplerCreateInfo info;
209
+ memset(&info, 0, sizeof(info));
210
+ info.min_filter = filter_of(record->min_filter);
211
+ info.mag_filter = filter_of(record->mag_filter);
212
+ info.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR;
213
+ info.address_mode_u = address_of(record->wrap_s);
214
+ info.address_mode_v = address_of(record->wrap_t);
215
+ info.address_mode_w = address_of(record->wrap_t);
216
+ info.min_lod = 0.0f;
217
+ /* A sampler object has no texture, so it cannot know how many levels exist;
218
+ * textures created by this engine always carry a full mip chain. */
219
+ info.max_lod = 1000.0f;
220
+ record->gpu = SDL_CreateGPUSampler(guidev::g_device, &info);
221
+ return record->gpu;
222
+ }
223
+
224
+ } // namespace xtgui
@@ -2,7 +2,7 @@
2
2
  * xbintsc media extension — `<video>`/`<audio>` decoding.
3
3
  *
4
4
  * This translation unit is the only place that knows about FFmpeg. It is
5
- * compiled into the prebuilt `media.a` by `scripts/build-media.ts` and linked
5
+ * compiled into the prebuilt `media.a`/`media.lib` by `scripts/build-media.ts` and linked
6
6
  * only when the `media` extension is enabled; the GUI engine itself is
7
7
  * decoder-free (see `../ext_gui/media_api.h`).
8
8
  *