Top Related Projects
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Universal shader translation in Rust
Quick Overview
Shaderc is a collection of tools, libraries, and tests for shader compilation. It provides a command-line compiler and a library interface for converting GLSL to SPIR-V, making it easier to work with shaders in graphics applications and game engines.
Pros
- Cross-platform support (Windows, Linux, macOS, Android)
- Integrates well with existing graphics pipelines and build systems
- Provides both command-line tools and library interfaces for flexibility
- Actively maintained by Google and the open-source community
Cons
- Learning curve for those unfamiliar with shader compilation processes
- Limited to GLSL and SPIR-V, may not support other shader languages directly
- Dependency on other libraries (e.g., glslang) which may require additional setup
Code Examples
- Compiling a GLSL vertex shader to SPIR-V:
#include <shaderc/shaderc.hpp>
#include <fstream>
#include <vector>
std::vector<uint32_t> compileVertexShader(const std::string& source) {
shaderc::Compiler compiler;
shaderc::CompileOptions options;
auto result = compiler.CompileGlslToSpv(
source, shaderc_vertex_shader, "shader.vert", options);
return {result.cbegin(), result.cend()};
}
- Setting compilation options:
shaderc::CompileOptions options;
options.SetOptimizationLevel(shaderc_optimization_level_performance);
options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_1);
- Error handling:
auto result = compiler.CompileGlslToSpv(source, shaderc_fragment_shader, "shader.frag", options);
if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
std::cerr << "Error: " << result.GetErrorMessage();
// Handle error
}
Getting Started
-
Clone the repository:
git clone https://github.com/google/shaderc.git
-
Build the project:
cd shaderc mkdir build && cd build cmake .. cmake --build .
-
Include in your project:
#include <shaderc/shaderc.hpp>
-
Link against the shaderc library in your build system.
Competitor Comparisons
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Pros of glslang
- Official reference implementation for GLSL/ESSL
- Supports a wider range of shader languages (GLSL, HLSL, ESSL)
- More flexible and customizable for advanced use cases
Cons of glslang
- Steeper learning curve for beginners
- Less streamlined for simple shader compilation tasks
- Requires more manual configuration for certain use cases
Code Comparison
glslang:
#include <glslang/Public/ShaderLang.h>
glslang::InitializeProcess();
glslang::TShader shader(EShLangVertex);
shader.setStrings(&shaderSource, 1);
shader.setEnvInput(glslang::EShSourceGlsl, EShLangVertex, glslang::EShClientVulkan, 100);
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_0);
shaderc:
#include <shaderc/shaderc.hpp>
shaderc::Compiler compiler;
shaderc::CompileOptions options;
auto result = compiler.CompileGlslToSpv(shaderSource, shaderc_glsl_vertex_shader, "shader.vert", options);
Both libraries serve the purpose of compiling shaders, but glslang offers more granular control and supports a broader range of shader languages. shaderc, on the other hand, provides a more straightforward API for common shader compilation tasks, making it easier for beginners to get started. The choice between the two depends on the specific requirements of your project and your familiarity with shader compilation processes.
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Pros of DirectXShaderCompiler
- More comprehensive support for DirectX-specific features and optimizations
- Actively maintained and updated by Microsoft, ensuring compatibility with latest DirectX versions
- Includes advanced features like shader model 6.x support and ray tracing capabilities
Cons of DirectXShaderCompiler
- Larger codebase and potentially more complex to integrate into projects
- Primarily focused on DirectX, which may limit cross-platform compatibility
- Steeper learning curve for developers not familiar with DirectX ecosystem
Code Comparison
DirectXShaderCompiler:
[shader("raygeneration")]
void RayGenShader()
{
RayDesc ray = GenerateRay();
TraceRay(SceneBVH, RAY_FLAG_NONE, 0xFF, 0, 1, 0, ray, payload);
}
Shaderc:
#version 450
layout(location = 0) in vec3 inPosition;
layout(location = 0) out vec4 outColor;
void main() {
gl_Position = vec4(inPosition, 1.0);
outColor = vec4(1.0, 0.0, 0.0, 1.0);
}
Summary
DirectXShaderCompiler excels in DirectX-specific features and optimizations, while Shaderc offers a more lightweight and cross-platform approach. The choice between them depends on the project's requirements, target platforms, and the development team's expertise.
Pros of SPIRV-Tools
- More comprehensive SPIR-V toolset, including optimization and validation
- Directly maintained by Khronos Group, ensuring up-to-date SPIR-V standard compliance
- Broader language support beyond just GLSL
Cons of SPIRV-Tools
- Steeper learning curve for beginners
- Less integrated workflow for shader compilation from high-level languages
Code Comparison
SPIRV-Tools (assembling SPIR-V):
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
spv_binary binary;
spv_result_t result = spvTextToBinary(context, source, sourceLength, &binary, &diagnostic);
spvContextDestroy(context);
shaderc (compiling GLSL to SPIR-V):
shaderc::Compiler compiler;
shaderc::CompileOptions options;
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(
source, shaderc_glsl_vertex_shader, "shader.vert", options);
Both repositories provide essential tools for working with SPIR-V, but they serve different purposes. SPIRV-Tools offers a more comprehensive suite for manipulating SPIR-V directly, while shaderc focuses on compiling high-level shading languages to SPIR-V, particularly GLSL. The choice between them depends on the specific requirements of your graphics pipeline and development workflow.
SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Pros of SPIRV-Cross
- More versatile: Can convert SPIR-V to multiple shading languages (GLSL, HLSL, MSL)
- Supports reflection capabilities for shader introspection
- Actively maintained by Khronos Group, ensuring compatibility with latest standards
Cons of SPIRV-Cross
- Focuses primarily on cross-compilation, not shader compilation
- May require additional steps for full shader processing pipeline
- Less integrated with Google's ecosystem compared to shaderc
Code Comparison
SPIRV-Cross (converting SPIR-V to GLSL):
spirv_cross::CompilerGLSL glsl(spirv_data);
std::string glsl_source = glsl.compile();
shaderc (compiling GLSL to SPIR-V):
shaderc::Compiler compiler;
shaderc::CompileOptions options;
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(
shader_source, shaderc_glsl_vertex_shader, "shader.vert", options);
Both libraries serve different purposes in the shader compilation pipeline. SPIRV-Cross excels at converting SPIR-V to various shading languages, making it useful for cross-platform development. shaderc, on the other hand, focuses on compiling shaders to SPIR-V, integrating well with Google's graphics ecosystem. The choice between them depends on specific project requirements and target platforms.
Universal shader translation in Rust
Pros of naga
- Written in Rust, offering memory safety and modern language features
- Supports a wider range of shader languages, including WGSL
- More actively maintained with frequent updates
Cons of naga
- Less mature and potentially less stable than shaderc
- Smaller community and ecosystem compared to shaderc
- May have fewer optimization options for specific GPU architectures
Code Comparison
naga (WGSL to SPIR-V):
let module = naga::front::wgsl::parse_str(&source)?;
let spv = naga::back::spv::write_vec(&module, &info, &options)?;
shaderc (GLSL to SPIR-V):
shaderc::Compiler compiler;
shaderc::CompileOptions options;
auto result = compiler.CompileGlslToSpv(source, shaderc_glsl_vertex_shader, "shader.vert", options);
Summary
naga is a newer, Rust-based shader translation library with support for modern shader languages like WGSL. It offers memory safety and active development but may be less mature than shaderc. shaderc, developed by Google, is a more established option with potentially better optimization for specific GPU architectures. The choice between them depends on project requirements, language preferences, and desired shader language support.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Shaderc
A collection of tools, libraries and tests for shader compilation. At the moment it includes:
glslc
, a command line compiler for GLSL/HLSL to SPIR-V, andlibshaderc
, a library API for accessingglslc
functionality.
Note: The fact that that libshaderc
is not named libshaderc_glslc
is a
quirk of history, and a known inconsistency. Changing it would require a
significant amount of renaming and breaking of downstream projects, so it is
being left as is.
glslc
wraps around core functionality in glslang
and SPIRV-Tools. glslc
and its library aims to
to provide:
- a command line compiler with GCC- and Clang-like usage, for better integration with build systems
- an API where functionality can be added without breaking existing clients
- an API supporting standard concurrency patterns across multiple operating systems
- increased functionality such as file
#include
support
Downloads
Note: These binaries are just the artifacts of the builders and have not undergone any QA, thus they should be considered unsupported.
Status
Shaderc has maintained backward compatibility for quite some time, and we don't anticipate any breaking changes. Ongoing enhancements are described in the CHANGES file.
Shaderc has been shipping in the Android NDK since version r12b. (The NDK build uses sources from https://android.googlesource.com/platform/external/shaderc/. Those repos are downstream from GitHub.) We currently require r25c.
For licensing terms, please see the LICENSE
file. If interested in
contributing to this project, please see CONTRIBUTING.md
.
This is not an official Google product (experimental or otherwise), it is just
code that happens to be owned by Google. That may change if Shaderc gains
contributions from others. See the CONTRIBUTING.md
file
for more information. See also the AUTHORS
and
CONTRIBUTORS
files.
File organization
android_test/
: a small Android application to verify compilationcmake/
: CMake utility functions and configuration for Shadercexamples/
: Example programsglslc/
: an executable to compile GLSL to SPIR-Vlibshaderc/
: a library for compiling shader strings into SPIR-Vlibshaderc_util/
: a utility library used by multiple shaderc componentsthird_party/
: third party open source packages; see belowutils/
: utility scripts for Shaderc
Shaderc depends on glslang, the Khronos reference compiler for GLSL.
Shaderc depends on SPIRV-Tools for assembling, disassembling, and transforming SPIR-V binaries.
For testing, Shaderc depends on:
Library | URL | Description |
---|---|---|
Googletest | https://github.com/google/googletest | Testing framework |
Effcee | https://github.com/google/effcee | Stateful pattern matcher inspired by LLVM's FileCheck |
RE2 | https://github.com/google/re2 | Regular expression matcher |
Abseil | https://github.com/abseil/abseil-cpp | Common basic utilities in C++ |
In the following sections, $SOURCE_DIR
is the directory you intend to clone
Shaderc into.
Getting and building Shaderc
If you only want prebuilt executables or libraries, see the Downloads section.
The rest of this section describes how to build Shaderc from sources.
Note: Shaderc assumes Glslang supports HLSL compilation. The instructions
below assume you're building Glslang from sources, and in a subtree
of shaderc/third_party
. In that scenario, Glslang's HLSL support
is automatically enabled. Shaderc also can be built using a Glslang
from outside the shaderc/third_party
tree. In that case you must
ensure that that external Glslang is built with HLSL functionality.
See Glslang's ENABLE_HLSL
CMake setting.)
- Check out the source code:
git clone https://github.com/google/shaderc $SOURCE_DIR
cd $SOURCE_DIR
./utils/git-sync-deps
Note: The known-good
branch of the repository contains a
known_good.json
file describing a set of repo URLs and specific commits that have been
tested together. This information is updated periodically, and typically
matches the latest update of these sources in the development branch
of the Android NDK.
The known-good
branch also contains a
update_shaderc.py
script that will read the JSON file and checkout those specific commits for you.
-
Ensure you have the requisite tools -- see the tools subsection below.
-
Decide where to place the build output. In the following steps, we'll call it
$BUILD_DIR
. Any new directory should work. We recommend building outside the source tree, but it is also common to build in a (new) subdirectory of$SOURCE_DIR
, such as$SOURCE_DIR/build
.
4a) Build (and test) with Ninja on Linux or Windows:
cd $BUILD_DIR
cmake -GNinja -DCMAKE_BUILD_TYPE={Debug|Release|RelWithDebInfo} $SOURCE_DIR
ninja
ctest # optional
4b) Or build (and test) with MSVC on Windows:
cd $BUILD_DIR
cmake $SOURCE_DIR
cmake --build . --config {Release|Debug|MinSizeRel|RelWithDebInfo}
ctest -C {Release|Debug|MinSizeRel|RelWithDebInfo}
4c) Or build with MinGW on Linux for Windows:
cd $BUILD_DIR
cmake -GNinja -DCMAKE_BUILD_TYPE={Debug|Release|RelWithDebInfo} $SOURCE_DIR \
-DCMAKE_TOOLCHAIN_FILE=$SOURCE_DIR/cmake/linux-mingw-toolchain.cmake
ninja
After a successful build, you should have a glslc
executable somewhere under
the $BUILD_DIR/glslc/
directory, as well as a libshaderc
library somewhere
under the $BUILD_DIR/libshaderc/
directory.
The default behavior on MSVC is to link with the static CRT. If you would like
to change this behavior -DSHADERC_ENABLE_SHARED_CRT
may be passed on the
cmake configure line.
See the libshaderc README for more on using the library API in your project.
Tools you'll need
For building, testing, and profiling Shaderc, the following tools should be installed regardless of your OS:
- A C++17 compiler. Recent versions of Clang, GCC, and MSVC work.
- CMake 3.14 or later: for generating compilation targets.
- Shaderc is tested with cmake 3.17.2
- Python 3: for utility scripts and running the test suite.
On Linux, if cross compiling to Windows:
mingw
: A GCC-based cross compiler targeting Windows so that generated executables use the Microsoft C runtime libraries. The MinGW compiler must support C++17.
On Windows, the following tools should be installed and available on your path:
- Visual Studio 2019 or later. Previous versions of Visual Studio may work but are untested and unsupported.
- Git - including the associated tools, Bash,
diff
.
Optionally, the following tools may be installed on any OS:
asciidoctor
: for generating documentation.pygments.rb
required byasciidoctor
for syntax highlighting.
Building and running Shaderc using Docker
Please make sure you have the Docker engine installed on your machine.
To create a Docker image containing Shaderc command line tools, issue the
following command in ${SOURCE_DIR}
: docker build -t <IMAGE-NAME> .
.
The created image will have all the command line tools installed at
/usr/local
internally, and a data volume mounted at /code
.
Assume <IMAGE-NAME>
is shaderc/shaderc
from now on.
To invoke a tool from the above created image in a Docker container:
docker run shaderc/shaderc glslc --version
Alternatively, you can mount a host directory (e.g., example
) containing
the shaders you want to manipulate and run different kinds of tools via
an interactive shell in the container:
$ docker run -i -t -v `pwd`/example:/code shaderc/shaderc
/code $ ls
test.vert
/code $ glslc -c -o - test.vert | spirv-dis
Bug tracking
We track bugs using GitHub -- click on the "Issues" button on the project's GitHub page.
Bindings
Bindings are maintained by third parties, may contain content offered under a different license, and may reference or contain older versions of Shaderc and its dependencies.
- Python: pyshaderc
- Rust: shaderc-rs
- Go: gshaderc
- .NET: shaderc.net
- Common Lisp: shadercl
Top Related Projects
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Universal shader translation in Rust
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot