EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 2: Shader Fundamentals (continued)

Slides:



Advertisements
Similar presentations
Vertex Buffer Objects, Vertex Array Objects, Pixel Buffer Objects.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Let’s Look at Some Functions Boolean Functions – functions that return ‘true’ or ‘false’ Other Functions – some with arguments, some with none.
Intermediate Code Generation
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
As well as colors, normals, and other vertex data PUSHIN’ GEO TO THE GPU JEFF CHASTINE 1.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Open Graphics Library (OpenGL)
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Purpose  This training course describes how to configure the the C/C++ compiler options.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
CS 4363/6353 TEXTURE MAPPING PART II. WHAT WE KNOW We can open image files for reading We can load them into texture buffers We can link that texture.
CS 450: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
CS 4363/6353 OPENGL BACKGROUND. WHY IS THIS CLASS SO HARD TO TEACH? (I’LL STOP WHINING SOON) Hardware (GPUs) double in processing power ever 6 months!
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
OpenGL Shader Language Vertex and Fragment Shading Programs.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL Shading Language (GLSL)
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
CS 480/680 Computer Graphics Programming with Open GL Part 5: Putting it all together Dr. Frederick C Harris, Jr. Fall 2011.
Programming with OpenGL Part 5: More GLSL Isaac Gang University of Mary Hardin-Baylor E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
CS 4363/6353 SHADERS/GLSL. ANOTHER LOOK AT THE PIPELINE Vertex Processing Clipping/Assembly Rasterization Fragment Processing.
OpenGL Shading Language
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
OpenGl Shaders Lighthouse3d.com.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Pushin’ Geo to the GPU As well as colors, normals, and other vertex data Made by Jeff Chastine, Modified by Chao Mei Jeff Chastine.
Shader.
Real-Time Rendering Buffers in OpenGL 3.3
Introduction to Computer Graphics with WebGL
Objectives Simple Shaders Programming shaders with GLSL
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Introduction to Computer Graphics with WebGL
Local Variables, Global Variables and Variable Scope
Programming with OpenGL Part 3: Shaders
Programming with OpenGL Part 5: More GLSL
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
Programming with OpenGL Part 5: More GLSL
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
Introduction to Computer Graphics with WebGL
OpenGL Background CS 4722.
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 2: Shader Fundamentals (continued)

Compiler Control – How Shaders are Compiled #pragma optimize (on) or #pragma optimize (off) Default: Optimization is enabled for shaders. Can only be issued outside of function definition.

Global Shader-Compilation Option Extension processing in shaders: #extension extension_name : where extension_name uses the same extension name returned by calling glGetString(GL_EXTENSIONS) or #extension all : affects the behavior of all extensions Directives: require, enable, warn, disable (see Table 2.11)

