Cg Shaders with Unity




     By Michael Ivanov
     3D Graphics Geek
What are Shaders.
Geometry Deformation.
Pixels processing.

Math intensive calculations.

GPGPU.
•    D3D / OpenGL Render Pipeline Stages


                Hull           Tessellator Stage         Domain

                                 Tessellation                          Geometry Shader




 Vertex                                            Primitive
                       Vertex Shader                                     Rasterization
Assembly                                           Assembly




                                                            Raster
 Screen                                Pixels                          Fragment Shader
                                                          Operations
Vertex Shader
• Runs per input vertex.
• Transform 3D into 2D (screen space) position.
• Can perform vertex position , color , UV (texture coordinates)
  manipulation.
• Cannot create new vertices.
• Cannot “see” other vertices.
Fragment (Pixel) Shader
• Runs per fragment.
• Computes fragment’s color.
• With fragment shader you can create: bump
  mapping, shadows , lights , post processing effects and other
  cool shit.
• See http://glsl.heroku.com/
What is Cg language?
Shaders before Cg/HLSL/GLSL:

TEX H0, f[TEX0], TEX4, 2D;
TEX H1, f[TEX2], TEX5, CUBE;
DP3X H1.xyz, H1, LUMINANCE;
MULX H0.w, H0.w, LUMINANCE.w;
MULX H1.w, H1.x, H1.x;
MOVH H2, f[TEX3].wxyz;
MULX H1.w, H1.x, H1.w;
DP3X H0.xyz, H2.xzyw, H0;
MULX H0.xyz, H0, H1.w;
TEX H1, f[TEX0], TEX1, 2D;
TEX H3, f[TEX0], TEX3, 2D;
MULX H0.xyz, H0, H3;
….
………
…………..
• “C for graphics”
• High Level language.


                           Cg != C

• No classes ,pointers ,malloc ,IO etc.
• Cg has loops , conditionals , functions/overloads.
• Member variables ,local (temporary) variables constants.
• Data types: numeric primitives ,vectors , matrices
  ,arrays, structs and interfaces.
• Built-in trig and other math methods.
• Static & Dynamic compilation.
•   Cg and Graphics APIs interop scheme:
How it works in Unity
3 Ways of writing shaders in Unity:




1. Surface shaders.
2. Vertex and fragment shaders.
3. Fixed function shaders.
Simple Custom shader:
• CG Program inputs / outputs(Show code example):

                             Inputs
1. Varying inputs(Attributes) –streamed data VARYING per
   processed element.(Vertex arrays ,UV arrays,normal arrays)
2. Uniform inputs(Uniforms)-separate from the main stream
   values which don’t change each stream element.(Matrices
   , vectors)

                             Outputs
1. Varying outputs – interpolated values sent from Vertex to
   Fragment shader.
2. Binding Semantics for fragment program output: COLOR
   semantic is must to have!
Per Frame varying data from CPU:




                         Uniform                                  Uniform
                         variables                                variables
             Variables




                                     Variables




                                                      Variables




                                                                              Variables
                                      Output




                                                                               Output
               Input



  Vertex




                                                        Input
Attributes




                   Vertex Shader                            Fragment Shader


                  Frame Buffer / Texture Attachment
