gl_buffer_cache: Enable async downloads

This commit is contained in:
Ameer J 2023-08-13 21:39:50 -04:00
parent 48b87d64de
commit 9b112107e2
4 changed files with 34 additions and 9 deletions

View File

@ -146,8 +146,12 @@ StagingBufferMap BufferCacheRuntime::UploadStagingBuffer(size_t size) {
return staging_buffer_pool.RequestUploadBuffer(size); return staging_buffer_pool.RequestUploadBuffer(size);
} }
StagingBufferMap BufferCacheRuntime::DownloadStagingBuffer(size_t size) { StagingBufferMap BufferCacheRuntime::DownloadStagingBuffer(size_t size, bool deferred) {
return staging_buffer_pool.RequestDownloadBuffer(size); return staging_buffer_pool.RequestDownloadBuffer(size, deferred);
}
void BufferCacheRuntime::FreeDeferredStagingBuffer(StagingBufferMap& buffer) {
staging_buffer_pool.FreeDeferredStagingBuffer(buffer.index);
} }
u64 BufferCacheRuntime::GetDeviceMemoryUsage() const { u64 BufferCacheRuntime::GetDeviceMemoryUsage() const {

View File

@ -64,7 +64,9 @@ public:
[[nodiscard]] StagingBufferMap UploadStagingBuffer(size_t size); [[nodiscard]] StagingBufferMap UploadStagingBuffer(size_t size);
[[nodiscard]] StagingBufferMap DownloadStagingBuffer(size_t size); [[nodiscard]] StagingBufferMap DownloadStagingBuffer(size_t size, bool deferred = false);
void FreeDeferredStagingBuffer(StagingBufferMap& buffer);
void CopyBuffer(GLuint dst_buffer, GLuint src_buffer, void CopyBuffer(GLuint dst_buffer, GLuint src_buffer,
std::span<const VideoCommon::BufferCopy> copies, bool barrier = true); std::span<const VideoCommon::BufferCopy> copies, bool barrier = true);
@ -233,7 +235,7 @@ struct BufferCacheParams {
static constexpr bool NEEDS_BIND_STORAGE_INDEX = true; static constexpr bool NEEDS_BIND_STORAGE_INDEX = true;
static constexpr bool USE_MEMORY_MAPS = true; static constexpr bool USE_MEMORY_MAPS = true;
static constexpr bool SEPARATE_IMAGE_BUFFER_BINDINGS = true; static constexpr bool SEPARATE_IMAGE_BUFFER_BINDINGS = true;
static constexpr bool IMPLEMENTS_ASYNC_DOWNLOADS = false; static constexpr bool IMPLEMENTS_ASYNC_DOWNLOADS = true;
// TODO: Investigate why OpenGL seems to perform worse with persistently mapped buffer uploads // TODO: Investigate why OpenGL seems to perform worse with persistently mapped buffer uploads
static constexpr bool USE_MEMORY_MAPS_FOR_UPLOADS = false; static constexpr bool USE_MEMORY_MAPS_FOR_UPLOADS = false;

View File

@ -28,19 +28,26 @@ StagingBuffers::StagingBuffers(GLenum storage_flags_, GLenum map_flags_)
StagingBuffers::~StagingBuffers() = default; StagingBuffers::~StagingBuffers() = default;
StagingBufferMap StagingBuffers::RequestMap(size_t requested_size, bool insert_fence) { StagingBufferMap StagingBuffers::RequestMap(size_t requested_size, bool insert_fence,
bool deferred) {
MICROPROFILE_SCOPE(OpenGL_BufferRequest); MICROPROFILE_SCOPE(OpenGL_BufferRequest);
const size_t index = RequestBuffer(requested_size); const size_t index = RequestBuffer(requested_size);
OGLSync* const sync = insert_fence ? &allocs[index].sync : nullptr; OGLSync* const sync = insert_fence ? &allocs[index].sync : nullptr;
allocs[index].sync_index = insert_fence ? ++current_sync_index : 0; allocs[index].sync_index = insert_fence ? ++current_sync_index : 0;
allocs[index].deferred = deferred;
return StagingBufferMap{ return StagingBufferMap{
.mapped_span = std::span(allocs[index].map, requested_size), .mapped_span = std::span(allocs[index].map, requested_size),
.sync = sync, .sync = sync,
.buffer = allocs[index].buffer.handle, .buffer = allocs[index].buffer.handle,
.index = index,
}; };
} }
void StagingBuffers::FreeDeferredStagingBuffer(size_t index) {
allocs[index].deferred = false;
}
size_t StagingBuffers::RequestBuffer(size_t requested_size) { size_t StagingBuffers::RequestBuffer(size_t requested_size) {
if (const std::optional<size_t> index = FindBuffer(requested_size); index) { if (const std::optional<size_t> index = FindBuffer(requested_size); index) {
return *index; return *index;
@ -68,6 +75,9 @@ std::optional<size_t> StagingBuffers::FindBuffer(size_t requested_size) {
if (buffer_size < requested_size || buffer_size >= smallest_buffer) { if (buffer_size < requested_size || buffer_size >= smallest_buffer) {
continue; continue;
} }
if (alloc.deferred) {
continue;
}
if (alloc.sync.handle != 0) { if (alloc.sync.handle != 0) {
if (alloc.sync_index >= known_unsignaled_index) { if (alloc.sync_index >= known_unsignaled_index) {
// This fence is later than a fence that is known to not be signaled // This fence is later than a fence that is known to not be signaled
@ -138,8 +148,12 @@ StagingBufferMap StagingBufferPool::RequestUploadBuffer(size_t size) {
return upload_buffers.RequestMap(size, true); return upload_buffers.RequestMap(size, true);
} }
StagingBufferMap StagingBufferPool::RequestDownloadBuffer(size_t size) { StagingBufferMap StagingBufferPool::RequestDownloadBuffer(size_t size, bool deferred) {
return download_buffers.RequestMap(size, false); return download_buffers.RequestMap(size, false, deferred);
}
void StagingBufferPool::FreeDeferredStagingBuffer(size_t index) {
download_buffers.FreeDeferredStagingBuffer(index);
} }
} // namespace OpenGL } // namespace OpenGL

View File

@ -26,13 +26,16 @@ struct StagingBufferMap {
size_t offset = 0; size_t offset = 0;
OGLSync* sync; OGLSync* sync;
GLuint buffer; GLuint buffer;
size_t index;
}; };
struct StagingBuffers { struct StagingBuffers {
explicit StagingBuffers(GLenum storage_flags_, GLenum map_flags_); explicit StagingBuffers(GLenum storage_flags_, GLenum map_flags_);
~StagingBuffers(); ~StagingBuffers();
StagingBufferMap RequestMap(size_t requested_size, bool insert_fence); StagingBufferMap RequestMap(size_t requested_size, bool insert_fence, bool deferred = false);
void FreeDeferredStagingBuffer(size_t index);
size_t RequestBuffer(size_t requested_size); size_t RequestBuffer(size_t requested_size);
@ -44,6 +47,7 @@ struct StagingBuffers {
u8* map; u8* map;
size_t size; size_t size;
size_t sync_index; size_t sync_index;
bool deferred;
}; };
std::vector<StagingBufferAlloc> allocs; std::vector<StagingBufferAlloc> allocs;
GLenum storage_flags; GLenum storage_flags;
@ -88,7 +92,8 @@ public:
~StagingBufferPool() = default; ~StagingBufferPool() = default;
StagingBufferMap RequestUploadBuffer(size_t size); StagingBufferMap RequestUploadBuffer(size_t size);
StagingBufferMap RequestDownloadBuffer(size_t size); StagingBufferMap RequestDownloadBuffer(size_t size, bool deferred = false);
void FreeDeferredStagingBuffer(size_t index);
private: private:
StagingBuffers upload_buffers{GL_MAP_WRITE_BIT, GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT}; StagingBuffers upload_buffers{GL_MAP_WRITE_BIT, GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT};