Basics of GPU-Based Programming

Similar documents
Tutorial on GPU Programming. Joong-Youn Lee Supercomputing Center, KISTI

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Shaders (some slides taken from David M. course)

Lecture 2. Shaders, GLSL and GPGPU

3D buzzwords. Adding programmability to the pipeline 6/7/16. Bandwidth Gravity of modern computer systems

1.2.3 The Graphics Hardware Pipeline

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

Getting Started with Cg. Release 1.2 February 2004

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

Programmable Graphics Hardware

12.2 Programmable Graphics Hardware

Programmable Graphics Hardware

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Supplement to Lecture 22

Graphics Hardware. Computer Graphics COMP 770 (236) Spring Instructor: Brandon Lloyd 2/26/07 1

Shaders. Slide credit to Prof. Zwicker

GpuPy: Accelerating NumPy With a GPU

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Sung-Eui Yoon ( 윤성의 )

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Graphics Hardware. Instructor Stephen J. Guy

Graphics Hardware, Graphics APIs, and Computation on GPUs. Mark Segal

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

COMP371 COMPUTER GRAPHICS

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Shader Programming CgFX, OpenGL 2.0. Michael Haller 2003

Teaching Cg. This presentation introduces Cg ( C for graphics ) and explains why it would be useful when teaching a computer graphics course.

Rasterization Overview

DirectX Programming #4. Kang, Seongtae Computer Graphics, 2009 Spring

The Application Stage. The Game Loop, Resource Management and Renderer Design

Advanced Rendering Techniques

Cg 2.0. Mark Kilgard

CHAPTER 1 Graphics Systems and Models 3

Introduction to Programmable GPUs CPSC 314. Introduction to GPU Programming CS314 Gordon Wetzstein, 09/03/09

Introduction to Shaders.

Do you wish you could hear the audio and read the transcription of this session?

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Shading Languages for Graphics Hardware

Cg: A system for programming graphics hardware in a C-like language

OpenGL Programmable Shaders

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

University of Toronto: CSC467F Compilers and Interpreters, Fall 2009

E.Order of Operations

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Design of a Programmable Vertex Processing Unit for Mobile Platforms

Rendering Objects. Need to transform all geometry then

Graphics Processing Units (GPUs) V1.2 (January 2010) Outline. High-Level Pipeline. 1. GPU Pipeline:

Introduction to the OpenGL Shading Language

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

2.11 Particle Systems

Programming Graphics Hardware

Computer Graphics Coursework 1

Evolution of GPUs Chris Seitz

Could you make the XNA functions yourself?

Design and Applications of a Real-Time Shading System Master s Thesis

Design of a Programmable Vertex Processing Unit for Mobile Platforms

OpenGL Status - November 2013 G-Truc Creation

It s All Done with Mirrors. GPU Gems Showcase Kevin Bjorke, NVIDIA GDC2004

Programmable Graphics Hardware

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

Optimal Shaders Using High-Level Languages

CS 130 Final. Fall 2015

CIS 536/636 Introduction to Computer Graphics. Kansas State University. CIS 536/636 Introduction to Computer Graphics

Pipeline Operations. CS 4620 Lecture 14

Canonical Shaders for Optimal Performance. Sébastien Dominé Manager of Developer Technology Tools

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Point-Based rendering on GPU hardware. Advanced Computer Graphics 2008

The Rasterizer Stage. Texturing, Lighting, Testing and Blending

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Intro to GPU Programming (OpenGL Shading Language) Cliff Lindsay Ph.D. Student CS WPI

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

Ciril Bohak. - INTRODUCTION TO WEBGL

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Models and Architectures

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

Hardware-Assisted Relief Texture Mapping

Programming Graphics Hardware

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

Computergraphics Exercise 15/ Shading & Texturing

The Graphics Pipeline

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Shaders in Eve Online Páll Ragnar Pálsson

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

GPU Memory Model. Adapted from:

Graphics Processing Units (GPUs) V2.2 (January 2013) Jan Kautz, Anthony Steed, Tim Weyrich

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

OpenGl Pipeline. triangles, lines, points, images. Per-vertex ops. Primitive assembly. Texturing. Rasterization. Per-fragment ops.

Dave Shreiner, ARM March 2009

Ultimate Graphics Performance for DirectX 10 Hardware

Forward rendering. Unity 5's rendering paths. Which lights get what treatment?

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Transcription:

