CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

Similar documents
Rendering Objects. Need to transform all geometry then

OpenGL. Toolkits.

Shaders. Slide credit to Prof. Zwicker

COMP371 COMPUTER GRAPHICS

Spring 2009 Prof. Hyesoon Kim

Spring 2011 Prof. Hyesoon Kim

Display Lists. Conceptually similar to a graphics file. In client-server environment, display list is placed on server

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

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

Building Models. Objectives. Introduce simple data structures for building polygonal models. OpenGL vertex arrays. Vertex lists Edge lists

Computer Graphics Seminar

CS 432 Interactive Computer Graphics

The Application Stage. The Game Loop, Resource Management and Renderer Design

Graphics Processing Unit Architecture (GPU Arch)

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

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

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

CSE 167. Discussion 03 ft. Glynn 10/16/2017

The Graphics Pipeline

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

Pipeline Operations. CS 4620 Lecture 14

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

GPU Architecture and Function. Michael Foster and Ian Frasch

Could you make the XNA functions yourself?

CSE 591/392: GPU Programming. Introduction. Klaus Mueller. Computer Science Department Stony Brook University

3 Meshes. Strips, Fans, Indexed Face Sets. Display Lists, Vertex Buffer Objects, Vertex Cache

Scanline Rendering 2 1/42

ECE 571 Advanced Microprocessor-Based Design Lecture 20

Rasterization Overview

Threading Hardware in G80

CPSC 436D Video Game Programming

Fast Interactive Sand Simulation for Gesture Tracking systems Shrenik Lad

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

CSE 591: GPU Programming. Introduction. Entertainment Graphics: Virtual Realism for the Masses. Computer games need to have: Klaus Mueller

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

Mali Developer Resources. Kevin Ho ARM Taiwan FAE

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Interaction. CSCI 420 Computer Graphics Lecture 3

The Rendering Pipeline (1)

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

CS 4620 Program 3: Pipeline

E.Order of Operations

OpenGL Performances and Flexibility

Ciril Bohak. - INTRODUCTION TO WEBGL

Introduction to Multicore architecture. Tao Zhang Oct. 21, 2010

CS427 Multicore Architecture and Parallel Computing

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

Performance OpenGL Programming (for whatever reason)

3D Graphics and OpenGl. First Steps

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

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4620/8626 Computer Graphics Spring 2014 Homework Set 1 Suggested Answers

Vertex Buffer Objects

Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

2.11 Particle Systems

Graphics Hardware. Instructor Stephen J. Guy

On-the-fly Vertex Reuse for Massively-Parallel Software Geometry Processing

Architectures. Michael Doggett Department of Computer Science Lund University 2009 Tomas Akenine-Möller and Michael Doggett 1

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

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

CS452/552; EE465/505. Review & Examples

Windowing System on a 3D Pipeline. February 2005

CS8803SC Software and Hardware Cooperative Computing GPGPU. Prof. Hyesoon Kim School of Computer Science Georgia Institute of Technology

Coding OpenGL ES 3.0 for Better Graphics Quality

PowerVR Hardware. Architecture Overview for Developers

Rendering Grass with Instancing in DirectX* 10

ECE 571 Advanced Microprocessor-Based Design Lecture 18

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

EECS 487: Interactive Computer Graphics

Real-Time Graphics Architecture

GPGPU introduction and network applications. PacketShaders, SSLShader

CS 381 Computer Graphics, Fall 2012 Midterm Exam Solutions. The Midterm Exam was given in class on Tuesday, October 16, 2012.

Graphics Performance Optimisation. John Spitzer Director of European Developer Technology

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

Graphics Programming

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

Lecture 25: Board Notes: Threads and GPUs

Lecture 13: OpenGL Shading Language (GLSL)

CS 428: Fall Introduction to. OpenGL primer. Andrew Nealen, Rutgers, /13/2010 1

Programmable GPUS. Last Time? Reading for Today. Homework 4. Planar Shadows Projective Texture Shadows Shadow Maps Shadow Volumes

Profiling and Debugging Games on Mobile Platforms

Current Trends in Computer Graphics Hardware

CSCI-GA Graphics Processing Units (GPUs): Architecture and Programming Lecture 2: Hardware Perspective of GPUs

Computer Graphics. Chapter 10 Three-Dimensional Viewing

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

From Brook to CUDA. GPU Technology Conference

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Antonio R. Miele Marco D. Santambrogio

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

