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

Similar documents
The Graphics Pipeline

The Graphics Pipeline

Programming shaders & GPUs Christian Miller CS Fall 2011

The Projection Matrix

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

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

Supplement to Lecture 22

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

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

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

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

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

The Projection Matrix

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

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

Sign up for crits! Announcments

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

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

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

Programmable Graphics Hardware

The Critical-Path Algorithm

Lecture 09: Shaders (Part 1)

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

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

Introduction to Shaders.

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

12.2 Programmable Graphics Hardware

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

Magnification and Minification

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

Programmable Graphics Hardware

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

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Lecture 31 Sections 9.1, 9.2, 9.3. Mon, Nov 23, 2009

Shaders. Slide credit to Prof. Zwicker

Lecture 2. Shaders, GLSL and GPGPU

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

OpenGL Programmable Shaders

2D graphics with WebGL

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

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

Introduction to the OpenGL Shading Language (GLSL)

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

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

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

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

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

OPENGL RENDERING PIPELINE

CS 432 Interactive Computer Graphics

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

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

Introduction to the OpenGL Shading Language

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Minimal Spanning Trees

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

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Objectives. Open GL Shading Language (GLSL)

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

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

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

Mobile Application Programing: Android. OpenGL Operation

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

COMP371 COMPUTER GRAPHICS

An Overview GLUT GLSL GLEW

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

The OpenGL Shading Language

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

The Traveling Salesman Problem Brute Force Method

Nondeterministic Programming in C++

Rotations and Translations

GPU Programming EE Final Examination

CMPS160 Shader-based OpenGL Programming

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Programmable shader. Hanyang University

Mobile Application Programing: Android. OpenGL Operation

CS559 Computer Graphics Fall 2015

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

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Computer Graphics Coursework 1

Density Curves Sections

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

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

Tuesday, 23 March OpenGL Shading Language

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

The Graphics Pipeline

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

GPU Programming EE Final Examination

Programmable GPUs Outline

Ambient and Diffuse Light

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Shading Language Update

Transcription:

Shader Programs Lecture 30 Subsections 2.8.2, 2.8.3 Robb T. Koether Hampden-Sydney College Wed, Nov 16, 2011 Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 1 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 2 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 3 / 43

The Fixed Graphics Pipeline The fixed graphics pipeline. Process vertices. Assemble primitives. Transform vertices to clip coordinates. Clip primitives. Perform perspective division. Rasterize primitives. Process fragments. Write fragments to the framebuffer. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 4 / 43

The Fixed Graphics Pipeline The fixed graphics pipeline. Process vertices. Assemble primitives. Transform vertices to clip coordinates. Clip primitives. Perform perspective division. Rasterize primitives. Process fragments. Write fragments to the framebuffer. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 5 / 43

The Fixed Graphics Pipeline The fixed graphics pipeline vertex processor Transforms the vertex to eye coordinates. Transforms the normal to eye coordinates. Generates texture coordinates. Applies color material. Performs lighting calculations. Transforms the vertex to clip coordinates. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 6 / 43

The Fixed Graphics Pipeline The fixed graphics pipeline fragment processor Applies the interpolated shade. Applies the interpolated textures. Does a few other things (e.g., fog) that we haven t talked about. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 7 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 8 / 43

Vertex Shaders Definition (Vertex Shader) A vertex shader is a program that performs its own calculations on each vertex instead of using the calculations of the fixed pipeline. It is mandatory that the vertex shader compute the clip coordinates of the vertex. All other calculations (lighting, textures, etc.) are optional. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 9 / 43

Vertex Shaders Example (A Minimal Vertex Shader) void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 10 / 43

Vertex Shaders Example (A Minimal Vertex Shader) void main() { gl_position = ftransform(); } The ftransform() function will do the same thing. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 11 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 12 / 43

Fragment Shaders Definition (Fragment Shader) A fragment shader is a program that performs its own calculations on each fragment of a primitive instead of using the interpolated values of the fixed pipeline. It is mandatory that the fragment shader compute the color of the fragment. All other calculations are optional. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 13 / 43