Interface Blocks Shader variables shared with application or between stages can be, and sometimes must be, organized into blocks of variables. Examples: Uniform variables: organize into uniform blocks Input/output variables: organize into in/out blocks Shader storage buffers: organize into buffer blocks Uniform blocks: uniform b { // uniform or in or out or buffer vec4 v1; // list of variables bool v2; }; // access members as v1 and v2 uniform b { // uniform or in or out or buffer vec4 v1; // list of variables bool v2; } name; // access members as name.v1 and name.v2

Specifying Uniform Blocks in Shaders Declaring a uniform block: uniform Matrices { mat4 ModelView; mat4 Projection; mat4 Color; }; Only transparent types can be within a uniform block. (Opaque types include samplers, images, and atomic counters.) Uniform blocks must be declared at global scope.

Uniform Block Layout Control Qualifiers are used to specify how to lay out the variables in a uniform block. Layout qualifiers (see Table 2.12): shared – share uniform block among multiple programs packed – lay out uniform block to minimize memory use (typically disables sharing) std140 – standard layout for uniform blocks or shader storage buffer blocks (see Appendix I) std430 – standard layout for buffer blocks (see Appendix I) row_major – store matrices in uniform block in row-major element ordering column_major – store in column-major element ordering (default ordering) Examples: layout (shared, row_major) uniform; layout (packed, column_major) uniform;

Accessing Uniform Blocks from Application 1.Find offsets of various uniform variables inside named uniform blocks in shaders by calling glGetUniformBlockIndex() 2.To initialize a buffer object to be associated with uniform block: bind a GL_UNIFORM_BUFFER target by calling glBindBuffer() 3.Determine how large to make buffer object to accommodate variables in named uniform block of shader by calling glGetActiveUniformBlockiv(). Request GL_UNIFORM_BLOCK_DATA_SIZE. 4.Associate buffer object with uniform block by calling glBindBufferRange() or glBindBufferBase() 5.Initialize or change values in block. To explicitly control uniform block’s binding, call glUniformBlockBinding() before glLinkProgram(). If default layout used, call glGetUniformIndices() followed by glGetActiveUniformsiv(). The former will yield the index of a uniform variable. The latter will give the offset and size for that index.

Example 2.4: Initializing Uniform Variables in a Named Uniform Block /* Vertex and fragment shaders that share a block of uniforms named Uniforms. */ const char* vShader = { “#version 330 core\n” “uniform Uniforms {“ “vec3 translation;” “float scale;” “vec4 rotation;” “bool enabled;” “};” “in vec2 vPos;” “in vec3 vColor;” “out vec4 fColor;”

Example 2.4: Continued “void main()” “{“ “vec3 pos = vec3(vPos, 0.0);” “float angle = radians(rotation[0]);” “vec3 axis = normalize(rotation.yzw);” “mat3 I = mat3(1.0);” “mat3 S = mat3( 0, -axis.z, axis.y, “ “ axis.z, 0, -axis.x, “ “ -axis.y, axis.x, 0);” “mat3 uuT = outerProduct(axis, axis);” “mat3 rot = uuT + cos(angle)*(I-uuT) + sin(angle)*S;” “pos *= scale;” “pos *= rot;” “fColor = vec4(scale, scale, scale, 1);” “gl_Position= vec4(pos, 1);” “}” };

Example 2.4: Continued const char* fShader = { “#version 330 core\n” “uniform Uniforms {“ “vec3 translation;” “float scale;” “vec4 rotation;” “bool enabled;” “};” “in vec4 fColor;” “out vec4 color;” “void main()” “{“ “color = fColor;” “}” };

Example 2.4: Continued /* Helper function to convert GLSL types to storage sizes */ size_t TypeSize(Glenum type) { size_t size; #define CASE(Enum, Count, Type) \ case Enum: size = Count*sizeof(Type); break switch(type) { CASE(GL_FLOAT, 1, GLfloat); … CASE(GL_INT_VEC2,2, GLint); … CASE(GL_FLOAT_MAT4x3,12, GLfloat); #undef CASE default: fprintf(stderr, “Unknown type: 0x%x\n”, type); exit(EXIT_FAILURE); break; } return size; }

Example 2.4: Continued void init() { GLuint program; glClearColor(1, 0, 0, 1); ShaderInfo shaders[] = { {GL_VERTEX_SHADER, vShader}, {GL_FRAGMENT_SHADER, fShader}, {GL_NONE, NULL} }; program = LoadShaders(shaders); glUseProgram(program); /* Initialize uniform values in uniform block “Uniforms” */ GLuint uboIndex; GLint uboSize; GLuint ubo; GLvoid *buffer; …

Example 2.4: Continued /* Find the uniform buffer index for “Uniforms”, and determine the block’s sizes */ uboIndex = glGetUniformBlockIndex(program, “Uniforms”); glGetActiveUniformBlockiv(program, uboIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize); buffer= malloc(uboSize); if (buffer == NULL) { fprintf(stderr, “Unable to allocate buffer\n”); exit(EXIT_FAILURE); }

Example 2.4: Continued else { enum {Translation, Scale, Rotation, Enabled, NumUniforms}; /* Values to be stored in buffer object */ GLfloat scale = 0.5; GLfloat translation[] = {0.1, 0.1, 0.0}; GLfloat rotation[] = {90, 0.0, 0.0, 1.0}; GLboolean enabled = GL_TRUE; /* Since we know the names of the uniforms in our block, make an ** array of those values */ const char* names[NumUniforms] = { “translation”, “scale”, “rotation”, “enabled” }; …

Example 2.4: Continued /* Query the necessary attributes to determine where in the buffer we ** should write the values */ GLuintindices[NumUniforms]; GLintsize[NumUniforms]; GLintoffset[NumUniforms]; GLinttype[NumUniforms]; glGetUniformIndices(program, NumUniforms, names, indices); glGetActiveUniformsiv(program, NumUniforms, indices, GL_UNIFORM_OFFFSET, offset); glGetActiveUniformsiv(program, NumUniforms, indices, GL_UNIFORM_SIZE, size); glGetActiveUniformsiv(program, NumUniforms, indices, GL_UNIFORM_TYPE, type);

Example 2.4: Continued /* Copy the uniform values into the buffer */ memcpy(buffer + offset[Scale], &scale, size[Scale] * TypeSize(type[Scale])); memcpy(buffer + offset[Translation], &translation, size[Translation] * TypeSize(type[Translation])); memcpy(buffer + offset[Rotation], &rotation, size[Rotation] * TypeSize(type[Rotation])); memcpy(buffer + offset[Enabled], &enabled, size[Enabled] * TypeSize(type[Enabled])); /* Create the uniform buffer object, initialize its storage, and associate ** it with the shader program */ glGenBuffers(1, &ubo); glBindBuffer(GL_UNIFORM_BUFFER, ubo); glBufferData(GL_UNIFORM_BUFFER, uboSize, buffer, GL_STATIC_RAW); glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex, ubo); } … }

Buffer Blocks GLSL buffer blocks operate similarly to uniform blocks. Important differences: 1.Shaders can write to them and modify their contents as seen from other shader invocations or the application. 2.Size can be established just before rendering, rather than at compile or link time. Example: buffer BufferObject { // create a read-writeable buffer int mode;// preamble members vec4 points[];// last member can be an unsized array }; Memory qualifiers apply to buffer blocks. Set-up shader storage buffer object similarly to how a uniform buffer was set-up. Now glBindBuffer() and glBufferData() take GL_SHADER_STORAGE_BUFFER as the target.

In/Out Blocks Shader variables that output from one stage and input into the next stage can also be organized into interface blocks. Example of vertex shader output: out Lighting { vec3 normal; vec2 bumpCoord; }; Example of fragment shader input: in Lighting { vec3 normal; vec2 bumpCoord; };

Compiling Shaders The compiler and linker for GLSL shaders are part of the OpenGL API. For each shader object: 1. Create a shader object. 2. Compile shader source into object. 3. Verify that shader compiled successfully. To link multiple shader objects into a shader program: 1. Create shader program. 2. Attach appropriate shader objects to shader program. 3. Link shader program. 4. Verify shader link phase completed successfully. 5. Use shader for vertex or fragment processing.

Shader-Compilation Command Sequence