Title: Shading Languages
1Shading Languages
2Agenda
- Introduction
- Pixars RenderMan, MSs HLSL, NVs CgFX, ATIs
RenderMonkey - Demos
- .fx file
- Comparsion
3Key Idea of a Shading Language
- Image synthesis can be divided into two basic
concerns - Shape Geometric Objects, Coordinates,
Transformations, Hidden-Surface Methods - Shading Light, Surface, Material, Texture,
- Control shading not only by adjusting parameters
and options, but by telling the shader what you
want it to do directly in the form of a procedure
4Pixars RenderMan
- Separation of Modeling and Rendering
- RenderMan serves as the interface.
- Scene Shape Shading
- The power of RenderMan is in the shading part.
5Pixars RenderMan -2
6RenderMans RIB File Structure
Options global to the entire animation Options global to the entire animation Options global to the entire animation
Frame Block Frame Block
Image options Camera options
World Block Attributes, lights, primitives
Changed Options
Another world block
Next frame block Next frame block
7RenderMans Shader
surface phong( float Ka 1, Kd 1, Ks
0.5 float roughness 0.1 color
specularcolor 1 ) normal Nf
faceforward( normalize(N), I ) vector V
-normalize(I) color C 0 illuminance( P
) vector R 2normalize(N)
(normalize(N) . normalize( L )) -
normalize( L ) C KaCs
KdCs( normalize(N) . normalize(L) )
Ksspecularcolor pow(( R . V ), 10)
Ci CCs
8RenderMans Shader -2
- Attaching to the RIB file
AttributeBegin Translate 0 -1.5
0 Rotate 20 0 0 1 Color 0.8 0.0 0.0 Surface
"phong" "Ka" .1 "Kd" .8 "Ks" 1
"roughness" 0.1 "specularcolor" 1 1 1 Basis
"bezier" 3 "bezier" 3 PatchMesh "bicubic" 13
"nonperiodic" 10 "nonperiodic" "P" 1.5 0 0 1.5
0.828427 0 0.828427 1.5 0 0 1.5 0 -0.828427 1.5 0
-1.5 0.828427 0 -1.5 0 0 -1.5 -0.828427 0
-0.828427 -1.5 0 0 -1.5 0 0.828427 -1.5 0 1.5
-0.828427 0 1.5 0 0 1.5 0 0.075 1.5 0.828427
0.075 0.828427 1.5 0.075 0 1.5 0.075 -0.828427
1.5 0.075 -1.5 0.828427 0.075 -1.5 0 0.075 -1.5
-0.828427 0.075 -0.828427 -1.5 0.075 0 -1.5 0.075
0.828427 -1.5 0.075 1.5 -0.828427 0.075 1.5 0
0.075 2 0 0.3 2 1.10457 0.3 1.10457 2 0.3 0 2 0.3
-1.10457
9Misrosofts HLSL (.fx)
- Assembly
-
- dp3 r0, r0, r1
- max r1.x, c5.x, r0.x
- pow r0.x, r1.x, c4.x
- mul r0, c3.x, r0.x
- mov r1, c2
- add r1, c1, r1
- mad r0, c0.x, r1, r0
- ...
HLSL float4 cSpec pow(max(0, dot(Nf, H)),
phongExp).xxx float4 cPlastic Cd (cAmbi
cDiff) Cs cSpec
- Simple Blinn-Phong shader expressed in both
assembly and HLSL
10nVidias CgFX Overview
11nVidias CgFX Overview -2
- Supports Microsoft .fx files
- Cg plus
- Multi-pass
- Hardware fallbacks (techniques)
- Complete Hardware states
- Tweakables
- MS .fx plus
- DirectX8 and OpenGL
12nVidias CgFX Overview -3
- CgFX (.fx)
- Manages whole rendering process
- Handles render states cross API support
- Convenient exposure of tweakables artist
controls - Cg Shaders (.cg)
- semantics directives to match your C and other
custom hardware shaders - Bind textures/parameters to specific HW registers
- Cg Runtime
- Thin API to compile on demand at runtime
- Optimizes manages .Cg for range of target HW
13nVidias CgFX Viewer
14nVidias CgFX Viewer -2
- Scene graph GUI
- .fx parameters edition
- Error reporting for easy .fx file problem
identification - Runs OpenGL, DirectX8, DirectX9
- Switch between devices at any point
15Production Pipeline with CgFX
16ATIs RenderMonkey
17ATIs RenderMonkey -2
- Try to solve some of the problems developers face
when designing software on emerging hardware - Create a flexible, extensible shader development
environment that allows easy incorporation of
existing APIs - Support for low level DirectX8/9 in the current
version - Extensible framework to support emerging HLSL
standards - DirectX9 HLSL
- OpenGL 2.0 Shading Language
- RenderMan
- Maya Shade Trees
-
18ATIs RenderMonkey -3
- Encapsulate all effect data in a single XML text
file - Each Effect Workspace consists of
- Effect Group(s)
- Effect(s)
- Pass(es)
- Render state
- Pixel Shader
- Vertex Shader
- Geometry
- Textures
- Variables and stream mapping nodes
19Demos !
20Bump Map Example
- Bump mapping simulates detail with a surface
normal that varies across a surface
21RenderMan Example
displacement lumpy ( float Km 1, frequency 1,
maxoctaves 6 string shadingspace
"shader" float truedisp 1)
point Pshad transform (shadingspace,
frequencyP) float dPshad
filterwidthp(Pshad) float magnitude fBm
(Pshad, dPshad, maxoctaves, 2, 0.5) N
Displace (normalize(N), shadingspace,
Kmmagnitude, truedisp)
22(No Transcript)
23Pixel Shader Example
ps.1.1 tex t0 // base map tex t1 // bump
map tex t2 // light vector from normalizer
cube map dp3_sat r1, t1_bx2, t2_bx2 //
N.L mul r1, r1, c0 // N.L
diffuse_light_color mul_sat r0, t0, r1 // (N.L
diffuse_light_color) base
24Cg Example
f2fb DiffuseBumpPS(v2f IN, uniform sampler2D
DiffuseMap, uniform sampler2D NormalMap,
uniform float4 bumpHeight) f2fb OUT float4
color tex2D(DiffuseMap) //fetch base
color //fetch bump normal float4 bumpNormal
expand(tex2D(NormalMap)) bumpHeight //expand
iterated light vector to -1,1 float4
lightVector expand(passthrough(IN.LightVector))
//compute final color (diffuse
ambient) float4 bump uclamp(dot3_rgba(bumpNorma
l.xyz, lightVector.xyz)) OUT.col color
bump return OUT
25(No Transcript)
26Fixed Function Example
Texture0 ltnormalMapgt Target0
Texture2D MinFilter0 Linear MagFilter0
Linear MipFilter0 Linear Texture1
ltdiffuseTexturegt Target1 Texture2D MinFilter
1 Linear MagFilter1 Linear MipFilter1
Linear
ColorOp0 DotProduct3 AlphaOp0
SelectArg1 ColorArg10 Texture ColorArg20
Diffuse AlphaArg10 Texture AlphaArg20
Diffuse ColorOp1 Modulate ColorArg11
Current ColorArg21 Texture AlphaOp1
SelectArg1
27(No Transcript)
28GL2 Logical Diagram
29OpenGLFixed Function Vertex
Vertex (object)
Vertex (clip)
TransformMVP,MV,MV-T
Normal
Vertex (eye)
ColorSecondaryColor
FrontBackColor
0,1
Lighting
FrontBackSecondaryColor
0,1
Texgen
TextureMatrixn
TexCoordn
TexCoordn
EdgeFlag
EdgeFlag
30GL2 Vertex Processor
Vertex (object)
Vertex (clip)
Uniform
Normal
Vertex (eye)
ColorSecondaryColor
VertexShader
FrontBackColor
FrontBackSecondaryColor
Temporaries
TexCoordn
TexCoordn
EdgeFlag
EdgeFlag
31OpenGLFixed Function Fragment
Color
SecondaryColor
Texn
TEn
Sum
Fog
Color
TexCoordn
0,1
z (ze,f )
Depth
Depth
Coord
Coord
FrontFacing
FrontFacing
32GL2 Fragment Processor
Color
Uniform
Texture
SecondaryColor
FragmentShader
TexCoordn
Color
z (ze,f )
Depth
Depth
Temporaries
Coord
Coord
FrontFacing
FrontFacing
33Using FX in Your Application
- Load effect
- Validate technique for hardware
- Detect parameters for technique
- Render Loop (for each object in scene)
- Set technique for the object
- Set parameter values for technique
- For each pass in technique
- Set state for pass
- Draw object
34Using FX The FX API
- LPD3DXBUFFER pError NULL
- D3DXCreateEffectFromFile(m_pd3dDevice,
_T("simple.fx"), NULL, NULL, 0, NULL, m_pEffect,
pError) - SAFE_RELEASE(pError)
- . . .
- UINT iPass, cPasses
- m_pEffect-gtSetTechnique(Simple")
- m_pEffect-gtSetVector(var1", v)
- m_pEffect-gtBegin(cPasses, 0)
- for (iPass 0 iPass lt cPasses iPass)
-
- m_pEffect-gtPass(iPass)
- m_pMesh-gtDraw()
-
- m_pEffect-gtEnd()
35Effect File Structure
- An effect is made up of multiple rendering
algorithms (techniques) each made up of one or
more passes - Effect File Structure
- Variable declarations
- Technique 1
- Pass 1
-
- Pass n
-
- Technique n
- Pass 1
-
- Pass n
36.fx file
float4x4 worldMatrix World // World or model
matrix float4x4 mvpMatrix WorldViewProjection /
/ Model View Projection float4x4
worldViewMatrix WorldView // World
View texture diffuseTexture DiffuseMap //
Diffuse Map semantic lt string File
"default_color.dds" // Default texture file
annotation gt texture normalMap NormalMap //
Normal Map semantic lt string File
"default_bump_normal.dds"// Default texture file
annotation gt float4 ambientColor Ambient lt gt
0.1, 0.1, 0.1, 1.0 float bumpHeight lt
// GUI annotations string gui "slider"
float min 0 float max 1 float step
0.1 gt 0.5
37.fx file
- void DiffuseBumpVS(float4 Position POSITION, //
Position in object space - float2 TexCoord TEXCOORD0,// Texture
coordinates -
- out float4 TexCoord0 TEXCOORD0, // Texture
coordinates - out float4 Position POSITION)// Position in
projection space -
- TexCoord0.xy IN.TexCoord.xy // Pass texture
coordinates for the diffuse map -
- Position mul(WorldViewProj, IN.Position)//
Compute position in projection space -
- void DiffuseBumpPS(float4 Position POSITION, //
Position in projection space -
- out float4 col COLOR)
-
- float4 color tex2D(DiffuseMap, UV) // Look up
the diffuse map -
- col color light // Modulate the diffuse
color by the light -
38.fx file
- sampler2D diffuseSampler sampler_state
- Texture ltdiffuseTexturegt
- MinFilter Linear
- MagFilter Linear
- MipFilter Linear
-
- sampler2D normalSampler sampler_state
- Texture ltnormalMapgt
- MinFilter Linear
- MagFilter Linear
- MipFilter Linear
-
- technique CgTechnique // Both the vertex and the
fragment shaders are in Cg -
- pass p0
-
- ZEnable true
- ZWriteEnable true
- CullMode None
39Cg Pipeline
Graphics programs are written in Cg ...
Cg Runtime API
... and compiled to ...
... low-level assembly code ...
... that runs on any GPU compatible with DirectX
or OpenGL
40Using the Cg Compiler
Application Development
Your Application
// // Diffuse lighting // float d
dot(normalize(frag.N), normalize(frag.L)) if (d
lt 0) d 0 c df4tex2D(t, frag.uv)diffuse
Cg program source code
- Load/bind program
- Specify program parameters
- Specify vertex inputs
- Render
Cg Compiler
DP3 r0.x, fTEX0, fTEX0 RSQ r0.x, r0.x MUL
r0, r0.x, fTEX0 DP3 r1.x, fTEX1,
fTEX1 RSQ r1.x, r1.x MUL r1, r1.x,
fTEX1 DP3 r0, r0, r1 MAX r0.x, r0.x, 1.0 MUL
r0, r0.x, DIFFUSE TEX r1, fTEX1, 0, 2D MUL
r0, r0, r1
Shader program assembly code
Shader Compiler (nvasm.exe, psa.exe)
Shader Binary
41Typical Production Pipeline
Artists create models, textures, maps, in DCC
tool of choice
Programmerswrite assembly for different hardware
DCC tool (Maya, Max, SoftImage, )
Scene exporter plug-in
Not the same!
Models, Textures, Maps,
Application (game, renderer, )
Scene manager
App Scene Manager hard-coded to choose at
run-time the appropriate ASM shaders state for
the hardware
42FX-Enabled Production Pipeline
Artists assign FX files to scene objects and
tweak parameters for each object in real-time
Programmers and/or artists write FX effects
DCC tool (Maya, Max, SoftImage, )
FX material plug-in
Scene exporter plug-in
Same Image!
Models, Textures, Maps, FX effects parameters
Application (game, renderer, )
Scene manager
For any FX, App Scene Manager chooses at
run-time the appropriate technique for the
hardware
FX runtime
43Comparison