Texture preloading is a feature of Coherent GT that allows you to manually load and decode textures (which Coherent GT would normally do for you). Texture preloading lets clients to specify content rect where the actual image is. This can be used if the texture has padding or is part of a texture atlas.
This lets you achieve the following use cases:
Preloading is an advanced feature and requires several steps to get it working.
void PreloadTexture() { for (const auto& textureURL : texturesToPreload) { preloadedTextures[textureURL] = LoadTexture(textureURL); } }
void MyResourceHandler::OnResourceRead(const Coherent::UIGT::ResourceRequestUIGT* request, Coherent::UIGT::ResourceResponseUIGT* response) { std::string url = request->GetURL(); auto texture = preloadedTextures[url]; // This texture isn't loaded yet, let Coherent GT read the raw file. if (!texture) { ReadRawFile(request, response); return; } // Tell Coherent GT that the image is preloaded using ImageData = Coherent::UIGT::ResourceResponseUIGT::UserImageData; ImageData data; data.Width = texture->Width; data.Height = texture->Height; data.ContentRectX = 0; data.ContentRectY = 0; data.ContentRectWidth = data.Width; data.ContentRectHeight = data.Height; data.Format = texture->Format; data.Texture = texture->Resource; response->ReceiveUserImage(data); response->SignalSuccess(); response->Release(); }For example how content rect work check Sample - Preloaded resources.
ReadRawFile
is called for resources that the application assumes not preloaded.void MyBackend::WrapUserTexture(void* userObject, const Texture2D& description, Texture2DObject object) { // userObject contains whatever you passed to ResourceResponseUIGT::UserImageData::Texture auto textureResource = (MyTextureResource*) userObject; // Prepare the texture }When the texture is no longer needed,
MyBackend::DestroyTexture
will be called as it would for any other texture.See the Sample - Preloaded resources for a demonstration of the technique.