Skip to content

Commit b0871e1

Browse files
committed
fix max heap size
1 parent dd3cb37 commit b0871e1

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

rvk/device.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ void Physical_Device::get_present_modes(VkSurfaceKHR surface, Slice<VkPresentMod
293293
device, surface, &n, const_cast<VkPresentModeKHR*>(modes.data())));
294294
}
295295

296+
u64 Physical_Device::max_allocation() {
297+
return properties_.maintenance3.maxMemoryAllocationSize;
298+
}
299+
296300
u64 Physical_Device::heap_size(u32 heap) {
297301
u32 heap_idx = properties_.memory.memoryProperties.memoryTypes[heap].heapIndex;
298302
return properties_.memory.memoryProperties.memoryHeaps[heap_idx].size;

rvk/device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct Physical_Device {
4141
Opt<u32> queue_index(Queue_Family family);
4242
Opt<u32> present_queue_index(VkSurfaceKHR surface);
4343

44+
u64 max_allocation();
4445
u64 heap_size(u32 heap);
4546
Pair<u64, u64> heap_stat(u32 heap);
4647
Opt<u32> heap_index(u32 mask, u32 properties);

rvk/memory.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ Opt<Image> Device_Memory::make(VkExtent3D extent, VkFormat format, VkImageUsageF
101101

102102
VkImageMemoryRequirementsInfo2 image_requirements = {
103103
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
104-
.pNext = null,
105104
.image = image,
106105
};
107106

108107
VkMemoryRequirements2 memory_requirements = {
109108
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
110-
.pNext = null,
111109
};
112110

113111
vkGetImageMemoryRequirements2(*device, &image_requirements, &memory_requirements);
@@ -153,12 +151,20 @@ Opt<Buffer> Device_Memory::make(u64 size, VkBufferUsageFlags usage) {
153151

154152
RVK_CHECK(vkCreateBuffer(*device, &info, null, &buffer));
155153

156-
VkMemoryRequirements memory_requirements = {};
157-
vkGetBufferMemoryRequirements(*device, buffer, &memory_requirements);
154+
VkBufferMemoryRequirementsInfo2 buffer_requirements = {
155+
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
156+
.buffer = buffer,
157+
};
158158

159-
auto address =
160-
allocator.allocate(memory_requirements.size,
161-
Math::max(memory_requirements.alignment, buffer_image_granularity));
159+
VkMemoryRequirements2 memory_requirements = {
160+
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
161+
};
162+
163+
vkGetBufferMemoryRequirements2(*device, &buffer_requirements, &memory_requirements);
164+
165+
auto address = allocator.allocate(
166+
memory_requirements.memoryRequirements.size,
167+
Math::max(memory_requirements.memoryRequirements.alignment, buffer_image_granularity));
162168
if(!address.ok()) {
163169
vkDestroyBuffer(*device, buffer, null);
164170
return {};
@@ -187,6 +193,8 @@ Image::~Image() {
187193
}
188194
image = null;
189195
address = null;
196+
extent_ = {};
197+
format_ = VK_FORMAT_UNDEFINED;
190198
}
191199

192200
Image::Image(Image&& src) {

rvk/rvk.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct Vk {
9797
Arc<Physical_Device, Alloc> physical_device;
9898
Arc<Device, Alloc> device;
9999
Arc<Device_Memory, Alloc> host_memory;
100-
Arc<Device_Memory, Alloc> device_memory;
100+
Vec<Arc<Device_Memory, Alloc>, Alloc> device_memories;
101101
Arc<Swapchain, Alloc> swapchain;
102102
Arc<Descriptor_Pool, Alloc> descriptor_pool;
103103
Arc<Command_Pool_Manager<Queue_Family::graphics>, Alloc> graphics_command_pool;
@@ -165,9 +165,16 @@ Vk::Vk(Config config) {
165165
host_memory = Arc<Device_Memory, Alloc>::make(physical_device, device.dup(), Heap::host,
166166
config.host_heap);
167167

168-
device_memory = Arc<Device_Memory, Alloc>::make(physical_device, device.dup(), Heap::device,
169-
device->heap_size(Heap::device) -
170-
config.device_heap_margin);
168+
{
169+
u64 allocated = 0;
170+
u64 target = device->heap_size(Heap::device) - config.device_heap_margin;
171+
while(allocated < target) {
172+
u64 size = Math::min(target - allocated, physical_device->max_allocation());
173+
device_memories.push(
174+
Arc<Device_Memory, Alloc>::make(physical_device, device.dup(), Heap::device, size));
175+
allocated += size;
176+
}
177+
}
171178

172179
descriptor_pool = Arc<Descriptor_Pool, Alloc>::make(device.dup(), config.descriptors_per_type,
173180
config.ray_tracing);
@@ -219,8 +226,8 @@ void Vk::imgui() {
219226
Text("Swapchain images: %u | Max frames: %u", swapchain->slot_count(), state.frames_in_flight);
220227
Text("Extent: %ux%u", swapchain->extent().width, swapchain->extent().height);
221228

222-
if(TreeNodeEx("Device Heap", ImGuiTreeNodeFlags_DefaultOpen)) {
223-
device_memory->imgui();
229+
if(TreeNodeEx("Device Heaps", ImGuiTreeNodeFlags_DefaultOpen)) {
230+
for(auto& device_memory : device_memories) device_memory->imgui();
224231
TreePop();
225232
}
226233
if(TreeNodeEx("Host Heap", ImGuiTreeNodeFlags_DefaultOpen)) {
@@ -438,11 +445,21 @@ Semaphore Vk::make_semaphore() {
438445
}
439446

440447
Opt<TLAS::Staged> Vk::make_tlas(Buffer instances, u32 n_instances) {
441-
return TLAS::make(device_memory.dup(), move(instances), n_instances);
448+
for(auto& device_memory : device_memories) {
449+
if(auto tlas = TLAS::make(device_memory.dup(), move(instances), n_instances); tlas.ok()) {
450+
return tlas;
451+
}
452+
}
453+
return {};
442454
}
443455

444456
Opt<BLAS::Staged> Vk::make_blas(Buffer geometry, Vec<BLAS::Offsets, Alloc> offsets) {
445-
return BLAS::make(device_memory.dup(), move(geometry), move(offsets));
457+
for(auto& device_memory : device_memories) {
458+
if(auto blas = BLAS::make(device_memory.dup(), move(geometry), move(offsets)); blas.ok()) {
459+
return blas;
460+
}
461+
}
462+
return {};
446463
}
447464

448465
Pipeline Vk::make_pipeline(Pipeline::Info info) {
@@ -557,11 +574,21 @@ Opt<Buffer> make_staging(u64 size) {
557574
}
558575

559576
Opt<Buffer> make_buffer(u64 size, VkBufferUsageFlags usage) {
560-
return impl::singleton->device_memory->make(size, usage);
577+
for(auto& device_memory : impl::singleton->device_memories) {
578+
if(auto buf = device_memory->make(size, usage); buf.ok()) {
579+
return buf;
580+
}
581+
}
582+
return {};
561583
}
562584

563585
Opt<Image> make_image(VkExtent3D extent, VkFormat format, VkImageUsageFlags usage) {
564-
return impl::singleton->device_memory->make(extent, format, usage);
586+
for(auto& device_memory : impl::singleton->device_memories) {
587+
if(auto img = device_memory->make(extent, format, usage); img.ok()) {
588+
return img;
589+
}
590+
}
591+
return {};
565592
}
566593

567594
Opt<TLAS::Staged> make_tlas(Buffer instances, u32 n_instances) {

0 commit comments

Comments
 (0)