[vulkan] Report a maxBufferSize that is a multiple of 4

On Vulkan, Dawn rounds non-host-mapped buffer allocation sizes up to a
multiple of 4 bytes. When VK_KHR_maintenance4 is supported,
maxBufferSize is taken directly from the driver, which on Mesa lavapipe
is 0xffffffff (4 GiB - 1). Creating a buffer at that limit rounds the
Vulkan size up to 0x100000000 and fails vkCreateBuffer with OOM (hit by
webgpu:api,validation,buffer,create:limit with untiered limits).

Align Vulkan's reported maxBufferSize down to a multiple of 4
(0xfffffffc on lavapipe) and assert this in MaxLimitTests.MaxBufferSizes.
This does not cover the extra padding added for vertex, index, or uniform
buffers.

Bug: None

This is an imported pull request from
https://github.com/google/dawn/pull/97

GITHUB_PR_HEAD_SHA=0ba9b5727d8c53d85e79723281f8129ca968a32a
ORIGINAL_AUTHOR=Alex Soliman <alexander.soliman@gmail.com>
GitOrigin-RevId: ecb5b48c05825978d38a4474ccb128069d27afb8
Change-Id: I79d9efced95199ef4489aaef45496c359b498a08
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/361637
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/dawn/native/vulkan/PhysicalDeviceVk.cpp b/src/dawn/native/vulkan/PhysicalDeviceVk.cpp
index fbf22b9..e64f626 100644
--- a/src/dawn/native/vulkan/PhysicalDeviceVk.cpp
+++ b/src/dawn/native/vulkan/PhysicalDeviceVk.cpp
@@ -36,6 +36,7 @@
 #include "dawn/platform/DawnPlatform.h"
 #include "src/dawn/common/Enumerator.h"
 #include "src/dawn/common/GPUInfo.h"
+#include "src/dawn/common/Math.h"
 #include "src/dawn/native/ChainUtils.h"
 #include "src/dawn/native/Error.h"
 #include "src/dawn/native/Instance.h"
@@ -947,6 +948,8 @@
     } else {
         limits->v1.maxBufferSize = mDeviceInfo.maintenance3Properties.maxMemoryAllocationSize;
     }
+    // Round down so normal four-byte buffer alignment cannot exceed the advertised Vulkan limit.
+    limits->v1.maxBufferSize = AlignDown(limits->v1.maxBufferSize, 4);
 
     // OpArrayLength returns 0 for buffers >= 2GB for some NVIDIA devices.
     // There is an open bug with NVIDIA to resolve. Likely some issue with the sign bit.
diff --git a/src/dawn/tests/end2end/MaxLimitTests.cpp b/src/dawn/tests/end2end/MaxLimitTests.cpp
index c0b8d37..9bc0199 100644
--- a/src/dawn/tests/end2end/MaxLimitTests.cpp
+++ b/src/dawn/tests/end2end/MaxLimitTests.cpp
@@ -864,6 +864,9 @@
     EXPECT_LE(baseLimits.maxStorageBufferBindingSize, baseLimits.maxBufferSize);
     EXPECT_LE(baseLimits.maxUniformBufferBindingSize, baseLimits.maxBufferSize);
     EXPECT_EQ(baseLimits.maxStorageBufferBindingSize % 4, 0u);
+    if (IsVulkan()) {
+        EXPECT_EQ(baseLimits.maxBufferSize % 4, 0u);
+    }
 
     // Base limits with tiering.
     GetAdapter().SetUseTieredLimits(true);