Introducing Sh: A Metaprogramming Language for Programmable GPUs
Sh is a metaprogramming language designed for programmable GPUs. Because these graphics processors can execute almost any effect with very high efficiency on the GPU itself rather than on the CPU, much of the graphical workload shifts from software onto specialised hardware. The graphics card effectively becomes a general-purpose secondary processor.
Sh as a High-Level GPU Language
Hand-writing GPU programs is usually tedious, since it generally means working in assembly. A high-level language lets developers program GPUs with familiar constructs and syntax while leaving hardware details to the compiler, and it also improves portability across different hardware and graphics API platforms. Sh serves exactly that purpose: it provides C++-style syntax and takes register allocation and other low-level concerns off the programmer's hands, so GPU programs can be written faster and ported much more easily.
Sh as a Metaprogramming Language
Sh is built on top of C++, although programs that use Sh need not be written in C++. It simply means the GPU programs themselves follow C++ syntax. That brings several practical benefits:
- Familiar syntax. Plenty of programmers already know C/C++ syntax, and Sh uses the same syntax.
- Type safety. Sh operations are type checked, so for example taking the dot product of a 3-vector and a 4-vector raises a compile-time error.
- Metaprogramming. Every legal C++ construct is also a legal Sh construct, so Sh programs can contain C++ if statements or function calls. This makes it easy to generate variants of the same shader (for instance, with features switched on or off) or to reuse code several times within a single shader.
- Programming "in the large." C++ object-oriented features help structure GPU programs, which can live in classes, be passed around as objects and make natural use of utility modules.
- Faster development for the Sh team. With no expression parser to maintain, the developers can concentrate on other parts of the language rather than on a complicated parser.
How Sh Works
Understanding Sh's internals is not necessary to use it, but the diagram below helps illustrate how it operates. Because Sh source is simply a C++ program, the C++ compiler builds it together with the Sh library. When the resulting binary runs, Sh instructions do not take effect right away; instead the program is converted into an intermediate representation. That representation sits somewhat higher-level than most GPU instruction sets, so it can be transformed into code for any particular GPU. The intermediate code is then handed to a backend (for example, the Sm simulator or the OpenGL ARB_{fragment,vertex}_program extensions) and turned into actual GPU assembly, which can be stored for later use or sent straight to the GPU. Compilation is fast enough to recompile the program into Sh intermediate and real GPU code on every run, though that is not required; the code can instead be stored and used in another program, which need not even be written in C++.

Sh as a Research Tool
Sh provides full language constructs for branching, including for loops and if statements. Once GPUs become powerful enough to execute these constructs (already partly true today), backends can be adapted to compile such code into real hardware assembly. In the meantime, the GPU simulator Sm implements features expected in near-future GPUs, such as a unified vertex and fragment instruction set. We intend to add more GPU-research features to Sh, including stream-processing models and texture-compression algorithms; these can first be supported by the simulator and later, hopefully, implemented in real GPUs. When such features arrive, Sh will be ready to support them.
Sponsors
AskGeek.io provides a CPU comparison and GPU comparison tool.
Sh as a Free Software Project
Sh is distributed under the GNU Lesser General Public License. This allows anyone to view the source code, redistribute it, improve it and use Sh in both open-source/free software and proprietary products, subject to certain conditions.
Origin of Sh
Sh grew out of research at the University of Waterloo Computer Graphics Lab. The language was conceived by Michael McCool and implemented by several members of the CGL. You can also meet the people behind the project.
Copyright 2003-2010 Intel Corporation.
Sh combines the familiarity of C++ with the efficiency of GPU execution, making it easier to write, organise and port graphics programs. Whether you are exploring the language for research or building shaders for real hardware, the pages on this site cover downloading Sh, browsing shaders and meeting the team behind it.