Sign up for crits! Announcments

Interaction. CSCI 480 Computer Graphics Lecture 3

Optimisation. CS7GV3 Real-time Rendering

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

The Graphics Pipeline and OpenGL I: Transformations!

Graphics Architectures and OpenCL. Michael Doggett Department of Computer Science Lund university

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

COMS 4160: Problems on Transformations and OpenGL

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

Transcription:

CENG 477 Introduction to Computer Graphics Graphics Hardware and OpenGL

Introduction Until now, we focused on graphic algorithms rather than hardware and implementation details But graphics, without using specialized tools and/or hardware would simply be too slow for most applications We will now learn about how a GPU works and how to program it using a specific API: OpenGL The presented ideas should apply to other APIs such as Direct3D with some modifications CENG 477 Computer Graphics 2

Graphics Hardware (GH) GH is a set of components which implements the forward rendering pipeline at a chip level called GPU Modern GPUs are programmable GPUs are massively parallel (orders of magnitude more parallel than CPUs) GPUs change continuously mainly due to the demands of the video game industry Big players: AMD, Nvidia, Intel, Microsoft, Apple, Qualcomm, CENG 477 Computer Graphics 3

Graphics Processing Unit (GPU) How parallel are GPUs? Let s watch this demo: CENG 477 Computer Graphics 4

GPGPU As a result of this performance, GPUs are used in many tasks that are not related to graphics at all: Called GPGPU: General-purpose computing on GPU Nvidia developed the CUDA language for GPGPU OpenCL is supported by AMD, Nvidia, and Intel Nowadays, many computational intensive tasks are performed on the GPU: Image and video processing Analyzing big data Bioinformatics Optimization Machine learning, CENG 477 Computer Graphics 5

GPU Architecture GPUs are similar to CPUs in their building blocks (in fact they are somewhat simpler than CPUs): Some logic to decode the instruction to be performed Registers Arithmetic logic units (ALUs) Cache Memory But they are massively parallel: Data parallelism Pipeline parallelism CENG 477 Computer Graphics 6

GPU Parallelism What makes GPUs parallel? GPUs are SIMD architectures SIMD: Single instruction multiple data The same instruction is applied to thousands of data elements at the same time wikipedia.com CENG 477 Computer Graphics 7

GPU Parallelism This works well for independent tasks such as: Transforming vertices Computing shading for each fragment Ideal if the task is the same but the data is different: Data length Pipeline length 1 2 3 4 5 6 7 8 9 10 /2 /2 /2 /2 /2 /2 /2 /2 /2 /2 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 CENG 477 Computer Graphics 8

GPU vs CPU GPUs have a larger number of ALUs allowing for data parallelism: CENG 477 Computer Graphics 9

GPU vs CPU Let s compare a good GPU with a good CPU Intel i7-4790k Nvidia GTX 1060 Cores: 4 (8 threads) Cores: 1280 Clock: 4 4.4 GHz Clock: 1.5 1.7 GHz Power: 88W Power: 120W Memory BW: 25.6 GB/s Memory BW: 192 GB/s CENG 477 Computer Graphics 10

Overall GPU Architecture Application Data Assembler Vtx Thread Issue Setup / Rstr / ZCull Prim Thread Issue Frag Thread Issue Application Vertex assembly Vertex operations SP TF SP L1 SP TF SP L1 SP TF SP L1 SP TF SP L1 SP TF SP L1 SP TF SP L1 SP TF SP L1 SP TF SP L1 Thread Processor Primitive assembly Primitive operations Rasterization L2 L2 L2 L2 L2 L2 Fragment operations FB FB FB FB FB FB Framebuffer NVIDIA GeForce 8800 OpenGL Pipeline CENG 477 Computer Graphics 11

GPU Data Flow Model User Program Bus Geometry Processing Fragment Processing CPU GPU GPU Displayed Image Monitor VGA, DVI, HDMI, Framebuffer GPU CENG 477 Computer Graphics 12

User Program The user program is an OpenGL (or Direct3D) program which itself runs on the CPU Also initially all data is in the main system memory The user program is responsible to arbitrate the overall flow and send data to GPU: Open a window Manage user interaction (mouse, keyboard, etc.) Decide what to draw and when to draw Ask GPU to compile shaders (programs to be later run on the GPU) CENG 477 Computer Graphics 13

