• entries
3
2
• views
3365

# Resources handling - part 1

980 views

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...

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.

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.

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

There are no comments to display.

## Create an account

Register a new account