FreshRSS

Normální zobrazení

Jsou dostupné nové články, klikněte pro obnovení stránky.
PředevčíremHlavní kanál

How would I implement multithreading to load in textures while displaying a loading screen in OpenGL?

So basically I'm stumped trying to make it so that when I load in a texture my entire application doesn't freeze. I have tried using futures and running the whole loading process on another thread, both to no avail.

Using futures I couldn't figure out why my program was still hanging after a loadTexture call had been made so I tried another approach which was to run all of my load calls in my scene on a separate thread. That didn't work either because it didn't have the opengl context of my main thread.

Does anyone know of any practical examples of multithreaded loading and passing said data back to the main thread after it loads the image from ssd -> ram to be loaded to the gpu?

I have looked all around but haven't seen any practical examples of multithreaded loading for opengl.

Below is my resourcemanager code, if you have an idea of how I could implement multithreaded rendering to it it would be greatly appreciated but I am mainly asking this question to be pointed in the right direction of learning resources that could lead me to solve this problem on my own.

Thanks for any help.

#include "ResourceManager.h"

// Instantiate static variables
std::map<std::string, Texture>    ResourceManager::Textures;
std::map<std::string, Shader>       ResourceManager::Shaders;


Shader ResourceManager::LoadShader(const char *vShaderFile, const char *fShaderFile, std::string name)
{
    Shaders[name] = Shader(vShaderFile, fShaderFile);
    return Shaders[name];
}

Shader& ResourceManager::GetShader(std::string name)
{
    return Shaders[name];
}

Texture ResourceManager::LoadTexture(const char *file, bool alpha, std::string name)
{
    Textures[name] = loadTextureFromFile(file, alpha);
    return Textures[name];
}

Texture& ResourceManager::GetTexture(std::string name)
{
    return Textures[name];
}

void ResourceManager::Clear()
{
    // (properly) delete all shaders    
    for (auto iter : Shaders)
        iter.second.Delete();
    // (properly) delete all textures
    for (auto iter : Textures)
        glDeleteTextures(1, &iter.second.ID);
}

Texture ResourceManager::loadTextureFromFile(const char *file, bool alpha)
{
    // create texture object
    Texture texture;
    if (alpha)
    {
        texture.Internal_Format = GL_RGBA;
        texture.Image_Format = GL_RGBA;
    }
    // load image
    int width, height, nrChannels;
    unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
    // now generate texture
    texture.Generate(width, height, data); //Does all opengl calls to generate texture
    // and finally free image data
    stbi_image_free(data);
    return texture;
}
```
❌
❌