]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/module_backend.cpp
Initial implementation of Vulkan backend
[libs/gl.git] / source / backends / vulkan / module_backend.cpp
1 #include <stdexcept>
2 #include "device.h"
3 #include "module.h"
4 #include "module_backend.h"
5 #include "vulkan.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 VulkanSpirVModule::VulkanSpirVModule():
13         device(Device::get_current())
14 { }
15
16 VulkanSpirVModule::VulkanSpirVModule(VulkanSpirVModule &&other):
17         device(other.device),
18         handle(other.handle)
19 {
20         other.handle = 0;
21 }
22
23 VulkanSpirVModule::~VulkanSpirVModule()
24 {
25         const VulkanFunctions &vk = device.get_functions();
26
27         if(handle)
28                 vk.DestroyShaderModule(handle);
29 }
30
31 void VulkanSpirVModule::create()
32 {
33         const vector<uint32_t> &code = static_cast<const SpirVModule *>(this)->code;
34         const VulkanFunctions &vk = device.get_functions();
35
36         VkShaderModuleCreateInfo module_info = { };
37         module_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
38         module_info.codeSize = code.size()*4;
39         module_info.pCode = code.data();
40
41         vk.CreateShaderModule(module_info, handle);
42 }
43
44
45 unsigned get_vulkan_stage(unsigned stage)
46 {
47         switch(stage)
48         {
49         case SpirVModule::VERTEX: return VK_SHADER_STAGE_VERTEX_BIT;
50         case SpirVModule::GEOMETRY: return VK_SHADER_STAGE_GEOMETRY_BIT;
51         case SpirVModule::FRAGMENT: return VK_SHADER_STAGE_FRAGMENT_BIT;
52         default: throw invalid_argument("get_vulkan_stage");
53         }
54 }
55
56 } // namespace GL
57 } // namespace Msp