• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Aqua Costa

Resource and level streaming

6 posts in this topic

Hello, I'm trying to implement resource and level streaming in my engine.

 

And I would like some help/clarification:

 

1 - From what I've been reading every resource should be divided in fixed-size chunks and stored using a pool allocator. But how should the resources be divided? I need meshes and textures to be stored continuously so I can create GPU resources. The solution I found is to load the whole resource using a temporary allocator, create the GPU resource, store resource info in chunks, and clear the temporary allocator. But what if the resource info doesn't fit in a single chunk?

 

2 - Resources like materials are simply command groups (containing commands to bind textures to shader slots, etc), ideally they should be stored continuously in memory, but might not fit in a single chunk so how should I handle them? 

 

3 - Is there any way to know how many chunks the pool must be able to hold? Or simply wait until a chunk is freed when the pool is full?

 

4 - Should I separate resource chunks and level object chunks in two pools? Each level (or section) in my engine contains a list of resource packages that must be loaded before the level. The sizes of level objects vary so I guess the chunks size should be equal to the size of the largest object (which is usually small).

0

Share this post


Link to post
Share on other sites


1 - From what I've been reading every resource should be divided in fixed-size chunks and stored using a pool allocator. But how should the resources be divided? I need meshes and textures to be stored continuously so I can create GPU resources. The solution I found is to load the whole resource using a temporary allocator, create the GPU resource, store resource info in chunks, and clear the temporary allocator. But what if the resource info doesn't fit in a single chunk?
I disagree. Every resource with a RAM cost too high should be divided, and that's a big difference.

Strangely enough, I did have streaming support in the past. I don't have it now. Why? Because right now 2GiBs are becoming commonplace... on video cards. I once estimated I could load my whole game in RAM - all the levels - and it would still fit. So there was no chance to really tune the streaming methods. Latency on real world data is a different thing. I am surprised someone just waited for textures "to become visible" to load them - that would have been unacceptable for me even with async loading.

 


2 - Resources like materials are simply command groups (containing commands to bind textures to shader slots, etc), ideally they should be stored continuously in memory, but might not fit in a single chunk so how should I handle them? 

Define what are those things. Streaming textures might make sense but in my system the draw calls, the binding commands themselves take very little. I would be very surprised if they exceeded 512KiB. Don't look for problems at all costs, there might be no problem at all. But if you want to go ahead, run the numbers and focus where it counts.

 


3 - Is there any way to know how many chunks the pool must be able to hold? Or simply wait until a chunk is freed when the pool is full?

Generic pools are nonsense in my opinion. Consider streaming sounds for example: guaranteed to be consumed at an almost perfectly regular rate. In general, it is better to have small incremental releases rather than one hiccup when the pool runs out, even if the combined work happens to be more performance intensive.

 


4 - Should I separate resource chunks and level object chunks in two pools?

Divide as much as you need to exploit coherent, predictable behavior. Most out of core methods I've seen don't consider data to be all the same. They exploit locality of some sort in a data-specific way.
2

Share this post


Link to post
Share on other sites

1 - From what I've been reading every resource should be divided in fixed-size chunks and stored using a pool allocator. But how should the resources be divided? I need meshes and textures to be stored continuously so I can create GPU resources. The solution I found is to load the whole resource using a temporary allocator, create the GPU resource, store resource info in chunks, and clear the temporary allocator. But what if the resource info doesn't fit in a single chunk?

I disagree. Every resource with a RAM cost too high should be divided, and that's a big difference.
Strangely enough, I did have streaming support in the past. I don't have it now. Why? Because right now 2GiBs are becoming commonplace... on video cards. I once estimated I could load my whole game in RAM - all the levels - and it would still fit. So there was no chance to really tune the streaming methods. Latency on real world data is a different thing. I am surprised someone just waited for textures "to become visible" to load them - that would have been unacceptable for me even with async loading.


granted, the demand loading was mostly out of it being easier to implement (and works acceptably), even if it can't fully eliminate delays. I tried multi-threaded loading at one point, but then later found that some drivers (ATI/AMD) seem to break pretty hard with uploading via multiple threads (it was working ok with nVidia HW though), so I went back to this being single-threaded (well, along with some other parts of my engine proving to not really be sufficiently thread-safe in general).

