in the name of god. shader programming fundamentals by: ali seyedof tochal advanced systems, ltd
TRANSCRIPT
In the Name of God
Shader Programming Fundamentals
By: Ali Seyedof Tochal Advanced Systems, Ltd.
3
Preface
• Workshop Content• History of shaders• Why using them• Types of shaders• Tools of the trade• HLSL language basics• Implementing a simple effect• Some more effects• …
• Prerequisites• C/C++ Programming• Computer Graphics• DirectX API/Programming
But don’t worry, all of you will learn something useful!
4
Computer Graphics
• Computer Graphics• Transformations• Lighting• Texturing• Shadows• Effects• …
If you have done all above, you have done computer graphics
• Problems• Complex (Time/Space consuming) task• Lots of floating point calculations• Limited hardware and software power/support• Is not the exact physical model of the real world (Important)• More problems when doing real-time graphics e.g. Computer Games
5
Computer Graphics Cont’d
• Who handles the task?• Human (Not our topic, like painters e.g. da Vinci)• Software (Software rendering, like old games e.g. Quake)• Hardware (Hardware Accelerated rendering, like modern
games)
• Rules• Hardware implementation is faster than software (in most
cases)• Hardware is not flexible (As its name says )• Software is slow but very flexible and extensible• How about a hybrid solution??
6
History (Home Computes)
• Home Computers Era (1980-1995)• Commodore• Amiga• Atari• Sinclair ZX Spectrum• Amstrad CPC• …
• Features• Used for games and entertainment• Limited 2D graphics• Performed better than PC’s• Had limited hardware gfx acceleration and dedicated graphics processors
(Bit-Blitting, Panning, Palette tricks…)• Maximum of 4096 colors
7
History (Early PC’s)
• Early Personal Computers (1980-1995)• Started from MDA graphics cards for IBM• From text only display to SVGA resolution• Many vendors for CPU, Graphics adapters
8
History (Early PC’s) Cont’d
• Features• With purpose of business tasks• Limited usage for games and entertainment• Limited 2D graphics• Poor graphics performance compared to home
computers and gaming consoles• Had limited hardware gfx acceleration (Bit-Blitting,
Panning, Palette tricks…)• But growing much faster in both technology and user
base• More computing power led to creation of first vector
based and even 3D games
9
History (3D Accelerators)
• Hardware Accelerated PC Gfx (1995-2001)• Reaching the limitations of CPU base Gfx (Software)• SGI already had kind of hardware accelerators• Voodoo and S3 Virge came to life• Graphics Programming API’s• Close competition on producing more powerful
accelerators in 90s
10
History (3D Accelerators) Cont’d
• Features• Limited support for Graphics Pipeline• Still needed CPU for some stages• Focused on Rasterization/Texturing/Z-Buffering• No hardware T&L• Fixed Pipeline• Diversity of programming API’s• Relatively expensive• Not very popular among both users and developers
11
Graphics Pipeline
• The process of graphics is a pipeline• To be implemented in machine (hardware/software)• Has different stages
1- Transform : Transforming from world to image space2- Lighting: Doing light properties on a per-vertex basis2-Triangle setup/clipping in image space3-Rasterizing: Converting from vectors to pixels and
drawing the pixels on the output buffer (screen)
12
Graphics Pipeline Cont’d
• Where to implement each stage?• Early 3D accelerators focused on the final stages• Different types of calculation in each stage• Different type of data in each stage• Different amount of data in each stage• CPU can do everything in a serial manner• CPU is good at integer calculations not floats• Early 3D accelerators are partial hardware
implementation of the gfx pipeline• This is why we call them Accelerators not GPUs
13
Graphics API’s
• What is required to use hardware in apps?• Each hardware needs a driver• Each hardware/driver needs an API to be used by
developer• Many hardware vendor means many API’s (bad)• The need for some generic/standard/widely accepted &
used API
14
Graphics APIs Cont’d
• Glide by 3DFx• Popular in early era• Preferred over OpenGL!• Failed because the brand failed in hardware
competition and driver support by OS’s and API support by other brands
• OpenGL by Silicon Graphics• Early versions had compatibility issues• Not so friendly• Needed serious overhaul• Very portable
Graphics APIs Cont’d
• DirectX by Microsoft• Not popular at early era• Limited to windows platform• Got popular in the next decades• Nice hardware abstraction layer to support all hardware
vendors• More and faster revisions than OpenGL• Currently dominates the PC game development
15
Back to History (GPUs)
• GPU (Graphics Processing Unit)• GeForce 256 by NVidia in 1999• The first card which implemented all stages in hardware• The term Hardware T&L• The whole graphics pipeline done in hw• So this is called the first GPU which did Processing not
just some interpolation
16
History (Gfx Pipeline)
17
Back to History (GPUs)
• Programmable Pipeline• GeForce 3 card by NVidia in 2001• A revolution in the history of real-time graphics and
games• Made advanced effects possible in real-time• Opened doors of creativity to graphics programmers• What is the so called fixed/programmable pipeline
18
Fixed Pipeline
• Fixed Pipeline or Fixed Function Pipeline
19
Programmable Pipeline
• Programmable Pipeline
20
Fixed vs. Programmable Pipeline
• Fixed Pipeline Limitations/Problems• Fixed pipeline stages are all implemented in hw• No change or limited change to them• You can just change some states and values to control
drawing• Impossible to implement many complex or even simple
lighting models/formulas/effect• Needed hardware implementation/support to do those
effects• Real-time graphics and games now more interested in
visual quality than quantity (game world size)21
Fixed vs. Programmable Cont’d
• What Programmable Pipeline Offers• Programmability of the stages (flexibility)• More complex formulas/lighting models• More general code in application side• More space for creativity and hardware improvement• But more complex to learn/implement• Lack of good authoring tools• Lack of good debuggers/profilers• Lack of good support in content authoring tools e.g.
3dsmax/maya• Lack of good support in file formats and exchange of data
22
Shaders
• What are shaders?• The program that controls or runs in an stage is called
shader• The concept shader was first introduced in RenderMan
software by Pixar but not in real-time• We have different kind of shaders depending on their
occurrence in the graphics pipeline• They are pieces of code written by programmer or even
artist• The shader code runs on the GPU side not CPU• Different languages for shader programming
23
Shaders
• Shader Types• Vertex Shader
– Since the first programmable cards– Takes vertex data from application– Does per-vertex calculations e.g. T&L– Runs on every vertex input
• Pixel Shader – Since the first programmable cards– Takes pixel data from rasterizer stage– Does per-pixel calculations e.g. Bump Mapping– Runs on every input pixel
• Geometry Shader– Since Shader Model 4.0 and Geforce – Needs DX10+ or OpenGL 3.2+ (OpenGL 2.0 via extentions)– Takes the output of vertex shader to generate 0 or more primitives (generates new
primitive)– Useful in particles, cubemap rendering, terrains , tesselation,…
• Hull Shader, Compute Shader, Domain Shader– New Shader Model 5.0 shaders used for tesselation, higher order surfaces,… 24
Shader Programming Languages
• HLSL (High Level Shading Language)• Developed by Microsoft for DirectX• Available since DX9.0• Microsoft provided the Effect Framework on this• C like syntax• Used in Windows and Xbox• Poor authoring support by MS (VS2012 going to change this)• Good support in content authoring apps (3dsmax,…)• Updates with the latest shader model• Limited to both hardware and software (OS and API) platform• Used in many games
25
Shader Prog. Langs. GLSL
• GLSL (OpenGL Shading Language)• Developed by Khronos group• Available since OpenGL 2.0• C like syntax• Available on OpenGL supported devices• Poor authoring tools• Poor support in content authoring tools• Poor support in debugger/profiler tools• Updates less frequently and later than others• Good platform portability but limited API (OpenGL)• Only option in handheld platforms like iDevices and Android• Used in some GL games
26
Shader Prog. Langs. Cg
• Cg (C for Graphics)• Developed by NVidia• Available since OpenGL 2.0• C like syntax, much like HLSL• Is not dependent on platform or API !• Good authoring tools• Medium support in content authoring tools• Good support in debugger/profiler tools• NVidia developed useful tools for Cg• Independent from hw, OS, API, its in a higher abstraction layer• Used in many games• Harder to develop than HLSL in Windows (Needs extra tools, run-
time DLLs,…)• No wrappers, tools for newer devices like handhelds
27
Shader Prog. Langs. Summary
• Shader Programming Languages Summerized
28
Shader Models
• Shader Model• Some kind of standard or feature set/feature level• Describes the instruction set• Describe the capabilities• Describe hardware specs like # of registers, instruction
slots, etc
Unified Shader Model: is something else which discusses about having the same shader caps and instruction set in vertex/pixel/geometry and other TYPES of shaders. 29
Shader Models (DirectX)
• Shader Models in DirectX• Please note DirectX versions prior to DirectX 8.0 do not support
shader models • DirectX 8.0 - Shader Model 1.0 & 1.1 • DirectX 8.0a - Shader Model 1.3 • DirectX 8.1 - Shader Model 1.4 • DirectX 9.0 - Shader Model 2.0 • DirectX 9.0a - Shader Model 2.0a • DirectX 9.0b - Shader Model 2.0b • DirectX 9.0c - Shader Model 3.0 • DirectX 10.0* - Shader Model 4.0 • DirectX 10.1* - Shader Model 4.1 • DirectX 11.0* - Shader Model 5.0 • DirectX 11.1* - Shader Model 5.0*DirectX 10.0 and higher will not run in Windows XP
30
Shader Models (OpenGL)• Shader Models in OpenGL• OpenGL 1.1 - Texture objects• OpenGL 1.2 - 3D textures, BGRA and packed pixel formats• OpenGL 1.3 - Multitexturing, multisampling, texture compression• OpenGL 1.4 - Depth textures• OpenGL 1.5 - VBO, Occlusion Queries[2]
• OpenGL 2.0 - GLSL 1.1, MRT, Non Power of Two textures, Point Sprites, Two-sided stencil• OpenGL 2.1 - GLSL 1.2, Pixel Buffer Object (PBO), sRGB Textures• OpenGL 3.0 - GLSL 1.3, Texture Arrays, Conditional rendering, FBO• OpenGL 3.1 - GLSL 1.4, Instancing, Texture Buffer Object, Uniform Buffer Object, Primitive restart• OpenGL 3.2 - GLSL 1.5, Geometry Shader, Multi-sampled textures• OpenGL 3.3 - GLSL 3.30 Backports as much functionality possible from the OpenGL 4.0 specification• OpenGL 4.0 - GLSL 4.00 Tessellation on GPU, shaders with 64-bit precision• OpenGL 4.1 - GLSL 4.10 Developer-friendly debug outputs, compatibility with OpenGL ES 2.0• OpenGL 4.2 - GLSL 4.20 Shaders with atomic counters, transform feedback, shader packing, performance
improvements• OpenGL 4.3 –• For all versions of OpenGL 3.3 and above, the corresponding GLSL version matches the OpenGL version. So GL 4.1
uses GLSL 4.10. Direct3D Shader Model 4.0 is equivalent to GLSL version 3.30. Earlier GLSL versions for OpenGL 3.x provide subsets of this functionality, based on the available functionality in the OpenGL version, though 1.50 is almost feature-identical to SM4.
• Direct3D Shader Model 5.0 is equivalent to GLSL version 4.30.• For earlier shader models, GLSL versions less than 1.30 are used
31
Vertex Shaders in Shader Models
32
Pixel Shaders in Shader Models
33
Beginning HLSL Programming
• Why HLSL• The tool setup is easier• Only requires Visual Studio and DirectX SDK• So easier to write a hello world• Majority of us (in Iran) are Windows programmers and
almost all games are developed for Windows/DX• Used widely in PC games worldwide• The basics are the same between all shader languages
34
Doing an HLSL Hello World• Workflow
• Install Microsoft Visual Studio• Install Microsoft DirectX SDK (latest version is June
2010)• Create a new project (C++/C#) (Dialog Based or Win32,
doesn’t really matter)• Setup the required directories for the project (DX
include/lib paths)• Create a device and setup a simple scene (Requires DX
programming knowledge)• Write the shader• Create an Effect object in the application, load and use
the shader35
Effect Framework
• What is Effect Framework• A shader is not enough• We need to set/hold some states and structures to
control/set/run shaders• This can be done manually, depends on usage• Effect framework does this abstraction/management• In other platforms/APIs you need to use or write
something similar
36
Effect Structure
• Structure of An Effect• An effect file is a text file with a *.fx extension. It is split
into three main sections:• Variable declarations - these are values that can be set
before rendering and then used in this effect file. Examples include: textures, world matrix, lighting parameters• Techniques & Passes- defines how something is to be
rendered. It includes state information along with vertex and shader declarations.• Functions - the shader code written in HLSL
37
Effect Structure (Variable/Parameters)
• Effect Parameters• Parameters are defined at the start of the effect file. You must specify a type
and an id (a unique name). You can then provide specific values depending on the type specifying the usage, user defined data and initial values.
• Examples• float4 Diffuse : DIFFUSE = { 0, 0, 0, 1 };•
float4 is the type (this must be one of the HLSL shader types), Diffuse is the name, the unique id. After the colon is a tag specifying the usage, in this case it is to be used as a diffuse colour. Finally the value is initialised to 0,0,0,1 (red, green, blue, alpha).
• float4x4 World : WORLD;float4x4 View : VIEW;float4x4 Projection : PROJECTION;
• The above declares three parameters with ids: World, View and Projection that will be used to set the world, view and projection matrix to be used during this effect file.
• Parameters can be read from and written to at run time by the application. More about how to do this later. 38
Effect Structure (Techniques)• Techniques
Within the technique you can define the passes which contain the actual state assignments. So an example of two techniques, the second having two passes, could be:
technique FirstTechnique{ pass P0 { // Set states here for pass 0 }}
technique SecondTechnique{ pass P0 { // Set states here for pass 0 }
pass P1 { // Set states here for pass 1 }
} 39
Effect Structure (States)
• StatesYou can set states to values or expressions. They are set in a very similar way
to normal Direct3D code (as shown in the states notes). E.g. in your code you might write:
gDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
but in the effect file the same thing is achieved by writing:ColorOp[0]=SelectArg1;Other examples:
Lighting = true; // turn on lightingLightEnable[0] = true; // turn on light ZEnable = true; // turn on depth tests 40
Effect Structure (Functions)
• Functions
The above examples did not include any shader code. We could if we wanted place the assembly shader code directly into the pass e.g.:
VertexShader =asm{ vs_1_1 m4x4 oPos, v0, c4 // pos in screen space.};
Most people now prefer using a high level shader language like HLSL to write their shaders in 'C' like code. Within the effect file we can write this code in a function just like we would write any shader code. We then specify the use of this shader in a technique pass e.g.
VertexShader = compile vs_1_1 OurShaderName(); 41
Effects (Setting Parameters)
• Setting Parameters from Application
pEffect->SetTexture("t0",texture1);pEffect->SetMatrix("world",&worldMatrix);pEffect->SetMatrix("camera",&viewMatrix);pEffect->SetVector("var1", v);
42
Effects (Rendering)
• Rendering an effectm_pEffect->SetTechnique ( “DiffuseMap” ) ;
UINT nPassCount ;m_pEffect->Begin ( &nPassCount , 0 ) ;
for ( UINT nPass = 0 ; nPass < nPassCount ; nPass++ ) {
m_pEffect->BeginPass ( nPass ) ; // Do your draw code (DrawPrimitive) // ... m_pEffect->EndPass () ;
}
m_pEffect->End () ; 43
Effect Demo (In VS)
44
Tools (Shader Authoring)• Shader Authoring
• Node based (not so good)– ShaderFX (3dsmax)– Shader Fustion (Unity)– Most game engines own one
» Artists like this style» Easier» Needs no programming knowledge» Needs no graphics math» Results in complex and un-optimized shaders
• Traditional Style (not visual)– RenderMonkey (ATI/AMD)– FXComposer (NVidia)– Your IDE texteditot or any other!– 3dsmax also has support for material shaders
» Graphics Programmers like this» Difficult» Needs lots of math/programming knowledge» More optimized and specific shader code compared to the node based 45
Tools (Content Authoring)• Shader Based Content Authoring
• Shaders alone are not enough• You must be able to assign them to objects• 3dsmax/maya support shader materials• This is great, enables you to preview results while modeling• Content interchange is also important• Content interchange needs support both in your content
authoting tool and the model interchange format (.fbx, .collada, .x, etc.)• Most game engines have problems on this (bad)• Many model file formats do not support shader or their
support is provided via some extension to the base format (bad)
46
Tools (Debugging/Profiling)• Shader Debugging Tools
• Debugging is an essential part of programming• Lack of debugging makes development a cumbersome time
–consuming task• Without this you can debug a shader by visualizing outputs!• Tools are getting betters on this• NVidia nSight, which integrates to Visual Studio• AMD ShaderAnalayser, which is used to optimize shader
code• AMD PerfStudio, is used to debug, profile shaders• NVidia PerfKit, is used to debug/profile shaders• NVidia Shader Debugger, is used to debug shaders• The new Microsoft Visual Studio Graphics Debugger, used to
debug hlsl code inside Visual Studio ! 47
Recommended Reading
48
Which topics do you prefer to be discussed in the next shader workshop?
Questions?
The End