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

Similar documents
CS475/CS675 - Computer Graphics. OpenGL Drawing

Programming shaders & GPUs Christian Miller CS Fall 2011

We assume that you are familiar with the following:

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

COMP371 COMPUTER GRAPHICS

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

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

CS452/552; EE465/505. Review & Examples

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

OPENGL RENDERING PIPELINE

CS 432 Interactive Computer Graphics

BCA611 Video Oyunları için 3B Grafik

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

Rendering Objects. Need to transform all geometry then

Lecture 09: Shaders (Part 1)

CS452/552; EE465/505. Image Processing Frame Buffer Objects

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

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

CS452/552; EE465/505. Texture Mapping in WebGL

CPSC 436D Video Game Programming

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

Buffers. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

Mobile Application Programing: Android. OpenGL Operation

Lecture 13: OpenGL Shading Language (GLSL)

The Graphics Pipeline

Shaders. Slide credit to Prof. Zwicker

The Graphics Pipeline

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

CISC 3620 Lecture 7 Lighting and shading. Topics: Exam results Buffers Texture mapping intro Texture mapping basics WebGL texture mapping

Texture Mapping. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS 432 Interactive Computer Graphics

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Overview. By end of the week:

OpenGL BOF Siggraph 2011

Mali & OpenGL ES 3.0. Dave Shreiner Jon Kirkham ARM. Game Developers Conference 27 March 2013

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Mobile Application Programing: Android. OpenGL Operation

Introduction to Shaders.

Dave Shreiner, ARM March 2009

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

CS452/552; EE465/505. Image Formation

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

Advanced Graphics. OpenGL and Shaders I. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

Computer graphics 2: Graduate seminar in computational aesthetics

Supplement to Lecture 22

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

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

2D graphics with WebGL

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Introduction to Computer Graphics. Hardware Acceleration Review

The Graphics Pipeline

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Programming with OpenGL Complete Programs Objectives Build a complete first program

Mobile Application Programming: Android. OpenGL Operation

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

Introduction to OpenGL/GLSL and WebGL GLSL

Ciril Bohak. - INTRODUCTION TO WEBGL

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

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Grafica Computazionale: Lezione 30. Grafica Computazionale. Hiding complexity... ;) Introduction to OpenGL. lezione30 Introduction to OpenGL

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

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

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

Advanced Graphics. OpenGL and Shaders I. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

GLSL: Creating GLSL Programs - Overview

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

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

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

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

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

Introduction to Computer Graphics with WebGL

The Projection Matrix

Achieving High-performance Graphics on Mobile With the Vulkan API

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

EECS 487: Interactive Computer Graphics

Mining the Rendering Power in Web Browsers. Jianxia Xue Jan. 28, 2014

Practical 1: OpenTK Tutorial

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

INTRODUCTION TO OPENGL PIPELINE

OpenGL SC. March Sean Harmer Managing Director UK

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

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

OpenGL shaders and programming models that provide object persistence

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

Tutorial 04. Harshavardhan Kode. September 14, 2015

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

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

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

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

Introduction to the OpenGL Shading Language (GLSL)

The Projection Matrix

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

Transcription:

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

Copyright Khronos Group 2012 Page 2 Agenda Overview of OpenGL family of APIs Comparison of rendering pipelines Debugging the most common OpenGL usage errors

Copyright Khronos Group 2012 Page 3 The OpenGL Family OpenGL - The cross-platform standard for 3D Graphics OpenGL ES - The standard for embedded 3D Graphics WebGL - Bringing interactive 3D graphics to the Web through a JavaScript interface OpenGL SC - Verified 3D graphics API for safety critical applications - significantly different pipeline than the others - (not discussed in this session)

Copyright Khronos Group 2012 Page 4 The Fundamental Rendering Pipeline Geometric Data (positions, colors, texture coordinates, etc.) Vertex Shading Rasterizer Fragment Shading Pixel Data (textures, etc.) This is the pipeline that OpenGL ES and WebGL expose. OpenGL extends the pipeline.

