Glslang
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Install / Use
/learn @KhronosGroup/GlslangREADME
News
-
The --shift-texture-binding[s] option no longer affects combined samplers. The new --shift-combined-sampler-binding[s] option should be used to control combined sampler bindings independently from separate textures. The old behavior can be achieved by setting both options to the same value.
-
The spirv-remap utility from glslang has been ported to the SPIRV-Tools repository as a new optimization pass called canonicalize-ids, available in spirv-opt. See spirv-opt --help for usage details.
-
Building glslang as a DLL or shared library is now possible and supported.
Glslang Components and Status
There are several components:
Reference Validator and GLSL/ESSL -> AST Front End
An OpenGL GLSL and OpenGL|ES GLSL (ESSL) front-end for reference validation and translation of GLSL/ESSL into an internal abstract syntax tree (AST).
Status: Virtually complete, with results carrying similar weight as the specifications.
HLSL -> AST Front End
An HLSL front-end for translation of an approximation of HLSL to glslang's AST form.
Status: Partially complete. Semantics are not reference quality and input is not validated. This is in contrast to the DXC project, which receives a much larger investment and attempts to have definitive/reference-level semantics.
See issue 362 and issue 701 for current status.
AST -> SPIR-V Back End
Translates glslang's AST to the Khronos-specified SPIR-V intermediate language.
Status: Virtually complete.
Reflector
An API for getting reflection information from the AST, reflection types/variables/etc. from the HLL source (not the SPIR-V).
Status: There is a large amount of functionality present, but no specification/goal to measure completeness against. It is accurate for the input HLL and AST, but only approximate for what would later be emitted for SPIR-V.
Standalone Wrapper
glslang is command-line tool for accessing the functionality above.
Status: Complete.
Tasks waiting to be done are documented as GitHub issues.
Other References
Also see the Khronos landing page for glslang as a reference front end:
https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
The above page, while not kept up to date, includes additional information regarding glslang as a reference validator.
How to Use Glslang
Execution of Standalone Wrapper
To use the standalone binary form, execute glslang, and it will print
a usage statement. Basic operation is to give it a file containing a shader,
and it will print out warnings/errors and optionally an AST.
The applied stage-specific rules are based on the file extension:
.vertfor a vertex shader.tescfor a tessellation control shader.tesefor a tessellation evaluation shader.geomfor a geometry shader.fragfor a fragment shader.compfor a compute shader
For ray tracing pipeline shaders:
.rgenfor a ray generation shader.rintfor a ray intersection shader.rahitfor a ray any-hit shader.rchitfor a ray closest-hit shader.rmissfor a ray miss shader.rcallfor a callable shader
There is also a non-shader extension:
.conffor a configuration file of limits, see usage statement for example
Building (CMake)
Instead of building manually, you can also download the binaries for your platform directly from the [main-tot release][main-tot-release] on GitHub. Those binaries are automatically uploaded by the buildbots after successful testing and they always reflect the current top of the tree of the main branch.
Dependencies
- A C++17 compiler. (For MSVS: use 2019 or later.)
- [CMake][cmake]: for generating compilation targets.
- make: Linux, ninja is an alternative, if configured.
- [Python 3.x][python]: for executing SPIRV-Tools scripts. (Optional if not using SPIRV-Tools and the 'External' subdirectory does not exist.)
- [bison][bison]: optional, but needed when changing the grammar (glslang.y).
- [googletest][googletest]: optional, but should use if making any changes to glslang.
Build steps
The following steps assume a Bash shell. On Windows, that could be the Git Bash shell or some other shell of your choosing.
1) Check-Out this project
cd <parent of where you want glslang to be>
git clone https://github.com/KhronosGroup/glslang.git
2) Check-Out External Projects
./update_glslang_sources.py
3) Configure
Assume the source directory is $SOURCE_DIR and the build directory is $BUILD_DIR.
CMake will create the $BUILD_DIR for the user if it doesn't exist.
First change your working directory:
cd $SOURCE_DIR
For building on Linux:
cmake -B $BUILD_DIR -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/install"
# "Release" (for CMAKE_BUILD_TYPE) could also be "Debug" or "RelWithDebInfo"
For building on Android:
cmake -B $BUILD_DIR -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$(pwd)/install" -DANDROID_ABI=arm64-v8a -DCMAKE_BUILD_TYPE=Release -DANDROID_STL=c++_static -DANDROID_PLATFORM=android-24 -DCMAKE_SYSTEM_NAME=Android -DANDROID_TOOLCHAIN=clang -DANDROID_ARM_MODE=arm -DCMAKE_MAKE_PROGRAM=$ANDROID_NDK_HOME/prebuilt/linux-x86_64/bin/make -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake
# If on Windows will be -DCMAKE_MAKE_PROGRAM=%ANDROID_NDK_HOME%\prebuilt\windows-x86_64\bin\make.exe
# -G is needed for building on Windows
# -DANDROID_ABI can also be armeabi-v7a for 32 bit
For building on Windows:
cmake -B $BUILD_DIR -DCMAKE_INSTALL_PREFIX="$(pwd)/install"
# The CMAKE_INSTALL_PREFIX part is for testing (explained later).
Also, consider using git config --global core.fileMode false (or with --local) on Windows
to prevent the addition of execution permission on files.
4) Build and Install
# for Linux:
make -j4 install
# for Windows:
cmake --build . --config Release --target install
# "Release" (for --config) could also be "Debug", "MinSizeRel", or "RelWithDebInfo"
If using MSVC, after running CMake to configure, use the
Configuration Manager to check the INSTALL project.
Building (GN)
glslang can also be built with the GN build system.
1) Install depot_tools
Download depot_tools.zip,
extract to a directory, and add this directory to your PATH.
2) Synchronize dependencies and generate build files
This only needs to be done once after updating glslang.
With the current directory set to your glslang checkout, type:
./update_glslang_sources.py
gclient sync --gclientfile=standalone.gclient
gn gen out/Default
3) Build
With the current directory set to your glslang checkout, type:
cd out/Default
ninja
If you need to change the GLSL grammar
The grammar in glslang/MachineIndependent/glslang.y has to be recompiled with
bison if it changes, the output files are committed to the repo to avoid every
developer needing to have bison configured to compile the project when grammar
changes are quite infrequent. For windows you can get binaries from
[GnuWin32][bison-gnu-win32].
The command to rebuild is:
bison --defines=MachineIndependent/glslang_tab.cpp.h
-t MachineIndependent/glslang.y
-o MachineIndependent/glslang_tab.cpp
The above command is also available in the bash script in updateGrammar,
when executed from the glslang subdirectory of the glslang repository.
Building to WASM for the Web and Node
Building a standalone JS/WASM library for the Web and Node
Use the steps in Build Steps, with the following notes/exceptions:
emsdkneeds to be present in your executable search path, PATH for Bash-like environments:- Wrap cmake call:
emcmake cmake - Set
-DENABLE_OPT=OFF. - Set
-DENABLE_HLSL=OFFif HLSL is not needed. - For a standalone JS/WASM library, turn on
-DENABLE_GLSLANG_JS=ON. - To get a fully minimized build, make sure to use
brotlito compress the .js and .wasm files - Note that by default, Emscripten allocates a very small stack size, which may cause stack overflows when compiling large shaders. Use the STACK_SIZE compiler setting to increase the stack size.
Example:
emcmake cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_GLSLANG_JS=ON \
-DENABLE_HLSL=OFF -DENABLE_OPT=OFF ..
Building glslang - Using vcpkg
You can download and install glslang using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install glslang
The glslang port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Testing
Right now, there are two test harnesses existing in glslang: one is Google
Test, one is the runtests script. The former
runs unit tests and single-shader single-threa
