Blame README.md

Packit Service d7a8c4
# News
Packit Service d7a8c4
Packit Service d7a8c4
[![Build Status](https://travis-ci.org/KhronosGroup/glslang.svg?branch=master)](https://travis-ci.org/KhronosGroup/glslang)
Packit Service d7a8c4
[![Build status](https://ci.appveyor.com/api/projects/status/q6fi9cb0qnhkla68/branch/master?svg=true)](https://ci.appveyor.com/project/Khronoswebmaster/glslang/branch/master)
Packit Service d7a8c4
Packit Service d7a8c4
## Planned Deprecations/Removals
Packit Service d7a8c4
Packit Service d7a8c4
1. **SPIRV Folder, 1-May, 2020.** Glslang, when installed through CMake,
Packit Service d7a8c4
will install a `SPIRV` folder into `${CMAKE_INSTALL_INCLUDEDIR}`.
Packit Service d7a8c4
This `SPIRV` folder is being moved to `glslang/SPIRV`.
Packit Service d7a8c4
During the transition the `SPIRV` folder will be installed into both locations.
Packit Service d7a8c4
The old install of `SPIRV/` will be removed as a CMake install target no sooner than May 1, 2020.
Packit Service d7a8c4
See issue #1964.
Packit Service d7a8c4
Packit Service d7a8c4
2. **Visual Studio 2013, 20-July, 2020.** Keeping code compiling for MS Visual Studio 2013 will no longer be
Packit Service d7a8c4
a goal as of July 20, 2020, the fifth anniversary of the release of Visual Studio 2015.
Packit Service d7a8c4
Packit Service d7a8c4
# Glslang Components and Status
Packit Service d7a8c4
Packit Service d7a8c4
There are several components:
Packit Service d7a8c4
Packit Service d7a8c4
### Reference Validator and GLSL/ESSL -> AST Front End
Packit Service d7a8c4
Packit Service d7a8c4
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).
Packit Service d7a8c4
Packit Service d7a8c4
**Status**: Virtually complete, with results carrying similar weight as the specifications.
Packit Service d7a8c4
Packit Service d7a8c4
### HLSL -> AST Front End
Packit Service d7a8c4
Packit Service d7a8c4
An HLSL front-end for translation of an approximation of HLSL to glslang's AST form.
Packit Service d7a8c4
Packit Service d7a8c4
**Status**: Partially complete. Semantics are not reference quality and input is not validated.
Packit Service d7a8c4
This is in contrast to the [DXC project](https://github.com/Microsoft/DirectXShaderCompiler), which receives a much larger investment and attempts to have definitive/reference-level semantics.
Packit Service d7a8c4
Packit Service d7a8c4
See [issue 362](https://github.com/KhronosGroup/glslang/issues/362) and [issue 701](https://github.com/KhronosGroup/glslang/issues/701) for current status.
Packit Service d7a8c4
Packit Service d7a8c4
### AST -> SPIR-V Back End
Packit Service d7a8c4
Packit Service d7a8c4
Translates glslang's AST to the Khronos-specified SPIR-V intermediate language.
Packit Service d7a8c4
Packit Service d7a8c4
**Status**: Virtually complete.
Packit Service d7a8c4
Packit Service d7a8c4
### Reflector
Packit Service d7a8c4
Packit Service d7a8c4
An API for getting reflection information from the AST, reflection types/variables/etc. from the HLL source (not the SPIR-V).
Packit Service d7a8c4
Packit Service d7a8c4
**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.
Packit Service d7a8c4
Packit Service d7a8c4
### Standalone Wrapper
Packit Service d7a8c4
Packit Service d7a8c4
`glslangValidator` is command-line tool for accessing the functionality above.
Packit Service d7a8c4
Packit Service d7a8c4
Status: Complete.
Packit Service d7a8c4
Packit Service d7a8c4
Tasks waiting to be done are documented as GitHub issues.
Packit Service d7a8c4
Packit Service d7a8c4
## Other References
Packit Service d7a8c4
Packit Service d7a8c4
Also see the Khronos landing page for glslang as a reference front end:
Packit Service d7a8c4
Packit Service d7a8c4
https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
Packit Service d7a8c4
Packit Service d7a8c4
The above page, while not kept up to date, includes additional information regarding glslang as a reference validator.
Packit Service d7a8c4
Packit Service d7a8c4
# How to Use Glslang
Packit Service d7a8c4
Packit Service d7a8c4
## Execution of Standalone Wrapper
Packit Service d7a8c4
Packit Service d7a8c4
To use the standalone binary form, execute `glslangValidator`, and it will print
Packit Service d7a8c4
a usage statement.  Basic operation is to give it a file containing a shader,
Packit Service d7a8c4
and it will print out warnings/errors and optionally an AST.
Packit Service d7a8c4
Packit Service d7a8c4
The applied stage-specific rules are based on the file extension:
Packit Service d7a8c4
* `.vert` for a vertex shader
Packit Service d7a8c4
* `.tesc` for a tessellation control shader
Packit Service d7a8c4
* `.tese` for a tessellation evaluation shader
Packit Service d7a8c4
* `.geom` for a geometry shader
Packit Service d7a8c4
* `.frag` for a fragment shader
Packit Service d7a8c4
* `.comp` for a compute shader
Packit Service d7a8c4
Packit Service d7a8c4
There is also a non-shader extension
Packit Service d7a8c4
* `.conf` for a configuration file of limits, see usage statement for example
Packit Service d7a8c4
Packit Service d7a8c4
## Building
Packit Service d7a8c4
Packit Service d7a8c4
Instead of building manually, you can also download the binaries for your
Packit Service d7a8c4
platform directly from the [master-tot release][master-tot-release] on GitHub.
Packit Service d7a8c4
Those binaries are automatically uploaded by the buildbots after successful
Packit Service d7a8c4
testing and they always reflect the current top of the tree of the master
Packit Service d7a8c4
branch.
Packit Service d7a8c4
Packit Service d7a8c4
### Dependencies
Packit Service d7a8c4
Packit Service d7a8c4
* A C++11 compiler.
Packit Service d7a8c4
  (For MSVS: 2015 is recommended, 2013 is fully supported/tested, and 2010 support is attempted, but not tested.)
Packit Service d7a8c4
* [CMake][cmake]: for generating compilation targets.
Packit Service d7a8c4
* make: _Linux_, ninja is an alternative, if configured.
Packit Service d7a8c4
* [Python 3.x][python]: for executing SPIRV-Tools scripts. (Optional if not using SPIRV-Tools and the 'External' subdirectory does not exist.)
Packit Service d7a8c4
* [bison][bison]: _optional_, but needed when changing the grammar (glslang.y).
Packit Service d7a8c4
* [googletest][googletest]: _optional_, but should use if making any changes to glslang.
Packit Service d7a8c4
Packit Service d7a8c4
### Build steps
Packit Service d7a8c4
Packit Service d7a8c4
The following steps assume a Bash shell. On Windows, that could be the Git Bash
Packit Service d7a8c4
shell or some other shell of your choosing.
Packit Service d7a8c4
Packit Service d7a8c4
#### 1) Check-Out this project
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cd <parent of where you want glslang to be>
Packit Service d7a8c4
git clone https://github.com/KhronosGroup/glslang.git
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
#### 2) Check-Out External Projects
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cd <the directory glslang was cloned to, "External" will be a subdirectory>
Packit Service d7a8c4
git clone https://github.com/google/googletest.git External/googletest
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
If you want to use googletest with Visual Studio 2013, you also need to check out an older version:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
# to use googletest with Visual Studio 2013
Packit Service d7a8c4
cd External/googletest
Packit Service d7a8c4
git checkout 440527a61e1c91188195f7de212c63c77e8f0a45
Packit Service d7a8c4
cd ../..
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
If you wish to assure that SPIR-V generated from HLSL is legal for Vulkan,
Packit Service d7a8c4
wish to invoke -Os to reduce SPIR-V size from HLSL or GLSL, or wish to run the
Packit Service d7a8c4
integrated test suite, install spirv-tools with this:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
./update_glslang_sources.py
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
#### 3) Configure
Packit Service d7a8c4
Packit Service d7a8c4
Assume the source directory is `$SOURCE_DIR` and the build directory is
Packit Service d7a8c4
`$BUILD_DIR`. First ensure the build directory exists, then navigate to it:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
mkdir -p $BUILD_DIR
Packit Service d7a8c4
cd $BUILD_DIR
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
For building on Linux:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/install" $SOURCE_DIR
Packit Service d7a8c4
# "Release" (for CMAKE_BUILD_TYPE) could also be "Debug" or "RelWithDebInfo"
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
For building on Android:
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cmake $SOURCE_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_ROOT/prebuilt/linux-x86_64/bin/make -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake
Packit Service d7a8c4
# If on Windows will be -DCMAKE_MAKE_PROGRAM=%ANDROID_NDK_ROOT%\prebuilt\windows-x86_64\bin\make.exe
Packit Service d7a8c4
# -G is needed for building on Windows
Packit Service d7a8c4
# -DANDROID_ABI can also be armeabi-v7a for 32 bit
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
For building on Windows:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cmake $SOURCE_DIR -DCMAKE_INSTALL_PREFIX="$(pwd)/install"
Packit Service d7a8c4
# The CMAKE_INSTALL_PREFIX part is for testing (explained later).
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
The CMake GUI also works for Windows (version 3.4.1 tested).
Packit Service d7a8c4
Packit Service d7a8c4
Also, consider using `git config --global core.fileMode false` (or with `--local`) on Windows
Packit Service d7a8c4
to prevent the addition of execution permission on files.
Packit Service d7a8c4
Packit Service d7a8c4
#### 4) Build and Install
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
# for Linux:
Packit Service d7a8c4
make -j4 install
Packit Service d7a8c4
Packit Service d7a8c4
# for Windows:
Packit Service d7a8c4
cmake --build . --config Release --target install
Packit Service d7a8c4
# "Release" (for --config) could also be "Debug", "MinSizeRel", or "RelWithDebInfo"
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
If using MSVC, after running CMake to configure, use the
Packit Service d7a8c4
Configuration Manager to check the `INSTALL` project.
Packit Service d7a8c4
Packit Service d7a8c4
### If you need to change the GLSL grammar
Packit Service d7a8c4
Packit Service d7a8c4
The grammar in `glslang/MachineIndependent/glslang.y` has to be recompiled with
Packit Service d7a8c4
bison if it changes, the output files are committed to the repo to avoid every
Packit Service d7a8c4
developer needing to have bison configured to compile the project when grammar
Packit Service d7a8c4
changes are quite infrequent. For windows you can get binaries from
Packit Service d7a8c4
[GnuWin32][bison-gnu-win32].
Packit Service d7a8c4
Packit Service d7a8c4
The command to rebuild is:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
m4 -P MachineIndependent/glslang.m4 > MachineIndependent/glslang.y
Packit Service d7a8c4
bison --defines=MachineIndependent/glslang_tab.cpp.h
Packit Service d7a8c4
      -t MachineIndependent/glslang.y
Packit Service d7a8c4
      -o MachineIndependent/glslang_tab.cpp
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
The above commands are also available in the bash script in `updateGrammar`,
Packit Service d7a8c4
when executed from the glslang subdirectory of the glslang repository.
Packit Service d7a8c4
With no arguments it builds the full grammar, and with a "web" argument,
Packit Service d7a8c4
the web grammar subset (see more about the web subset in the next section).
Packit Service d7a8c4
Packit Service d7a8c4
### Building to WASM for the Web and Node
Packit Service d7a8c4
### Building a standalone JS/WASM library for the Web and Node
Packit Service d7a8c4
Packit Service d7a8c4
Use the steps in [Build Steps](#build-steps), with the following notes/exceptions:
Packit Service d7a8c4
* `emsdk` needs to be present in your executable search path, *PATH* for
Packit Service d7a8c4
  Bash-like environments:
Packit Service d7a8c4
  + [Instructions located here](https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install)
Packit Service d7a8c4
* Wrap cmake call: `emcmake cmake`
Packit Service d7a8c4
* Set `-DBUILD_TESTING=OFF -DENABLE_OPT=OFF -DINSTALL_GTEST=OFF`.
Packit Service d7a8c4
* Set `-DENABLE_HLSL=OFF` if HLSL is not needed.
Packit Service d7a8c4
* For a standalone JS/WASM library, turn on `-DENABLE_GLSLANG_JS=ON`.
Packit Service d7a8c4
* For building a minimum-size web subset of core glslang:
Packit Service d7a8c4
  + turn on `-DENABLE_GLSLANG_WEBMIN=ON` (disables HLSL)
Packit Service d7a8c4
  + execute `updateGrammar web` from the glslang subdirectory
Packit Service d7a8c4
    (or if using your own scripts, `m4` needs a `-DGLSLANG_WEB` argument)
Packit Service d7a8c4
  + optionally, for GLSL compilation error messages, turn on
Packit Service d7a8c4
    `-DENABLE_GLSLANG_WEBMIN_DEVEL=ON`
Packit Service d7a8c4
* To get a fully minimized build, make sure to use `brotli` to compress the .js
Packit Service d7a8c4
  and .wasm files
Packit Service d7a8c4
Packit Service d7a8c4
Example:
Packit Service d7a8c4
Packit Service d7a8c4
```sh
Packit Service d7a8c4
emcmake cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_GLSLANG_JS=ON \
Packit Service d7a8c4
    -DENABLE_HLSL=OFF -DBUILD_TESTING=OFF -DENABLE_OPT=OFF -DINSTALL_GTEST=OFF ..
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
## Building glslang - Using vcpkg
Packit Service d7a8c4
Packit Service d7a8c4
You can download and install glslang using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
Packit Service d7a8c4
Packit Service d7a8c4
    git clone https://github.com/Microsoft/vcpkg.git
Packit Service d7a8c4
    cd vcpkg
Packit Service d7a8c4
    ./bootstrap-vcpkg.sh
Packit Service d7a8c4
    ./vcpkg integrate install
Packit Service d7a8c4
    ./vcpkg install glslang
Packit Service d7a8c4
Packit Service d7a8c4
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](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
Packit Service d7a8c4
Packit Service d7a8c4
## Testing
Packit Service d7a8c4
Packit Service d7a8c4
Right now, there are two test harnesses existing in glslang: one is [Google
Packit Service d7a8c4
Test](gtests/), one is the [`runtests` script](Test/runtests). The former
Packit Service d7a8c4
runs unit tests and single-shader single-threaded integration tests, while
Packit Service d7a8c4
the latter runs multiple-shader linking tests and multi-threaded tests.
Packit Service d7a8c4
Packit Service d7a8c4
### Running tests
Packit Service d7a8c4
Packit Service d7a8c4
The [`runtests` script](Test/runtests) requires compiled binaries to be
Packit Service d7a8c4
installed into `$BUILD_DIR/install`. Please make sure you have supplied the
Packit Service d7a8c4
correct configuration to CMake (using `-DCMAKE_INSTALL_PREFIX`) when building;
Packit Service d7a8c4
otherwise, you may want to modify the path in the `runtests` script.
Packit Service d7a8c4
Packit Service d7a8c4
Running Google Test-backed tests:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cd $BUILD_DIR
Packit Service d7a8c4
Packit Service d7a8c4
# for Linux:
Packit Service d7a8c4
ctest
Packit Service d7a8c4
Packit Service d7a8c4
# for Windows:
Packit Service d7a8c4
ctest -C {Debug|Release|RelWithDebInfo|MinSizeRel}
Packit Service d7a8c4
Packit Service d7a8c4
# or, run the test binary directly
Packit Service d7a8c4
# (which gives more fine-grained control like filtering):
Packit Service d7a8c4
<dir-to-glslangtests-in-build-dir>/glslangtests
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
Running `runtests` script-backed tests:
Packit Service d7a8c4
Packit Service d7a8c4
```bash
Packit Service d7a8c4
cd $SOURCE_DIR/Test && ./runtests
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
If some tests fail with validation errors, there may be a mismatch between the
Packit Service d7a8c4
version of `spirv-val` on the system and the version of glslang.  In this
Packit Service d7a8c4
case, it is necessary to run `update_glslang_sources.py`.  See "Check-Out
Packit Service d7a8c4
External Projects" above for more details.
Packit Service d7a8c4
Packit Service d7a8c4
### Contributing tests
Packit Service d7a8c4
Packit Service d7a8c4
Test results should always be included with a pull request that modifies
Packit Service d7a8c4
functionality.
Packit Service d7a8c4
Packit Service d7a8c4
If you are writing unit tests, please use the Google Test framework and
Packit Service d7a8c4
place the tests under the `gtests/` directory.
Packit Service d7a8c4
Packit Service d7a8c4
Integration tests are placed in the `Test/` directory. It contains test input
Packit Service d7a8c4
and a subdirectory `baseResults/` that contains the expected results of the
Packit Service d7a8c4
tests.  Both the tests and `baseResults/` are under source-code control.
Packit Service d7a8c4
Packit Service d7a8c4
Google Test runs those integration tests by reading the test input, compiling
Packit Service d7a8c4
them, and then compare against the expected results in `baseResults/`. The
Packit Service d7a8c4
integration tests to run via Google Test is registered in various
Packit Service d7a8c4
`gtests/*.FromFile.cpp` source files. `glslangtests` provides a command-line
Packit Service d7a8c4
option `--update-mode`, which, if supplied, will overwrite the golden files
Packit Service d7a8c4
under the `baseResults/` directory with real output from that invocation.
Packit Service d7a8c4
For more information, please check `gtests/` directory's
Packit Service d7a8c4
[README](gtests/README.md).
Packit Service d7a8c4
Packit Service d7a8c4
For the `runtests` script, it will generate current results in the
Packit Service d7a8c4
`localResults/` directory and `diff` them against the `baseResults/`.
Packit Service d7a8c4
When you want to update the tracked test results, they need to be
Packit Service d7a8c4
copied from `localResults/` to `baseResults/`.  This can be done by
Packit Service d7a8c4
the `bump` shell script.
Packit Service d7a8c4
Packit Service d7a8c4
You can add your own private list of tests, not tracked publicly, by using
Packit Service d7a8c4
`localtestlist` to list non-tracked tests.  This is automatically read
Packit Service d7a8c4
by `runtests` and included in the `diff` and `bump` process.
Packit Service d7a8c4
Packit Service d7a8c4
## Programmatic Interfaces
Packit Service d7a8c4
Packit Service d7a8c4
Another piece of software can programmatically translate shaders to an AST
Packit Service d7a8c4
using one of two different interfaces:
Packit Service d7a8c4
* A new C++ class-oriented interface, or
Packit Service d7a8c4
* The original C functional interface
Packit Service d7a8c4
Packit Service d7a8c4
The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
Packit Service d7a8c4
Packit Service d7a8c4
### C++ Class Interface (new, preferred)
Packit Service d7a8c4
Packit Service d7a8c4
This interface is in roughly the last 1/3 of `ShaderLang.h`.  It is in the
Packit Service d7a8c4
glslang namespace and contains the following, here with suggested calls
Packit Service d7a8c4
for generating SPIR-V:
Packit Service d7a8c4
Packit Service d7a8c4
```cxx
Packit Service d7a8c4
const char* GetEsslVersionString();
Packit Service d7a8c4
const char* GetGlslVersionString();
Packit Service d7a8c4
bool InitializeProcess();
Packit Service d7a8c4
void FinalizeProcess();
Packit Service d7a8c4
Packit Service d7a8c4
class TShader
Packit Service d7a8c4
    setStrings(...);
Packit Service d7a8c4
    setEnvInput(EShSourceHlsl or EShSourceGlsl, stage,  EShClientVulkan or EShClientOpenGL, 100);
Packit Service d7a8c4
    setEnvClient(EShClientVulkan or EShClientOpenGL, EShTargetVulkan_1_0 or EShTargetVulkan_1_1 or EShTargetOpenGL_450);
Packit Service d7a8c4
    setEnvTarget(EShTargetSpv, EShTargetSpv_1_0 or EShTargetSpv_1_3);
Packit Service d7a8c4
    bool parse(...);
Packit Service d7a8c4
    const char* getInfoLog();
Packit Service d7a8c4
Packit Service d7a8c4
class TProgram
Packit Service d7a8c4
    void addShader(...);
Packit Service d7a8c4
    bool link(...);
Packit Service d7a8c4
    const char* getInfoLog();
Packit Service d7a8c4
    Reflection queries
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
For just validating (not generating code), substitute these calls:
Packit Service d7a8c4
Packit Service d7a8c4
```cxx
Packit Service d7a8c4
    setEnvInput(EShSourceHlsl or EShSourceGlsl, stage,  EShClientNone, 0);
Packit Service d7a8c4
    setEnvClient(EShClientNone, 0);
Packit Service d7a8c4
    setEnvTarget(EShTargetNone, 0);
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
Packit Service d7a8c4
details. There is a block comment giving more detail above the calls for
Packit Service d7a8c4
`setEnvInput, setEnvClient, and setEnvTarget`.
Packit Service d7a8c4
Packit Service d7a8c4
### C Functional Interface (original)
Packit Service d7a8c4
Packit Service d7a8c4
This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
Packit Service d7a8c4
as the `Sh*()` interface, as all the entry points start `Sh`.
Packit Service d7a8c4
Packit Service d7a8c4
The `Sh*()` interface takes a "compiler" call-back object, which it calls after
Packit Service d7a8c4
building call back that is passed the AST and can then execute a back end on it.
Packit Service d7a8c4
Packit Service d7a8c4
The following is a simplified resulting run-time call stack:
Packit Service d7a8c4
Packit Service d7a8c4
```c
Packit Service d7a8c4
ShCompile(shader, compiler) -> compiler(AST) -> <back end>
Packit Service d7a8c4
```
Packit Service d7a8c4
Packit Service d7a8c4
In practice, `ShCompile()` takes shader strings, default version, and
Packit Service d7a8c4
warning/error and other options for controlling compilation.
Packit Service d7a8c4
Packit Service d7a8c4
## Basic Internal Operation
Packit Service d7a8c4
Packit Service d7a8c4
* Initial lexical analysis is done by the preprocessor in
Packit Service d7a8c4
  `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
Packit Service d7a8c4
  in `MachineIndependent/Scan.cpp`.  There is currently no use of flex.
Packit Service d7a8c4
Packit Service d7a8c4
* Code is parsed using bison on `MachineIndependent/glslang.y` with the
Packit Service d7a8c4
  aid of a symbol table and an AST.  The symbol table is not passed on to
Packit Service d7a8c4
  the back-end; the intermediate representation stands on its own.
Packit Service d7a8c4
  The tree is built by the grammar productions, many of which are
Packit Service d7a8c4
  offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
Packit Service d7a8c4
Packit Service d7a8c4
* The intermediate representation is very high-level, and represented
Packit Service d7a8c4
  as an in-memory tree.   This serves to lose no information from the
Packit Service d7a8c4
  original program, and to have efficient transfer of the result from
Packit Service d7a8c4
  parsing to the back-end.  In the AST, constants are propagated and
Packit Service d7a8c4
  folded, and a very small amount of dead code is eliminated.
Packit Service d7a8c4
Packit Service d7a8c4
  To aid linking and reflection, the last top-level branch in the AST
Packit Service d7a8c4
  lists all global symbols.
Packit Service d7a8c4
Packit Service d7a8c4
* The primary algorithm of the back-end compiler is to traverse the
Packit Service d7a8c4
  tree (high-level intermediate representation), and create an internal
Packit Service d7a8c4
  object code representation.  There is an example of how to do this
Packit Service d7a8c4
  in `MachineIndependent/intermOut.cpp`.
Packit Service d7a8c4
Packit Service d7a8c4
* Reduction of the tree to a linear byte-code style low-level intermediate
Packit Service d7a8c4
  representation is likely a good way to generate fully optimized code.
Packit Service d7a8c4
Packit Service d7a8c4
* There is currently some dead old-style linker-type code still lying around.
Packit Service d7a8c4
Packit Service d7a8c4
* Memory pool: parsing uses types derived from C++ `std` types, using a
Packit Service d7a8c4
  custom allocator that puts them in a memory pool.  This makes allocation
Packit Service d7a8c4
  of individual container/contents just few cycles and deallocation free.
Packit Service d7a8c4
  This pool is popped after the AST is made and processed.
Packit Service d7a8c4
Packit Service d7a8c4
  The use is simple: if you are going to call `new`, there are three cases:
Packit Service d7a8c4
Packit Service d7a8c4
  - the object comes from the pool (its base class has the macro
Packit Service d7a8c4
    `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
Packit Service d7a8c4
Packit Service d7a8c4
  - it is a `TString`, in which case call `NewPoolTString()`, which gets
Packit Service d7a8c4
    it from the pool, and there is no corresponding `delete`
Packit Service d7a8c4
Packit Service d7a8c4
  - the object does not come from the pool, and you have to do normal
Packit Service d7a8c4
    C++ memory management of what you `new`
Packit Service d7a8c4
Packit Service d7a8c4
* Features can be protected by version/extension/stage/profile:
Packit Service d7a8c4
  See the comment in `glslang/MachineIndependent/Versions.cpp`.
Packit Service d7a8c4
Packit Service d7a8c4
[cmake]: https://cmake.org/
Packit Service d7a8c4
[python]: https://www.python.org/
Packit Service d7a8c4
[bison]: https://www.gnu.org/software/bison/
Packit Service d7a8c4
[googletest]: https://github.com/google/googletest
Packit Service d7a8c4
[bison-gnu-win32]: http://gnuwin32.sourceforge.net/packages/bison.htm
Packit Service d7a8c4
[master-tot-release]: https://github.com/KhronosGroup/glslang/releases/tag/master-tot