Top Banner
The GPU Pipeline The GPU Pipeline Programmable Hardware Programmable Hardware 1
21

The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon...

Mar 18, 2018

Download

Documents

duonglien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

The GPU Pipeline The GPU Pipeline

Programmable HardwareProgrammable Hardware

1

Page 2: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

The GPU

� Graphics Processing Unit

� Designed to produce real-time graphics and effects

� GPUs today are the strongest parallel processor

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 2

Page 3: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

NVIDIA 580GTX

�State-of-the-art GPU

�Characteristics:

Processor cores: 512(!)

Computation speed : 1581 GFLOPS

3.33 GHz Intel 6-Core – 100 GFLOPS peak3.33 GHz Intel 6-Core – 100 GFLOPS peak

Texture Fill Rate: 50 Billion Pixel/Sec

�OpenGL Version: 4.1

�DX11

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 3

Page 4: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

GPU Pipeline

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 4

Page 5: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Programmable Hardware

� Originally GPU had only fixed operations

E.g.: Support OpenGL 1.2 API functionality

� Modern GPUs allow modifications

Flexibility (programmable vs. fixed)

Fast implementation of complex algorithms

GP-GPU (GPU general computing) GP-GPU (GPU general computing)

OpenCL / CUDA

� Programmable parts can run programs

(Shaders):

Vertex

Fragment (Pixel)

Geometry (introduced few years ago)

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 5

Page 6: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Programmable Pipeline

( ( x, y, z, w )x, y, z, w )

( ( nxnx, , nyny, , nznz ))

( ( s,s, t, r, q )t, r, q )

( ( r,r, g, b, a )g, b, a )

VERTEX VERTEX

SHADERSHADER

( ( x’, y’, z’, w’ )x’, y’, z’, w’ )

( ( nxnx’, ’, nyny’, ’, nznz’ )’ )

( ( s’,s’, t’, r’, q’ )t’, r’, q’ )

( ( r’,r’, g’, b’, a’ )g’, b’, a’ )

( ( x, y )x, y )

( ( r’,r’, g’, b’, a’ )g’, b’, a’ )

( ( depth’ )depth’ )

( ( x, y )x, y )

( ( r,r, g, b, a )g, b, a )

( ( depth )depth )

FRAGMENTFRAGMENT

SHADERSHADER

Page 7: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

The Vertex Processor

� Responsible for the following operations:

Vertex transformation

Normal transformation and normalization

Texture Coordinates Generation and transformation

Lighting (per vertex)

Color & Material (per vertex)Color & Material (per vertex)

� Shader replaces ALL functionality of the fixed processor

When shader is active the fixed processing is disabled

Shader must support all the above in its code

� Vertex Shaders runs in parallel (almost) on all vertices

No vertex information can affect another vertex.

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 7

Page 8: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

The Fragment Processor

� Operates on the fragments AFTER the interpolation

and rasterization of vertex data.

� Allows the following programming:

Operation on interpolated vertex values (phong, anyone?)

Texture access & application

FogFog

Color sum etc.

� All fragment shaders work in parallel

Again, no access to neighboring fragments.

Does not replace back-end operations

alpha blending, pixel ownership tests, masking and such.

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 8

Page 9: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Shading Languages

�Low level languages - a GPU assemblerARB (OpenGL), PTX (Cuda)

�High level languages – C-like

Cg (NVidia)

GLSL (OpenGL) – we will use this oneGLSL (OpenGL) – we will use this one

HLSL (DirectX)

�OpenGL and DirectX support API forLoading shaders

Feeding them with user-defined data

Deleting them.

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 9

Page 10: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

GLSL

� A high-level procedural languages (similar to C++).

� As of OpenGL 2.0, it is a standard, with a simple

glXYZ API from OpenGL applications

Former implementations of GLSL used OpenGL extensions

� The same language used for both types of shaders

There are some small differences

� Native to 3D graphics usage

Supports types for position, color, matrix, etcE

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 10

Page 11: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Initial Example – RGB Cube

� Vertex Shader:varying float xpos;

varying float ypos;

varying float zpos;

void main(void)

