Supplement to Lecture 22

Similar documents
Objectives. Open GL Shading Language (GLSL)

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

3D Game Programming Programmable Pipelines. Ming-Te Chi Department of Computer Science, National Chengchi University

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programmable Graphics Hardware

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

12.2 Programmable Graphics Hardware

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Computer Graphics (CS 4731) & 2D Graphics Systems

GLSL II. Objectives. Coupling GLSL to Applications Example applications

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

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

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

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

Lecture 09: Shaders (Part 1)

Introduction to Shaders.

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

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

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

Programming shaders & GPUs Christian Miller CS Fall 2011

2D graphics with WebGL

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

Programmable Graphics Hardware

Objectives. Coupling GLSL to Applications Example applications

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

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

Introduction to the OpenGL Shading Language (GLSL)

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

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

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

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

- Surface Rendering -

Programmable shader. Hanyang University

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

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

Shaders. Slide credit to Prof. Zwicker

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Models and Architectures

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

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

OpenGL Programmable Shaders

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

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

Introduction to the OpenGL Shading Language

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

Lecture 2. Shaders, GLSL and GPGPU

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

E.Order of Operations

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

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

Pipeline Operations. CS 4620 Lecture 14

Introduction to Computer Graphics with WebGL

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

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

CHAPTER 1 Graphics Systems and Models 3

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

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

Advanced Rendering Techniques

CS 432 Interactive Computer Graphics

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

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

COMP371 COMPUTER GRAPHICS

CMPS160 Shader-based OpenGL Programming

Sign up for crits! Announcments

Graphics Hardware. Instructor Stephen J. Guy

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

1.2.3 The Graphics Hardware Pipeline

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

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

Programming Graphics Hardware

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

COURSE 17: STATE-OF-THE-ART IN SHADING HARDWARE1 CHAPTER 6: THE OPENGL SHADING LANGUAGE 2. Randi J. Rost 3Dlabs, Inc.

INF3320 Computer Graphics and Discrete Geometry

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

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Evolution of GPUs Chris Seitz

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

Sung-Eui Yoon ( 윤성의 )

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

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

Lecture 13: OpenGL Shading Language (GLSL)

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

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

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

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

Programmable GPUs Outline

Shaders (some slides taken from David M. course)

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

Transcription:

Supplement to Lecture 22 Programmable GPUs

Programmable Pipelines Introduce programmable pipelines - Vertex shaders - Fragment shaders Introduce shading languages - Needed to describe shaders - RenderMan

Recent major advance in real time graphics is programmable pipeline - First introduced by NVIDIA GForce 3 - Supported by high-end commodity cards NVIDIA, ATI, 3D Labs - Software Support Direct X 8, 9, 10 OpenGL Extensions OpenGL Shading Language (GLSL) Cg Two components - Vertex programs (shaders) - Fragment programs (shaders) Requires detailed understanding of two seemingly contradictory apporachs - OpenGL pipeline Real time - RenderMan ideas offline Preliminaries

Pipeline vertices vertices fragments fragments CPU Geometry Processor Rasterizer Fragment Processor Frame Buffer

Geometric Calculations Geometric data: set of vertices + type - Can come from program, evaluator, display list - type: point, line, polygon - Vertex data can be (x,y,z,w) coordinates of a vertex (glvertex) Normal vector Texture Coordinates RGBA color Other data: color indices, edge flags Additional user-defined data in GLSL

Per Vertex Operations Vertex locations are transformed by the model-view matrix into eye coordinates Normals must be transformed with the inverse transpose of the model-view matrix so that v n=v n in both spaces - Assumes there is no scaling - May have to use autonormalization Textures coordinates are generated if autotexture enabled and the texture matrix is applied

Vertex Processor Takes in vertices - Position attribute - Possibly color - OpenGL state Produces - Position in clip coordinates - Vertex color

Vertex Objects -> Fragments Vertices are next assembled into objects - Polygons - Line Segments - Points Transformation by projection matrix Clipping - Against user defined planes - View volume, x=±w, y=±w, z=±w - Polygon clipping can create new vertices Perspective Division Viewport mapping Geometric objects are rasterized into fragments Each fragment corresponds to a point on an integer grid: a displayed pixel Hence each fragment is a potential pixel Each fragment has - A color - Possibly a depth value - Texture coordinates

Fragment Processor Takes in output of rasterizer (fragments) - Vertex values have been interpolated over primitive by rasterizer Outputs a fragment - Color - Texture Fragments still go through fragment tests - Hidden-surface removal - alpha

Fragment Operations Texture generation Fog Antialiasing Scissoring Alpha test Blending Dithering Logical Operation Masking

Programmable Shaders Replace fixed function vertex and fragment processing by programmable processors called shaders Can replace either or both If we use a programmable shader we must do all required functions of the fixed function processor

Modeling, Rendering, Shaders Modeler outputs geometric model plus information for the renderer - Specifications of camera - Materials - Lights May have different kinds of renderers - Ray tracer - Radiosity How do we specify a shader? Shaders such as the Phong model can be written as algebraic expressions I =k d I d l n + k s I s (v r ) s + k a I a But expressions can be described by trees Need now operators such as dot and cross products and new data types such as matrices and vectors Environmental variables are part of state

