xbintsc 0.5.32 → 0.5.36

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 (173) hide show
  1. package/README.md +10 -7
  2. package/README.zh-CN.md +10 -8
  3. package/dist/scripts/check-version.d.ts +27 -0
  4. package/dist/scripts/check-version.js +71 -0
  5. package/dist/scripts/check-version.js.map +1 -0
  6. package/dist/scripts/package-platform.d.ts +58 -0
  7. package/dist/scripts/package-platform.js +142 -0
  8. package/dist/scripts/package-platform.js.map +1 -0
  9. package/dist/scripts/verify-platform-package.d.ts +34 -0
  10. package/dist/scripts/verify-platform-package.js +161 -0
  11. package/dist/scripts/verify-platform-package.js.map +1 -0
  12. package/dist/src/cli/help.d.ts +1 -1
  13. package/dist/src/cli/main.js +3 -1
  14. package/dist/src/cli/main.js.map +1 -1
  15. package/dist/src/driver/runtime-lib.d.ts +29 -4
  16. package/dist/src/driver/runtime-lib.js +68 -10
  17. package/dist/src/driver/runtime-lib.js.map +1 -1
  18. package/dist/src/driver/version.d.ts +1 -1
  19. package/dist/src/driver/version.js +1 -1
  20. package/dist/src/extensions/gui/index.js +6 -5
  21. package/dist/src/extensions/gui/index.js.map +1 -1
  22. package/dist/src/extensions/media/index.js +4 -3
  23. package/dist/src/extensions/media/index.js.map +1 -1
  24. package/dist/src/parser/context.d.ts +41 -0
  25. package/dist/src/parser/context.js +89 -4
  26. package/dist/src/parser/context.js.map +1 -1
  27. package/dist/src/parser/literals.js +6 -12
  28. package/dist/src/parser/literals.js.map +1 -1
  29. package/dist/tests/driver/platform-package.test.d.ts +1 -0
  30. package/dist/tests/driver/platform-package.test.js +49 -0
  31. package/dist/tests/driver/platform-package.test.js.map +1 -0
  32. package/dist/tests/driver/runtime-lib.test.js +51 -4
  33. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  34. package/dist/tests/e2e/gui-audio.test.d.ts +14 -0
  35. package/dist/tests/e2e/gui-audio.test.js +287 -0
  36. package/dist/tests/e2e/gui-audio.test.js.map +1 -0
  37. package/dist/tests/e2e/gui-helpers.d.ts +7 -0
  38. package/dist/tests/e2e/gui-helpers.js +1 -0
  39. package/dist/tests/e2e/gui-helpers.js.map +1 -1
  40. package/dist/tests/e2e/gui-media.test.js +78 -0
  41. package/dist/tests/e2e/gui-media.test.js.map +1 -1
  42. package/dist/tests/e2e/gui-style.test.js +55 -0
  43. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-webgl.test.js +258 -0
  45. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  46. package/dist/tests/e2e/gui-webgl2-3d.test.d.ts +4 -0
  47. package/dist/tests/e2e/gui-webgl2-3d.test.js +264 -0
  48. package/dist/tests/e2e/gui-webgl2-3d.test.js.map +1 -0
  49. package/dist/tests/e2e/gui-webgl2-blit.test.d.ts +10 -0
  50. package/dist/tests/e2e/gui-webgl2-blit.test.js +262 -0
  51. package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -0
  52. package/dist/tests/e2e/gui-webgl2-formats.test.d.ts +6 -0
  53. package/dist/tests/e2e/gui-webgl2-formats.test.js +395 -0
  54. package/dist/tests/e2e/gui-webgl2-formats.test.js.map +1 -0
  55. package/dist/tests/e2e/gui-webgl2-mrt.test.d.ts +4 -0
  56. package/dist/tests/e2e/gui-webgl2-mrt.test.js +105 -0
  57. package/dist/tests/e2e/gui-webgl2-mrt.test.js.map +1 -0
  58. package/dist/tests/e2e/gui-webgl2-ubo.test.d.ts +8 -0
  59. package/dist/tests/e2e/gui-webgl2-ubo.test.js +112 -0
  60. package/dist/tests/e2e/gui-webgl2-ubo.test.js.map +1 -0
  61. package/dist/tests/e2e/gui-webgl2.test.js +45 -1
  62. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
  63. package/dist/tests/extensions/gui-index.test.js +1 -0
  64. package/dist/tests/extensions/gui-index.test.js.map +1 -1
  65. package/dist/tests/extensions/media.test.js +1 -0
  66. package/dist/tests/extensions/media.test.js.map +1 -1
  67. package/dist/tests/parser/expressions.test.js +30 -0
  68. package/dist/tests/parser/expressions.test.js.map +1 -1
  69. package/dist/tests/platform-tarball.test.d.ts +1 -0
  70. package/dist/tests/platform-tarball.test.js +111 -0
  71. package/dist/tests/platform-tarball.test.js.map +1 -0
  72. package/dist/tests/version.test.js +6 -0
  73. package/dist/tests/version.test.js.map +1 -1
  74. package/doc/ai/contributing.md +11 -0
  75. package/doc/ai/extensions.md +7 -4
  76. package/doc/ai/troubleshooting.md +7 -5
  77. package/doc/ai/zh-CN/contributing.md +9 -0
  78. package/doc/ai/zh-CN/extensions.md +4 -2
  79. package/doc/ai/zh-CN/troubleshooting.md +5 -3
  80. package/doc/gui/api.md +61 -13
  81. package/doc/gui/build.md +11 -4
  82. package/doc/gui/dom.md +6 -3
  83. package/doc/gui/media.md +4 -0
  84. package/doc/gui/webgl.md +105 -46
  85. package/doc/gui.md +7 -3
  86. package/doc/npm-package.md +55 -27
  87. package/doc/requirements.md +6 -2
  88. package/doc/zh-CN/gui/api.md +51 -10
  89. package/doc/zh-CN/gui/build.md +7 -2
  90. package/doc/zh-CN/gui/dom.md +3 -1
  91. package/doc/zh-CN/gui/media.md +3 -0
  92. package/doc/zh-CN/gui/webgl.md +22 -16
  93. package/doc/zh-CN/gui.md +3 -2
  94. package/doc/zh-CN/npm-package.md +44 -23
  95. package/doc/zh-CN/requirements.md +4 -1
  96. package/package.json +17 -1
  97. package/runtime/ext_gui/audio.cpp +365 -153
  98. package/runtime/ext_gui/audio_buffer.cpp +166 -0
  99. package/runtime/ext_gui/audio_buffer.h +37 -0
  100. package/runtime/ext_gui/audio_decode.cpp +219 -0
  101. package/runtime/ext_gui/audio_decode.h +39 -0
  102. package/runtime/ext_gui/audio_device.cpp +87 -0
  103. package/runtime/ext_gui/audio_effects.cpp +163 -0
  104. package/runtime/ext_gui/audio_effects.h +25 -0
  105. package/runtime/ext_gui/audio_internal.h +30 -0
  106. package/runtime/ext_gui/audio_mixer.cpp +494 -0
  107. package/runtime/ext_gui/audio_mixer.h +238 -0
  108. package/runtime/ext_gui/audio_param.cpp +104 -0
  109. package/runtime/ext_gui/audio_param_object.cpp +155 -0
  110. package/runtime/ext_gui/browser.cpp +11 -1
  111. package/runtime/ext_gui/css_selector.cpp +21 -3
  112. package/runtime/ext_gui/dom_api_internal.h +16 -0
  113. package/runtime/ext_gui/dom_api_webgl.cpp +7 -0
  114. package/runtime/ext_gui/dom_api_webgl2.cpp +151 -36
  115. package/runtime/ext_gui/dom_api_webgl2_draw.cpp +3 -3
  116. package/runtime/ext_gui/dom_api_webgl_buffer.cpp +6 -2
  117. package/runtime/ext_gui/dom_api_webgl_compressed.cpp +167 -0
  118. package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +160 -0
  119. package/runtime/ext_gui/dom_api_webgl_shader.cpp +4 -4
  120. package/runtime/ext_gui/dom_api_webgl_source.cpp +161 -0
  121. package/runtime/ext_gui/dom_api_webgl_state.cpp +169 -23
  122. package/runtime/ext_gui/dom_api_webgl_texture.cpp +276 -236
  123. package/runtime/ext_gui/dom_api_webgl_ubo.cpp +81 -0
  124. package/runtime/ext_gui/gui.cpp +44 -9
  125. package/runtime/ext_gui/gui_engine.h +9 -4
  126. package/runtime/ext_gui/image_bitmap.cpp +28 -0
  127. package/runtime/ext_gui/image_bitmap.h +26 -0
  128. package/runtime/ext_gui/image_bitmap_browser.cpp +136 -0
  129. package/runtime/ext_gui/media.cpp +32 -0
  130. package/runtime/ext_gui/media.h +13 -0
  131. package/runtime/ext_gui/media_api.h +23 -0
  132. package/runtime/ext_gui/webgl.h +152 -18
  133. package/runtime/ext_gui/webgl_blit.cpp +95 -0
  134. package/runtime/ext_gui/webgl_buffer.cpp +178 -263
  135. package/runtime/ext_gui/webgl_buffer_objects.cpp +270 -0
  136. package/runtime/ext_gui/webgl_clear.cpp +142 -0
  137. package/runtime/ext_gui/webgl_compressed.cpp +180 -0
  138. package/runtime/ext_gui/webgl_context.cpp +241 -156
  139. package/runtime/ext_gui/webgl_draw.cpp +68 -3
  140. package/runtime/ext_gui/webgl_draw_buffers.cpp +75 -0
  141. package/runtime/ext_gui/webgl_glsl.h +40 -0
  142. package/runtime/ext_gui/webgl_glsl_compile.cpp +6 -0
  143. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +18 -0
  144. package/runtime/ext_gui/webgl_glsl_scan.cpp +20 -11
  145. package/runtime/ext_gui/webgl_glsl_scan_block.cpp +126 -0
  146. package/runtime/ext_gui/webgl_glsl_tokens.h +28 -0
  147. package/runtime/ext_gui/webgl_pipeline.cpp +71 -19
  148. package/runtime/ext_gui/webgl_program_blocks.cpp +58 -0
  149. package/runtime/ext_gui/webgl_program_internal.h +6 -0
  150. package/runtime/ext_gui/webgl_shader.cpp +13 -1
  151. package/runtime/ext_gui/webgl_state.cpp +228 -0
  152. package/runtime/ext_gui/webgl_texture.cpp +144 -17
  153. package/runtime/ext_gui/webgl_texture_3d.cpp +215 -0
  154. package/runtime/ext_gui/webgl_uniform_block.h +31 -0
  155. package/runtime/ext_gui/webgl_webgl2.cpp +58 -5
  156. package/runtime/ext_media/decode_audio.cpp +308 -0
  157. package/runtime/ext_media/media_bridge.c +2 -0
  158. package/runtime/rt.h +17 -0
  159. package/runtime/xt_array_buffer.c +30 -0
  160. package/runtime/xt_typed_array/elements.inc +13 -0
  161. package/scripts/build-media.ts +14 -6
  162. package/scripts/build-runtime.ts +21 -16
  163. package/scripts/check-version.ts +39 -9
  164. package/scripts/package-platform.ts +183 -0
  165. package/scripts/set-platform-version.ts +62 -0
  166. package/scripts/verify-platform-package.ts +182 -0
  167. package/src/cli/main.ts +5 -1
  168. package/src/driver/runtime-lib.ts +73 -9
  169. package/src/driver/version.ts +1 -1
  170. package/src/extensions/gui/index.ts +6 -5
  171. package/src/extensions/media/index.ts +4 -3
  172. package/src/parser/context.ts +99 -4
  173. package/src/parser/literals.ts +6 -12
