Shading Languages -- nuts & bolts
Plan
Today
Shader Language Specifics Thursday: “Hello Shader” Lab
Shader Languages
RenderMan
Cg
How they are alike
How they are different
Shader Languages
Questions to ponder:
Boring language syntax
How to pass data from scene to shader How to pass data between shaders
RenderMan
Renderman consists of three parts:
Functional scene description mechanism (API for
C) Renderman is an Interface!
State Model Description – Maintains a current graphics
state that can be placed onto a stack.
Geometry is drawn by utilizing the current graphics state. File format - Renderman Interface Bytestream
(RIB)
Shading Language and Compiler.
https://renderman.pixar.com/products/rispec/index.htm
Runtime architecture
Rendering application RenderMan Graphics state Shader 1 Shader 2 Shader 3 slc Sh ad er / re nd er lin k Shader “object” file Shader “object” file Shader “object” fileRenderman Shading Language
Types of shaders
Light source shaders - calculates the color of a light being emitted in given direction.
Surface reflectance shaders - computes the light reflected from a surface in a given direction Volume shaders - implements the effect of light
passing through a volume of space, i.e., exterior, interior and atmospheric scattering effects. Displacement Shaders - perturb the surface of an
object
Imager Shader - post processing on image pixels.
Renderman Shading Language
Features
C-like (but not C)
Declaration – not a function but a shader Instance variables (shader arguments) Local variables
Global variables (e.g., for color and opacity for surfaces)
No return type
Shader modifies global graphic state variables
Local variable Global variables Instantiation variables (w/default) Name of shader Type of shader
Quick Tour of a Shader
surface plastic( float Ks = .5,Kd = .5, Ka = 1, roughness = .1; color specularcolor = 1) { point Nf = faceforward( N, I ); Ci = Cs; Oi = Os; Ci = Os * ( Ci * (Ka*ambient() + Kd*diffuse(Nf)) + specularcolor * Ks * specular(Nf,-I,roughness) ); }
Datatpes
float
Basic datatype (no ints)
colors
triplets (though spec doesn’t restrict this) Can specify color space
All computation done in RGB space
color (0, 0, 0)
color “rgb” (0.75, 0.5, 0.5); color “hsv” (0.2, 0.5, 0.63);
Datatypes
color
Other color spaces rbg
hsv -- hue, saturation, value hsl -- hue, saturation, lightness YIQ -- used in NTSC TV xyz -- CIE XYZ xyY -- CIE xyY
Datatypes
Point datatypes
3D vectors
Defined in some coordinate space
Structually the same, semantically different point -- location in 3D space
vector -- has length and direction normal -- “normal” vector.
Datatypes
Point datatypes
Defined in “current” coordinate system Coordinate system can be defined explicitly
Q = point “object” (0, 0, 0); Some Coordinate systems
current object shader camera screen
Datatypes
Matricies 4x4 transformation matrices (defined by 1 or 16 values)
Can be defined to transform within a given coordinate space matrix zero = 0; matrix “world” (m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33);
Datatypes
Strings
For filenames and such “This is a string”
Questions so far?
Variables
Three types:
Global -- freely available. Link to the world being rendered
Local -- variables for internal shader use Shader -- instantiation parameters.
Variables
Storage classes
uniform -- same value everywhere on the surface (not necessarily read-only) varying -- can have different values on
different points on the surface (default)
Use of uniform can lead to major
optimizations.
Variables
Arrays
Must have constant length (no dynamic allocation)
Statements
Expressions
Operators: +, - (unary & binary), *, /, ^ (vector cross product)
. (vector dot product) +=, -=, *=, /=
Implicit casting (where it make sense) Has the wonderful cond ? expr1 : expr2; No support for C-operators:
&, |, ^, &=, |=, ^=, %, ++,
--Statements
Supports:
if/then -- if/then/else while forBuilt in functions
Trigonometric
radians,degrees,sin,cos,tan,asin,acos,atan Exponential
pow,exp,log,sqrt,inversesqrt Math
abs,sign,floor,ceil,round,mod,min,max, clamp, mix (alpha blending), step, smoothstepBuilt in functions
Color
comp (ith component), setcomp, ctransform
Geometric
xcomp,ycomp,zcomp,setxcomp,setycomp, setzcompBuilt in functions
Vector functions
length, distance, normalize faceforward, reflect, refract transform, vtransform, ntransform rotate
String functions
printf, concat, match
Built in functions
Matrix functions
determinant, translate, rotate, scale
Specialized functions
We’ll get to as we discuss particular effects. (e.g. noise, texture, lighting, etc.)
Passing data
Shader instantiation parameters
Means of passing data from RIB file to shader
Global variables
Defines “graphics state” at time of shading. Can be read/written by shaders
Allows communication between shaders
Renderman Shading Language
Attaching shaders to object
RiLightHandle RiLightSource (“name”, parameterlist); orLightHandle LightSource “name” parameterlist - sets shader “name” to be the current light source shader
RiSurface (“name”, parameterlist); or Surface “name” parameterlist
- sets shader “name” to be the current surface shader.
RiAtmosphere (“name”, parameterlist); or Atmosphere “name” parameterlist
- sets shader “name” to be the current atmosphere shader.
RenderMan scene description
WorldBeginLightSource "ambientlight" 1 "intensity" 0.5
LightSource "distantlight" 1 "from" [0 1 4] "to" [0 0 0] "intensity" 0.8 AttributeBegin
Color [ 1.0 1.0 1.0 ]
Surface "brick" "brickwidth" 1.0 "brickheight" 0.25 "mortarthickness" 0.05 Polygon "P" [ -5 -5 0 5 -5 0 5 5 0 -5 5 0 ] AttributeEnd AttributeBegin Translate 0 0 2 Color [1 1 .06] Surface "plastic" Sphere 1 -1 1 360 AttributeEnd WorldEnd
Global Variables
point P - position of point being shaded normal N - normal at P
normal Ng - true normal at P vector I - view (incident) vector color Cs, Os - surface color/opacity float u,v - 2D parametric coords float s,t - 2D texture coords
vector dPdu, dPdv - partial derivatives at P float du, dv - change in u,v per sample vector L, color Cl - direction and color of light color Ci, Oi - final color / opacity
Renderman
Global
Variables
[Renderman Companion,296] Local variable Global variables Instantiation variables (w/default) Name of shader Type of shaderQuick Tour of a Shader
surface plastic( float Ks = .5,Kd = .5, Ka = 1, roughness = .1; color specularcolor = 1) { point Nf = faceforward( N, I ); Ci = Cs; Oi = Os; Ci = Os * ( Ci * (Ka*ambient() + Kd*diffuse(Nf)) + specularcolor * Ks * specular(Nf,-I,roughness) ); }
Shader order in prman
Displacement Shader Surface Shader
Queries light shaders
Volume Shaders Imager Shader
Data passed to/from shaders via global variables.
Local Variables
class type name[arrlen] = { a, b, c, …}; Can be placed anywhere
float a; uniform float b; float c = 1; float d = b*a; float e[10];
Shader params
surface plastic( float Ks = .5, Kd = .5, Ka = 1, roughness = .1; color specularcolor = 1)
In RIB file:
Declare “Kd” float Declare “roughness” float
Surface “plastic” “Kd” [0.8] “roughness” [0.1]
SL functions
Can write your own functions called by shaders.
Only 1 return statement
Functions passed by reference but read-only Can be declared as output
No separate compilation (use #include)
Variables can be declared as extern
SL functions
float x, y;float myFunc (float f, output color c) {
float x; /* local */ extern float y; /* global y */ extern point P; /* global P */ …
return x; }
Questions?
Graphics Hardware Pipeline
Graphics Hardware Pipeline
Vertices
define geometrical primitives Position in 3D space
Color / text coords / materials / etc.
Fragments
“pixel state” -- Correspond to pixels 3D -> 2D
Projection Scan conversion
Programmable Graphics Pipeline
Programmable Graphics Pipeline
Vertex processor
Operates on vertices Vertex shaders
Fragment processor
Operates on fragments Fragment / Pixel shader
return type
Struct for return semantic
Sample Shader
struct my_Output {
float4 position : POSITION; float4 color : COLOR; };
my_Output green (float2 position : POSITION) {
my_Output OUT;
OUT.position = float4 (position, 0, 1); OUT.color = float4 (0, 1, 0, 1); return OUT;
}
args
Datatypes
float -- 32 bit IEEE floating point
half -- half a float (16 bit)
int -- 32 bit integer
fixed -- 12 bit fixed
bool -- boolean
Datatypes
Vector types
Packed arrays (more efficient than normal arrays)
float4, float3, float2, float1 bool4, bool3, bool2, bool1
Matrix types
float1x1, float2x2, float3x3, float4x4 Not the same as 2D array!
Variables
structs
Defines new data structure
Arrays
Supported but pointers are not.
Statements and expressions
Control flow
return, if/else, for, while
Function overloading based on parameter types
Basic arithmetic operations (those in C)
For vectors as well as scalars.
Matrix/vector multiplication
mul()
Statements and expressions
Boolean comparison
&&, ||, !, <, >, etc. Allowed on vectors Not short-ciruited!
Swizzle operator
Allows vector components to be rearranged to form new vector
float3(a, b, c).zyx yields float3(c, b, a) float4(a, b, c, d).xxyy yields float4(a, a, b, b) float2(a, b).yyxx yields float4(b, b, a, a) float4(a, b, c, d).w yields d
Statements and expressions
Write mask operator
Selectively overwrites components of vectors.
float4 color = float4(1.0, 1.0, 0.0, 0.0); color.a = 1.0; // Set a to 1.0, leaving RGB alone.
And the operator:
( cond ) ? expr1 : expr2;
Standard library functions
Mathematical / Trigonometric
abs, clamp, ceil, floor, etc. sin, cos, tan, etc. exp, log, log10, log2 min, max
lerp, noise
Vector / Matrix
cross, dot, mul, transpose, determinant faceforward, reflect, refract
Standard Library Functions
Texture functions
More when we get to textures
Derivative functions
Inputs and Outputs
Shader operates on streams of data Gets executed once for each element in the
stream
Vertex shader - Stream data are vertices Fragment shaders - Stream data are fragments. What comes out of vertex shaders go into
fragment shaders.
Inputs
Varying inputs -- data that changes with
each element in the stream
Uniform inputs -- data that doesn’t
change with each element of the
stream (e.g. graphics state)
Binding Semantics
Link between shader and world being shaded. Link between vertex shaders and fragment
shaders
Predefined and bound in OpenGL /DirectX program
POSITION, NORMAL, TANGENT, COLOR TEXCOORD0
(Corresponds to registers)
A note about profiles
Different cards support different
features.
Profile = subset of Cg commands
supported by a given card
arbfb1 arbvp1 Basic GL fp30 vp30 Adv nVidia fp20 vp20 Basic nVidia Fragment Vertex
A note about profiles
When writing shaders:
More advanced profile More capability More speed More efficiency Less portability
Must specify profile when compiling
Profile error -- if using feature not supported by a profile.
Using Cg shaders
Cg Runtime
Cg Runtime (OpenGL version)
Include files
#include <Cg/cg.h> #include <Cg/cgGL.h>
Creating a context (for storing Cg shaders)
CGcontext context = cgCreateContext();
Compiling a Cg program
CGprogram program = cgCreateProgram(context, CG_SOURCE, myVertexProgramString, CG_PROFILE_ARBVP1, "main", args);
Cg Runtime
Loading a compiled program
cgGLLoadProgram(program);
Modifying shader parameters
CGparameter myParameter = cgGetNamedParameter ( program, "myParameter"); cgGLSetParameter4fv(myParameter, value);
Cg Runtime
Executing a shader
cgGLEnableProfile(CG_PROFILE_ARBVP1); cgGLBindProgram(program); Cleanup
cgDestroyProgram(program); cgDestroyContext(context);Cg vs. RenderMan
Shader vs. World
Both use “global variables” to hook into world
Both use “global variables” to communicate between shaders
World model
Both use a “graphics state” model.
Cg vs. RenderMan
Types of shaders
RenderMan: 6 types, related to logical function
Cg: 2 types, related to hardware functions.
Support
RenderMan: assumed software renderer. Most features assumed supported. Cg: hardware profiles