This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

Soft-Wired Shaders
Submitted by

Software rendering is more flexible than hardware rendering, yet most
software renderers don't have as many features as modern graphics cards. And
when they do, they totally lack performance and are only useful for offline
rendering...
The main reason for this is that you need lots of control statements to
implement the different render states. Function calling and parameter
passing in the middle of the pixel pipeline is no rarity in a reference
rasterizer's shader interpreter. This causes lots of redundant memory
accesses for variables that could have stayed in registers. They often also
do not use any advanced instruction sets like MMX and SSE but are written in
plain C.
Today I present swShader, a software renderer which combines the full
flexibility of software rendering with performance comparable to
hand-written SIMD assembly. This is possible thanks to soft-wiring
technology. Because the code path for a pixel pipeline is fixed as long as
the render state doesn't change, all control statements can be eliminated by
selecting only the useful arithmetic instructions that are executed for the
current state. My SoftWire run-time assembler library
(http://softwire.sourceforge.net/) is used for dynamically generating this
code. Its run-time intrinsics make it very easy to use SIMD instructions and
perform automatic register allocation to eliminate redundant memory
accesses.
Because of this total flexibility, it was not that hard to write a compiler
for ps 2.0 code. For the parsing I use Alec Panovici's CppCC, which creates much nicer code in my opinion
than the more well-known flex/bison duo. I can't repeat enough that writing
shaders for the CPU allows total freedom and ps 2.0 is a restriction. It's
even possible to be ahead of features supported in hardware. Also, some
interesting algorithms which are hard to implement, inefficient and/or
expensive in hardware can be much simpler in software. You can add a new
feature to a software renderer in a matter of hours, while developing an
integrated circuit takes months. So free your mind and create what you want
to create!
For more information and the newest version, please check out
http://sw-shader.sourceforge.net. Read Readme.html before use. Note that in
its current state it's only an alpha release, a proof-of-concept if you
want. So don't be surprised to find tons of unimplemented methods, and bugs.
If you discover any strange behaviour that you think I am not aware of,
please report it on the sourceforge page. Of course performance isn't
comparable to dedicated hardware, but contrary to the reference rasterizer
it is real-time for simple applications. With an optimized clipper and
rasterizer, improved register allocator, a peephole optimizer and a
scheduler I hope to nearly double performance in the future...
You can read all the implementation details in my article for the upcoming
ShaderX 2 book: http://www.shaderx2.com.
Enjoy!
Nicolas "Nick" Capens