Commit a57e2eca by Nicolas Capens Committed by Nicolas Capens

Limit Vulkan memory allocation size to 1 GiB

Memory offset calculations in 32-bit SIMD elements limit us to addressing at most 4 GiB. Signed arithmetic further restricts it to 2 GiB. The legacy OpenGL ES implementation limits image allocations to 1 GiB, to discourage excessive memory usage which wouldn't typically succeed on other (mobile) implementations anyway. So until we have a strong use case which requires more, let's impose the same limit. Bug: b/146515574 Change-Id: Iaa7bd22789fd20b4e7975d3d2ffee2a8f90b006a Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39828 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 19b43a60
...@@ -88,4 +88,10 @@ constexpr int SUBPIXEL_PRECISION_MASK = 0xFFFFFFFF >> (32 - SUBPIXEL_PRECISION_B ...@@ -88,4 +88,10 @@ constexpr int SUBPIXEL_PRECISION_MASK = 0xFFFFFFFF >> (32 - SUBPIXEL_PRECISION_B
#define SWIFTSHADER_EXTERNAL_SEMAPHORE_OPAQUE_FD 1 #define SWIFTSHADER_EXTERNAL_SEMAPHORE_OPAQUE_FD 1
#endif #endif
constexpr VkDeviceSize MAX_MEMORY_ALLOCATION_SIZE = 0x40000000ull; // 0x40000000 = 1 GiB
// Memory offset calculations in 32-bit SIMD elements limit us to addressing at most 4 GiB.
// Signed arithmetic further restricts it to 2 GiB.
static_assert(MAX_MEMORY_ALLOCATION_SIZE <= 0x80000000ull, "maxMemoryAllocationSize must not exceed 2 GiB");
#endif // VK_CONFIG_HPP_ #endif // VK_CONFIG_HPP_
...@@ -100,7 +100,9 @@ public: ...@@ -100,7 +100,9 @@ public:
{ {
void *buffer = vk::allocate(size, REQUIRED_MEMORY_ALIGNMENT, DEVICE_MEMORY); void *buffer = vk::allocate(size, REQUIRED_MEMORY_ALIGNMENT, DEVICE_MEMORY);
if(!buffer) if(!buffer)
{
return VK_ERROR_OUT_OF_DEVICE_MEMORY; return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
*pBuffer = buffer; *pBuffer = buffer;
return VK_SUCCESS; return VK_SUCCESS;
...@@ -173,6 +175,11 @@ size_t DeviceMemory::ComputeRequiredAllocationSize(const VkMemoryAllocateInfo *p ...@@ -173,6 +175,11 @@ size_t DeviceMemory::ComputeRequiredAllocationSize(const VkMemoryAllocateInfo *p
VkResult DeviceMemory::allocate() VkResult DeviceMemory::allocate()
{ {
if(size > MAX_MEMORY_ALLOCATION_SIZE)
{
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
VkResult result = VK_SUCCESS; VkResult result = VK_SUCCESS;
if(!buffer) if(!buffer)
{ {
......
...@@ -326,7 +326,7 @@ void PhysicalDevice::getProperties(VkPhysicalDeviceIDProperties *properties) con ...@@ -326,7 +326,7 @@ void PhysicalDevice::getProperties(VkPhysicalDeviceIDProperties *properties) con
void PhysicalDevice::getProperties(VkPhysicalDeviceMaintenance3Properties *properties) const void PhysicalDevice::getProperties(VkPhysicalDeviceMaintenance3Properties *properties) const
{ {
properties->maxMemoryAllocationSize = 1u << 31; properties->maxMemoryAllocationSize = MAX_MEMORY_ALLOCATION_SIZE;
properties->maxPerSetDescriptors = 1024; properties->maxPerSetDescriptors = 1024;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment