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 #include "Pipeline/SpirvBinary.hpp"
21
22 #include "marl/mutex.h"
23 #include "marl/tsa.h"
24
25 #include <cstring>
26 #include <functional>
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <vector>
31
32 namespace sw {
33
34 class ComputeProgram;
35 class SpirvShader;
36
37 } // namespace sw
38
39 namespace vk {
40
41 class PipelineLayout;
42 class RenderPass;
43
44 class PipelineCache : public Object<PipelineCache, VkPipelineCache>
45 {
46 public:
GetAllocationScope()47 static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_CACHE; }
48
49 PipelineCache(const VkPipelineCacheCreateInfo *pCreateInfo, void *mem);
50 virtual ~PipelineCache();
51 void destroy(const VkAllocationCallbacks *pAllocator);
52
53 static size_t ComputeRequiredAllocationSize(const VkPipelineCacheCreateInfo *pCreateInfo);
54
55 VkResult getData(size_t *pDataSize, void *pData);
56 VkResult merge(uint32_t srcCacheCount, const VkPipelineCache *pSrcCaches);
57
58 struct SpirvBinaryKey
59 {
60 SpirvBinaryKey(const sw::SpirvBinary &spirv,
61 const VkSpecializationInfo *specializationInfo,
62 bool robustBufferAccess,
63 bool optimize);
64
65 bool operator<(const SpirvBinaryKey &other) const;
66
getBinaryvk::PipelineCache::SpirvBinaryKey67 const sw::SpirvBinary &getBinary() const { return spirv; }
getSpecializationInfovk::PipelineCache::SpirvBinaryKey68 const VkSpecializationInfo *getSpecializationInfo() const { return specializationInfo.get(); }
getOptimizationvk::PipelineCache::SpirvBinaryKey69 bool getOptimization() const { return optimize; }
70
71 private:
72 const sw::SpirvBinary spirv;
73 const vk::SpecializationInfo specializationInfo;
74 const bool robustBufferAccess;
75 const bool optimize;
76 };
77
78 // contains() queries whether the cache contains a shader with the given key.
79 inline bool contains(const PipelineCache::SpirvBinaryKey &key);
80
81 // getOrOptimizeSpirv() queries the cache for a shader with the given key.
82 // If one is found, it is returned, otherwise create() is called, the
83 // returned SPIR-V binary is added to the cache, and it is returned.
84 // CreateOnCacheMiss must be a function of the signature:
85 // sw::ShaderBinary()
86 template<typename CreateOnCacheMiss, typename CacheHit>
87 inline sw::SpirvBinary getOrOptimizeSpirv(const PipelineCache::SpirvBinaryKey &key, CreateOnCacheMiss &&create, CacheHit &&cacheHit);
88
89 struct ComputeProgramKey
90 {
91 ComputeProgramKey(uint64_t shaderIdentifier, uint32_t pipelineLayoutIdentifier);
92
93 bool operator<(const ComputeProgramKey &other) const;
94
95 private:
96 const uint64_t shaderIdentifier;
97 const uint32_t pipelineLayoutIdentifier;
98 };
99
100 // getOrCreateComputeProgram() queries the cache for a compute program with
101 // the given key.
102 // If one is found, it is returned, otherwise create() is called, the
103 // returned program is added to the cache, and it is returned.
104 // Function must be a function of the signature:
105 // std::shared_ptr<sw::ComputeProgram>()
106 template<typename Function>
107 inline std::shared_ptr<sw::ComputeProgram> getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create);
108
109 private:
110 struct CacheHeader
111 {
112 uint32_t headerLength;
113 uint32_t headerVersion;
114 uint32_t vendorID;
115 uint32_t deviceID;
116 uint8_t pipelineCacheUUID[VK_UUID_SIZE];
117 };
118
119 size_t dataSize = 0;
120 uint8_t *data = nullptr;
121
122 marl::mutex spirvShadersMutex;
123 std::map<SpirvBinaryKey, sw::SpirvBinary> spirvShaders GUARDED_BY(spirvShadersMutex);
124
125 marl::mutex computeProgramsMutex;
126 std::map<ComputeProgramKey, std::shared_ptr<sw::ComputeProgram>> computePrograms GUARDED_BY(computeProgramsMutex);
127 };
128
Cast(VkPipelineCache object)129 static inline PipelineCache *Cast(VkPipelineCache object)
130 {
131 return PipelineCache::Cast(object);
132 }
133
134 template<typename Function>
getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey & key,Function && create)135 std::shared_ptr<sw::ComputeProgram> PipelineCache::getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create)
136 {
137 marl::lock lock(computeProgramsMutex);
138
139 auto it = computePrograms.find(key);
140 if(it != computePrograms.end())
141 {
142 return it->second;
143 }
144
145 auto created = create();
146 computePrograms.emplace(key, created);
147
148 return created;
149 }
150
contains(const PipelineCache::SpirvBinaryKey & key)151 inline bool PipelineCache::contains(const PipelineCache::SpirvBinaryKey &key)
152 {
153 marl::lock lock(spirvShadersMutex);
154
155 return spirvShaders.find(key) != spirvShaders.end();
156 }
157
158 template<typename CreateOnCacheMiss, typename CacheHit>
getOrOptimizeSpirv(const PipelineCache::SpirvBinaryKey & key,CreateOnCacheMiss && create,CacheHit && cacheHit)159 sw::SpirvBinary PipelineCache::getOrOptimizeSpirv(const PipelineCache::SpirvBinaryKey &key, CreateOnCacheMiss &&create, CacheHit &&cacheHit)
160 {
161 marl::lock lock(spirvShadersMutex);
162
163 auto it = spirvShaders.find(key);
164 if(it != spirvShaders.end())
165 {
166 cacheHit();
167 return it->second;
168 }
169
170 sw::SpirvBinary outShader = create();
171 spirvShaders.emplace(key, outShader);
172 return outShader;
173 }
174
175 } // namespace vk
176
177 #endif // VK_PIPELINE_CACHE_HPP_
178