Light Hearted Look At GPU Programming And Linear Algebra Operators.

Presentation on theme: "Light Hearted Look At GPU Programming And Linear Algebra Operators."— Presentation transcript:

Light Hearted Look At GPU Programming And Linear Algebra Operators

Pass Out (3) Papers And GPUGems Book Book Has Some Nice Graphics

Demos

Pyramids Demo Blackbox

Codecreatures Demo Benchmarks!

What Are The Differences Between These Demos?

Food For Thought Science/Education Versus Entertainment? “Edutainment”? Is “User Friendliness” The Same? To Me, The Codecreatures Demo Was State Of The Art! Probably To Some Video Gamers, It’s Old Hat! I Assume This Code Is Really Taking Advantage Of The GPU

“The Continuum” Assembly Programming  “GPU Programming”  Monitor Programming  4GL, 5GL, …? User Friendliness small medium large What, Exactly, Is Happening On The GPU? Probably Don’t Know Exactly SDKS e.g. Visual Studio

My Continuum GPU Knowledge small medium large Me Prof Bailey (and Prof Zhang and Jacob)

;-) :-)

What’s On the GPU, What’s on the CPU? “Don’t Ask Me!”

Is the GPU Synonymous With the Graphics Card? “Yes, close enough. The GPU is actually the processor chip that lives on the graphics card. The "graphics card" has a bunch of other stuff on it to support the GPU and its operations, plus memory, bus interaction, etc.” Mike Bailey

Gonna Wear This One Out!

GPU/ Graphics Card ?

CPU, Bus, VP, FP, bunch of other stuff

Benchmarking (CPU) (BUS)

Benchmarking (VP) (FP) Science? Entertainment?

Benchmarking glFinish(); int t0 = glutGet( GLUT_ELAPSED_TIME ); > glFinish(); int t1 = glutGet( GLUT_ELAPSED_TIME ); glutSwapBuffers(); fprintf( stderr, "One display frame took %d milliseconds\n", t1 - t0 );

What’s On the GPU, What’s On the CPU? How Well Do We Know The Hardware/Software Boundary?

// minimal vertex shader // www.lighthouse3d.com void main() { // the following three lines provide the same result //gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_Position = ftransform(); } The end result is of course the same. Does this guarantee the same transformation as in the fixed functionality? Well in theory yes, but in practice the process of transforming the vertices may not follow the same order as in here. This is normally a highly optimized task in a graphic card, and a special function is provided to take advantage of that optimization. Another reason for this function is due to the limit in the precision of the float data type. When calculus is done in different orders, different results may be obtained due to this limited precision. Hence the GLSL provides a function that guarantees that not only the best performance is obtained but also that the result is always the same as when using the fixed functionality. This magical function is: vec4 ftransform(void); This function returns the transformed incoming vertex, following the same steps as the fixed functionality does. http://www.lighthouse3d.com/opengl/glsl/index.php?minimal Jumping Ahead: Specific Code, But Takeaway Is Some Uncertainty In Implementation Simplest Shader Program You Will Ever See!

GPU Overview

What is the GPU? “Graphical Processing Unit”

How We Gonna Dive In?

