Shaders Part II. Traditional Rendering Pipeline

Similar documents
The Effects Framework. October 31, Composed of the following components:

DirectX Programming #4. Kang, Seongtae Computer Graphics, 2009 Spring

Shaders (some slides taken from David M. course)

Meshes. Meshes what are they? How to use them? Progressive Meshes? Doron Nussbaum COMP Meshes 1. Doron Nussbaum COMP Meshes 2

Technical Report. Mesh Instancing

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

!" "!#$!"%!&% ' ( )) * ) )+( ( ))+ ))(, ) ( - +,./ )) ) ) $( -( 1), , ) ( -)) ') 5 ' +) 67 4 ) ' 8, ( ) ( 9 :! ; $) ) ; 5<4,4$6 ( =) >

DirectX 11 First Elements

Shader Programming CgFX, OpenGL 2.0. Michael Haller 2003

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

The Application Stage. The Game Loop, Resource Management and Renderer Design

Lab 3 Shadow Mapping. Giuseppe Maggiore

Evolution of GPUs Chris Seitz

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Could you make the XNA functions yourself?

Introduction to Shaders.

Shaders in Eve Online Páll Ragnar Pálsson

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Programming Graphics Hardware

Meshes. 9 th Week, To gain an understanding of the internal data organization of an ID3DXMesh object

Lecture 2. Shaders, GLSL and GPGPU

Basics of GPU-Based Programming

Advanced Rendering & Shading

Lab 1 Sample Code. Giuseppe Maggiore

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

Graphics Hardware. Instructor Stephen J. Guy

Meshes Part II. June 13, 2005

Advanced Visual Effects with Direct3D

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Lecture 15 of 41. Scene Graphs: State Videos 1: CGA Shorts, Demos

Shaders. Slide credit to Prof. Zwicker

Supplement to Lecture 22

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

Cg 2.0. Mark Kilgard

DirectX Programming #1. Hyunna Lee Computer Graphics, 2010 Spring

Sébastien Dominé, NVIDIA. CgFX

Beginning Direct3D Game Programming: 1. The History of Direct3D Graphics

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Introductory Seminar

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

1.2.3 The Graphics Hardware Pipeline

Point-Based rendering on GPU hardware. Advanced Computer Graphics 2008

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

COMP371 COMPUTER GRAPHICS

Ciril Bohak. - INTRODUCTION TO WEBGL

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Pipeline Operations. CS 4620 Lecture 14

MXwendler Fragment Shader Development Reference Version 1.0

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Rendering Objects. Need to transform all geometry then

Sign up for crits! Announcments

Rendering Subdivision Surfaces Efficiently on the GPU

CIS 536/636 Introduction to Computer Graphics. Kansas State University. CIS 536/636 Introduction to Computer Graphics

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

TentamensKod: (Ifylles av student) Tentamensdatum: Tid: 10:00 12:00. Hjälpmedel: Inga hjälpmedel

Cg Toolkit. Cg 1.3 Release Notes. December 2004

Introduction to OpenGL

GPU Memory Model. Adapted from:

Deferred Rendering Due: Wednesday November 15 at 10pm

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Ch 10: Game Event Management. Quiz # 4 Discussion

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

Shader Programming and Graphics Hardware

Real - Time Rendering. Pipeline optimization. Michal Červeňanský Juraj Starinský

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

Models and Architectures

Introduction to Computer Graphics with WebGL

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

Optimizing DirectX Graphics. Richard Huddy European Developer Relations Manager

Programmable Graphics Hardware

CHAPTER 1 Graphics Systems and Models 3

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

A Trip Down The (2011) Rasterization Pipeline

Module 13C: Using The 3D Graphics APIs OpenGL ES

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Pixel Power (P2) Eliane Kabkab System Integrator Nageswar Keetha Language Guru Eric Liu Project Manager Kaushik Viswanathan System Architect

Blending. To blend (combine) the pixels. To render semi-transparent objects. Currently Rasterizing. Previous on the Back Buffer.

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

User Guide. DU _v01f January 2004

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Rendering approaches. 1.image-oriented. 2.object-oriented. foreach pixel... 3D rendering pipeline. foreach object...

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

12.2 Programmable Graphics Hardware

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Designing a Modern GPU Interface

Content. Building Geometry Appearance Lights Model Loaders

The Graphics Pipeline

Lecture 13: OpenGL Shading Language (GLSL)

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Transcription:

