xbintsc 0.5.38 → 0.5.42

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 (132) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/codegen/generator/module.js +23 -7
  3. package/dist/src/codegen/generator/module.js.map +1 -1
  4. package/dist/src/driver/version.d.ts +1 -1
  5. package/dist/src/driver/version.js +1 -1
  6. package/dist/src/extensions/gui/html.js +9 -2
  7. package/dist/src/extensions/gui/html.js.map +1 -1
  8. package/dist/tests/codegen/closures.test.js +7 -0
  9. package/dist/tests/codegen/closures.test.js.map +1 -1
  10. package/dist/tests/e2e/compile-objects-classes.test.js +31 -0
  11. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-canvas-state.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-canvas-state.test.js +297 -0
  14. package/dist/tests/e2e/gui-canvas-state.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-data-url.test.d.ts +10 -0
  16. package/dist/tests/e2e/gui-data-url.test.js +127 -0
  17. package/dist/tests/e2e/gui-data-url.test.js.map +1 -0
  18. package/dist/tests/e2e/gui-draw-perf.test.d.ts +22 -0
  19. package/dist/tests/e2e/gui-draw-perf.test.js +405 -0
  20. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -0
  21. package/dist/tests/e2e/gui-effects.test.js +96 -0
  22. package/dist/tests/e2e/gui-effects.test.js.map +1 -1
  23. package/dist/tests/e2e/gui-paint.test.js +77 -0
  24. package/dist/tests/e2e/gui-paint.test.js.map +1 -1
  25. package/dist/tests/e2e/gui-perf.test.d.ts +23 -0
  26. package/dist/tests/e2e/gui-perf.test.js +283 -0
  27. package/dist/tests/e2e/gui-perf.test.js.map +1 -0
  28. package/dist/tests/e2e/gui-script-import-order.test.js +9 -1
  29. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  30. package/dist/tests/e2e/gui-webgl.test.js +4 -4
  31. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  32. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.d.ts +8 -0
  33. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +336 -0
  34. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -0
  35. package/dist/tests/extensions/gui.test.js +23 -0
  36. package/dist/tests/extensions/gui.test.js.map +1 -1
  37. package/doc/gui/api.md +28 -2
  38. package/doc/gui/canvas.md +114 -27
  39. package/doc/gui/known-issues.md +47 -19
  40. package/doc/gui/media.md +11 -2
  41. package/doc/gui/rendering.md +120 -18
  42. package/doc/gui/runtime.md +18 -3
  43. package/doc/gui/webgl.md +38 -19
  44. package/doc/gui.md +38 -5
  45. package/doc/zh-CN/gui/api.md +25 -2
  46. package/doc/zh-CN/gui/canvas.md +97 -26
  47. package/doc/zh-CN/gui/known-issues.md +32 -12
  48. package/doc/zh-CN/gui/media.md +9 -2
  49. package/doc/zh-CN/gui/rendering.md +89 -11
  50. package/doc/zh-CN/gui/runtime.md +13 -3
  51. package/doc/zh-CN/gui/webgl.md +27 -5
  52. package/doc/zh-CN/gui.md +25 -2
  53. package/package.json +7 -7
  54. package/runtime/ext_gui/browser.cpp +3 -1
  55. package/runtime/ext_gui/canvas.cpp +135 -89
  56. package/runtime/ext_gui/canvas.h +116 -0
  57. package/runtime/ext_gui/canvas_clip.cpp +143 -0
  58. package/runtime/ext_gui/canvas_composite.cpp +241 -0
  59. package/runtime/ext_gui/canvas_coverage.cpp +241 -0
  60. package/runtime/ext_gui/canvas_gradient.cpp +41 -4
  61. package/runtime/ext_gui/canvas_image_data.cpp +4 -0
  62. package/runtime/ext_gui/canvas_internal.h +34 -9
  63. package/runtime/ext_gui/canvas_raster.cpp +207 -178
  64. package/runtime/ext_gui/canvas_shadow.cpp +161 -0
  65. package/runtime/ext_gui/canvas_stroke.cpp +37 -9
  66. package/runtime/ext_gui/canvas_style.cpp +59 -0
  67. package/runtime/ext_gui/canvas_text.cpp +140 -0
  68. package/runtime/ext_gui/css.h +7 -0
  69. package/runtime/ext_gui/css_sheet.cpp +22 -0
  70. package/runtime/ext_gui/document.cpp +25 -1
  71. package/runtime/ext_gui/document.h +8 -0
  72. package/runtime/ext_gui/dom_api_canvas.cpp +48 -15
  73. package/runtime/ext_gui/dom_api_canvas_image.cpp +9 -0
  74. package/runtime/ext_gui/dom_api_canvas_state.cpp +217 -0
  75. package/runtime/ext_gui/dom_api_internal.h +36 -37
  76. package/runtime/ext_gui/dom_api_offscreen.cpp +213 -0
  77. package/runtime/ext_gui/dom_api_proto.cpp +18 -0
  78. package/runtime/ext_gui/dom_api_webgl.cpp +19 -9
  79. package/runtime/ext_gui/dom_api_webgl_internal.h +58 -0
  80. package/runtime/ext_gui/dom_api_webgl_source.cpp +18 -0
  81. package/runtime/ext_gui/gradient_values.cpp +56 -49
  82. package/runtime/ext_gui/gui.cpp +5 -0
  83. package/runtime/ext_gui/gui_canvas.cpp +4 -0
  84. package/runtime/ext_gui/gui_engine.h +30 -0
  85. package/runtime/ext_gui/gui_events.cpp +185 -58
  86. package/runtime/ext_gui/gui_media.cpp +3 -0
  87. package/runtime/ext_gui/image.cpp +225 -1
  88. package/runtime/ext_gui/image.h +6 -4
  89. package/runtime/ext_gui/image_bitmap_browser.cpp +11 -0
  90. package/runtime/ext_gui/layout.cpp +4 -0
  91. package/runtime/ext_gui/paint.cpp +72 -3
  92. package/runtime/ext_gui/paint.h +11 -1
  93. package/runtime/ext_gui/paint_background.cpp +110 -12
  94. package/runtime/ext_gui/paint_effects.cpp +2 -1
  95. package/runtime/ext_gui/paint_internal.h +5 -1
  96. package/runtime/ext_gui/perf.cpp +171 -0
  97. package/runtime/ext_gui/perf.h +127 -0
  98. package/runtime/ext_gui/renderer.cpp +190 -54
  99. package/runtime/ext_gui/renderer.h +52 -14
  100. package/runtime/ext_gui/renderer_image.cpp +135 -65
  101. package/runtime/ext_gui/renderer_internal.h +41 -3
  102. package/runtime/ext_gui/renderer_layer.cpp +552 -0
  103. package/runtime/ext_gui/renderer_text.cpp +24 -12
  104. package/runtime/ext_gui/style.cpp +101 -30
  105. package/runtime/ext_gui/style.h +35 -4
  106. package/runtime/ext_gui/style_print.cpp +15 -1
  107. package/runtime/ext_gui/style_values.cpp +10 -0
  108. package/runtime/ext_gui/text.cpp +23 -4
  109. package/runtime/ext_gui/text.h +29 -0
  110. package/runtime/ext_gui/text_cache.cpp +118 -0
  111. package/runtime/ext_gui/text_glyph_cache.cpp +100 -0
  112. package/runtime/ext_gui/webgl.h +48 -146
  113. package/runtime/ext_gui/webgl_blit.cpp +12 -1
  114. package/runtime/ext_gui/webgl_blit_depth.cpp +270 -0
  115. package/runtime/ext_gui/webgl_buffer.cpp +59 -1
  116. package/runtime/ext_gui/webgl_buffer_objects.cpp +106 -9
  117. package/runtime/ext_gui/webgl_clear.cpp +3 -0
  118. package/runtime/ext_gui/webgl_context.cpp +29 -3
  119. package/runtime/ext_gui/webgl_draw.cpp +75 -270
  120. package/runtime/ext_gui/webgl_draw_buffers.cpp +1 -0
  121. package/runtime/ext_gui/webgl_draw_setup.cpp +321 -0
  122. package/runtime/ext_gui/webgl_draw_state.h +130 -0
  123. package/runtime/ext_gui/webgl_glsl_scan.cpp +3 -8
  124. package/runtime/ext_gui/webgl_objects.h +148 -0
  125. package/runtime/ext_gui/webgl_pipeline.cpp +8 -8
  126. package/runtime/ext_gui/webgl_program_internal.h +35 -0
  127. package/runtime/ext_gui/webgl_shader.cpp +22 -30
  128. package/runtime/ext_gui/webgl_webgl2.cpp +36 -0
  129. package/runtime/ext_gui/window.cpp +56 -1
  130. package/src/codegen/generator/module.ts +22 -7
  131. package/src/driver/version.ts +1 -1
  132. package/src/extensions/gui/html.ts +9 -2
