Skip to content

Commit

Permalink
Add BDA support to AllocatedBuffer in demos
Browse files Browse the repository at this point in the history
* This requires setting vmaBDA to true, which is opt-in as it applies
  universally to all memory allocations in the test. We don't know if there are
  driver impacts from enabling BDA and it may also mess with our tests.
baldurk committed Nov 20, 2024
1 parent 9916def commit dea24cd
Showing 3 changed files with 38 additions and 64 deletions.
91 changes: 27 additions & 64 deletions util/test/demos/vk/vk_khr_buffer_address.cpp
Original file line number Diff line number Diff line change
@@ -146,6 +146,8 @@ void main()

int main()
{
vmaBDA = true;

// initialise, create window, create context, etc
if(!Init())
return 3;
@@ -165,45 +167,13 @@ void main()

VkPipeline pipe = createGraphicsPipeline(pipeCreateInfo);

vkh::BufferCreateInfo bufinfo(0x100000, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR);

VkMemoryAllocateInfo memoryAllocateInfo = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
VkMemoryAllocateFlagsInfo memoryAllocateFlags = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO};

memoryAllocateFlags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
memoryAllocateInfo.pNext = &memoryAllocateFlags;

const VkPhysicalDeviceMemoryProperties *memProps = NULL;
vmaGetMemoryProperties(allocator, &memProps);

VkBuffer databuf;
vkCreateBuffer(device, bufinfo, NULL, &databuf);
AllocatedBuffer databuf(
this, vkh::BufferCreateInfo(0x100000, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR),
VmaAllocationCreateInfo({0, VMA_MEMORY_USAGE_CPU_TO_GPU}));

VkBuffer staticBuf;
vkCreateBuffer(device, bufinfo, NULL, &staticBuf);

VkMemoryRequirements mrq;
vkGetBufferMemoryRequirements(device, databuf, &mrq);

memoryAllocateInfo.allocationSize = mrq.size;

for(uint32_t i = 0; i < memProps->memoryTypeCount; i++)
{
if((mrq.memoryTypeBits & (1u << i)) &&
(memProps->memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
{
memoryAllocateInfo.memoryTypeIndex = i;
break;
}
}

VkDeviceMemory databufMem;
vkAllocateMemory(device, &memoryAllocateInfo, NULL, &databufMem);
vkBindBufferMemory(device, databuf, databufMem, 0);

VkDeviceMemory staticBufMem;
vkAllocateMemory(device, &memoryAllocateInfo, NULL, &staticBufMem);
vkBindBufferMemory(device, staticBuf, staticBufMem, 0);
AllocatedBuffer staticBuf(
this, vkh::BufferCreateInfo(0x100000, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR),
VmaAllocationCreateInfo({0, VMA_MEMORY_USAGE_CPU_TO_GPU}));

// north-facing primary colours triangle
const DefaultA2V tri1[3] = {
@@ -219,14 +189,10 @@ void main()
{Vec3f(-0.5f, -0.5f, 0.0f), Vec4f(1.0f, 1.0f, 0.4f, 1.0f), Vec2f(1.0f, 0.0f)},
};

VkBufferDeviceAddressInfoKHR info = {VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO_KHR};
info.buffer = databuf;

VkDeviceAddress baseAddr = vkGetBufferDeviceAddressKHR(device, &info);
byte *gpuptr = (byte *)baseAddr; // not a valid cpu pointer but useful for avoiding casting
// not a valid cpu pointer but useful for avoiding casting
byte *gpuptr = (byte *)databuf.address;

byte *cpuptr = NULL;
vkMapMemory(device, databufMem, 0, mrq.size, 0, (void **)&cpuptr);
byte *cpuptr = databuf.map();

// put triangle data first
memcpy(cpuptr, tri1, sizeof(tri1));
@@ -266,10 +232,8 @@ void main()
drawscpu[2].tint = Vec4f(1.0f, 1.0f, 1.0f, 1.0f);

// Make a static buffer of draw data
info.buffer = staticBuf;
baseAddr = vkGetBufferDeviceAddressKHR(device, &info);
gpuptr = (byte *)baseAddr; // not a valid cpu pointer but useful for avoiding casting
vkMapMemory(device, staticBufMem, 0, mrq.size, 0, (void **)&cpuptr);
gpuptr = (byte *)staticBuf.address; // not a valid cpu pointer but useful for avoiding casting
cpuptr = staticBuf.map();

// put triangle data first
memcpy(cpuptr, tri1, sizeof(tri1));
@@ -298,7 +262,7 @@ void main()
staticDrawsCpu[0].scale = Vec2f(0.5f, 0.5f);
staticDrawsCpu[0].tint = Vec4f(1.0f, 1.0f, 0.2f, 1.0f); // tint yellow

vkUnmapMemory(device, staticBufMem);
staticBuf.unmap();

float time = 0.0f;

@@ -313,17 +277,18 @@ void main()

for(int i = 0; i < 2; i++)
{
VkBuffer midbuf = VK_NULL_HANDLE;
vkCreateBuffer(device, bufinfo, NULL, &midbuf);

VkDeviceMemory midmem = VK_NULL_HANDLE;
vkAllocateMemory(device, &memoryAllocateInfo, NULL, &midmem);
vkBindBufferMemory(device, midbuf, midmem, 0);

vkMapMemory(device, midmem, 0, mrq.size, 0, (void **)&cpuptr);
vkDestroyBuffer(device, midbuf, NULL);
vkUnmapMemory(device, midmem);
vkFreeMemory(device, midmem, NULL);
AllocatedBuffer midbuf(
this, vkh::BufferCreateInfo(0x100000, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR),
VmaAllocationCreateInfo(
{VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU}));

AllocatedBuffer midbuf2(
this, vkh::BufferCreateInfo(0x100000, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR),
VmaAllocationCreateInfo(
{VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, VMA_MEMORY_USAGE_GPU_ONLY}));

midbuf.free();
midbuf2.free();
}

vkCmdClearColorImage(cmd, swapimg, VK_IMAGE_LAYOUT_GENERAL,
@@ -390,9 +355,7 @@ void main()

CHECK_VKR(vkDeviceWaitIdle(device));

vkDestroyBuffer(device, databuf, NULL);
vkUnmapMemory(device, databufMem);
vkFreeMemory(device, databufMem, NULL);
databuf.unmap();

return 0;
}
10 changes: 10 additions & 0 deletions util/test/demos/vk/vk_test.cpp
Original file line number Diff line number Diff line change
@@ -1826,6 +1826,16 @@ AllocatedBuffer::AllocatedBuffer(VulkanGraphicsTest *test, const VkBufferCreateI
vmaCreateBuffer(allocator, &bufInfo, &allocInfo, &buffer, &alloc, NULL);

test->bufferAllocs[buffer] = alloc;

if(bufInfo.usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)
{
VkBufferDeviceAddressInfoKHR info = {
VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO_KHR,
NULL,
buffer,
};
address = vkGetBufferDeviceAddressKHR(test->device, &info);
}
}

void AllocatedBuffer::free()
1 change: 1 addition & 0 deletions util/test/demos/vk/vk_test.h
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ struct AllocatedBuffer
VmaAllocator allocator = NULL;
VkBuffer buffer = VK_NULL_HANDLE;
VmaAllocation alloc = {};
VkDeviceAddress address = 0;

AllocatedBuffer() {}
AllocatedBuffer(VulkanGraphicsTest *test, const VkBufferCreateInfo &bufInfo,

0 comments on commit dea24cd

Please sign in to comment.