Opening a Window Opening a window for rendering is not part of OpenGL Each OS has a different mechanism There are some high-level APIs that simplify this process Perhaps the simplest of these APIs is GLUT You will learn GLFW in the recitation, which is simpler and better Buffers that we want CENG 477 Computer Graphics 14

Double Buffering Double buffering is a technique to avoid tearing Problem happens when drawing and displaying the same buffer CENG 477 Computer Graphics 15

Double Buffering To avoid such artifacts, we render to a back buffer and show that buffer only when drawing is complete (usually synchronized with monitor s refresh cycle) Windowed more requires a copy: oracle.com CENG 477 Computer Graphics 16

Double Buffering In fullscreen mode, only the video pointer is flipped: oracle.com CENG 477 Computer Graphics 17

Managing User Interaction The user may interact with the program through input devices: traditionally keyboard and mouse GLUT also simplifies this task by registering callbacks: CENG 477 Computer Graphics 18

Managing User Interaction Sample keyboard callback: CENG 477 Computer Graphics 19

Managing User Interaction Sample special key-press callback: CENG 477 Computer Graphics 20

Displaying/Resizing the Window Whenever a window is displayed or resized, certain settings (such as the viewport) may need to be updated: This function can also be registered by using GLUT: CENG 477 Computer Graphics 21

Displaying/Resizing the Window Here, we typically reset the viewport and transformation matrices: Window width and height Projection transform can be set by glortho or glfrustum. It is also possible to use gluperspective Combined modeling and viewing transform CENG 477 Computer Graphics 22

Rendering Each Frame Each frame must be redrawn from scratch! Again, we first register a callback for this task The registered function is automatically called by the windowing system whenever required: CENG 477 Computer Graphics 23

Rendering Each Frame We first clear all buffers, then render our frame, and finally swap buffers (remember double buffering): CENG 477 Computer Graphics 24

Animation If we have animation, we must make sure that the window system calls our display function continuously For that purpose, we register another callback: In this function, we simply ask our display function to be called during GLUT s main loop: Sets a flag so that our display function will be called CENG 477 Computer Graphics 25

Sending Geometry Data The user program must communicate the geometry information to the GPU A simple approach: glbegin(gl_lines); glvertex3f(x0, y0, z0); glvertex3f(x1, y1, z1); glend(); We tell GPU that we want to draw a line from (x 0, y 0, z 0 ) to (x 1, y 1, z 1 ) CENG 477 Computer Graphics 26

Sending Geometry Data Attributes besides position can be sent as well: glbegin(gl_lines); glcolor3f(1, 0, 0); // red glvertex3f(x0, y0, z0); glcolor3f(0, 1, 0); // green glvertex3f(x1, y1, z1); glend(); We tell GPU that we want to draw a line from (x 0, y 0, z 0 ) to (x 1, y 1, z 1 ) The endpoint colors are (1, 0, 0) and (0, 1, 0) CENG 477 Computer Graphics 27

Sending Geometry Data Triangles are similar: Every group of three vertices define a triangle Drawing two triangles: glbegin(gl_triangles); glvertex3f(x0, y0, z0); glvertex3f(x1, y1, z1); glvertex3f(x2, y2, z2); glend(); glbegin(gl_triangles); glvertex3f(x0, y0, z0); glvertex3f(x1, y1, z1); glvertex3f(x2, y2, z2); glvertex3f(x3, y3, z3); glvertex3f(x4, y4, z4); glvertex3f(x5, y5, z5); glend(); CENG 477 Computer Graphics 28

Sending Geometry Data With this approach m triangles require 3m vertex calls An improved method is to use triangle strips for meshes The first three vertices define the first triangle Every vertex afterwards defines a new triangle glbegin(gl_triangle_strip); glvertex3f(x0, y0, z0); glvertex3f(x1, y1, z1); glvertex3f(x2, y2, z2); glvertex3f(x3, y3, z3); glend(); 0 0, 1, 2 1 2 2, 1, 3 3 m triangles require m+2 vertex calls CENG 477 Computer Graphics 29

Winding Order Winding order determines the facing of a triangle Here both triangles are facing toward the viewer: glbegin(gl_triangle_strip); glvertex3f(x0, y0, z0); glvertex3f(x1, y1, z1); glvertex3f(x2, y2, z2); glvertex3f(x3, y3, z3); glend(); 0 0, 1, 2 1 2 2, 1, 3 3 CENG 477 Computer Graphics 30

