Vertex and Pixel Shaders Making the world safe
- Slides: 32
Vertex and Pixel Shaders: Making the world safe for Computer Graphics Adrian Perez 15 -463: Computer Graphics 2 February 27 th, 2001
Administrivia ® Homework 1 will be handed back on Thursday ® Midterm is next Tuesday ® Review for midterm is this Thursday.
Talk Outline ® Motivation for vertex/pixel shaders ® Vertex Shaders ® Implementation in DX 8 ® Examples ® Pixel Shaders ® Implementation in DX 8 ® Examples ® Integration with other areas of DX 8
But First…holy war update ® Direct 3 D won. Get over it. ® Open. GL cannot accommodate the speed at which the graphics industry is moving now. ® All of the extentions to do things like vertex and pixel shading are vendor-specific. ® Direct 3 D stopped sucking a few versions ago anyway.
Transformation and Lighting ® The way D 3 D used to be was fixedfunction ® Unlit primitives (objectspace position/normal/material/uv’s) ® Lit primitives (objectspace position/color/uv’s) ® Transformed-and-lit primitives (screenspace position/color/uv’s)
Transformation and Lighting ® The Good ® Provided a mechanism for people to roll their own T&L (back when everybody did so) ® The Bad ® Restrictive in function. You were bound to D 3 D’s lighting model and transformation paradigm; not a terribly big deal for awhile
Transformation and Lighting ® Games these days want more control over the hardware Important because otherwise, every game is going through the same code path, making them look very similar ® Especially important since hardware is getting unfucking-believably fast ® ® Microsoft and n. Vidia realized that a more generic way for applications to perform transformation and lighting was needed
Enter Vertex Shaders ®A Vertex Shader is essentially assembly language for the T&L engine ® ® If Subset of most ASM languages: no branching, for example hardware T&L is available, the op-codes are just downloaded to the chip and run natively ® The op-codes are interpreted by a surprisingly fast software implementation if T&L hardware isn’t available
Enter Vertex Shaders ® The Good Amazingly powerful ® You are not bound to provide it any specific data as input, as you are writing the handling code ® Much more precision – everything is in floating point ® ® The Bad Hard to debug and grok ® We’re right back in assembly hacking land ®
Vertex Shader Registers ® Input Registers ® Output Registers Name Count R/W an 1 scalar W only o. Dn 2 vectors W only c[n] 96 vectors R/W o. Fog 1 scalar W only rn 12 vectors R/W o. Pos 1 vector W only vn 16 vectors R only o. Pts 1 scalar W only o. Tn 4 vectors W only note: all vectors are 4 floats
Vertex Shader Instructions add dp 3 dp 4 dst expp lit logp mad max min mov mul rcp rsq Sum Three-components dot-product Four-component dot-product Distance vector Exponential 10 -bit precision Lighting coefficients Logarithm 10 -bit precision Multiply and add Maximum Minimum Move Multiply Reciprocal square root sge slt sub def vs exp frc log m 3 x 2 m 3 x 3 m 3 x 4 m 4 x 3 m 4 x 4 Set on greater or equal than Set on less than Subtract Define Constants Version and Type Exponential base 2 full precis. Fraction Logarithm base 2 full precision 3× 2 vector matrix multiply 3× 3 vector matrix multiply 3× 4 vector matrix multiply 4× 3 vector matrix multiply 4× 4 vector matrix multiply
Vertex Shader Semantics ® Shaders are defined in a separate source file. They are ‘compiled’ into bytecode (at runtime, if you wish) and then the bytecode can be loaded onto the card. ® When you render, you choose a vertex shader to use ® Fixed-function pipeline is still available (as a hard-coded vertex shader)
Vertex Shader Examples
Vertex Shader Examples #include "Constants. h" // v 0 -- position // v 1 -- normal // v 2 -- tex coord vs. 1. 0 // compute world space position dp 4 r 1. x, v 0, c[CV_WORLD_0] dp 4 r 1. y, v 0, c[CV_WORLD_1] dp 4 r 1. z, v 0, c[CV_WORLD_2] dp 4 r 1. w, v 0, c[CV_WORLD_3] // transform position dp 4 o. Pos. x, v 0, c[CV_WORLDVIEWPROJ_0] dp 4 o. Pos. y, v 0, c[CV_WORLDVIEWPROJ_1] dp 4 o. Pos. z, v 0, c[CV_WORLDVIEWPROJ_2] dp 4 o. Pos. w, v 0, c[CV_WORLDVIEWPROJ_3] // vector from point to eye add r 2, c[CV_EYE], -r 1 // transform normal dp 3 r 0. x, v 1, c[CV_WORLD_IT_0] dp 3 r 0. y, v 1, c[CV_WORLD_IT_1] dp 3 r 0. z, v 1, c[CV_WORLD_IT_2] // normalize normal dp 3 r 0. w, r 0 rsq r 0. w, r 0. w mul r 0, r 0. w // normalize e dp 3 r 2. w, r 2 rsq r 2. w, r 2. w mul r 2, r 2. w // e dot n dp 3 o. T 1. x, r 0, r 2 // l dot n dp 3 o. T 0. x, r 0, c[CV_LIGHT_DIRECTION]
Vertex Shader Examples
Vertex Shader Examples ; Distorts vertices in eye-space to give a fish-eye lens effect #include "trees. h" #define FACTOR_KPLUS 1 c[CV_CONSTANTS]. x #define XYZW_MAX_XZRATIO x #define XYZW_FACTOR_K y #define XYZW_ZERO z #define XYZW_ONE w vs. 1. 0 ; Transform position to view space dp 4 r 0. x, v 0, c[CV_WORLDVIEW_0] dp 4 r 0. y, v 0, c[CV_WORLDVIEW_1] dp 4 r 0. z, v 0, c[CV_WORLDVIEW_2] dp 4 r 0. w, v 0, c[CV_WORLDVIEW_3] ; scale x and y, set z and w to 0 mul r 1, r 0, c[CV_FISHEYEINFO]. xxzz ; compute normalized distance from camera (camera in viewspace is alwasy at (0, 0, 0)) ; r 1. x = sqrt(x^2 + y^2)/z dp 3 r 1. x, r 1 rsq r 1. y, r 1. x mul r 1. y, r 0. z rcp r 1. x, r 1. y ; and take the absolute value max r 1. x, -r 1. x ; compute (k*d + 1) mad r 1. z, r 1. x, c[CV_FISHEYEINFO]. XYZW_FACTOR_K, c[CV_FISHEYEINFO]. XYZW_ONE ; compute (k+1)/(k*d + 1) -- this is the reciprocal formula of the ; above referenced web-page because our distances are ; generally not less than 1. 0! rcp r 1. z, r 1. z mul r 1. xy, FACTOR_KPLUS 1, r 1. z ; only scale the original x, y with this factor mul r 0. xy, r 0, r 1 ; transform new position to clip space dp 4 o. Pos. x, r 0, c[CV_PROJ_0] dp 4 o. Pos. y, r 0, c[CV_PROJ_1] dp 4 o. Pos. z, r 0, c[CV_PROJ_2] dp 4 o. Pos. w, r 0, c[CV_PROJ_3] ; Draw using supplied tree color mov o. D 0. xyzw, v 1 mov o. T 0. xy, v 2
Vertex Shader Examples
One Last Example…
One Last Example… vs. 1. 0 ; Projected Textures ; Adrian Perez ; 2/27/01 ; Constants: ; c 0 -c 3 - World+View+Projection matrix ; c 4 -c 7 - Texture matrix 1 ; c 8 - Color ; c 9 - 1. 0 ; c 10 - -0. 5's for adjusting the texture coordinate ; c 11 - Texture position 1 (worldspace) ; c 12 -c 15 - world->model matrix ; Transform position m 4 x 4 o. Pos, v 0, c 0 ; r 3: the light positoin in worldspace mov r 3, c 11 ; Set r 0 to the clip-space view from the texture light m 4 x 4 r 0, v 0, c 4 ; Normalize r 5 with r 6 mul r 5. xyz, r 6. xyz ; Fill r 1 with 1's mov r 1, c 9 ; Compute two recriprocals to do perspective correction rcp r 1. xy, r 0. z ; r 6: Compute a dot product to find the diffuse light dp 3 r 6. x, r 5, v 3 ; Apply reciprocals mul r 2, r 0, r 1 ; subtract 0. 5's to adjust the texture coordinates add r 2. xy, c 10. xy ; r 4: the light position in modelspace m 4 x 4 r 4, r 3, c 12 ; r 5: subtract the model-space position to get a light vector sub r 5, r 4, v 0 ; r 8: store the temp length dp 3 r 8. w, r 5 ; r 6: reciprocal square root of the scalar rsq r 6. xyzw, r 8 ; modulate r 6 by the diffuse color mul r 6, r 6. xxxx, c 8 ; Write out the color and texture mov o. D 0, r 6 mov o. T 0, r 2
Pixel Shaders ® Same idea as vertex shaders, but they control how textures and colors are combined per-pixel ® Currently there is no hardware that implements them ® The Ge. Force 3 (when it comes out) will do them, so will the XBox
Pixel Shaders ® The Good ® Explicit control of texture combining ® Lots of Nifty Tricks (EMBM) ® The Bad ® Designed for a more general piece of hardware; a lot of the functionality of the Ge. Force 3 is not exposed
Pixel Shader Registers Name I/O Min. Number Max/Inst Source cn Read-only 8 2 API call rn Read/write 2 2 Written tn Read/write 4 1 Textures vn Read-only 2 1 Vertex Colors
Pixel Shader Instructions add Add cnd Conditional dp 3 Three-Component Vector Dot. Product lrp Linear Interpolation Blend mad Multiply and Add mov Copy Source Into Destination mul Modulate sub Loads the difference of the two colors in the source operands. tex No Modification texbem Bump Environment Map texbeml Bump Environment Map with Luminance texcoord Texture Coordinate texkill Mask Out Pixel texm 3 x 2 pad Input to 3× 2 Matrix Multiply texm 3 x 2 tex 3× 2 Matrix Multiply Result texreg 2 ar Remapping Alpha and Red Components texreg 2 gb Remapping Green and Blue Components texm 3 x 3 pad Input to 3× 3 Matrix Multiply texm 3 x 3 spec Specular Reflection and Environment Mapping texm 3 x 3 tex 3× 3 Matrix Multiply Result texm 3 x 3 vspec Specular Reflection/Environment Mapping without Constant Eye Vector
Pixel Shader Examples ® Unfortunately, right now the only way to develop pixel shader apps is to use the Reference Rasterizer. So source on how to use it is pretty scarce ® The documentation is also pretty cryptic, it’s not nearly as well-engineered as vertex shaders are
Pixel Shader Examples
Pixel Shader Examples
Other Cool Shit in DX 8 ® N-Patches ® Subdivision scheme that requires no adjacency information. You provide a set of vertices and vertex normals, and it renders a smooth mesh. ® Neat: You send the base level mesh down to the card, and your vertex shader gets fed the subdivided vertices. ® This is one good way to get around the fact that you can’t create new vertices in a shader ® Bezier surfaces ® Yea, yea… they’re not as cool as N-Patches
N-Patches
Other Cool Shit in DX 8 ® Volume Textures ® Holy crap these things fucking rock ® Finally, an elegant way to handle explosions and rocket flares ® Plus a million other tricks ® Third texture coordinate, they behave almost exactly like 2 D textures
Other Cool Shit not in DX 8 ® Shadow Mapping ® Supported by the Ge. Force 3, but the functionality is not exposed by DX 8. ® And yes, it is exposed in Open. GL. (sigh…)
Conclusion ® Direct. X 8. 0 is the fucking bomb.
Questions? ? ? ?
- Gl_triangles_adjacency
- Next-gen skin shaders
- High performance shader
- Christophe riccio
- Safe feed safe food
- Safe people safe places
- Lane positions
- Explain what is meant by selective use of the ipde process
- War making and state making as organized crime summary
- What is inferring
- Neighbors of a pixel
- 4-neighbors and 8 neighbors of a pixel example
- Hình ảnh bộ gõ cơ thể búng tay
- Lp html
- Bổ thể
- Tỉ lệ cơ thể trẻ em
- Chó sói
- Chụp tư thế worms-breton
- Chúa sống lại
- Các môn thể thao bắt đầu bằng tiếng đua
- Thế nào là hệ số cao nhất
- Các châu lục và đại dương trên thế giới
- Công thức tiính động năng
- Trời xanh đây là của chúng ta thể thơ
- Mật thư anh em như thể tay chân
- Làm thế nào để 102-1=99
- độ dài liên kết
- Các châu lục và đại dương trên thế giới
- Thể thơ truyền thống
- Quá trình desamine hóa có thể tạo ra
- Một số thể thơ truyền thống
- Cái miệng nó xinh thế chỉ nói điều hay thôi
- Vẽ hình chiếu vuông góc của vật thể sau