Skip to content

Commit

Permalink
Merge pull request #18217 from hrydgard/gles-simplify-disk-cache
Browse files Browse the repository at this point in the history
Simplify disk-cache-load on GLES as well, for the same reasons as #18216
  • Loading branch information
hrydgard committed Oct 3, 2023
2 parents 7c184a7 + fb4a1fb commit bd760b9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 95 deletions.
9 changes: 0 additions & 9 deletions GPU/GLES/GPU_GLES.cpp
Expand Up @@ -201,14 +201,6 @@ u32 GPU_GLES::CheckGPUFeatures() const {
return features;
}

bool GPU_GLES::IsReady() {
return shaderManagerGL_->ContinuePrecompile();
}

void GPU_GLES::CancelReady() {
shaderManagerGL_->CancelPrecompile();
}

void GPU_GLES::BuildReportingInfo() {
GLRenderManager *render = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);

Expand Down Expand Up @@ -238,7 +230,6 @@ void GPU_GLES::DeviceLost() {
// Simply drop all caches and textures.
// FBOs appear to survive? Or no?
// TransformDraw has registered as a GfxResourceHolder.
CancelReady();
fragmentTestCache_.DeviceLost();

GPUCommonHW::DeviceLost();
Expand Down
3 changes: 0 additions & 3 deletions GPU/GLES/GPU_GLES.h
Expand Up @@ -40,9 +40,6 @@ class GPU_GLES : public GPUCommonHW {
// This gets called on startup and when we get back from settings.
u32 CheckGPUFeatures() const override;

bool IsReady() override;
void CancelReady() override;

void GetStats(char *buffer, size_t bufsize) override;

void DeviceLost() override; // Only happens on Android. Drop all textures and shaders.
Expand Down
59 changes: 25 additions & 34 deletions GPU/GLES/ShaderManagerGLES.cpp
Expand Up @@ -821,7 +821,6 @@ Shader *ShaderManagerGLES::ApplyVertexShader(bool useHWTransform, bool useHWTess
}

vsCache_.Insert(*VSID, vs);
diskCacheDirty_ = true;
return vs;
}

Expand Down Expand Up @@ -860,7 +859,6 @@ LinkedShader *ShaderManagerGLES::ApplyFragmentShader(VShaderID VSID, Shader *vs,
// Still insert it so we don't end up spamming generation.
}
fsCache_.Insert(FSID, fs);
diskCacheDirty_ = true;
}

// Okay, we have both shaders. Let's see if there's a linked one.
Expand Down Expand Up @@ -1012,6 +1010,31 @@ bool ShaderManagerGLES::LoadCacheFlags(File::IOFile &f, DrawEngineGLES *drawEngi
}

bool ShaderManagerGLES::LoadCache(File::IOFile &f) {
// TODO: Get rid of this struct.
struct {
std::vector<VShaderID> vert;
std::vector<FShaderID> frag;
std::vector<std::pair<VShaderID, FShaderID>> link;

size_t vertPos = 0;
size_t fragPos = 0;
size_t linkPos = 0;
double start;

void Clear() {
vert.clear();
frag.clear();
link.clear();
vertPos = 0;
fragPos = 0;
linkPos = 0;
}

bool Done() {
return vertPos >= vert.size() && fragPos >= frag.size() && linkPos >= link.size();
}
} diskCachePending_;

u64 sz = f.GetSize();
f.Seek(0, SEEK_SET);
CacheHeader header;
Expand Down Expand Up @@ -1066,12 +1089,6 @@ bool ShaderManagerGLES::LoadCache(File::IOFile &f) {
diskCachePending_.link.emplace_back(vsid, fsid);
}

// Actual compilation happens in ContinuePrecompile(), called by GPU_GLES's IsReady.
diskCacheDirty_ = false;
return true;
}

bool ShaderManagerGLES::ContinuePrecompile(float sliceTime) {
auto &pending = diskCachePending_;
if (pending.Done()) {
return true;
Expand All @@ -1080,15 +1097,8 @@ bool ShaderManagerGLES::ContinuePrecompile(float sliceTime) {
PSP_SetLoading("Compiling shaders...");

double start = time_now_d();
// Let's try to keep it under sliceTime if possible.
double end = start + sliceTime;

for (size_t &i = pending.vertPos; i < pending.vert.size(); i++) {
if (time_now_d() >= end) {
// We'll finish later.
return false;
}

const VShaderID &id = pending.vert[i];
if (!vsCache_.ContainsKey(id)) {
if (id.Bit(VS_BIT_IS_THROUGH) && id.Bit(VS_BIT_USE_HW_TRANSFORM)) {
Expand All @@ -1114,11 +1124,6 @@ bool ShaderManagerGLES::ContinuePrecompile(float sliceTime) {
}

for (size_t &i = pending.fragPos; i < pending.frag.size(); i++) {
if (time_now_d() >= end) {
// We'll finish later.
return false;
}

const FShaderID &id = pending.frag[i];
if (!fsCache_.ContainsKey(id)) {
Shader *fs = CompileFragmentShader(id);
Expand All @@ -1137,11 +1142,6 @@ bool ShaderManagerGLES::ContinuePrecompile(float sliceTime) {
}

for (size_t &i = pending.linkPos; i < pending.link.size(); i++) {
if (time_now_d() >= end) {
// We'll finish later.
return false;
}

const VShaderID &vsid = pending.link[i].first;
const FShaderID &fsid = pending.link[i].second;
Shader *vs = nullptr;
Expand All @@ -1164,22 +1164,14 @@ bool ShaderManagerGLES::ContinuePrecompile(float sliceTime) {
return true;
}

void ShaderManagerGLES::CancelPrecompile() {
diskCachePending_.Clear();
}

void ShaderManagerGLES::SaveCache(const Path &filename, DrawEngineGLES *drawEngine) {
if (!diskCacheDirty_) {
return;
}
if (linkedShaderCache_.empty()) {
return;
}
INFO_LOG(G3D, "Saving the shader cache to '%s'", filename.c_str());
FILE *f = File::OpenCFile(filename, "wb");
if (!f) {
// Can't save, give up for now.
diskCacheDirty_ = false;
return;
}
CacheHeader header;
Expand Down Expand Up @@ -1213,5 +1205,4 @@ void ShaderManagerGLES::SaveCache(const Path &filename, DrawEngineGLES *drawEngi
fwrite(&fsid, 1, sizeof(fsid), f);
}
fclose(f);
diskCacheDirty_ = false;
}
27 changes: 0 additions & 27 deletions GPU/GLES/ShaderManagerGLES.h
Expand Up @@ -183,8 +183,6 @@ class ShaderManagerGLES : public ShaderManagerCommon {

bool LoadCacheFlags(File::IOFile &f, DrawEngineGLES *drawEngine);
bool LoadCache(File::IOFile &f);
bool ContinuePrecompile(float sliceTime = 1.0f / 60.0f);
void CancelPrecompile();
void SaveCache(const Path &filename, DrawEngineGLES *drawEngine);

private:
Expand Down Expand Up @@ -219,29 +217,4 @@ class ShaderManagerGLES : public ShaderManagerCommon {

typedef DenseHashMap<VShaderID, Shader *> VSCache;
VSCache vsCache_;

bool diskCacheDirty_ = false;
struct {
std::vector<VShaderID> vert;
std::vector<FShaderID> frag;
std::vector<std::pair<VShaderID, FShaderID>> link;

size_t vertPos = 0;
size_t fragPos = 0;
size_t linkPos = 0;
double start;

void Clear() {
vert.clear();
frag.clear();
link.clear();
vertPos = 0;
fragPos = 0;
linkPos = 0;
}

bool Done() {
return vertPos >= vert.size() && fragPos >= frag.size() && linkPos >= link.size();
}
} diskCachePending_;
};
15 changes: 3 additions & 12 deletions GPU/GPU.cpp
Expand Up @@ -53,14 +53,12 @@ static void SetGPU(T *obj) {
#endif

bool GPU_IsReady() {
if (gpu)
return gpu->IsReady();
return false;
return gpu != nullptr;
}

bool GPU_IsStarted() {
if (gpu)
return gpu->IsReady() && gpu->IsStarted();
return gpu->IsStarted();
return false;
}

Expand Down Expand Up @@ -112,7 +110,7 @@ bool GPU_Init(GraphicsContext *ctx, Draw::DrawContext *draw) {
#endif
}

if (gpu && gpu->IsReady() && !gpu->IsStarted())
if (gpu && !gpu->IsStarted())
SetGPU<SoftGPU>(nullptr);

return gpu != nullptr;
Expand All @@ -126,13 +124,6 @@ void GPU_Shutdown() {
// Reduce the risk for weird races with the Windows GE debugger.
gpuDebug = nullptr;

// Wait for IsReady, since it might be running on a thread.
if (gpu) {
gpu->CancelReady();
while (!gpu->IsReady()) {
sleep_ms(10);
}
}
delete gpu;
gpu = nullptr;
}
Expand Down
4 changes: 0 additions & 4 deletions GPU/GPUCommon.h
Expand Up @@ -78,13 +78,9 @@ class GPUCommon : public GPUInterface, public GPUDebugInterface {

virtual void UpdateCmdInfo() = 0;

bool IsReady() override {
return true;
}
bool IsStarted() override {
return true;
}
void CancelReady() override {}
void Reinitialize() override;

void BeginHostFrame() override;
Expand Down
2 changes: 0 additions & 2 deletions GPU/GPUInterface.h
Expand Up @@ -190,8 +190,6 @@ class GPUInterface {
virtual Draw::DrawContext *GetDrawContext() = 0;

// Initialization
virtual bool IsReady() = 0;
virtual void CancelReady() = 0;
virtual bool IsStarted() = 0;
virtual void Reinitialize() = 0;

Expand Down
4 changes: 0 additions & 4 deletions GPU/Vulkan/GPU_Vulkan.cpp
Expand Up @@ -422,10 +422,6 @@ void GPU_Vulkan::CheckRenderResized() {
}

void GPU_Vulkan::DeviceLost() {
CancelReady();
while (!IsReady()) {
sleep_ms(10);
}
// draw_ is normally actually still valid here in Vulkan. But we null it out in GPUCommonHW::DeviceLost so we don't try to use it again.
Draw::DrawContext *draw = draw_;
if (draw) {
Expand Down

0 comments on commit bd760b9

Please sign in to comment.