Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi688 committed Apr 27, 2024
2 parents 99e0d83 + 0dfa394 commit 7ec805a
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ci-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

make -s setup
git submodule update
make -s build
make -s build WALL=1
make -s shader-clean
make -s clean
1 change: 1 addition & 0 deletions include/renderer/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ static INLINE_IF_RELEASE_MODE void __memcopy(void* const dst, const void* const
_debug_memcpy(dst, src, requested_size * count);
}

#define memcopy_void(dst, src, size) _debug_memcpy(dst, src, size)
#define memcopy(dst, src, type) __memcopy(dst, src, sizeof(*(src)), sizeof(*(dst)), sizeof(type), 1, __LINE__, __FUNCTION__, __FILE__)
#define memcopyv(dst, src, type, count) __memcopy(dst, src, sizeof(*(src)), sizeof(*(dst)), sizeof(type), count, __LINE__, __FUNCTION__, __FILE__)

Expand Down
2 changes: 2 additions & 0 deletions include/renderer/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ RENDERER_API function_signature(void, dictionary_free, dictionary_t* dictionary)
RENDERER_API function_signature(void, dictionary_get_at, dictionary_t* dictionary, buf_ucount_t index, void* out_key, void* out_value);
#define dictionary_get_value(...) define_alias_function_macro(dictionary_get_value, __VA_ARGS__)
RENDERER_API function_signature(void, dictionary_get_value, dictionary_t* dictionary, void* key, void* out_value);
#define dictionary_try_get_value_ptr(...) define_alias_function_macro(dictionary_try_get_value_ptr, __VA_ARGS__)
RENDERER_API function_signature(bool, dictionary_try_get_value_ptr, dictionary_t* dictionary, void* key, void** out_ptr);
#define dictionary_try_get_value(...) define_alias_function_macro(dictionary_try_get_value, __VA_ARGS__)
RENDERER_API function_signature(bool, dictionary_try_get_value, dictionary_t* dictionary, void* key, void* out_value);
#define dictionary_get_key_at(...) define_alias_function_macro(dictionary_get_key_at, __VA_ARGS__)
Expand Down
8 changes: 7 additions & 1 deletion include/renderer/internal/vulkan/vulkan_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ BEGIN_CPP_COMPATIBLE

// constructors and destructors
RENDERER_API vulkan_instance_t* vulkan_instance_new(memory_allocator_t* allocator);
RENDERER_API vulkan_instance_t* vulkan_instance_create(vulkan_renderer_t* renderer, const char* const* extensions, u32 extension_count);
RENDERER_API vulkan_instance_t* vulkan_instance_create(vulkan_renderer_t* renderer, const char* const* extensions, u32 extension_count, const char* const* layers, u32 layer_count);
RENDERER_API void vulkan_instance_destroy(vulkan_instance_t* instance);
RENDERER_API void vulkan_instance_release_resources(vulkan_instance_t* instance);