Shaders Part II Doron Nussbaum Doron Nussbaum COMP 3501 - Shaders Part II 1 Traditional Rendering Pipeline Traditional pipeline (older graphics cards) restricts developer to texture and the 3-term lighting model Pixel information was interpolated to vertices Shaders Lift the restrictions Allow developers to manipulate outcome Result vertex shaders Pixel/fragment shaders Vertex Operations Transformation and Lighting Primitive Assembly Viewport Culling Clipping Pixel Operations Rasterization Pixel Colouring Texture Mapping Per pixel operation (Blending) Frame buffer operations Doron Nussbaum COMP 3501 - Shaders Part II 2

Shader Support Test for shader availability (on the graphics card) Different graphics cards may have different capabilities Result Use the fixed (traditional) graphics pipeline Use programmable graphics pipeline (shaders) Different version of shaders Doron Nussbaum COMP 3501 - Shaders Part II 3 checkshadersupport() D3DCAPS9 caps; d3ddev->getdevicecaps(&caps); // check for shader support of version 3 If (caps.vertexshaderversion < D3DVS_VERSION(3,0)) // no support for Vertex Shader version 3 // do something use fixed pipline, try version 2.0 etc. // check for pixel Shader Support If (caps.pixelshaderversion < D3DVS_VERSION(3,0)) // no support for Vertex Shader version 3 // do something use fixed pipline, try version 2.0 etc. Doron Nussbaum COMP 3501 - Shaders Part II 4

Shaders Stored in an external file Accessed at run time Independent of compiled code Compiled at run time Easy to replace and modify Send a new copy to the client Easy to upgrade/degrade the graphics presentation Executed on the graphics card Doron Nussbaum COMP 3501 - Shaders Part II 5 Shader Operation Three main steps Write the shader code in an external file Bind required shader code to game code Determine at run time which parts of the code to execute Game Code Bind Code Shader Code Doron Nussbaum COMP 3501 - Shaders Part II 6

Shader Framework Direct3D Effect The shader is encapsulated in Direct3D effect Want to determine at run time What to execute (which function(s)) Effect File When to execute Global/external Adapt the shaders tl Code Functions Performance capabilities Technique pass1 pass2 Doron Nussbaum COMP 3501 - Shaders Part II 7 Direct3D Effect - Technique Technique - The core of the effect Purpose Determines how an object is rendered Different objects different techniques More than that Use different techniques to support different HW Use different techniques to create different effects Calm water, Waves, Whirlpool Techniques contain one or more passes Each pass contains Vertex shader function Pixel shader function Effect File Lake Technique Pass1 VSFun PSFun Pass2 VSFun1 PSFun Sea Technique Doron Nussbaum COMP 3501 - Shaders Part II 8

Shader Framework Direct3D Effect Variables Global Local Functions Designated Special functions Vertex shader Pixel shader Local functions Effect File Global/external Functions Technique pass1 pass2 Doron Nussbaum COMP 3501 - Shaders Part II 9 Vertex Shader Doron Nussbaum COMP 3501 - Shaders Part II 10

// transformation matrix from the program (LAST) uniform extern float4x4 WorldViewProjMat; struct OutputVS float4 pos : POSITION0; ; // vertex shader struct OutputVS TransformFunVS(float3 pos : POSITION0) // initalize the output struct OutputVS v = 0.0,0.0,0.0,0.0; v.pos = mul(float4(pos,1.0f),worldviewprojmat); // produce output return(v); // Pixel Shader float4 TransformFunPS() : COLOR float4 colour = 1.0,1.0,1.0,1.0; return(colour); Doron Nussbaum COMP 3501 - Shaders Part II 11 // transformation matrix from the program uniform extern float4x4 WorldViewProjMat; struct OutputVS float4 pos : POSITION0; float4 col : COLOR0; ; // vertex shader struct OutputVS TransformFunVS(float3 pos : POSITION0, float4 col : COLOR0) // initalize the output struct OutputVS v = 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0; v.pos = mul(float4(pos,1.0f),worldviewprojmat); v.col = col; // produce output return(v); // Pixel Shader float4 TransformFunPS(float4 col : COLOR0) : COLOR return(col); Doron Nussbaum COMP 3501 - Shaders Part II 12

