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