Expand All @@ -63,6 +63,12 @@ RENDERER_API VkExtensionProperties* vulkan_instance_get_extension_properties(vul
// returning bools
RENDERER_API bool vulkan_instance_is_extension_supported(vulkan_instance_t* instance, const char* extension, const char* layer_name);

/* vulkan_instance_get_filter_for_supported_layers: returns boolean filter array designating which layers are supported and which are not
* layer_count: number of layers (const char*)
* layers: list of required layer names (const char* const*)
*/
RENDERER_API bool* vulkan_instance_get_filter_for_supported_layers(memory_allocator_t* allocator, u32 layer_count, const char* const* layers);

// to string (s)
RENDERER_API void vulkan_instance_to_string(vulkan_instance_t* instance, BUFFER* string_buffer);

Expand Down
1 change: 1 addition & 0 deletions include/renderer/memory_allocation_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ typedef enum memory_allocation_type_t
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_QUEUE_FAMILY_PROPERTIES_ARRAY,
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_PHYSICAL_DEVICE_ARRAY,
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_EXTENSION_PROPERTIES_ARRAY,
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_LAYER_PROPERTIES_ARRAY,
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_PIPELINE_COLOR_BLEND_ATTACHMENT_STATE_ARRAY,
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_RECT2D_ARRAY,
MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_VIEWPORT_ARRAY,
Expand Down
5 changes: 5 additions & 0 deletions include/renderer/memory_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,19 @@ RENDERER_API void memory_allocator_destroy(memory_allocator_t* allocator);
/* allocates memory chunk and attaches appropriate debug information to that chunk */
#define memory_allocator_alloc_obj_array(allocator, allocation_type, type, count) CAST_TO(type*, memory_allocator_alloc(allocator, allocation_type, sizeof(type) * (count)))
#define memory_allocator_alloc_obj(allocator, allocation_type, type) CAST_TO(type*, memory_allocator_alloc(allocator, allocation_type, sizeof(type)))
#define memory_allocator_realloc_obj(allocator, old_ptr, allocation_type, type) CAST_TO(type*, memory_allocator_realloc(allocator, old_ptr, allocation_type, sizeof(type)))
#define memory_allocator_realloc_obj_array(allocator, old_ptr, allocation_type, type, count) CAST_TO(type*, memory_allocator_realloc(allocator, old_ptr, allocation_type, sizeof(type) * (count)))

#ifdef MEMORY_METRICS
# define memory_allocator_alloc(allocator, allocation_type, size) __memory_allocator_alloc(allocator, __memory_allocation_debug_info(allocation_type), size)
# define memory_allocator_realloc(allocator, old_ptr, allocation_type, size) __memory_allocator_realloc(allocator, __memory_allocation_debug_info(allocation_type), size)
#else
# define memory_allocator_alloc(allocator, allocation_type, size) heap_alloc(size)
# define memory_allocator_realloc(allocator, old_ptr, allocation_type, size) heap_realloc(old_ptr, size)
#endif

RENDERER_API void* __memory_allocator_alloc(memory_allocator_t* allocator, __memory_allocation_debug_info_t debug_info, u32 size);
RENDERER_API void* __memory_allocator_realloc(memory_allocator_t* allocator, void* old_ptr, __memory_allocation_debug_info_t debug_info, u32 size);
/* allocates memory chunk with alignment, and attaches appropriate debug information to that chunk */
#define memory_allocator_aligned_alloc(allocator, allocation_type, size, align) __memory_allocator_aligned_alloc(allocator, __memory_allocation_debug_info(allocation_type), size, align)
RENDERER_API void* __memory_allocator_aligned_alloc(memory_allocator_t* allocator, __memory_allocation_debug_info_t debug_info, u32 size, u32 align);
Expand Down
15 changes: 13 additions & 2 deletions source/renderer/dictionary.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ RENDERER_API function_signature(void, dictionary_get_value, dictionary_t* dictio
CALLTRACE_END();
}

RENDERER_API function_signature(bool, dictionary_try_get_value, dictionary_t* dictionary, void* key, void* out_value)
RENDERER_API function_signature(bool, dictionary_try_get_value_ptr, dictionary_t* dictionary, void* key, void** out_ptr)
{
CALLTRACE_BEGIN();
check_pre_condition(dictionary);
Expand All @@ -87,7 +87,18 @@ RENDERER_API function_signature(bool, dictionary_try_get_value, dictionary_t* di
if(index == BUF_INVALID_INDEX)
CALLTRACE_RETURN(false);
void* ptr = buf_get_ptr_at(buffer, index);
memcopyv(out_value, ptr + dictionary->key_size, u8, dictionary->value_size);
void* value_ptr = ptr + dictionary->key_size;
memcopy_void(out_ptr, &value_ptr, sizeof(DREF_VOID_PTR(out_ptr)));
CALLTRACE_RETURN(true);
}

RENDERER_API function_signature(bool, dictionary_try_get_value, dictionary_t* dictionary, void* key, void* out_value)
{
CALLTRACE_BEGIN();
void* value_ptr;
if(!dictionary_try_get_value_ptr(dictionary, key, &value_ptr))
return false;
memcopyv(out_value, value_ptr, u8, dictionary->value_size);
CALLTRACE_RETURN(true);
}

Expand Down
9 changes: 8 additions & 1 deletion source/renderer/memory_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ RENDERER_API void* __memory_allocator_alloc(memory_allocator_t* allocator, __mem
return __memory_allocator_aligned_alloc(allocator, debug_info, size, ALLOCATION_FLAG_NO_ALIGN_RESTRICTION);
}

RENDERER_API void* __memory_allocator_realloc(memory_allocator_t* allocator, void* old_ptr, __memory_allocation_debug_info_t debug_info, u32 size)
{
return __memory_allocator_aligned_realloc(allocator, old_ptr, debug_info, size, ALLOCATION_FLAG_NO_ALIGN_RESTRICTION);
}

RENDERER_API void* __memory_allocator_aligned_alloc(memory_allocator_t* allocator, __memory_allocation_debug_info_t debug_info, u32 size, u32 align)
{
_debug_assert__(size != 0);
allocate_result_t result = allocator->allocate(size, align, allocator->user_data);
if(result.flags != ALLOCATE_RESULT_SUCCESS)
return NULL;
Expand All @@ -180,6 +186,7 @@ RENDERER_API void* __memory_allocator_aligned_alloc(memory_allocator_t* allocato

RENDERER_API void* __memory_allocator_aligned_realloc(memory_allocator_t* allocator, void* old_ptr, __memory_allocation_debug_info_t debug_info, u32 size, u32 align)
{
_debug_assert__(size != 0);
memory_allocation_t* old_alloc = NULL;
buf_ucount_t index = BUF_INVALID_INDEX;

Expand Down Expand Up @@ -217,7 +224,7 @@ RENDERER_API void* __memory_allocator_aligned_realloc(memory_allocator_t* alloca

RENDERER_API void __memory_allocator_dealloc(memory_allocator_t* allocator, void* ptr)
{
if(ptr == NULL) return;
_debug_assert__(ptr != NULL);

buf_ucount_t index = dictionary_find_index_of(&allocator->allocation_map, &ptr);
if(index == BUF_INVALID_INDEX) return;
Expand Down
67 changes: 64 additions & 3 deletions source/renderer/vulkan/vulkan_instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RENDERER_API vulkan_instance_t* vulkan_instance_new(memory_allocator_t* allocato
return instance;
}

RENDERER_API vulkan_instance_t* vulkan_instance_create(vulkan_renderer_t* renderer, const char* const* extensions, u32 extension_count)
RENDERER_API vulkan_instance_t* vulkan_instance_create(vulkan_renderer_t* renderer, const char* const* extensions, u32 extension_count, const char* const* layers, u32 layer_count)
{
vulkan_instance_t* instance = vulkan_instance_new(renderer->allocator);
instance->renderer = renderer;
Expand Down Expand Up @@ -73,13 +73,26 @@ RENDERER_API vulkan_instance_t* vulkan_instance_create(vulkan_renderer_t* render
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = VK_API_VERSION_1_0
};

// create a filtered list of layers which are supported only
bool* layer_filter = vulkan_instance_get_filter_for_supported_layers(renderer->allocator, layer_count, layers);
const char* supported_layers[layer_count];
u32 supported_layer_count = 0;
for(u32 i = 0; i < layer_count; i++)
{
if(layer_filter[i])
supported_layers[supported_layer_count++] = layers[i];
else
DEBUG_LOG_WARNING("Layer %s is not supported, ignored", layers[i]);
}
memory_allocator_dealloc(renderer->allocator, layer_filter);

VkInstanceCreateInfo create_info =
{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app_info,
.enabledLayerCount = 0, // for now let it be 0
.ppEnabledLayerNames = NULL, // for now let it be NULL
.enabledLayerCount = supported_layer_count,
.ppEnabledLayerNames = supported_layers,
.enabledExtensionCount = supported_extension_count,
.ppEnabledExtensionNames = supported_extensions
};
Expand Down Expand Up @@ -173,6 +186,54 @@ RENDERER_API bool vulkan_instance_is_extension_supported(vulkan_instance_t* inst
return false;
}

RENDERER_API bool* vulkan_instance_get_filter_for_supported_layers(memory_allocator_t* allocator, u32 layer_count, const char* const* layers)
{
if(layer_count == 0)
return NULL;

// fetch list of VkLayerProperties

u32 property_count;
vkCall(vkEnumerateInstanceLayerProperties(&property_count, NULL));

VkLayerProperties* properties = NULL;
VkResult result = VK_INCOMPLETE;
while(result == VK_INCOMPLETE)
{
if(properties != NULL)
properties = memory_allocator_realloc_obj_array(allocator, properties, MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_LAYER_PROPERTIES_ARRAY, VkLayerProperties, property_count);
else
properties = memory_allocator_alloc_obj_array(allocator, MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_LAYER_PROPERTIES_ARRAY, VkLayerProperties, property_count);
result = vkEnumerateInstanceLayerProperties(&property_count, properties);
}
vulkan_result_assert_success(result);

// create a filter for which layers are supported and which are not supported
bool* filter = memory_allocator_alloc_obj_array(allocator, MEMORY_ALLOCATION_TYPE_OBJ_BOOL_ARRAY, bool, layer_count);
for(u32 i = 0; i < layer_count; i++)
filter[i] = false;

u32 count = 0;
// check each requested layer for any match
for(u32 i = 0; i < property_count; i++)
{
for(u32 j = 0; j < layer_count; j++)
{
if(strcmp(properties[i].layerName, layers[j]) == 0)
{
filter[j] = true;
++count;
}
}
// do not proceed for more checks if all layers are supported
if(count == layer_count)
break;
}

memory_allocator_dealloc(instance->renderer->allocator, properties);
return filter;
}

// to string (s)
RENDERER_API void vulkan_instance_to_string(vulkan_instance_t* instance, BUFFER* string_buffer)
{
Expand Down
28 changes: 16 additions & 12 deletions source/renderer/vulkan/vulkan_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ RENDERER_API vulkan_renderer_t* vulkan_renderer_init(renderer_t* _renderer, vulk

// create a vulkan instance with extensions VK_KHR_surface, VK_KHR_win32_surface
const char* extensions[3] = { "VK_KHR_surface", PLATFORM_SPECIFIC_VK_SURFACE_EXTENSION };
renderer->instance = vulkan_instance_create(renderer, extensions, 2);
const char* layers[] = { "VK_LAYER_KHRONOS_validation", "VK_LAYER_KHRONOS_synchronization2" };
renderer->instance = vulkan_instance_create(renderer, extensions, 2, layers, SIZEOF_ARRAY(layers));
vulkan_physical_device_t* physical_devices = vulkan_instance_get_physical_devices(renderer->instance);
u32 physical_device_count = vulkan_instance_get_physical_device_count(renderer->instance);

Expand Down Expand Up @@ -270,7 +271,8 @@ DEBUG_BLOCK
VkExtent2D image_extent = find_extent(&surface_capabilities, renderer->window);

// setup image count
u32 image_count = clamp_u32(surface_capabilities.minImageCount + 1, surface_capabilities.minImageCount, surface_capabilities.maxImageCount);
// NOTE: if VkSurfaceCapabilitiesKHR::maxImageCount equals 0, then there is no maximum limit for image count
u32 image_count = clamp_u32(3, surface_capabilities.minImageCount, (surface_capabilities.maxImageCount == 0) ? U32_MAX : surface_capabilities.maxImageCount);

// create logical device
VkPhysicalDeviceFeatures* minimum_required_features = memory_allocator_alloc_obj(renderer->allocator, MEMORY_ALLOCATION_TYPE_OBJ_VKAPI_PHYSICAL_DEVICE_FEATURES, VkPhysicalDeviceFeatures);
Expand All @@ -292,8 +294,6 @@ DEBUG_BLOCK
log_u32(queue_family_indices[1]);

extensions[0] = "VK_KHR_swapchain";
extensions[1] = "VK_KHR_driver_properties";
extensions[2] = "VK_KHR_get_physical_device_properties2";

vulkan_logical_device_create_info_t logical_device_create_info =
{
Expand Down Expand Up @@ -321,19 +321,16 @@ DEBUG_BLOCK
renderer->vo_render_finished_semaphore = get_semaphore(renderer);
renderer->vo_fence = get_unsigned_fence(renderer);

//Set up graphics queue
vkGetDeviceQueue(renderer->logical_device->vo_handle, queue_family_indices[0], 0, &renderer->vo_graphics_queue);

// setup command pool
renderer->vo_command_pool = vulkan_command_pool_create(renderer, queue_family_indices[0], VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);

// setup command buffers
renderer->vo_command_buffers = memory_allocator_alloc_obj_array(renderer->allocator, MEMORY_ALLOCATION_TYPE_OBJ_VK_CMD_BUFFER_ARRAY, VkCommandBuffer, image_count);
vulkan_command_buffer_allocatev(renderer, VK_COMMAND_BUFFER_LEVEL_PRIMARY, renderer->vo_command_pool, image_count, renderer->vo_command_buffers);
log_msg("Command Buffers has been allocated successfully\n");

// auxiliary command buffer will be used by vulkan_swapchain_create for transitioning its images to presentatble initially
// so, it must be created before any call to vulkan_swapchain_create or functions alike.
vulkan_command_buffer_allocatev(renderer, VK_COMMAND_BUFFER_LEVEL_PRIMARY, renderer->vo_command_pool, 1, &renderer->vo_aux_command_buffer);

//Set up graphics queue
vkGetDeviceQueue(renderer->logical_device->vo_handle, queue_family_indices[0], 0, &renderer->vo_graphics_queue);

//Create Swapchain
vulkan_swapchain_create_info_t swapchain_info =
{
Expand All @@ -348,6 +345,13 @@ DEBUG_BLOCK
memcopy(&renderer->swapchain_create_info, &swapchain_info, vulkan_swapchain_create_info_t);
renderer->swapchain = vulkan_swapchain_create(renderer, &swapchain_info);

// setup command buffers
// update the image_count variable as the actual number of images allocated for the swapchain might be greater than what had been requested
image_count = renderer->swapchain->image_count;
renderer->vo_command_buffers = memory_allocator_alloc_obj_array(renderer->allocator, MEMORY_ALLOCATION_TYPE_OBJ_VK_CMD_BUFFER_ARRAY, VkCommandBuffer, image_count);
vulkan_command_buffer_allocatev(renderer, VK_COMMAND_BUFFER_LEVEL_PRIMARY, renderer->vo_command_pool, image_count, renderer->vo_command_buffers);
log_msg("Command Buffers has been allocated successfully\n");


//Create descripter pool
VkDescriptorPoolSize sizes[3] =
Expand Down
2 changes: 1 addition & 1 deletion source/renderer/vulkan/vulkan_swapchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ RENDERER_API void vulkan_swapchain_release_resources(vulkan_swapchain_t* swapcha

RENDERER_API u32 vulkan_swapchain_acquire_next_image(vulkan_swapchain_t* swapchain)
{
vkAcquireNextImageKHR(swapchain->renderer->logical_device->vo_handle, swapchain->vo_handle, UINT64_MAX, swapchain->renderer->vo_image_available_semaphore, VK_NULL_HANDLE, &(swapchain->current_image_index));
vkCall(vkAcquireNextImageKHR(swapchain->renderer->logical_device->vo_handle, swapchain->vo_handle, UINT64_MAX, swapchain->renderer->vo_image_available_semaphore, VK_NULL_HANDLE, &(swapchain->current_image_index)));
return swapchain->current_image_index;
}

Expand Down

0 comments on commit 7ec805a

Please sign in to comment.