Pixel Shader - UniNa STiDuE

Shader
Programming
Introduzione
History
• We have seen ever-increasing graphics
performance in PCs since the release
of the first 3dfx Voodoo cards in 1995.
Although this performance increase
has allowed PCs to run graphics faster,
it arguably has not allowed graphics to
run much better.
History
• he fundamental limitation thus far in PC
graphics accelerators has been that
they are mostly fixed-function. Fixedfunction means that the silicon
designers have hard-coded specific
graphics algorithms into the graphics
chips, and as a result the game and
application developers have been
limited to using these specific fixed
algorithms.
Now
• Now, for the first time, low-cost consumer
hardware has reached the point where it can
begin implementing the basics of
programmable shading similar to the
RenderMan graphics language with real-time
performance. (Pixar’s shading language )
• The principal 3D APIs (DirectX and OpenGL)
have evolved alongside graphics hardware.
One of the most important new features in
DirectX Graphics is the addition of a
programmable pipeline that provides an
assembly language interface to the
transformation and lighting hardware (vertex
shader) and the pixel pipeline (pixel shader).
Why use Vertex Shaders?
• If you use Vertex Shaders, you bypass
the fixed-function pipeline or T&L
pipeline.
• Why would you want to skip them?
• The fixed-function pipeline doesn't give
the developer the freedom he need to
develop unique and revolutionary
graphical effects
What can u get?
• Procedural Geometry (cloth simulation, soap
bubble)
• Advanced Vertex Blending for Skinning and
Vertex Morphing (tweening)
• Texture Generation
• Advanced Keyframe Interpolation (complex
facial expression and speech)
• Particle System Rendering
• Real-Time Modifications of the Perspective
View (lens effects, underwater effect)
• Advanced Lighting Models
• First Steps to Displacement Mapping
What can u get?
• In addition to opening up creative
possibilities for developers and
artists, shaders also attack the
problem of constrained video
memory bandwidth by executing
on-chip on shader-capable
hardware.
Why use Pixel Shaders
• The final output of any 3D graphics hardware
consists of pixels. Depending on the
resolution, in excess of 2 million pixels may
need to be rendered, lit, shaded and colored.
Prior to DirectX 8.0, Direct3D used a fixedfunction for pixel processing. The effects
possible with this approach were very limited
on the implementation of the graphics card
device driver and the specific underlying
hardware. A programmer was restricted on the
graphic algorithms implemented by these
Why use Pixel Shaders
• Pixel shaders are small programs
that are executed on individual
pixels.
• a whole new programming
universe can be explored by
game/demo coders
Why use Pixel Shaders
•
•
•
•
•
•
•
•
Single pass, per-pixel lighting
True phong shading
Anisotropic lighting
Non-Photorealistic-Rendering: cartoon
shading, hatching, gooch lighting, image
space techniques
Volumetric effects
Advanced bump mapping (self-shadowing
bump maps (also known as Horizon Mapping)
Procedural textures and texture perturbation
Bidirectional reflectance distribution functions
The architecture
• Vertex
• Pixel
Vertex Shader Architecture
• All data are in a vertex shader is represented by 128-bit
quad-floats (4 x 32-bit):
Vertex Shader
Architecture
• A hardware vertex shader can be seen as a
typical SIMD (Single Instruction Multiple Data)
processor for you are applying one instruction
and affecting a set of up to four 32-bit
variables. This data format is very useful,
because most of the transformation and
lighting calculations are performed using 4x4
matrices or quaternions. The instructions are
very simple and easy to understand. The
vertex shader at their introduction doesn’t
allow any loops, jumps or conditional
branches, which means that it executes the
program linearly - one instruction after the
other.
Costants
• The constant registers (Constant Memory) are loaded
by the CPU, before the vertex shader starts executing
parameters defined by the programmer. The vertex
shader is not able to write to the constant registers.
They are used to store parameters such as light
position, matrices, procedural data for special
animation effects, vertex interpolation data for
morphing/key frame interpolation and more. The
constants can be applied within the program and they
can even be addressed indirectly with the help of the
address register a0.x, but only one constant can be
used per instruction. If an instruction needs more than
one constant, it must be loaded into on e of the
temporary regsiters before it its required. The names of
the constant registers are c0 - c95
Instructions
MIPS Instructions
OpName dest, [-]s1 [,[-]s2 [,[-]s3]]
Examples..
• Add
dest, src1, src2
• Mov
dest, sr
• Mul
dest, src1, src2
Pixel Shaders Architecture
• Before an image that is draw by
the Vertexes become available for
the Pixel Shader several step
happen.
• A vertex leaves the Vertex Shader
as a transformed and colored
vertex.
Pixel Pipeline
• Backface Culling removes all triangles, that
are facing away from the viewer or camera.
• User Clip Planes can be set by the developer
to clip triangles
• Homogenous or perspective Divide
happens. This means that the x-, y- and zcoordinates of each vertex of the
homogenous coordinates are divided by
w. The perspective divide makes nearer
objects larger, and further objects smaller as
you would expect when viewing a scene in
reality.
Pixel Shaders Rise
• Now comes the Triangle Setup, where
the life of the vertices end and the
life of the pixel begins. It computes
triangle for triangle the parameters
required for the rasterization of the
triangles.it defines the first and the last
pixel of the triangle scan line by scan
line:
Pixel Pipeline
• Rasterizer interpolates color and depth
values for each pixel from the color and depth
values of the vertices.
• These values are interpolated using a
weighted average of the color and depth
values of the edge's vertex values, where the
color and depth data of edge pixels closer to a
given vertex more closely approximate values
for that vertex. Then the rasterizer fills in
pixels for each line.
Pixel Shader
• The Pixel Shader is not involved
on the sub-pixel level. It gets the
already multisampled pixels along
with z, color values and texture
information
After the Pixel Shader
• Alpha Blending stage blends the
pixel's data with the pixel data already
in the Render Target. The blending
happens with the following formula:
• FinalColor = SourcePixelColor *
SourceBlendFactor + DestPixelColor *
DestBlendFactor
After the Pixel Shaders
• Dithering tries to fool the eye into seeing
more colors than are actually present by
placing different colored pixels next to one
another to create a composite color that the
eye will see
• Dithering tries to fool the eye into seeing
more colors than are actually present by
placing different colored pixels next to one
another to create a composite color that the
eye will see
Pixel Shaders Registers
• The Color Registers stream iterated vertex
color data from a vertex shader or a fixedfunction vertex pipeline to a pixel shader.
• Constant Registers provide constants to the
shader, that are loaded by program or in the
pixel shader with the def instruction.
• Temporary Registers rn are able to store
temporary data. The r0 register also serves as
the Output register of the pixel shader.
Pixel Shaders Instructions
• As for the Vertex Shaders the
instructions resemble a MIPS
SIMD execution unit.
• The there are different instruction
that provide texture functions.
“We come One”
• We can easly see the architecture and
the structure of Vertex and Pixel
Shaders are very strictly correlated and
very close to each other.
• Not only the datastructure used in both
Shaders is equal, also the instructions
are close..
“We come one”
• The big difference, is more where this 2
kind of Shaders are used!
• The vertex shader create a new 3D
model from the starting one
• Pass it to pixel Shaders
• Pixel Shaders Elaborate the world and
change the appearance of single pixel
“We come one”
• In future GPU that are coming out we can expect
to see more and more Unified design shaders.
• Example: ATI’s Xenos Xbox 360
• “This GPU also features 48 unified shaders, which
can be used as either vertex or pixel units.“
www.TomsHardware.com
• Windows Graphics Foundation 2, the version of
DirectX that will ship with Longhorn, will be
designed around the idea that the graphics card
will have unified vertex and pixel pipelines
ATI Point of view
“Rather than separate pixel and vertex pipelines,
we’ve created a single unified pipeline that can do
both.
Providing developers throw instructions at our
architecture in the right way, Xenos can run at
100% efficiency all the time, rather than having
some pipeline instructions waiting for others. For
comparison, most high-end PC chips run at 5060% typical efficiency.
The super cool point is that ‘in the right way’ just
means ‘give us plenty of work to do’. The
hardware manages itself.”
•
ATI European Developer Relations Manager , Richard Huddy