Date post: | 13-Jan-2015 |
Category: |
Documents |
Upload: | spartasoft |
View: | 2,606 times |
Download: | 0 times |
The GPU
Highly ParallelizedPerforms Vector, Matrix, and Texture lookup operations at fast speedsBut comes with limitations
not as flexible as the CPU rigid levels of communication
Limited forms of data output but this is changing with newer Graphics APIs DirectCompute, OpenCL, and CUDA allow more
general usage of the GPU
Two main access points in Unity
Shading The conversion of 3D models into 2D-space (your
screen) with the graphics pipeline Lighting, Bump Mapping, Toon Shading, etc
Post-Processing performing shader operations on the image that is
about to be drawn to the screen to achieve certain effects
Motion Blur, Depth of Field, SSAO, etc
The Pipeline
A 3D Model
Most important parts: A collection of points (vertices) The triangles that are drawn on those points
Additional information: Uv coordinates for Texture look-ups
2 sets (can be swapped with Unity's importer) Normals
Important for lighting Tangents
used for bump mapping
The Vertex Shader
Responsible for transforming vertex coordinatesPasses information to pixel shader
normals, uvs, vertex lighting, custom information information is interpolated between the 3 vertices of
a triangleThe vertex shader can often help obtain less accurate effects at a lower cost
vertex lighting vs. pixel lighting
The Pixel Shader
Responsible for outputting a color for each triangle fragmentLargely color-focused (usually returns a half4)
Texture Look-ups, adding bump map shading, compositing
note that the GPU utilizes a half data typeRuns ~10x as many times as vertex shader
that number may increase with newer shader pipelines
Sample Code!
Types of Lighting
Flat Normal is uniform for each triangle
Vertex / Gouraud Lighting is calculated in vertex shader for each
normal and then interpolatedPixel / Phong
Lighting is computed per fragment by having the normals interpolated
most expensive
Types of Lighting
Shaderlab
Not shader code??? Not that complicated, but can tend to abstract
things too much CG code is injected into it when more control
is needed Fairly well described in the Unity documentation Also serves as wrapper for your shaders, so
don't ignore it! Especially Colormask RGB, Cull Off, etc
Multi-pass Rendering
The other reason why you should care about Shaderlab
Essentially allows you to draw the model multiple times and combine the results together
Fairly well described in the Unity documentation Unity uses this to combine lighting equations Can be used for your own devices
Transparency + Glow
More Sample Code!
Image Effects
Some effects can happen by changing the screen in 2D space before it gets displayed
Part script, part shader Unity has a script in Pro Standard Assets that
the script can inherit from OnRenderImage and Graphics.Blit are the key Graphics.Blit will apply the material to the
image, in this case that material will have a shader set up to do the image effect
Image EffectSample Code!
Image Effect “Hacks”
Use them to do calculations outside of OnRenderImage
I've used them to do texture combination and interpolation
Remember that GPU is stupid fast
Create a buffer by rendering to a texture and then using that with a screen space effect
Distortion particle effect Render with replacement is a powerful Unity
feature (see camera docs)
More Image EffectSample Code!
The Future: Unity 3.0
Deferred Rendering Huge improvement over Multi-pass More dynamic lights per scene Access to G-Buffer
Shaderlab Supposedly being revamped to make it easier
to use (according to Aras) Occlusion Culling
Helps prevent hidden objects from wasting processing
The Future: DX11+ OpenGL 4.0
Tessellation is apparently the future Adding geometry to meshes in real time to
give them more details and better lighting Allows for models much closer to the quality of
animated movies New Shader Pipeline
The future grows the number of programmable shaders from 3 to 5
Vertex Shader → Tesselation Shader → Hull Shader → Geometry Shader → Pixel Shader
Additional Resources
NVIDIA developer zone The CG Tutorial GPU Gems (legendary for a reason)
Real Time Rendering (Akenine-Moller) ShaderX books (similar to GPU Gems) SIGGRAPH White Papers Unite Presentations
Several on shader tips and tricks