Shading Language Basics CSCD 471 Slide 1 4/5/10
Shading Language Overview Shaders describe the output of light sources, and how the light is attenuated by surfaces and volumes Execution model: Shader is only concerned with a single point on the surface and supplies information on how to calculate the color and opacity at that point. A shader starts out with a variety of information about the point being shaded but cannot find out about other points. CSCD 471 Slide 2 4/5/10
Types of Shaders In order of execution: Displacement Shaders modification of position of surface geometry or modifying the normal vectors (bump mapping) run first because they may hide some parts of the geometry and because they change surface normals that are needed for surface shaders. Surface Shaders responsible for providing surface color usually a collection of functions that generate procedural patterns, do texture lookups or both. CSCD 471 Slide 3 4/5/10
Types of Shaders Lights (light shaders) run in parallel with surface shaders which query the lights for their intensity. sometimes a uber-light is used that can be parameterized in such a way as to create any type of light you wish. Volume (atmospheric) Shaders simulate atmospheric effects such as dust or particles in the environment. last shader before image calculation CSCD 471 Slide 4 4/5/10
Types of Shaders Image Shaders only applied after the image is already shaded and cast into 2D. essentially an image processing filter applied to the near final image to allow certain effects. CSCD 471 Slide 5 4/5/10
Shading Language Components Shading Language is adapted from C and gives you: Basic data types for manipulating points, vectors, or colors. Mathematical, geometric, and string functions. Access to the geometric state at the point being shaded, including: position, normal, surface parameters, amount of incoming light. Parameters supplied to the shader, as specified in the shader declaration (or alternately attached to the geometry itself) CSCD 471 Slide 6 4/5/10
Variables and Data Classes Generalities: SL variables have both a type and a storage class associated. As in C variables can be: global variables, local variables or parameters Data Storage Classes related to how the data will be used by the SIMD machine used in the renderer. uniform : the data stored in this variable will not change when applied to different vertices in a polygon grid. CSCD 471 Slide 7 4/5/10
Variables and Data Classes varying : the data stored in this variable will have different values when applied to different vertices in a polygon grid. The default storage class is varying for local variables and uniform for parameters. CSCD 471 Slide 8 4/5/10
Data Types float – the only arithmetic type in the language – no ints or doubles. constants specified in the same way as C: e.g. 1 2.48 -4.3e2 floats are used in place of int types in control for loops and while loops CSCD 471 Slide 9 4/5/10
Data Types color – by default all colors are stored as RGB triples Assignment can be done in several ways: Using a “constructor” color c2 = color (0.5, 0.3, 0.7); Using a constructor in a different color space color c3 = color “hsv” (.2, .5, .63); c3 will be stored in rgb converted from hsv Assigning the same value into all 3 elements. color c4 = 1; this is “white” since r = g =b =1 CSCD 471 Slide 10 4/5/10
Data Types matrix – used to represent transformations inside shaders always a 16 element float array that is a 4 x 4 homogeneous transform in row major order. Initialization: matrix m1 = (0,1,2,3, 4,5,6,7, 8,9,10,11, 12,13,14,15); an identity matrix in shader space matrix ident = “shader” 1; a zero matrix in world space matrix zero = “world” 0; CSCD 471 Slide 11 4/5/10
Data Types points, normals, vectors – all are stored in a three element float array but the semantics and how they are manipulated are different. like matrices all these can be specified within a particular coordinate system or space – if no space is specified the “current” space is assumed. points are automatically transformed from the specified space to “current” space. each type has its own transform function – transform for points – vtransform for vectors – ntransform for normals. CSCD 471 Slide 12 4/5/10
Data Types points, normals, vectors - initialization examples: point p = point (0, 2.3, 1); vector v = vector (a, b, c); normal n = normal (0, 0, 1); point Q; Q = point “object” (0, 0, 0); CSCD 471 Slide 13 4/5/10
Coordinate Spaces “current” - coord system in which all points start out and in which all lighting calculations are carried out. “camera” in prman - “world” in BRMT. “object” - local system of the primitive being shaded. “shader” - coord system active when the shader was declared. “world” - the system at WorldBegin “camera” - the camera coord system. “screen” - persp. corrected system of the camera's image plane. CSCD 471 Slide 14 4/5/10
Data Types strings - stored similar to C type strings may be compared (not very efficiently) or manipulated with format() or concat() should always have storage class uniform. initialization examples: string imageMap = “/home/maps/myimage.txt” color surfaceColor = texture(imageMap, s, t); color surfaceColor = texture(“/home/maps/myimage.txt”, s, t); CSCD 471 Slide 15 4/5/10
Shader Parameters surface mtorBlinn(color ambientColor = color(0); color incandescence = color(0); float diffuseCoeff = .8; float translucenceCoeff = 0; color specularColor = color(1); float eccentricity = .5; float specularRollOff = .1; float reflectivity = 1;) Parameters have: type – name – default value(required) storage class is optional – defaults to uniform must also be declared in RIB e.g.: Declare “ambientColor” “color” Declare “diffuseCoeff” “float” CSCD 471 Slide 16 4/5/10
Arrays SL supports 1D arrays (ONLY) of all basic data types. No dynamic arrays only static sized. General array pattern: class type arrayname[arraysize] = { E1, E2, ...} arrays are not objects – cannot be compared or assigned (as an entire array) but functions can be written that compare, arrange or copy arrays. example: string textureMaps[2] = {“./tex1.tx”, “./tex2.tx”}; CSCD 471 Slide 17 4/5/10
Syntax of SL White Space: ignored by the SL compiler except as it delineates tokens End of line: always ; Comments: C-Style: /* comment */ (multiline) And C++Style: // comment (single line) CSCD 471 Slide 18 4/5/10
Shader Structure shaderType shaderName (shaderParameters) { executable RSL code } only one shader per file CSCD 471 Slide 19 4/5/10
Operators Remember the only numeric type is float Operators: =, +=, *=, /= -- assignment + -- addition - -- subtraction, unary * -- multiplication ?: (conditional operator) Vector only: . -- dot product ^ -- cross product Matrix only: * -- matrix multiplication / -- inverse multiplication CSCD 471 Slide 20 4/5/10
Point and Vector Operators An operator applied to a point, normal, vector or color is applied to every component of the type so vector (2,3,4) * vector (4,3,3) produces a new vector (8, 9, 12) + -- addition a vector added to a point returns a point a vector added to a vector returns a vector. - -- subtraction a point subtracted from a point returns a vector. * -- multiplication a vector multiplied by a float returns a scaled vector CSCD 471 Slide 21 4/5/10
Flow Control Conditions Evaluated as in C as 0 (false) or not 0 (true) but SL acts more like it has true booleans – must use conditional operators. Conditional operators: ==, != <, <=, >, >= Logical operators: &&, ||, ! All conditional and logical operators are used as they are in C CSCD 471 Slide 22 4/5/10
Flow Control if, if-else work exactly as in C Loops for loops: exactly as in C except counting variables are floats not ints. Be careful to use only uniform, not varying variables for loops. break and continue have a numeric value that allows breaking out of both an inner and outer loop. while loops: exactly as in C CSCD 471 Slide 23 4/5/10
Functions Function prototype: returntype functionname (params) { body return return_value ; } Differences from C: only one return statement per function all parameters are passed call by reference but before they can be modified with the output keyword: e.g. output float f all functions must be compiled within a single file – but they can be included files. valid return types: float, color, point, vector, normal, matrix, string CSCD 471 Slide 24 4/5/10
Functions Other Differences from C: Functions in a shader are expanded inline, not called as subroutines. this makes the code faster but bigger and means no support for recursion. Generally functions can only access local variables and parameters but globals can be accessed by declaring them extern as in C. this is true even for globals declared in the same file. CSCD 471 Slide 25 4/5/10
Pre-processor Works almost entirely as in C including: #define constants and macros #include files – works like in C – files may contain any code but often contain function definitions Included code may not contain other shader definitions Often called libraries in SL discussion Usually have a .h extension #if and #ifdef CSCD 471 Slide 26 4/5/10
Recommend
More recommend