23 May, 2020•
4 min read
Shaders are pixel manipulation tools. To create shaders, we use some kind of Shading Language.
Some interesting details on Shaders can be found in this Abstract
We would be using
GLSL as our Shading Language in
Before moving forward, we should know what
is!!! SPIR-V is an intermediate language defined by Khronos
Group for Vulkan. It is the language that Vulkan understands
gfx-hal too. So to work with shaders in
we can use any Shading Language, but we need to first
convert those Shader Code into SPIR-V format and then
can we use it directly in
gfx-hal, but they are quite complex to setup.
HLSLcode to SPIR-V format and use that directly in
gfxcommunity, named naga, to support cross-platform, multi Shader Languages all in pure Rust.
naga, can be found
I am new to shaders completely. For me choosing any Shading
Language was difficult, let aside understanding
overcoming Rust's support for any stable Shader language
I am just following people here, and have found
GLSL to be used more often.
GLSL being used more effectively
in the Open Source Community. Thus, will understand and use
GLSL in our projects as well.
If you have no prior knowledge on Shaders, I would recommend to read this post once, just to get some quick hands on.
I am keeping link a to GLSL language specs here, as a reference.
Moving forward!! Teaching
GLSL is out of scope for this post,
but you can go through this awesome shaders list
for resources to teach you GLSL.
We would be testing two shaders
I would recommend you to try the solution mentioned in
Compiling GLSL to SPIR-V,
for simplicity of converting
shader files to
Though I failed to run the script mentioned in
Compiling GLSL to SPIR-V.
glsl-to-spirv is outdated.
It was part of project vulkano,
but is abandoned in
support of google's shaderc
project, which we will be using as well.
C++project and thus setting it up is tedious and harder compared to Rust's
glslcbinary, to pre-compile our
HLSLShaders, or we can use Rust Shaderc Bindings in our rust code, so as to compile our
HLSLshaders at runtime.
glslcbinary, than using
shaderc-rsin rust won't be of any trouble.
Before moving forward, you can use Shader Playground
to get some basic understanding on how
Details on compilation of
shaderc can be found
in there doc.
Though I would like to mention some dependencies, to be present
before compilation, in short here, assuming you are using
Linux or Mac OS (Mac OS should have
brew installed for simplicity).
gccshould be installed. (For MacOS use
brew install gcc)
cmakeshould be installed. (For MacOS use
brew install cmake)
git clone https://github.com/google/shaderc $SOURCE_DIR
./utils/git-sync-depsfor all important dependency to get pulled into the directory for compilation
mkdir ./build && cd ./build
gcov, if already not installed. (For MacOS use
brew install gcov)
lcov(For MacOS use
brew install lcov)
cmakebuilds (For MacOS use
brew install ninja)
shaderc, using command -
cmake -GNinja -DCMAKE_BUILD_TYPE=Release $SOURCE_DIR
warningswhich is ok, but do check that you don't get any
errorwhile binaries are getting generated.
If everything goes fine, you will find
glslc binary inside
$SOURCE_DIR/build/glslc. We can now directly use
GLSL shaders to
© Copyright 2020 Subroto Biswas