Images bound past 4 GB in one VkDeviceMemory alias memory 4 GB earlier
repro.c is a self-contained Vulkan program (no window, no assets). It allocates one 4.5 GB device-local
VkDeviceMemory, binds 18 images of 8192x8192 RGBA8 (256 MB each) back to back at their required alignment, clears
each to its own value, then copies the first and last texel of each to a host-visible buffer. It repeats the same with
18 buffers filled by vkCmdFillBuffer.
Expected: every image and buffer reads back its own value. The usage is valid: the driver reports
maxMemoryAllocationSize 18446744073709551615, all offsets meet VkMemoryRequirements.alignment and
bufferImageGranularity, and the validation layer (--validate) reports nothing.
Result
RTX 5080, Windows 11, Vulkan SDK 1.4.363. Same output on NVIDIA drivers 596.49 and 617.42:
images: 18 in one allocation of 4.50 GB
image 0 at 0.00 GB: want 01010101, first 11111111, last 11111111 WRONG
image 1 at 0.25 GB: want 02020202, first 12121212, last 12121212 WRONG
image 2 .. 17: ok
buffers: 18 in one allocation of 4.50 GB
buffer 0 .. 17: ok
Images 0 and 1 read back the values of images 16 and 17, which are bound exactly 4 GB later in the same allocation:
image memory bound at offset >= 4 GB lands at offset - 4 GB. Buffers in the same layout are unaffected.
On the same GPU and driver through D3D12 instead (Mesa’s Dozen Vulkan-on-D3D12 driver 26.1.6, under WSL), every image
and buffer reads back its own value. That points at the Vulkan driver rather than the hardware or kernel driver. Dozen
reports maxMemoryAllocationSize 2 GB, so the 4.5 GB allocation exceeds its stated limit; it allocated anyway.
Workaround: keep every allocation that holds images under 4 GB.
Build and run
Windows (Vulkan SDK installed):
clang -std=c11 -O2 -I%VULKAN_SDK%\Include repro.c -L%VULKAN_SDK%\Lib -lvulkan-1 -o repro.exe
repro.exe (or repro.exe --validate)
Linux: cc -std=c11 -O2 repro.c -lvulkan -o repro && ./repro
Exit code 0: every value right; 1: wrong values; 2: a Vulkan call failed. Needs about 4.6 GB of free device memory.
// Minimal reproduction: resources bound past 4 GB into one VkDeviceMemory read back as other resources' data.
//
// One device-local allocation holds COUNT resources of 256 MB each (4.5 GB in total), placed back to back at their
// required alignment. Each is filled with its own value on the GPU (vkCmdClearColorImage for images, vkCmdFillBuffer
// for buffers), then its first and last texel or word is copied to a host-visible buffer and compared.
// Expected: every resource reads back its own value.
//
// Build (Windows, Vulkan SDK):
// clang -std=c11 -O2 -I%VULKAN_SDK%\Include repro.c -L%VULKAN_SDK%\Lib -lvulkan-1 -o repro.exe
// Build (Linux): cc -std=c11 -O2 repro.c -lvulkan -o repro
// Run: repro [--validate] Exit code 0: every value right; 1: wrong values; 2: a Vulkan call failed.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vulkan/vulkan.h>
#define COUNT 18
#define SIDE 8192 // 8192 x 8192 RGBA8: 256 MB per image; buffers the same size.
#define CHECK(x) \
do { \
VkResult r_ = (x); \
if (r_ != VK_SUCCESS) { \
fprintf(stderr, "%s:%d: %s failed: %d\n", __FILE__, __LINE__, #x, (int)r_); \
exit(2); \
} \
} while (0)
static VkPhysicalDeviceMemoryProperties mem_props;
static uint32_t memory_type(uint32_t bits, VkMemoryPropertyFlags want) {
for (uint32_t i = 0; i < mem_props.memoryTypeCount; i++) {
if ((bits & (1u << i)) && (mem_props.memoryTypes[i].propertyFlags & want) == want) return i;
}
fprintf(stderr, "no memory type with flags 0x%x in mask 0x%x\n", (unsigned)want, (unsigned)bits);
exit(2);
}
static VkDeviceSize align_up(VkDeviceSize v, VkDeviceSize a) { return (v + a - 1) / a * a; }
// Distinct per resource, the same in every byte (so it reads the same as a texel or a word).
static uint32_t value_of(uint32_t i) { return 0x01010101u * (i + 1); }
static VkDevice device;
static VkQueue queue;
static VkCommandPool pool;
static VkCommandBuffer begin(void) {
VkCommandBuffer cmd;
CHECK(vkAllocateCommandBuffers(device,
&(VkCommandBufferAllocateInfo){.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = pool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1},
&cmd));
CHECK(vkBeginCommandBuffer(cmd, &(VkCommandBufferBeginInfo){.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}));
return cmd;
}
static void submit_and_wait(VkCommandBuffer cmd) {
CHECK(vkEndCommandBuffer(cmd));
CHECK(vkQueueSubmit(queue, 1, &(VkSubmitInfo){.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &cmd},
VK_NULL_HANDLE));
CHECK(vkQueueWaitIdle(queue));
vkFreeCommandBuffers(device, pool, 1, &cmd);
}
static void barrier(VkCommandBuffer cmd, VkImage image, VkImageLayout from, VkImageLayout to, VkAccessFlags src, VkAccessFlags dst) {
vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1,
&(VkImageMemoryBarrier){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = src,
.dstAccessMask = dst,
.oldLayout = from,
.newLayout = to,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = image,
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}});
}
// Host-visible buffer for 2 values per resource.
static VkBuffer readback;
static VkDeviceMemory readback_memory;
static uint32_t *readback_values;
static int report(const char *kind, const VkDeviceSize *offsets) {
int wrong = 0;
for (uint32_t i = 0; i < COUNT; i++) {
uint32_t want = value_of(i), first = readback_values[i * 2], last = readback_values[i * 2 + 1];
int ok = first == want && last == want;
wrong += !ok;
printf("%s %2u at %5.2f GB: want %08x, first %08x, last %08x %s\n", kind, i, (double)offsets[i] / (1u << 30), want,
first, last, ok ? "ok" : "WRONG");
}
return wrong;
}
int main(int argc, char **argv) {
int validate = argc > 1 && strcmp(argv[1], "--validate") == 0;
const char *layer = "VK_LAYER_KHRONOS_validation";
VkInstance instance;
CHECK(vkCreateInstance(&(VkInstanceCreateInfo){.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &(VkApplicationInfo){.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.apiVersion = VK_API_VERSION_1_1},
.enabledLayerCount = validate ? 1u : 0u,
.ppEnabledLayerNames = &layer},
NULL, &instance));
uint32_t n = 1;
VkPhysicalDevice physical;
VkResult enumerated = vkEnumeratePhysicalDevices(instance, &n, &physical);
if (enumerated != VK_INCOMPLETE) CHECK(enumerated);
if (n == 0) {
fprintf(stderr, "no Vulkan device\n");
return 2;
}
VkPhysicalDeviceMaintenance3Properties m3 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES};
VkPhysicalDeviceProperties2 props = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &m3};
vkGetPhysicalDeviceProperties2(physical, &props);
vkGetPhysicalDeviceMemoryProperties(physical, &mem_props);
uint32_t dv = props.properties.driverVersion;
printf("%s, driverVersion 0x%08x (NVIDIA: %u.%u), maxMemoryAllocationSize %llu, bufferImageGranularity %llu\n",
props.properties.deviceName, dv, dv >> 22, (dv >> 14) & 0xff, (unsigned long long)m3.maxMemoryAllocationSize,
(unsigned long long)props.properties.limits.bufferImageGranularity);
uint32_t family_count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physical, &family_count, NULL);
VkQueueFamilyProperties families[16];
if (family_count > 16) family_count = 16;
vkGetPhysicalDeviceQueueFamilyProperties(physical, &family_count, families);
uint32_t family = 0;
while (family < family_count && !(families[family].queueFlags & VK_QUEUE_GRAPHICS_BIT)) family++;
float priority = 1.0f;
CHECK(vkCreateDevice(physical,
&(VkDeviceCreateInfo){.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &(VkDeviceQueueCreateInfo){.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = family,
.queueCount = 1,
.pQueuePriorities = &priority}},
NULL, &device));
vkGetDeviceQueue(device, family, 0, &queue);
CHECK(vkCreateCommandPool(device, &(VkCommandPoolCreateInfo){.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .queueFamilyIndex = family},
NULL, &pool));
VkBufferCreateInfo readback_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = COUNT * 2 * sizeof(uint32_t),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT};
CHECK(vkCreateBuffer(device, &readback_info, NULL, &readback));
VkMemoryRequirements rr;
vkGetBufferMemoryRequirements(device, readback, &rr);
CHECK(vkAllocateMemory(device,
&(VkMemoryAllocateInfo){.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = rr.size,
.memoryTypeIndex = memory_type(rr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)},
NULL, &readback_memory));
CHECK(vkBindBufferMemory(device, readback, readback_memory, 0));
CHECK(vkMapMemory(device, readback_memory, 0, VK_WHOLE_SIZE, 0, (void **)&readback_values));
int wrong = 0;
// Images: created, then placed back to back in one allocation.
{
VkImage images[COUNT];
VkDeviceSize offsets[COUNT], size = 0;
uint32_t bits = ~0u;
for (uint32_t i = 0; i < COUNT; i++) {
CHECK(vkCreateImage(device,
&(VkImageCreateInfo){.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.imageType = VK_IMAGE_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.extent = {SIDE, SIDE, 1},
.mipLevels = 1,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT},
NULL, &images[i]));
VkMemoryRequirements req;
vkGetImageMemoryRequirements(device, images[i], &req);
offsets[i] = align_up(size, req.alignment);
size = offsets[i] + req.size;
bits &= req.memoryTypeBits;
}
printf("images: %u in one allocation of %.2f GB\n", COUNT, (double)size / (1u << 30));
VkDeviceMemory memory;
CHECK(vkAllocateMemory(device,
&(VkMemoryAllocateInfo){.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = size,
.memoryTypeIndex = memory_type(bits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)},
NULL, &memory));
for (uint32_t i = 0; i < COUNT; i++) CHECK(vkBindImageMemory(device, images[i], memory, offsets[i]));
VkCommandBuffer cmd = begin();
for (uint32_t i = 0; i < COUNT; i++) {
barrier(cmd, images[i], VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_ACCESS_TRANSFER_WRITE_BIT);
uint32_t v = value_of(i);
float c = (float)(v & 0xff) / 255.0f;
vkCmdClearColorImage(cmd, images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &(VkClearColorValue){.float32 = {c, c, c, c}}, 1,
&(VkImageSubresourceRange){VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1});
}
submit_and_wait(cmd); // Every clear lands before any read, so a later image can overwrite an earlier one.
cmd = begin();
for (uint32_t i = 0; i < COUNT; i++) {
barrier(cmd, images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT);
VkBufferImageCopy regions[2] = {
{.bufferOffset = i * 8, .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .imageExtent = {1, 1, 1}},
{.bufferOffset = i * 8 + 4,
.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
.imageOffset = {SIDE - 1, SIDE - 1, 0},
.imageExtent = {1, 1, 1}},
};
vkCmdCopyImageToBuffer(cmd, images[i], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, readback, 2, regions);
}
submit_and_wait(cmd);
wrong += report("image", offsets);
for (uint32_t i = 0; i < COUNT; i++) vkDestroyImage(device, images[i], NULL);
vkFreeMemory(device, memory, NULL);
}
// Buffers: the same layout.
{
VkBuffer buffers[COUNT];
VkDeviceSize offsets[COUNT], size = 0;
uint32_t bits = ~0u;
for (uint32_t i = 0; i < COUNT; i++) {
CHECK(vkCreateBuffer(device,
&(VkBufferCreateInfo){.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = (VkDeviceSize)SIDE * SIDE * 4,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT},
NULL, &buffers[i]));
VkMemoryRequirements req;
vkGetBufferMemoryRequirements(device, buffers[i], &req);
offsets[i] = align_up(size, req.alignment);
size = offsets[i] + req.size;
bits &= req.memoryTypeBits;
}
printf("buffers: %u in one allocation of %.2f GB\n", COUNT, (double)size / (1u << 30));
VkDeviceMemory memory;
CHECK(vkAllocateMemory(device,
&(VkMemoryAllocateInfo){.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = size,
.memoryTypeIndex = memory_type(bits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)},
NULL, &memory));
for (uint32_t i = 0; i < COUNT; i++) CHECK(vkBindBufferMemory(device, buffers[i], memory, offsets[i]));
VkCommandBuffer cmd = begin();
for (uint32_t i = 0; i < COUNT; i++) vkCmdFillBuffer(cmd, buffers[i], 0, VK_WHOLE_SIZE, value_of(i));
submit_and_wait(cmd);
cmd = begin();
VkDeviceSize last = (VkDeviceSize)SIDE * SIDE * 4 - 4;
for (uint32_t i = 0; i < COUNT; i++) {
VkBufferCopy copies[2] = {{.srcOffset = 0, .dstOffset = i * 8, .size = 4}, {.srcOffset = last, .dstOffset = i * 8 + 4, .size = 4}};
vkCmdCopyBuffer(cmd, buffers[i], readback, 2, copies);
}
submit_and_wait(cmd);
wrong += report("buffer", offsets);
for (uint32_t i = 0; i < COUNT; i++) vkDestroyBuffer(device, buffers[i], NULL);
vkFreeMemory(device, memory, NULL);
}
vkDestroyBuffer(device, readback, NULL);
vkFreeMemory(device, readback_memory, NULL);
vkDestroyCommandPool(device, pool, NULL);
vkDestroyDevice(device, NULL);
vkDestroyInstance(instance, NULL);
printf("%s\n", wrong ? "WRONG VALUES" : "all values right");
return wrong ? 1 : 0;
}