Winding Order Winding order determines the facing of a triangle Here both triangles are facing away from the viewer: glbegin(gl_triangle_strip); glvertex3f(x0, y0, z0); glvertex3f(x2, y2, z2); glvertex3f(x1, y1, z1); glvertex3f(x3, y3, z3); glend(); 0 0, 2, 1 1 2 1, 2, 3 3 It is important to use a consisting winding order when drawing a mesh due to backface culling CENG 477 Computer Graphics 31

Graphics State Graphics APIs are state machines Various states are preserved until we change them In the example below, the color of each vertex is set to (0, 1, 0), that is green: glcolor3f(0, 1, 0) glbegin(gl_triangle_strip); glvertex3f(x0, y0, z0); glvertex3f(x1, y1, z1); glvertex3f(x2, y2, z2); glvertex3f(x3, y3, z3); glend(); CENG 477 Computer Graphics 32

Graphics State Below the first three vertices have the same color and normal The fourth vertex has a different color and normal: glcolor3f(0, 1, 0) glnormal3f(0, 0, 1) glbegin(gl_triangle_strip); glvertex3f(x0, y0, z0); glvertex3f(x2, y2, z2); glvertex3f(x1, y1, z1); glcolor3f(1, 0, 0) glnormal3f(1, 0, 1) glvertex3f(x3, y3, z3); glend(); CENG 477 Computer Graphics 33

Sending Geometry Data Previous examples send data in immediate mode Immediate mode is ineffiecient: A large model would require too many glvertex calls Each glvertex call is executed on the CPU and the corresponding data is sent to the GPU A better approach would be to send all vertex data to the GPU using a single call We use vertex arrays for that purpose CENG 477 Computer Graphics 34

Vertex Arrays There are several arrays such as vertex position array, vertex color array, vertex normal array, Below is an example of vertex position array: glvertexpointer(size, type, stride, pointer) Number of coordinates per vertex Pointer to vertex position data Type of each coordinate Byte offset between consecutive vertices You must enable an array before using it: glenableclientstate(gl_vertex_array) CENG 477 Computer Graphics 35

Vertex Arrays In modern OpenGL, these explicit attribute names are replaced by a generic attribute array function: glvertexattribpointer(index, size, type, normalized, stride, pointer) Array index Whether integer data should be normalized Pointer to vertex position data Number of coordinates per vertex Type of each coordinate Byte offset between consecutive vertices Don t forget to enable it: glenablevertexattribarray(index) CENG 477 Computer Graphics 36

Drawing with Vertex Arrays We use a single draw call to draw using vertex arrays: gldrawarrays(mode, first, count) Primitive type Starting index in the enabled arrays Number of indices to be rendered CENG 477 Computer Graphics 37

Drawing with Vertex Arrays gldrawarrays may still be inefficient as vertex attribute data must be repeated for each primitive gldrawelements is designed to solve this issue by using indices: gldrawelements(mode, count, type, indices) Primitive type Number of indices to be rendered Type of each index Pointer to indices CENG 477 Computer Graphics 38

Drawing with Vertex Arrays When using client-side vertex arrays, the vertex attribute data is copied from the system memory (user pointer) to the GPU memory at every draw call There is a better alternative, known as vertex buffers glvertexpointer(size, type, stride, pointer) copy at draw call System memory GPU memory CENG 477 Computer Graphics 39

Vertex Buffer Objects Previous methods required the data to be copied from the system memory to GPU memory at each draw Vertex Buffer Objects (VBOs) are designed to allow this copy to take place only one The copied data is reused at each draw CENG 477 Computer Graphics 40

Vertex Buffer Objects To use VBOs, we generate two buffers: Vertex attribute buffer (position, color, normal, etc.) Element array buffer (indices) GLuint vertexattribbuffer, indexbuffer; glgenbuffers(1, &vertexattribbuffer); glgenbuffers(1, &indexbuffer); CENG 477 Computer Graphics 41

Vertex Buffer Objects Next, we bind these buffers to locations that are meaningful for the GPU: glbindbuffer(gl_array_buffer, vertexattribbuffer); glbindbuffer(gl_element_array_buffer, indexbuffer) CENG 477 Computer Graphics 42

Vertex Buffer Objects We then ask the GPU to allocate memory for us and copy our data into this memory Attribute data size in bytes Attribute data pointer glbufferdata(gl_array_buffer, asize, aptr, GL_STATIC_DRAW); glbufferdata(gl_element_array_buffer, isize, iptr, GL_STATIC_DRAW); Index data size in bytes Index data pointer CENG 477 Computer Graphics 43

