Introduction to Shader Programming

Slides:



Advertisements
Similar presentations
Perspective aperture ygyg yryr n zgzg y s = y g (n/z g ) ysys y s = y r (n/z r ) zrzr.
Advertisements

COMPUTER GRAPHICS CS 482 – FALL 2014 NOVEMBER 10, 2014 GRAPHICS HARDWARE GRAPHICS PROCESSING UNITS PARALLELISM.
Point Lighting Using Shaders Game Design Experience Professor Jim Whitehead March 13, 2009 Creative Commons Attribution 3.0 (Except copyrighted images.
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
Particle Systems Final Exam Study Guide Game Design Experience Professor Jim Whitehead March 16, 2009 Creative Commons Attribution 3.0 (Except copyrighted.
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Introduction to Computer Graphics Ed Angel Professor of Computer Science, Electrical and.
Introduction to 3D in XNA Game Design Experience Professor Jim Whitehead February 27, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Triangles, Translations Game Design Experience Professor Jim Whitehead March 2, 2009 Creative Commons Attribution 3.0 (Except copyrighted images) creativecommons.org/licenses/by/3.0.
The Graphics Pipeline CS2150 Anthony Jones. Introduction What is this lecture about? – The graphics pipeline as a whole – With examples from the video.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
Week 3 - Wednesday.  What did we talk about last time?  Project 1  Graphics processing unit  Programmable shading.
CSE 872 Dr. Charles B. Owen Advanced Computer Graphics1 Illumination and Shading Lights Diffuse and Specular Illumination BasicEffect Setting and Animating.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
CHAPTER 4 Window Creation and Control © 2008 Cengage Learning EMEA.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Real-time Graphical Shader Programming with Cg (HLSL)
Geometric Objects and Transformations. Coordinate systems rial.html.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
GPU Shading and Rendering Shading Technology 8:30 Introduction (:30–Olano) 9:00 Direct3D 10 (:45–Blythe) Languages, Systems and Demos 10:30 RapidMind.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Week 2 - Wednesday CS361.
Chris Kerkhoff Matthew Sullivan 10/16/2009.  Shaders are simple programs that describe the traits of either a vertex or a pixel.  Shaders replace a.
A Crash Course in HLSL Matt Christian.
Week 3 - Monday.  What did we talk about last time?  Graphics rendering pipeline  Rasterizer Stage.
The Graphics Rendering Pipeline 3D SCENE Collection of 3D primitives IMAGE Array of pixels Primitives: Basic geometric structures (points, lines, triangles,
1 Introduction to Computer Graphics SEN Introduction to OpenGL Graphics Applications.
Computer Graphics I, Fall 2008 Introduction to Computer Graphics.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
CS662 Computer Graphics Game Technologies Jim X. Chen, Ph.D. Computer Science Department George Mason University.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
Ray Tracing using Programmable Graphics Hardware
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
OpenGL Shading Language
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
GPU Computing for GIS James Mower Department of Geography and Planning University at Albany.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
GLSL Review Monday, Nov OpenGL pipeline Command Stream Vertex Processing Geometry processing Rasterization Fragment processing Fragment Ops/Blending.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
How to use a Pixel Shader CMT3317. Pixel shaders There is NO requirement to use a pixel shader for the coursework though you can if you want to You should.
COMPUTER GRAPHICS CHAPTER 38 CS 482 – Fall 2017 GRAPHICS HARDWARE
- Introduction - Graphics Pipeline
Week 8 - Monday CS361.
Week 3 - Monday CS361.
Week 8 - Wednesday CS361.
Programmable Pipelines
Graphics Processing Unit
Chapter 6 GPU, Shaders, and Shading Languages
CS451Real-time Rendering Pipeline
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Day 05 Shader Basics.
UMBC Graphics for Games
Chapter VI OpenGL ES and Shader
Computer Graphics Practical Lesson 10
Programming with OpenGL Part 3: Shaders
Computer Graphics Introduction to Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Introduction to Shader Programming Game Design Experience Professor Jim Whitehead March 9, 2009 Creative Commons Attribution 3.0 (Except copyrighted images and example Shader) creativecommons.org/licenses/by/3.0

Announcements Days until Final Project Due: 7 3D modeling homework Due Monday, March 16 Few students have been attending help sessions We will not be able to help you as well at the last minute 3D modeling homework Due Today, by 5PM Submit code via Homework submission website

What is a Shader? Recall that all 3D drawing in XNA uses a Shader Have been using BasicEffect shader so far But, more generally, what is a shader? Today, gaming computers have both a CPU, and a GPU CPU is on motherboard, GPU is on graphics card CPU is an unspecialized computer GPU is a computer specialized for 3D graphics Advantage: faster 3D graphics, more effects, larger scenes A Shader is a small program that runs on the GPU Written in a Shader language (HLSL, Cg, GLSL) XNA supports only the HLSL shader language

ARB Vertex/Fragment (OpenGL ARB, 2002) Shader Languages Currently 3 major shader languages Cg (Nvidia) HLSL (Microsoft) Derived from Cg GLSL (OpenGL) Main influences are C language pre-existing Shader languages developed in university and industry Source: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html (Modified with information on HLSL and GLSL) ARB Vertex/Fragment (OpenGL ARB, 2002) HLSL (Microsoft, 2002?) GLSL (OpenGL ARB, 2003)

Brief history Initially, computers did not have specialized graphics hardware In mid-90’s 3D acceleration hardware appeared OpenGL typically provided better support DirectX 7 (1999) introduced support for hardware T&L Transform and lighting Moved vertex transformations and lighting computations from CPU to GPU Improved game graphics, but at a cost: lighting and display calculations hard-wired into cards Led to games having similar look In 2002, first consumer-level programmable GPUs became available Led to development of Cg, HLSL, and GLSL shader languages Benefit: can have game-specific custom graphics programs running on GPU Games can have very distinctive visuals

Types of Shaders Shaders (GPU programs) are specialized into 3 different types: Vertex shaders Executed once per vertex in a scene. Transforms 3D position in space to 2D coordinate on screen Can manipulate position, color, texture coordinates Cannot add new vertices Geometry shaders Can add/remove vertices from a mesh Can procedurally generate geometry, or add detail to shapes Pixel shaders (fragment shaders) Calculates the color of individual pixels Used for lighting, texturing, bump mapping, etc. Executed once per pixel per geometric primitive

Shader control flow CPU GPU C#/XNA program sends vertices and textures to the GPU These are the input for the vertex and pixel shader Shader executes vertex shader Once per vertex Shader executes pixel shader Once per pixel in each primitive object vertices, textures C#/XNA program Vertex Shader Pixel Shader display

Anatomy of a Shader in HLSL Shader is a program written in textual form in HLSL Programs tend to have these parts Global variables Variables used by multiple functions Way to pass arbitrary data from C#/XNA to Shader Data structure definitions Data structures used within the shader functions Vertex and Pixel shaders Functions written in HLSL Techniques Describe grouping of vertex and pixel shaders Describe ordering of same Global variables Data structure definitions Vertex shading functions Pixel shading functions Techniques (calls to vertex and pixel shading functions)

Common data types in HLSL HLSL has well known data types int, float, bool, string, void Vectors float3, float4 – 3/4 item floating point vector float4 color = float4(1, 0, 0, 1); Red, in RGBA (red, green, blue, alpha) color space Used to represent vertices, colors Matrices floatRxC – creates matrix with R rows, C cols Float4x4 – a 4x4 matrix Used to represent transformation matrices Structures struct structname { variable declarations of members } Example: struct myStruct { float4 position;

Passing Information to/from a Shader There are two ways information is passed into a Shader Directly set global variables In C#/XNA: effect.Parameters[“global variable name”].SetValue(value) Example: HLSL: float4x4 World;  The global variable C#/XNA: effect.Parameters[“World”].SetValue(Matrix.Identity); Semantics “Magic” variables Names and meaning are hard-wired by HLSL language specification Examples: POSITION0: a float4 representing the current vertex When the HLSL program is executing, before each Vertex shader is called, POSITION0 is updated with the next vertex COLOR0: a float4 representing the current pixel color

Example Shader float4x4 World; float4x4 View; float4x4 Projection; struct VertexShaderInput { float4 Position : POSITION0; }; struct VertexShaderOutput { float4 Position : POSITION0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); float4 viewPosition = mul(worldPosition, View); output.Position = mul(viewPosition, Projection); return output; } Example is Shader from Chapter 13 of Learning XNA 3.0, Aaron Reed, O’Reilly, 2009. Global variables semantic Data structures Vertex Shader Computes final output position (x,y,z,w) from input position

Example Shader (cont’d) An output semantic float4 PixelShaderFunction() : COLOR0 { return float4(1, 0, 0, 1); } Technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_1_1 PixelShaderFunction(); } } Pixel Shader function Makes every pixel red. Define a technique combining the vertex and pixel shaders Contains a single pass Compile Vertex and Pixel shaders using Shader version 1.1

Connecting Shader to C#/XNA Four main steps in using a Shader from XNA Load the Shader via the Content manager Creates Effect variable using the loaded shader Add shader under Content directory Move .fx file in file system to Content directory On Content, right-click, then Add … Existing Item to add to project Content.Load<Effect>(@”name of effect”) Identify current technique to use effect.CurrentTechnique = effect.Techniques[“technique name from HLSL source code”] Set global variables effect.Parameters[“global variable name”].SetValue(value) Iterate through passes (techniques) in the shader

Connecting sample shader to C#/XNA Effect effect; effect = Content.Load<Effect>(@”red”); effect.CurrentTechnique = effect.Techniques[“Technique1”]; effect.Parameters[“World”].SetValue(Matrix.Identity); effect.Parameters[“View”].SetValue(camera.view); effect.Parameters[“Projection”].SetValue(camera.projection); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture> (PrimitiveType.TriangleStrip, verts, 0, 2); pass.End(); } effect.End(); Create effect, load it via Content manager Set current technique Set global variables in HLSL code Iterate through passes inside current technique