From e3bdc7dc9da2c25c285af7896e22823af6eaccf9 Mon Sep 17 00:00:00 2001 From: Draklaw Date: Sun, 2 Jan 2022 01:07:09 +0100 Subject: [PATCH] Move some functions to Context. --- src/Vulkan/Context.cpp | 46 +++++++++++++++++++++++++++++++++++ src/Vulkan/Context.h | 5 ++++ src/VulkanTutorial.cpp | 54 ++++-------------------------------------- src/VulkanTutorial.h | 5 ---- 4 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/Vulkan/Context.cpp b/src/Vulkan/Context.cpp index 95a25cb..02f2664 100644 --- a/src/Vulkan/Context.cpp +++ b/src/Vulkan/Context.cpp @@ -240,6 +240,52 @@ void Context::set_object_name(VkObjectType type, uint64_t object, const std::str set_object_name(type, object, name.c_str()); } +VkShaderModule Context::create_shader_module(const std::vector bytecode) { + VkShaderModuleCreateInfo shader_info { + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = bytecode.size(), + .pCode = reinterpret_cast(bytecode.data()), + }; + + VkShaderModule shader_module = VK_NULL_HANDLE; + if(vkCreateShaderModule( + m_device, + &shader_info, + nullptr, + &shader_module + ) != VK_SUCCESS) + throw std::runtime_error("failed to create shader module"); + + return shader_module; +} + +VkShaderModule Context::create_shader_module_from_file(const char* path) { + auto const bytecode = read_binary_file(path); + try { + return create_shader_module(bytecode); + } + catch(std::exception err) { + throw std::runtime_error(cat("failed to create shader '", path, "':\n ", err.what())); + } + return VK_NULL_HANDLE; +} + +int32_t Context::find_memory(uint32_t type_filter, VkMemoryPropertyFlags properties) { + VkPhysicalDeviceMemoryProperties memory_properties; + vkGetPhysicalDeviceMemoryProperties( + m_physical_device, + &memory_properties + ); + + for(uint32_t type_index = 0; type_index < memory_properties.memoryTypeCount; type_index += 1) { + if(((1 << type_index) & type_filter) && + (memory_properties.memoryTypes[type_index].propertyFlags & properties) == properties) + return type_index; + } + + return -1; +} + void Context::destroy_instance(VkInstance& instance) { if(instance == nullptr) diff --git a/src/Vulkan/Context.h b/src/Vulkan/Context.h index b6e001b..45171c9 100644 --- a/src/Vulkan/Context.h +++ b/src/Vulkan/Context.h @@ -97,6 +97,11 @@ public: void set_object_name(VkObjectType type, uint64_t object, const char* name); void set_object_name(VkObjectType type, uint64_t object, const std::string& name); + VkShaderModule create_shader_module(const std::vector bytecode); + VkShaderModule create_shader_module_from_file(const char* path); + + int32_t find_memory(uint32_t type_filter, VkMemoryPropertyFlags properties); + void destroy_instance(VkInstance& instance); void destroy_debug_messenger(VkDebugUtilsMessengerEXT& debug_messenger); diff --git a/src/VulkanTutorial.cpp b/src/VulkanTutorial.cpp index e10a6db..c09dcb2 100644 --- a/src/VulkanTutorial.cpp +++ b/src/VulkanTutorial.cpp @@ -226,12 +226,14 @@ void VulkanTutorial::create_render_pass() { } void VulkanTutorial::create_graphic_pipeline() { - auto const vertex_shader_module = create_shader_module_from_file("shaders/shader.vert.spv"); + auto const vertex_shader_module = + m_context.create_shader_module_from_file("shaders/shader.vert.spv"); auto const vertex_shader_guard = make_guard([&]{ vkDestroyShaderModule(m_context.device(), vertex_shader_module, nullptr); }); - auto const fragment_shader_module = create_shader_module_from_file("shaders/shader.frag.spv"); + auto const fragment_shader_module = + m_context.create_shader_module_from_file("shaders/shader.frag.spv"); auto const fragment_shader_guard = make_guard([&]{ vkDestroyShaderModule(m_context.device(), fragment_shader_module, nullptr); }); @@ -456,7 +458,7 @@ void VulkanTutorial::create_vertex_buffer() { &memory_requirements ); - const auto memory_type = find_memory( + const auto memory_type = m_context.find_memory( memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT @@ -598,49 +600,3 @@ void VulkanTutorial::create_command_buffers() { throw("failed to record command buffer"); } } - -VkShaderModule VulkanTutorial::create_shader_module(const std::vector bytecode) { - VkShaderModuleCreateInfo shader_info { - .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, - .codeSize = bytecode.size(), - .pCode = reinterpret_cast(bytecode.data()), - }; - - VkShaderModule shader_module = VK_NULL_HANDLE; - if(vkCreateShaderModule( - m_context.device(), - &shader_info, - nullptr, - &shader_module - ) != VK_SUCCESS) - throw std::runtime_error("failed to create shader module"); - - return shader_module; -} - -VkShaderModule VulkanTutorial::create_shader_module_from_file(const char* path) { - auto const bytecode = read_binary_file(path); - try { - return create_shader_module(bytecode); - } - catch(std::exception err) { - throw std::runtime_error(cat("failed to create shader '", path, "':\n ", err.what())); - } - return VK_NULL_HANDLE; -} - -int32_t VulkanTutorial::find_memory(uint32_t type_filter, VkMemoryPropertyFlags properties) { - VkPhysicalDeviceMemoryProperties memory_properties; - vkGetPhysicalDeviceMemoryProperties( - m_context.physical_device(), - &memory_properties - ); - - for(uint32_t type_index = 0; type_index < memory_properties.memoryTypeCount; type_index += 1) { - if(((1 << type_index) & type_filter) && - (memory_properties.memoryTypes[type_index].propertyFlags & properties) == properties) - return type_index; - } - - return -1; -} diff --git a/src/VulkanTutorial.h b/src/VulkanTutorial.h index 125ecae..887ce92 100644 --- a/src/VulkanTutorial.h +++ b/src/VulkanTutorial.h @@ -48,11 +48,6 @@ private: void create_vertex_buffer(); void create_command_buffers(); - VkShaderModule create_shader_module(const std::vector bytecode); - VkShaderModule create_shader_module_from_file(const char* path); - - int32_t find_memory(uint32_t type_filter, VkMemoryPropertyFlags properties); - private: Vulkan::Context m_context; Vulkan::Swapchain m_swapchain;