introduction to gpu programming with glslw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is glsl?...

77
Introduction to GPU Programming with GLSL October, 2009 SIBGRAPI Ricardo Marroquim André Maximo

Upload: others

Post on 04-May-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

Introduction to GPU Programmingwith GLSL

October, 2009SIBGRAPI

Ricardo Marroquim

André Maximo

Page 2: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

summary

tutorial

Part I

Introduction and motivation (RM)

GPU architecture and pipeline (AM)

GLSL language :

Hello World (AM)

Basic Types (AM)

Data Flow (RM)

OpenGL – GLSL integration (AM)

Part II

Examples:

Cartoon Effect (AM)

Texture Mapping (RM)

Environment Mapping (RM)

Phong Shading (AM)

Spike Effect (AM)

GPGPU (RM)

Particle System (RM)

Wrap-up (RM)

Page 3: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

a word about parallelism

new reality

Commodity computers are now high performance machines

Computers don't increase in speed anymore, they just get wider

Parallel computing should be taught in 1st / 2nd semester of Computer Science and Engineering graduations

For example:

Merge Sort x Heap Sort

Both are O(n log n)

One parallel-friendly, one not

Students need to understand this early!

motivation

Page 4: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

introduction

what is GPU?motivation

Graphics Processing Unit

Specialized hardware for graphics

Free CPU from the burden of rendering

Used everywhere:

desktop, notebooks, mobiles, embedded system, etc.

Graphics Processing Unit

Page 5: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

introduction

why GPU programming?motivation

Fixed functionality limits the programmer

Deep changes in graphics hardware

Outstanding effects are possible

Control over the processing inside the GPU

Page 6: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

introduction

GPU historymotivation

90’s: GPUs were black boxes (fixed functionalities)

2002: basic programmability

2004: GLSL (OpenGL 2.0), vertex and fragment shader

2006: geometry shader

2010?: tesselation shader (OpenGL 3.0)

Page 7: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

motivation

motivation

what is GLSL? OpenGL Shading Language

C/C++ like language

Coding short programs called shaders to run on the GPU

Used for different graphics card functionalities

High level shader language

(orange book)

Page 8: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

motivation

motivation

why GLSL?

(open standard) (+GLSL)

Based on OpenGL

OpenGL is the foundation of professional graphics

GLSL was introduced with OpenGL 2.0 in 2004

OpenGL 2.0 is the foundation of programmable, cross-platform and professional graphics

OpenGL / OpenGL 2.0 is an open standard

Page 9: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

motivation

motivation

interesting new effects using shaders

better anti-aliasing realistic surfaceenvironment map

non-photorealistic renderingnatural phenomenarealistic materials

Page 10: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

Page 11: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

Page 12: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

Page 13: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

the GPU

High performance

High bandwidth

Arithmetic intensity

Throughput computing

Page 14: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

GPU x CPU

storage

data path

control

min( latency ) – CPU (many levels of cache)

max( throughput ) – GPU (stream programming model)

GPU CPU

Page 15: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architectureGPU x CPU – performance comparison

Page 16: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

GPU x CPU – memory bandwidth comparison

architecture

architecture

Page 17: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

OpenGL pipeline

architecture

architecture

Basic concepts

Page 18: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

GPU pipeline – GeForce 7

One-way pipeline architecture

Different number of shader processors

Page 19: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

GPU pipeline – GeForce 8

Fixed number of shader processors

Unified shading architecture

More programmability

Page 20: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

OpenGL logical pipeline

Basic graphics pipeline

[image fromlighthouse3d.com]

Page 21: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

OpenGL logical pipeline

Fixed functionalities

Programmable functionalities

Flexible memory access

Page 22: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

Vertex Shader

Vertex transformation

Once per vertex

Input attributes

Normal

Texture coordinates

Colors

...

Page 23: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

Geometry Shader

Geometry composition

Once per geometry

Input primitives

Points, lines, triangles

Lines and triangles with adjacency

Output primitives

Points, line strips or triangle strips

[0, n] primitives outputted

Page 24: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

architecture

architecture

Fragment Shader

Pre-pixel (or fragment) composition

Once per fragment

Operations on interpolated values

Vertex attributes

User-defined varying variables

Page 25: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

languageARB Assembly Language – Architecture Review Board

nVidia (+ Microsoft) Cg – C for graphics

Microsoft HLSL – High Level Shading Language

RSL – RenderMan Shading Language

...

other shading language

Page 26: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

shader structure

/** * :: Comments :: **/

:: Global definitions ::

void main(void) {

:: Function body ::

}

Page 27: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

hello world vertex shader

Page 28: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

hello world geometry shader

Page 29: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

hello world fragment shader

Page 30: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

hello world results

1

language

Page 31: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

languageBased on C Ansi with some C++ additions

Vector types

Float, integers and booleans

2-, 3- and 4- components

Vector components can be swizzled

Matrix types

Only floats

2x2, 3x3 and 4x4 sizes

Texture types

1-, 2- and 3- dimensions

Cube mapping

Shadow mapping

GLSL basic definitions

Page 32: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

GLSL basic definitions

Type qualifiers

Uniform

Attribute

Varying

Function qualifiers

In, out and in-out

constant

Page 33: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

GLSL basic definitions

Built-in functions

Sin, cos, tan

Pow, exp, sqrt

Discard keyword

Cease fragment processing

Page 34: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

GLSL basic definitions

Built-in uniform variables

Modelview and projection matrices

Texture and normal matrices

Light and material parameters

Fog and point parameters

Page 35: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

languageAdditional vector types

Unsigned integers

Additional texture types

Array of textures

Buffer textures

Additional type qualifiers

Flat varying

Non perspective varying

Centroid varying

GLSL additional definitions (Shader Model 4.0)

Page 36: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

languageAdditional built-in functions

Modulo

Bit-wise operations

Texture access

GLSL additional definitions (Shader Model 4.0)

Page 37: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

data flow

Input/Output summary of the vertex shader

Page 38: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

data flow

Input/Output summary of the geometry shader

Page 39: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

language

language

data flow

Input/Output summary of the fragment shader

Page 40: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

OpenGL – GLSL integration

language

language

Page 41: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

OpenGL – GLSL integration

language

language

Page 42: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

summary

tutorial

Part II

Examples:

Cartoon Effect (AM)

Texture Mapping (RM)

Environment Mapping (RM)

Phong Shading (AM)

Spike Effect (AM)

GPGPU (RM)

Particle System (RM)

Wrap-up (RM)

Part I

Introduction and motivation (RM)

GPU architecture and pipeline (AM)

GLSL language :

Hello World (AM)

Basic Types (AM)

Data Flow (RM)

OpenGL – GLSL integration (AM)

Page 43: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

cartoon effect

vertex shader

examples

Page 44: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

cartoon effect

fragment shader

examples

Page 45: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

cartoon effect

results

2

examples

Page 46: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

texture mapping

map image onto polygon model

increase realism

avoid detailed meshes -> gain performance

examples

Page 47: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

texture mapping

texture coordinates

examples

Page 48: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

texture mapping

vertex shader

examples

Page 49: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

texture mapping

fragment shader

examples

Page 50: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

texture mapping

results

3

examples

Page 51: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

phong shading

vertex shader

examples

Page 52: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

phong shading

fragment shader

examples

Page 53: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

phong shading

results

4

examples

Page 54: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

environment mapping

simulate reflection of ambient on an object

map photo onto sphere (use your favorite image editor)

use mapped photo as texture for look up

examples

Page 55: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

environment mapping

vertex shader

examples

Page 56: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

environment mapping

fragment shader

examples

Page 57: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

environment mapping

results

5

examples

Page 58: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

spike effect

vertex shader

examples

Page 59: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

spike effect

geometry shader I

examples

Page 60: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

spike effect

geometry shader II

examples

Page 61: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

spike effect

results

6

examples

Page 62: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

GPGPU

General Purpose Computation on Graphics Processing Unit

use the GPU as a parallel machine

auxiliary computation for graphics

non-graphics stuff

scientific computation

mask non-graphics primitives:

pixels and vertices may represent any kind of information

examples

Page 63: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

GPGPU

General Purpose Computation on Graphics Processing Unit

example: particle simulation

first pass: compute positions using basic kinematics

no rendering

second pass: display particles

rendering

examples

Page 64: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

particle simulation

first pass

basic kinematics

store information using textures

each texel -> one particle

two textures: one for position, one for velocity

do not render -> write output to texture

examples

Page 65: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamplescompute positions using basic kinematics

pnew=pcurvcur∗t

vnew=vcuracur∗t

shaders

output: newinput: current

acceleration: simulate gravity

examples

first pass

particle simulation

Page 66: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

how to access one texture position per fragment shader pass?

draw a Quad the size of the screen

one texel = one pixel = one fragment pass

examples

first pass

particle simulation

Page 67: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

setup the texture coordinate

do not transform using modelview-> keep Quad aligned with screen

examples

vertex shader

particle simulation

Page 68: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

Attention: read/write to same texture is not specified by documentation! we are using it here for the sake of simplicity and because we are sure each particle only reads/writes from/to a single texture position (no conflict)

examples

fragment shader

particle simulation

Page 69: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexampleshow to write to a texture?

usually writes to the render buffer

Framebuffer extension lets you write elsewhere

1. Create texture

2. Create framebuffer

3. Bind texture to framebuffer

4. Before rendering quad, specify draw buffer

5. Read back information from framebuffer

examples

first pass

particle simulation

Page 70: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamplesexamples

create texture

particle simulation

Page 71: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamplesexamples

bind texture to framebuffer

particle simulation

Page 72: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamplesexamples

render to texture

particle simulation

Page 73: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

read current particle position

Not always necessary to read back information: use as input for next step

examples

second pass

particle simulation

Page 74: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

extra in code: reset particles and rotate gravity vector

7

examples

results

particle simulation

Page 75: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

wrap-up

important points about shader programming:

understand:

graphics pipeline

parallel programming (streaming)

watch out:

shader too long: rethink your strategy

too many texture fetches: slow

try to keep things inside the GPU

internet: lots of examples

above all: experiment!

Page 76: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

wrap-up

so, why use shaders?

what's coming up?

OpenGL 3.2, GLSL 1.5

hull and domain shader

nVidia 300 series(?), Intel Larrabee(?)

will GPGPU languages (CUDA, OpenCL) substitute shaders?

do I really need to know shader programming if I want to write graphics applications?

Page 77: Introduction to GPU Programming with GLSLw3.impa.br/~andmax/talks/tutglsl2009.pdfwhat is GLSL? OpenGL Shading Language C/C++ like language Coding short programs called shaders to run

SIBGRAPI 2009Ricardo Marroquim

André Maximo

motivationarchitecture

languageexamples

wrap-up

Tutorial project: htttp://www.lcg.ufrj.br/Projectos/glsl_tutorial

Sample codes: http://code.google.com/p/glsl-intro-shaders

AM page: http://www.lcg.ufrj.br/Members/andream

RM page: http://www.lcg.ufrj.br/Member/ricardo

AM email: [email protected]

RM email: [email protected]

important linkscodes for all presented demoslast version of this presentationsurvey papercool and useful links

any further questions? we will be very happy to help, do not hesitate to contact us!