Programming shaders & GPUs Christian Miller CS Fall 2011

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

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

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

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Lecture 09: Shaders (Part 1)

Shaders. Slide credit to Prof. Zwicker

CS475/CS675 - Computer Graphics. OpenGL Drawing

12.2 Programmable Graphics Hardware

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

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

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Programmable Graphics Hardware

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

CSE 167: Introduction to Computer Graphics Lecture #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Supplement to Lecture 22

Introduction to the OpenGL Shading Language

COMP371 COMPUTER GRAPHICS

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

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

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

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

OpenGL Programmable Shaders

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

Programmable shader. Hanyang University

Introduction to Shaders.

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

Sign up for crits! Announcments

Lecture 13: OpenGL Shading Language (GLSL)

Programmable Graphics Hardware

Advanced Graphics. The Shader knows. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

GPU Programming EE Final Examination

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

The Graphics Pipeline

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

CS179 GPU Programming Introduction to CUDA. Lecture originally by Luke Durant and Tamas Szalay

CMPS160 Shader-based OpenGL Programming

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Levy: Constraint Texture Mapping, SIGGRAPH, CS 148, Summer 2012 Introduction to Computer Graphics and Imaging Justin Solomon

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

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

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Lecture 2. Shaders, GLSL and GPGPU

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

OPENGL RENDERING PIPELINE

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

OpenGL shaders and programming models that provide object persistence

Rendering Objects. Need to transform all geometry then

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

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

Could you make the XNA functions yourself?

- Surface Rendering -

Programmable Shading. University of Texas at Austin CS354 - Computer Graphics

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Mobile Application Programing: Android. OpenGL Operation

BCA611 Video Oyunları için 3B Grafik

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Introduction to the OpenGL Shading Language (GLSL)

Dave Shreiner, ARM March 2009

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS 179: GPU Programming

The Graphics Pipeline

GLSL: Creating GLSL Programs - Overview

The Rasterization Pipeline

2.11 Particle Systems

Graphics Hardware. Instructor Stephen J. Guy

Programmable Graphics Hardware

GPU Programming EE Final Examination

GPU Programming EE Final Examination

An Overview GLUT GLSL GLEW

The Graphics Pipeline

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

CS GPU and GPGPU Programming Lecture 8+9: GPU Architecture 7+8. Markus Hadwiger, KAUST

CS452/552; EE465/505. Review & Examples

GpuPy: Accelerating NumPy With a GPU

OpenGL. Shading Language. Third Edition

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

Lab 9 - Metal and Glass

CS427 Multicore Architecture and Parallel Computing

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

The Projection Matrix

CS770/870 Fall 2015 Advanced GLSL

Zeyang Li Carnegie Mellon University

We assume that you are familiar with the following:

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

GPU Programming EE Final Examination

Introduction to Computer Graphics. Hardware Acceleration Review

Mobile Application Programing: Android. OpenGL Operation

INF3320 Computer Graphics and Discrete Geometry

CS 432 Interactive Computer Graphics

INTRODUCTION TO OPENGL PIPELINE

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Transcription:

Programming shaders & GPUs Christian Miller CS 354 - Fall 2011

Fixed-function vs. programmable Up until 2001, graphics cards implemented the whole pipeline for you Fixed functionality but configurable through the graphics API and extensions Starting with GeForce 3, you could start programming chunks of the pipeline yourself Written as shaders, first in assembly, then in more usable languages

Shaders today Basically all graphics hardware for the last 5 years has shipped with shader support The pipeline is still implemented in hardware, but several parts are totally programmable DirectX and OpenGL s recommended modes are completely shader based, OpenGL ES requires it If you still want the old functionality, implement it yourself in a shader!

Shader pipeline Green = programmable Yellow = configurable Blue = fixed

Vertex shader Takes one submitted vertex, generates one output vertex Usually used to: Multiply each submitted vertex by projection and modelview matrices Perform per-vertex lighting and texcoord calculations Apply shape warping effects

Geometry shader Takes incoming primitives and may generate additional vertices and primitives Generally used to: Perform tesselation, surface evaluation, or subdivision Dynamically handle level-of-detail