Copyright Khronos Group 2012 Page 5 The OpenGL 4.3 Rendering Pipeline Geometric Data (positions, colors, texture coordinates, etc.) Vertex Shading Tessellation Shading Geometry Shading Pixel Data (textures, etc.) Compute Shading Rasterizer Fragment Shading

Copyright Khronos Group 2012 Page 6 A Few Basics Will Go a Long Way All of the APIs are shader based - they use variants of the OpenGL Shading Language GLSL - up porting (from ES or WebGL) to OpenGL shouldn t require any work - down porting may require a few changes Always storing you data in buffer objects will guarantee portability and performance - there are a few differences that you ll need to keep in mind You ll also need to know a little linear algebra Now, to introduce a few introductory concepts

Copyright Khronos Group 2012 Page 7 Representing Geometric Objects Geometric objects in OpenGL are represented using vertices A vertex is a collection of generic attributes - positional coordinates - colors - texture coordinates - any other data associated with that point in space Positions are stored as 4-dimensional homogeneous coordinates Vertex data must be stored in vertex buffer objects (VBOs) VBOs must be stored in vertex array objects (VAOs) (OpenGL only) x y z w

How Data Flows through GLSL Shaders attributes (in) are the inputs into vertex shaders varyings (out) outputs of vertex shaders and inputs into fragment shaders uniforms are constants available to any shader stage uniform API attribute GLSL Keyword varying OpenGL in out ES / WebGL attribute varying For OpenGL, all shader stages use in for all inputs into a shader out for all outputs from a shader 8 Vertex Shader Fragment Shader attribute varying fragments Copyright Khronos Group 2012 Page 8

Copyright Khronos Group 2012 Page 9 Anatomy of a Simple Vertex Shader (OpenGL version) in vec4 vposition; in vec4 vcolor; out vec4 color; void main() { color = vcolor; gl_position = vertex; }

Copyright Khronos Group 2012 Page 10 Anatomy of a Simple Vertex Shader (ES/WebGL version) attribute vec4 vposition; attribute vec4 vcolor; varying vec4 color; void main() { color = vcolor; gl_position = vertex; }

Copyright Khronos Group 2012 Page 11 Anatomy of a Simple Fragment Shader (OpenGL version) in vec4 vcolor; out vec4 color; void main() { color = vcolor; }

Copyright Khronos Group 2012 Page 12 Anatomy of a Simple Fragment Shader (ES/WebGL version) varying vec4 color; void main() { gl_fragcolor = color; }

Copyright Khronos Group 2012 Page 13 Getting Your Shaders into OpenGL Shaders need to be compiled and linked to form an executable shader program OpenGL provides the compiler and linker - you access them by making function calls to the API A program must contain - one vertex shader - one fragment shader - other shader stages are optional (OpenGL only) Create Program Create Shader Load Shader Source Compile Shader Attach Shader to Program Link Program glcreateprogram() glcreateshader() glshadersource() glcompileshader() glattachshader() gllinkprogram() These steps need to be repeated for each type of shader in the shader program Use Program gluseprogram()

Copyright Khronos Group 2012 Page 14 OpenGL Shader Plumbing You need to make several connections to get data into the pipeline - only need to do this attributes varyings take care of themselves vposition[0] in vec4 vposition; in vec4 vcolor; out vec4 color; void main() { gl_position = vposition; color = vcolor; } Shader Compilation Variable Name Attribute Index vposition 12 vcolor 2 vcolor[0] vposition[1] vcolor[1] vposition[2] vcolor[2] vposition[3] vcolor[3]

Copyright Khronos Group 2012 Page 15 OpenGL Shader Plumbing You need to make several connections to get data into the pipeline - only need to do this attributes varyings take care of themselves vposition[0] in vec4 vposition; in vec4 vcolor; out vec4 color; void main() { gl_position = vposition; color = vcolor; } glgetattriblocation vcolor[0] vposition[1] vcolor[1] vposition[2] vcolor[2] vposition[3] vcolor[3] Variable Name Attribute Index vposition 12 vcolor 2

