Archive 17/01/2023.

Why does Urho load resource only in the main thread?

haolly

I find the code ResourceCache::GetResource(StringHash type, ...) checks the load happens in the main thread,
but I do not understand why it does this check, why Urho could not load resource in another thread

My knowledge tells me that use multiply thread will accelerate the progress when loading multiply resource
:confused:

spwork

see the document,you can use BackgroundLoadResource() load resource in background thread

SirNate0

As to why I’m pretty sure it’s just because various graphics resources (like textures) have to be uploaded to the GPU from the main thread, and that is a part of the loading process.

WangKai

There are two phases for background resource loading:

bool Resource::BeginLoad(Deserializer& source)
{
    // This always needs to be overridden by subclasses
    return false;
}

bool Resource::EndLoad()
{
    // If no GPU upload step is necessary, no override is necessary
    return true;
}

BeginLoad is on the background loading thread and EndLoad is executed on the main thread for the GPU resource creatation, since all graphics stuff are on the main thread.

We can use a rendering thread for all the GPU stuff, it’s another topic anyway.

Eugene
  1. Because there shall be locks to get an existing resource in multiple threads, so it would end up with deadlocks and multiple loadings of single resource. And other MT bullshit that nobody want to care about.

  2. Because not every resource could be loaded in worker threads.

haolly

This is the document in resource says,

Depending on the resource, only a part of the loading process may be moved to a background thread, for example the finishing GPU upload step always needs to happen in the main thread.

It seems that whether or not a resource could be loaded in background thread depends on the type of the process,
So, could I say that when loading any resource that does not involve GPU stuff, it could be a loading at background thread ?
but @Eugene 's words make me think that it depends on the type of the resource :disappointed_relieved:

Eugene

Yes. So it depends on the type of the resource.