{

xpos = clamp(gl_Vertex.x,0.0,1.0); xpos = clamp(gl_Vertex.x,0.0,1.0);

ypos = clamp(gl_Vertex.y,0.0,1.0);

zpos = clamp(gl_Vertex.z,0.0,1.0);

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 11

From http://www.clockworkcoders.com/oglslFrom http://www.clockworkcoders.com/oglsl

Page 12: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

RGB Cube – Cont’d

� Fragment Shader:

varying float xpos;

varying float ypos;

varying float zpos; varying float zpos;

void main (void) {

gl_FragColor = vec4 (xpos, ypos, zpos, 1.0);

}

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 12

Page 13: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

GLSL Definitions

� Data Types:

Scalar – float, int, bool

Vectors – (i|b|nothing)vec2|3|4

Matrices – (i|b|nothing)mat2|3|4

� Texture (sample) types:

Regular texture – Sampler1/2/3DRegular texture – Sampler1/2/3D

Texture with depth – Sampler1|2|3DShadow

� Structures, main() function, arrays - as in C

NO implicit type casting, however!

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 13

Page 14: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Qualifiers and Attributes

� Serve as I/O for a shader from the application and

between the shaders

Attribute – For frequently changing information, from the

application to a vertex shader

Uniform - For infrequently changing information, from the

application to either shader.application to either shader.

Varying – For interpolated information passed on from the

vertex shader to the fragment shader.

Const – Compile-time constants (as in C).

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 14

Page 15: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Inside a Vertex Shader

� Shader input

Supplied by the user through the glVertex() or similar

gl_Vertex – vertex position

gl_Normal – vertex normal

gl_TexCoord – vertex texture mapping coordinates

EE

gl_ModelViewMatrix - current model view matrix

gl_ProjectionMatrix - current projection matrix

gl_LightSource[2].spotDirection

E

� Must output gl_Position (the position after

transformation, in camera space coordinates).

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 15

Page 16: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Writing A Fragment Shader

� Given data from the application and the Vertex

shader:

gl_Color – interpolated color

gl_TexCoord

� Output the following variables

gl_FragColorgl_FragColor

gl_FragDepth – can be overridden. Default is the fragment’s

depth.

Gl_FragData – can be used for other buffers, to be accessed

by the program with glGet() functions.

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 16

Page 17: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

OpenGL shader API

� In order for a shader to be programmed unto the

hardware, the following steps must be taken

Create a shader object with glCreateShader()

Provide source code of shader (a text, in sets of strings) with

glShaderSource()

Compile (just like in C!) with glCompileShader()Compile (just like in C!) with glCompileShader()

Create program object with glCreateProgram()

Attach shader object to program with glAttachShader()

Link (again, like in C) program with glLinkProgram()

Install program to OpenGL with glUseProgram()

Passing Attributes through glUniform(), glVertexAttrib()

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 17

Page 18: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

GLSL Links and Tools

� GLSL Tutorials

http://www.clockworkcoders.com/oglsl/tutorials.html

� GLSL shader tools

AMD rendermonkey (end of life, but does the work)

http://developer.amd.com/archive/gpu/rendermonkey/pages/def

ault.aspxault.aspx

ShaderDesigner – quite old shader designer (WinXP only!)

See link in HW assignment section

GLSLDevil - GLSL debugger (very unstable)

http://www.vis.uni-stuttgart.de/glsldevil

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 18

Page 19: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Examples

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 19

ToonToon TeapotTeapot

Glass KleinGlass Klein--BottleBottle

Page 20: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

More Examples

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion 20

http://www.bencloward.com/images/shaders_offest_comhttp://www.bencloward.com/images/shaders_offest_com

plete.jpgplete.jpg

Page 21: The GPU Pipeline - TechnionGPU Pipeline OpenGL OpenGL -- Center for Graphics and Geometric Computin Center for Graphics and Geometric Computing, Technig, Technionon 4webcourse.cs.technion.ac.il/.../ho/WCFiles/Pipeline-bw.pdf ·

Yet More Examples

OpenGL OpenGL -- Center for Graphics and Geometric Computing, TechnionCenter for Graphics and Geometric Computing, Technion21

Ray Tracing, Courtesy Ray Tracing, Courtesy : Martin Christen University of Basel: Martin Christen University of Basel

Geometric TexturesGeometric Textures

DeformationsDeformations