• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 optimize);
63 
64 		bool operator<(const SpirvBinaryKey &other) const;
65 
getBinaryvk::PipelineCache::SpirvBinaryKey66 		const sw::SpirvBinary &getBinary() const { return spirv; }
getSpecializationInfovk::PipelineCache::SpirvBinaryKey67 		const VkSpecializationInfo *getSpecializationInfo() const { return specializationInfo.get(); }
getOptimizationvk::PipelineCache::SpirvBinaryKey68 		bool getOptimization() const { return optimize; }
69 
70 	private:
71 		const sw::SpirvBinary spirv;
72 		const vk::SpecializationInfo specializationInfo;
73 		const bool optimize;
74 	};
75 
76 	// contains() queries whether the cache contains a shader with the given key.
77 	inline bool contains(const PipelineCache::SpirvBinaryKey &key);
78 
79 	// getOrOptimizeSpirv() queries the cache for a shader with the given key.
80 	// If one is found, it is returned, otherwise create() is called, the
81 	// returned SPIR-V binary is added to the cache, and it is returned.
82 	// CreateOnCacheMiss must be a function of the signature:
83 	//     sw::ShaderBinary()
84 	template<typename CreateOnCacheMiss, typename CacheHit>
85 	inline sw::SpirvBinary getOrOptimizeSpirv(const PipelineCache::SpirvBinaryKey &key, CreateOnCacheMiss &&create, CacheHit &&cacheHit);
86 
87 	struct ComputeProgramKey
88 	{
89 		ComputeProgramKey(uint64_t shaderIdentifier, uint32_t pipelineLayoutIdentifier);
90 
91 		bool operator<(const ComputeProgramKey &other) const;
92 
93 	private:
94 		const uint64_t shaderIdentifier;
95 		const uint32_t pipelineLayoutIdentifier;
96 	};
97 
98 	// getOrCreateComputeProgram() queries the cache for a compute program with
99 	// the given key.
100 	// If one is found, it is returned, otherwise create() is called, the
101 	// returned program is added to the cache, and it is returned.
102 	// Function must be a function of the signature:
103 	//     std::shared_ptr<sw::ComputeProgram>()
104 	template<typename Function>
105 	inline std::shared_ptr<sw::ComputeProgram> getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create);
106 
107 private:
108 	struct CacheHeader
109 	{
110 		uint32_t headerLength;
111 		uint32_t headerVersion;
112 		uint32_t vendorID;
113 		uint32_t deviceID;
114 		uint8_t pipelineCacheUUID[VK_UUID_SIZE];
115 	};
116 
117 	size_t dataSize = 0;
118 	uint8_t *data = nullptr;
119 
120 	marl::mutex spirvShadersMutex;
121 	std::map<SpirvBinaryKey, sw::SpirvBinary> spirvShaders GUARDED_BY(spirvShadersMutex);
122 
123 	marl::mutex computeProgramsMutex;
124 	std::map<ComputeProgramKey, std::shared_ptr<sw::ComputeProgram>> computePrograms GUARDED_BY(computeProgramsMutex);
125 };
126 
Cast(VkPipelineCache object)127 static inline PipelineCache *Cast(VkPipelineCache object)
128 {
129 	return PipelineCache::Cast(object);
130 }
131 
132 template<typename Function>
getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey & key,Function && create)133 std::shared_ptr<sw::ComputeProgram> PipelineCache::getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create)
134 {
135 	marl::lock lock(computeProgramsMutex);
136 
137 	auto it = computePrograms.find(key);
138 	if(it != computePrograms.end())
139 	{
140 		return it->second;
141 	}
142 
143 	auto created = create();
144 	computePrograms.emplace(key, created);
145 
146 	return created;
147 }
148 
contains(const PipelineCache::SpirvBinaryKey & key)149 inline bool PipelineCache::contains(const PipelineCache::SpirvBinaryKey &key)
150 {
151 	marl::lock lock(spirvShadersMutex);
152 
153 	return spirvShaders.find(key) != spirvShaders.end();
154 }
155 
156 template<typename CreateOnCacheMiss, typename CacheHit>
getOrOptimizeSpirv(const PipelineCache::SpirvBinaryKey & key,CreateOnCacheMiss && create,CacheHit && cacheHit)157 sw::SpirvBinary PipelineCache::getOrOptimizeSpirv(const PipelineCache::SpirvBinaryKey &key, CreateOnCacheMiss &&create, CacheHit &&cacheHit)
158 {
159 	marl::lock lock(spirvShadersMutex);
160 
161 	auto it = spirvShaders.find(key);
162 	if(it != spirvShaders.end())
163 	{
164 		cacheHit();
165 		return it->second;
166 	}
167 
168 	sw::SpirvBinary outShader = create();
169 	spirvShaders.emplace(key, outShader);
170 	return outShader;
171 }
172 
173 }  // namespace vk
174 
175 #endif  // VK_PIPELINE_CACHE_HPP_
176