Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Entries in this blog


Resources handling - part 2

To recap from previous post:
1) [color=rgb(40,40,40)][font=arial]There's a central hub that I call ResourceHub (resHub) that acts as the main interface from the app to the resources.[/font][/color]
[color=rgb(40,40,40)][font=arial]2) You can add to the hub many ResFamily (like shape family, image family, shader family and so on)[/font][/color]
[color=rgb(40,40,40)][font=arial]3) Every family can have many ResProvider which are used to load and to "specialize" a resource[/font][/color]

So, when it's time to add a resource, all you have to do is to call resHub->addResource (family, resourceName, &resID) in order to obtain an unique resource ID that will identify this resource (and all its specializations) forever.

The key point here is the word "forever". You have no way to remove a resource once it's added. This may sounds a little strange, but it has to do with the lifetime management of the resource and its ownership.

First of all, an unloaded resource, once added, has a memory footprint of only 8 bytes (plus the resource's name string which is usually short, something like 8 - 12 characters as it does not include any path or extension).
I call this 8 byte the "resource header".
Resource headers are stored in tables of 256 headers each (named TableU8). A single table needs 8 * 256 bytes = 2KB which is not that much.
A resource provider, store resource headers in a table of tables (named TableU16), which is a table that can hold a max of 256 TableU8.
The maximum number of resources that a single provider can hold is 256 * 256 = 65536, which is a not bad for a game I guess.
This limit is also limiting the number of resources you can add to a family.
In short: you can have no more than 65536 textures, 65536 shaders, 65536 materials, 65536 whatever.

Usually you don't need that much available at the same time, so TableU16 can load/unload a whole TableU8 at any time, just like a pagination algorithm.
When you add a new family, TableU16 will allocate only one TableU8 (ie 2KB) and will allocate further TableU8 only when needed. It will also deallocate any TableU8 that contains only unloaded resources that are not used anymore (more on this later).

So, keeping any added resources forever will not cost that much in memory, giving that a single resource header need only 8 bytes and that resources are paginated in blocks of 256 headers.

What comes handy, is that once you obtain a resourceID, you can use it forever, at anytime. So maybe you have added a bunch of textures for your terrain and than you warped 10.000 km away and have to load a whole new set of textures... well, going back to the starting point (maybe after 10 minutes), will not require you to add again the textures and the meshes, you already have all the resourceID that you need.
In fact, you could even add all the textures/meshes you will ever need for your terrain, get the resourceID and then forget about it and keep on using only the resourceID. The ResourceSystem will take care of loading/unloading every resource.

So, who own a resource? Resource ownership is always a delicate argument. There's a very good post from swiftcoder that gives you a nice introduction; you can find the post here
My solution is: the ResourceHub own the resource, and is the only responsible for loading/freeing it. Actually it's the family inside the hub that own the resource, but from a user point of view, the magic is inside the hub.
So you can not delete a resource, you can't even free or unload it. You can maybe give hints to the hub, but he will decide on what to do.

When you need access to a resource, you call resHub->getResource (resID) and the hub will give you back a status and a pointer.
The status tell you if the resource is loaded or not. If it's loaded, you can use the pointer, otherwise you can't.
The next time you'll need that resource, you'll do the same thing, call getResource() and check the status. If a resource is not loaded, calling getResource() will return a status of "loading" and will schedule an asynchronous load so it's quite possible that the first time you call getResource() you will recevice a "loading" status, and the second/third time you will receive a "loaded" status.

Internally the hub use a Last Recently Used (LRU) schema to keep tracks of used/unused resources.
Any resource lies in one of 8 LRU level . Level 0 is the most recently used, level 7 is the last.
It's your responsibility to call resHub->onLRUTick() once every 1 second or so; it will "move" resources from Level 0 to level 1, from level 1 to level 2 and so on. Once a resource hits level 7, it will unloaded and will stay there on level 7.
A TableU8 that holds only resources at level 7, will be unloaded as it contains only unused resources.

Calling getResource() will always move a resource from whatever LRU level actually lies, to LRU level 0, just to indicate that this resource has been used very recently so it should not be unloaded very soon.

This is how the hub can automatically unload unused resource and free some memory.

From a performace point of view, the whole LRU thing is highly optimized and does not involve "memcpying resources from level to level" which will surely be a bad thing to do once every second...
Also, "moving" a resource to level0 is just a matter of setting a byte in the resource header so it's not that costly, since you have to access the resource header anyway in order to getResource() and retrieve the resource status.

So far so good, it's seems to work pretty well but, I admit, I still have to stress it to see how really performs under heavy load. It will take sometimes before I can stress test it, I'm now involved in writing with the GUI library which does not need much resources to work.
I've also to refine and simplify some aspects of the main interface, but I'm satisfied of the results so far.

See you next time




Resources handling - part 1

During the years, I've developed many resources handling systems; none of them have survived.
At first, every new systems looked like the right one, the smartest ever.
After a few weeks of real use, they all had shown their weakness.
So, for the new engine, I wrote a brand new resources handling system.
First, what is a resource? To me, a resource is something that:
1) can be loaded, unloaded and re-loaded at any time, without having to always specify some fancy parameters. I want to define a resource only once and, from there on, be able to refer to it with a simple handle.
2) can be shared. This means that a texture for example, can be used by model1, model2, and whoever needs to. Obviously, the resource is unique, so it should not be loaded/create twice.
3) when I need it, I immediately get it. No wait time at all. I call getResource("name") and I want a result now. This does not means that the resource will be always immediately available, it could be not loaded for example, but it does not matter. I want immediately its unique handle and a status that tell me if the resource is ready or not
4) can be updated at runtime. Once the resource is updated, everybody see it in its new updated state. So if I update a texture, then model1 and model2, both see the updated texture without any special handling needed.
4a) as a bonus, if a resource is updated and then unloaded, next time it's loaded it should be loaded in its updated state
5) it can have sub-resources which are normal resources that are loaded along with the master resource. For example, if a material resource has 2 textures, when I load the material, both the textures should be also loaded, without any special handling needed.