Fragment Shaders Example (A Minimal Fragment Shader) void main() { gl_fragcolor = gl_color; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 14 / 43

Minimal Shaders Lecture 30 Demo 1 This demo consists of three programs: Lecture 30 Demo 1.vert Lecture 30 Demo 1.frag Lecture 30 Demo 1.cpp The shader programs are minimal shaders. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 15 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 16 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 17 / 43

GLSL Data Types GLSL is the OpenGL Shading Language. GLSL provides several variable types. Shader variables must be of these types. The three primitive types are bool int float Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 18 / 43

GLSL Data Types GLSL has several vector types Boolean vectors - bvec2, bvec3, bvec4. Integer vectors - ivec2, ivec3, ivec4. Floating-point vectors - vec2, vec3, vec4. GLSL has three floating-point matrix types 2 2 matrices - mat2. 3 3 matrices - mat3. 4 4 matrices - mat4. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 19 / 43

GLSL Data Types Example (GLSL Data Types) // Vertex Shader void main() { gl_position = ftransform(); gl_frontcolor = vec4(1.0, 0.0, 1.0, 1.0); } // Fragment Shader void main() { gl_fragcolor = gl_color; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 20 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 21 / 43

GLSL Variables There are four categories of variable. const uniform attribute varying Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 22 / 43

GLSL Variables Definition (const Variable) A const variable is constant at compile time and, therefore, constant throughout the program. A const is defined within a shader and its scope is limited to that shader. Application Vertex Shader const int s = 0; Fragment Shader const int t = 1; Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43

GLSL Variables Definition (uniform Variable) A uniform variable may be modified by the application program, but its value is constant over the scope of a primitive, i.e., throughout a glbegin()/glend() pair. A uniform variable holds a value that is associated with the primitive. Vertex Shader uniform int s; Application s gluniform(sloc, s); gluniform(tloc, t); t Fragment Shader uniform int t; Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43

GLSL Variables Definition (attribute Variable) An attribute variable may be modified for each vertex, but its value is constant during the processing of that vertex. An attribute variable holds a value that is associated with the vertex. Vertex Shader attribute int s; Application s glvertexattrib(sloc, s); Fragment Shader Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43

GLSL Variables Definition (varying Variable) A varying variable is given a value by the vertex shader. Its values at the vertices are interpolated over the primitive. A varying variable holds a value that will be sent to the fragment shader. Application Vertex Shader varying float s; s Fragment Shader varying float s; Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 24 / 43

GLSL Data Types Example (Constants) const vec4 blue = vec4(0.0, 0.0, 1.0, 1.0); void main() { gl_position = ftransform(); gl_frontcolor = blue; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 25 / 43

Constants Lecture 30 Demo 2 This vertex shader program uses a constant to set the color to blue. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 26 / 43

GLSL Data Types Example (Uniform Variables) const vec4 blue = vec4(0.0, 0.0, 1.0, 1.0); uniform float s; void main() { gl_position = ftransform(); gl_frontcolor = mix(gl_color, blue, s); } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 27 / 43

Uniform Variables For uniform variables, it is necessary to establish a means of communicating their values from the application to the shader programs. In the application program, use glgetuniformlocation() to receive a storage location for the variable in the GPU (once and for all). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 28 / 43

Uniform Variables glgetuniformlocation() unsigned int glgetuniformlocation(unsigned int progobj, string var_name); progobj The program reference number (to be discussed later). var_name The name of the variable in the shader. Return value A reference number for the location of the variable in the GPU. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 29 / 43

Uniform Variables Then it is necessary to pass the current value from the application program to the GPU. Use gluniform() to do this (as often as the value changes). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 30 / 43

Uniform Variables gluniform() void gluniform(unsigned int loc, type value); loc The reference number returned by glgetuniformlocation(). value The value to be passed to the shader. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 31 / 43

Uniform Variables Example (Uniform Variables) // In the application float time; unsigned int tloc = glgetuniformlocation(myprog, "t"); gluniform1f(tloc, time); // In the vertex shader uniform float t; void main() { gl_frontcolor = mix(red, blue, sin(t)); }. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 32 / 43

Uniform Variables Lecture 30 Demo 3 This vertex shader program uses a uniform variable to change the color. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 33 / 43

GLSL Variables Example (Varying Variables) // In the vertex shader varying vec3 vertex_color; void main() { vertex_color = gl_normal; }. // In the fragment shader varying vec3 vertex_color; void main() { gl_fragcolor = vertex_color; }. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 34 / 43

Varying Variables Lecture 30 Demo 4 These shader programs use a varying variable to change the color. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 35 / 43

Attribute Variables Attribute variables may be given a different value for each vertex in a primitive. Otherwise, they are handled much like uniform variables. For attribute variables, it is necessary to establish a means of communicating their values from the application to the shader programs. In the application program, use glgetattriblocation() to receive a storage location for the variable in the GPU (once and for all). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 36 / 43

Attribute Variables glgetuniformlocation() int glgetattriblocation(unsigned int progobj, string var_name); progobj The program reference number (to be discussed on Friday). var_name The name of the variable in the shader. Return value A reference number for the location of the variable in the GPU. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 37 / 43

Attribute Variables Then it is necessary to pass the current value from the application program to the GPU. Use glvertexattrib() to do this (as often as the value changes). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 38 / 43

Attribute Variables glvertexattrib() void glvertexattrib(unsigned int loc, type value); loc The reference number returned by glgetattriblocation(). value The value to be passed to the shader. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 39 / 43

GLSL Variables Example (Attribute Variables) // In the vertex shader attribute vec3 color; void main() { gl_frontcolor = mix(gl_color, color, 0.5); }. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 40 / 43

Attribute Variables Lecture 30 Demo 5 This vertex shader program uses an attribute variable to change the color of the upper vertex of each vertex in a triangle strip. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 41 / 43

Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 42 / 43

Homework Homework Read Subsection 2.8.2 The vertex shader. Read Subsection 2.8.3 The fragment shader. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 43 / 43