2D Graphics Optimizations

Slides:



Advertisements
Similar presentations
Using 2D sprite with OpenGL 2003 team Koguyue. Overview Motivation and basic concepts Advantages with using OpenGL Basic requirements of implementation.
Advertisements

COMPUTER GRAPHICS CS 482 – FALL 2014 NOVEMBER 10, 2014 GRAPHICS HARDWARE GRAPHICS PROCESSING UNITS PARALLELISM.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CAP4730: Computational Structures in Computer Graphics Visible Surface Determination.
Computer Graphics Hardware Acceleration for Embedded Level Systems Brian Murray
Tools for Investigating Graphics System Performance
Automatic Generation of Parallel OpenGL Programs Robert Hero CMPS 203 December 2, 2004.
Ch 1 Intro to Graphics page 1CS 367 First Day Agenda Best course you have ever had (survey) Info Cards Name, , Nickname C / C++ experience, EOS experience.
Fluid Simulation using CUDA Thomas Wambold CS680: GPU Program Optimization August 31, 2011.
Sprite Batching and Texture Atlases Randy Gaul. Overview Batches Sending data to GPU Texture atlases Premultiplied alpha Note: Discussion on slides is.
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 19 Other Graphics Considerations Review.
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
CoreWall Prototype Presented by Arun EVL’s Tech Meeting 8/25/04.
CSE 381 – Advanced Game Programming Basic 3D Graphics
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Recursion, Complexity, and Sorting By Andrew Zeng.
NVTune Kenneth Hurley. NVIDIA CONFIDENTIAL NVTune Overview What issues are we trying to solve? Games and applications need to have high frame rates Answer.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Programming Video Games
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
CSE 380 – Computer Game Programming Player Controls & Scrolling Mega Man, by Capcom, released 1988.
Image Synthesis Rabie A. Ramadan, PhD 4. 2 Review Questions Q1: What are the two principal tasks required to create an image of a three-dimensional scene?
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
Outline Announcements: –HW II Idue Friday! Validating Model Problem Software performance Measuring performance Improving performance.
1 Programming and problem solving in C, Maxima, and Excel.
Image Fusion In Real-time, on a PC. Goals Interactive display of volume data in 3D –Allow more than one data set –Allow fusion of different modalities.
Best Practices for Multi-threading Eric Young Developer Technology.
Technology Literacy Unit 3: Software.
Sorting Mr. Jacobs.
Graphical Output Basic Images.
COMPUTER GRAPHICS CHAPTER 38 CS 482 – Fall 2017 GRAPHICS HARDWARE
- Introduction - Graphics Pipeline
Real-Time Soft Shadows with Adaptive Light Source Sampling
Complex Geometry Visualization TOol
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Frame Update, Level Representation & Graphics
Collision Detection Box-to-Box.
Simple Sorting Algorithms
Graphics on GPU © David Kirk/NVIDIA and Wen-mei W. Hwu,
Cache Memory Presentation I
Chapter 7 Sorting Spring 14
CS451Real-time Rendering Pipeline
TerraForm3D Plasma Works 3D Engine & USGS Terrain Modeler
CSCE 441: Computer Graphics Hidden Surface Removal
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm Design and Analysis (ADA)
Software and Hardware Circular Buffer Operations
Central Processing Unit
Static Image Filtering on Commodity Graphics Processors
Trying to avoid pipeline delays
1.1 The Characteristics of Contemporary Processors, Input, Output and Storage Devices Types of Processors.
Recall: ROM example Here are three functions, V2V1V0, implemented with an 8 x 3 ROM. Blue crosses (X) indicate connections between decoder outputs and.
Explaining issues with DCremoval( )
EE 312 Software Design and Implementation I
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Debugging Tools Tim Purcell NVIDIA.
Data Structures Unsorted Arrays
Data Structures & Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Computer Graphics Seminar 2018 Stanislav Belogrivov
Problems with Locks Andrew Whitaker CSE451.
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
Game Programming Algorithms and Techniques
Embedded System Development Lecture 12 4/4/2007
Software Testing.
Presentation transcript:

2D Graphics Optimizations

Today in Video Games

HW2 Questions Any questions about HW2?

Optimization Mentality What makes Quicksort so fast anyway? If you tried to come up with a sorting algorithm, you would probably start with Selection Sort Selection Sort O(n^2) Quicksort O(n log n)

Optimization Mentality Selection Sort algorithm: Look at each element of your array to find the minimal element. Put the minimal element at the end of the sorted array Repeat 4 8 5 6 7 2 1 3 9 0