one partial strategy used (for a fairly long time) is that many textures are split up into 2 resolution levels, with a lower resolution "base image", and higher resolution "alternate images". typically, the base image is loaded immediately, with the alternate images being delayed until there is time to load them (say, at first it loads a 128x128 version, followed later by a 512x512 or 1024x1024 version, and any normal/specular/luminance/... maps).

also, things may be pulled into memory and decoded at different times, with in some cases, data is pulled into RAM earlier, but not decoded and handed over to OpenGL or similar until it is referenced.


some of this is also related to my development of specialized codecs as well (though this is mostly more focused on things like video and similar).

video frames are typically only decoded when geometry using the texture is directly visible. this leads currently to an issue for I vs P frames, where it is a tradeoff of either wasting cycles decoding non-visible I-Frames, so that P-Frames can be decoded immediately if the video-texture comes back into view, or delaying decoding until an I-Frame has been seen.

as-is, it currently doesn't bother, leading potentially to a fraction of a second of garbled video when a texture first comes back into view.


2 - Resources like materials are simply command groups (containing commands to bind textures to shader slots, etc), ideally they should be stored continuously in memory, but might not fit in a single chunk so how should I handle them?

Define what are those things. Streaming textures might make sense but in my system the draw calls, the binding commands themselves take very little. I would be very surprised if they exceeded 512KiB. Don't look for problems at all costs, there might be no problem at all. But if you want to go ahead, run the numbers and focus where it counts.


I am not entirely sure what is meant by this.

in my case, materials are structures and most rendering is array-driven, and beyond this is mostly code directly making API calls.

 

3 - Is there any way to know how many chunks the pool must be able to hold? Or simply wait until a chunk is freed when the pool is full?

Generic pools are nonsense in my opinion. Consider streaming sounds for example: guaranteed to be consumed at an almost perfectly regular rate. In general, it is better to have small incremental releases rather than one hiccup when the pool runs out, even if the combined work happens to be more performance intensive.

 
yeah.

FWIW, it is possible to get reasonably fast allocation/freeing without needing to force everything into using fixed-size chunks.


one strategy is mostly using free-lists of various sizes of fixed-size items, then when a given size item is allocated or freed, we first either check its appropriate free-list, or return an item to that specific list size.

on allocation, if the given list is empty, then we may resort to different strategies, such as carving up an item from a bigger free-list, or falling back to a more generic allocation strategy (a personal preference here being, as-noted, bitmaps and cells).

one example breakdown of such an allocator could be (based on object size):
0-4093 bytes: objects are in terms of 1-257 x 16-byte cells;
4096-65535 bytes: objects are in terms of 1-257 x 256-byte cells;
much past this point, it makes more sense to fall back to a different allocation strategy (such as falling back to malloc and/or other OS facilities).

so, after we know the size range for an object to allocate, we may scan the bitmaps for a free span of the needed size, generally with a rover to help speed up the search.


4 - Should I separate resource chunks and level object chunks in two pools?

Divide as much as you need to exploit coherent, predictable behavior. Most out of core methods I've seen don't consider data to be all the same. They exploit locality of some sort in a data-specific way.


yeah.

main ones I think are either exploiting temporal access patterns (more generally), or spatial ones (such as for world data).


for example, for most world data, it is only really visible within a certain viewing radius.
it is fairly effective IME to simply drive the whole process effectively by distances.

some of this is mostly because unpacked voxel data can use fairly excessive amounts of RAM, and even RLE-packed still eats up a big chunk of RAM, and puts a bit of a strain on my memory-allocator performance as well (currently mostly for allocating/freeing buffers for visible chunks).


previously, audio data was showing up on memory dumps as using a lot of space (nowhere near voxel data though), so I ended up partly moving a lot of it to a customized audio codec (where it is generally kept compressed in RAM and decoded piecewise). currently, it isn't used nearly as much for on-disk storage though (though is used in conjunction with PAK files in a few cases, with a tool converting audio to the format and spitting out the PAK).


I might eventually do similar for textures (IOW: converting all the textures into a custom codec and putting them into a big PAK file or similar). probably the engine would read in the texture PAK files in advance (similar to the sound PAKs).
1

Share this post


