Vertex and fragment programs



Similar documents
CSE 564: Visualization. GPU Programming (First Steps) GPU Generations. Klaus Mueller. Computer Science Department Stony Brook University

Image Processing and Computer Graphics. Rendering Pipeline. Matthias Teschner. Computer Science Department University of Freiburg

Introduction to GPGPU. Tiziano Diamanti

Shader Model 3.0. Ashu Rege. NVIDIA Developer Technology Group

Monash University Clayton s School of Information Technology CSE3313 Computer Graphics Sample Exam Questions 2007

OpenGL Shading Language Course. Chapter 5 Appendix. By Jacobo Rodriguez Villar jacobo.rodriguez@typhoonlabs.com

Introduction to Computer Graphics

CS418 GLSL Shader Programming Tutorial (I) Shader Introduction. Presented by : Wei-Wen Feng 3/12/2008

A Crash Course on Programmable Graphics Hardware

Computer Graphics Hardware An Overview

2: Introducing image synthesis. Some orientation how did we get here? Graphics system architecture Overview of OpenGL / GLU / GLUT

Introduction to Computer Graphics with WebGL

GPU(Graphics Processing Unit) with a Focus on Nvidia GeForce 6 Series. By: Binesh Tuladhar Clay Smith

Computer Graphics. Anders Hast

Recent Advances and Future Trends in Graphics Hardware. Michael Doggett Architect November 23, 2005

INTRODUCTION TO RENDERING TECHNIQUES

Overview Motivation and applications Challenges. Dynamic Volume Computation and Visualization on the GPU. GPU feature requests Conclusions

GPU Architecture. Michael Doggett ATI

How To Teach Computer Graphics

Optimizing AAA Games for Mobile Platforms

Realtime 3D Computer Graphics Virtual Reality

Writing Applications for the GPU Using the RapidMind Development Platform

QCD as a Video Game?

OpenGL Performance Tuning

The Evolution of Computer Graphics. SVP, Content & Technology, NVIDIA

Image Synthesis. Transparency. computer graphics & visualization

OpenGL & Delphi. Max Kleiner. 1/22

NVPRO-PIPELINE A RESEARCH RENDERING PIPELINE MARKUS TAVENRATH MATAVENRATH@NVIDIA.COM SENIOR DEVELOPER TECHNOLOGY ENGINEER, NVIDIA

Computer Applications in Textile Engineering. Computer Applications in Textile Engineering

LLVM for OpenGL and other stuff. Chris Lattner Apple Computer

Data Visualization Using Hardware Accelerated Spline Interpolation

Lecture Notes, CEng 477

Introduction GPU Hardware GPU Computing Today GPU Computing Example Outlook Summary. GPU Computing. Numerical Simulation - from Models to Software

GPGPU Computing. Yong Cao

Computer Graphics on Mobile Devices VL SS ECTS

GPUs Under the Hood. Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology

Introduction to Computer Graphics

GUI GRAPHICS AND USER INTERFACES. Welcome to GUI! Mechanics. Mihail Gaianu 26/02/2014 1

Graphics Cards and Graphics Processing Units. Ben Johnstone Russ Martin November 15, 2011

A Short Introduction to Computer Graphics

Hardware design for ray tracing

Programmable Graphics Hardware

A Pipeline From COLLADA to WebGL for Skeletal Animation

Radeon HD 2900 and Geometry Generation. Michael Doggett

Silverlight for Windows Embedded Graphics and Rendering Pipeline 1

Optimizing Unity Games for Mobile Platforms. Angelo Theodorou Software Engineer Unite 2013, 28 th -30 th August

CUBE-MAP DATA STRUCTURE FOR INTERACTIVE GLOBAL ILLUMINATION COMPUTATION IN DYNAMIC DIFFUSE ENVIRONMENTS

COMP175: Computer Graphics. Lecture 1 Introduction and Display Technologies

Computer Graphics CS 543 Lecture 12 (Part 1) Curves. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Low power GPUs a view from the industry. Edvard Sørgård

Performance Optimization and Debug Tools for mobile games with PlayCanvas

Dynamic Resolution Rendering

Tutorial 9: Skeletal Animation

Android and OpenGL. Android Smartphone Programming. Matthias Keil. University of Freiburg

Introduction to Computer Graphics. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

GPU Christmas Tree Rendering. Evan Hart

Animation (-4, -2, 0 ) + (( 2, 6, -4 ) - (-4, -2, 0 ))*.75 = (-4, -2, 0 ) + ( 6, 8, -4)*.75 = (.5, 4, -3 ).

B2.53-R3: COMPUTER GRAPHICS. NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions.

Shadows. Shadows. Thanks to: Frédo Durand and Seth Teller MIT. Realism Depth cue

Interactive Level-Set Deformation On the GPU

Web Based 3D Visualization for COMSOL Multiphysics

GPU Shading and Rendering: Introduction & Graphics Hardware

Interactive Computer Graphics

Introduction to GPU Programming Languages

3D Graphics and Cameras

Volume visualization I Elvins

Aston University. School of Engineering & Applied Science

Learning Modern 3D Graphics Programming. Jason L. McKesson

The OpenGL R Graphics System: A Specification (Version 3.3 (Core Profile) - March 11, 2010)

Volume Rendering on Mobile Devices. Mika Pesonen

UNIVERSITY OF OSLO. Faculty of Mathematics and Natural Sciences

Procedural Shaders: A Feature Animation Perspective

RoboCup Advanced 3D Monitor

NVFX : A NEW SCENE AND MATERIAL EFFECT FRAMEWORK FOR OPENGL AND DIRECTX. TRISTAN LORACH Senior Devtech Engineer SIGGRAPH 2013

Midgard GPU Architecture. October 2014

DATA VISUALIZATION OF THE GRAPHICS PIPELINE: TRACKING STATE WITH THE STATEVIEWER

L20: GPU Architecture and Models

Programming 3D Applications with HTML5 and WebGL

Real-Time Realistic Rendering. Michael Doggett Docent Department of Computer Science Lund university

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Advanced Computer Science. Friday 18 th January 2008.

Touchstone -A Fresh Approach to Multimedia for the PC

Optimization for DirectX9 Graphics. Ashu Rege

Color correction in 3D environments Nicholas Blackhawk

We can display an object on a monitor screen in three different computer-model forms: Wireframe model Surface Model Solid model

Methodology for Lecture. Review of Last Demo

Advanced Rendering for Engineering & Styling

Graphical displays are generally of two types: vector displays and raster displays. Vector displays

How To Use An Amd Graphics Card In Greece And (Amd) With Greege (Greege) With An Amd Greeper 2.2.

Introduction to Game Programming. Steven Osman

3D Math Overview and 3D Graphics Foundations

How To Understand The Power Of Unity 3D (Pro) And The Power Behind It (Pro/Pro)

Using Photorealistic RenderMan for High-Quality Direct Volume Rendering

Data Parallel Computing on Graphics Hardware. Ian Buck Stanford University

Game Development in Android Disgruntled Rats LLC. Sean Godinez Brian Morgan Michael Boldischar

Transcription:

Vertex and fragment programs Jon Hjelmervik email: jonmi@ifi.uio.no 1

Fixed function transform and lighting Each vertex is treated separately Affine transformation transforms the vertex by matrix multiplication Lighting Determines the color of each vertex. Calculated using normal vector and light direction/position. Can be manipulated by light parameters, light model, material properties and position Texture parameter(s) are transformed using texture matrices. 2

Advanced vertex transformations Real time applications store vertex data on graphics memory, therefore all vertex transformations must be done at the graphics processor (GPU). Animations of a human body need to modify vertices in a non linear way depending on bones or control points. Morphing is used for animation, and uses a convex combination of two or more objects to create intermediate objects. Extending the fixed function API to handle all such applications would lead to a too messy interface. 3