Passing vertex attributes and uniforms.
• Programmable setup (via scripts): (demo)
SetFloat(),SetVector(),SetMatrix(),SetColor(),SetBuffer(),SetTexture().
• Shader lab properties:
[UniformName+(*“Uniform property panel name“+, *Data Type+) = [Default value]

•   Attributes definitions are hooked to Mesh object directly:
Accessing vertex attributes
    Vertex attributes structs are defined in UnityCG.cginc include file

•   appdata_base: vertex consists of position, normal and one texture coordinate.
•   appdata_tan: vertex consists of position, tangent, normal and one texture coordinate.
•   appdata_full: vertex consists of position, tangent, normal, two texture coordinates and color.

We can define our own vertex attribute structs but must use the following names for the
members:
• float4 vertex is the vertex position
• float3 normal is the vertex normal
• float4 texcoord is the first UV coordinate
• float4 texcoord1 is the second UV coordinate
• float4 tangent is the tangent vector (used for normal mapping)
• float4 color is per-vertex color
Accessing uniforms:
Very simple:
Declare uniforms in the shader header with exactly the same names and data types as they have
in Shader Lab prop block.
Some demos.
Post-process shaders
• DOF ,SSAO, Motion Blur , Gaussian Blur ,Glow ,Blum etc.
• Done via accessing Frame Buffer texture.
Some more demos.
Advanced stuff: multi-pass shading.
• Used for advanced effects.
• Done using “pass” blocks.



//first pass
pass {
 … shader program code here:

  }
 GrabPass{} //pass first pass output into second pass
//second pass//
 pass{
  … shader program code here:
}
Even more demos.
Thank you.
il.linkedin.com/pub/michael-ivanov/16/570/a0a/
              explomaster@gmail.com
                blog.alladvanced.net
Resources
•   http://aras-p.info/blog/
•   http://docs.unity3d.com/Documentation/Manual/Shaders.html
•   http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html
•   https://developer.nvidia.com/shader-library
•   http://wiki.unity3d.com/index.php/Shaders

Cg shaders with Unity3D

  • 1.
    Cg Shaders withUnity By Michael Ivanov 3D Graphics Geek
  • 2.
  • 3.
    Geometry Deformation. Pixels processing. Mathintensive calculations. GPGPU.
  • 4.
    D3D / OpenGL Render Pipeline Stages Hull Tessellator Stage Domain Tessellation Geometry Shader Vertex Primitive Vertex Shader Rasterization Assembly Assembly Raster Screen Pixels Fragment Shader Operations
  • 5.
  • 6.
    • Runs perinput vertex. • Transform 3D into 2D (screen space) position. • Can perform vertex position , color , UV (texture coordinates) manipulation. • Cannot create new vertices. • Cannot “see” other vertices.
  • 7.
  • 8.
    • Runs perfragment. • Computes fragment’s color. • With fragment shader you can create: bump mapping, shadows , lights , post processing effects and other cool shit. • See http://glsl.heroku.com/
  • 9.
    What is Cglanguage?
  • 10.
    Shaders before Cg/HLSL/GLSL: TEXH0, f[TEX0], TEX4, 2D; TEX H1, f[TEX2], TEX5, CUBE; DP3X H1.xyz, H1, LUMINANCE; MULX H0.w, H0.w, LUMINANCE.w; MULX H1.w, H1.x, H1.x; MOVH H2, f[TEX3].wxyz; MULX H1.w, H1.x, H1.w; DP3X H0.xyz, H2.xzyw, H0; MULX H0.xyz, H0, H1.w; TEX H1, f[TEX0], TEX1, 2D; TEX H3, f[TEX0], TEX3, 2D; MULX H0.xyz, H0, H3; …. ……… …………..
  • 11.
    • “C forgraphics” • High Level language. Cg != C • No classes ,pointers ,malloc ,IO etc. • Cg has loops , conditionals , functions/overloads. • Member variables ,local (temporary) variables constants. • Data types: numeric primitives ,vectors , matrices ,arrays, structs and interfaces. • Built-in trig and other math methods. • Static & Dynamic compilation.
  • 12.
    Cg and Graphics APIs interop scheme:
  • 13.
    How it worksin Unity
  • 14.
    3 Ways ofwriting shaders in Unity: 1. Surface shaders. 2. Vertex and fragment shaders. 3. Fixed function shaders.
  • 15.
  • 16.
    • CG Programinputs / outputs(Show code example): Inputs 1. Varying inputs(Attributes) –streamed data VARYING per processed element.(Vertex arrays ,UV arrays,normal arrays) 2. Uniform inputs(Uniforms)-separate from the main stream values which don’t change each stream element.(Matrices , vectors) Outputs 1. Varying outputs – interpolated values sent from Vertex to Fragment shader. 2. Binding Semantics for fragment program output: COLOR semantic is must to have!
  • 17.
    Per Frame varyingdata from CPU: Uniform Uniform variables variables Variables Variables Variables Variables Output Output Input Vertex Input Attributes Vertex Shader Fragment Shader Frame Buffer / Texture Attachment
  • 18.
    Passing vertex attributesand uniforms. • Programmable setup (via scripts): (demo) SetFloat(),SetVector(),SetMatrix(),SetColor(),SetBuffer(),SetTexture(). • Shader lab properties: [UniformName+(*“Uniform property panel name“+, *Data Type+) = [Default value] • Attributes definitions are hooked to Mesh object directly:
  • 19.
    Accessing vertex attributes Vertex attributes structs are defined in UnityCG.cginc include file • appdata_base: vertex consists of position, normal and one texture coordinate. • appdata_tan: vertex consists of position, tangent, normal and one texture coordinate. • appdata_full: vertex consists of position, tangent, normal, two texture coordinates and color. We can define our own vertex attribute structs but must use the following names for the members: • float4 vertex is the vertex position • float3 normal is the vertex normal • float4 texcoord is the first UV coordinate • float4 texcoord1 is the second UV coordinate • float4 tangent is the tangent vector (used for normal mapping) • float4 color is per-vertex color
  • 20.
    Accessing uniforms: Very simple: Declareuniforms in the shader header with exactly the same names and data types as they have in Shader Lab prop block.
  • 21.
  • 22.
  • 23.
    • DOF ,SSAO,Motion Blur , Gaussian Blur ,Glow ,Blum etc. • Done via accessing Frame Buffer texture.
  • 24.
  • 25.
  • 26.
    • Used foradvanced effects. • Done using “pass” blocks. //first pass pass { … shader program code here: } GrabPass{} //pass first pass output into second pass //second pass// pass{ … shader program code here: }
  • 27.
  • 28.
    Thank you. il.linkedin.com/pub/michael-ivanov/16/570/a0a/ explomaster@gmail.com blog.alladvanced.net
  • 29.
    Resources • http://aras-p.info/blog/ • http://docs.unity3d.com/Documentation/Manual/Shaders.html • http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html • https://developer.nvidia.com/shader-library • http://wiki.unity3d.com/index.php/Shaders