@@ -58,6 +58,27 @@ SDL_GPUSamplerAddressMode address_of(int wrap) {
58
58
  }
59
59
  }
60
60
 
61
+ SDL_GPUCompareOp compare_of(int func) {
62
+ switch (func) {
63
+ case GL_NEVER:
64
+ return SDL_GPU_COMPAREOP_NEVER;
65
+ case GL_LESS:
66
+ return SDL_GPU_COMPAREOP_LESS;
67
+ case GL_EQUAL:
68
+ return SDL_GPU_COMPAREOP_EQUAL;
69
+ case GL_LEQUAL:
70
+ return SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
71
+ case GL_GREATER:
72
+ return SDL_GPU_COMPAREOP_GREATER;
73
+ case GL_NOTEQUAL:
74
+ return SDL_GPU_COMPAREOP_NOT_EQUAL;
75
+ case GL_GEQUAL:
76
+ return SDL_GPU_COMPAREOP_GREATER_OR_EQUAL;
77
+ default:
78
+ return SDL_GPU_COMPAREOP_ALWAYS;
79
+ }
80
+ }
81
+
61
82
  } // namespace
62
83
 
63
84
  /* -- textures ------------------------------------------------------------ */
@@ -111,6 +132,9 @@ void WebGLContext::delete_texture(uint32_t id) {
111
132
  if (found->second->sampler != nullptr) {
112
133
  SDL_ReleaseGPUSampler(guidev::g_device, found->second->sampler);
113
134
  }
135
+ if (found->second->compare_sampler != nullptr) {
136
+ SDL_ReleaseGPUSampler(guidev::g_device, found->second->compare_sampler);
137
+ }
114
138
  }