@@ -17,6 +17,7 @@
17
17
  #include "canvas.h"
18
18
  #include "dom_api_webgl_rt.h"
19
19
  #include "image.h"
20
+ #include "image_bitmap.h"
20
21
  #include "webgl.h"
21
22
 
22
23
  #define METHOD(name) xt_value name(xt_value self, xt_value env, int32_t argc, xt_value *argv)
@@ -24,7 +25,7 @@
24
25
  namespace domapi {
25
26
  namespace {
26
27
 
27
- xtgui::WebGLContext *context_of(xt_value self) { return webgl_from_context(self); }
28
+ xtgui::WebGLContext *context_of(xt_value self) { return webgl_live_context(self); }
28
29
 
29
30
  uint32_t texture_argument(xt_value value) { return webgl_object_id(value, "__xt_gl_texture"); }
30
31
 
@@ -35,53 +36,8 @@ int bytes_per_pixel(int format) {
35
36
  return 1;
36
37
  }
37
38
 
38
- void convert_pixels(const uint8_t *source, int count, int format, bool premultiply,
39
- std::vector<uint8_t> *out) {
40
- out->assign((size_t)count * 4, 255);
41
- for (int i = 0; i < count; i++) {
42
- uint8_t r = 0;
43
- uint8_t g = 0;
44
- uint8_t b = 0;
45
- uint8_t a = 255;
46
- switch (format) {
47
- case GL_RGBA:
48
- r = source[i * 4 + 0];
49
- g = source[i * 4 + 1];
50
- b = source[i * 4 + 2];
51
- a = source[i * 4 + 3];
52
- break;
53
- case GL_RGB:
54
- r = source[i * 3 + 0];
55
- g = source[i * 3 + 1];
56
- b = source[i * 3 + 2];
57
- break;
58
- case GL_LUMINANCE_ALPHA:
59
- r = g = b = source[i * 2 + 0];
60
- a = source[i * 2 + 1];
61
- break;
62
- case GL_ALPHA:
63
- a = source[i];
64
- r = g = b = 0;
65
- break;
66
- case GL_LUMINANCE:
67
- default:
68
- r = g = b = source[i];
69
- break;
70
- }
71
- if (premultiply) {
72
- r = (uint8_t)((r * a + 127) / 255);
73
- g = (uint8_t)((g * a + 127) / 255);
74
- b = (uint8_t)((b * a + 127) / 255);
75
- }
76
- (*out)[(size_t)i * 4 + 0] = r;
77
- (*out)[(size_t)i * 4 + 1] = g;
78
- (*out)[(size_t)i * 4 + 2] = b;
79
- (*out)[(size_t)i * 4 + 3] = a;
80
- }
81
- }
82
-
83
- void flip_rows(std::vector<uint8_t> *pixels, int width, int height) {
84
- const size_t stride = (size_t)width * 4;
39
+ void flip_rows_bpp(std::vector<uint8_t> *pixels, int width, int height, int bpp) {
40
+ const size_t stride = (size_t)width * bpp;
85
41
  std::vector<uint8_t> row(stride);
86
42
  for (int y = 0; y < height / 2; y++) {
87
43
  uint8_t *top = pixels->data() + (size_t)y * stride;
@@ -92,63 +48,182 @@ void flip_rows(std::vector<uint8_t> *pixels, int width, int height) {
92
48
  }
93
49
  }
94
50
 
95
- /** Decode a DOM source into top-down RGBA8. */
96
- bool read_source(xt_value source, int *width, int *height, std::vector<uint8_t> *rgba,
97
- bool premultiply) {
98
- XtGuiWindow *win = nullptr;
99
- xtgui::Node *node = resolve_node(source, &win);
100
- const xtgui::Image *image = nullptr;
101
- std::vector<uint8_t> owned;
102
- if (node != nullptr && node->isElement() && node->isTag("img")) {
103
- const std::string *src = node->attr("src");
104
- if (src != nullptr) image = xtgui::xt_image_load(*src);
105
- } else if (node != nullptr && node->isElement() && node->isTag("canvas")) {
106
- if (xtgui::Canvas2D *canvas = xt_gui_canvas_find(win, node)) {
107
- image = &canvas->image();
108
- } else if (xtgui::WebGLContext *context = xt_gui_webgl_find(win, node)) {
109
- owned.resize((size_t)context->width() * context->height() * 4);
110
- if (!context->read_pixels(0, 0, context->width(), context->height(), owned.data())) {
111
- return false;
51
+ float half_to_float(uint16_t half) {
52
+ const uint32_t sign = (uint32_t)(half & 0x8000u) << 16;
53
+ uint32_t exponent = (half >> 10) & 0x1Fu;
54
+ uint32_t mantissa = half & 0x3FFu;
55
+ uint32_t bits = 0;
56
+ if (exponent == 0) {
57
+ if (mantissa != 0) {
58
+ exponent = 127 - 15 + 1;
59
+ while ((mantissa & 0x400u) == 0) {
60
+ mantissa <<= 1;
61
+ exponent--;
112
62
  }
113
- /* `readPixels` is GL-ordered (bottom-up); textures are stored top-down. */
114
- flip_rows(&owned, context->width(), context->height());
115
- *width = context->width();
116
- *height = context->height();
117
- rgba->swap(owned);
118
- return true;
63
+ mantissa &= 0x3FFu;
64
+ bits = sign | (exponent << 23) | (mantissa << 13);
65
+ } else {
66
+ bits = sign;
119
67
  }
120
- } else if (XT_IS_OBJECT(source) && !xt_is_array_buffer(source)) {
121
- xt_value data = xt_object_get_cstr(source, "data");
122
- xt_value width_value = xt_object_get_cstr(source, "width");
123
- xt_value height_value = xt_object_get_cstr(source, "height");
124
- if (XT_IS_OBJECT(data) && XT_IS_NUMBER(width_value) && XT_IS_NUMBER(height_value)) {
125
- const int w = (int)xt_to_number(width_value);
126
- const int h = (int)xt_to_number(height_value);
127
- const std::vector<uint8_t> bytes = webgl_bytes(data);
128
- if (w <= 0 || h <= 0 || bytes.size() < (size_t)w * h * 4) return false;
129
- convert_pixels(bytes.data(), w * h, GL_RGBA, premultiply, rgba);
130
- *width = w;
131
- *height = h;
132
- return true;
68
+ } else if (exponent == 31) {
69
+ bits = sign | 0x7F800000u | (mantissa << 13);
70
+ } else {
71
+ bits = sign | ((exponent + 112u) << 23) | (mantissa << 13);
72
+ }
73
+ float value = 0.0f;
74
+ memcpy(&value, &bits, sizeof(value));
75
+ return value;
76
+ }
77
+
78
+ uint16_t float_to_half(float value) {
79
+ uint32_t bits = 0;
80
+ memcpy(&bits, &value, sizeof(bits));
81
+ const uint16_t sign = (uint16_t)((bits >> 16) & 0x8000u);
82
+ const int32_t exponent = (int32_t)((bits >> 23) & 0xFFu) - 127 + 15;
83
+ uint32_t mantissa = bits & 0x7FFFFFu;
84
+ if (((bits >> 23) & 0xFFu) == 255) {
85
+ return (uint16_t)(sign | 0x7C00u | (mantissa != 0 ? 0x200u : 0));
86
+ }
87
+ if (exponent <= 0) {
88
+ if (exponent < -10) return sign;
89
+ mantissa |= 0x800000u;
90
+ const int shift = 14 - exponent;
91
+ return (uint16_t)(sign | (mantissa >> shift));
92
+ }
93
+ if (exponent >= 31) return (uint16_t)(sign | 0x7C00u);
94
+ return (uint16_t)(sign | ((uint32_t)exponent << 10) | (mantissa >> 13));
95
+ }
96
+
97
+ bool is_depth_internal(int internalformat) {
98
+ return internalformat == GL_DEPTH_COMPONENT16 || internalformat == GL_DEPTH_COMPONENT24 ||
99
+ internalformat == GL_DEPTH_COMPONENT32F || internalformat == GL_DEPTH_STENCIL ||
100
+ internalformat == GL_DEPTH24_STENCIL8;
101
+ }
102
+
103
+ bool is_float_internal(int internalformat) {
104
+ return internalformat == GL_RGBA32F || internalformat == GL_RGBA16F ||
105
+ internalformat == GL_RGB32F || internalformat == GL_RGB16F ||
106
+ internalformat == GL_RG32F || internalformat == GL_RG16F ||
107
+ internalformat == GL_R32F || internalformat == GL_R16F;
108
+ }
109
+
110
+ int source_components(int format) {
111
+ if (format == GL_RGBA) return 4;
112
+ if (format == GL_RGB) return 3;
113
+ if (format == GL_RG || format == GL_LUMINANCE_ALPHA) return 2;
114
+ return 1;
115
+ }
116
+
117
+ size_t component_bytes(int type) {
118
+ if (type == GL_FLOAT) return 4;
119
+ if (type == GL_HALF_FLOAT) return 2;
120
+ return 1;
121
+ }
122
+
123
+ double read_component(const uint8_t *source, size_t index, int type) {
124
+ if (type == GL_FLOAT) {
125
+ float value = 0.0f;
126
+ memcpy(&value, source + index, sizeof(value));
127
+ return value;
128
+ }
129
+ if (type == GL_HALF_FLOAT) {
130
+ uint16_t half = 0;
131
+ memcpy(&half, source + index, sizeof(half));
132
+ return half_to_float(half);
133
+ }
134
+ return (double)source[index] / 255.0;
135
+ }
136
+
137
+ /** Expand float sources into the RGBA layout the SDL float formats expect. */
138
+ std::vector<uint8_t> convert_float_pixels(const uint8_t *source, int count, int format, int type,
139
+ bool half) {
140
+ const int components = source_components(format);
141
+ const size_t in_bytes = component_bytes(type);
142
+ std::vector<uint8_t> out((size_t)count * 4 * (half ? 2 : 4), 0);
143
+ for (int i = 0; i < count; i++) {
144
+ float rgba[4] = {0.0f, 0.0f, 0.0f, 1.0f};
145
+ if (source != nullptr) {
146
+ double values[4] = {0.0, 0.0, 0.0, 1.0};
147
+ for (int c = 0; c < components; c++) {
148
+ values[c] = read_component(source, ((size_t)i * components + c) * in_bytes, type);
149
+ }
150
+ switch (format) {
151
+ case GL_RGBA:
152
+ rgba[0] = (float)values[0];
153
+ rgba[1] = (float)values[1];
154
+ rgba[2] = (float)values[2];
155
+ rgba[3] = (float)values[3];
156
+ break;
157
+ case GL_RGB:
158
+ rgba[0] = (float)values[0];
159
+ rgba[1] = (float)values[1];
160
+ rgba[2] = (float)values[2];
161
+ break;
162
+ case GL_RG:
163
+ rgba[0] = (float)values[0];
164
+ rgba[1] = (float)values[1];
165
+ break;
166
+ case GL_ALPHA:
167
+ rgba[3] = (float)values[0];
168
+ break;
169
+ case GL_LUMINANCE_ALPHA:
170
+ rgba[0] = rgba[1] = rgba[2] = (float)values[0];
171
+ rgba[3] = (float)values[1];
172
+ break;
173
+ default:
174
+ rgba[0] = rgba[1] = rgba[2] = (float)values[0];
175
+ break;
176
+ }
133
177
  }
134
- /* `new Image()` is a plain object with a `src`. */
135
- xt_value src = xt_object_get_cstr(source, "src");
136
- if (XT_IS_STRING(src)) {
137
- const char *path = xt_string_data(xt_to_string(src));
138
- if (path != nullptr && path[0] != '\0') image = xtgui::xt_image_load(path);
178
+ if (half) {
179
+ uint16_t *dst = reinterpret_cast<uint16_t *>(out.data()) + (size_t)i * 4;
180
+ for (int c = 0; c < 4; c++) dst[c] = float_to_half(rgba[c]);
181
+ } else {
182
+ float *dst = reinterpret_cast<float *>(out.data()) + (size_t)i * 4;
183
+ for (int c = 0; c < 4; c++) dst[c] = rgba[c];
139
184
  }
140
185
  }
141
- if (image == nullptr || image->width <= 0 || image->height <= 0) return false;
142
- *width = image->width;
143
- *height = image->height;
144
- if (premultiply) {
145
- convert_pixels(image->pixels.data(), image->width * image->height, GL_RGBA, true, rgba);
146
- } else {
147
- *rgba = image->pixels;
186
+ return out;
187
+ }
188
+
189
+ /** Convert depth source values to the D32_FLOAT texels SDL expects. */
190
+ std::vector<uint8_t> convert_depth_pixels(const uint8_t *source, int count, int type,
191
+ bool stencil) {
192
+ std::vector<uint8_t> out((size_t)count * 4, 0);
193
+ if (stencil) {
194
+ /* DEPTH_STENCIL sources use the GL_UNSIGNED_INT_24_8 packing; keep the raw
195
+ * texel, the D32_FLOAT_S8_UINT format consumes the same layout. */
196
+ if (source != nullptr) memcpy(out.data(), source, (size_t)count * 4);
197
+ return out;
198
+ }
199
+ float *dst = reinterpret_cast<float *>(out.data());
200
+ for (int i = 0; i < count; i++) {
201
+ double value = 0.0;
202
+ if (source != nullptr) {
203
+ if (type == GL_UNSIGNED_SHORT) {
204
+ uint16_t bits = 0;
205
+ memcpy(&bits, source + (size_t)i * 2, sizeof(bits));
206
+ value = (double)bits / 65535.0;
207
+ } else if (type == GL_UNSIGNED_INT) {
208
+ uint32_t bits = 0;
209
+ memcpy(&bits, source + (size_t)i * 4, sizeof(bits));
210
+ value = (double)bits / 4294967295.0;
211
+ } else if (type == GL_FLOAT) {
212
+ float bits = 0.0f;
213
+ memcpy(&bits, source + (size_t)i * 4, sizeof(bits));
214
+ value = bits;
215
+ }
216
+ }
217
+ dst[i] = (float)value;
148
218
  }
149
- return true;
219
+ return out;
150
220
  }
151
221
 
222
+ SDL_GPUTextureFormat storage_format_for(int internalformat) {
223
+ return xtgui::webgl_storage_format(internalformat);
224
+ }
225
+
226
+
152
227
  } // namespace
153
228
 
154
229
  /* -- texture objects ----------------------------------------------------- */
@@ -220,7 +295,12 @@ METHOD(webgl_tex_image_2d) {
220
295
  xtgui::WebGLContext *context = context_of(self);
221
296
  if (context == nullptr) return XT_UNDEFINED;
222
297
  const int target = webgl_int(argc, argv, 0, GL_TEXTURE_2D);
223
- if (target != GL_TEXTURE_2D) return XT_UNDEFINED;
298
+ int layer = -1;
299
+ if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
300
+ layer = target - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
301
+ } else if (target != GL_TEXTURE_2D) {
302
+ return XT_UNDEFINED;
303
+ }
224
304
  const uint32_t texture = context->bound_texture(0);
225
305
  if (texture == 0) {
226
306
  context->set_error(GL_INVALID_OPERATION);
@@ -239,20 +319,71 @@ METHOD(webgl_tex_image_2d) {
239
319
  if (argc >= 9) {
240
320
  width = webgl_int(argc, argv, 3, 0);
241
321
  height = webgl_int(argc, argv, 4, 0);
322
+ const int internalformat = webgl_int(argc, argv, 2, GL_RGBA);
242
323
  format = webgl_int(argc, argv, 6, GL_RGBA);
243
324
  const int type = webgl_int(argc, argv, 7, GL_UNSIGNED_BYTE);
244
- if (type != GL_UNSIGNED_BYTE) return XT_UNDEFINED;
245
325
  const std::vector<uint8_t> pixels = webgl_bytes(xt_arg(argc, argv, 8));
326
+ const size_t count = (size_t)width * (size_t)height;
327
+ if (width <= 0 || height <= 0) {
328
+ context->set_error(GL_INVALID_VALUE);
329
+ return XT_UNDEFINED;
330
+ }
331
+ if (is_depth_internal(internalformat)) {
332
+ const bool stencil = internalformat == GL_DEPTH_STENCIL ||
333
+ internalformat == GL_DEPTH24_STENCIL8;
334
+ if (type != GL_UNSIGNED_SHORT && type != GL_UNSIGNED_INT && type != GL_FLOAT &&
335
+ type != 0x84FA) {
336
+ context->set_error(GL_INVALID_ENUM);
337
+ return XT_UNDEFINED;
338
+ }
339
+ const size_t source_bytes = stencil ? 4 : (type == GL_UNSIGNED_SHORT ? 2 : 4);
340
+ if (!pixels.empty() && pixels.size() < count * source_bytes) {
341
+ context->set_error(GL_INVALID_VALUE);
342
+ return XT_UNDEFINED;
343
+ }
344
+ std::vector<uint8_t> data =
345
+ convert_depth_pixels(pixels.empty() ? nullptr : pixels.data(), (int)count, type, stencil);
346
+ if (flip_y) flip_rows_bpp(&data, width, height, 4);
347
+ context->tex_image_format(texture, width, height, storage_format_for(internalformat),
348
+ data.data(), layer);
349
+ return XT_UNDEFINED;
350
+ }
351
+ if (is_float_internal(internalformat) || type == GL_FLOAT || type == GL_HALF_FLOAT) {
352
+ const SDL_GPUTextureFormat sdl =
353
+ is_float_internal(internalformat)
354
+ ? storage_format_for(internalformat)
355
+ : (type == GL_HALF_FLOAT ? SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT
356
+ : SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT);
357
+ const bool half = sdl == SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT;
358
+ if (type != GL_FLOAT && type != GL_HALF_FLOAT && type != GL_UNSIGNED_BYTE) {
359
+ context->set_error(GL_INVALID_ENUM);
360
+ return XT_UNDEFINED;
361
+ }
362
+ if (!pixels.empty() &&
363
+ pixels.size() < count * (size_t)source_components(format) * component_bytes(type)) {
364
+ context->set_error(GL_INVALID_VALUE);
365
+ return XT_UNDEFINED;
366
+ }
367
+ std::vector<uint8_t> data = convert_float_pixels(
368
+ pixels.empty() ? nullptr : pixels.data(), (int)count, format, type, half);
369
+ if (flip_y) flip_rows_bpp(&data, width, height, half ? 8 : 16);
370
+ context->tex_image_format(texture, width, height, sdl, data.data(), layer);
371
+ return XT_UNDEFINED;
372
+ }
373
+ if (type != GL_UNSIGNED_BYTE) {
374
+ context->set_error(GL_INVALID_ENUM);
375
+ return XT_UNDEFINED;
376
+ }
246
377
  if (pixels.empty()) {
247
378
  rgba.assign((size_t)width * height * 4, 0);
248
379
  } else if (pixels.size() < (size_t)width * height * bytes_per_pixel(format)) {
249
380
  context->set_error(GL_INVALID_VALUE);
250
381
  return XT_UNDEFINED;
251
382
  } else {
252
- convert_pixels(pixels.data(), width * height, format, premultiply, &rgba);
383
+ webgl_convert_pixels(pixels.data(), width * height, format, premultiply, &rgba);
253
384
  }
254
385
  } else {
255
- if (!read_source(xt_arg(argc, argv, 5), &width, &height, &rgba, premultiply)) {
386
+ if (!webgl_read_source(xt_arg(argc, argv, 5), &width, &height, &rgba, premultiply)) {
256
387
  context->set_error(GL_INVALID_VALUE);
257
388
  return XT_UNDEFINED;
258
389
  }
@@ -262,8 +393,9 @@ METHOD(webgl_tex_image_2d) {
262
393
  context->set_error(GL_INVALID_VALUE);
263
394
  return XT_UNDEFINED;
264
395
  }
265
- if (flip_y) flip_rows(&rgba, width, height);
266
- context->tex_image_rgba(texture, width, height, rgba.data());
396
+ if (flip_y) flip_rows_bpp(&rgba, width, height, 4);
397
+ context->tex_image_format(texture, width, height, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
398
+ rgba.data(), layer);
267
399
  return XT_UNDEFINED;
268
400
  }
269
401
 
@@ -271,6 +403,13 @@ METHOD(webgl_tex_sub_image_2d) {
271
403
  (void)env;
272
404
  xtgui::WebGLContext *context = context_of(self);
273
405
  if (context == nullptr) return XT_UNDEFINED;
406
+ const int target = webgl_int(argc, argv, 0, GL_TEXTURE_2D);
407
+ int layer = 0;
408
+ if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
409
+ layer = target - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
410
+ } else if (target != GL_TEXTURE_2D) {
411
+ return XT_UNDEFINED;
412
+ }
274
413
  const uint32_t texture = context->bound_texture(0);
275
414
  if (texture == 0) return XT_UNDEFINED;
276
415
  const int level = webgl_int(argc, argv, 1, 0);
@@ -290,20 +429,49 @@ METHOD(webgl_tex_sub_image_2d) {
290
429
  width = webgl_int(argc, argv, 4, 0);
291
430
  height = webgl_int(argc, argv, 5, 0);
292
431
  format = webgl_int(argc, argv, 6, GL_RGBA);
432
+ const int type = webgl_int(argc, argv, 7, GL_UNSIGNED_BYTE);
293
433
  const std::vector<uint8_t> pixels = webgl_bytes(xt_arg(argc, argv, 8));
294
- if (pixels.size() < (size_t)width * height * bytes_per_pixel(format)) return XT_UNDEFINED;
295
- convert_pixels(pixels.data(), width * height, format, premultiply, &rgba);
434
+ const size_t count = (size_t)width * (size_t)height;
435
+ const int current = context->texture_format(texture);
436
+ if (current == (int)SDL_GPU_TEXTUREFORMAT_D32_FLOAT ||
437
+ current == (int)SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT) {
438
+ const bool stencil = current == (int)SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT;
439
+ if (!pixels.empty() &&
440
+ pixels.size() < count * (stencil ? 4 : (type == GL_UNSIGNED_SHORT ? 2 : 4))) {
441
+ return XT_UNDEFINED;
442
+ }
443
+ std::vector<uint8_t> data = convert_depth_pixels(
444
+ pixels.empty() ? nullptr : pixels.data(), (int)count, type, stencil);
445
+ if (flip_y) flip_rows_bpp(&data, width, height, 4);
446
+ context->tex_sub_image_format(texture, x, y, width, height, data.data(), layer);
447
+ return XT_UNDEFINED;
448
+ }
449
+ if (current == (int)SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT ||
450
+ current == (int)SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT) {
451
+ const bool half = current == (int)SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT;
452
+ if (!pixels.empty() &&
453
+ pixels.size() < count * (size_t)source_components(format) * component_bytes(type)) {
454
+ return XT_UNDEFINED;
455
+ }
456
+ std::vector<uint8_t> data = convert_float_pixels(
457
+ pixels.empty() ? nullptr : pixels.data(), (int)count, format, type, half);
458
+ if (flip_y) flip_rows_bpp(&data, width, height, half ? 8 : 16);
459
+ context->tex_sub_image_format(texture, x, y, width, height, data.data(), layer);
460
+ return XT_UNDEFINED;
461
+ }
462
+ if (pixels.size() < count * (size_t)bytes_per_pixel(format)) return XT_UNDEFINED;
463
+ webgl_convert_pixels(pixels.data(), width * height, format, premultiply, &rgba);
296
464
  } else {
297
465
  int source_width = 0;
298
466
  int source_height = 0;
299
- if (!read_source(xt_arg(argc, argv, 6), &source_width, &source_height, &rgba, premultiply)) {
467
+ if (!webgl_read_source(xt_arg(argc, argv, 6), &source_width, &source_height, &rgba, premultiply)) {
300
468
  return XT_UNDEFINED;
301
469
  }
302
470
  width = source_width;
303
471
  height = source_height;
304
472
  }
305
- if (flip_y) flip_rows(&rgba, width, height);
306
- context->tex_sub_image_rgba(texture, x, y, width, height, rgba.data());
473
+ if (flip_y) flip_rows_bpp(&rgba, width, height, 4);
474
+ context->tex_sub_image_format(texture, x, y, width, height, rgba.data(), layer);
307
475
  return XT_UNDEFINED;
308
476
  }
309
477
 
@@ -314,132 +482,4 @@ METHOD(webgl_generate_mipmap) {
314
482
  return XT_UNDEFINED;
315
483
  }
316
484
 
317
- /* -- framebuffers and renderbuffers -------------------------------------- */
318
-
319
- METHOD(webgl_create_framebuffer) {
320
- (void)env;
321
- (void)argc;
322
- (void)argv;
323
- xtgui::WebGLContext *context = context_of(self);
324
- if (context == nullptr) return XT_NULL;
325
- return webgl_make_object(self, "__xt_gl_framebuffer", context->create_framebuffer(),
326
- "framebuffer");
327
- }
328
-
329
- METHOD(webgl_delete_framebuffer) {
330
- (void)env;
331
- xtgui::WebGLContext *context = context_of(self);
332
- if (context != nullptr) {
333
- context->delete_framebuffer(webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_framebuffer"));
334
- }
335
- return XT_UNDEFINED;
336
- }
337
-
338
- METHOD(webgl_bind_framebuffer) {
339
- (void)env;
340
- xtgui::WebGLContext *context = context_of(self);
341
- if (context != nullptr) {
342
- context->bind_framebuffer(webgl_object_id(xt_arg(argc, argv, 1), "__xt_gl_framebuffer"));
343
- }
344
- return XT_UNDEFINED;
345
- }
346
-
347
- METHOD(webgl_framebuffer_texture_2d) {
348
- (void)env;
349
- xtgui::WebGLContext *context = context_of(self);
350
- if (context != nullptr) {
351
- context->framebuffer_texture(webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_framebuffer"),
352
- texture_argument(xt_arg(argc, argv, 3)));
353
- }
354
- return XT_UNDEFINED;
355
- }
356
-
357
- METHOD(webgl_check_framebuffer_status) {
358
- (void)env;
359
- xtgui::WebGLContext *context = context_of(self);
360
- if (context == nullptr) return xt_number(GL_FRAMEBUFFER_COMPLETE);
361
- const uint32_t framebuffer = context->bound_framebuffer_id();
362
- if (framebuffer == 0) return xt_number(GL_FRAMEBUFFER_COMPLETE);
363
- return xt_number(context->framebuffer_status(framebuffer) ? GL_FRAMEBUFFER_COMPLETE
364
- : GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
365
- }
366
-
367
- METHOD(webgl_create_renderbuffer) {
368
- (void)env;
369
- (void)argc;
370
- (void)argv;
371
- xtgui::WebGLContext *context = context_of(self);
372
- if (context == nullptr) return XT_NULL;
373
- return webgl_make_object(self, "__xt_gl_renderbuffer", context->create_renderbuffer(),
374
- "renderbuffer");
375
- }
376
-
377
- METHOD(webgl_delete_renderbuffer) {
378
- (void)env;
379
- xtgui::WebGLContext *context = context_of(self);
380
- if (context != nullptr) {
381
- context->delete_renderbuffer(webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_renderbuffer"));
382
- }
383
- return XT_UNDEFINED;
384
- }
385
-
386
- METHOD(webgl_bind_renderbuffer) {
387
- (void)env;
388
- xtgui::WebGLContext *context = context_of(self);
389
- if (context != nullptr) {
390
- context->bind_renderbuffer(webgl_object_id(xt_arg(argc, argv, 1), "__xt_gl_renderbuffer"));
391
- }
392
- return XT_UNDEFINED;
393
- }
394
-
395
- METHOD(webgl_renderbuffer_storage) {
396
- (void)env;
397
- xtgui::WebGLContext *context = context_of(self);
398
- if (context != nullptr) {
399
- context->renderbuffer_storage(context->bound_renderbuffer_id(), webgl_int(argc, argv, 3, 0),
400
- webgl_int(argc, argv, 4, 0), false);
401
- }
402
- return XT_UNDEFINED;
403
- }
404
-
405
- METHOD(webgl_framebuffer_renderbuffer) {
406
- (void)env;
407
- xtgui::WebGLContext *context = context_of(self);
408
- if (context != nullptr) {
409
- context->framebuffer_renderbuffer(
410
- webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_framebuffer"),
411
- webgl_object_id(xt_arg(argc, argv, 3), "__xt_gl_renderbuffer"));
412
- }
413
- return XT_UNDEFINED;
414
- }
415
-
416
- METHOD(webgl_get_renderbuffer_parameter) {
417
- (void)env;
418
- xtgui::WebGLContext *context = context_of(self);
419
- if (context == nullptr) return xt_number(0);
420
- xtgui::WebGLRenderbuffer *renderbuffer = context->renderbuffer(context->bound_renderbuffer_id());
421
- if (renderbuffer == nullptr) return xt_number(0);
422
- const int pname = webgl_int(argc, argv, 1, 0);
423
- if (pname == GL_RENDERBUFFER_WIDTH) return xt_number(renderbuffer->width);
424
- if (pname == GL_RENDERBUFFER_HEIGHT) return xt_number(renderbuffer->height);
425
- return xt_number(0);
426
- }
427
-
428
- METHOD(webgl_read_pixels) {
429
- (void)env;
430
- xtgui::WebGLContext *context = context_of(self);
431
- if (context == nullptr) return XT_UNDEFINED;
432
- const int x = webgl_int(argc, argv, 0, 0);
433
- const int y = webgl_int(argc, argv, 1, 0);
434
- const int width = webgl_int(argc, argv, 2, 0);
435
- const int height = webgl_int(argc, argv, 3, 0);
436
- const int format = webgl_int(argc, argv, 4, GL_RGBA);
437
- const int type = webgl_int(argc, argv, 5, GL_UNSIGNED_BYTE);
438
- if (format != GL_RGBA || type != GL_UNSIGNED_BYTE) return XT_UNDEFINED;
439
- std::vector<uint8_t> pixels((size_t)width * height * 4);
440
- if (!context->read_pixels(x, y, width, height, pixels.data())) return XT_UNDEFINED;
441
- webgl_write_bytes(xt_arg(argc, argv, 6), pixels.data(), pixels.size());
442
- return XT_UNDEFINED;
443
- }
444
-
445
485
  } // namespace domapi
@@ -0,0 +1,81 @@
1
+ /*
2
+ * xbintsc GUI engine — WebGL 2 uniform-buffer bindings.
3
+ */
4
+
5
+ #include <cstdint>
6
+ #include <string>
7
+
8
+ #include "dom_api_internal.h"
9
+ #include "gui_internal.h"
10
+ #include "webgl.h"
11
+
12
+ #define METHOD(name) xt_value name(xt_value self, xt_value env, int32_t argc, xt_value *argv)
13
+
14
+ namespace domapi {
15
+
16
+ METHOD(webgl2_bind_buffer_base) {
17
+ (void)env;
18
+ xtgui::WebGLContext *context = webgl_live_context(self);
19
+ if (context == nullptr) return XT_UNDEFINED;
20
+ context->bind_buffer_base(webgl_int(argc, argv, 0, 0), webgl_int(argc, argv, 1, 0),
21
+ webgl_object_id(xt_arg(argc, argv, 2), "__xt_gl_buffer"), 0, 0);
22
+ return XT_UNDEFINED;
23
+ }
24
+
25
+ METHOD(webgl2_bind_buffer_range) {
26
+ (void)env;
27
+ xtgui::WebGLContext *context = webgl_live_context(self);
28
+ if (context == nullptr) return XT_UNDEFINED;
29
+ context->bind_buffer_base(
30
+ webgl_int(argc, argv, 0, 0), webgl_int(argc, argv, 1, 0),
31
+ webgl_object_id(xt_arg(argc, argv, 2), "__xt_gl_buffer"),
32
+ (uint32_t)webgl_number(argc, argv, 3, 0.0), (uint32_t)webgl_number(argc, argv, 4, 0.0));
33
+ return XT_UNDEFINED;
34
+ }
35
+
36
+ METHOD(webgl2_uniform_block_binding) {
37
+ (void)env;
38
+ xtgui::WebGLContext *context = webgl_live_context(self);
39
+ if (context != nullptr) {
40
+ context->uniform_block_binding(webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_program"),
41
+ (uint32_t)webgl_number(argc, argv, 1, 0.0),
42
+ webgl_int(argc, argv, 2, 0));
43
+ }
44
+ return XT_UNDEFINED;
45
+ }
46
+
47
+ METHOD(webgl2_get_active_uniform_block_parameter) {
48
+ (void)env;
49
+ xtgui::WebGLContext *context = webgl_live_context(self);
50
+ if (context == nullptr) return xt_number(0);
51
+ return xt_number(context->uniform_block_parameter(
52
+ webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_program"),
53
+ (uint32_t)webgl_number(argc, argv, 1, 0.0), webgl_int(argc, argv, 2, 0)));
54
+ }
55
+
56
+ METHOD(webgl2_get_active_uniform_block_name) {
57
+ (void)env;
58
+ xtgui::WebGLContext *context = webgl_live_context(self);
59
+ if (context == nullptr) return XT_NULL;
60
+ const std::string name = context->uniform_block_name(
61
+ webgl_object_id(xt_arg(argc, argv, 0), "__xt_gl_program"),
62
+ (uint32_t)webgl_number(argc, argv, 1, 0.0));
63
+ return name.empty() ? XT_NULL : xt_string_from_cstr(name.c_str());
64
+ }
65
+
66
+ METHOD(webgl2_get_indexed_parameter) {
67
+ (void)env;
68
+ xtgui::WebGLContext *context = webgl_live_context(self);
69
+ if (context == nullptr) return XT_NULL;
70
+ const int target = webgl_int(argc, argv, 0, 0);
71
+ const int index = webgl_int(argc, argv, 1, 0);
72
+ if (target == GL_UNIFORM_BUFFER) {
73
+ const uint32_t buffer = context->uniform_buffer_binding(index);
74
+ return buffer != 0 ? webgl_make_object(self, "__xt_gl_buffer", buffer, "buffer") : XT_NULL;
75
+ }
76
+ return XT_NULL;
77
+ }
78
+
79
+ } // namespace domapi
80
+
81
+ #undef METHOD