From c6055affbf18d7d03cf318c008ac939669e1c794 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 15 Oct 2024 18:16:41 +1000 Subject: [PATCH] OpenGLDevice: Fix invalidating pipeline cache on Windows --- src/util/opengl_pipeline.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/util/opengl_pipeline.cpp b/src/util/opengl_pipeline.cpp index b812a78db..e84ae59eb 100644 --- a/src/util/opengl_pipeline.cpp +++ b/src/util/opengl_pipeline.cpp @@ -757,12 +757,12 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error) { DebugAssert(!m_pipeline_disk_cache_file); - m_pipeline_disk_cache_file = FileSystem::OpenCFile(path.c_str(), "r+b", error); - if (!m_pipeline_disk_cache_file) + std::FILE* fp = FileSystem::OpenCFile(path.c_str(), "r+b", error); + if (!fp) return false; // Read footer. - const s64 size = FileSystem::FSize64(m_pipeline_disk_cache_file); + const s64 size = FileSystem::FSize64(fp); if (size < static_cast(sizeof(PipelineDiskCacheFooter)) || size >= static_cast(std::numeric_limits::max())) { @@ -771,10 +771,11 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error) } PipelineDiskCacheFooter file_footer; - if (FileSystem::FSeek64(m_pipeline_disk_cache_file, size - sizeof(PipelineDiskCacheFooter), SEEK_SET) != 0 || - std::fread(&file_footer, sizeof(file_footer), 1, m_pipeline_disk_cache_file) != 1) + if (FileSystem::FSeek64(fp, size - sizeof(PipelineDiskCacheFooter), SEEK_SET) != 0 || + std::fread(&file_footer, sizeof(file_footer), 1, fp) != 1) { Error::SetStringView(error, "Invalid cache file footer."); + std::fclose(fp); return false; } @@ -790,15 +791,16 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error) 0) { Error::SetStringView(error, "Cache does not match expected driver/version."); + std::fclose(fp); return false; } m_pipeline_disk_cache_data_end = static_cast(size) - sizeof(PipelineDiskCacheFooter) - (sizeof(PipelineDiskCacheIndexEntry) * file_footer.num_programs); - if (m_pipeline_disk_cache_data_end < 0 || - FileSystem::FSeek64(m_pipeline_disk_cache_file, m_pipeline_disk_cache_data_end, SEEK_SET) != 0) + if (m_pipeline_disk_cache_data_end < 0 || FileSystem::FSeek64(fp, m_pipeline_disk_cache_data_end, SEEK_SET) != 0) { Error::SetStringView(error, "Failed to seek to start of index entries."); + std::fclose(fp); return false; } @@ -806,11 +808,12 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error) for (u32 i = 0; i < file_footer.num_programs; i++) { PipelineDiskCacheIndexEntry entry; - if (std::fread(&entry, sizeof(entry), 1, m_pipeline_disk_cache_file) != 1 || + if (std::fread(&entry, sizeof(entry), 1, fp) != 1 || (static_cast(entry.offset) + static_cast(entry.compressed_size)) >= size) { Error::SetStringView(error, "Failed to read disk cache entry."); m_program_cache.clear(); + std::fclose(fp); return false; } @@ -818,6 +821,7 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error) { Error::SetStringView(error, "Duplicate program in disk cache."); m_program_cache.clear(); + std::fclose(fp); return false; } @@ -832,6 +836,7 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error) } VERBOSE_LOG("Read {} programs from disk cache.", m_program_cache.size()); + m_pipeline_disk_cache_file = fp; return true; }