GLSL - (Relatively) User Friendly GLman - User Friendly Good Demos Available (e.g. Pyramids) GLSL/GLman Orientation (Simple, Less Powerful, Less User Friendly Visual Studio Demos/Prototypes Do Exist And Will Be Overviewed

GLSL/GLman Orientation

“GLman is written in OpenGL. It is meant to be a scripting system to make it easy to explore GPU programming, so internally it takes care of a lot of the boilerplate of writing GPU-based programs, leaving you just to write the GPU code. So, the.glib files are the script, and the.vert and.frag files are the vertex and fragment GPU code.” Mike Bailey http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/Glman/glman.pdf

Glman Lets You Concentrate On: Vertex Processor -.vert file // text file Fragment Processor -.frag file // text file GLSL/GLman Orientation

Glman makes this part easy

GLSL/GLman Orientation Simple/ Standalone Visual Studio Demo Apps Available, And Will Be Looked At

Why Use the GPU?

“The rasterizer is a part of the GPU, but its functionality can't be customized by the user, other than sending user-defined variables through it to be interpolated.” Mike Bailey

Rasterizer In The “Bunch Of Other Stuff” Category ?

GPU versus CPU “The distinction is becoming more and more blurred, but the basic difference is that GPUs have certain architectural changes that appeal more to graphics, such as more cores, less cache, and special texture fetching hardware.” Mike Bailey Can’t Do Recursion On The GPU!!!

GPU versus CPU Can’t Do Recursion On The GPU!!! CPU – double oriented GPU – float oriented

Multi Cores  Parallelism Less Cache  Streaming Texture (Local) Memory  Reduced Bus Activity

GPU versus CPU

GPU versus CPU

We’ll take a quick look at these (mostly linear operator) terms shortly

http://ieeexplore.ieee.org/iel5/4221378/4221379/04221459.pdf ?tp=&isnumber=&arnumber=4221459 [Gong, Langille, Gong]

GPU Typical Programming Environment GLSL - OpenGL Shading Language HLSL - DirectX's high-level shading language Cg – C For Graphics …

Vertex Shader Program(ming) Fragment Shader Program(ming) each with a main() examples shortly

Specific Linear Operators Matrices for Multiplication Matrices for Differentiation – e.g. Sobel Filter

Specific Linear Operators vec2 texcoord1, texcoord2; vec3 position; vec4 myRGBA; ivec2 textureLookup; bvec3 less; Data Types

Specific Linear Operators mat2 mat2D; mat3 optMatrix; mat4 view, projection; mat4x4 view; // an alternate way of declaring a mat4 mat3x2 m; // a matrix with 3 columns and 2 rows Data Types

Specific Linear Operators vec3(float) // initializes each component of with the float vec4(ivec4) // makes a vec4 with component-wise conversion vec2(float, float) // initializes a vec2 with 2 floats ivec3(int, int, int) // initializes an ivec3 with 3 ints bvec4(int, int, float, float) // uses 4 Boolean conversions vec2(vec3) // drops the third component of a vec3 vec3(vec4) // drops the fourth component of a vec4 vec3(vec2, float) // vec3.x = vec2.x, vec3.y = vec2.y, vec3.z = float vec3(float, vec2) // vec3.x = float, vec3.y = vec2.x, vec3.z = vec2.y vec4(vec3, float) // We’’ll See An Application Of One Of These vec4(float, vec3) // Or Something Similar vec4(vec2, vec2) Constructors

Specific Linear Operators http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf

Specific Linear Operators Testing/ Benchmarking

Specific Linear Operators Testing/ Benchmarking

Specific Linear Operators Non Linear Testing/ Benchmarking

Specific Linear Operators SWIZZLING!

Specific Linear Operators // different denominator forms

Specific Linear Operators

http://wwwcg.in.tum.de/Research/data/Publications/sig03.pdf [Kruger,Westermann] Specific Linear Operators

Basic Architecture View Input Is (C/C++ Program),.vert file,.frag file (,.glib file) Output Is Image(s)

More Complex Architecture Views

Another Pipeline Overview MC – Model Coordinates SC – Screen Coordinates Complete Colored Screen Coordinates

Another Pipeline Overview “Programmable Unit”

Over Simplified?

MC – Model Coordinates SC – Screen Coordinates Colored Screen Coordinates

Snazzy Examples

Getting Started GLSL/GLman

Demos Start -> Programs -> Shaders -> Glman http://cs.oregonstate.edu/~mjb/cs519 has multiple sets of input files (.glib,.vert, and.frag) (In e.g. CGEL)

Pyramids - pyramids.glib - pyramids.vert - pyramids.frag

GLSL/Glman Specialization

##OpenGL GLIB Perspective 70 Vertex pyramids.vert Fragment pyramids.frag Program BumpMapTest\ LightX \ LightY \ LightZ \ SurfaceColor {0.7 0.8 0.1 1.}\ Ang \ BumpDensity \ Ambient \ Height Sphere 1 200 200 #Obj cow.obj #Teapot pyramids.glib

##OpenGL GLIB Perspective 70 Vertex pyramids.vert Fragment pyramids.frag Program BumpMapTest\ LightX \ LightY \ LightZ \ SurfaceColor {0.7 0.8 0.1 1.}\ Ang \ BumpDensity \ Ambient \ Height Sphere 1 200 200 #Obj cow.obj #Teapot “one to one” Pyramids.glib vert file frag file “one to one”

Getting Started http://www.lighthouse3d.com/opengl/glsl/index.php?minimal http://www.lighthouse3d.com/opengl/glsl/examples/glutglsl5_2.0.zip has VS Project (glutglsl) that compiles (e.g. in CGEL) (needs glew32.lib which can be copied from the local hard drive) Another Approach

// minimal vertex shader // www.lighthouse3d.com void main() { // the following three lines provide the same result //gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_Position = ftransform(); } The end result is of course the same. Does this guarantee the same transformation as in the fixed functionality? Well in theory yes, but in practice the process of transforming the vertices may not follow the same order as in here. This is normally a highly optimized task in a graphic card, and a special function is provided to take advantage of that optimization. Another reason for this function is due to the limit in the precision of the float data type. When calculus is done in different orders, different results may be obtained due to this limited precision. Hence the GLSL provides a function that guarantees that not only the best performance is obtained but also that the result is always the same as when using the fixed functionality. This magical function is: vec4 ftransform(void); This function returns the transformed incoming vertex, following the same steps as the fixed functionality does.

// minimal fragment shader // www.lighthouse3d.com void main() { gl_FragColor = vec4(0.4,0.4,0.8,1.0); What Color Is This? } // minimal vertex shader // www.lighthouse3d.com void main() { // the following three lines provide the same result //gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_Position = ftransform(); }

CTRL-ALT-PRINT_SCREEN PASTE INTO PAINT SAVE AS 24 BIT BMP RIGHT CLICK FILE OPEN WITH PHOTOSHOP TEARDROPPER TOOL DOUBLE CLICK 102 =.4 * 255 204 =.8 * 255

// minimal fragment shader // www.lighthouse3d.com void main() { gl_FragColor = gl_Color ; // varying vec4 gl_Color; } // minimal vector shader // www.lighthouse3d.com void main() { gl_FrontColor = gl_Color; // attribute vec4 gl_Color; gl_Position = ftransform(); }

// “near” minimal vertex shader // www.lighthouse3d.com varying vec3 lightDir,normal; void main() { lightDir = normalize(vec3(gl_LightSource[0].position)); normal = gl_NormalMatrix * gl_Normal; gl_Position = ftransform(); } // “near” minimal fragment shader // www.lighthouse3d.com varying vec3 lightDir,normal; void main() { float intensity; vec4 color; // normalizing the lights position to be on the safe side vec3 l = normalize(vec3(gl_LightSource[0].position)); vec3 n = normalize(normal); intensity = dot(l,n); if (intensity > 0.95) color = vec4(1.0,0.5,0.5,1.0); else if (intensity > 0.5) color = vec4(0.6,0.3,0.3,1.0); else if (intensity > 0.25) color = vec4(0.4,0.2,0.2,1.0); else color = vec4(0.2,0.1,0.1,1.0); gl_FragColor = color; }

Real Utility? Exploiting (Living With) Existing Architecture/ Hardware That’s Beyond Programmatic Control // Special Constructor

Context

Exploiting (Living With) Existing Architecture/ Hardware That’s Beyond Programmatic Control

Context // We Saw An // Example

Context

Miscellaneous “Programmable Unit”

Miscellaneous “Programmable Unit”

Did You See All 3 Papers And The Book?

Questions?

GLUTGLSL Demo z:\Windows.Documents\Desktop\colorglut_2.0\Debug>glutglsl

Download ppt "Light Hearted Look At GPU Programming And Linear Algebra Operators."

Similar presentations