1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef VK_PIPELINE_CACHE_HPP_
16 #define VK_PIPELINE_CACHE_HPP_
17
18 #include "VkObject.hpp"
19 #include "VkSpecializationInfo.hpp"
20
21 #include "marl/mutex.h"
22 #include "marl/tsa.h"
23
24 #include <cstring>
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30
31 namespace sw {
32
33 class ComputeProgram;
34 class SpirvShader;
35
36 } // namespace sw
37
38 namespace vk {
39
40 class PipelineLayout;
41 class RenderPass;
42
43 class PipelineCache : public Object<PipelineCache, VkPipelineCache>
44 {
45 public:
46 PipelineCache(const VkPipelineCacheCreateInfo *pCreateInfo, void *mem);
47 virtual ~PipelineCache();
48 void destroy(const VkAllocationCallbacks *pAllocator);
49
50 static size_t ComputeRequiredAllocationSize(const VkPipelineCacheCreateInfo *pCreateInfo);
51
52 VkResult getData(size_t *pDataSize, void *pData);
53 VkResult merge(uint32_t srcCacheCount, const VkPipelineCache *pSrcCaches);
54
55 struct SpirvShaderKey
56 {
57 SpirvShaderKey(const VkShaderStageFlagBits pipelineStage,
58 const std::string &entryPointName,
59 const std::vector<uint32_t> &insns,
60 const vk::RenderPass *renderPass,
61 const uint32_t subpassIndex,
62 const vk::SpecializationInfo &specializationInfo);
63
64 bool operator<(const SpirvShaderKey &other) const;
65
getPipelineStagevk::PipelineCache::SpirvShaderKey66 const VkShaderStageFlagBits &getPipelineStage() const { return pipelineStage; }
getEntryPointNamevk::PipelineCache::SpirvShaderKey67 const std::string &getEntryPointName() const { return entryPointName; }
getInsnsvk::PipelineCache::SpirvShaderKey68 const std::vector<uint32_t> &getInsns() const { return insns; }
getRenderPassvk::PipelineCache::SpirvShaderKey69 const vk::RenderPass *getRenderPass() const { return renderPass; }
getSubpassIndexvk::PipelineCache::SpirvShaderKey70 uint32_t getSubpassIndex() const { return subpassIndex; }
getSpecializationInfovk::PipelineCache::SpirvShaderKey71 const VkSpecializationInfo *getSpecializationInfo() const { return specializationInfo.get(); }
72
73 private:
74 const VkShaderStageFlagBits pipelineStage;
75 const std::string entryPointName;
76 const std::vector<uint32_t> insns;
77 const vk::RenderPass *renderPass;
78 const uint32_t subpassIndex;
79 const vk::SpecializationInfo specializationInfo;
80 };
81
82 // getOrCreateShader() queries the cache for a shader with the given key.
83 // If one is found, it is returned, otherwise create() is called, the
84 // returned shader is added to the cache, and it is returned.
85 // Function must be a function of the signature:
86 // std::shared_ptr<sw::SpirvShader>()
87 template<typename Function>
88 inline std::shared_ptr<sw::SpirvShader> getOrCreateShader(const PipelineCache::SpirvShaderKey &key, Function &&create);
89
90 struct ComputeProgramKey
91 {
ComputeProgramKeyvk::PipelineCache::ComputeProgramKey92 ComputeProgramKey(const sw::SpirvShader *shader, const vk::PipelineLayout *layout)
93 : shader(shader)
94 , layout(layout)
95 {}
96
operator <vk::PipelineCache::ComputeProgramKey97 bool operator<(const ComputeProgramKey &other) const
98 {
99 return std::tie(shader, layout) < std::tie(other.shader, other.layout);
100 }
101
getShadervk::PipelineCache::ComputeProgramKey102 const sw::SpirvShader *getShader() const { return shader; }
getLayoutvk::PipelineCache::ComputeProgramKey103 const vk::PipelineLayout *getLayout() const { return layout; }
104
105 private:
106 const sw::SpirvShader *shader;
107 const vk::PipelineLayout *layout;
108 };
109
110 // getOrCreateComputeProgram() queries the cache for a compute program with
111 // the given key.
112 // If one is found, it is returned, otherwise create() is called, the
113 // returned program is added to the cache, and it is returned.
114 // Function must be a function of the signature:
115 // std::shared_ptr<sw::ComputeProgram>()
116 template<typename Function>
117 inline std::shared_ptr<sw::ComputeProgram> getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create);
118
119 private:
120 struct CacheHeader
121 {
122 uint32_t headerLength;
123 uint32_t headerVersion;
124 uint32_t vendorID;
125 uint32_t deviceID;
126 uint8_t pipelineCacheUUID[VK_UUID_SIZE];
127 };
128
129 size_t dataSize = 0;
130 uint8_t *data = nullptr;
131
132 marl::mutex spirvShadersMutex;
133 std::map<SpirvShaderKey, std::shared_ptr<sw::SpirvShader>> spirvShaders GUARDED_BY(spirvShadersMutex);
134
135 marl::mutex computeProgramsMutex;
136 std::map<ComputeProgramKey, std::shared_ptr<sw::ComputeProgram>> computePrograms GUARDED_BY(computeProgramsMutex);
137 };
138
Cast(VkPipelineCache object)139 static inline PipelineCache *Cast(VkPipelineCache object)
140 {
141 return PipelineCache::Cast(object);
142 }
143
144 template<typename Function>
getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey & key,Function && create)145 std::shared_ptr<sw::ComputeProgram> PipelineCache::getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create)
146 {
147 marl::lock lock(computeProgramsMutex);
148
149 auto it = computePrograms.find(key);
150 if(it != computePrograms.end()) { return it->second; }
151
152 auto created = create();
153 computePrograms.emplace(key, created);
154 return created;
155 }
156
157 template<typename Function>
getOrCreateShader(const PipelineCache::SpirvShaderKey & key,Function && create)158 std::shared_ptr<sw::SpirvShader> PipelineCache::getOrCreateShader(const PipelineCache::SpirvShaderKey &key, Function &&create)
159 {
160 marl::lock lock(spirvShadersMutex);
161
162 auto it = spirvShaders.find(key);
163 if(it != spirvShaders.end()) { return it->second; }
164
165 auto created = create();
166 spirvShaders.emplace(key, created);
167 return created;
168 }
169
170 } // namespace vk
171
172 #endif // VK_PIPELINE_CACHE_HPP_
173