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,165 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — GLSL ES for WebGL.
|
|
3
|
+
*
|
|
4
|
+
* WebGL shaders reach `shaderSource` as GLSL ES 1.00/3.00 text, while SDL_GPU's
|
|
5
|
+
* Vulkan backend consumes SPIR-V. There is no GLSL compiler in SDL_GPU, so this
|
|
6
|
+
* module does two things:
|
|
7
|
+
*
|
|
8
|
+
* 1. rewrites a WebGL shader into Vulkan-flavoured GLSL ES 3.10
|
|
9
|
+
* (`attribute` -> `in`, `varying` -> in/out with explicit locations,
|
|
10
|
+
* `gl_FragColor` -> an output, non-sampler uniforms -> one std140 block,
|
|
11
|
+
* samplers -> explicit descriptor set/binding, `texture2D` -> `texture`);
|
|
12
|
+
* 2. compiles it with glslang and reflects the resulting SPIR-V, which yields
|
|
13
|
+
* the exact std140 offsets, array strides and matrix strides of the
|
|
14
|
+
* uniform block plus the active attributes and sampler bindings.
|
|
15
|
+
*
|
|
16
|
+
* Reflection is the source of truth for layout: the shader text may size an
|
|
17
|
+
* array with a macro, but glslang has already resolved it by the time we look
|
|
18
|
+
* at the block members.
|
|
19
|
+
*/
|
|
20
|
+
#ifndef XT_GUI_WEBGL_GLSL_H
|
|
21
|
+
#define XT_GUI_WEBGL_GLSL_H
|
|
22
|
+
|
|
23
|
+
#include <cstdint>
|
|
24
|
+
#include <string>
|
|
25
|
+
|
|
26
|
+
#include "webgl_glsl_tokens.h"
|
|
27
|
+
#include <unordered_map>
|
|
28
|
+
#include <utility>
|
|
29
|
+
#include <vector>
|
|
30
|
+
|
|
31
|
+
namespace xtgui {
|
|
32
|
+
|
|
33
|
+
/** One `uniform` declaration found in a shader source. */
|
|
34
|
+
struct GlslUniformDecl {
|
|
35
|
+
std::string type; // `mat4`, `vec3`, `sampler2D`, …
|
|
36
|
+
std::string name;
|
|
37
|
+
std::string array; // `[4]` (raw text, may contain a macro)
|
|
38
|
+
std::string precision; // `highp` / `mediump` / `lowp` / empty
|
|
39
|
+
bool sampler = false;
|
|
40
|
+
/** Extra qualifiers (`layout(...)`) that were already present; empty for a
|
|
41
|
+
* WebGL 1 source. */
|
|
42
|
+
std::string extra;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** One member of a user-declared uniform block. */
|
|
46
|
+
struct GlslBlockMemberDecl {
|
|
47
|
+
std::string type;
|
|
48
|
+
std::string name;
|
|
49
|
+
std::string array;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** A user-declared `uniform Block { … } instance;`. The rewriter merges its
|
|
53
|
+
* members into the engine block under `__ub_<instance>_<member>` names and
|
|
54
|
+
* rewrites references, because SDL_GPU has no bindable uniform buffers. */
|
|
55
|
+
struct GlslUniformBlock {
|
|
56
|
+
std::string name;
|
|
57
|
+
std::string instance;
|
|
58
|
+
std::vector<GlslBlockMemberDecl> members;
|
|
59
|
+
size_t begin = 0;
|
|
60
|
+
size_t end = 0;
|
|
61
|
+
bool conditional = false;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** One `instance.member` reference to rename. */
|
|
65
|
+
struct GlslRename {
|
|
66
|
+
size_t begin = 0;
|
|
67
|
+
size_t end = 0;
|
|
68
|
+
std::string text;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Where one declaration sits in the source text, and what it declares. */
|
|
72
|
+
struct GlslDeclSite {
|
|
73
|
+
enum Kind { Uniform, Sampler, Attribute, Varying, FragmentOut };
|
|
74
|
+
Kind kind = Uniform;
|
|
75
|
+
size_t begin = 0; // byte offset of the first token (`layout` included)
|
|
76
|
+
size_t end = 0; // byte offset just past the `;`
|
|
77
|
+
GlslUniformDecl decl;
|
|
78
|
+
int existing_location = -1; // from a `layout(location = …)` qualifier
|
|
79
|
+
/** True when a preprocessor `#if`/`#ifdef` encloses the declaration. */
|
|
80
|
+
bool conditional = false;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** The declarations a shader contributes to a program. */
|
|
84
|
+
struct GlslShaderInfo;
|
|
85
|
+
|
|
86
|
+
/** Parse a user uniform block at `uniform_index`, merging its members. Returns
|
|
87
|
+
* true when a block declaration was consumed; `after` is the next token. */
|
|
88
|
+
bool scan_uniform_block(const std::vector<GlslToken> &tokens, size_t uniform_index,
|
|
89
|
+
size_t pending_layout, bool conditional, GlslShaderInfo *info,
|
|
90
|
+
size_t *after, size_t *first_uniform_any);
|
|
91
|
+
|
|
92
|
+
struct GlslShaderInfo {
|
|
93
|
+
std::vector<GlslUniformDecl> uniforms; // non-sampler
|
|
94
|
+
std::vector<GlslUniformDecl> samplers;
|
|
95
|
+
std::vector<GlslUniformDecl> attributes; // vertex only
|
|
96
|
+
std::vector<GlslUniformDecl> varyings;
|
|
97
|
+
/** Every top-level declaration, in source order, for the rewriter. */
|
|
98
|
+
std::vector<GlslDeclSite> sites;
|
|
99
|
+
/** Active top-level `struct Name { … };` definitions, hoisted before the
|
|
100
|
+
* uniform block because the block may name those types (three.js lights). */
|
|
101
|
+
std::vector<std::pair<size_t, size_t>> structs;
|
|
102
|
+
/** The text of those definitions, shared across stages at link time. */
|
|
103
|
+
std::vector<std::string> struct_definitions;
|
|
104
|
+
/** User uniform blocks, merged into the single engine block at link time. */
|
|
105
|
+
std::vector<GlslUniformBlock> blocks;
|
|
106
|
+
/** References the rewriter must spell with the prefixed member name. */
|
|
107
|
+
std::vector<GlslRename> renames;
|
|
108
|
+
/** True when the source writes `gl_FragColor` / `gl_FragData[0]`. */
|
|
109
|
+
bool legacy_fragment_output = false;
|
|
110
|
+
/** True when the source declared its own `out` variable (ES 3.00). */
|
|
111
|
+
bool has_fragment_output = false;
|
|
112
|
+
/** Byte offset of the first non-sampler uniform declaration (block insertion
|
|
113
|
+
* point); `npos` when the shader has none. */
|
|
114
|
+
size_t first_uniform_offset = std::string::npos;
|
|
115
|
+
/** A declaration this engine cannot translate (struct uniform, block, …). */
|
|
116
|
+
std::string unsupported;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** Locations and bindings assigned at link time. */
|
|
120
|
+
struct GlslLinkLayout {
|
|
121
|
+
std::unordered_map<std::string, int> attributes; // name -> location
|
|
122
|
+
std::unordered_map<std::string, int> varyings; // name -> location
|
|
123
|
+
/** stage-local sampler binding: name -> binding index inside the stage. */
|
|
124
|
+
std::unordered_map<std::string, int> samplers;
|
|
125
|
+
/** Merged, declaration-ordered non-sampler uniforms (shared block members). */
|
|
126
|
+
std::vector<GlslUniformDecl> block_members;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/** Scan `source` for the declarations WebGL needs to know about. */
|
|
130
|
+
void scan_glsl(const std::string &source, bool fragment, GlslShaderInfo *info);
|
|
131
|
+
|
|
132
|
+
/** Rewrite `source` into Vulkan GLSL ES 3.10 (`layout` added everywhere). */
|
|
133
|
+
std::string rewrite_glsl(const std::string &source, bool fragment, const GlslLinkLayout &layout,
|
|
134
|
+
const GlslShaderInfo &info);
|
|
135
|
+
|
|
136
|
+
/** Compile a rewritten shader to SPIR-V. Returns false and fills `log`. */
|
|
137
|
+
bool compile_glsl(const std::string &source, bool fragment, std::vector<uint32_t> *spirv,
|
|
138
|
+
std::string *log);
|
|
139
|
+
|
|
140
|
+
/** One std140 block member, reflected from SPIR-V. */
|
|
141
|
+
struct GlslBlockMember {
|
|
142
|
+
std::string name;
|
|
143
|
+
int gl_type = 0; // GL_FLOAT, GL_FLOAT_VEC4, GL_FLOAT_MAT4, GL_INT_VEC2, …
|
|
144
|
+
int components = 0; // 1..4
|
|
145
|
+
bool is_matrix = false;
|
|
146
|
+
int columns = 0; // matrix columns; 0 for non-matrix members
|
|
147
|
+
int rows = 0;
|
|
148
|
+
bool is_array = false;
|
|
149
|
+
int array_size = 1;
|
|
150
|
+
int offset = 0; // byte offset of the member in the block
|
|
151
|
+
int array_stride = 0; // byte stride between array elements
|
|
152
|
+
int matrix_stride = 0; // byte stride between matrix columns
|
|
153
|
+
int scalar_type = 0; // GL_FLOAT / GL_INT / GL_BOOL
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** Reflect the (single) uniform block of a compiled shader. */
|
|
157
|
+
bool reflect_uniform_block(const std::vector<uint32_t> &spirv, std::vector<GlslBlockMember> *out);
|
|
158
|
+
|
|
159
|
+
/** Reflect `in` variable locations (vertex attributes) from SPIR-V. */
|
|
160
|
+
bool reflect_attributes(const std::vector<uint32_t> &spirv,
|
|
161
|
+
std::unordered_map<std::string, int> *out);
|
|
162
|
+
|
|
163
|
+
} // namespace xtgui
|
|
164
|
+
|
|
165
|
+
#endif /* XT_GUI_WEBGL_GLSL_H */
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — glslang integration and SPIR-V reflection.
|
|
3
|
+
*
|
|
4
|
+
* `compile_glsl` is the only place that calls into glslang. The reflection
|
|
5
|
+
* helpers read the generated module directly: WebGL has to know the exact
|
|
6
|
+
* std140 offsets of the uniform block (so `uniformMatrix4fv` writes the right
|
|
7
|
+
* bytes) and the active vertex attributes, and the SPIR-V already carries both
|
|
8
|
+
* as decorations.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include "webgl_glsl.h"
|
|
12
|
+
|
|
13
|
+
#include <cstdio>
|
|
14
|
+
#include <cstdlib>
|
|
15
|
+
#include <cstring>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <unordered_map>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
#include <glslang/Public/ResourceLimits.h>
|
|
21
|
+
#include <glslang/Public/ShaderLang.h>
|
|
22
|
+
#include <SPIRV/GlslangToSpv.h>
|
|
23
|
+
|
|
24
|
+
namespace xtgui {
|
|
25
|
+
namespace {
|
|
26
|
+
|
|
27
|
+
/* SPIR-V words are little-endian 32-bit values. */
|
|
28
|
+
constexpr uint16_t kOpName = 5;
|
|
29
|
+
constexpr uint16_t kOpMemberName = 6;
|
|
30
|
+
constexpr uint16_t kOpTypeBool = 20;
|
|
31
|
+
constexpr uint16_t kOpTypeInt = 21;
|
|
32
|
+
constexpr uint16_t kOpTypeFloat = 22;
|
|
33
|
+
constexpr uint16_t kOpTypeVector = 23;
|
|
34
|
+
constexpr uint16_t kOpTypeMatrix = 24;
|
|
35
|
+
constexpr uint16_t kOpTypeArray = 28;
|
|
36
|
+
constexpr uint16_t kOpTypeRuntimeArray = 29;
|
|
37
|
+
constexpr uint16_t kOpTypeStruct = 30;
|
|
38
|
+
constexpr uint16_t kOpTypePointer = 32;
|
|
39
|
+
constexpr uint16_t kOpConstant = 43;
|
|
40
|
+
constexpr uint16_t kOpVariable = 59;
|
|
41
|
+
constexpr uint16_t kOpDecorate = 71;
|
|
42
|
+
constexpr uint16_t kOpMemberDecorate = 72;
|
|
43
|
+
|
|
44
|
+
constexpr uint32_t kDecorationBlock = 2;
|
|
45
|
+
constexpr uint32_t kDecorationArrayStride = 6;
|
|
46
|
+
constexpr uint32_t kDecorationMatrixStride = 7;
|
|
47
|
+
constexpr uint32_t kDecorationLocation = 30;
|
|
48
|
+
constexpr uint32_t kDecorationOffset = 35;
|
|
49
|
+
constexpr uint32_t kDecorationBinding = 33;
|
|
50
|
+
|
|
51
|
+
constexpr uint32_t kStorageInput = 1;
|
|
52
|
+
|
|
53
|
+
/* GL type enums (GLES2 values). */
|
|
54
|
+
constexpr int kGlFloat = 0x1406;
|
|
55
|
+
constexpr int kGlInt = 0x1404;
|
|
56
|
+
constexpr int kGlBool = 0x8B56;
|
|
57
|
+
constexpr int kGlFloatVec2 = 0x8B50;
|
|
58
|
+
constexpr int kGlFloatVec3 = 0x8B51;
|
|
59
|
+
constexpr int kGlFloatVec4 = 0x8B52;
|
|
60
|
+
constexpr int kGlIntVec2 = 0x8B53;
|
|
61
|
+
constexpr int kGlIntVec3 = 0x8B54;
|
|
62
|
+
constexpr int kGlIntVec4 = 0x8B55;
|
|
63
|
+
constexpr int kGlFloatMat2 = 0x8B5A;
|
|
64
|
+
constexpr int kGlFloatMat3 = 0x8B5B;
|
|
65
|
+
constexpr int kGlFloatMat4 = 0x8B5C;
|
|
66
|
+
|
|
67
|
+
struct TypeInfo {
|
|
68
|
+
uint16_t op = 0;
|
|
69
|
+
uint32_t a = 0; // component/element/pointee type
|
|
70
|
+
uint32_t b = 0; // count / length constant
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
struct Decor {
|
|
74
|
+
uint32_t set = 0;
|
|
75
|
+
uint32_t binding = 0;
|
|
76
|
+
int location = -1;
|
|
77
|
+
uint32_t array_stride = 0;
|
|
78
|
+
bool block = false;
|
|
79
|
+
bool has_descriptor_set = false;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
struct MemberDecor {
|
|
83
|
+
uint32_t offset = 0;
|
|
84
|
+
uint32_t matrix_stride = 0;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
std::string string_at(const std::vector<uint32_t> &words, size_t at) {
|
|
88
|
+
std::string text;
|
|
89
|
+
for (size_t i = at; i < words.size(); i++) {
|
|
90
|
+
const uint32_t word = words[i];
|
|
91
|
+
for (int byte = 0; byte < 4; byte++) {
|
|
92
|
+
const char c = (char)((word >> (byte * 8)) & 0xFF);
|
|
93
|
+
if (c == '\0') return text;
|
|
94
|
+
text += c;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return text;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
struct Module {
|
|
101
|
+
std::unordered_map<uint32_t, TypeInfo> types;
|
|
102
|
+
std::unordered_map<uint32_t, std::vector<uint32_t>> struct_members;
|
|
103
|
+
std::unordered_map<uint32_t, uint32_t> constants;
|
|
104
|
+
std::unordered_map<uint32_t, std::string> names;
|
|
105
|
+
std::unordered_map<uint64_t, std::string> member_names;
|
|
106
|
+
std::unordered_map<uint32_t, Decor> decorations;
|
|
107
|
+
std::unordered_map<uint64_t, MemberDecor> member_decorations;
|
|
108
|
+
std::unordered_map<uint32_t, uint32_t> variables; // result id -> pointer type
|
|
109
|
+
|
|
110
|
+
bool parse(const std::vector<uint32_t> &words) {
|
|
111
|
+
if (words.size() < 5 || words[0] != 0x07230203u) return false;
|
|
112
|
+
size_t i = 5;
|
|
113
|
+
while (i < words.size()) {
|
|
114
|
+
const uint16_t count = (uint16_t)(words[i] >> 16);
|
|
115
|
+
const uint16_t opcode = (uint16_t)(words[i] & 0xFFFF);
|
|
116
|
+
if (count == 0 || i + count > words.size()) return false;
|
|
117
|
+
const uint32_t *operands = &words[i] + 1;
|
|
118
|
+
switch (opcode) {
|
|
119
|
+
case kOpName:
|
|
120
|
+
if (count >= 3) names[operands[0]] = string_at(words, i + 2);
|
|
121
|
+
break;
|
|
122
|
+
case kOpMemberName:
|
|
123
|
+
if (count >= 4) member_names[((uint64_t)operands[0] << 32) | operands[1]] =
|
|
124
|
+
string_at(words, i + 3);
|
|
125
|
+
break;
|
|
126
|
+
case kOpTypeBool:
|
|
127
|
+
if (count >= 2) types[operands[0]] = {kOpTypeBool, 0, 0};
|
|
128
|
+
break;
|
|
129
|
+
case kOpTypeInt:
|
|
130
|
+
if (count >= 2) types[operands[0]] = {kOpTypeInt, 0, 0};
|
|
131
|
+
break;
|
|
132
|
+
case kOpTypeFloat:
|
|
133
|
+
if (count >= 2) types[operands[0]] = {kOpTypeFloat, 0, 0};
|
|
134
|
+
break;
|
|
135
|
+
case kOpTypeVector:
|
|
136
|
+
if (count >= 3) types[operands[0]] = {kOpTypeVector, operands[1], operands[2]};
|
|
137
|
+
break;
|
|
138
|
+
case kOpTypeMatrix:
|
|
139
|
+
if (count >= 3) types[operands[0]] = {kOpTypeMatrix, operands[1], operands[2]};
|
|
140
|
+
break;
|
|
141
|
+
case kOpTypeArray:
|
|
142
|
+
if (count >= 3) types[operands[0]] = {kOpTypeArray, operands[1], operands[2]};
|
|
143
|
+
break;
|
|
144
|
+
case kOpTypeRuntimeArray:
|
|
145
|
+
if (count >= 2) types[operands[0]] = {kOpTypeRuntimeArray, operands[1], 0};
|
|
146
|
+
break;
|
|
147
|
+
case kOpTypeStruct: {
|
|
148
|
+
TypeInfo info;
|
|
149
|
+
info.op = kOpTypeStruct;
|
|
150
|
+
info.a = count >= 2 ? count - 2 : 0; // member count
|
|
151
|
+
types[operands[0]] = info;
|
|
152
|
+
std::vector<uint32_t> members;
|
|
153
|
+
/* operands[0] is the result id; member type ids start at operands[1]. */
|
|
154
|
+
for (uint16_t m = 1; m + 1 < count; m++) members.push_back(operands[m]);
|
|
155
|
+
struct_members[operands[0]] = std::move(members);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case kOpTypePointer:
|
|
159
|
+
if (count >= 3) types[operands[0]] = {kOpTypePointer, operands[1], operands[2]};
|
|
160
|
+
break;
|
|
161
|
+
case kOpConstant:
|
|
162
|
+
/* operands[1] is the result id, operands[2] the literal value. */
|
|
163
|
+
if (count >= 3) constants[operands[1]] = operands[2];
|
|
164
|
+
break;
|
|
165
|
+
case kOpVariable:
|
|
166
|
+
if (count >= 3) variables[operands[1]] = operands[0];
|
|
167
|
+
break;
|
|
168
|
+
case kOpDecorate: {
|
|
169
|
+
if (count < 3) break;
|
|
170
|
+
Decor &decor = decorations[operands[0]];
|
|
171
|
+
if (operands[1] == kDecorationBlock) decor.block = true;
|
|
172
|
+
if (operands[1] == kDecorationBinding && count >= 4) decor.binding = operands[2];
|
|
173
|
+
if ((operands[1] == kDecorationLocation) && count >= 4) {
|
|
174
|
+
decor.location = (int)operands[2];
|
|
175
|
+
}
|
|
176
|
+
if (operands[1] == 34 && count >= 4) { decor.set = operands[2]; decor.has_descriptor_set = true; }
|
|
177
|
+
if (operands[1] == kDecorationArrayStride && count >= 4) {
|
|
178
|
+
decor.array_stride = operands[2];
|
|
179
|
+
}
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case kOpMemberDecorate: {
|
|
183
|
+
if (count < 4) break;
|
|
184
|
+
const uint64_t key = ((uint64_t)operands[0] << 32) | operands[1];
|
|
185
|
+
MemberDecor &decor = member_decorations[key];
|
|
186
|
+
if (operands[2] == kDecorationOffset && count >= 5) decor.offset = operands[3];
|
|
187
|
+
if (operands[2] == kDecorationMatrixStride && count >= 5) {
|
|
188
|
+
decor.matrix_stride = operands[3];
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
default:
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
i += count;
|
|
196
|
+
}
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The vector size of a scalar/vector type, 0 for anything else. */
|
|
201
|
+
int vector_components(uint32_t type_id, int *gl_scalar) const {
|
|
202
|
+
auto found = types.find(type_id);
|
|
203
|
+
if (found == types.end()) return 0;
|
|
204
|
+
const TypeInfo &info = found->second;
|
|
205
|
+
if (info.op == kOpTypeFloat) {
|
|
206
|
+
*gl_scalar = kGlFloat;
|
|
207
|
+
return 1;
|
|
208
|
+
}
|
|
209
|
+
if (info.op == kOpTypeInt) {
|
|
210
|
+
*gl_scalar = kGlInt;
|
|
211
|
+
return 1;
|
|
212
|
+
}
|
|
213
|
+
if (info.op == kOpTypeBool) {
|
|
214
|
+
*gl_scalar = kGlBool;
|
|
215
|
+
return 1;
|
|
216
|
+
}
|
|
217
|
+
if (info.op == kOpTypeVector) {
|
|
218
|
+
auto component = types.find(info.a);
|
|
219
|
+
if (component == types.end()) return 0;
|
|
220
|
+
if (component->second.op == kOpTypeFloat) *gl_scalar = kGlFloat;
|
|
221
|
+
if (component->second.op == kOpTypeInt) *gl_scalar = kGlInt;
|
|
222
|
+
if (component->second.op == kOpTypeBool) *gl_scalar = kGlBool;
|
|
223
|
+
return (int)info.b;
|
|
224
|
+
}
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/** Fills one scalar/vector/matrix member (possibly an array of them). */
|
|
230
|
+
bool basic_block_member(const Module &module, uint32_t type_id, int offset,
|
|
231
|
+
const std::string &name, bool is_array, int array_size,
|
|
232
|
+
int array_stride, int matrix_stride, GlslBlockMember *member) {
|
|
233
|
+
member->name = name;
|
|
234
|
+
member->offset = offset;
|
|
235
|
+
member->is_array = is_array;
|
|
236
|
+
member->array_size = array_size;
|
|
237
|
+
member->array_stride = array_stride;
|
|
238
|
+
member->matrix_stride = matrix_stride;
|
|
239
|
+
int scalar = 0;
|
|
240
|
+
auto type = module.types.find(type_id);
|
|
241
|
+
if (type != module.types.end() && type->second.op == kOpTypeMatrix) {
|
|
242
|
+
int column_scalar = 0;
|
|
243
|
+
const int rows = module.vector_components(type->second.a, &column_scalar);
|
|
244
|
+
member->is_matrix = true;
|
|
245
|
+
member->columns = (int)type->second.b;
|
|
246
|
+
member->rows = rows;
|
|
247
|
+
member->components = rows;
|
|
248
|
+
member->scalar_type = column_scalar;
|
|
249
|
+
switch (member->columns) {
|
|
250
|
+
case 2:
|
|
251
|
+
member->gl_type = kGlFloatMat2;
|
|
252
|
+
break;
|
|
253
|
+
case 3:
|
|
254
|
+
member->gl_type = kGlFloatMat3;
|
|
255
|
+
break;
|
|
256
|
+
default:
|
|
257
|
+
member->gl_type = kGlFloatMat4;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
if (member->array_stride == 0 && member->is_array) {
|
|
261
|
+
member->array_stride = member->columns * member->matrix_stride;
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
const int components = module.vector_components(type_id, &scalar);
|
|
266
|
+
if (components <= 0) return false;
|
|
267
|
+
member->components = components;
|
|
268
|
+
member->scalar_type = scalar;
|
|
269
|
+
if (components == 1) {
|
|
270
|
+
member->gl_type = scalar;
|
|
271
|
+
} else if (scalar == kGlFloat) {
|
|
272
|
+
member->gl_type = components == 2 ? kGlFloatVec2
|
|
273
|
+
: (components == 3 ? kGlFloatVec3 : kGlFloatVec4);
|
|
274
|
+
} else if (scalar == kGlInt) {
|
|
275
|
+
member->gl_type = components == 2 ? kGlIntVec2
|
|
276
|
+
: (components == 3 ? kGlIntVec3 : kGlIntVec4);
|
|
277
|
+
} else {
|
|
278
|
+
member->gl_type = components == 2 ? 0x8B57 : (components == 3 ? 0x8B58 : 0x8B59);
|
|
279
|
+
}
|
|
280
|
+
if (member->array_stride == 0) member->array_stride = components * 4;
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
void reflect_struct_members(const Module &module, uint32_t struct_id, int base,
|
|
285
|
+
const std::string &prefix, std::vector<GlslBlockMember> *out);
|
|
286
|
+
|
|
287
|
+
/** Walks one block member type, flattening structs/arrays of structs into
|
|
288
|
+
* individual `name[i].field` entries so three.js can upload each light field. */
|
|
289
|
+
void reflect_block_value(const Module &module, uint32_t type_id, int offset,
|
|
290
|
+
const std::string &name, int matrix_stride,
|
|
291
|
+
std::vector<GlslBlockMember> *out) {
|
|
292
|
+
auto type = module.types.find(type_id);
|
|
293
|
+
if (type == module.types.end()) return;
|
|
294
|
+
if (type->second.op == kOpTypeArray) {
|
|
295
|
+
auto length = module.constants.find(type->second.b);
|
|
296
|
+
const int count = length == module.constants.end() ? 0 : (int)length->second;
|
|
297
|
+
int stride = 0;
|
|
298
|
+
auto decor = module.decorations.find(type_id);
|
|
299
|
+
if (decor != module.decorations.end()) stride = (int)decor->second.array_stride;
|
|
300
|
+
const uint32_t element = type->second.a;
|
|
301
|
+
auto element_type = module.types.find(element);
|
|
302
|
+
if (element_type != module.types.end() && element_type->second.op == kOpTypeStruct) {
|
|
303
|
+
for (int i = 0; i < count; i++) {
|
|
304
|
+
reflect_struct_members(module, element, offset + i * stride,
|
|
305
|
+
name + "[" + std::to_string(i) + "]", out);
|
|
306
|
+
}
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
GlslBlockMember member;
|
|
310
|
+
if (basic_block_member(module, element, offset, name, true, count, stride, matrix_stride,
|
|
311
|
+
&member)) {
|
|
312
|
+
out->push_back(member);
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (type->second.op == kOpTypeStruct) {
|
|
317
|
+
reflect_struct_members(module, type_id, offset, name, out);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
GlslBlockMember member;
|
|
321
|
+
if (basic_block_member(module, type_id, offset, name, false, 1, 0, matrix_stride, &member)) {
|
|
322
|
+
out->push_back(member);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
void reflect_struct_members(const Module &module, uint32_t struct_id, int base,
|
|
327
|
+
const std::string &prefix, std::vector<GlslBlockMember> *out) {
|
|
328
|
+
auto members = module.struct_members.find(struct_id);
|
|
329
|
+
if (members == module.struct_members.end()) return;
|
|
330
|
+
for (size_t index = 0; index < members->second.size(); index++) {
|
|
331
|
+
const uint64_t key = ((uint64_t)struct_id << 32) | (uint32_t)index;
|
|
332
|
+
auto name = module.member_names.find(key);
|
|
333
|
+
auto decor = module.member_decorations.find(key);
|
|
334
|
+
if (name == module.member_names.end() || decor == module.member_decorations.end()) continue;
|
|
335
|
+
const std::string child = prefix.empty() ? name->second : prefix + "." + name->second;
|
|
336
|
+
reflect_block_value(module, members->second[index], base + (int)decor->second.offset, child,
|
|
337
|
+
(int)decor->second.matrix_stride, out);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
} // namespace
|
|
342
|
+
|
|
343
|
+
bool compile_glsl(const std::string &source, bool fragment, std::vector<uint32_t> *spirv,
|
|
344
|
+
std::string *log) {
|
|
345
|
+
static const bool initialized = glslang::InitializeProcess();
|
|
346
|
+
if (!initialized) {
|
|
347
|
+
*log = "glslang initialization failed";
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
const EShLanguage stage = fragment ? EShLangFragment : EShLangVertex;
|
|
351
|
+
glslang::TShader shader(stage);
|
|
352
|
+
if (getenv("XT_WEBGL_DUMP_GLSL") != nullptr) {
|
|
353
|
+
fprintf(stderr, "--- GLSL %s ---\n%s\n---\n", fragment ? "fragment" : "vertex",
|
|
354
|
+
source.c_str());
|
|
355
|
+
}
|
|
356
|
+
const char *text = source.c_str();
|
|
357
|
+
shader.setStrings(&text, 1);
|
|
358
|
+
shader.setEntryPoint("main");
|
|
359
|
+
shader.setSourceEntryPoint("main");
|
|
360
|
+
shader.setEnvInput(glslang::EShSourceGlsl, stage, glslang::EShClientVulkan, 310);
|
|
361
|
+
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_0);
|
|
362
|
+
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
|
|
363
|
+
if (!shader.parse(GetDefaultResources(), 310, false, EShMsgDefault)) {
|
|
364
|
+
*log = std::string("vertex/fragment parse failed:\n") + shader.getInfoLog() +
|
|
365
|
+
shader.getInfoDebugLog();
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
glslang::TProgram program;
|
|
369
|
+
program.addShader(&shader);
|
|
370
|
+
if (!program.link(EShMsgDefault)) {
|
|
371
|
+
*log = std::string("link failed:\n") + program.getInfoLog() + program.getInfoDebugLog();
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
spirv->clear();
|
|
375
|
+
glslang::GlslangToSpv(*program.getIntermediate(stage), *spirv);
|
|
376
|
+
return !spirv->empty();
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
bool reflect_uniform_block(const std::vector<uint32_t> &spirv, std::vector<GlslBlockMember> *out) {
|
|
380
|
+
out->clear();
|
|
381
|
+
Module module;
|
|
382
|
+
if (!module.parse(spirv)) return false;
|
|
383
|
+
|
|
384
|
+
/* Find the uniform block this engine emitted: a Block struct reached through
|
|
385
|
+
* a variable that carries an explicit descriptor set. The vertex stage also
|
|
386
|
+
* has the built-in `gl_PerVertex` Block (no descriptor set), which must not
|
|
387
|
+
* be mistaken for the std140 uniform block. */
|
|
388
|
+
uint32_t struct_id = 0;
|
|
389
|
+
for (const auto &entry : module.variables) {
|
|
390
|
+
auto type = module.types.find(entry.second);
|
|
391
|
+
if (type == module.types.end() || type->second.op != kOpTypePointer) continue;
|
|
392
|
+
auto pointee = module.types.find(type->second.b);
|
|
393
|
+
if (pointee == module.types.end() || pointee->second.op != kOpTypeStruct) continue;
|
|
394
|
+
auto decor = module.decorations.find(type->second.b);
|
|
395
|
+
if (decor == module.decorations.end() || !decor->second.block) continue;
|
|
396
|
+
auto variable_decor = module.decorations.find(entry.first);
|
|
397
|
+
if (variable_decor == module.decorations.end() || !variable_decor->second.has_descriptor_set) {
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
struct_id = type->second.b;
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
if (struct_id == 0) return true; // no uniform block: nothing to reflect
|
|
404
|
+
|
|
405
|
+
auto members = module.struct_members.find(struct_id);
|
|
406
|
+
if (members != module.struct_members.end()) {
|
|
407
|
+
for (size_t index = 0; index < members->second.size(); index++) {
|
|
408
|
+
const uint64_t key = ((uint64_t)struct_id << 32) | (uint32_t)index;
|
|
409
|
+
auto name = module.member_names.find(key);
|
|
410
|
+
auto decor = module.member_decorations.find(key);
|
|
411
|
+
if (name == module.member_names.end() || decor == module.member_decorations.end()) continue;
|
|
412
|
+
reflect_block_value(module, members->second[index], (int)decor->second.offset, name->second,
|
|
413
|
+
(int)decor->second.matrix_stride, out);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
bool reflect_attributes(const std::vector<uint32_t> &spirv,
|
|
420
|
+
std::unordered_map<std::string, int> *out) {
|
|
421
|
+
out->clear();
|
|
422
|
+
Module module;
|
|
423
|
+
if (!module.parse(spirv)) return false;
|
|
424
|
+
for (const auto &entry : module.variables) {
|
|
425
|
+
auto pointer = module.types.find(entry.second);
|
|
426
|
+
if (pointer == module.types.end() || pointer->second.op != kOpTypePointer) continue;
|
|
427
|
+
if (pointer->second.a != kStorageInput) continue;
|
|
428
|
+
auto name = module.names.find(entry.first);
|
|
429
|
+
auto decor = module.decorations.find(entry.first);
|
|
430
|
+
if (name == module.names.end() || decor == module.decorations.end()) continue;
|
|
431
|
+
if (decor->second.location < 0) continue;
|
|
432
|
+
(*out)[name->second] = decor->second.location;
|
|
433
|
+
}
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
} // namespace xtgui
|