Semantic HLSL All input parameters must be marked in order to link/bind flow of data in the shaders Position Color Normal Texture coordinates struct meshvertex D3DXVECTOR3 pos; // position DWORD color; // vertex color ; // vertex shader struct OutputVS TransformFunVS(float3 pos : POSITION0, float4 col : COLOR0) Doron Nussbaum COMP 3501 - Shaders Part II 13 Semantic HLSL struct meshvertex D3DXVECTOR3 pos; // position DWORD color; // vertex color ; D3DVERTEXELEMENT9 decl[] = 0,0,D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0, 0,12,D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0, D3DDECL_END(); // vertex shader struct OutputVS TransformFunVS(float3 pos : POSITION0, float4 col : COLOR0) Doron Nussbaum COMP 3501 - Shaders Part II 14

struct OutputVS float4 pos : POSITION0; float4 col : COLOR0; float2 texsky : TEXCOORD0 float2 unusedtex : TEXCOORD1 ; Semantic HLSL // vertex shader struct OutputVS TransformFunVS(float3 pos : POSITION0, float4 col : COLOR0, float2 texsky : TEXCOORD0, float2 unusedtex : TEXCOORD1) // initalize the output struct OutputVS v = 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0; return(v); // Pixel Shader float4 TransformFunPS(float4 col : COLOR0, float2 texsky : TEXCOORD0, float2 unusedtex : TEXCOORD1) : COLOR Doron Nussbaum COMP 3501 - Shaders Part II 15 Typical VS semantic Input Description Type COLOR[n] Diffuse and specular color float4 NORMAL[n] Normal vector float4 POSITION[n] Vertex position in object space. float4 PSIZE[n] Point size float TEXCOORD[n] Texture coordinates float4 Output Description Type COLOR[n] Diffuse or specular color float4 FOG Vertex fog float POSITION[n] Position of a vertex in homogenous space. Compute position in screen-space by dividing (x,y,z) by w. Every vertex shader must write out a parameter with this semantic. float4 TEXCOORD[n] Texture coordinates float4 Doron Nussbaum COMP 3501 - Shaders Part II 16

struct OutputVS float4 pos : POSITION0; float4 col : COLOR0; ; float2 texsky float2 unusedtex : Semantic TEXCOORD0 HLSL : TEXCOORD1 // vertex shader struct OutputVS TransformFunVS(float3 pos : POSITION0, float4 col : COLOR0, float2 texsky : TEXCOORD0, float2 unusedtex : TEXCOORD1) // Pixel Shader float4 TransformFunPS(float4 col : COLOR0, float2 texsky : TEXCOORD0, float2 unusedtex : TEXCOORD1) : COLOR technique TransformationTechnique pass P1 vertexshader = compile vs_3_0 TransformFunVS(); pixelshader = compile ps_3_0 TransformFunPS(); FillMode = WireFrame; // see possible states for shader (documentation) Doron Nussbaum COMP 3501 - Shaders Part II 17 Technique Defined by technique identifier e.g., technique FirstTech technique TransformationTechnique pass pass-identifier... pass pass-identifier... pass pass-identifier... Doron Nussbaum COMP 3501 - Shaders Part II 18

Steps Preparation Create an effect Create a handle for external effect parameters Set the effect parameters Apply the effect Determine the technique Set the effect parameters Begin the effect Draw primitives End the effect Doron Nussbaum COMP 3501 - Shaders Part II 19 Preparation Steps Create an effect Create an effect interface Load the effect from the file Parse the effect Doron Nussbaum COMP 3501 - Shaders Part II 20