115
139
  textures_.erase(found);
116
140
  }
@@ -159,17 +183,29 @@ bool WebGLContext::tex_parameter(uint32_t id, int pname, int value) {
159
183
  else if (pname == GL_TEXTURE_MAG_FILTER) record->mag_filter = value;
160
184
  else if (pname == GL_TEXTURE_WRAP_S) record->wrap_s = value;
161
185
  else if (pname == GL_TEXTURE_WRAP_T) record->wrap_t = value;
162
- else return true; // other parameters are accepted and ignored
186
+ else if (pname == GL_TEXTURE_COMPARE_MODE) {
187
+ record->compare_mode = value == GL_COMPARE_REF_TO_TEXTURE;
188
+ } else if (pname == GL_TEXTURE_COMPARE_FUNC) {
189
+ record->compare_func = value;
190
+ } else return true; // other parameters are accepted and ignored
163
191
  if (record->sampler != nullptr && guidev::g_device != nullptr) {
164
192
  SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
165
193
  record->sampler = nullptr;
166
194
  }
195
+ if (record->compare_sampler != nullptr && guidev::g_device != nullptr) {
196
+ SDL_ReleaseGPUSampler(guidev::g_device, record->compare_sampler);
197
+ record->compare_sampler = nullptr;
198
+ }
167
199
  return true;
168
200
  }
169
201
 