Vertex Buffer Objects Once this is done, the CPU data can safely be deleted: Attribute data size in bytes Attribute data pointer glbufferdata(gl_array_buffer, asize, aptr, GL_STATIC_DRAW); glbufferdata(gl_element_array_buffer, isize, iptr, GL_STATIC_DRAW); Index data size in bytes Index data pointer delete[] aptr; delete[] iptr; CENG 477 Computer Graphics 44

Vertex Buffer Objects Before drawing, we can specifying an offset into our buffers It is accomplished by the same function as before But this time, pointer indicates a byte offset into our buffer (similar for glcolorpointer, etc.) glvertexpointer(size, type, stride, pointer) Number of coordinates per vertex Offset to vertex position data Type of each coordinate Byte offset between consecutive vertices CENG 477 Computer Graphics 45

Vertex Buffer Objects Drawing is the same as before where index pointer is now also an offset to the element array buffer: gldrawelements(mode, count, type, indices) Primitive type Number of indices to be rendered Type of each index Offset to indices CENG 477 Computer Graphics 46

Vertex Buffer Objects The relevant buffers must still be enabled: glenableclientstate(gl_vertex_array) glenableclientstate(gl_color_array) Unfortunately, this is a very bad naming as it suggests clientside data is being used In modern OpenGL, these are replaced with: glenablevertexattribarray(0); glenablevertexattribarray(1); CENG 477 Computer Graphics 47

Vertex Buffer Objects Note that in glvertexpointer and gldrawelements the last parameter is sometimes treated as pointer and sometimes offset OpenGL makes this decision as follows: If a non-zero name is bound to GL_ARRAY_BUFFER, the last parameter glvertexpointer is treated as offset (otherwise pointer) If a non-zero name is bound to GL_ELEMENT_ARRAY_BUFFER, the last parameter gldrawelements is treated as offset (otherwise pointer) CENG 477 Computer Graphics 48

Performance Comparison Drawing an Armadillo model comprised of 212574 triangles at four distinct locations (resulting in a total of 850296 triangles): CENG 477 Computer Graphics 49

Performance Comparison On AMD Mobility Radeon HD4650 and at resolution 640x480: Using VBOs the frame rate was about 100 FPS Using client-side gldrawelements, the frame rate was about 20 FPS Therefore, almost all modern games use VBOs for drawing complex models CENG 477 Computer Graphics 50

Transformations in OpenGL In classic OpenGL, transformations are performed using three commands: These commands effect the current matrix Therefore the current matrix should be set as GL_MODELVIEW before calling these commands Note that angle is in degrees (not radians)! CENG 477 Computer Graphics 51

Transformations in OpenGL Transformations apply in the reverse order The command closest to the draw call takes effect first Here, the cube is first scaled, then rotated, and finally translated CENG 477 Computer Graphics 52

Transformations in OpenGL Transformations keep effecting the current matrix If you want to draw an object at the same position at each frame you need to reset the matrix to identity: Otherwise your object will quickly disappear! CENG 477 Computer Graphics 53

Transformations in OpenGL In OpenGL, we do not specify a camera position It is assumed that the camera is at (0, 0, 0) and looking down the negative z axis You can view a modelview transformation in two ways: Transform all objects drawn after the transformation by keeping the camera fixed Transform the camera (i.e. coordinate system) by the opposite transformations by keeping the objects fixed In reality, objects are transformed but both would produce the same result CENG 477 Computer Graphics 54

Transformations in OpenGL Assume we have an object at (0, 0, 4): v = y -w = -z u = x Apply glrotatef(90, 1, 0, 0) v = y -w = -z u = x Apply gltranslatef(0, 0, -5) v = y -w = -z u = x CENG 477 Computer Graphics 55

Transformations in OpenGL Now imagine applying the opposite to the camera: v = y -w = -z Apply gltranslatef(0, 0, 5) u = x u v v -w y -z y -w -z u x x Apply glrotatef(-90, 1, 0, 0) CENG 477 Computer Graphics 56

Transformations in OpenGL Now imagine applying the opposite to the camera: u v = y v -w = -z -w u = x New result Original result The object position w.r.t. the camera is exactly the same in these two cases CENG 477 Computer Graphics 57