i'm loading a texture in my imgui menu and it's causing some fps issues, i'm using…
- User
i'm loading a texture in my imgui menu and it's causing some fps issues, i'm using stb_image.h, and this function
bool LoadTextureFromFile(const char* filename, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height) { // Load from disk into a raw RGBA buffer int image_width = 0; int image_height = 0; unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4); if (image_data == NULL) return false;
// Create texture D3D11_TEXTURE2D_DESC desc; ZeroMemory(&desc, sizeof(desc)); desc.Width = image_width; desc.Height = image_height; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; desc.SampleDesc.Count = 1; desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc.CPUAccessFlags = 0; ID3D11Texture2D* pTexture = NULL; D3D11_SUBRESOURCE_DATA subResource; subResource.pSysMem = image_data; subResource.SysMemPitch = desc.Width * 4; subResource.SysMemSlicePitch = 0; g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); // Create texture view D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; ZeroMemory(&srvDesc, sizeof(srvDesc)); srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = desc.MipLevels; srvDesc.Texture2D.MostDetailedMip = 0; g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, out_srv); pTexture->Release(); *out_width = image_width; *out_height = image_height; stbi_image_free(image_data); return true;}
- Assistant
One possible reason for the FPS issues could be the loading of the texture itself. Loading a texture from disk can be a resource-intensive process, and if it is being done every frame or very frequently, it could impact performance.
One possible solution could be to load the texture only once, at the start of the application, and then reuse the already loaded texture in subsequent frames. Another option could be to use a texture cache, where recently loaded textures are stored in memory and reused if requested again.
Another possible reason could be the format and size of the texture being loaded. It is important to choose the appropriate format and size for the texture, based on the needs of the application. Using a larger texture than necessary or using a higher resolution than required can impact performance. Similarly, using a format that is not optimized for the application, or using an uncompressed format, could also impact performance.
In addition, it is important to ensure that the texture loading code is optimized and efficient, and that it is not unnecessarily blocking other parts of the application or the rendering pipeline. Profiling the code and identifying any bottlenecks or areas for optimization could help improve performance.
- User
Can you create me a texture cache so we have no impact on our fps
- Assistant
Sure, here's a basic implementation of a texture cache that should help improve performance:
#include <unordered_map> #include <string> std::unordered_map<std::string, ID3D11ShaderResourceView*> textureCache; bool LoadTexture(const std::string& filename, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height) { auto it = textureCache.find(filename); if (it != textureCache.end()) // Texture is already in cache { ID3D11ShaderResourceView* srv = it->second; srv->AddRef(); // Increase reference count *out_srv = srv; *out_width = srv->GetWidth(); *out_height = srv->GetHeight(); return true; } // Load texture from disk // ... // Create texture and shader resource view ID3D11Texture2D* pTexture = NULL; // ... g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); // ... g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, out_srv); // Store the texture in the cache textureCache[filename] = *out_srv; (*out_srv)->AddRef(); // Increase reference count // Cleanup pTexture->Release(); stbi_image_free(image_data); return true; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.