170
202
  int WebGLContext::tex_parameter_value(uint32_t id, int pname) {
171
203
  WebGLTexture *record = texture(id);
172
204
  if (record == nullptr) return 0;
205
+ if (pname == GL_TEXTURE_COMPARE_MODE) {
206
+ return record->compare_mode ? GL_COMPARE_REF_TO_TEXTURE : GL_NONE;
207
+ }
208
+ if (pname == GL_TEXTURE_COMPARE_FUNC) return record->compare_func;
173
209
  if (pname == GL_TEXTURE_MIN_FILTER) return record->min_filter;
174
210
  if (pname == GL_TEXTURE_MAG_FILTER) return record->mag_filter;
175
211
  if (pname == GL_TEXTURE_WRAP_S) return record->wrap_s;
@@ -269,6 +305,25 @@ SDL_GPUSampler *WebGLContext::sampler_for(WebGLTexture *record) {
269
305
  return record->sampler;
270
306
  }
271
307
 
308
+ SDL_GPUSampler *WebGLContext::sampler_for_compare(WebGLTexture *record) {
309
+ if (record == nullptr || guidev::g_device == nullptr) return nullptr;
310
+ if (record->compare_sampler != nullptr) return record->compare_sampler;
311
+ SDL_GPUSamplerCreateInfo info;
312
+ memset(&info, 0, sizeof(info));
313
+ info.min_filter = SDL_GPU_FILTER_LINEAR;
314
+ info.mag_filter = SDL_GPU_FILTER_LINEAR;
315
+ info.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST;
316
+ info.address_mode_u = address_of(record->wrap_s);
317
+ info.address_mode_v = address_of(record->wrap_t);
318
+ info.address_mode_w = address_of(record->wrap_t);
319
+ info.min_lod = 0.0f;
320
+ info.max_lod = record->has_mipmaps ? 1000.0f : 0.0f;
321
+ info.enable_compare = true;
322
+ info.compare_op = compare_of(record->compare_func);
323
+ record->compare_sampler = SDL_CreateGPUSampler(guidev::g_device, &info);
324
+ return record->compare_sampler;
325
+ }
326
+
272
327
  int WebGLContext::texture_width(uint32_t id) {
273
328
  WebGLTexture *record = texture(id);
274
329
  return record != nullptr ? record->width : 0;
@@ -317,12 +372,14 @@ void WebGLContext::bind_framebuffer(int target, uint32_t id) {
317
372
  if (target != GL_FRAMEBUFFER && target != GL_DRAW_FRAMEBUFFER) return;
318
373
  if (id != bound_framebuffer_) end_pass();
319
374
  bound_framebuffer_ = id;
375
+ framebuffer_version_ += 1;
320
376
  if (target == GL_FRAMEBUFFER) read_framebuffer_ = id;
321
377
  read_buffer_ = id == 0 ? GL_BACK : GL_COLOR_ATTACHMENT0;
322
378
  }
323
379
 
324
380
  bool WebGLContext::framebuffer_texture(uint32_t framebuffer_id, uint32_t texture_id,
325
381
  int attachment) {
382
+ framebuffer_version_ += 1;
326
383
  WebGLFramebuffer *record_framebuffer = framebuffer(framebuffer_id);
327
384
  WebGLTexture *record = texture(texture_id);
328
385
  if (record_framebuffer == nullptr) return false;
@@ -436,6 +493,7 @@ bool WebGLContext::renderbuffer_storage(uint32_t id, int width, int height, bool
436
493
  }
437
494
 
438
495
  bool WebGLContext::framebuffer_renderbuffer(uint32_t framebuffer_id, uint32_t renderbuffer_id) {
496
+ framebuffer_version_ += 1;
439
497
  WebGLFramebuffer *framebuffer_ = framebuffer(framebuffer_id);
440
498
  WebGLRenderbuffer *record = renderbuffer(renderbuffer_id);
441
499
  if (framebuffer_ == nullptr) return false;
@@ -12,12 +12,13 @@
12
12
  #include <cstring>
13
13
 
14
14
  #include "gui_internal.h"
15
+ #include "perf.h"
15
16
 
16
17
  namespace xtgui {
17
18
  namespace {
18
19
 
19
20
  bool upload_transfer(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, SDL_GPUBuffer *buffer,
20
- const void *bytes, uint32_t size) {
21
+ const void *bytes, uint32_t size, uint32_t offset) {
21
22
  if (size == 0) return true;
22
23
  SDL_GPUTransferBufferCreateInfo info;
23
24
  memset(&info, 0, sizeof(info));
@@ -39,6 +40,7 @@ bool upload_transfer(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, SDL_GPUBu
39
40
  SDL_GPUBufferRegion region;
40
41
  memset(&region, 0, sizeof(region));
41
42
  region.buffer = buffer;
43
+ region.offset = offset;
42
44
  region.size = size;
43
45
  SDL_UploadToGPUBuffer(copy, &source, &region, false);
44
46
  SDL_EndGPUCopyPass(copy);
@@ -46,6 +48,33 @@ bool upload_transfer(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, SDL_GPUBu
46
48
  return true;
47
49
  }
48
50
 
51
+ /** Extend a buffer's dirty range to cover `[offset, offset + size)`. Offsets
52
+ * are rounded to 4 bytes because the SDL_GPU copy path requires an aligned
53
+ * destination offset. */
54
+ void mark_dirty_range(WebGLBuffer *buffer, uint32_t offset, uint32_t size) {
55
+ if (size == 0) return;
56
+ uint32_t lo = offset & ~3u;
57
+ uint32_t hi = (offset + size + 3u) & ~3u;
58
+ if (hi > (uint32_t)buffer->data.size()) hi = (uint32_t)buffer->data.size();
59
+ if (lo > hi) lo = hi;
60
+ if (!buffer->dirty) {
61
+ buffer->dirty_lo = lo;
62
+ buffer->dirty_hi = hi;
63
+ } else {
64
+ buffer->dirty_lo = std::min(buffer->dirty_lo, lo);
65
+ buffer->dirty_hi = std::max(buffer->dirty_hi, hi);
66
+ }
67
+ buffer->dirty = true;
68
+ }
69
+
70
+ /** Round a byte size up to a power of two, so a growing buffer is reallocated
71
+ * O(log n) times instead of once per new frame's vertex count. */
72
+ uint32_t grow_capacity(uint32_t size) {
73
+ uint32_t capacity = 64;
74
+ while (capacity < size && capacity < (1u << 31)) capacity <<= 1;
75
+ return capacity < size ? size : capacity;
76
+ }
77
+
49
78
  } // namespace
50
79
 
51
80
  /* -- buffers ------------------------------------------------------------- */
@@ -66,6 +95,8 @@ void WebGLContext::delete_buffer(uint32_t id) {
66
95
  for (WebGLAttribState &attrib : attribs_) {
67
96
  if (attrib.buffer == id) attrib.buffer = 0;
68
97
  }
98
+ gpu_version_ += 1;
99
+ attribs_version_ += 1;
69
100
  if (found->second->gpu != nullptr && guidev::g_device != nullptr) {
70
101
  submit(); SDL_WaitForGPUIdle(guidev::g_device);
71
102
  SDL_ReleaseGPUBuffer(guidev::g_device, found->second->gpu);
@@ -107,7 +138,7 @@ bool WebGLContext::copy_buffer_sub_data(int read_target, int write_target, uint3
107
138
  memmove(write_buffer->data.data() + write_offset,
108
139
  read_buffer->data.data() + read_offset, size);
109
140
  }
110
- write_buffer->dirty = true;
141
+ mark_dirty_range(write_buffer, write_offset, size);
111
142
  return ensure_buffer_gpu(write_buffer) != nullptr || size == 0;
112
143
  }
113
144
 
@@ -125,39 +156,97 @@ bool WebGLContext::get_buffer_sub_data(int target, uint32_t offset, uint8_t *out
125
156
  SDL_GPUBuffer *WebGLContext::ensure_buffer_gpu(WebGLBuffer *buffer) {
126
157
  if (buffer == nullptr || guidev::g_device == nullptr) return nullptr;
127
158
  if (buffer->data.empty()) return nullptr;
159
+ const uint32_t size = (uint32_t)buffer->data.size();
160
+ const bool needs_storage = buffer->gpu == nullptr || buffer->capacity < size;
161
+ /* Steady state (data already on the GPU) must not end the open render pass:
162
+ * that was one pass restart per draw for every multi-element scene. */
163
+ if (!needs_storage && !buffer->dirty) return buffer->gpu;
128
164
  end_pass();
129
165
  ensure_command();
130
166
  if (cmd_ == nullptr) return nullptr;
131
- const uint32_t size = (uint32_t)buffer->data.size();
132
- if (buffer->gpu == nullptr || buffer->capacity < size) {
167
+ if (needs_storage) {
133
168
  if (buffer->gpu != nullptr) {
134
- submit(); SDL_WaitForGPUIdle(guidev::g_device);
169
+ submit();
170
+ SDL_WaitForGPUIdle(guidev::g_device);
135
171
  SDL_ReleaseGPUBuffer(guidev::g_device, buffer->gpu);
136
172
  }
137
173
  SDL_GPUBufferCreateInfo info;
138
174
  memset(&info, 0, sizeof(info));
139
175
  info.usage = SDL_GPU_BUFFERUSAGE_VERTEX | SDL_GPU_BUFFERUSAGE_INDEX;
140
- info.size = size;
176
+ info.size = grow_capacity(size);
141
177
  buffer->gpu = SDL_CreateGPUBuffer(guidev::g_device, &info);
142
- buffer->capacity = size;
178
+ buffer->capacity = info.size;
143
179
  if (buffer->gpu == nullptr) {
144
180
  set_error(GL_OUT_OF_MEMORY);
145
181
  return nullptr;
146
182
  }
183
+ /* A fresh buffer holds nothing: everything must be uploaded. */
184
+ buffer->dirty_lo = 0;
185
+ buffer->dirty_hi = size;
186
+ buffer->dirty = true;
187
+ /* Cached draw setups hold the old handle. */
188
+ gpu_version_ += 1;
147
189
  }
148
190
  if (buffer->dirty) {
149
191
  /* Recreating the buffer above submitted the pending command buffer. */
150
192
  ensure_command();
151
193
  if (cmd_ == nullptr) return nullptr;
152
- if (!upload_transfer(guidev::g_device, cmd_, buffer->gpu, buffer->data.data(), size)) {
194
+ uint32_t lo = std::min(buffer->dirty_lo, size);
195
+ uint32_t hi = std::min(buffer->dirty_hi, size);
196
+ if (hi > lo &&
197
+ !upload_transfer(guidev::g_device, cmd_, buffer->gpu, buffer->data.data() + lo, hi - lo,
198
+ lo)) {
153
199
  set_error(GL_OUT_OF_MEMORY);
154
200
  return nullptr;
155
201
  }
202
+ g_perf_frame.webglBufferUploadBytes += hi - lo;
156
203
  buffer->dirty = false;
204
+ buffer->dirty_lo = 0;
205
+ buffer->dirty_hi = 0;
157
206
  }
158
207
  return buffer->gpu;
159
208
  }
160
209
 
210
+ bool WebGLContext::upload_buffer_range(SDL_GPUBuffer *gpu, uint32_t offset, const void *bytes,
211
+ uint32_t size) {
212
+ if (gpu == nullptr || size == 0) return true;
213
+ end_pass();
214
+ ensure_command();
215
+ if (cmd_ == nullptr) return false;
216
+ if (!upload_transfer(guidev::g_device, cmd_, gpu, bytes, size, offset)) {
217
+ set_error(GL_OUT_OF_MEMORY);
218
+ return false;
219
+ }
220
+ g_perf_frame.webglBufferUploadBytes += size;
221
+ return true;
222
+ }
223
+
224
+ SDL_GPUBuffer *WebGLContext::constant_attrib_buffer(int index) {
225
+ if (index < 0 || index >= kWebGLMaxAttribs || guidev::g_device == nullptr) return nullptr;
226
+ WebGLAttribState &attrib = attribs_[index];
227
+ if (attrib.constant_gpu == nullptr) {
228
+ SDL_GPUBufferCreateInfo info;
229
+ memset(&info, 0, sizeof(info));
230
+ info.usage = SDL_GPU_BUFFERUSAGE_VERTEX;
231
+ info.size = sizeof(attrib.current);
232
+ attrib.constant_gpu = SDL_CreateGPUBuffer(guidev::g_device, &info);
233
+ if (attrib.constant_gpu == nullptr) {
234
+ set_error(GL_OUT_OF_MEMORY);
235
+ return nullptr;
236
+ }
237
+ attrib.constant_bytes = (uint32_t)sizeof(attrib.current);
238
+ attrib.constant_dirty = true;
239
+ }
240
+ if (attrib.constant_dirty) {
241
+ if (!upload_buffer_range(attrib.constant_gpu, 0, attrib.current,
242
+ (uint32_t)sizeof(attrib.current))) {
243
+ return nullptr;
244
+ }
245
+ attrib.constant_dirty = false;
246
+ }
247
+ return attrib.constant_gpu;
248
+ }
249
+
161
250
  bool WebGLContext::buffer_data(uint32_t id, const uint8_t *bytes, uint32_t size, int usage) {
162
251
  WebGLBuffer *record = buffer(id);
163
252
  if (record == nullptr) {
@@ -173,6 +262,8 @@ bool WebGLContext::buffer_data(uint32_t id, const uint8_t *bytes, uint32_t size,
173
262
  record->data.assign(size, 0);
174
263
  if (bytes != nullptr && size > 0) memcpy(record->data.data(), bytes, size);
175
264
  record->dirty = true;
265
+ record->dirty_lo = 0;
266
+ record->dirty_hi = size;
176
267
  return ensure_buffer_gpu(record) != nullptr || size == 0;
177
268
  }
178
269
 
@@ -185,7 +276,7 @@ bool WebGLContext::buffer_sub_data(uint32_t id, uint32_t offset, const uint8_t *
185
276
  return false;
186
277
  }
187
278
  memcpy(record->data.data() + offset, bytes, size);
188
- record->dirty = true;
279
+ mark_dirty_range(record, offset, size);
189
280
  return ensure_buffer_gpu(record) != nullptr;
190
281
  }
191
282
 
@@ -202,6 +293,7 @@ void WebGLContext::enable_attrib(int index, bool enabled) {
202
293
  return;
203
294
  }
204
295
  attribs_[index].enabled = enabled;
296
+ attribs_version_ += 1;
205
297
  }
206
298
 
207
299
  bool WebGLContext::attrib_enabled(int index) const {
@@ -222,6 +314,7 @@ void WebGLContext::attrib_pointer(int index, int size, int type, bool normalized
222
314
  attrib.integer = false;
223
315
  attrib.stride = stride > 0 ? stride : 0;
224
316
  attrib.offset = offset;
317
+ attribs_version_ += 1;
225
318
  }
226
319
 
227
320
  void WebGLContext::attrib_ipointer(int index, int size, int type, int stride, int offset) {
@@ -237,6 +330,7 @@ void WebGLContext::attrib_ipointer(int index, int size, int type, int stride, in
237
330
  attrib.integer = true;
238
331
  attrib.stride = stride > 0 ? stride : 0;
239
332
  attrib.offset = offset;
333
+ attribs_version_ += 1;
240
334
  }
241
335
 
242
336
  void WebGLContext::attrib_divisor(int index, int divisor) {
@@ -248,12 +342,15 @@ void WebGLContext::attrib_divisor(int index, int divisor) {
248
342
  * rates WebGL programs use — per vertex (0) and per instance (1) — map onto
249
343
  * the backend. A larger divisor is clamped. */
250
344
  attribs_[index].divisor = divisor > 0 ? 1 : 0;
345
+ attribs_version_ += 1;
251
346
  }
252
347
 
253
348
  void WebGLContext::attrib_value(int index, int components, const float *values) {
254
349
  if (index < 0 || index >= kWebGLMaxAttribs) return;
255
350
  for (int i = 0; i < 4; i++) attribs_[index].current[i] = i < components ? values[i] : 0.0f;
256
351
  if (components < 4) attribs_[index].current[3] = 1.0f;
352
+ attribs_[index].constant_dirty = true;
353
+ attribs_version_ += 1;
257
354
  }
258
355
 
259
356
  void WebGLContext::attrib_state(int index, int *size, int *type, int *stride, bool *normalized,
@@ -114,6 +114,8 @@ void WebGLContext::scissored_clear(int mask) {
114
114
  attribs_[attrib].type = GL_FLOAT;
115
115
  attribs_[attrib].stride = 8;
116
116
  attribs_[attrib].offset = 0;
117
+ /* The temporary attribute layout must not be mistaken for the caller's. */
118
+ attribs_version_ += 1;
117
119
  int target_width = 0;
118
120
  int target_height = 0;
119
121
  framebuffer_size(bound_framebuffer_, &target_width, &target_height);
@@ -136,6 +138,7 @@ void WebGLContext::scissored_clear(int mask) {
136
138
  current_program_ = saved_program;
137
139
  bound_array_ = saved_array;
138
140
  memcpy(attribs_, saved_attribs, sizeof(attribs_));
141
+ attribs_version_ += 1;
139
142
  for (int i = 0; i < 4; i++) viewport_[i] = saved_viewport[i];
140
143
  }
141
144
 
@@ -15,6 +15,7 @@
15
15
  #include <cstring>
16
16
 
17
17
  #include "gui_internal.h"
18
+ #include "perf.h"
18
19
 
19
20
  namespace xtgui {
20
21
  namespace {
@@ -54,13 +55,22 @@ void WebGLContext::release_gpu_resources() {
54
55
  if (guidev::g_device == nullptr) return;
55
56
  SDL_WaitForGPUIdle(guidev::g_device);
56
57
  for (auto &entry : programs_) release_program_pipelines(entry.second.get());
58
+ for (auto &entry : internal_pipelines_) {
59
+ if (entry.second != nullptr) SDL_ReleaseGPUGraphicsPipeline(guidev::g_device, entry.second);
60
+ }
61
+ internal_pipelines_.clear();
62
+ clear_draw_cache();
57
63
  for (auto &entry : textures_) {
58
64
  if (entry.second->gpu != nullptr) SDL_ReleaseGPUTexture(guidev::g_device, entry.second->gpu);
59
65
  if (entry.second->sampler != nullptr) {
60
66
  SDL_ReleaseGPUSampler(guidev::g_device, entry.second->sampler);
61
67
  }
68
+ if (entry.second->compare_sampler != nullptr) {
69
+ SDL_ReleaseGPUSampler(guidev::g_device, entry.second->compare_sampler);
70
+ }
62
71
  entry.second->gpu = nullptr;
63
72
  entry.second->sampler = nullptr;
73
+ entry.second->compare_sampler = nullptr;
64
74
  }
65
75
  for (auto &entry : buffers_) {
66
76
  if (entry.second->gpu != nullptr) SDL_ReleaseGPUBuffer(guidev::g_device, entry.second->gpu);
@@ -95,6 +105,10 @@ void WebGLContext::release_gpu_resources() {
95
105
  SDL_ReleaseGPUSampler(guidev::g_device, fallback_sampler_);
96
106
  fallback_sampler_ = nullptr;
97
107
  }
108
+ if (blit_sampler_ != nullptr) {
109
+ SDL_ReleaseGPUSampler(guidev::g_device, blit_sampler_);
110
+ blit_sampler_ = nullptr;
111
+ }
98
112
  if (color_ != nullptr) {
99
113
  SDL_ReleaseGPUTexture(guidev::g_device, color_);
100
114
  color_ = nullptr;
@@ -118,6 +132,8 @@ void WebGLContext::lose_context() {
118
132
  * after the restore. */
119
133
  shaders_.clear();
120
134
  programs_.clear();
135
+ clear_draw_cache();
136
+ internal_pipelines_.clear();
121
137
  buffers_.clear();
122
138
  textures_.clear();
123
139
  framebuffers_.clear();
@@ -198,7 +214,8 @@ void WebGLContext::ensure_default_target() {
198
214
  info.sample_count = SDL_GPU_SAMPLECOUNT_1;
199
215
  if (color_ == nullptr) color_ = SDL_CreateGPUTexture(guidev::g_device, &info);
200
216
  info.format = SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT;
201
- info.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET;
217
+ /* SAMPLER too: `blitFramebuffer` may copy the default depth attachment. */
218
+ info.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
202
219
  if (depth_ == nullptr) depth_ = SDL_CreateGPUTexture(guidev::g_device, &info);
203
220
  }
204
221
 
@@ -234,6 +251,7 @@ void WebGLContext::resize(int width, int height) {
234
251
  if (height < 0) height = 0;
235
252
  width_ = width > 0 ? width : 1;
236
253
  height_ = height > 0 ? height : 1;
254
+ framebuffer_version_ += 1;
237
255
  viewport_[0] = 0;
238
256
  viewport_[1] = 0;
239
257
  viewport_[2] = width_;
@@ -290,11 +308,15 @@ void WebGLContext::framebuffer_size(uint32_t id, int *width, int *height) {
290
308
  }
291
309
 
292
310
  SDL_GPUTexture *WebGLContext::target_depth() {
293
- if (bound_framebuffer_ == 0) {
311
+ return framebuffer_depth(bound_framebuffer_);
312
+ }
313
+
314
+ SDL_GPUTexture *WebGLContext::framebuffer_depth(uint32_t id) {
315
+ if (id == 0) {
294
316
  ensure_default_target();
295
317
  return depth_;
296
318
  }
297
- WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
319
+ WebGLFramebuffer *framebuffer = this->framebuffer(id);
298
320
  if (framebuffer == nullptr) return nullptr;
299
321
  if (framebuffer->depth_texture != 0) {
300
322
  WebGLTexture *texture = this->texture(framebuffer->depth_texture);
@@ -393,6 +415,10 @@ void WebGLContext::begin_pass(bool clear_color, bool clear_depth, bool clear_ste
393
415
 
394
416
  pass_ = SDL_BeginGPURenderPass(cmd_, color_infos, (Uint32)color_count,
395
417
  depth != nullptr ? &depth_info : nullptr);
418
+ if (pass_ != nullptr) {
419
+ g_perf_frame.webglPasses += 1;
420
+ pass_serial_ += 1;
421
+ }
396
422
  pass_targets_default_ = bound_framebuffer_ == 0;
397
423
  pass_framebuffer_ = bound_framebuffer_;
398
424
  }