Looking at the list of requisite, it's clear why I failed with all my previous attempts; it's not very easy to fulfill this goals altogether.

The resources system I'm developing for the current engine, seems a good candidate. I finished developing just 3 days ago, and so far it worked fine. It's still a work in progress and probably will be adjusted in the future, but I can already see the power in it (muahahahah).

How does it work?
There's a central hub that I call resourceHub (resHub) that acts as the main interface from the app to the resources.
No, it's not a singleton, nor a global. Ideally you can have as many hub as you want, they wont interact each other.
The resHub can have many families. Every family, can have many providers.
//family shaperesHub.addFamily ("shape", 4, &famShape);resHub.addProvider (famShape, geom::shape::Provider::InitParams(), &providerShape_gosgeom);//family imageresHub.addFamily ("image", 128, &famImage);resHub.addProvider (famImage, image::Provider::InitParams(), &providerImage_gosimage);resHub.addProvider (famImage, gpu::ImageProvider::InitParams(gpu, providerImage_gosimage), &providerImage_gpuimage);//family shaderresHub.addFamily ("shader", 128, &famShader)resHub.addProvider (famShader, gpu::TextShaderProvider::InitParams(), &providerShader_txt);
A bit scary isn't it?
What this code does, is to add 3 families to the hub: family shape, family image and family shader
Then it adds a geom::shape::Provider to the shape family, adds a image::Provider and a gpu::Image::Provider to the image family and so on.

What this means, is that an image resource can exists in 2 different formats: a image format, and a gpu::Image format.
A shape resource, can exists only in a geom::shape format (but you can add more providers later if you need additional formats).
More on formats later...

When it's time to actually add a real resource, I use the resHub this way:[code=:0]ResID resID;resHub.addResource ("shape", "cubo1x1x1", &resID);resHub.addResource (famShape, "plane1x2x2", &resID);resHub.addResource (famShape, "cubeTassellated2x3x4x2", &resID);resHub.addResource ("shape", "sphere3x2", &resID);//add texture resourcesresHub.addResource ("image", "checker", &resID);resHub.addResource (famImage, "checker_512", &resID);resHub.addResource (famImage, "checker_256", &resID);
addResource() wants a family (either a name or a familyID obtained from the addFamily), a "resource name" and fill a resID with the resource unique id.
The resource name is also (part) of the filename that a provider will use to try to load the resource itself.
The provider will typically add one or more extensions to the filename.
Take as an example the texture "checker_512". If an image::Provider is asked to load it, it will look for a file named "checker_512.image.gosimage".
The same resource but with the gpu::image::Provider, will result in a "checker_512.image.gpu" filename.
Generally speaking, a resource filename is composed by resource_name.resource_family.provider_parameters.

