Skip to content

Commit 5769d03

Browse files
committed
vk_runtime: do not use imported memory where unavailable
1 parent ebb8b1a commit 5769d03

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/runtime/vulkan/vk_runtime_private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ struct ProgramResourceInfo_ {
203203
ProgramResourceInfo* parent;
204204
size_t offset;
205205

206-
bool host_owned;
206+
bool host_backed_allocation;
207207
char* host_ptr;
208208

209+
char* staging;
210+
209211
AddressSpace as;
210212
size_t size;
211213
VkrBuffer* buffer;

src/runtime/vulkan/vk_runtime_program.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ static bool extract_resources_layout(VkrSpecProgram* program, VkDescriptorSetLay
172172
.as = as,
173173
.set = set,
174174
.binding = binding,
175-
.host_owned = true,
176175
};
177176
growy_append_object(resources, res_info);
178177
program->resources.num_resources++;
@@ -199,6 +198,11 @@ static bool extract_resources_layout(VkrSpecProgram* program, VkDescriptorSetLay
199198
// TODO initial value
200199
}
201200

201+
if (vkr_can_import_host_memory(program->device))
202+
res_info->host_backed_allocation = true;
203+
else
204+
res_info->staging = calloc(1, res_info->size);
205+
202206
VkDescriptorSetLayoutBinding vk_binding = {
203207
.binding = binding,
204208
.descriptorType = as_to_descriptor_type(as),
@@ -366,11 +370,22 @@ static bool allocate_sets(VkrSpecProgram* program) {
366370
return true;
367371
}
368372

373+
static void flush_staged_data(VkrSpecProgram* program) {
374+
for (size_t i = 0; i < program->resources.num_resources; i++) {
375+
ProgramResourceInfo* resource = program->resources.resources[i];
376+
if (resource->staging) {
377+
copy_to_buffer(resource->buffer, 0, resource->buffer, resource->size);
378+
free(resource->staging);
379+
}
380+
}
381+
}
382+
369383
static bool prepare_resources(VkrSpecProgram* program) {
370384
for (size_t i = 0; i < program->resources.num_resources; i++) {
371385
ProgramResourceInfo* resource = program->resources.resources[i];
372386

373-
if (resource->host_owned) {
387+
if (resource->host_backed_allocation) {
388+
assert(vkr_can_import_host_memory(program->device));
374389
resource->host_ptr = alloc_aligned(resource->size, program->device->caps.properties.external_memory_host.minImportedHostPointerAlignment);
375390
resource->buffer = import_buffer_host(program->device, resource->host_ptr, resource->size);
376391
} else {
@@ -384,12 +399,17 @@ static bool prepare_resources(VkrSpecProgram* program) {
384399
free(zeroes);
385400

386401
if (resource->parent) {
387-
assert(resource->parent->host_ptr);
388-
char* parent = resource->parent->host_ptr;
389-
*((uint64_t*) (parent + resource->offset)) = get_buffer_device_pointer(resource->buffer);
402+
char* dst = resource->parent->host_ptr;
403+
if (!dst) {
404+
dst = resource->parent->staging;
405+
}
406+
assert(dst);
407+
*((uint64_t*) (dst + resource->offset)) = get_buffer_device_pointer(resource->buffer);
390408
}
391409
}
392410

411+
flush_staged_data(program);
412+
393413
return true;
394414
}
395415

@@ -436,7 +456,7 @@ void destroy_specialized_program(VkrSpecProgram* spec) {
436456
ProgramResourceInfo* resource = spec->resources.resources[i];
437457
if (resource->buffer)
438458
destroy_buffer(resource->buffer);
439-
if (resource->host_ptr && resource->host_owned)
459+
if (resource->host_ptr && resource->host_backed_allocation)
440460
free_aligned(resource->host_ptr);
441461
}
442462
free(spec->resources.resources);

0 commit comments

Comments
 (0)