Fragment Shader Applications Per fragment lighting calculations per vertex lighting per fragment lighting

Vertex Shader for Per Fragment Lighting varying vec3 N, L, E, H; void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; } vec4 eyeposition = gl_modelviewmatrix * gl_vertex; vec4 eyelightpos = gl_lightsource[0].position; N = normalize(gl_normalmatrix * gl_normal); L = normalize(eyelightpos.xyz - eyeposition.xyz); E = -normalize(eyeposition.xyz); H = normalize(l + E);

Fragment Shader for Phong Lighting I varying vec3 N; varying vec3 L; varying vec3 E; varying vec3 H; void main() { vec3 Normal = normalize(n); vec3 Light = normalize(l); vec3 Eye = normalize(e); vec3 Half = normalize(h);

Fragment Shader for Phong Lighting II float Kd = max(dot(normal, Light), 0.0); float Ks = pow(max(dot(half, Normal), 0.0), gl_frontmaterial.shininess); float Ka = 0.0; vec4 diffuse = Kd * gl_frontlightproduct[0].diffuse; vec4 specular = Ks * gl_frontlightproduct[0].specular; vec4 ambient = Ka * gl_frontlightproduct[0].ambient; } gl_fragcolor = ambient + diffuse + specular;

Writing Shaders First programmable shaders were programmed in an assembly-like manner OpenGL extensions added for vertex and fragment shaders Cg (C for graphics) C-like language for programming shaders - Works with both OpenGL and DirectX - Interface to OpenGL complex OpenGL Shading Language (GLSL)

GLSL OpenGL Shading Language Part of OpenGL 2.0 High level C-like language New data types - Matrices - Vectors - Samplers OpenGL state available through built-in variables

Single Vertex Shader const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); void main(void) { gl_position = gl_projectionmatrix *gl_modelviewmartrix*gl_vertex; } gl_frontcolor = red;

Execution Model

Simple Fragment Program

Data Types C types: int, float, bool Vectors: - float vec2, vec 3, vec4 - Also int (ivec) and boolean (bvec) Matrices: mat2, mat3, mat4 - Stored by columns - Standard referencing m[row][column] C++ style constructors - vec3 a =vec3(1.0, 2.0, 3.0) - vec2 b = vec2(a)

Pointers There are no pointers in GLSL We can use C structs which can be copied back from functions Because matrices and vectors are basic types they can be passed into and output from GLSL functions, e.g. matrix3 func(matrix3 a)

Qualifiers GLSL has many of the same qualifiers such as const as C/C++ Need others due to the nature of the execution model Variables can change - Once per primitive - Once per vertex - Once per fragment - At any time in the application Vertex attributes are interpolated by the rasterizer into fragment attributes

Attribute Qualifier Attribute-qualified variables can change at most once per vertex - Cannot be used in fragment shaders Built in (OpenGL state variables) - gl_color - gl_modelviewmatrix User defined (in application program) - attribute float temperature - attribute vec3 velocity

Uniform Qualified Variables that are constant for an entire primitive Can be changed in application outside scope of glbegin and glend Cannot be changed in shader Used to pass information to shader such as the bounding box of a primitive

Variables that are passed from vertex shader to fragment shader Automatically interpolated by the rasterizer Built in Varying Qualified - Vertex colors - Texture coordinates User defined - Requires a user defined fragment shader

Required Fragment Shader varying vec3 color_out; void main(void) { gl_fragcolor = color_out; }

Passing Values call by value-return Variables are copied in Returned values are copied back Three possibilities - in - out - inout

Vertex Shader const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); varying vec3 color_out; void main(void) { gl_position = gl_modelviewprojectionmatrix*gl_vertex; color_out = red; }

Operators and Functions Standard C functions - Trigonometric - Arithmetic - Normalize, reflect, length Overloading of vector and matrix types mat4 a; vec4 b, c, d; c = b*a; // a column vector stored as a 1d array d = a*b; // a row vector stored as a 1d array

Swizzling & Selection Can refer to array elements by element using [] or selection (.) operator with - x, y, z, w - r, g, b, a - s, t, p, q - a[2], a.b, a.z, a.p are the same Swizzling operator lets us manipulate components vec4 a; a.yz = vec2(1.0, 2.0);

Linking Shaders to OpenGL OpenGL Extensions - ARB_shader_objects - ARB_vertex_shader - ARB_fragment_shader OpenGL 2.0 - Almost identical to using extensions - Avoids extension suffixes on function names

Program Object Container for shaders - Can contain multiple shaders - Other GLSL functions GLuint myprogobj; myprogobj = glcreateprogram(); /* define shader objects here */ gluseprogram(myprogobj); gllinkprogram(myprogobj);

Reading a Shader Shader are added to the program object and compiled Usual method of passing a shader is as a null-terminated string using the function glshadersource If the shader is in a file, we can write a reader to convert the file to a string

Adding a Vertex Shader GLint vshader; GLunit myvertexobj; GLchar vshaderfile[] = my_vertex_shader ; GLchar* vsource = readshadersource(vshaderfile); glshadersource(myvertexobj, 1, &vertexshaderfile, NULL); myvertexobj = glcreateshader(gl_vertex_shader); glcompileshader(myvertexobj); glattachobject(myprogobj, myvertexobj);