Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Implement GPU mappable buffers.

Skythe43dffa09840
diff --git a/drivers/d3d12/rendering_device_driver_d3d12.cpp b/drivers/d3d12/rendering_device_driver_d3d12.cpp
index 64313a1b144..1fa3c8285f5 100644
--- a/drivers/d3d12/rendering_device_driver_d3d12.cpp
+++ b/drivers/d3d12/rendering_device_driver_d3d12.cpp
@@ -913,6 +913,15 @@ RDD::BufferID RenderingDeviceDriverD3D12::buffer_create(uint64_t p_size, BitFiel
resource_desc.Flags = resource_desc.Flags & ~D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
} break;
+ case MEMORY_ALLOCATION_TYPE_GPU_MAPPABLE: {
+ if (misc_features_support.gpu_upload_heap_supported) {
+ allocation_desc.HeapType = D3D12_HEAP_TYPE_GPU_UPLOAD;
+ } else if (misc_features_support.uma_supported) {
+ allocation_desc.CustomPool = uma_gpu_mappable_pool.Get();
+ } else {
+ ERR_FAIL_V_MSG(BufferID(), "GPU mappable buffers are unsupported on this device.");
+ }
+ } break;
}
ComPtr<ID3D12Resource> buffer;
@@ -5890,6 +5899,8 @@ bool RenderingDeviceDriverD3D12::has_feature(Features p_feature) {
return false;
case SUPPORTS_HDR_OUTPUT:
return true;
+ case SUPPORTS_GPU_MAPPABLE_BUFFER:
+ return misc_features_support.uma_supported || misc_features_support.gpu_upload_heap_supported;
default:
return false;
}
@@ -6322,6 +6333,24 @@ Error RenderingDeviceDriverD3D12::_initialize_allocator() {
print_verbose("D3D12: Device does NOT support GPU UPLOAD heap. ReBAR must be enabled for this feature. Regular UPLOAD heaps will be used as fallback.");
}
+ misc_features_support.uma_supported = allocator->IsUMA();
+ misc_features_support.gpu_upload_heap_supported = allocator->IsGPUUploadHeapSupported();
+
+ // If UMA is supported but GPU upload heap isn't, the same functionality can be achieved
+ // by creating resources from a pool with the same properties as an upload heap.
+ if (misc_features_support.uma_supported && !misc_features_support.gpu_upload_heap_supported) {
+ D3D12MA::POOL_DESC pool_desc = {};
+#if defined(_MSC_VER) || !defined(_WIN32)
+ pool_desc.HeapProperties = device->GetCustomHeapProperties(0, D3D12_HEAP_TYPE_UPLOAD);
+#else
+ device->GetCustomHeapProperties(&pool_desc.HeapProperties, 0, D3D12_HEAP_TYPE_UPLOAD);
+#endif
+ pool_desc.HeapFlags = D3D12MA_RECOMMENDED_HEAP_FLAGS | D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
+ res = allocator->CreatePool(&pool_desc, uma_gpu_mappable_pool.GetAddressOf());
+
+ ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_CANT_CREATE, "D3D12MA::Allocator::CreatePool failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
+ }
+
return OK;
}
diff --git a/drivers/d3d12/rendering_device_driver_d3d12.h b/drivers/d3d12/rendering_device_driver_d3d12.h
index e9c9da90cf4..36639117f0d 100644
--- a/drivers/d3d12/rendering_device_driver_d3d12.h
+++ b/drivers/d3d12/rendering_device_driver_d3d12.h
@@ -51,6 +51,7 @@ namespace D3D12MA {
class Allocation;
class Allocator;
class VirtualBlock;
+class Pool;
}; // namespace D3D12MA
struct IDXGIAdapter;
@@ -103,6 +104,8 @@ class RenderingDeviceDriverD3D12 : public RenderingDeviceDriver {
struct MiscFeaturesSupport {
bool depth_bounds_supported = false;
+ bool uma_supported = false;
+ bool gpu_upload_heap_supported = false;
};
struct SamplerCapabilities {
@@ -199,6 +202,7 @@ private:
/****************/
Microsoft::WRL::ComPtr<D3D12MA::Allocator> allocator;
+ Microsoft::WRL::ComPtr<D3D12MA::Pool> uma_gpu_mappable_pool;
/******************/
/**** RESOURCE ****/
diff --git a/drivers/metal/rendering_device_driver_metal.cpp b/drivers/metal/rendering_device_driver_metal.cpp
index 8aa9d046991..ba9b392be13 100644
--- a/drivers/metal/rendering_device_driver_metal.cpp
+++ b/drivers/metal/rendering_device_driver_metal.cpp
@@ -127,6 +127,9 @@ RDD::BufferID RenderingDeviceDriverMetal::buffer_create(uint64_t p_size, BitFiel
options = base_hazard_tracking | MTL::ResourceStorageModePrivate;
}
break;
+ case MEMORY_ALLOCATION_TYPE_GPU_MAPPABLE:
+ options = base_hazard_tracking | MTL::ResourceStorageModeShared;
+ break;
}
MTL::Buffer *obj = device->newBuffer(p_size, options);
@@ -2749,6 +2752,8 @@ bool RenderingDeviceDriverMetal::has_feature(Features p_feature) {
#endif
return is_supported;
}
+ case SUPPORTS_GPU_MAPPABLE_BUFFER:
+ return true;
default:
return false;
}
diff --git a/drivers/vulkan/rendering_device_driver_vulkan.cpp b/drivers/vulkan/rendering_device_driver_vulkan.cpp
index fa3be3a0af6..d43210c3b4a 100644
--- a/drivers/vulkan/rendering_device_driver_vulkan.cpp
+++ b/drivers/vulkan/rendering_device_driver_vulkan.cpp
@@ -1588,6 +1588,24 @@ Error RenderingDeviceDriverVulkan::_initialize_allocator() {
VkResult err = vmaCreateAllocator(&allocator_info, &allocator);
ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, vformat("Couldn't create Vulkan memory allocator (VkResult error %d).", err));
+ // Check for device local, host visible and host coherent memory support.
+ const VkPhysicalDeviceMemoryProperties *memory_properties = nullptr;
+ vmaGetMemoryProperties(allocator, &memory_properties);
+
+ const VkMemoryPropertyFlags device_local_host_visible_host_coherent_flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
+
+ for (uint32_t i = 0; i < memory_properties->memoryTypeCount; i++) {
+ const VkMemoryType &memory_type = memory_properties->memoryTypes[i];
+
+ if ((memory_type.propertyFlags & device_local_host_visible_host_coherent_flags) == device_local_host_visible_host_coherent_flags) {
+ // Above 256 MiB is a good indicator for ReBAR or cache-coherent UMA support.
+ if (memory_properties->memoryHeaps[memory_type.heapIndex].size > (256 * 1024 * 1024)) {
+ device_local_host_visible_host_coherent_memory_support = true;
+ break;
+ }
+ }
+ }
+
return OK;
}
@@ -2029,6 +2047,15 @@ RDD::BufferID RenderingDeviceDriverVulkan::buffer_create(uint64_t p_size, BitFie
alloc_create_info.pool = _find_or_create_small_allocs_pool(mem_type_index);
}
} break;
+ case MEMORY_ALLOCATION_TYPE_GPU_MAPPABLE: {
+ if (device_local_host_visible_host_coherent_memory_support) {
+ vma_usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
+ alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
+ alloc_create_info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
+ } else {
+ ERR_FAIL_V_MSG(BufferID(), "GPU mappable buffers are unsupported on this device.");
+ }
+ } break;
}
VkBuffer vk_buffer = VK_NULL_HANDLE;
@@ -7448,6 +7475,8 @@ bool RenderingDeviceDriverVulkan::has_feature(Features p_feature) {
#else
return context_driver->is_colorspace_supported();
#endif // defined(WINDOWS_ENABLED)
+ case SUPPORTS_GPU_MAPPABLE_BUFFER:
+ return device_local_host_visible_host_coherent_memory_support;
default:
return false;
}
diff --git a/drivers/vulkan/rendering_device_driver_vulkan.h b/drivers/vulkan/rendering_device_driver_vulkan.h
index 276220d1bfa..0e2726a593e 100644
--- a/drivers/vulkan/rendering_device_driver_vulkan.h
+++ b/drivers/vulkan/rendering_device_driver_vulkan.h
@@ -171,6 +171,7 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
#if defined(VK_TRACK_DEVICE_MEMORY)
bool device_memory_report_support = false;
#endif
+ bool device_local_host_visible_host_coherent_memory_support = false;
#if defined(SWAPPY_FRAME_PACING_ENABLED)
// Swappy frame pacer for Android.
bool swappy_frame_pacer_enable = false;
diff --git a/servers/rendering/rendering_device.cpp b/servers/rendering/rendering_device.cpp
index c4df10fdca1..b5a507d4496 100644
--- a/servers/rendering/rendering_device.cpp
+++ b/servers/rendering/rendering_device.cpp
@@ -834,6 +834,24 @@ Error RenderingDevice::hit_sbt_range_update(RID p_hit_sbt, HitShaderBindingTable
/**** BUFFER MANAGEMENT ****/
/***************************/
+RDD::MemoryAllocationType RenderingDevice::_get_buffer_alloc_type(bool p_has_initial_data, Thread::ID p_thread_id) const {
+ bool gpu_mappable = false;
+
+ if (p_has_initial_data && driver->has_feature(SUPPORTS_GPU_MAPPABLE_BUFFER)) {
+ // Integrated GPUs have no cost when copying to GPU mappable buffers.
+ if (device.type == RenderingContextDriver::DEVICE_TYPE_INTEGRATED_GPU) {
+ gpu_mappable = true;
+ } else {
+ // Discrete GPUs can have extra cost when copying to GPU mappable buffers. Avoid that work on the render thread.
+ if (p_thread_id != render_thread_id) {
+ gpu_mappable = true;
+ }
+ }
+ }
+
+ return gpu_mappable ? RDD::MEMORY_ALLOCATION_TYPE_GPU_MAPPABLE : RDD::MEMORY_ALLOCATION_TYPE_GPU;
+}
+
RenderingDevice::Buffer *RenderingDevice::_get_buffer_from_owner(RID p_buffer) {
Buffer *buffer = nullptr;
if (vertex_buffer_owner.owns(p_buffer)) {
@@ -852,30 +870,41 @@ RenderingDevice::Buffer *RenderingDevice::_get_buffer_from_owner(RID p_buffer) {
}
Error RenderingDevice::_buffer_initialize(Buffer *p_buffer, Span<uint8_t> p_data, uint32_t p_required_align) {
- uint32_t transfer_worker_offset;
- TransferWorker *transfer_worker = _acquire_transfer_worker(p_data.size(), p_required_align, transfer_worker_offset);
- p_buffer->transfer_worker_index = transfer_worker->index;
+ if (p_buffer->alloc_type == RDD::MEMORY_ALLOCATION_TYPE_GPU_MAPPABLE) {
+ // Copy directly to the buffer if available.
+ uint8_t *data_ptr = driver->buffer_map(p_buffer->driver_id);
+ ERR_FAIL_NULL_V(data_ptr, ERR_CANT_CREATE);
- {
- MutexLock lock(transfer_worker->operations_mutex);
- p_buffer->transfer_worker_operation = ++transfer_worker->operations_counter;
- }
+ memcpy(data_ptr, p_data.ptr(), p_data.size());
- // Copy to the worker's staging buffer.
- uint8_t *data_ptr = driver->buffer_map(transfer_worker->staging_buffer);
- ERR_FAIL_NULL_V(data_ptr, ERR_CANT_CREATE);
+ driver->buffer_unmap(p_buffer->driver_id);
+ } else {
+ // Otherwise, use a transfer worker.
+ uint32_t transfer_worker_offset;
+ TransferWorker *transfer_worker = _acquire_transfer_worker(p_data.size(), p_required_align, transfer_worker_offset);
+ p_buffer->transfer_worker_index = transfer_worker->index;
- memcpy(data_ptr + transfer_worker_offset, p_data.ptr(), p_data.size());
- driver->buffer_unmap(transfer_worker->staging_buffer);
+ {
+ MutexLock lock(transfer_worker->operations_mutex);
+ p_buffer->transfer_worker_operation = ++transfer_worker->operations_counter;
+ }
- // Copy from the staging buffer to the real buffer.
- RDD::BufferCopyRegion region;
- region.src_offset = transfer_worker_offset;
- region.dst_offset = 0;
- region.size = p_data.size();
- driver->command_copy_buffer(transfer_worker->command_buffer, transfer_worker->staging_buffer, p_buffer->driver_id, region);
+ // Copy to the worker's staging buffer.
+ uint8_t *data_ptr = driver->buffer_map(transfer_worker->staging_buffer);
+ ERR_FAIL_NULL_V(data_ptr, ERR_CANT_CREATE);
+
+ memcpy(data_ptr + transfer_worker_offset, p_data.ptr(), p_data.size());
+ driver->buffer_unmap(transfer_worker->staging_buffer);
+
+ // Copy from the staging buffer to the real buffer.
+ RDD::BufferCopyRegion region;
+ region.src_offset = transfer_worker_offset;
+ region.dst_offset = 0;
+ region.size = p_data.size();
+ driver->command_copy_buffer(transfer_worker->command_buffer, transfer_worker->staging_buffer, p_buffer->driver_id, region);
- _release_transfer_worker(transfer_worker);
+ _release_transfer_worker(transfer_worker);
+ }
return OK;
}
@@ -1474,7 +1503,9 @@ RID RenderingDevice::storage_buffer_create(uint32_t p_size_bytes, Span<uint8_t>
buffer.usage.set_flag(RDD::BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT);
}
- buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn);
+ buffer.alloc_type = _get_buffer_alloc_type(!p_data.is_empty(), Thread::get_caller_id());
+
+ buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, buffer.alloc_type, frames_drawn);
ERR_FAIL_COND_V(!buffer.driver_id, RID());
// Storage buffers are assumed to be mutable.
@@ -3864,7 +3895,10 @@ RID RenderingDevice::vertex_buffer_create(uint32_t p_size_bytes, Span<uint8_t> p
if (p_creation_bits.has_flag(BUFFER_CREATION_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT)) {
buffer.usage.set_flag(RDD::BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT);
}
- buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn);
+
+ buffer.alloc_type = _get_buffer_alloc_type(!p_data.is_empty(), Thread::get_caller_id());
+
+ buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, buffer.alloc_type, frames_drawn);
ERR_FAIL_COND_V(!buffer.driver_id, RID());
// Vertex buffers are assumed to be immutable unless they don't have initial data or they've been marked for storage explicitly.
@@ -4079,7 +4113,10 @@ RID RenderingDevice::index_buffer_create(uint32_t p_index_count, IndexBufferForm
if (p_creation_bits.has_flag(BUFFER_CREATION_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT)) {
index_buffer.usage.set_flag(RDD::BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT);
}
- index_buffer.driver_id = driver->buffer_create(index_buffer.size, index_buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn);
+
+ index_buffer.alloc_type = _get_buffer_alloc_type(!p_data.is_empty(), Thread::get_caller_id());
+
+ index_buffer.driver_id = driver->buffer_create(index_buffer.size, index_buffer.usage, index_buffer.alloc_type, frames_drawn);
ERR_FAIL_COND_V(!index_buffer.driver_id, RID());
// Index buffers are assumed to be immutable unless they don't have initial data.
@@ -4342,7 +4379,10 @@ RID RenderingDevice::uniform_buffer_create(uint32_t p_size_bytes, Span<uint8_t>
// stick to the known/intended use cases and scream if we deviate from it.
buffer.usage.clear_flag(RDD::BUFFER_USAGE_TRANSFER_TO_BIT);
}
- buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU, frames_drawn);
+
+ buffer.alloc_type = _get_buffer_alloc_type(!p_data.is_empty(), Thread::get_caller_id());
+
+ buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, buffer.alloc_type, frames_drawn);
ERR_FAIL_COND_V(!buffer.driver_id, RID());
// Uniform buffers are assumed to be immutable unless they don't have initial data.
diff --git a/servers/rendering/rendering_device.h b/servers/rendering/rendering_device.h
index 820f340f441..f99204e6d20 100644
--- a/servers/rendering/rendering_device.h
+++ b/servers/rendering/rendering_device.h
@@ -185,11 +185,14 @@ private:
RDD::BufferID driver_id;
uint32_t size = 0;
BitField<RDD::BufferUsageBits> usage = {};
+ RDD::MemoryAllocationType alloc_type = {};
RDG::ResourceTracker *draw_tracker = nullptr;
int32_t transfer_worker_index = -1;
uint64_t transfer_worker_operation = 0;
};
+ RDD::MemoryAllocationType _get_buffer_alloc_type(bool p_has_initial_data, Thread::ID p_thread_id) const;
+
Buffer *_get_buffer_from_owner(RID p_buffer);
Error _buffer_initialize(Buffer *p_buffer, Span<uint8_t> p_data, uint32_t p_required_align = 32);
diff --git a/servers/rendering/rendering_device_commons.h b/servers/rendering/rendering_device_commons.h
index ad646070f0b..aa2687564bd 100644
--- a/servers/rendering/rendering_device_commons.h
+++ b/servers/rendering/rendering_device_commons.h
@@ -1037,6 +1037,7 @@ public:
SUPPORTS_RAYTRACING_PIPELINE,
SUPPORTS_HDR_OUTPUT,
SUPPORTS_RASTERIZATION_RATE_MAP,
+ SUPPORTS_GPU_MAPPABLE_BUFFER,
};
enum SubgroupOperations {
diff --git a/servers/rendering/rendering_device_driver.h b/servers/rendering/rendering_device_driver.h
index 103bd9976bc..61ef2fba420 100644
--- a/servers/rendering/rendering_device_driver.h
+++ b/servers/rendering/rendering_device_driver.h
@@ -157,6 +157,7 @@ public:
enum MemoryAllocationType {
MEMORY_ALLOCATION_TYPE_CPU, // For images, CPU allocation also means linear, GPU is tiling optimal.
MEMORY_ALLOCATION_TYPE_GPU,
+ MEMORY_ALLOCATION_TYPE_GPU_MAPPABLE, // Supported on UMA devices or discrete devices with ReBAR.
};
/*****************/