Light Hearted Look At GPU Programming And Linear Algebra Operators

Preview:

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Light Hearted Look At

GPU Programming And Linear Algebra Operators

Pass Out (3) PapersAnd GPUGems Book

Book Has Some Nice Graphics

Demos

Pyramids DemoBlackbox

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 ContinuumGPU Knowledge

small medium large

Me Prof Bailey (andProf Zhang andJacob)

;-) :-)

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

GonnaWear ThisOne Out!

GPU/Graphics Card?

CPU,Bus,VP,FP,bunchof other stuff

Benchmarking

(CPU)

(BUS)

Benchmarking

(VP)

(FP)

Science?

Entertainment?

Benchmarking

glFinish(); int t0 = glutGet( GLUT_ELAPSED_TIME );

<< All Graphics Calls That You Are Testing The Speed Of >>

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 TakeawayIs Some UncertaintyIn Implementation

Simplest Shader Program You Will Ever See!

GPU Overview

What is the GPU?

“Graphical Processing Unit”

HowWe 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 theboilerplate 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

GLSL/GLman Orientation

Glman makesthis part easy

GLSL/GLman Orientation

Simple/Standalone Visual Studio Demo AppsAvailable, AndWill Be LookedAt

Why Use the GPU?

“The rasterizer is a part of the GPU, but its functionality can't becustomized by the user, other than sending user-defined variablesthrough 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

http://www.springerlink.com/content/eupfj7euk0jvj98u/fulltext.pdf

[Chiang, Hsueh, Liu]

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 mat4mat3x2 m; // a matrix with 3 columns and 2 rows

Data Types

Specific Linear Operators

vec3(float) // initializes each component of with the floatvec4(ivec4) // makes a vec4 with component-wise conversionvec2(float, float) // initializes a vec2 with 2 floatsivec3(int, int, int) // initializes an ivec3 with 3 intsbvec4(int, int, float, float) // uses 4 Boolean conversionsvec2(vec3) // drops the third component of a vec3vec3(vec4) // drops the fourth component of a vec4vec3(vec2, float) // vec3.x = vec2.x, vec3.y = vec2.y, vec3.z = floatvec3(float, vec2) // vec3.x = float, vec3.y = vec2.x, vec3.z = vec2.yvec4(vec3, float) // We’’ll See An Application Of One Of Thesevec4(float, vec3) // Or Something Similarvec4(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

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

CompleteColored Screen

Coordinates

Another Pipeline Overview

“Programmable Unit”

OverSimplified?

MC – Model Coordinates

SC – Screen Coordinates

Colored Screen Coordinates

Snazzy Examples

Snazzy Examples

Snazzy Examples

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

GLSL/Glman Specialization

##OpenGL GLIB

Perspective 70

Vertex pyramids.vertFragment pyramids.fragProgram BumpMapTest \

LightX <-10. 0. 10.0> \LightY <-10. 10. 10.0> \LightZ <-10. 10. 10.0> \SurfaceColor {0.7 0.8 0.1 1.} \Ang <-3.14159 0.785398 3.14159> \BumpDensity <5. 16. 100.> \Ambient <0. 0.1 .4> \Height <-.06 .005 .06>

Sphere 1 200 200#Obj cow.obj#Teapot

pyramids.glib

##OpenGL GLIB

Perspective 70

Vertex pyramids.vertFragment pyramids.frag

Program BumpMapTest \LightX <-10. 0. 10.0> \LightY <-10. 10. 10.0> \LightZ <-10. 10. 10.0> \SurfaceColor {0.7 0.8 0.1 1.} \Ang <-3.14159 0.785398 3.14159> \BumpDensity <5. 16. 100.> \Ambient <0. 0.1 .4> \Height <-.06 .005 .06>

Sphere 1 200 200#Obj cow.obj#Teapot

“one to one”

Pyramids.glib

vert filefrag 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

void setShaders() {

char *vs = NULL,*fs = NULL,*fs2 = NULL;

v = glCreateShader(GL_VERTEX_SHADER);f = glCreateShader(GL_FRAGMENT_SHADER);f2 = glCreateShader(GL_FRAGMENT_SHADER);

vs = textFileRead("minimal.vert");fs = textFileRead("minimal.frag");

const char * vv = vs;const char * ff = fs;

glShaderSource(v, 1, &vv,NULL);glShaderSource(f, 1, &ff,NULL);

free(vs);free(fs);

glCompileShader(v);glCompileShader(f);

printShaderInfoLog(v);printShaderInfoLog(f);printShaderInfoLog(f2);

p = glCreateProgram();glAttachShader(p,v);glAttachShader(p,f);

glLinkProgram(p);printProgramInfoLog(p);

glUseProgram(p);

}

“THE CODE !!!”

(“THE SYNTAX”?)

// 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 FILEOPEN WITH PHOTOSHOP

TEARDROPPER TOOL

DOUBLE CLICK

102 = .4 * 255102 = .4 * 255204 = .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 sidevec3 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);

elsecolor = vec4(0.2,0.1,0.1,1.0);

gl_FragColor = color;}

ShaderPseudo Lighting

Real Utility?

Exploiting(Living With)ExistingArchitecture/HardwareThat’s BeyondProgrammaticControl

// Special Constructor

Context

Context

Exploiting(Living With)ExistingArchitecture/HardwareThat’s BeyondProgrammaticControl

Context

// We Saw An // Example

Context

Miscellaneous

“Programmable Unit”

Miscellaneous

“Programmable Unit”

Did You See All 3 PapersAnd The Book?

Questions?

GLUTGLSL Demo

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