Pixel shader Takes a fragment and interpolated vertex attribute data, outputs framebuffer values for merging Usually used to: Evaluate per-pixel lighting models Do texture lookups and blending

Things not handled in shaders Perspective divide Clipping Vertex attribute interpolation Fragment generation Merging fragments into the framebuffer

Compatibility Vertex and pixel shaders have been around since DirectX 8 and OpenGL 2.0 Support is basically guaranteed Geometry shaders are newer, in DirectX 10 and OpenGL 3.2 Support for these is still not universal Other types of shaders have shown up in DirectX 11 and OpenGL 4.0, but support is very spotty

Programming shaders Step 1: Write your shaders in GLSL Step 2: In your C program, create a shader object for each shader, load up the code, and have the driver compile it for you Step 3: Create a GL program context, attach the compiled shaders to it, and have the driver link it Step 4: Let the driver know which program you want to use, then start rendering

Writing shaders GLSL is a C-like language used to write shaders Just write everything in text files, like regular code Vector math is built in (exactly like GLM) Can have several functions spread across files We ll cover shader language 1.20 here Old (OpenGL 2.1, 2006), but very common

Simple example #version 120 void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; } #version 120 void main() { gl_fragcolor = vec4(0.4, 0.4, 0.8, 1.0); }

Inputs / outputs Each shader has a set of inputs and outputs named beginning with gl_ These are set up automatically for you Some of them must be written in order to have a valid shader Can also make user-defined I/Os

I/O types const: Constants known at shader compile time uniform: Constants known at render time attribute: Submitted vertex attributes (input) varying: Written as output by vertex shaders, then interpolated and used as input by pixel shaders

Built-in uniforms Made available to all shaders gl_modelviewmatrix, gl_projectionmatrix, gl_normalmatrix, gl_modelviewmatrixinverse, etc...

Vertex shader I/Os Inputs (attribute): gl_vertex, gl_normal, gl_color, gl_multitexcoordn Outputs (varying): gl_position, gl_frontcolor, gl_backcolor, gl_texcoord[n]

Pixel shader I/Os Input (varying): gl_fragcoord, gl_frontfacing, gl_color, gl_texcoord[n] Output: gl_fragcolor, gl_fragdepth

Another example

Loading and using shaders Create shader objects in OpenGL, bind them together into a program, then use it for rendering

Creating shader objects Use glcreateshader() with the type of shader to create a new number for it Then, use glshadersource() to attach your source to that shader Finally, use glcompileshader() to compile it Use glgetshaderinfolog() to get a log explaining the errors if compilation failed

Creating program objects Use glcreateprogram() to get a number for a new program object Use glattachshader() to attach the compiled vertex and fragment shaders to it gllinkprogram() will link them together Use glgetprograminfolog() to get errors Finally, gluseprogram() will tell GL to use that set of shaders for rendering

Submitting data Once compiled, both uniforms and attributes get numbers assigned to them Use glgetuniformlocation() and glgetattriblocation() to get those numbers gluniform(), gluniformmatrix() and so forth will load in uniform data glvertexattrib() will submit per-vertex data

More examples... The website for the OpenGL orange book has lots of examples: http://3dshaders.com/

GPU programming GPUs have become common and general enough that you can use them outside of graphics They re massively powerful, but only if the work can be executed completely in parallel Synchronous parallel, to be exact... Very recently, APIs have become available to allow you to write arbitrary code for GPUs

Two GPGPU languages CUDA: Nvidia s language, arrived in 2007 Extended C++ syntax, API is custom Works only on Nvidia hardware OpenCL: Multi-vendor, proposed in late 2008 C99 syntax, API mimics OpenGL Works across GPUs, CPUs, etc...

How GPGPU works You write a kernel (like a shader) This has the code you actually want to run Your host program creates a context, loads up the program and its data (along with how it s distributed), and tells it to execute asynchronously Host can go off and do other things, and gets a notification when the computation is done

Good performance Writing GPGPU code (and shaders) that perform well is all about knowing GPU architecture That s next lecture...