Link to post
Share on other sites

Thanks for your answers.
 

for example, I do most of my general-purpose memory allocation via a custom heap-style allocator, rather than using pool-based or region-based allocators.


Using an heap (free list) allocator don't you have fragmentation problems? After a while of loading/unloading of different sizes the memory will most likely become fragmented, how do you deal with it?
 
 

 

1 - From what I've been reading every resource should be divided in fixed-size chunks and stored using a pool allocator. But how should the resources be divided? I need meshes and textures to be stored continuously so I can create GPU resources. The solution I found is to load the whole resource using a temporary allocator, create the GPU resource, store resource info in chunks, and clear the temporary allocator. But what if the resource info doesn't fit in a single chunk?

I disagree. Every resource with a RAM cost too high should be divided, and that's a big difference.
Strangely enough, I did have streaming support in the past. I don't have it now. Why? Because right now 2GiBs are becoming commonplace... on video cards. I once estimated I could load my whole game in RAM - all the levels - and it would still fit. So there was no chance to really tune the streaming methods. Latency on real world data is a different thing.

 


I would rather have a more flexible (streaming) solution than have to limit levels size.
 

I am surprised someone just waited for textures "to become visible" to load them - that would have been unacceptable for me even with async loading.


I agree. My plan is to be able to define areas in the world editor where streaming of other zones should start.

 

 

For games with large levels (open world), that use streaming, is it "ok" to limit the number of resources loaded to a fixed number? Bitsquid (for example) limits the number of units to 65k, is it usual to put limits in an engine like that? How should the limit be calculated or are there more dynamic solutions?

 

Having a maximum number of resources/world objects allows me to use linear arrays increasing simplicity and most likely performance.

 

EDIT: After thinking a bit about it limiting the number of resources shouldn't cause any issue since I would need less than 16 MiB to store the info of 1 million resources (hashed name, reference count and pointer to resource data), and it is highly unlikely that I'll ever need that many resources in memory simultaneously happy.png .

Edited by TiagoCosta
0

Share this post


Link to post
Share on other sites

Thanks for your answers.
 

for example, I do most of my general-purpose memory allocation via a custom heap-style allocator, rather than using pool-based or region-based allocators.


Using an heap (free list) allocator don't you have fragmentation problems? After a while of loading/unloading of different sizes the memory will most likely become fragmented, how do you deal with it?

 


for the most part, I don't (I don't really try to deal with it, but it doesn't really seem to be too big of an issue either, at least vs general memory usage).


however, after a while, one may observe a pattern:
for the most part, the various sizes of free-lists will become filled with various amounts of various-sized memory objects, and (assuming that leaks are not present), will often eventually tend towards stabilizing (few or no general-purpose allocations of new memory objects in this size range, though with large numbers of allocations/frees within a given size of free-list).

typically, the existence of memory objects of various sizes (allocated and available) tends to resemble a histogram (roughly following a Gaussian distribution), with very large numbers of small objects (< 4kB), followed by progressive decreases as sizes get larger, though often with a few spikes (for example, in my engine, there is typically a fairly big spike around 32kB, which is the natural size of an uncompressed voxel chunk). larger memory objects tend to be fairly sparse.

as-is, objects < 4kB comprise around 30-40% of the total memory use in my case, with 32kB chunks and occasional large objects making up most of the rest. of these small objects, the number of objects is roughly inversely related to size (with 0-15 and 16-31 byte objects having object-counts in the millions).

objects under 64kB in-general tend to represent the majority (around 80%) of total memory use in my engine.

above this tends mostly to be largish semi-permanent data structures (such as PAK-files, video files, region files, buffers for decoded video frames, the console buffer, ...), and occasionally temporary buffers. many of these tend to range between 100s of KB and into the MB range.

granted, these dumps were usually made in cases where the engine was low on memory (typically because the available 32-bit address space was pretty much used up...).


some specific types of small memory objects are also handled specially (namely things like cons-cells and boxed-values).
a cons cell is basically a pair of tagged-pointers (traditionally called 'car' and 'cdr'), and typically boxed values are things like boxed-longs and boxed-doubles. in the former case, the MM manages these specially (they have their own heap), and boxed-values in general use slab allocation (and lack individual headers). these are not counted in the above stats (which were for the general allocator), but likewise tend to have fairly high allocation counts (also in the millions).

