Browse Source

Move some functions to Context.

master
Draklaw 4 years ago
parent
commit
e3bdc7dc9d
  1. 46
      src/Vulkan/Context.cpp
  2. 5
      src/Vulkan/Context.h
  3. 54
      src/VulkanTutorial.cpp
  4. 5
      src/VulkanTutorial.h

46
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<char> bytecode) {
VkShaderModuleCreateInfo shader_info {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = bytecode.size(),
.pCode = reinterpret_cast<const uint32_t*>(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)

5
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<char> 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);

54
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<char> bytecode) {
VkShaderModuleCreateInfo shader_info {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = bytecode.size(),
.pCode = reinterpret_cast<const uint32_t*>(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;
}

5
src/VulkanTutorial.h

@ -48,11 +48,6 @@ private:
void create_vertex_buffer();
void create_command_buffers();
VkShaderModule create_shader_module(const std::vector<char> 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;

Loading…
Cancel
Save