4 changed files with 141 additions and 17 deletions
@ -0,0 +1,71 @@ |
|||||
|
// Copyright 2022 Simon Boyé
|
||||
|
|
||||
|
#include <vk/ShaderModule.h> |
||||
|
#include <vk/Context.h> |
||||
|
|
||||
|
#include <cassert> |
||||
|
|
||||
|
|
||||
|
namespace vk { |
||||
|
|
||||
|
|
||||
|
ShaderModule::ShaderModule() noexcept { |
||||
|
} |
||||
|
|
||||
|
ShaderModule::ShaderModule(Context& context, const std::vector<char>& code) |
||||
|
: m_context(&context) |
||||
|
{ |
||||
|
assert(m_context); |
||||
|
|
||||
|
VkShaderModuleCreateInfo create_info { |
||||
|
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
||||
|
.codeSize = code.size(), |
||||
|
.pCode = (uint32_t*)code.data(), |
||||
|
}; |
||||
|
if(vkCreateShaderModule( |
||||
|
context.device(), |
||||
|
&create_info, |
||||
|
nullptr, |
||||
|
&m_shader_module |
||||
|
) != VK_SUCCESS) |
||||
|
throw std::runtime_error("failed to create shader module"); |
||||
|
} |
||||
|
|
||||
|
ShaderModule::ShaderModule(Context& context, const char* path) |
||||
|
: ShaderModule(context, read_binary_file(path)) |
||||
|
{} |
||||
|
|
||||
|
ShaderModule::ShaderModule(ShaderModule&& other) noexcept |
||||
|
{ |
||||
|
swap(*this, other); |
||||
|
} |
||||
|
|
||||
|
ShaderModule::~ShaderModule() noexcept { |
||||
|
if(!is_null()) |
||||
|
destroy(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
ShaderModule& ShaderModule::operator=(ShaderModule&& other) noexcept { |
||||
|
swap(*this, other); |
||||
|
if(other) |
||||
|
other.destroy(); |
||||
|
return *this; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void ShaderModule::destroy() noexcept { |
||||
|
assert(!is_null()); |
||||
|
assert(m_context); |
||||
|
|
||||
|
vkDestroyShaderModule( |
||||
|
m_context->device(), |
||||
|
m_shader_module, |
||||
|
nullptr |
||||
|
); |
||||
|
|
||||
|
m_shader_module = nullptr; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
// Copyright 2022 Simon Boyé
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <core/utils.h> |
||||
|
|
||||
|
#include <vk/forward.h> |
||||
|
|
||||
|
#include <vulkan/vulkan.h> |
||||
|
|
||||
|
|
||||
|
namespace vk { |
||||
|
|
||||
|
|
||||
|
class ShaderModule { |
||||
|
public: |
||||
|
ShaderModule() noexcept; |
||||
|
ShaderModule(Context& context, const std::vector<char>& code); |
||||
|
ShaderModule(Context& context, const char* path); |
||||
|
ShaderModule(const ShaderModule&) = delete; |
||||
|
ShaderModule(ShaderModule&& other) noexcept; |
||||
|
~ShaderModule() noexcept; |
||||
|
|
||||
|
ShaderModule& operator=(const ShaderModule&) = delete; |
||||
|
ShaderModule& operator=(ShaderModule&& other) noexcept; |
||||
|
|
||||
|
explicit inline operator bool() const noexcept { |
||||
|
return !is_null(); |
||||
|
} |
||||
|
|
||||
|
inline bool is_null() const noexcept { |
||||
|
return m_shader_module == VK_NULL_HANDLE; |
||||
|
} |
||||
|
|
||||
|
inline const Context* context() const noexcept { |
||||
|
return m_context; |
||||
|
} |
||||
|
|
||||
|
inline Context* context() noexcept { |
||||
|
return m_context; |
||||
|
} |
||||
|
|
||||
|
inline operator VkShaderModule() noexcept { |
||||
|
return m_shader_module; |
||||
|
} |
||||
|
|
||||
|
inline VkShaderModule shader_module() noexcept { |
||||
|
return m_shader_module; |
||||
|
} |
||||
|
|
||||
|
friend inline void swap(ShaderModule& shader_module_0, ShaderModule& shader_module_1) noexcept { |
||||
|
using std::swap; |
||||
|
swap(shader_module_0.m_context, shader_module_1.m_context); |
||||
|
swap(shader_module_0.m_shader_module, shader_module_1.m_shader_module); |
||||
|
} |
||||
|
|
||||
|
void destroy() noexcept; |
||||
|
|
||||
|
private: |
||||
|
Context* m_context = nullptr; |
||||
|
VkShaderModule m_shader_module = VK_NULL_HANDLE; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue