@ -8,6 +8,7 @@
# include <fmt/format.h>
# include "common/assert.h"
# include "common/bit_util.h"
# include "common/common_types.h"
# include "video_core/renderer_vulkan/vk_scheduler.h"
@ -23,23 +24,24 @@ StagingBufferPool::StagingBufferPool(const Device& device_, MemoryAllocator& mem
StagingBufferPool : : ~ StagingBufferPool ( ) = default ;
StagingBufferRef StagingBufferPool : : Request ( size_t size , bool host_visibl e) {
if ( const std : : optional < StagingBufferRef > ref = TryGetReservedBuffer ( size , host_visibl e) ) {
StagingBufferRef StagingBufferPool : : Request ( size_t size , MemoryUsage usag e) {
if ( const std : : optional < StagingBufferRef > ref = TryGetReservedBuffer ( size , usag e) ) {
return * ref ;
}
return CreateStagingBuffer ( size , host_visibl e) ;
return CreateStagingBuffer ( size , usag e) ;
}
void StagingBufferPool : : TickFrame ( ) {
current_delete_level = ( current_delete_level + 1 ) % NUM_LEVELS ;
ReleaseCache ( true ) ;
ReleaseCache ( false ) ;
ReleaseCache ( MemoryUsage : : DeviceLocal ) ;
ReleaseCache ( MemoryUsage : : Upload ) ;
ReleaseCache ( MemoryUsage : : Download ) ;
}
std : : optional < StagingBufferRef > StagingBufferPool : : TryGetReservedBuffer ( size_t size ,
bool host_visibl e) {
StagingBuffers & cache_level = GetCache ( host_visibl e) [ Common : : Log2Ceil64 ( size ) ] ;
MemoryUsage usag e) {
StagingBuffers & cache_level = GetCache ( usag e) [ Common : : Log2Ceil64 ( size ) ] ;
const auto is_free = [ this ] ( const StagingBuffer & entry ) {
return scheduler . IsFree ( entry . tick ) ;
@ -58,7 +60,7 @@ std::optional<StagingBufferRef> StagingBufferPool::TryGetReservedBuffer(size_t s
return it - > Ref ( ) ;
}
StagingBufferRef StagingBufferPool : : CreateStagingBuffer ( size_t size , bool host_visibl e) {
StagingBufferRef StagingBufferPool : : CreateStagingBuffer ( size_t size , MemoryUsage usag e) {
const u32 log2 = Common : : Log2Ceil64 ( size ) ;
vk : : Buffer buffer = device . GetLogical ( ) . CreateBuffer ( {
. sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO ,
@ -76,10 +78,10 @@ StagingBufferRef StagingBufferPool::CreateStagingBuffer(size_t size, bool host_v
+ + buffer_index ;
buffer . SetObjectNameEXT ( fmt : : format ( " Staging Buffer {} " , buffer_index ) . c_str ( ) ) ;
}
MemoryCommit commit = memory_allocator . Commit ( buffer , host_visibl e) ;
const std : : span < u8 > mapped_span = host_visible ? commit . Map ( ) : std : : span < u8 > { } ;
MemoryCommit commit = memory_allocator . Commit ( buffer , usag e) ;
const std : : span < u8 > mapped_span = IsHostVisible( usage ) ? commit . Map ( ) : std : : span < u8 > { } ;
StagingBuffer & entry = GetCache ( host_visibl e) [ log2 ] . entries . emplace_back ( StagingBuffer {
StagingBuffer & entry = GetCache ( usag e) [ log2 ] . entries . emplace_back ( StagingBuffer {
. buffer = std : : move ( buffer ) ,
. commit = std : : move ( commit ) ,
. mapped_span = mapped_span ,
@ -88,12 +90,22 @@ StagingBufferRef StagingBufferPool::CreateStagingBuffer(size_t size, bool host_v
return entry . Ref ( ) ;
}
StagingBufferPool : : StagingBuffersCache & StagingBufferPool : : GetCache ( bool host_visible ) {
return host_visible ? host_staging_buffers : device_staging_buffers ;
StagingBufferPool : : StagingBuffersCache & StagingBufferPool : : GetCache ( MemoryUsage usage ) {
switch ( usage ) {
case MemoryUsage : : DeviceLocal :
return device_local_cache ;
case MemoryUsage : : Upload :
return upload_cache ;
case MemoryUsage : : Download :
return download_cache ;
default :
UNREACHABLE_MSG ( " Invalid memory usage={} " , usage ) ;
return upload_cache ;
}
}
void StagingBufferPool : : ReleaseCache ( bool host_visible ) {
ReleaseLevel ( GetCache ( host_visible ) , current_delete_level ) ;
void StagingBufferPool : : ReleaseCache ( MemoryUsage usag e) {
ReleaseLevel ( GetCache ( usag e) , current_delete_level ) ;
}
void StagingBufferPool : : ReleaseLevel ( StagingBuffersCache & cache , size_t log2 ) {