Module 1: Introduction to GPU-Based Methods Basics of GPU-Based Programming Overview Rendering pipeline on current GPUs Low-level languages Vertex programming Fragment programming High-level shading languages Institute of Visualization and Interactive Systems University of Stuttgart IEEE Visualization 2003 Tutorial: Interactive Visualization of Volumetric Data on Consumer PC Hardware Traditional OpenGL Pipeline (by Kurt Akeley) Programmable Pipeline Most parts of the rendering pipeline can be programmed Shading programs to change hardware behavior Transform and lighting: vertex shaders / vertex programs Fragment processing: pixel shaders / fragment programs History: from fixed-function pipeline to configurable pipeline Steps towards programmability

Programmable Pipeline 3D application GPU 3D API GPU front end CPU primitive assembly rasterization raster operations Shaders vertex processor fragment processor Issues How are vertex and pixel shaders specified? Low-level, assembler-like High-level language Data flow between components Per-vertex data (for vertex shader) Per-fragment data (for pixel shader) Uniform (constant) data: e.g. modelview matrix, material parameters frame buffer Overview What Are Low-Level APIs? Rendering pipeline on current GPUs Low-level languages Vertex programming Fragment programming High-level shading languages Similarity to assembler Close to hardware functionality Input: vertex/fragment attributes Output: new vertex/fragment attributes Sequence of instructions on registers Very limited control flow (if any) Platform-dependent BUT: there is convergence

What Are Low-Level APIs? Current low-level APIs: OpenGL extensions: GL_ARB_vertex_program, GL_ARB_fragment_program DirectX 9: Vertex Shader 2.0, Pixel Shader 2.0 Older low-level APIs: DirectX 8.x: Vertex Shader 1.x, Pixel Shader 1.x OpenGL extensions: GL_ATI_fragment_shader, GL_NV_vertex_program, Why Use Low-Level APIs? Low-level APIs offer best performance & functionality Help to understand the graphics hardware (ATI s r300, NVIDIA s nv30,...) Help to understand high-level APIs (Cg, HLSL,...) Much easier than directly specifying configurable graphics pipeline (e.g. register combiners) Overview Rendering pipeline on current GPUs Low-level languages Vertex programming Fragment programming High-level shading languages Applications of Vertex Programming Customized computation of vertex attributes Computation of anything that can be interpolated linearly between vertices Limitations: Vertices can neither be generated nor destroyed No information about topology or ordering of vertices is available

OpenGL GL_ARB_vertex_program Circumvents the traditional vertex pipeline What is replaced by a vertex program? Vertex transformations Vertex weighting/blending Normal transformations Color material Per-vertex lighting Texture coordinate generation Texture matrix transformations Per-vertex point size computations Per-vertex fog coordinate computations Client-defined clip planes OpenGL GL_ARB_vertex_program What is not replaced? Clipping to the view frustum Perspective divide (division by w) Viewport transformation Depth range transformation Front and back color selection Clamping colors Primitive assembly and per-fragment operations Evaluators GL_ARB_vertex_program: Machine Model Vertex Attributes 16 x 4 registers vertex.* Vertex Program 128 instructions Program Results 8 x 4 registers result.* Program Environment/ Local Parameters each 96 x 4 registers program.env[...] / program.local[...] Program Temporaries 12 x 4 registers Program Address Registers 1 x 1 register GL_ARB_vertex_program: Variables Vertex attributes vertex.position, vertex.color, Explicit binding: ATTRIB name = vertex.*; State variables state.material.* (diffuse,...), state.matrix.* (modelview[n], ), Program results and output variables result.color.* (primary, secondary, ), result.position, OUTPUT name = result.*;

GL_ARB_vertex_program: Instructions Instruction set 27 instructions Operate on floating-point scalars or 4-vectors Basic syntax: OP destination [, source1 [, source2 [, source3]]]; # comm. Example: MOV result.position, vertex.position; # sets result.position Numerical operations: ADD, MUL, LOG, EXP, Modifiers: swizzle, negate, mask, saturation GL_ARB_vertex_program: Example Transformation to clip coordinates:!!arbvp1.0 ATTRIB pos = vertex.position; ATTRIB col = vertex.color; OUTPUT clippos = result.position; OUTPUT newcol = result.color; PARAM modelviewproj[4] = { state.matrix.mvp }; DP4 clippos.x, modelviewproj[0], pos; DP4 clippos.y, modelviewproj[1], pos; DP4 clippos.z, modelviewproj[2], pos; DP4 clippos.w, modelviewproj[3], pos; MOV newcol, col; END DirectX 9: Vertex Shader 2.0 Vertex Shader 2.0 introduced in DirectX 9.0 Similar functionality and limitations as GL_ARB_vertex_program Similar registers and syntax Additional functionality: static flow control Control of flow determined by constants (not by per-vertex attributes) Conditional blocks, repetition, subroutines Overview Rendering pipeline on current GPUs Low-level languages Vertex programming Fragment programming High-level shading languages