most other things are mostly things like strings and various small structures and similar.


having all objects in a given free list be the same size also helps some.
if each object has to be an exact number of bytes, this creates a mess.
however, with a given number of cells, the same object may hold a certain range of memory-object sizes.

say, we have a 17x16-cell object:
it can hold an allocation anywhere from 240-256 bytes.

typically, after a little while, one then hits a plateau of the number of objects of that given size range.


fragmentation is potentially still an issue for larger objects, but luckily these are relatively uncommon.

a lot of this is likely to depend a lot on the specific engine though.
 

 

ADD (specifics):

while each free list contains fixed size objects, there are a fair number of free lists:

256 free-lists for small objects (in increments of 16 bytes);

256 free-lists for medium objects (in increments of 256 bytes);

...

 

small/medium objects, as noted, can have their background memory managed by by cells and bitmaps;

large objects are generally managed by sorted arrays (managed via binary searches and positional inserts).

 

there is actually a fairly involved set of algorithms responsible for dealing with the case where the relevant free-list is empty, and linearly scanning for a sufficient span of free cells fails to find free memory (it tries to reclaim memory from other free-lists, considers trying to expand the heap, ...).

 

in a few cases, "memory usage probes" are used by some logic to evaluate some decisions, like whether or not to load voxel-chunks, as well as temporarily reducing the view radius (to try to reduce memory pressure by forcing distant chunks to be unloaded). this basically means, for example, that if memory-usage exceeds 65% of maximum, then the view radius drops to 192 meters, and then to 128 meters at 70%.

 

this policy was itself fairly effective in helping reduce the problem the engine running out of memory.

 

note that the (combined) heap limit for 32-bit processes is currently limited to around 2GB (though this is partly subdivided between the different kinds of heap memory). the remaining address space is left for "pretty much everything else".

 

 

 

 

 

1 - From what I've been reading every resource should be divided in fixed-size chunks and stored using a pool allocator. But how should the resources be divided? I need meshes and textures to be stored continuously so I can create GPU resources. The solution I found is to load the whole resource using a temporary allocator, create the GPU resource, store resource info in chunks, and clear the temporary allocator. But what if the resource info doesn't fit in a single chunk?

I disagree. Every resource with a RAM cost too high should be divided, and that's a big difference.
Strangely enough, I did have streaming support in the past. I don't have it now. Why? Because right now 2GiBs are becoming commonplace... on video cards. I once estimated I could load my whole game in RAM - all the levels - and it would still fit. So there was no chance to really tune the streaming methods. Latency on real world data is a different thing.

 


I would rather have a more flexible (streaming) solution than have to limit levels size.

 


likewise.

then again, I am using voxels, which if handled naively can use up all the RAM within a fairly short view radius.

if the worlds were Quake style, you could probably actually fit a "pretty damn big world" in a single giant BSP, and would more likely run into floating-point precision issues before running into memory issues. not that this would necessarily be practical though (there are likely to be other limiting factors before this, namely performance and map-creation issues, and dividing the world into a regular grid would probably make more sense).


 

 

I am surprised someone just waited for textures "to become visible" to load them - that would have been unacceptable for me even with async loading.


I agree. My plan is to be able to define areas in the world editor where streaming of other zones should start.
 
 
For games with large levels (open world), that use streaming, is it "ok" to limit the number of resources loaded to a fixed number? Bitsquid (for example) limits the number of units to 65k, is it usual to put limits in an engine like that? How should the limit be calculated or are there more dynamic solutions?
 
Having a maximum number of resources/world objects allows me to use linear arrays increasing simplicity and most likely performance.
 
EDIT: After thinking a bit about it limiting the number of resources shouldn't cause any issue since I would need less than 16 MiB to store the info of 1 million resources (hashed name, reference count and pointer to resource data), and it is highly unlikely that I'll ever need that many resources in memory simultaneously happy.png .

 


yeah, as long as the limit is higher than what is likely to be needed, and doesn't pose some strict architectural limit (IOW: like say using 16-bits in a file-format for something that may need more bits later and can't be easily expanded), it typically doesn't matter too much.

Edited by cr88192
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0