17
CSE 381 – Advanced Game Programming Loading & Caching Data

CSE 381 – Advanced Game Programming Loading & Caching Data

Embed Size (px)

Citation preview

Page 1: CSE 381 – Advanced Game Programming Loading & Caching Data

CSE 381 – Advanced Game ProgrammingLoading & Caching Data

Page 2: CSE 381 – Advanced Game Programming Loading & Caching Data

Real Games

• Have tens of thousands of “data files”

• Loading them one by one isn’t practical• might not fit into memory together• too much overhead loading individually

• What’s the alternative?• resource files

Page 3: CSE 381 – Advanced Game Programming Loading & Caching Data

Game Resources

• I.e. Game Assets• Game data

• Art, animations, sounds 3D meshes, map levels, etc

• Must be loaded small

• Must keep quality

• Formats platform dependent

Page 4: CSE 381 – Advanced Game Programming Loading & Caching Data

Using Resources

Source FilesArt: BMP, JPG, TGA, PNG, etc.

Sound: WAV, OGG, MP3, etc.

Meshes: X, COLLADA, etc.

Video: AVI, BIK, etc.

Game Resource FilesZIP, WAD, etc.

Pack

&

Compress

Game Application Layer

Game SubsystemsRenderer, Sound, Logic, etc.

ResourceCache

Get

Cached

Resource

Read

From

Disk

Page 5: CSE 381 – Advanced Game Programming Loading & Caching Data

Typical Game Estimates

• Total ~ Gigabytes of data

• Map/Level Data• small and easy to compress

• 3D Object Meshes & Environments• tens of megabytes for geometry

• 3D Mesh/Object Animation Data• tens of megabytes

• Sprite & Texture Data• hundreds of megabytes

• Sound, Music, Recorded Dialog• typically largest component

• Video & Pre-rendered Cinematics• most expensive

Page 6: CSE 381 – Advanced Game Programming Loading & Caching Data

Resource Loading Objective

• Get in and out as quickly as possible

• Minimize search time on disk

• How?• load large blocks together• should be things you’ll likely use together• order resource blocks carefully on disk according to use

• Ex: Group by Level• Read a Block of Data for Level 1• Level Blocks and Common (shared resource) blocks

Page 7: CSE 381 – Advanced Game Programming Loading & Caching Data

Packaging Resources into a Single File

• Avoids wasting space

• Makes loading faster

• Good for proprietary issues

• Store the data in the format you plan to use it

• In what format?• WAD• ZIP• Etc.

Page 8: CSE 381 – Advanced Game Programming Loading & Caching Data

ZLib

• http://www.zlib.net

• Free, open source

• DEFLATE compression algorithm

• Typical compression ratios are 2:1 to 5:1

• Can be used to compress many files into one file

• Table of contents stored at the end of the file

Page 9: CSE 381 – Advanced Game Programming Loading & Caching Data

Zip File Internal Structure

File 0

TZipLocalHeader

File Data

File 1

File 2

TZipDirFileHeader[0]TZipDirFileHeader[1]

TZipDirHeader

Page 10: CSE 381 – Advanced Game Programming Loading & Caching Data

TZipDirHeader

• One structure for each file in zip• Name of file• Type of compression• Size of file before & after compression

Page 11: CSE 381 – Advanced Game Programming Loading & Caching Data

The ZipFile Class

• How does it work?1. Opens a ZIP file2. Reads the directory into memory3. Uses the directory to index to the rest of the file

class ZipFile{ bool Init(wstring &resFileName)bool ReadFile(int i, void *pBuf)

FILE *m_pFile;char *m_pDirData;int m_nEntriesconst TZipDirFileHeader **m_papaDir;

Page 12: CSE 381 – Advanced Game Programming Loading & Caching Data

#pragma pack(1)?

• What’s that? Continuous ZIP file memory

• Put around structs TZipLocalHeader, TZipDirHeader, and TZipFileHeader

• Why?• disables some memory C++ optimization that might spread

them out

• So?• We want to stream them in large blocks

• Rule of thumb: Pack your structs anytime you define a struct that will be stored onto a disk or in a stream

Page 13: CSE 381 – Advanced Game Programming Loading & Caching Data

The Resource Cache

• Sits on top of your resource files

• Manages memory

• Manages loading

• Predict resource requirements before you use them• minimize cache misses

Page 14: CSE 381 – Advanced Game Programming Loading & Caching Data

Types of Cache Misses

• Compulsory Miss• happens when the desired data is first requested and loads for

first time

• Capacity Miss• happens when the cache is out of space and must throws

something out to load in the data

• Conflict Miss• system incorrectly determined data was no longer needed and

threw it out

• Poor Resource Cache management causes Thrashing

Page 15: CSE 381 – Advanced Game Programming Loading & Caching Data

GCC Resource Types

• Resource & ResHandle

• IResourceFile & ResourceZipFile

• IResourceLoader & DefaultResourceLoader

• ResCache

Page 16: CSE 381 – Advanced Game Programming Loading & Caching Data

A Simple Resource Cache

• ResCache

• Maintains 2 data structures• Linked list:

• Nodes appear in the order in which the resource was last used

• Every time a resource is used, move it to the front of the list

• Map• find resource

Page 17: CSE 381 – Advanced Game Programming Loading & Caching Data

World Design & Cache Prediction

• Options:• Load Everything at Once• Load Only at Pinch Points• Load Constantly