Optimization Mentality Quicksort takes advantage of the transitive property of comparisons This extra insight is what makes quicksort fast Big-O notation is a measuring tool, NOT what makes the code fast.

Optimization Strategies Automated compiler optimizations Generate better code! Easy as flipping a switch Take better advantage of the hardware The memory cache! Data in native formats for GPU Reduce the amount of work done Organize your data in cells / trees Use geometric knowledge about your data

Optimizations What is the speed of drawing a game so far? (d+a) * s + d*b Roughly O(n) Even though O(n) is good, drawing can be so slow that we want to dramatically reduce it! Be wary of anything that has to run every frame

Drawing Optimization #1 Release Mode (Automated complier optimization)

Drawing Optimization #1 Have the compiler generate better code for you. On Visual Studio you use “Release Mode” On GCC (Mac and Linux) -O2 or -O3

Drawing Optimization #1

Drawing Optimization #1 Visual Studio's Release Mode has its own set of configurations GCC -O3 contains optimizations that break under common buggy code. -O2 is “safer” If you encounter bugs with optimizations turned on, it will be much harder to use a debugger Assembly knowledge helps a lot here!

Drawing Optimization #2 Don't Draw Offscreen Sprites (Take better advantage of hardware)

Drawing Optimization #2 Current implementation sends all sprites and background tiles to the graphics card Currently about 20,000 background tiles; 500 sprites Overhead from command based architecture Every command takes memory Every command uses the data bus CPU ↔ GPU On CPU, check if sprite is offscreen, if so, don't generate a command

Drawing Optimization #2 Sprites, camera are both Axis-Aligned Bounding Box AABB/AABB test If they don't intersect, then one box must be above, below, to the left, or to the right of the other box

Drawing Optimization #2 struct AABB { int x, y, w, h; }; boolean AABBIntersect(AABB box1, AABB box2) { // box1 to the right if (box1.x > box2.x + box2.w) { return false; } // box1 to the left if (box1.x + box1.w < box2.x) { // box1 below if (box1.y > box2.y + box2.h) { // box1 above if (box1.y + box1.h < box2.y) { return true;

Drawing Optimization #2 Do AABB test BEFORE each draw if (AABBIntersect(camera, sprite)) DrawSprite(sprite) else // Do Nothing

Drawing Optimization #3 Don't Process Backgrounds Offscreen At All (Avoid doing unnecessary work)

Drawing Optimization #3 We are still spending CPU time on every single background tile (all 20,000 or more) If we knew where to start and stop, we could avoid even looking at 90% of the tiles This does not need to be 100% accurate, it just needs to have no false negatives

Drawing Optimization #3

Drawing Optimization #3 For grids: tile_x = floor(camera_x / tile_width) tile_y = floor(camera_y / tile_height) For hex-grids: tile_x = floor(camera_x / tile_step) tile_y = floor(camera_y / tile_step) Bottom right coordinate needs extra offset!

Drawing Optimization #3

Drawing Optimization #4 Do the same thing, for sprites (Avoid doing unnecessary work)

Drawing Optimization #4 Unlike backgrounds, sprites move, so the exact same optimization doesn't work Create logical buckets on a grid that the sprites can move between. This is similar to “Bucket Sort”

Drawing Optimization #4

Drawing Optimization #4 Buckets should be big enough that there are at least tens of sprites per bucket Buckets should be small enough to exclude a significant percentage of the level During an update, you may need to re-bucketize the sprite

Drawing Optimization #5 Use the modern OpenGL API (Make better use of the hardware)

Drawing Optimization #5 The current implementation of DrawSprite() uses OpenGL “immediate mode”, from the early 90s. Extra slow! No parallelism Lots of extra copies on the CPU ↔ GPU There has been a better way since 2003, Vertex Buffer Objects (and Vertex Arrays in 1997)

Drawing Optimization #5 You build up a vertex buffer object each frame, copy it all over at the end, then draw it. glBufferData() - copies the data Likely with usage=GL_STREAM_DRAW glDrawArrays() - draws the verts Note: This requires all the sprites drawn in one call to be in a single texture

Drawing Optimization #5 Build a texture atlas a.k.a. sprite sheet Draw subsets of the images using texture coords

Summary Release Mode Don't draw things that are off screen Only process background tiles that are on screen, make background a regular grid if necessary. Only process sprites that are near the screen, put them into a regular grid. Use the modern OpenGL interface.

Next class is a lab! Bring your laptop!