Applications of Fragment Programming Customized computation of fragment attributes Computation of anything that should be computed per pixel Limitations: Fragments cannot be generated Position of fragments cannot be changed No information about geometric primitive is available OpenGL GL_ARB_fragment_program Circumvents the traditional fragment pipeline What is replaced by a pixel program? Texturing Color sum Fog for the rasterization of points, lines, polygons, pixel rectangles, and bitmaps What is not replaced? Fragment tests (alpha, stencil, and depth tests) Blending GL_ARB_fragment_program: Machine Model Fragment Attributes 10 x 4 registers fragment.* Fragment Program 48 ALU instructions 24 texture instructions 4 texture indirections Program Results 2 x 4 registers; result.* Program Environment/ Local Parameters 24 x 4 registers program.env[...] / program.local[...] Program Temporaries 16 x 4 registers GL_ARB_fragment_program: Instructions Instruction set Similar to vertex program instructions Operate on floating-point scalars or 4-vectors Texture sampling OP destination, source, texture[index], type; Texture types: 1D, 2D, 3D, CUBE, RECT TEX result.color, fragment.texcoord[1], texture[0], 2D; samples 2D texture in unit 0 with texture coordinate set 1 and writes result to result.color

GL_ARB_fragment_program: Example!!ARBfp1.0 ATTRIB tex = fragment.texcoord; ATTRIB col = fragment.color.primary; OUTPUT outcolor = result.color; TEMP tmp; DirectX 9: Pixel Shader 2.0 Pixel Shader 2.0 introduced in DirectX 9.0 Similar functionality and limitations as GL_ARB_fragment_program Similar registers and syntax TXP tmp, tex, texture[0], 2D; MUL outcolor, tmp, col; END DirectX 9: Pixel Shader 2.0 Declaration of texture samplers dcl_type s* Declaration of input color and texture coordinate dcl v*[.mask] dcl t*[.mask] Instruction set Operate on floating-point scalars or 4-vectors Pixel Shader 2.0: Simple Example ps_2_0 dcl_2d s0 dcl t0.xy texld r1, t0, s0 mov oc0, r1 op destination [, source1 [, source2 [, source3]]] Texture sampling op destination, source, sn

Overview Rendering pipeline on current GPUs Low-level languages Vertex programming Fragment programming High-level shading languages High-Level Shading Languages Why? Avoids programming, debugging, and maintenance of long assembly shaders Easy to read Easier to modify existing shaders Automatic code optimization Wide range of platforms Shaders often inspired RenderMan shading language Assembly vs. High-Level Language Assembly dp3 r0, r0, r1 max r1.x, c5.x, r0.x pow r0.x, r1.x, c4.x mul r0, c3.x, r0.x mov r1, c2 add r1, c1, r1 mad r0, c0.x, r1, r0... High-level language float4 cspec = pow(max(0, dot(nf, H)), phongexp).xxx; float4 cplastic = Cd * (cambi + cdiff) + Cs * cspec; Blinn-Phong shader expressed in both assembly and high-level language Data Flow through Pipeline Vertex shader program Fragment shader program Connectors 3D application vertex program high-level shader fragment program high-level shader frame buffer

High-Level Shading Languages Cg C for Graphics By NVIDIA HLSL High-level shading language Part of DirectX 9 (Microsoft) OpenGL 2.0 Shading Language Proposal by 3D Labs Cg Typical concepts for a high-level shading language Language is (almost) identical to DirectX HLSL Syntax, operators, functions from C/C++ Conditionals and flow control Backends according to hardware profiles Support for GPU-specific features (compare to low-level) Vector and matrix operations Hardware data types for maximum performance Access to GPU functions: mul, sqrt, dot, Mathematical functions for graphics, e.g. reflect Profiles for particular hardware feature sets Workflow in Cg parse offline or Cg shader Cg compiler runtime compile compilation and optimization Phong Shading in Cg: Vertex Shader First part of pipeline Connectors: what kind of data is transferred to / from vertex program? Actual vertex shader 3D application vertex program fragment program frame buffer DirectX OpenGL low-level assembly code vertexin vertexout GPU internal machine code shader mainvs shader