Copyright Khronos Group 2012 Page 16 OpenGL Shader Plumbing You need to make several connections to get data into the pipeline - only need to do this attributes varyings take care of themselves vposition[0] in vec4 vposition; in vec4 vcolor; out vec4 color; void main() { gl_position = vposition; color = vcolor; } glvertexattribpointer vcolor[0] vposition[1] vcolor[1] vposition[2] vcolor[2] vposition[3] vcolor[3] Variable Name Attribute Index vposition 12 vcolor 2

Debugging the Most Common Problems Copyright Khronos Group 2012 Page 17

Copyright Khronos Group 2012 Page 18 The Bind-to-Edit Model OpenGL uses objects for storing data - it s loosely modeled on object-oriented programming, but not quite Objects are implicitly specified by a binding operation - glbind*() calls Objects are usually bound twice: 1. object creation and initialization 2. specification as a data source for OpenGL rendering This most often causes problems for novice programmers - they forget the second binding (to use the data), or - they re bound to the wrong object

Copyright Khronos Group 2012 Page 19 Where are my objects? There are many steps that are required to have geometry show in a window The following must all occur correctly: 1. specifying data into vertex arrays 2. compiling and linking shaders 3. associating vertex attributes with shader variables 4. specifying the viewport 5. loading uniform variables for shader use 6. transforming and projecting geometry into the viewport 7. passing the transformed vertices out of the vertex shader Where should you start looking?

Copyright Khronos Group 2012 Page 20 Verifying Your Data s Getting Into OpenGL You can use Normalized Device Coordinates (NDCs) to make sure data s flowing correctly - x and y need to be between -1.0 and 1.0 - z needs to be between 0.0 and 1.0 Verify using very simple vertex and fragment shaders - often called a pass-thru shader in vec4 vpos; void main() { gl_position = vpos; } If your test geometry shows up, then you know: - data is correctly specified to OpenGL - attributes are correctly specified and enabled - shaders compiled and linked successfully - viewport is correctly specified out vec4 color; void main() { color = vec4(1); }

Copyright Khronos Group 2012 Page 21 OpenGL Matrices for C Programmers Matrices in OpenGL are column-major - this is the exact opposite of how C programmers think There are many places were a matrix s transpose can be introduced - in the application (most straightforward) - when specifying the matrix as a uniform gluniformmatrix(, GL_TRUE, ); - in the shader - column_major qualifier in GLSL - transpose() method in GLSL é ê êê M = ê ê ë m 0 m 4 m 8 m 12 m 1 m 5 m 9 m 13 m 2 m 6 m 10 m 14 m 3 m 7 m 11 m 15 ù ú úú ú ú û

Copyright Khronos Group 2012 Page 22 Matrix Multiplication Is Not Commutative Vertices are usually transformed by 4 4 matrices - modeling and viewing transformations - projection transformations Vertices should be multiplied on right of a matrix v' = Mv Matrices should be accumulated on the right as well C(BA)

Where Are My Textures? Loading an image to use as a texture in OpenGL is easy - use the glteximage*d call - loads a single texture level - levels are fundamental to a technique called mipmapping Sampling controls how colors are retrieved from a texture map - including whether mipmaps are to be used However, OpenGL s default sampling mode require mipmaps Three potential solutions: - change the sampling mode: gltexparameteri(, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - load more mipmap levels - automatically build mipmaps: glgeneratemipmaps() Copyright Khronos Group 2012 Page 23

Copyright Khronos Group 2012 Page 24 Why Do My Textures Looked Skewed? glteximage2d transfers pixels form an application s memory into OpenGL - various parameters are used to control which addresses are read from All of those parameters are controlled by the glpixelstrore function The default mode is to assume every pixel starts on a four-byte boundary - makes sense for RGBA 8-bit per component images - but, a lot of images are RGB only (and have a three-byte stride) Adjust the GL_UNPACK_ALIGNMENT to 1 - default is 4-1 specifies byte alignment

Thanks! Copyright Khronos Group 2012 Page 25