Morphing at the CPU, using C++ Assuming we use a vector library, morphing could be written as: vec4 morphposition(vec4 vertexpos1, float weight1, vec4 vertexpos2, float weight2) { vec4 Pos=weight1*vertexPos1; Pos+=weight2*vertexPos2; return Pos; } 4

Morphing and lighting using opengl shading language attribute vec4 vertexpos1; attribute vec4 vertexpos2; uniform float weight1; uniform float weight2; void main(void) { vec4 Pos=weight1*vertexPos1; Pos+=weight2*vertexPos2; // Morph position vec3 Norm=weight1*vertexNorm1; Norm+=weight2*vertexNorm2; // Morph normals Norm=normalize(Norm); //normalize the morphed normal vec4 ecposition = gl_modelviewmatrix * Pos; // Transform position to eyespace vec3 tnorm = gl_normalmatrix * Norm; // Transform normal vec3 lightvec = normalize(gl_lightsource[0].position.xyz - vec3(ecposition)); // calculate vector from light to vertex in eye space gl_frontcolor.rgb=dot(tnorm,lightvec); // calculate color gl_position = gl_modelviewprojectionmatrix * Pos; // Transform position to window space } 5

Vertex shaders in OpenGL shading language OpenGL shading language is a C-like programming for defining vertex shaders and fragment shaders. Vertex shaders takes two types of input Uniform variables are variables that are constant for the entire triangle. Examples: modelview matrix and light position. Uniform variables cannot be set between glbegin and glend. Attribute variables that differs from vertex to vertex. Examples: position, normal and texture coordinate. Vertex shaders must return vertex position transformed to window coordinates (ready to be projected). 6

Vertex shaders in OpenGL shading language (2) A vertex shader acts on one vertex at the time and is responsible for the T&L part of the rendering pipeline, this includes: Transforming the vertex into window space Transforming the normal, and normalization Lighting and calculating the color of the vertex Generating/transforming texture coordinates 7

Matrix and vector data types The GPU is a vector processor, which uses vectors of length 4. vectors: vec2, vec3 and vec4 are two, three and four component vectors respectively. The name of the components are given by one letter Three naming conventions can be used {x,y,z,w} {r,g,b,a} {s,t,p,q} where x, r and s is the first component in a vector. Swissling vec4 pos = vec4(1.0, 2.0, 3.0, 4.0); vec4 swiz = pos.wzyx; // swiz = (4.0, 3.0, 2.0, 1.0) vec4 dup = pos.rrgg; // dup = (1.0, 1.0, 2.0, 2.0) pos.yx = vec2(1.0, 0.0); // pos = (0.0, 1.0, 3.0, 4.0) vec3 tmp = pos.xrs; // not valid mat2, mat3 and mat4 are 2x2, 3x3 and 4x4 matrices respectively. 8

Commonly used built-in uniform variables Built-in variables are set by standard opengl calls. uniform mat4 gl_modelniewmatrix; uniform mat4 gl_projectionmatrix; uniform mat4 gl_modelviewprojectionmatrix; uniform mat4 gl_normalmatrix; uniform gllightsourceparameters gl_lightsource[gl_maxlights]; // array of structs containing light parameters 9

Commonly used built-in attributes built in attributes are set by standard opengl calls, such as glvertex() and glnormal() attribute vec4 gl_color; // The color of the vetex attribute vec4 gl_normal; // Vertex normal attribute vec4 gl_vertex; // Vertex position attribute gl_multitexcoord0; // texture coordinate 10

Vertex shaders can not Any operation that requires knowledge about neighbors Polygon clipping. Generate new vertices or primitives. Set global data. Remove geometry (culling). A GPU transforms (shades) several vertices in parallel, therefore any operation requiring that the vertices are transformed in a specific order is impossible. 11

Morphing and lighting using opengl shading language revisited attribute vec4 vertexpos1; attribute vec4 vertexpos2; uniform float weight1; uniform float weight2; void main(void) { vec4 Pos=weight1*vertexPos1; Pos+=weight2*vertexPos2; // Morph position vec3 Norm=weight1*vertexNorm1; Norm+=weight2*vertexNorm2; // Morph normals Norm=normalize(Norm); //normalize the morphed normal vec4 ecposition = gl_modelviewmatrix * Pos; // Transform position to eyespace vec3 tnorm = gl_normalmatrix * Norm; // Transform normal vec3 lightvec = normalize(gl_lightsource[0].position.xyz - vec3(ecposition)); // calculate vector from light to vertex in eye space gl_frontcolor.rgb=dot(tnorm,lightvec); // calculate color gl_position = gl_modelviewprojectionmatrix * Pos; // Transform position to window space } 12

Fragment shaders 13

Fixed function texturing Simple opengl applications does one texture lookup based on the texture coordinate, and either multiplies, adds or replaces the input color by this value. More complex methods for combining textures are available using fixed functions, but the API is complex and the functions are not flexible. 14

Per pixel lighting and advanced texturing High quality rendering of complex models we must either calculate the lighting per pixel, or use many triangles. Realistic car-paint rendering requires complex light models, and per pixel lighting and reflection calculations. Toon shading, makes the scene look like a part of a cartoon. Bump mapping uses a texture to augment the normal vector, and uses the resulting vector for lighting calculations. Realistic skin rendering requires several texture lookups per pixel and complex calculations to combine the results. 15

Phong shading/normal map example uniform sampler2d normalmap; uniform vec3 lightvect; // Directional light, light vector in object space void main(void) { vec3 normal=texture2d(normalmap, gl_multitexcoord0,xy); normal = normalize(normal); gl_fragcolor = gl_color*dot(lightvect, normal); } 16

OpenGL fragment shader A fragment shader is a programmable replacement for the texturing in fixed function pipeline. Fragment shaders takes two types of input Uniform variables are variables that are constant for the entire triangle. Examples: modelview matrix and light position. Uniform variables cannot be set between glbegin and glend. Varying variables are linearly interpolated between the vertices. Examples: color and texture coordinate. Varying variables are output from the vertices of the triangle, and therefore not accessible from the application. 17

Texture lookups in shader Both fragment shaders and vertex shaders may use texture lookups. Texture lookups require information of which texture/ texture unit to use. This information is located in samplers. sampler1d one-dimensional texture sampler2d two-dimensional texture sampler3d three-dimensional texture samplercube cube map is a special texture where a 3D vector is used for texture lookups 18

Returning information from a fragment shader A fragment shader can return the following elements discard, when a shader calls discard the fragment will not update the frame buffer. gl_fragcolor is the output color of the fragment. gl_fragdepth, the fragment shader may change the depth value of the fragment by writing to this variable. 19

Input to a fragment shader Special input variables vec4 gl_fragcoord, holds the window coordinates of the fragment. May be used to implement scissor test in a fragment shader. bool gl_fronfacing is true for front facing triangles, and false for back faceing triangles. Commonly used built in varying variables vec4 gl_color vec4 gltexcoord[gl_maxtexturecoords] 20

Phong shading/normal map revisited uniform sampler2d normalmap; uniform vec3 lightvect; // Directional light, light vector in object space void main(void) { vec3 normal=texture2d(normalmap, gl_multitexcoord0,xy); normal = normalize(normal); gl_fragcolor = gl_color*dot(lightvect, normal); } 21

Built-in functions Trigonometric functions: radians, degrees, sin, cos, tan, asin, acos, atan Exponential functions : pow, exp2, log2, sqrt, inversesqrt Regular functions : abs, sign, floor, ceil, fract, mod, min, max, clamp, mix, step, smoothstep Geometrical functions : length, distance, dot, cross, normalize, ftransform, faceforward, reflect Matrix functions, vector relation functions, texture lookup functions, fragment processing functions and noise functions. 22

Branching using GeForce 6 series There are three different types of branching, the compiler chooses the type. Compile time branching: The compiler resolves the branch. Dependent write: All possible branches are calculated, and the result of the false ones are discarded (only used for fragment shaders). True branching: In a fragment shader true branching is very expensive unless many neighboring fragments go through the same branch. 23

Numerical simulation at GPU Simulation problem must be converted to a geometric problem Pro More FLOPS per Dollar then CPU Simulation at graphics hardware allows visualization embedded in simulation Cons Less flexible than CPU Less memory than CPU Less bandwidth between system and GPU 24

Explicit schemes We have started investigating evolutionary PDEs, which can be solved using explicit schemes. When using explicit schemes the unknown(s) at each grid point is updated from its neighbors at previous time steps. Relatively simple to convert to a geometric problem. 25

Heat Equation Very simple scheme. Implemented as a 1-pass algorithm. Scheme is the same as the Gauss filter used in image processing. The PDE is given as r u = u + t xx u yy r 1-4r r and is discredited by a standard finite difference stencil to r U n+ 1 i, j ( n n n n n + r U + U + U + U U ). n = Ui, j i+ 1, j i 1, j i, j 1 i, j+ 1 4 i, j 26

Implementation of heat equation Implemented as a fragment program. Uses two off screen pixel buffers, each with the same dimensions as the area of the simulation. Toggles drawing to one buffer, while reading the other as a texture. Render a quad covering the entire viewport. Each fragment reads the color of the pixels at the same position from the previous frame, and it s neighbors. 27

Heat equation 28

Wave equation 29

Shallow water equation 30

References OpenGL Shading Language by Randy J. Rost www.opengl.org Shader Designer www.typhoonlabs.com 31

Bruk av shadere 1. Gi shader kildekode til OpenGL 2. Kompiler shader 3. Link sammen kompilerte shadere 4. Ta i bruk program Merk: Kompilator ligger i driveren til grafikkortet. 32

Shader-objekter Lage et shader-objekt: shaderid = glcreateshaderobjectarb(shadertype); shadertype: GL_VERTEX_SHADER_ARB GL_FRAGMENT_SHADER_ARB 33

Kildekode Gi shader kildekode til OpenGL: glshadersourcearb(shaderid, numstr, strings, length); length kan settes til NULL hvis strengene er null-terminert 34

Kompilering Shader objekter blir kompilert ved: glcompileshaderarb(shaderid); Setter status parameter til GL_TRUE hvis suksess. Informasjon om kompilering kan fås tak i med: glgetinfologarb(shaderid, bufferlen, strlen, buffer); 35

Program objects Et program object er en kontainer for shader objects. Program objectet utgjør shaderene som må linkes sammen ved bruk. programid = glcreateprogramobjectarb(); glattachobjectarb(programid, shaderid); gldetachobjectarb(programid, shaderid); 36

Sletting av objekter Shader objects og program objects slettes med: gldeleteobjectarb(objectid); Data for et shader object blir ikke slettet før objektet er frakoblet et program object. Data for et program object blir ikke slettet mens det er i bruk. 37

Linking Shaderene i et program object linkes med: gllinkprogram(programid); Informasjon om linkingen kan fås tak i med: glgetinfologarb(programid, bufferlen, strlen, buffer); 38

Bruke programmer For å ta i bruk et program kaller man: gluseprogramobjectarb(programid); Hvis et program object er gyldig så blir det en del av gjeldende rendering modell. For å returnere til fixed function rendering modell så kaller man gluseprogramobjectarb(0); 39

Generic attributes Sette vertex attributes glgetattriblocationarb(programhandle_, name); glvertexattrib{1234}{fv}arb(location, &attrib[0]); Sette vertex attribute pointers glgetattriblocationarb(programhandle_, name); glbindattriblocationarb(programhandle_, location, name); glvertexattribpointerarb(location, size, type, normalized, stride, pointer); Enable client state glgetattriblocationarb(programhandle_, name); glenablevertexattribarrayarb(location); 40

Uniforms Sette uniforms glgetuniformlocationarb(programhandle_, name); gluniform{1234}{if}varb(location, count, &constant[0]); gluniformmatrix{234}fvarb(location, count, transpose, matrix) 41