Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Gpu presentation

4,341 views

Published on

Graphics Programming with Unity 3D

  • Be the first to comment

  • Be the first to like this

Gpu presentation

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

×