ID3DXEffect *myeffect = NULL; ID3DXBuffer *errbuf = NULL; Rc = D3DXCreateEffectFromFile( d3ddev, simpleshaders.fx, NULL, NULL, D3DXSHADER_DEBUG, NULL, &myeffect, &errbuf); If (rc!= D3D_OK) if (errbuf!= NULL) MessageBox(0, (char *) errbuf->getbufferpointer(), 0,0); errbuf->release(); errbug = NULL; Doron Nussbaum COMP 3501 - Shaders Part II 21 HRESULT D3DXCreateEffectFromFile( LPDIRECT3DDEVICE9 pdevice - Pointer to the device that will create the effect. LPCSTR psrcfile - Pointer to the filename. D3DXMACRO *pdefines - Optional NULL-terminated array of preprocessor macro definitions. LPD3DXINCLUDE pinclude Optional interface pointer use NULL long Flags optional flags for compiling the shaders. Use 0 for no flags important flags: D3DXSHADER_DEBUG, LPD3DXEFFECTPOOL ppool - Pointer to a ID3DXEffectPool object to used for shared parameters across a number of effects files. Use NULL (no parameters are shared) ID3DXEffect *ppeffect - returns a pointer to a compiled effect interface ID3DXEffect. ID3DXBuffer *ppcompilationerrors Returns a pointer to a buffer containing a listing of compile errors Return Values Success - D3D_OK. Failure - D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA. E_OUTOFMEMORY Doron Nussbaum COMP 3501 - Shaders Part II 22

Preparation Steps Create a handle for external effect parameters Bind the name from the file with a handle in the game program Set the effect parameters Links a specific program variable with a parameter in the shader // declare the handle D3DXHANDLE ID3DXXEffect::GetParameterByName( D3DXHANDLE parent, LPCSTR nnull, ) // scope of variable parent structure // name of variable in the fx file HRESULT SetMatrix( D3DXHANDLE hparameter // handle identifier D3DXATRIX* pmatrix // Pointer to a nontransposed matrix ) Doron Nussbaum COMP 3501 - Shaders Part II 23 Other Set Functions ID3DXBaseEffect::SetXXXX(D3DXHANDLE hparamater, XXXX var) XXXX BOOL, float, int, ID3DXBaseEffect::SetXXXX(D3DXHANDLE hparamater, XXXX *var) XXXX vector (4D), D3DXMATRIX ID3DXBaseEffect::SetXXXXArray(D3DXHANDLE hparamater, XXXX *var, UINT count) XXXX BOOL, float, int, vector (4D), D3DXMARTIX // Set the value of an arbitrary parameter or annotation, including simple types, structs, arrays, strings, shaders and textures. ID3DXBaseEffect::SetValue(D3DXHANDLE hparameter, void *buf, unsigned int bufsize)

Preparation Steps (last) C++ Code D3DXMASTRIX combinedmat ; // declare the handle D3DXHANDLE *hworldviewprojmat = NULL; // Bind the handle to a name the name is declared in the.fx file hworldviewprojmat = myeffect->getparameterbyname(null, WorldViewProjMat );... // set the matrix in the code myeffect->setmatrix(hworldviewprojmat, &combinedmat); FX File uniform extern float4x4 WorldViewProjMat;.. Doron Nussbaum COMP 3501 - Shaders Part II 25 Steps Preparation Create an effect Create an effect interface Load the effect from the file Parse the effect Create a handle for external effect parameters Bind the name from the file with a handle in the game program Set the effect parameters Links a specific program variable with a parameter in the shader Doron Nussbaum COMP 3501 - Shaders Part II 26

Steps Apply the effect Obtain a handle to the technique D3DXHANDLE ht = myeffect->gettechnicquebyname( firsttech ); Activate the desired technique myeffect->settechnique(ht); Set the effect parameters Begin the effect For each pass in the technique Draw primitives End the effect Doron Nussbaum COMP 3501 - Shaders Part II 27 Steps Apply the effect Obtain a handle to the technique D3DXHANDLE ht = myeffect->gettechnicquebyname( firsttech ); Activate the desired technique Set the effect parameters Begin the effect For each pass in the technique Draw primitives End the effect Doron Nussbaum COMP 3501 - Shaders Part II 28

// set the technique rc = pmesheffect->settechnique(htechnique); // set the global matrix pmesheffect->setmatrix(hworldviewprojmat, &(worldmat*viewmat*projmat)); // set the time ftime = time; pmesheffect->setfloat(htime, ftime); // start the effect pmesheffect->begin(&numpasses, 0); // 0 means save & restore device states // start the technique passess for (i = 0; i < numpasses; i++) pmesheffect->beginpass(i); // draw the mesh md3ddev->drawindexedprimitive(d3dpt_trianglelist, 0, 0, this->numvtx, 0, this->numtriangles); pmesheffect->endpass(); pmesheffect->end(); Steps Apply the effect Obtain a handle to the technique Activate the desired technique Set the effect parameters Begin the effect For each pass in the technique Draw primitives End the effect ID3DXEffect::Begin sets the active technique. ID3DXEffect::BeginPass sets the active pass. ID3DXEffect::CommitChanges updates changes to any set calls in the pass. This should be called before any draw call. ID3DXEffect::EndPass ends a pass. ID3DXEffect::End ends the active technique. Doron Nussbaum COMP 3501 - Shaders Part II 30