Slashdot Mirror


Metaprogramming GPUs with Sh

Martin Ecker writes "With the advent of powerful, programmable GPUs in consumer graphics hardware, an increasing number of shading languages to program these GPUs has become available. One quite interesting language that - in many ways - has a very different approach than other mainstream shading languages (such as Cg or the OpenGL Shading Language) is Sh. The recently released book "Metaprogramming GPUs with Sh" by Michael McCool and Stefanus Du Toit, both major contributors to the Sh project, explains the basics of the Sh high-level shading language and the corresponding API and also goes into some of the details of the implementation. The book is intended for an audience that is already familiar with traditional shader development for programmable GPUs. Also, a firm background in 3D graphics programming and C++ is a must for the interested reader." Read on for the rest. Metaprogramming GPUs with Sh author Michael McCool, Stefanus Du Toit pages 308 publisher A K Peters rating 7/10 reviewer Martin Ecker ISBN 0321197895 summary A book that describes an interesting shading language and accompanying API to program GPUs.

Before discussing the book in more detail, I will try to give a basic overview of Sh, since most readers will not be familiar with it. For a more in-depth look at Sh, I recommend taking a look at a recently posted Gamasutra article by Michael McCool (http://www.gamasutra.com/features/20040716/mccool _01.shtml), the paper on Sh from the authors presented at the recently held SIGGRAPH 2004 conference (http://www.cgl.uwaterloo.ca/Projects/rendering/Pa pers/#algebra), and of course the Sh homepage at http://www.libsh.org.

Sh started out as a research project at the University of Waterloo (http://www.cgl.uwaterloo.ca), and it is both a shading language and a runtime API to use the Sh shaders. As a shading language Sh is embedded into C++ as a domain-specific language, which is made possible by using C++ operator overloading and by defining special tuple and matrix types that are used extensively in shader code. So instead of defining its own language that requires a full compiler, like other shading languages do, Sh uses regular C++ syntax to describe shader code, which is then dynamically (at runtime) compiled to a specific backend, such as a GPU or possibly even the CPU. In addition to compiling to a specific GPU or CPU target, Sh can also be used in a special stream mode where a shader is applied to a stream of input tuples. This is very useful for general purpose GPU programming where the GPU is basically used as an additional processor to the host CPU (see http://www.gpgpu.org for more information on the subject). Finally, Sh code can also be executed in an immediate mode where every Sh statement is directly executed on the host CPU (without being compiled into a shader program), which makes it very easy to debug shaders with any host debugger running on the CPU.

Due to the way Sh is embedded into C++, the full range of abstraction mechanisms offered by C++ can be used to structure and modularize shader code. Abstract base classes, regular functions, templates, and any other features offered by C++ can be used to develop shaders. This is an interesting consequence of the metaprogramming approach of Sh that also allows the use of software engineering principles in shader development, such as object orientation, that other shading languages currently cannot offer.
This kind of metaprogramming in C++ is used by an increasing number of libraries. For example, the Spirit parser framework (see http://spirit.sourceforge.net) uses a similar approach to describe and generate parsers directly in C++ instead of using traditional external tools, such as yacc or bison.

One of the most fascinating features of the Sh toolkit is the possibility to combine and connect shader programs to form new shader programs, which allows one to easily build complex shaders out of simple shader fragments. In a more general sense, Sh provides what can be called a shader algebra (see also the aforementioned SIGGRAPH 2004 paper), where shader programs are the objects on which special operators to combine and connect them are defined. An interesting application of this shader algebra is to specifically bind certain varying shader inputs to uniform variables and the other way around (this is what functional programming languages usually call currying). Also combining a matrix palette skinning shader with any light model shader (or any shaders that perform specific tasks, for that matter) is easily possible.

After this short introduction to the Sh toolkit, we shall now take a closer look at the book "Metaprogramming GPUs with Sh".
The book is split into three parts, an introduction, a reference, and an engineering overview.

The introduction consists of the first five chapters and discusses the basics of the Sh shading language and the API. In particular, the tuple and matrix types and the operators defined on them are presented. The way shader programs are defined and how parameters and attributes are handled is discussed, followed by the way textures are represented. Finally, the stream and channel concept used to feed data into shader programs is discussed. These introductory chapters contain a number of examples that demonstrate the presented concepts. Chapter three contains a quite interesting sample shader that uses constructive solid geometry techniques and metaprogramming in Sh to render text. While not the most useful use case, the shader shows some interesting capabilities of Sh, in particular the shader algebra operators. Chapter four on textures has some more nice sample shaders for doing shiny bump mapping, rendering wood and marble, and using Worley noise.

The second part of the book is a reference on Sh. Unlike references in many other computer books, this is not just a technical listing of the available features of Sh but is written in regular prose (with the occasional reference-like table here and there). The six chapters of the reference section describe how to setup and use the Sh library, and then discuss the available types, operators, and standard library functions more thoroughly than in the introduction. Additionally, the available backends are mentioned in the last chapter of this part of the book. A draft of the reference manual can also be found online at http://www.libsh.org/ref/online.

The final part of the book deals with engineering aspects of Sh. These final five chapters of the book discuss the details of the current implementation. The intermediate representation for shaders that is used by Sh is presented as well as how streams and textures are managed and stored internally. The interface between the Sh frontend and the various specific backends is discussed, as well as the current state of the optimizer including some further improvements that are planned in the future.

The images in the book are all in black and white except for 14 color plates in the middle of the book. The color plates and other images usually show teapots or animals, so they aren't all that exciting, but do demonstrate what the sample shaders presented in the book look like.

The book does not come with a CD-ROM, but with such a young library that is still under heavy development, putting a snapshot of the library's source code base on a CD-ROM would be a waste of resources. Sh itself as well as all sample shaders presented in the book can be downloaded from the Sh homepage at http://www.libsh.org. This website also has additional documentation, including some papers and the API reference documentation generated with Doxygen from the sources. Sh is distributed under a very liberal open source license (based on the zlib/libpng license) that also allows commercial use.

For the reader with enough expertise in 3D and shader programming, this book provides a concise and well-written introduction to Sh. The book will definitely contribute to enlarging the currently relative small user base of Sh and hopefully help the library grow and get more refined in the near future. Everyone familiar with "regular" high-level shading languages, such as Cg or the OpenGL Shading Language, should take a look at this book to see a new and interesting way of programming GPUs that the aforementioned languages do not offer.

About the review author:
The author has been involved in real-time graphics programming for more than 9 years and works as a games developer for arcade games. In his rare spare time he works on a graphics-related open source project called XEngine http://xengine.sourceforge.net.

You can purchase Metaprogramming GPUs with Sh from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

25 of 139 comments (clear)

  1. Wow by Anonymous Coward · · Score: 4, Funny

    I thought I'd accidentally clicked on this article.

  2. Front Page Story by mnemonic_ · · Score: 4, Funny

    Awesome story formatting. Looks great!

  3. That is sooo... awesome.... by illumin8 · · Score: 5, Funny

    Schweet!!! I always wanted to write my own CounterStrike-like 3D FPS as a shell script! Now my uber-leet shell hacking skills can make me rock-star dollars as a video game developer!

    --
    "When the president does it, that means it's not illegal." - Richard M. Nixon
  4. /bin/sh by philfr · · Score: 5, Funny

    At first read I thought they ported bash to the Radeon GPU...

    1. Re:/bin/sh by jericho4.0 · · Score: 3, Interesting

      On SGI machines there is a '/dev/opengl', that one can pipe openGL bytecode to.

      --
      "A language that doesn't affect the way you think about programming, is not worth knowing" - Alan Perlis
  5. SDL-perl by Speare · · Score: 3, Insightful
    I've done some OpenGL work with SDL's Perl bindings, but the SDL-perl group has been quite fractured over the past several months. Forks, missing maintainers, etc. I finally got texture-mapping to work, but there are some persistent bugs with missing GL constants or missing APIs which have put my spare-time projects on the shelf for now.

    This is a shame, because perl's a nice quick way of working through the edit-try-edit-try-edit development process when working with OpenGL and 3D geometrical stuff in general.

    I don't want to learn Yet Another Scripting Language just to try out some OpenGL routines, and I don't want to slog through a ton of C code makefile junk either. Script languages are a great help there.

    --
    [ .sig file not found ]
  6. Amazon linkage by bujoojoo · · Score: 4, Informative

    Or, buy it from Amazon

    --
    This space for rent
  7. /bin/sh by rcamera · · Score: 4, Funny

    i always do my low level device programming in sh. my graphics card has never been faster!

    sed | awk | sed | grep | awk | sed | awk

    --
    Wave upon wave of demented avengers March cheerfully out of obscurity into the dream
  8. lol by here4fun · · Score: 3, Insightful
    Before discussing the book in more detail, I will try to give a basic overview of Sh, since most readers will not be familiar with it.

    lol, how many languages do we need? back in the day, knowing 2 or 3 languages very well (like c and some db language) would have been enough to land a job. add to that knowing another 2 or 3 languages well enough to feel comfortable doing small coding, and you have an excellent programmer. now it seems like there are so many languages, from python to cobol to fortran. there must easily be over 50. it is like if i wanted to work at the united nations and said i knew english and spanish and german, and they asked "well what about sanskrit and tagalog". how much is enough?

    1. Re:lol by Lord+Omlette · · Score: 3, Insightful

      "I've got a hammer, and I use it for everything!"

      Excellent plan.

      --
      [o]_O
  9. What about other kinds of GPU programming? by Angst+Badger · · Score: 4, Interesting

    It may well be my utter ignorance of how GPUs work, but it would be nice, if possible, to have tools to utilize all of that computing power when I'm just displaying a simple GUI or text interface. I understand GPUs are heavily optimized for what they do, but I expect they're still good for general purpose tasks and might be really useful in some math-heavy applications.

    --
    Proud member of the Weirdo-American community.
  10. Sh? maybe. Brook? Definitely. by Esterhaus_48 · · Score: 5, Informative

    The GPGPU course was at the top of my list for SIGGRAPH 2004 this year, and I was impressed with all of the presenters' material. However, in my estimation, Sh is built to more closely resemble the existing HLSL for DX and similar GLSLang from the ARB.

    Brook, on the other hand, was written from a more C-like perspective, and approaches the GPU as a massively data-parallel stream processor (well, Sh does as well, but IMHO Brook achieves that aim more directly as is evidenced by things like iterator streams and similar kernels).

  11. Not there yet for "real" interactive framerates by adisakp · · Score: 5, Informative

    I'm a graphics and systems programmer at Midway Games.

    I think it will be a while before sh / GPU metaprogramming will be commonly used for "real" games programming. For example, their paper on Worley shaders claimed interactive rates of 14 FPS on a very simple single model for stone shading on the fastest video hardware (6800GT) currently available.

    The benefit of HLSL and Cg are that they achieve performance close to or better than the PS/VS-Asembler implementations and are orders of magnitude easier to program. This allows them to be used on hundreds of objects per frame at video game "interactive" framerates (which usually starts at 30 FPS, with 60 FPS as a gold standard, and 300 FPS or other crazy numbers as the Doom/Quake standard for some reason).

    Still it's good to see that there are languages evolving that will handle more complex shading algorithms and as the hardware becomes faster in 2 or 3 years from now, this may be practical for real-time use in video games.

    1. Re:Not there yet for "real" interactive framerates by sdt · · Score: 5, Informative

      (Disclaimer: I'm Stefanus Du Toit, one of the authors of this book and implementer of most of the Sh compiler/library)

      I think it will be a while before sh / GPU metaprogramming will be commonly used for "real" games programming. For example, their paper on Worley shaders claimed interactive rates of 14 FPS on a very simple single model for stone shading on the fastest video hardware (6800GT) currently available.

      I should point out that the Worley shaders are rather large shaders. It's not that they're written in Sh that makes them so huge, it's simply the way the algorithm works. The shaders would be approximately the same size if written in Cg (or HLSL, etc.).

      The benefit of HLSL and Cg are that they achieve performance close to or better than the PS/VS-Asembler implementations and are orders of magnitude easier to program.

      The same holds true for Sh. Both Sh and Cg target "assembly" interfaces such as RB_fragment_program. Sh provides a superset of Cg's functionality. Because in both the case of Sh and Cg the actual program is executed by the GPU, not in any way the system itself, the only difference in performance that might be caused between the two languages would be due to the optimizer. I've actually just rewritten the Sh optimizer, and the only major optimization left to do is common subexpression elimination, and some arithmetic simplifications.

      In fact, we already provide an optimization that no other shading language provides (this will be in the next release, and is working in the subversion repository) called uniform lifting. Sh will automatically discover computations which do not change from one fragment to the next, and move them to the host where they can be evaluated more efficiently. This is something that would be difficult to do in other shading languages.

      So, to summarize, there is nothing intrinsic about Sh that makes it any slower than Cg. In fact, we're thinking of adding a Cg backend, so that your Sh code can automatically be converted to equivalent Cg code. Then, if you really want, you can take advantage of the backends and optimizations provided by Cg while using the modularity facilities of Sh.

    2. Re:Not there yet for "real" interactive framerates by spworley · · Score: 4, Informative

      Yes, 14 fps seems slow, doesn't it? But what are you comparing the 14 fps to? The simple stone model they show isn't an image map, it's fully procedural and not just a texture lookup.

      I happen to know a bit about that specific procedural model, and on a modern CPU, I'd expect perhaps about 0.5s to render the same pixels.
      So that example is already an order of magnitude faster than CPU evaluations. And GPU speeds scale faster than CPU so this gulf will widen, quickly, with time.

      But the poster is correct, GPU metaprogramming is NOT going to be common in games quite yet. But it's going to happen, absolutely, and the time frame is measured in just a year or two, not some vague future date. sh is fascinating because it is a solid attempt to give developers tools to start using the GPU for different kinds of computations, not just polygons/pixels/shading.

      And the important applications are likely NOT to be games. Think 3D movie renderering, think fluid simulators, think finite element analysis, think fracture simulation.. all of those massively parallel, highly numeric applications. Graphics is just one of them.

    3. Re:Not there yet for "real" interactive framerates by adisakp · · Score: 4, Interesting

      Well that's good to know Stefanus. If your performance claims are true, I guess I'll have to take a look at Sh then :-) Now that MK6 is just about wrapped up, the guys on my team are eager to get a chance to learn and explore new technology for upcoming games.

      And here's a question that's sure to anger the general Slashdot crowd -- however, the answer will be important in whether or not your language makes it into common usage in the game industry. How do you feel about the use of your technology in closed source proprietary products (like video games)?

    4. Re:Not there yet for "real" interactive framerates by GooberToo · · Score: 3, Informative

      Considering the license that he released his efforts under, I think it speaks clear and loud.

      The zlib/libpng License

      Copyright (c)

      This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

      Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

      1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

      2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

      3. This notice may not be removed or altered from any source distribution.

  12. sh is awesome! by thelenm · · Score: 3, Funny

    I just installed this "sh" thing in /bin, and now all my shell scripts run with 3D shading! This rules!!

    --
    Use Ctrl-C instead of ESC in Vim!
  13. Mr. McCool and Mr. Du Toit by borwells · · Score: 4, Funny

    I don't care what anyone says. With names like that the book has to be awesome.

    --
    "We can't solve problems by using the same kind of thinking we used when we created them."
  14. triangle drawing example in sh by dfiguero · · Score: 3, Funny

    #!/bin/sh

    echo #
    echo ###
    echo ##### :] oh come on!!!

    --
    My penguin ate my sig
  15. Re:"Metaprogramming"? by sdt · · Score: 4, Informative

    From the review, it sounds like SH is basically a library, and that library invocations are dressed up through the use of operator overloading. Is this "metaprogramming"?

    Yes, because rather than just executing the the operations you specify, Sh has a special "retained mode", which instead collects these operations, builds an intermediate representation of them, runs an optimizer and passes them on to a backend compiler. For example:

    void foo() {
    ShAttrib3f x, y, z; // three Sh variables
    x = y + z; // Executes immediately
    ShProgram prg = SH_BEGIN_PROGRAM() {
    ShAttrib3f i, j, k;
    i = j + k; // Doesn't execute, gets collected
    } SH_END;
    }

    So, the bits that are written inside the declaration of "prg" (the above is valid C++/Sh), are actually collected into prg, instead of being executed. prg can then be compiled to a GPU (or CPU) backend and sent to the GPU to run. Thus, your C++ program is used to write Sh programs at runtime. This allows all sorts of metaprogramming techniques.

    We also provide further levels of metaprogramming with the shader algebra operations, but that's at an even higher level. These let you take previously written Sh programs and combine them in various ways, at run time. See our SIGGRAPH paper for details.

    The About Page on our homepage tries to explain this in more detail.

  16. Re:Don't Believe Everything You Read by sdt · · Score: 5, Informative

    Unfortunately, the developers of Sh chose to publish the book which details features which are not yet supported in the library.

    Yes, this is true. If we had written the book based entirely on what we had implemented at the time, it would however have become out of date completely very quickly. Instead we chose to write the book as a specification. Too many programming language books, and books describing systems become out of date as soon as they are published, so it's a tough decision to make.

    I should point out that we are getting closer to full support of everything in the book every day. Most of the missing features are very simple things like missing library functions. Sh works, right now, and can be used to write real shaders.

    With the next release I will post a fixed version of the glut example. As unfortunately happens so often, the example got broken during book writing stage...

    You're welcome to browse our Issue Tracker. Progress has been slow of late because I've been working hard on the optimizer, and we've been busy with non-Sh-related things. However, note that most of the issues in the track are either "easy" or "bitesized", and once development gets on track again (in a week or so) I expect most of them to be resolved quite quickly.

  17. Re:Not really. by tiger99 · · Score: 3, Interesting
    Yes, and I don't see why you could not run an entire GUI in the GPU. Maybe not right now, but if the chip manufacturers wanted to make it possible it would not be too difficult. Just imagine, X running in the GPU, the CPU(s) free to get on with non-graphical tasks....

    It is one place that an OS architecture which makes a clean separation of GUI and kernel would win over the mess of sundry .dlls from the Criminal Monopoly. The other place would have been the ill-fated tablet computer, had it been correctly designed, with the X serevr on the portable bit.

    Because simple single-processor relatively high-performance silicon is now very affordable, the time is right to go back to parallelism, dedicated I/O and graphics processors, etc, to get even better performance.

    It would be nice to have a GPU compiler to run ordinary code, such as X, but the compiler would be different for each architecture, not like AMD vs Intel, where you can use the same compiler with just the optimisations tweaked. But my thinking is to replicate a full CPU in the GPU chip or at least on the graphics card. You could put a Pentium/Athlon there, feeding the gPU as usual, fed itself from the AGP bus or its successor via a big FIFO.

    But whatever way things go, I do think we are in for some big steps forward in graphics processing, at least. I am not into shaders and so on, I don't need fast 3-D or anything like that, but I know that many people do, and not just for games. But if it offloads things from my main CPU, I would find immediate benefit.

  18. Re:That is sooo... awesome.... by mdmccool · · Score: 3, Informative
    The choice of the name Sh is a long, evolutionary one. That was our internal name for a long time, and we published a paper using it long before we thought we'd be releasing it publically. But then after we published the first paper on it, we didn't want to change the name (there's already some confusion between Sh and the system it grew out of, SMASH...). But, we figured no-one would be silly enough to confuse a C++ library with a scripting language, so... although believe me, we had long discussions on exactly this problem.

    Get over it. The name comes from the "Sh" prefix used on all the types, and (originally) was short for "shader".

  19. Re:"Metaprogramming"? by mdmccool · · Score: 3, Informative
    To emphasize this... Sh just "pretends" to look like a vector/matrix/colour library, since that is a familiar concept to graphics programmers. But it's this "retained mode" trick (also a common concept to graphics programmers) that lets you, basically, add dynamic definition of functions to C++. And those functions are really "remote procedure calls" that can run on another processor... like the GPU.

    Another thing to emphasize: yes, Sh does compile to the *CPU* as well as the GPU. So you can use it for dynamic code generation.

    And finally: our long term goal is to make sh suitable for all kinds of scientfic programming on all kinds of data-parallel machines, not just GPUs.