Whenever I need to use a resource, I will[code=:0]u8 *shaderBuffer;gos::eResStatus s = resHub->getResource (shaderResID, resProviderID_gpuShader, &shaderBuffer);if (s == gos::eResStatus_loaded){ gpuShader = (gpu::Shader*)shaderBuffer; return;}
getResource() wants a resourceID (obtained from addResource()), a providerID (obtained from addProvider()) and a pointer to a buffer that will eventually points to the loaded resource.

getResource() returns the resource state, that can be one of the following:[code=:0]eResStatus_notLoaded = 1,eResStatus_loading = 2,eResStatus_loaded = 3,eResStatus_loadError = 4, //if file not found during loadeResStatus_noHope = 5 //no way to load it, for example if file is missing
If eResStatus_notLoaded, getResource() will automatically start an async load and return eResStatus_loading.
If eResStatus_loading, you can't use the resource, the resBuffer will be NULL, but you know that the resource is being loaded.
If eResStatus_loaded, you're done, the resBuffer will point to valid data and you are free to use the resource as you wish. You are guarantee that the resource will stay loaded until the next sync-point (more on this later)
If eResStatus_loadError, a load() started previously failed. At this point you know there's no way to load it from the HD, so you can eventually manually create it and update() with valid data (more on this later..again), or mark it as noHope which will prevent any further load() to occur.

The key point here, is that with the same resource ID (which is an unsigned 32 bit), I can ask for different formats.
For example:[code=:0]ResID resIDresHub.addResource ("image", "checker", &resID);//let's say that now resID = 12345, I can://ask for the gosimage formatresHub->getResource (resID, providerImage, ..)//and also the gpu.formatresHub->getResource (resID, providerGPUImage, ..)
This come handy in many way.
For example, the render queue takes a textureID as parameter.
Now, the gpu needs a gpu.image (which is a texture loaded in gpu memory and ready to be used by the gpu, ie: a ID3D11Texture2D*).
To get the gpu.image I call getResource (textureID, providerGPUImage).
If this fail and return eNotLoaded or eLoadError, I can load the "real" image (a dds file for example) by calling getResource (textureID, providerImage) and then, create the gpu.image using the "real" image just loaded.

The same ID, many formats.
This come handy with shader too. The same ID can reference a text file with the plain source code, or a pre-compiled binary, or a text file to be compiled with a set of #define and so on.

I find this feature very useful. The resource creation parameters, are sort of stored in the provider. Switching provider, will change the way a resource is viewed and/or created.

I think it's enough for now, way too many lines of text. See you next time with part 2





So, welcome to this journal.
My idea is to blog progress about the new incarnation of my own 3d engine.
I'm developing 3d engines since 2000 or so, not for living, not for hobby.
Well, it all started as an hobby project, but in the last 10 years I managed to "employ" my own engine in at least one or two real (read: paid) applications every year, so it's not a real hobbistic engine, but not even a rock-solid-state-of-the-art piece of code.

Back in 2000, it wasn't even a real engine...how could I define it...well....just a mess.
It's not easy to develop an engine, even if you do program as a job. Engines are complicated things, and experience is the only way you have to improve your design and you code skills.

So I'm (still) doing this for learning, and just for the plain fun of doing. Yes, I'm the one who write an engine, fill it with features, ship it for an applications or two and then, discard everything and restart from zero just because I don't like (anymore) the way the engine is organized, or the way it works.

The last engine was named GOS2013, even if I started coding it in late 2012. It should have been the final engine... obviously I'm now working on GOS2014, and just patching GOS2013 whenever someone reports a bug.
GOS 2013 was the first one with multithreading and cross platform support. I made some mistakes with it, and now I'm willing to take only what went good and restarting from there.

So, in the next days I will start reporting something about the way I'm structuring the new engine, and we'll see what GOS2014 will become.

Just before leaving, here are 2 screenshots of GOS2013 in action (model editor, and scene editor).



Sign in to follow this  
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!