xbintsc 0.5.31 → 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.
- package/README.md +25 -16
- package/README.zh-CN.md +10 -8
- package/dist/scripts/check-version.d.ts +27 -0
- package/dist/scripts/check-version.js +71 -0
- package/dist/scripts/check-version.js.map +1 -0
- package/dist/scripts/package-platform.d.ts +58 -0
- package/dist/scripts/package-platform.js +142 -0
- package/dist/scripts/package-platform.js.map +1 -0
- package/dist/scripts/verify-platform-package.d.ts +34 -0
- package/dist/scripts/verify-platform-package.js +161 -0
- package/dist/scripts/verify-platform-package.js.map +1 -0
- package/dist/src/binder/references.js +14 -1
- package/dist/src/binder/references.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +3 -1
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/module.d.ts +1 -0
- package/dist/src/codegen/generator/module.js +54 -0
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/statements/variables.js +6 -4
- package/dist/src/codegen/generator/statements/variables.js.map +1 -1
- package/dist/src/driver/runtime-lib.d.ts +29 -4
- package/dist/src/driver/runtime-lib.js +68 -10
- package/dist/src/driver/runtime-lib.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/css.d.ts +26 -0
- package/dist/src/extensions/gui/css.js +100 -0
- package/dist/src/extensions/gui/css.js.map +1 -0
- package/dist/src/extensions/gui/html.js +9 -2
- package/dist/src/extensions/gui/html.js.map +1 -1
- package/dist/src/extensions/gui/index.js +22 -7
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/gui/three.d.ts +16 -0
- package/dist/src/extensions/gui/three.js +22 -0
- package/dist/src/extensions/gui/three.js.map +1 -0
- package/dist/src/extensions/media/index.d.ts +6 -5
- package/dist/src/extensions/media/index.js +15 -16
- package/dist/src/extensions/media/index.js.map +1 -1
- package/dist/src/parser/context.d.ts +41 -0
- package/dist/src/parser/context.js +89 -4
- package/dist/src/parser/context.js.map +1 -1
- package/dist/src/parser/literals.js +6 -12
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/driver/platform-package.test.d.ts +1 -0
- package/dist/tests/driver/platform-package.test.js +49 -0
- package/dist/tests/driver/platform-package.test.js.map +1 -0
- package/dist/tests/driver/runtime-lib.test.js +51 -4
- package/dist/tests/driver/runtime-lib.test.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +24 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-objects-classes.test.js +23 -0
- package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
- package/dist/tests/e2e/gui-audio.test.d.ts +14 -0
- package/dist/tests/e2e/gui-audio.test.js +287 -0
- package/dist/tests/e2e/gui-audio.test.js.map +1 -0
- package/dist/tests/e2e/gui-browser.test.js +1 -1
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-effects.test.d.ts +6 -0
- package/dist/tests/e2e/gui-effects.test.js +262 -0
- package/dist/tests/e2e/gui-effects.test.js.map +1 -0
- package/dist/tests/e2e/gui-example-webgl.test.d.ts +10 -0
- package/dist/tests/e2e/gui-example-webgl.test.js +75 -0
- package/dist/tests/e2e/gui-example-webgl.test.js.map +1 -0
- package/dist/tests/e2e/gui-example-webgl2.test.d.ts +8 -0
- package/dist/tests/e2e/gui-example-webgl2.test.js +72 -0
- package/dist/tests/e2e/gui-example-webgl2.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +7 -0
- package/dist/tests/e2e/gui-helpers.js +1 -0
- package/dist/tests/e2e/gui-helpers.js.map +1 -1
- package/dist/tests/e2e/gui-layout.test.js +48 -0
- package/dist/tests/e2e/gui-layout.test.js.map +1 -1
- package/dist/tests/e2e/gui-media.test.js +83 -4
- package/dist/tests/e2e/gui-media.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +55 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl.test.d.ts +11 -0
- package/dist/tests/e2e/gui-webgl.test.js +437 -0
- package/dist/tests/e2e/gui-webgl.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-3d.test.d.ts +4 -0
- package/dist/tests/e2e/gui-webgl2-3d.test.js +264 -0
- package/dist/tests/e2e/gui-webgl2-3d.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.d.ts +10 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.js +262 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-formats.test.d.ts +6 -0
- package/dist/tests/e2e/gui-webgl2-formats.test.js +395 -0
- package/dist/tests/e2e/gui-webgl2-formats.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-mrt.test.d.ts +4 -0
- package/dist/tests/e2e/gui-webgl2-mrt.test.js +105 -0
- package/dist/tests/e2e/gui-webgl2-mrt.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-ubo.test.d.ts +8 -0
- package/dist/tests/e2e/gui-webgl2-ubo.test.js +112 -0
- package/dist/tests/e2e/gui-webgl2-ubo.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2.test.d.ts +10 -0
- package/dist/tests/e2e/gui-webgl2.test.js +282 -0
- package/dist/tests/e2e/gui-webgl2.test.js.map +1 -0
- package/dist/tests/extensions/gui-index.test.js +1 -0
- package/dist/tests/extensions/gui-index.test.js.map +1 -1
- package/dist/tests/extensions/gui.test.js +41 -0
- package/dist/tests/extensions/gui.test.js.map +1 -1
- package/dist/tests/extensions/media.test.js +8 -3
- package/dist/tests/extensions/media.test.js.map +1 -1
- package/dist/tests/parser/expressions.test.js +30 -0
- package/dist/tests/parser/expressions.test.js.map +1 -1
- package/dist/tests/platform-tarball.test.d.ts +1 -0
- package/dist/tests/platform-tarball.test.js +111 -0
- package/dist/tests/platform-tarball.test.js.map +1 -0
- package/dist/tests/version.test.js +6 -0
- package/dist/tests/version.test.js.map +1 -1
- package/doc/ai/contributing.md +11 -0
- package/doc/ai/extensions.md +35 -12
- package/doc/ai/troubleshooting.md +17 -11
- package/doc/ai/zh-CN/contributing.md +9 -0
- package/doc/ai/zh-CN/extensions.md +30 -10
- package/doc/ai/zh-CN/troubleshooting.md +14 -8
- package/doc/gui/api.md +63 -14
- package/doc/gui/build.md +79 -16
- package/doc/gui/canvas.md +11 -3
- package/doc/gui/dom.md +35 -17
- package/doc/gui/known-issues.md +57 -24
- package/doc/gui/layout.md +18 -0
- package/doc/gui/media.md +21 -13
- package/doc/gui/rendering.md +50 -3
- package/doc/gui/webgl.md +315 -0
- package/doc/gui.md +113 -18
- package/doc/npm-package.md +59 -29
- package/doc/requirements.md +24 -11
- package/doc/zh-CN/gui/api.md +53 -11
- package/doc/zh-CN/gui/build.md +70 -16
- package/doc/zh-CN/gui/canvas.md +9 -2
- package/doc/zh-CN/gui/dom.md +22 -7
- package/doc/zh-CN/gui/known-issues.md +45 -19
- package/doc/zh-CN/gui/layout.md +14 -0
- package/doc/zh-CN/gui/media.md +18 -11
- package/doc/zh-CN/gui/rendering.md +41 -4
- package/doc/zh-CN/gui/webgl.md +121 -0
- package/doc/zh-CN/gui.md +91 -15
- package/doc/zh-CN/npm-package.md +48 -24
- package/doc/zh-CN/requirements.md +16 -7
- package/llms.txt +2 -0
- package/package.json +17 -1
- package/runtime/ext_gui/audio.cpp +365 -153
- package/runtime/ext_gui/audio_buffer.cpp +166 -0
- package/runtime/ext_gui/audio_buffer.h +37 -0
- package/runtime/ext_gui/audio_decode.cpp +219 -0
- package/runtime/ext_gui/audio_decode.h +39 -0
- package/runtime/ext_gui/audio_device.cpp +87 -0
- package/runtime/ext_gui/audio_effects.cpp +163 -0
- package/runtime/ext_gui/audio_effects.h +25 -0
- package/runtime/ext_gui/audio_internal.h +30 -0
- package/runtime/ext_gui/audio_mixer.cpp +494 -0
- package/runtime/ext_gui/audio_mixer.h +238 -0
- package/runtime/ext_gui/audio_param.cpp +104 -0
- package/runtime/ext_gui/audio_param_object.cpp +155 -0
- package/runtime/ext_gui/browser.cpp +50 -14
- package/runtime/ext_gui/canvas.cpp +3 -18
- package/runtime/ext_gui/canvas_internal.h +10 -0
- package/runtime/ext_gui/canvas_raster.cpp +60 -0
- package/runtime/ext_gui/clip_values.cpp +246 -0
- package/runtime/ext_gui/css.cpp +94 -0
- package/runtime/ext_gui/css.h +22 -1
- package/runtime/ext_gui/css_match.cpp +3 -0
- package/runtime/ext_gui/css_selector.cpp +30 -3
- package/runtime/ext_gui/css_sheet.cpp +89 -45
- package/runtime/ext_gui/document.cpp +4 -1
- package/runtime/ext_gui/document.h +2 -0
- package/runtime/ext_gui/dom.h +16 -0
- package/runtime/ext_gui/dom_api.cpp +1 -0
- package/runtime/ext_gui/dom_api_canvas.cpp +17 -80
- package/runtime/ext_gui/dom_api_canvas_extra.cpp +130 -1
- package/runtime/ext_gui/dom_api_canvas_pending.cpp +41 -0
- package/runtime/ext_gui/dom_api_internal.h +275 -0
- package/runtime/ext_gui/dom_api_webgl.cpp +383 -0
- package/runtime/ext_gui/dom_api_webgl2.cpp +549 -0
- package/runtime/ext_gui/dom_api_webgl2_draw.cpp +269 -0
- package/runtime/ext_gui/dom_api_webgl_buffer.cpp +216 -0
- package/runtime/ext_gui/dom_api_webgl_compressed.cpp +167 -0
- package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +160 -0
- package/runtime/ext_gui/dom_api_webgl_rt.h +30 -0
- package/runtime/ext_gui/dom_api_webgl_shader.cpp +443 -0
- package/runtime/ext_gui/dom_api_webgl_source.cpp +161 -0
- package/runtime/ext_gui/dom_api_webgl_state.cpp +590 -0
- package/runtime/ext_gui/dom_api_webgl_texture.cpp +485 -0
- package/runtime/ext_gui/dom_api_webgl_ubo.cpp +81 -0
- package/runtime/ext_gui/effect_values.cpp +312 -0
- package/runtime/ext_gui/gradient_values.cpp +476 -0
- package/runtime/ext_gui/grid_values.cpp +230 -0
- package/runtime/ext_gui/gui.cpp +44 -9
- package/runtime/ext_gui/gui_canvas.cpp +50 -2
- package/runtime/ext_gui/gui_engine.h +36 -4
- package/runtime/ext_gui/gui_events.cpp +4 -1
- package/runtime/ext_gui/gui_media.cpp +1 -1
- package/runtime/ext_gui/gui_webgl.cpp +83 -0
- package/runtime/ext_gui/image_bitmap.cpp +28 -0
- package/runtime/ext_gui/image_bitmap.h +26 -0
- package/runtime/ext_gui/image_bitmap_browser.cpp +136 -0
- package/runtime/ext_gui/layout.cpp +21 -0
- package/runtime/ext_gui/layout_flow.cpp +81 -4
- package/runtime/ext_gui/layout_flow_internal.h +31 -0
- package/runtime/ext_gui/layout_grid.cpp +374 -0
- package/runtime/ext_gui/media.cpp +32 -0
- package/runtime/ext_gui/media.h +14 -1
- package/runtime/ext_gui/media_api.h +25 -2
- package/runtime/ext_gui/media_query.cpp +211 -0
- package/runtime/ext_gui/paint.cpp +111 -77
- package/runtime/ext_gui/paint.h +33 -3
- package/runtime/ext_gui/paint_background.cpp +229 -0
- package/runtime/ext_gui/paint_clip.cpp +268 -0
- package/runtime/ext_gui/paint_effects.cpp +324 -0
- package/runtime/ext_gui/paint_internal.h +85 -0
- package/runtime/ext_gui/renderer.cpp +8 -31
- package/runtime/ext_gui/renderer_clip.cpp +196 -0
- package/runtime/ext_gui/renderer_image.cpp +13 -8
- package/runtime/ext_gui/renderer_internal.h +15 -8
- package/runtime/ext_gui/renderer_text.cpp +6 -31
- package/runtime/ext_gui/style.cpp +343 -20
- package/runtime/ext_gui/style.h +144 -3
- package/runtime/ext_gui/style_print.cpp +134 -1
- package/runtime/ext_gui/style_values.cpp +152 -126
- package/runtime/ext_gui/style_values_internal.h +35 -0
- package/runtime/ext_gui/webgl.h +600 -0
- package/runtime/ext_gui/webgl2_constants.h +274 -0
- package/runtime/ext_gui/webgl_blit.cpp +95 -0
- package/runtime/ext_gui/webgl_buffer.cpp +480 -0
- package/runtime/ext_gui/webgl_buffer_objects.cpp +270 -0
- package/runtime/ext_gui/webgl_clear.cpp +142 -0
- package/runtime/ext_gui/webgl_compressed.cpp +180 -0
- package/runtime/ext_gui/webgl_constants.h +317 -0
- package/runtime/ext_gui/webgl_context.cpp +551 -0
- package/runtime/ext_gui/webgl_draw.cpp +560 -0
- package/runtime/ext_gui/webgl_draw_buffers.cpp +75 -0
- package/runtime/ext_gui/webgl_glsl.h +165 -0
- package/runtime/ext_gui/webgl_glsl_compile.cpp +437 -0
- package/runtime/ext_gui/webgl_glsl_rewrite.cpp +367 -0
- package/runtime/ext_gui/webgl_glsl_scan.cpp +590 -0
- package/runtime/ext_gui/webgl_glsl_scan_block.cpp +126 -0
- package/runtime/ext_gui/webgl_glsl_tokens.h +28 -0
- package/runtime/ext_gui/webgl_msl.cpp +62 -0
- package/runtime/ext_gui/webgl_msl.h +33 -0
- package/runtime/ext_gui/webgl_pipeline.cpp +326 -0
- package/runtime/ext_gui/webgl_program_blocks.cpp +58 -0
- package/runtime/ext_gui/webgl_program_internal.h +24 -0
- package/runtime/ext_gui/webgl_shader.cpp +594 -0
- package/runtime/ext_gui/webgl_state.cpp +228 -0
- package/runtime/ext_gui/webgl_texture.cpp +420 -0
- package/runtime/ext_gui/webgl_texture_3d.cpp +215 -0
- package/runtime/ext_gui/webgl_uniform.cpp +167 -0
- package/runtime/ext_gui/webgl_uniform_block.h +31 -0
- package/runtime/ext_gui/webgl_webgl2.cpp +277 -0
- package/runtime/ext_media/decode_audio.cpp +308 -0
- package/runtime/ext_media/media.cpp +1 -1
- package/runtime/ext_media/media_bridge.c +11 -3
- package/runtime/rt.h +17 -0
- package/runtime/rt_internal.h +1 -0
- package/runtime/xt_array_buffer.c +30 -0
- package/runtime/xt_containers/access.inc +8 -0
- package/runtime/xt_regex/api.inc +2 -0
- package/runtime/xt_regex/matcher.inc +16 -3
- package/runtime/xt_regex/parser.inc +8 -2
- package/runtime/xt_regex/types.inc +3 -0
- package/runtime/xt_stdlib2/regexp-match.inc +60 -21
- package/runtime/xt_stdlib2/regexp.inc +1 -0
- package/runtime/xt_typed_array/elements.inc +13 -0
- package/runtime/xt_typed_array.c +1 -1
- package/scripts/build-gui.ts +90 -3
- package/scripts/build-media.ts +335 -62
- package/scripts/build-runtime.ts +21 -16
- package/scripts/check-version.ts +39 -9
- package/scripts/package-platform.ts +183 -0
- package/scripts/set-platform-version.ts +62 -0
- package/scripts/setup-vs-toolchain.ps1 +152 -0
- package/scripts/verify-platform-package.ts +182 -0
- package/src/binder/references.ts +13 -1
- package/src/cli/main.ts +5 -1
- package/src/codegen/generator/module.ts +50 -0
- package/src/codegen/generator/statements/variables.ts +6 -3
- package/src/driver/runtime-lib.ts +73 -9
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/css.ts +113 -0
- package/src/extensions/gui/html.ts +9 -2
- package/src/extensions/gui/index.ts +22 -7
- package/src/extensions/gui/three.ts +25 -0
- package/src/extensions/media/index.ts +16 -18
- package/src/parser/context.ts +99 -4
- package/src/parser/literals.ts +6 -12
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WebGL draw calls.
|
|
3
|
+
*
|
|
4
|
+
* SDL_GPU has no dynamic state for blend/depth/rasterisation: those live in a
|
|
5
|
+
* `SDL_GPUGraphicsPipeline`. WebGL changes them with `glEnable`/`glDepthFunc`,
|
|
6
|
+
* so a pipeline is built lazily per (program, vertex layout, the subset of
|
|
7
|
+
* fixed-function state that matters) and cached on the program.
|
|
8
|
+
*
|
|
9
|
+
* A matrix attribute consumes several consecutive vertex locations; the
|
|
10
|
+
* pipeline spells out one `SDL_GPUVertexAttribute` per column, all reading the
|
|
11
|
+
* same vertex buffer binding.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
#include "webgl.h"
|
|
15
|
+
|
|
16
|
+
#include <algorithm>
|
|
17
|
+
#include <cstring>
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <vector>
|
|
20
|
+
|
|
21
|
+
#include "gui_internal.h"
|
|
22
|
+
|
|
23
|
+
namespace xtgui {
|
|
24
|
+
namespace {
|
|
25
|
+
|
|
26
|
+
int type_size(int type) {
|
|
27
|
+
switch (type) {
|
|
28
|
+
case GL_BYTE:
|
|
29
|
+
case GL_UNSIGNED_BYTE:
|
|
30
|
+
return 1;
|
|
31
|
+
case GL_SHORT:
|
|
32
|
+
case GL_UNSIGNED_SHORT:
|
|
33
|
+
return 2;
|
|
34
|
+
default:
|
|
35
|
+
return 4;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
int components_of_type(const std::string &type) {
|
|
40
|
+
if (!type.empty() && type.back() >= '2' && type.back() <= '4') return type.back() - '0';
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
SDL_GPUVertexElementFormat float_format(int size) {
|
|
45
|
+
switch (size) {
|
|
46
|
+
case 1:
|
|
47
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_FLOAT;
|
|
48
|
+
case 2:
|
|
49
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
|
|
50
|
+
case 3:
|
|
51
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
|
|
52
|
+
default:
|
|
53
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
SDL_GPUVertexElementFormat format_of(int size, int type, bool normalized) {
|
|
58
|
+
if (size < 1 || size > 4) return SDL_GPU_VERTEXELEMENTFORMAT_INVALID;
|
|
59
|
+
if (type == GL_FLOAT) return float_format(size);
|
|
60
|
+
if (type == GL_UNSIGNED_BYTE || type == GL_BYTE) {
|
|
61
|
+
const bool is_byte = type == GL_BYTE;
|
|
62
|
+
if (size == 2) {
|
|
63
|
+
return normalized ? (is_byte ? SDL_GPU_VERTEXELEMENTFORMAT_BYTE2_NORM
|
|
64
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2_NORM)
|
|
65
|
+
: (is_byte ? SDL_GPU_VERTEXELEMENTFORMAT_BYTE2
|
|
66
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2);
|
|
67
|
+
}
|
|
68
|
+
if (size == 4) {
|
|
69
|
+
return normalized ? (is_byte ? SDL_GPU_VERTEXELEMENTFORMAT_BYTE4_NORM
|
|
70
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM)
|
|
71
|
+
: (is_byte ? SDL_GPU_VERTEXELEMENTFORMAT_BYTE4
|
|
72
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4);
|
|
73
|
+
}
|
|
74
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INVALID;
|
|
75
|
+
}
|
|
76
|
+
if (type == GL_UNSIGNED_SHORT || type == GL_SHORT) {
|
|
77
|
+
const bool is_short = type == GL_SHORT;
|
|
78
|
+
if (size == 2) {
|
|
79
|
+
return normalized ? (is_short ? SDL_GPU_VERTEXELEMENTFORMAT_SHORT2_NORM
|
|
80
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_USHORT2_NORM)
|
|
81
|
+
: (is_short ? SDL_GPU_VERTEXELEMENTFORMAT_SHORT2
|
|
82
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_USHORT2);
|
|
83
|
+
}
|
|
84
|
+
if (size == 4) {
|
|
85
|
+
return normalized ? (is_short ? SDL_GPU_VERTEXELEMENTFORMAT_SHORT4_NORM
|
|
86
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_USHORT4_NORM)
|
|
87
|
+
: (is_short ? SDL_GPU_VERTEXELEMENTFORMAT_SHORT4
|
|
88
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_USHORT4);
|
|
89
|
+
}
|
|
90
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INVALID;
|
|
91
|
+
}
|
|
92
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INVALID;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** `vertexAttribIPointer` formats: integer attributes keep their integer type
|
|
96
|
+
* in the shader (`in ivec3 …`). SDL has no 1- or 3-component byte/short
|
|
97
|
+
* integer formats, so only the sizes it can express are accepted. */
|
|
98
|
+
SDL_GPUVertexElementFormat int_format_of(int size, int type) {
|
|
99
|
+
if (size < 1 || size > 4) return SDL_GPU_VERTEXELEMENTFORMAT_INVALID;
|
|
100
|
+
if (type == GL_INT) {
|
|
101
|
+
switch (size) {
|
|
102
|
+
case 1:
|
|
103
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INT;
|
|
104
|
+
case 2:
|
|
105
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INT2;
|
|
106
|
+
case 3:
|
|
107
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INT3;
|
|
108
|
+
default:
|
|
109
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INT4;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (type == GL_UNSIGNED_INT) {
|
|
113
|
+
switch (size) {
|
|
114
|
+
case 1:
|
|
115
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_UINT;
|
|
116
|
+
case 2:
|
|
117
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_UINT2;
|
|
118
|
+
case 3:
|
|
119
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_UINT3;
|
|
120
|
+
default:
|
|
121
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_UINT4;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (type == GL_SHORT && (size == 2 || size == 4)) {
|
|
125
|
+
return size == 2 ? SDL_GPU_VERTEXELEMENTFORMAT_SHORT2 : SDL_GPU_VERTEXELEMENTFORMAT_SHORT4;
|
|
126
|
+
}
|
|
127
|
+
if (type == GL_UNSIGNED_SHORT && (size == 2 || size == 4)) {
|
|
128
|
+
return size == 2 ? SDL_GPU_VERTEXELEMENTFORMAT_USHORT2
|
|
129
|
+
: SDL_GPU_VERTEXELEMENTFORMAT_USHORT4;
|
|
130
|
+
}
|
|
131
|
+
if (type == GL_BYTE && (size == 2 || size == 4)) {
|
|
132
|
+
return size == 2 ? SDL_GPU_VERTEXELEMENTFORMAT_BYTE2 : SDL_GPU_VERTEXELEMENTFORMAT_BYTE4;
|
|
133
|
+
}
|
|
134
|
+
if (type == GL_UNSIGNED_BYTE && (size == 2 || size == 4)) {
|
|
135
|
+
return size == 2 ? SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2 : SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4;
|
|
136
|
+
}
|
|
137
|
+
return SDL_GPU_VERTEXELEMENTFORMAT_INVALID;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A cache key covering everything (other than the program) that a
|
|
141
|
+
* `SDL_GPUGraphicsPipeline` bakes in. */
|
|
142
|
+
std::string pipeline_key(int mode, int index_size,
|
|
143
|
+
const std::vector<SDL_GPUVertexAttribute> &attrs,
|
|
144
|
+
const std::vector<SDL_GPUVertexBufferDescription> &buffers,
|
|
145
|
+
const WebGLDrawState &state) {
|
|
146
|
+
std::string key = std::to_string(mode) + "/" + std::to_string(index_size);
|
|
147
|
+
key += "/f" + std::to_string(state.color_target_count) + ":";
|
|
148
|
+
for (int i = 0; i < state.color_target_count; i++) {
|
|
149
|
+
key += std::to_string(state.color_formats[i]) + ",";
|
|
150
|
+
}
|
|
151
|
+
key += ":" + std::to_string(state.depth_format);
|
|
152
|
+
for (const SDL_GPUVertexAttribute &attr : attrs) {
|
|
153
|
+
key += "/" + std::to_string(attr.location) + ":" + std::to_string(attr.buffer_slot) + ":" +
|
|
154
|
+
std::to_string((int)attr.format) + ":" + std::to_string(attr.offset);
|
|
155
|
+
}
|
|
156
|
+
for (const SDL_GPUVertexBufferDescription &buffer : buffers) {
|
|
157
|
+
key += "|" + std::to_string(buffer.slot) + ":" + std::to_string(buffer.pitch) + ":" +
|
|
158
|
+
std::to_string((int)buffer.input_rate);
|
|
159
|
+
}
|
|
160
|
+
char state_key[256];
|
|
161
|
+
snprintf(state_key, sizeof(state_key), "|%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
|
|
162
|
+
(int)state.blend, state.blend_src_rgb, state.blend_dst_rgb, state.blend_src_alpha,
|
|
163
|
+
state.blend_dst_alpha, state.blend_eq_rgb, state.blend_eq_alpha, (int)state.depth_test,
|
|
164
|
+
(int)state.depth_write, state.depth_func, (int)state.cull, state.cull_face,
|
|
165
|
+
state.front_face, (int)state.color_mask[0]);
|
|
166
|
+
key += state_key;
|
|
167
|
+
key += std::to_string((int)state.color_mask[1]);
|
|
168
|
+
key += std::to_string((int)state.color_mask[2]);
|
|
169
|
+
key += std::to_string((int)state.color_mask[3]);
|
|
170
|
+
char stencil_key[224];
|
|
171
|
+
snprintf(stencil_key, sizeof(stencil_key), "|s%d,%d,%d,%d,%d,%u,%d,%d,%d,%d,%u,%u,%u",
|
|
172
|
+
(int)state.stencil, state.stencil_front.func, state.stencil_front.fail,
|
|
173
|
+
state.stencil_front.zfail, state.stencil_front.zpass,
|
|
174
|
+
(unsigned)state.stencil_front.write_mask, state.stencil_back.func,
|
|
175
|
+
state.stencil_back.fail, state.stencil_back.zfail, state.stencil_back.zpass,
|
|
176
|
+
(unsigned)state.stencil_back.write_mask,
|
|
177
|
+
(unsigned)state.stencil_front.value_mask,
|
|
178
|
+
(unsigned)state.stencil_front.write_mask);
|
|
179
|
+
key += stencil_key;
|
|
180
|
+
return key;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
} // namespace
|
|
184
|
+
|
|
185
|
+
void WebGLContext::draw_arrays(int mode, int first, int count) {
|
|
186
|
+
if (count < 0) {
|
|
187
|
+
set_error(GL_INVALID_VALUE);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
draw(mode, first, count, 0, 0, 1);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
void WebGLContext::draw_elements(int mode, int count, int type, int offset) {
|
|
194
|
+
if (count < 0) {
|
|
195
|
+
set_error(GL_INVALID_VALUE);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (type != GL_UNSIGNED_SHORT && type != GL_UNSIGNED_INT && type != GL_UNSIGNED_BYTE) {
|
|
199
|
+
set_error(GL_INVALID_ENUM);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
draw(mode, 0, count, type, offset, 1);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
void WebGLContext::draw_arrays_instanced(int mode, int first, int count, int instances) {
|
|
206
|
+
if (count < 0 || instances < 0) {
|
|
207
|
+
set_error(GL_INVALID_VALUE);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
draw(mode, first, count, 0, 0, instances > 0 ? instances : 1);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
void WebGLContext::draw_elements_instanced(int mode, int count, int type, int offset,
|
|
214
|
+
int instances) {
|
|
215
|
+
if (count < 0 || instances < 0) {
|
|
216
|
+
set_error(GL_INVALID_VALUE);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (type != GL_UNSIGNED_SHORT && type != GL_UNSIGNED_INT && type != GL_UNSIGNED_BYTE) {
|
|
220
|
+
set_error(GL_INVALID_ENUM);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
draw(mode, 0, count, type, offset, instances > 0 ? instances : 1);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
void WebGLContext::draw_range_elements(int mode, int start, int end, int count, int type,
|
|
227
|
+
int offset) {
|
|
228
|
+
(void)start;
|
|
229
|
+
(void)end;
|
|
230
|
+
draw_elements(mode, count, type, offset);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
void WebGLContext::draw(int mode, int first, int count, int index_type, int offset,
|
|
234
|
+
int instances) {
|
|
235
|
+
if (count <= 0) return;
|
|
236
|
+
WebGLProgram *program_ = program(current_program_);
|
|
237
|
+
if (program_ == nullptr || !program_->linked || program_->vs_shader == nullptr) {
|
|
238
|
+
set_error(GL_INVALID_OPERATION);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (mode != GL_POINTS && mode != GL_LINES && mode != GL_LINE_STRIP && mode != GL_TRIANGLES &&
|
|
242
|
+
mode != GL_TRIANGLE_STRIP && mode != GL_TRIANGLE_FAN && mode != GL_LINE_LOOP) {
|
|
243
|
+
set_error(GL_INVALID_ENUM);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
ensure_fallback();
|
|
247
|
+
end_pass();
|
|
248
|
+
ensure_command();
|
|
249
|
+
|
|
250
|
+
/* Expand source indices when the draw needs a primitive SDL_GPU lacks or a
|
|
251
|
+
* byte-sized index type. */
|
|
252
|
+
std::vector<uint32_t> expanded;
|
|
253
|
+
SDL_GPUBuffer *index_gpu = nullptr;
|
|
254
|
+
int index_size = 0;
|
|
255
|
+
int first_index = 0;
|
|
256
|
+
int draw_mode = mode;
|
|
257
|
+
const bool needs_expand =
|
|
258
|
+
mode == GL_TRIANGLE_FAN || mode == GL_LINE_LOOP || index_type == GL_UNSIGNED_BYTE;
|
|
259
|
+
if (needs_expand) {
|
|
260
|
+
std::vector<uint32_t> source;
|
|
261
|
+
source.reserve((size_t)count);
|
|
262
|
+
if (index_type != 0) {
|
|
263
|
+
WebGLBuffer *element = buffer(bound_element_);
|
|
264
|
+
if (element == nullptr || element->data.empty()) {
|
|
265
|
+
set_error(GL_INVALID_OPERATION);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const int element_size = type_size(index_type);
|
|
269
|
+
for (int i = 0; i < count; i++) {
|
|
270
|
+
const size_t at = (size_t)offset + (size_t)i * element_size;
|
|
271
|
+
if (at + element_size > element->data.size()) {
|
|
272
|
+
source.push_back(0);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
uint32_t value = element->data[at];
|
|
276
|
+
if (element_size >= 2) value |= (uint32_t)element->data[at + 1] << 8;
|
|
277
|
+
if (element_size >= 4) {
|
|
278
|
+
value |= (uint32_t)element->data[at + 2] << 16;
|
|
279
|
+
value |= (uint32_t)element->data[at + 3] << 24;
|
|
280
|
+
}
|
|
281
|
+
source.push_back(value);
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
for (int i = 0; i < count; i++) source.push_back((uint32_t)(first + i));
|
|
285
|
+
}
|
|
286
|
+
if (mode == GL_TRIANGLE_FAN) {
|
|
287
|
+
for (size_t i = 1; i + 1 < source.size(); i++) {
|
|
288
|
+
expanded.push_back(source[0]);
|
|
289
|
+
expanded.push_back(source[i]);
|
|
290
|
+
expanded.push_back(source[i + 1]);
|
|
291
|
+
}
|
|
292
|
+
draw_mode = GL_TRIANGLES;
|
|
293
|
+
} else if (mode == GL_LINE_LOOP) {
|
|
294
|
+
for (size_t i = 0; i < source.size(); i++) {
|
|
295
|
+
expanded.push_back(source[i]);
|
|
296
|
+
expanded.push_back(source[(i + 1) % source.size()]);
|
|
297
|
+
}
|
|
298
|
+
draw_mode = GL_LINES;
|
|
299
|
+
} else {
|
|
300
|
+
expanded = source;
|
|
301
|
+
draw_mode = mode;
|
|
302
|
+
}
|
|
303
|
+
index_size = 4;
|
|
304
|
+
if (!expanded.empty()) {
|
|
305
|
+
index_gpu = upload_temp_buffer(expanded.data(),
|
|
306
|
+
(uint32_t)(expanded.size() * sizeof(uint32_t)),
|
|
307
|
+
SDL_GPU_BUFFERUSAGE_INDEX);
|
|
308
|
+
if (index_gpu == nullptr) return;
|
|
309
|
+
}
|
|
310
|
+
} else if (index_type != 0) {
|
|
311
|
+
WebGLBuffer *element = buffer(bound_element_);
|
|
312
|
+
if (element == nullptr || element->data.empty()) {
|
|
313
|
+
set_error(GL_INVALID_OPERATION);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
index_gpu = ensure_buffer_gpu(element);
|
|
317
|
+
if (index_gpu == nullptr) return;
|
|
318
|
+
index_size = type_size(index_type) == 4 ? 4 : 2;
|
|
319
|
+
first_index = offset / index_size;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
WebGLFramebuffer *framebuffer_ =
|
|
323
|
+
bound_framebuffer_ != 0 ? framebuffer(bound_framebuffer_) : nullptr;
|
|
324
|
+
const int target_width = framebuffer_ != nullptr ? framebuffer_->width : width_;
|
|
325
|
+
const int target_height = framebuffer_ != nullptr ? framebuffer_->height : height_;
|
|
326
|
+
/* A pipeline bakes in the attachment formats; float colour targets, MRT
|
|
327
|
+
* layouts and depth textures therefore need their own pipeline variants. */
|
|
328
|
+
state_.color_target_count = 0;
|
|
329
|
+
for (int slot = 0; slot < kWebGLMaxDrawBuffers; slot++) {
|
|
330
|
+
if (draw_color_texture(slot) == nullptr) continue;
|
|
331
|
+
state_.color_formats[state_.color_target_count++] = draw_color_format(slot);
|
|
332
|
+
}
|
|
333
|
+
if (state_.color_target_count == 0) state_.color_target_count = 1;
|
|
334
|
+
state_.depth_format = target_depth_format();
|
|
335
|
+
|
|
336
|
+
std::vector<SDL_GPUVertexAttribute> attributes;
|
|
337
|
+
std::vector<SDL_GPUVertexBufferDescription> buffers;
|
|
338
|
+
std::vector<SDL_GPUBufferBinding> bindings;
|
|
339
|
+
|
|
340
|
+
for (const GlslUniformDecl &decl : program_->attribs) {
|
|
341
|
+
auto location_it = program_->attrib_locations.find(decl.name);
|
|
342
|
+
if (location_it == program_->attrib_locations.end()) continue;
|
|
343
|
+
const int base_location = location_it->second;
|
|
344
|
+
if (base_location < 0 || base_location >= kWebGLMaxAttribs) continue;
|
|
345
|
+
WebGLAttribState &attrib = attribs_[base_location];
|
|
346
|
+
const int slots = decl.type == "mat2" ? 2 : (decl.type == "mat3" ? 3 : (decl.type == "mat4" ? 4 : 1));
|
|
347
|
+
const int components = slots > 1 ? slots : (components_of_type(decl.type) > 4 ? 4
|
|
348
|
+
: components_of_type(decl.type));
|
|
349
|
+
const uint32_t slot = (uint32_t)buffers.size();
|
|
350
|
+
SDL_GPUVertexBufferDescription description;
|
|
351
|
+
memset(&description, 0, sizeof(description));
|
|
352
|
+
description.slot = slot;
|
|
353
|
+
description.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
|
|
354
|
+
SDL_GPUBufferBinding binding;
|
|
355
|
+
memset(&binding, 0, sizeof(binding));
|
|
356
|
+
if (!attrib.enabled) {
|
|
357
|
+
float values[4] = {attrib.current[0], attrib.current[1], attrib.current[2], attrib.current[3]};
|
|
358
|
+
binding.buffer = upload_temp_buffer(values, sizeof(values), SDL_GPU_BUFFERUSAGE_VERTEX);
|
|
359
|
+
description.pitch = 0;
|
|
360
|
+
if (binding.buffer == nullptr) continue;
|
|
361
|
+
for (int column = 0; column < slots; column++) {
|
|
362
|
+
SDL_GPUVertexAttribute attribute;
|
|
363
|
+
memset(&attribute, 0, sizeof(attribute));
|
|
364
|
+
attribute.location = (Uint32)(base_location + column);
|
|
365
|
+
attribute.buffer_slot = slot;
|
|
366
|
+
attribute.format = float_format(components);
|
|
367
|
+
attribute.offset = 0;
|
|
368
|
+
attributes.push_back(attribute);
|
|
369
|
+
}
|
|
370
|
+
buffers.push_back(description);
|
|
371
|
+
bindings.push_back(binding);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
WebGLBuffer *buffer_ = buffer(attrib.buffer);
|
|
375
|
+
if (buffer_ == nullptr || buffer_->data.empty()) {
|
|
376
|
+
set_error(GL_INVALID_OPERATION);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
SDL_GPUVertexElementFormat format = attrib.integer
|
|
380
|
+
? int_format_of(attrib.size, attrib.type)
|
|
381
|
+
: format_of(attrib.size, attrib.type, attrib.normalized);
|
|
382
|
+
if (format == SDL_GPU_VERTEXELEMENTFORMAT_INVALID) {
|
|
383
|
+
set_error(GL_INVALID_OPERATION);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
description.input_rate = attrib.divisor > 0 ? SDL_GPU_VERTEXINPUTRATE_INSTANCE
|
|
387
|
+
: SDL_GPU_VERTEXINPUTRATE_VERTEX;
|
|
388
|
+
const int stride =
|
|
389
|
+
attrib.stride > 0 ? attrib.stride : components * type_size(attrib.type) * slots;
|
|
390
|
+
description.pitch = (Uint32)stride;
|
|
391
|
+
SDL_GPUBuffer *gpu = ensure_buffer_gpu(buffer_);
|
|
392
|
+
if (gpu == nullptr) return;
|
|
393
|
+
binding.buffer = gpu;
|
|
394
|
+
binding.offset = 0;
|
|
395
|
+
for (int column = 0; column < slots; column++) {
|
|
396
|
+
SDL_GPUVertexAttribute attribute;
|
|
397
|
+
memset(&attribute, 0, sizeof(attribute));
|
|
398
|
+
attribute.location = (Uint32)(base_location + column);
|
|
399
|
+
attribute.buffer_slot = slot;
|
|
400
|
+
attribute.format = format;
|
|
401
|
+
attribute.offset = (Uint32)(attrib.offset + column * components * type_size(attrib.type));
|
|
402
|
+
attributes.push_back(attribute);
|
|
403
|
+
}
|
|
404
|
+
buffers.push_back(description);
|
|
405
|
+
bindings.push_back(binding);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
begin_pass(false, false, false);
|
|
409
|
+
if (pass_ == nullptr) return;
|
|
410
|
+
|
|
411
|
+
SDL_GPUViewport viewport;
|
|
412
|
+
memset(&viewport, 0, sizeof(viewport));
|
|
413
|
+
viewport.x = (float)viewport_[0];
|
|
414
|
+
viewport.y = (float)(target_height - (viewport_[1] + viewport_[3]));
|
|
415
|
+
viewport.w = (float)std::max(1, viewport_[2]);
|
|
416
|
+
viewport.h = (float)std::max(1, viewport_[3]);
|
|
417
|
+
viewport.min_depth = 0.0f;
|
|
418
|
+
viewport.max_depth = 1.0f;
|
|
419
|
+
SDL_SetGPUViewport(pass_, &viewport);
|
|
420
|
+
|
|
421
|
+
SDL_Rect scissor;
|
|
422
|
+
scissor.x = 0;
|
|
423
|
+
scissor.y = 0;
|
|
424
|
+
scissor.w = target_width;
|
|
425
|
+
scissor.h = target_height;
|
|
426
|
+
if (scissor_test_ && scissor_box_[2] > 0 && scissor_box_[3] > 0) {
|
|
427
|
+
scissor.x = scissor_box_[0];
|
|
428
|
+
scissor.y = target_height - (scissor_box_[1] + scissor_box_[3]);
|
|
429
|
+
scissor.w = scissor_box_[2];
|
|
430
|
+
scissor.h = scissor_box_[3];
|
|
431
|
+
}
|
|
432
|
+
if (scissor.x < 0) {
|
|
433
|
+
scissor.w += scissor.x;
|
|
434
|
+
scissor.x = 0;
|
|
435
|
+
}
|
|
436
|
+
if (scissor.y < 0) {
|
|
437
|
+
scissor.h += scissor.y;
|
|
438
|
+
scissor.y = 0;
|
|
439
|
+
}
|
|
440
|
+
if (scissor.x + scissor.w > target_width) scissor.w = target_width - scissor.x;
|
|
441
|
+
if (scissor.y + scissor.h > target_height) scissor.h = target_height - scissor.y;
|
|
442
|
+
if (scissor.w < 0) scissor.w = 0;
|
|
443
|
+
if (scissor.h < 0) scissor.h = 0;
|
|
444
|
+
SDL_SetGPUScissor(pass_, &scissor);
|
|
445
|
+
|
|
446
|
+
const std::string key = pipeline_key(draw_mode, index_size, attributes, buffers, state_);
|
|
447
|
+
SDL_GPUGraphicsPipeline *pipeline = nullptr;
|
|
448
|
+
auto cached = program_->pipeline_cache.find(key);
|
|
449
|
+
if (cached != program_->pipeline_cache.end()) {
|
|
450
|
+
pipeline = cached->second;
|
|
451
|
+
} else {
|
|
452
|
+
pipeline = create_pipeline(program_, draw_mode, index_size, (int)attributes.size(),
|
|
453
|
+
(int)buffers.size(), attributes.data(), buffers.data(), key);
|
|
454
|
+
}
|
|
455
|
+
if (pipeline == nullptr) return;
|
|
456
|
+
|
|
457
|
+
SDL_BindGPUGraphicsPipeline(pass_, pipeline);
|
|
458
|
+
if (state_.stencil) {
|
|
459
|
+
SDL_SetGPUStencilReference(pass_, (Uint8)(stencil_reference() & 0xFF));
|
|
460
|
+
}
|
|
461
|
+
if (state_.blend) {
|
|
462
|
+
SDL_FColor constants;
|
|
463
|
+
constants.r = blend_color_[0];
|
|
464
|
+
constants.g = blend_color_[1];
|
|
465
|
+
constants.b = blend_color_[2];
|
|
466
|
+
constants.a = blend_color_[3];
|
|
467
|
+
SDL_SetGPUBlendConstants(pass_, constants);
|
|
468
|
+
}
|
|
469
|
+
if (!bindings.empty()) {
|
|
470
|
+
SDL_BindGPUVertexBuffers(pass_, 0, bindings.data(), (Uint32)bindings.size());
|
|
471
|
+
}
|
|
472
|
+
if (index_size > 0) {
|
|
473
|
+
SDL_GPUBufferBinding binding;
|
|
474
|
+
binding.buffer = index_gpu;
|
|
475
|
+
binding.offset = 0;
|
|
476
|
+
SDL_BindGPUIndexBuffer(pass_, &binding,
|
|
477
|
+
index_size == 4 ? SDL_GPU_INDEXELEMENTSIZE_32BIT
|
|
478
|
+
: SDL_GPU_INDEXELEMENTSIZE_16BIT);
|
|
479
|
+
}
|
|
480
|
+
/* User uniform blocks live in the same std140 block; refresh their members
|
|
481
|
+
* from the CPU-side buffer bound to each block's binding point. */
|
|
482
|
+
for (const WebGLUniformBlock &block : program_->blocks) {
|
|
483
|
+
if (block.binding < 0 || block.binding >= 16) continue;
|
|
484
|
+
const WebGLUniformBinding &binding = uniform_bindings_[block.binding];
|
|
485
|
+
if (binding.buffer == 0) continue;
|
|
486
|
+
WebGLBuffer *uniform_buffer = buffer(binding.buffer);
|
|
487
|
+
if (uniform_buffer == nullptr) continue;
|
|
488
|
+
size_t offset = binding.offset;
|
|
489
|
+
size_t length = (size_t)block.data_size;
|
|
490
|
+
if (binding.size > 0 && length > binding.size) length = binding.size;
|
|
491
|
+
if (offset > uniform_buffer->data.size()) continue;
|
|
492
|
+
if (length > uniform_buffer->data.size() - offset) {
|
|
493
|
+
length = uniform_buffer->data.size() - offset;
|
|
494
|
+
}
|
|
495
|
+
if (block.base_offset >= 0 &&
|
|
496
|
+
block.base_offset + (int)length <= program_->block_size) {
|
|
497
|
+
memcpy(program_->block.data() + block.base_offset,
|
|
498
|
+
uniform_buffer->data.data() + offset, length);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (program_->vs_has_block && program_->block_size > 0) {
|
|
503
|
+
SDL_PushGPUVertexUniformData(cmd_, 0, program_->block.data(), (Uint32)program_->block.size());
|
|
504
|
+
}
|
|
505
|
+
if (program_->fs_has_block && program_->block_size > 0) {
|
|
506
|
+
SDL_PushGPUFragmentUniformData(cmd_, 0, program_->block.data(), (Uint32)program_->block.size());
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
auto bind_samplers = [&](const std::vector<int> &stage, bool vertex_stage) {
|
|
510
|
+
if (stage.empty()) return;
|
|
511
|
+
std::vector<SDL_GPUTextureSamplerBinding> sampler_bindings;
|
|
512
|
+
sampler_bindings.reserve(stage.size());
|
|
513
|
+
for (int uniform_index : stage) {
|
|
514
|
+
const WebGLUniform &uniform = program_->uniforms[(size_t)uniform_index];
|
|
515
|
+
const int unit =
|
|
516
|
+
uniform.unit >= 0 && uniform.unit < kWebGLMaxTextureUnits ? uniform.unit : 0;
|
|
517
|
+
uint32_t texture_id = bound_textures_[unit];
|
|
518
|
+
if (uniform.layout.gl_type == GL_SAMPLER_CUBE) {
|
|
519
|
+
texture_id = bound_cube_textures_[unit];
|
|
520
|
+
} else if (uniform.layout.gl_type == GL_SAMPLER_3D) {
|
|
521
|
+
texture_id = bound_3d_textures_[unit];
|
|
522
|
+
} else if (uniform.layout.gl_type == GL_SAMPLER_2D_ARRAY) {
|
|
523
|
+
texture_id = bound_array_textures_[unit];
|
|
524
|
+
}
|
|
525
|
+
WebGLTexture *sampled = texture(texture_id);
|
|
526
|
+
SDL_GPUTexture *gpu = sampled != nullptr ? ensure_texture_gpu(sampled, sampled->as_target)
|
|
527
|
+
: nullptr;
|
|
528
|
+
WebGLSampler *sampler_object = this->sampler(bound_samplers_[unit]);
|
|
529
|
+
SDL_GPUSampler *sampler = sampler_object != nullptr
|
|
530
|
+
? sampler_gpu(sampler_object)
|
|
531
|
+
: (sampled != nullptr ? sampler_for(sampled) : nullptr);
|
|
532
|
+
if (gpu == nullptr || sampler == nullptr) {
|
|
533
|
+
gpu = fallback_texture_;
|
|
534
|
+
sampler = fallback_sampler_;
|
|
535
|
+
}
|
|
536
|
+
SDL_GPUTextureSamplerBinding texture_binding;
|
|
537
|
+
texture_binding.texture = gpu;
|
|
538
|
+
texture_binding.sampler = sampler;
|
|
539
|
+
sampler_bindings.push_back(texture_binding);
|
|
540
|
+
}
|
|
541
|
+
if (vertex_stage) {
|
|
542
|
+
SDL_BindGPUVertexSamplers(pass_, 0, sampler_bindings.data(),
|
|
543
|
+
(Uint32)sampler_bindings.size());
|
|
544
|
+
} else {
|
|
545
|
+
SDL_BindGPUFragmentSamplers(pass_, 0, sampler_bindings.data(),
|
|
546
|
+
(Uint32)sampler_bindings.size());
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
bind_samplers(program_->vs_sampler_uniforms, true);
|
|
550
|
+
bind_samplers(program_->fs_sampler_uniforms, false);
|
|
551
|
+
|
|
552
|
+
if (index_size > 0) {
|
|
553
|
+
SDL_DrawGPUIndexedPrimitives(pass_, (Uint32)count, (Uint32)instances, (Uint32)first_index, 0,
|
|
554
|
+
0);
|
|
555
|
+
} else {
|
|
556
|
+
SDL_DrawGPUPrimitives(pass_, (Uint32)count, (Uint32)instances, (Uint32)first, 0);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WebGL 2 draw/read buffers and multiple render targets.
|
|
3
|
+
*
|
|
4
|
+
* The framebuffer stores up to four colour attachments and the draw-buffer
|
|
5
|
+
* table that maps pipeline slot N to one of them. `begin_pass` and
|
|
6
|
+
* `create_pipeline` consume the same slots, so a fragment shader's `out`
|
|
7
|
+
* variables land in the attachments the page selected with `drawBuffers`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "webgl.h"
|
|
11
|
+
|
|
12
|
+
#include "gui_internal.h"
|
|
13
|
+
|
|
14
|
+
namespace xtgui {
|
|
15
|
+
|
|
16
|
+
SDL_GPUTexture *WebGLContext::draw_color_texture(int slot) {
|
|
17
|
+
if (slot < 0 || slot >= kWebGLMaxDrawBuffers) return nullptr;
|
|
18
|
+
if (bound_framebuffer_ == 0) {
|
|
19
|
+
ensure_default_target();
|
|
20
|
+
return slot == 0 && default_draw_buffers_[0] == GL_BACK ? color_ : nullptr;
|
|
21
|
+
}
|
|
22
|
+
WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
|
|
23
|
+
if (framebuffer == nullptr) return nullptr;
|
|
24
|
+
const int buffer = framebuffer->draw_buffers[slot];
|
|
25
|
+
const int index = buffer - GL_COLOR_ATTACHMENT0;
|
|
26
|
+
if (index < 0 || index >= kWebGLMaxDrawBuffers) return nullptr;
|
|
27
|
+
if (framebuffer->color_textures[index] == 0) return nullptr;
|
|
28
|
+
WebGLTexture *texture = this->texture(framebuffer->color_textures[index]);
|
|
29
|
+
return texture != nullptr ? ensure_texture_gpu(texture, true) : nullptr;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
int WebGLContext::draw_color_format(int slot) {
|
|
33
|
+
if (bound_framebuffer_ == 0) return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
|
34
|
+
WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
|
|
35
|
+
if (framebuffer == nullptr) return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
|
36
|
+
const int index = framebuffer->draw_buffers[slot] - GL_COLOR_ATTACHMENT0;
|
|
37
|
+
if (index < 0 || index >= kWebGLMaxDrawBuffers) {
|
|
38
|
+
return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
|
39
|
+
}
|
|
40
|
+
WebGLTexture *texture = this->texture(framebuffer->color_textures[index]);
|
|
41
|
+
return texture != nullptr ? (int)texture->format : SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
int WebGLContext::draw_color_layer(int slot) {
|
|
45
|
+
if (slot < 0 || slot >= kWebGLMaxDrawBuffers || bound_framebuffer_ == 0) return 0;
|
|
46
|
+
WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
|
|
47
|
+
if (framebuffer == nullptr) return 0;
|
|
48
|
+
const int index = framebuffer->draw_buffers[slot] - GL_COLOR_ATTACHMENT0;
|
|
49
|
+
if (index < 0 || index >= kWebGLMaxDrawBuffers) return 0;
|
|
50
|
+
return framebuffer->color_layers[index];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void WebGLContext::draw_buffers(int count, const int *buffers) {
|
|
54
|
+
if (count < 0) count = 0;
|
|
55
|
+
if (count > kWebGLMaxDrawBuffers) count = kWebGLMaxDrawBuffers;
|
|
56
|
+
int *target = default_draw_buffers_;
|
|
57
|
+
if (bound_framebuffer_ != 0) {
|
|
58
|
+
WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
|
|
59
|
+
if (framebuffer == nullptr) return;
|
|
60
|
+
target = framebuffer->draw_buffers;
|
|
61
|
+
}
|
|
62
|
+
for (int i = 0; i < kWebGLMaxDrawBuffers; i++) {
|
|
63
|
+
target[i] = (i < count && buffers != nullptr) ? buffers[i] : GL_NONE;
|
|
64
|
+
}
|
|
65
|
+
end_pass();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
int WebGLContext::draw_buffer_value(int index) {
|
|
69
|
+
if (index < 0 || index >= kWebGLMaxDrawBuffers) return GL_NONE;
|
|
70
|
+
if (bound_framebuffer_ == 0) return default_draw_buffers_[index];
|
|
71
|
+
WebGLFramebuffer *framebuffer = this->framebuffer(bound_framebuffer_);
|
|
72
|
+
return framebuffer != nullptr ? framebuffer->draw_buffers[index] : GL_NONE;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
} // namespace xtgui
|