]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/pipelinecache.cpp
Support compute shaders and compute operations
[libs/gl.git] / source / backends / vulkan / pipelinecache.cpp
1 #include "device.h"
2 #include "pipelinecache.h"
3 #include "pipelinestate.h"
4 #include "renderpass.h"
5 #include "vulkan.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 PipelineCache::PipelineCache(Device &d):
13         device(d)
14 { }
15
16 PipelineCache::~PipelineCache()
17 {
18         const VulkanFunctions &vk = device.get_functions();
19
20         for(const auto &kvp: render_passes)
21                 vk.DestroyRenderPass(kvp.second);
22         for(const auto &kvp: pipelines)
23                 vk.DestroyPipeline(kvp.second);
24 }
25
26 VkRenderPass PipelineCache::get_render_pass(const RenderPass &rp)
27 {
28         uint64_t key = rp.compute_hash();
29         auto i = render_passes.find(key);
30         if(i!=render_passes.end())
31                 return i->second;
32
33         const VulkanFunctions &vk = device.get_functions();
34
35         vector<char> buffer;
36         rp.fill_creation_info(buffer);
37         const VkRenderPassCreateInfo *creation_info = reinterpret_cast<const VkRenderPassCreateInfo *>(buffer.data());
38
39         VkRenderPass render_pass;
40         vk.CreateRenderPass(*creation_info, render_pass);
41
42         render_passes.insert(make_pair(key, render_pass));
43
44         return render_pass;
45 }
46
47 VkPipeline PipelineCache::get_pipeline(const PipelineState &ps)
48 {
49         const VulkanFunctions &vk = device.get_functions();
50
51         uint64_t key = ps.compute_hash();
52         auto i = pipelines.find(key);
53         if(i!=pipelines.end())
54                 return i->second;
55
56         vector<char> buffer;
57         ps.fill_creation_info(buffer);
58
59         VkStructureType type = *reinterpret_cast<const VkStructureType *>(buffer.data());
60         VkPipeline pipeline;
61         if(type==VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO)
62         {
63                 const VkComputePipelineCreateInfo *creation_info = reinterpret_cast<const VkComputePipelineCreateInfo *>(buffer.data());
64                 vk.CreateComputePipelines(0, 1, creation_info, &pipeline);
65         }
66         else
67         {
68                 const VkGraphicsPipelineCreateInfo *creation_info = reinterpret_cast<const VkGraphicsPipelineCreateInfo *>(buffer.data());
69                 vk.CreateGraphicsPipelines(0, 1, creation_info, &pipeline);
70         }
71
72         pipelines.insert(make_pair(key, pipeline));
73
74         return pipeline;
75 }
76
77 } // namespace GL
78 } // namespace Msp