Phong Shading in Cg: Connectors Describe input and output Varying data Specified as struct Extensible Adapted to respective implementation Only important data is transferred Pre-defined registers: POSITION, NORMAL,... Phong Shading in Cg: Connectors // data from application to vertex program struct vertexin { float3 Position : POSITION; // pre-defined registers float4 UV : TEXCOORD0; float4 Normal : NORMAL; }; // vertex shader to pixel shader struct vertexout { float4 HPosition : POSITION; float4 TexCoord : TEXCOORD0; float3 LightVec : TEXCOORD1; float3 WorldNormal : TEXCOORD2; float3 WorldPos : TEXCOORD3; float3 WorldView : TEXCOORD4; }; Phong Shading in Cg: Vertex Shader Vertex shader is a function with required Varying application-to-vertex input parameter Vertex-to-fragment output structure Optional uniform input parameters Constant for a larger number of primitives Passed to the Cg program by the application through the runtime library Vertex shader for Phong shading: Compute position, normal vector, viewing vector, and light vector in world coordinates Phong Shading in Cg: Vertex Shader // vertex shader vertexout mainvs(vertexin IN, // vertex input from app uniform float4x4 WorldViewProj,// constant parameters uniform float4x4 WorldIT, // from app: various uniform float4x4 World, // transformation uniform float4x4 ViewIT, // matrices and a uniform float3 LightPos // point-light source ) { vertexout OUT; // output of the vertex shader OUT.WorldNormal = mul(worldit, IN.Normal).xyz; // position in object coords: float4 Po = float4(in.position.x,in.position.y, IN.Position.z,1.0); float3 Pw = mul(world, Po).xyz; // pos in world coords

Phong Shading in Cg: Vertex Shader } OUT.WorldPos = Pw; // pos in world coords OUT.LightVec = LightPos - Pw; // light vector OUT.TexCoord = IN.UV; // original tex coords // view vector in world coords: OUT.WorldView = normalize(viewit[3].xyz - Pw); // pos in clip coords: OUT.HPosition = mul(worldviewproj, Po); return OUT; // output of vertex shader Phong Shading in Cg: Pixel Shader Second part of pipeline Connectors: from vertex to pixel shader, from pixel shader to frame buffer Actual pixel shader 3D application vertex program fragment program frame buffer shader vertexout shader mainps pixelout Phong Shading in Cg: Pixel Shader Pixel shader is a function with required Varying vertex-to-fragment input parameter Fragment-to-pixel output structure Optional uniform input parameters Constant for a larger number of fragments Passed to the Cg program by the application through the runtime library Pixel shader for Phong shading: Normalize light, viewing, and normal vectors Compute halfway vector Add specular, diffuse, and ambient contributions Phong Shading in Cg: Pixel Shader // final pixel output: // data from pixel shader to frame buffer struct pixelout { float4 col : COLOR; }; // pixel shader pixelout mainps(vertexout IN, // input from vertex shader uniform float SpecExpon, // constant parameters from uniform float4 AmbiColor, // application uniform float4 SurfColor, uniform float4 LightColor ) { pixelout OUT; // output of the pixel shader float3 Ln = normalize(in.lightvec); float3 Nn = normalize(in.worldnormal); float3 Vn = normalize(in.worldview); float3 Hn = normalize(vn + Ln);

Phong Shading in Cg: Pixel Shader } // scalar product between light and normal vectors: float ldn = dot(ln,nn); // scalar product between halfway and normal vectors: float hdn = dot(hn,nn); // specialized lit function computes weights for // diffuse and specular parts: float4 litv = lit(ldn,hdn,specexpon); float4 diffcontrib = SurfColor * ( litv.y * LightColor + AmbiColor); float4 speccontrib = litv.y*litv.z * LightColor; // sum of diffuse and specular contributions: float4 result = diffcontrib + speccontrib; OUT.col = result; return OUT; // output of pixel shader What s Next? Usage of GPU for specific tasks Visualization Volumetric data Filtering, mapping, and rendering Focus on fragment processing