-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18538 from hrydgard/async-texture-load
Async texture load on Pause screen
- Loading branch information
Showing
8 changed files
with
162 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,81 @@ | ||
#pragma once | ||
|
||
#include <cstring> | ||
#include <string_view> | ||
#include <memory> | ||
|
||
#include "Common/GPU/thin3d.h" | ||
#include "Common/UI/View.h" | ||
#include "Common/File/Path.h" | ||
|
||
enum ImageFileType { | ||
enum class ImageFileType { | ||
PNG, | ||
JPEG, | ||
ZIM, | ||
DETECT, | ||
TYPE_UNKNOWN, | ||
UNKNOWN, | ||
}; | ||
|
||
class ManagedTexture { | ||
public: | ||
ManagedTexture(Draw::DrawContext *draw) : draw_(draw) { | ||
} | ||
~ManagedTexture() { | ||
if (texture_) | ||
texture_->Release(); | ||
class TextureLoadTask; | ||
class LimitedWaitable; | ||
|
||
// For UI images loaded from disk, loaded into RAM, generally staged for upload. | ||
// The reason for the separation is so that the image can be loaded and decompressed on a thread, | ||
// and then only uploaded to the GPU on the main thread. | ||
struct TempImage { | ||
~TempImage(); | ||
Draw::DataFormat fmt = Draw::DataFormat::UNDEFINED; | ||
ImageFileType type = ImageFileType::UNKNOWN; | ||
uint8_t *levels[16]{}; // only free the first pointer, they all point to the same buffer. | ||
int zimFlags = 0; | ||
int width[16]{}; | ||
int height[16]{}; | ||
int numLevels = 0; | ||
|
||
bool LoadTextureLevels(const uint8_t *data, size_t size, ImageFileType typeSuggestion = ImageFileType::DETECT); | ||
void Free() { | ||
if (levels[0]) { | ||
free(levels[0]); | ||
memset(levels, 0, sizeof(levels)); | ||
} | ||
} | ||
}; | ||
|
||
bool LoadFromFile(const std::string &filename, ImageFileType type = ImageFileType::DETECT, bool generateMips = false); | ||
bool LoadFromFileData(const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips, const char *name); | ||
// Managed (will auto-reload from file) and async. For use in UI. | ||
class ManagedTexture { | ||
public: | ||
ManagedTexture(Draw::DrawContext *draw, std::string_view filename, ImageFileType type = ImageFileType::DETECT, bool generateMips = false); | ||
~ManagedTexture(); | ||
Draw::Texture *GetTexture(); // For immediate use, don't store. | ||
int Width() const { return texture_->Width(); } | ||
int Height() const { return texture_->Height(); } | ||
|
||
void DeviceLost(); | ||
void DeviceRestored(Draw::DrawContext *draw); | ||
|
||
bool Failed() const { | ||
return state_ == LoadState::FAILED; | ||
} | ||
|
||
enum class LoadState { | ||
PENDING, | ||
FAILED, | ||
SUCCESS, | ||
}; | ||
|
||
private: | ||
void StartLoadTask(); | ||
|
||
Draw::Texture *texture_ = nullptr; | ||
Draw::DrawContext *draw_; | ||
std::string filename_; // Textures that are loaded from files can reload themselves automatically. | ||
bool generateMips_ = false; | ||
bool loadPending_ = false; | ||
ImageFileType type_ = ImageFileType::DETECT; | ||
TextureLoadTask *loadTask_ = nullptr; | ||
LimitedWaitable *taskWaitable_ = nullptr; | ||
TempImage pendingImage_; | ||
LoadState state_ = LoadState::PENDING; | ||
}; | ||
|
||
Draw::Texture *CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips, const char *name); | ||
Draw::Texture *CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips); | ||
|
||
std::unique_ptr<ManagedTexture> CreateManagedTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType fileType, bool generateMips); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.