xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkDevice.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_DEVICE_HPP_
16*03ce13f7SAndroid Build Coastguard Worker #define VK_DEVICE_HPP_
17*03ce13f7SAndroid Build Coastguard Worker 
18*03ce13f7SAndroid Build Coastguard Worker #include "VkImageView.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "VkSampler.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include "Device/Blitter.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "Pipeline/Constants.hpp"
22*03ce13f7SAndroid Build Coastguard Worker #include "Reactor/Routine.hpp"
23*03ce13f7SAndroid Build Coastguard Worker #include "System/LRUCache.hpp"
24*03ce13f7SAndroid Build Coastguard Worker 
25*03ce13f7SAndroid Build Coastguard Worker #include "marl/mutex.h"
26*03ce13f7SAndroid Build Coastguard Worker #include "marl/tsa.h"
27*03ce13f7SAndroid Build Coastguard Worker 
28*03ce13f7SAndroid Build Coastguard Worker #include <map>
29*03ce13f7SAndroid Build Coastguard Worker #include <memory>
30*03ce13f7SAndroid Build Coastguard Worker #include <unordered_map>
31*03ce13f7SAndroid Build Coastguard Worker #include <unordered_set>
32*03ce13f7SAndroid Build Coastguard Worker 
33*03ce13f7SAndroid Build Coastguard Worker namespace marl {
34*03ce13f7SAndroid Build Coastguard Worker class Scheduler;
35*03ce13f7SAndroid Build Coastguard Worker }
36*03ce13f7SAndroid Build Coastguard Worker 
37*03ce13f7SAndroid Build Coastguard Worker namespace vk {
38*03ce13f7SAndroid Build Coastguard Worker 
39*03ce13f7SAndroid Build Coastguard Worker class PhysicalDevice;
40*03ce13f7SAndroid Build Coastguard Worker class PrivateData;
41*03ce13f7SAndroid Build Coastguard Worker class Queue;
42*03ce13f7SAndroid Build Coastguard Worker 
43*03ce13f7SAndroid Build Coastguard Worker class Device
44*03ce13f7SAndroid Build Coastguard Worker {
45*03ce13f7SAndroid Build Coastguard Worker public:
GetAllocationScope()46*03ce13f7SAndroid Build Coastguard Worker 	static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; }
47*03ce13f7SAndroid Build Coastguard Worker 
48*03ce13f7SAndroid Build Coastguard Worker 	Device(const VkDeviceCreateInfo *pCreateInfo, void *mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, const std::shared_ptr<marl::Scheduler> &scheduler);
49*03ce13f7SAndroid Build Coastguard Worker 	void destroy(const VkAllocationCallbacks *pAllocator);
50*03ce13f7SAndroid Build Coastguard Worker 
51*03ce13f7SAndroid Build Coastguard Worker 	static size_t ComputeRequiredAllocationSize(const VkDeviceCreateInfo *pCreateInfo);
52*03ce13f7SAndroid Build Coastguard Worker 
53*03ce13f7SAndroid Build Coastguard Worker 	bool hasExtension(const char *extensionName) const;
54*03ce13f7SAndroid Build Coastguard Worker 	VkQueue getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const;
55*03ce13f7SAndroid Build Coastguard Worker 	VkResult waitForFences(uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout);
56*03ce13f7SAndroid Build Coastguard Worker 	VkResult waitForSemaphores(const VkSemaphoreWaitInfo *pWaitInfo, uint64_t timeout);
57*03ce13f7SAndroid Build Coastguard Worker 	VkResult waitIdle();
58*03ce13f7SAndroid Build Coastguard Worker 	void getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
59*03ce13f7SAndroid Build Coastguard Worker 	                                   VkDescriptorSetLayoutSupport *pSupport) const;
getPhysicalDevice() const60*03ce13f7SAndroid Build Coastguard Worker 	PhysicalDevice *getPhysicalDevice() const { return physicalDevice; }
61*03ce13f7SAndroid Build Coastguard Worker 	void updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
62*03ce13f7SAndroid Build Coastguard Worker 	                          uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies);
63*03ce13f7SAndroid Build Coastguard Worker 	void getRequirements(VkMemoryDedicatedRequirements *requirements) const;
getEnabledFeatures() const64*03ce13f7SAndroid Build Coastguard Worker 	const VkPhysicalDeviceFeatures &getEnabledFeatures() const { return enabledFeatures; }
getBlitter() const65*03ce13f7SAndroid Build Coastguard Worker 	sw::Blitter *getBlitter() const { return blitter.get(); }
66*03ce13f7SAndroid Build Coastguard Worker 
67*03ce13f7SAndroid Build Coastguard Worker 	void registerImageView(ImageView *imageView);
68*03ce13f7SAndroid Build Coastguard Worker 	void unregisterImageView(ImageView *imageView);
69*03ce13f7SAndroid Build Coastguard Worker 	void prepareForSampling(ImageView *imageView);
70*03ce13f7SAndroid Build Coastguard Worker 	void contentsChanged(ImageView *imageView, Image::ContentsChangedContext context);
71*03ce13f7SAndroid Build Coastguard Worker 
72*03ce13f7SAndroid Build Coastguard Worker 	VkResult setPrivateData(VkObjectType objectType, uint64_t objectHandle, const PrivateData *privateDataSlot, uint64_t data);
73*03ce13f7SAndroid Build Coastguard Worker 	void getPrivateData(VkObjectType objectType, uint64_t objectHandle, const PrivateData *privateDataSlot, uint64_t *data);
74*03ce13f7SAndroid Build Coastguard Worker 	void removePrivateDataSlot(const PrivateData *privateDataSlot);
75*03ce13f7SAndroid Build Coastguard Worker 
76*03ce13f7SAndroid Build Coastguard Worker 	class SamplingRoutineCache
77*03ce13f7SAndroid Build Coastguard Worker 	{
78*03ce13f7SAndroid Build Coastguard Worker 	public:
SamplingRoutineCache()79*03ce13f7SAndroid Build Coastguard Worker 		SamplingRoutineCache()
80*03ce13f7SAndroid Build Coastguard Worker 		    : cache(1024)
81*03ce13f7SAndroid Build Coastguard Worker 		{}
~SamplingRoutineCache()82*03ce13f7SAndroid Build Coastguard Worker 		~SamplingRoutineCache() {}
83*03ce13f7SAndroid Build Coastguard Worker 
84*03ce13f7SAndroid Build Coastguard Worker 		struct Key
85*03ce13f7SAndroid Build Coastguard Worker 		{
86*03ce13f7SAndroid Build Coastguard Worker 			uint32_t instruction;
87*03ce13f7SAndroid Build Coastguard Worker 			uint32_t sampler;
88*03ce13f7SAndroid Build Coastguard Worker 			uint32_t imageView;
89*03ce13f7SAndroid Build Coastguard Worker 
90*03ce13f7SAndroid Build Coastguard Worker 			inline bool operator==(const Key &rhs) const;
91*03ce13f7SAndroid Build Coastguard Worker 
92*03ce13f7SAndroid Build Coastguard Worker 			struct Hash
93*03ce13f7SAndroid Build Coastguard Worker 			{
94*03ce13f7SAndroid Build Coastguard Worker 				inline std::size_t operator()(const Key &key) const noexcept;
95*03ce13f7SAndroid Build Coastguard Worker 			};
96*03ce13f7SAndroid Build Coastguard Worker 		};
97*03ce13f7SAndroid Build Coastguard Worker 
98*03ce13f7SAndroid Build Coastguard Worker 		// getOrCreate() queries the cache for a Routine with the given key.
99*03ce13f7SAndroid Build Coastguard Worker 		// If one is found, it is returned, otherwise createRoutine(key) is
100*03ce13f7SAndroid Build Coastguard Worker 		// called, the returned Routine is added to the cache, and it is
101*03ce13f7SAndroid Build Coastguard Worker 		// returned.
102*03ce13f7SAndroid Build Coastguard Worker 		// Function must be a function of the signature:
103*03ce13f7SAndroid Build Coastguard Worker 		//     std::shared_ptr<rr::Routine>(const Key &)
104*03ce13f7SAndroid Build Coastguard Worker 		template<typename Function>
getOrCreate(const Key & key,Function && createRoutine)105*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<rr::Routine> getOrCreate(const Key &key, Function &&createRoutine)
106*03ce13f7SAndroid Build Coastguard Worker 		{
107*03ce13f7SAndroid Build Coastguard Worker 			auto it = snapshot.find(key);
108*03ce13f7SAndroid Build Coastguard Worker 			if(it != snapshot.end()) { return it->second; }
109*03ce13f7SAndroid Build Coastguard Worker 
110*03ce13f7SAndroid Build Coastguard Worker 			marl::lock lock(mutex);
111*03ce13f7SAndroid Build Coastguard Worker 			if(auto existingRoutine = cache.lookup(key))
112*03ce13f7SAndroid Build Coastguard Worker 			{
113*03ce13f7SAndroid Build Coastguard Worker 				return existingRoutine;
114*03ce13f7SAndroid Build Coastguard Worker 			}
115*03ce13f7SAndroid Build Coastguard Worker 
116*03ce13f7SAndroid Build Coastguard Worker 			std::shared_ptr<rr::Routine> newRoutine = createRoutine(key);
117*03ce13f7SAndroid Build Coastguard Worker 			cache.add(key, newRoutine);
118*03ce13f7SAndroid Build Coastguard Worker 			snapshotNeedsUpdate = true;
119*03ce13f7SAndroid Build Coastguard Worker 
120*03ce13f7SAndroid Build Coastguard Worker 			return newRoutine;
121*03ce13f7SAndroid Build Coastguard Worker 		}
122*03ce13f7SAndroid Build Coastguard Worker 
123*03ce13f7SAndroid Build Coastguard Worker 		void updateSnapshot();
124*03ce13f7SAndroid Build Coastguard Worker 
125*03ce13f7SAndroid Build Coastguard Worker 	private:
126*03ce13f7SAndroid Build Coastguard Worker 		bool snapshotNeedsUpdate = false;
127*03ce13f7SAndroid Build Coastguard Worker 		std::unordered_map<Key, std::shared_ptr<rr::Routine>, Key::Hash> snapshot;
128*03ce13f7SAndroid Build Coastguard Worker 
129*03ce13f7SAndroid Build Coastguard Worker 		marl::mutex mutex;
130*03ce13f7SAndroid Build Coastguard Worker 		sw::LRUCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache GUARDED_BY(mutex);
131*03ce13f7SAndroid Build Coastguard Worker 	};
132*03ce13f7SAndroid Build Coastguard Worker 
133*03ce13f7SAndroid Build Coastguard Worker 	SamplingRoutineCache *getSamplingRoutineCache() const;
134*03ce13f7SAndroid Build Coastguard Worker 	void updateSamplingRoutineSnapshotCache();
135*03ce13f7SAndroid Build Coastguard Worker 
136*03ce13f7SAndroid Build Coastguard Worker 	class SamplerIndexer
137*03ce13f7SAndroid Build Coastguard Worker 	{
138*03ce13f7SAndroid Build Coastguard Worker 	public:
139*03ce13f7SAndroid Build Coastguard Worker 		~SamplerIndexer();
140*03ce13f7SAndroid Build Coastguard Worker 
141*03ce13f7SAndroid Build Coastguard Worker 		uint32_t index(const SamplerState &samplerState);
142*03ce13f7SAndroid Build Coastguard Worker 		void remove(const SamplerState &samplerState);
143*03ce13f7SAndroid Build Coastguard Worker 		const SamplerState *find(uint32_t id);
144*03ce13f7SAndroid Build Coastguard Worker 
145*03ce13f7SAndroid Build Coastguard Worker 	private:
146*03ce13f7SAndroid Build Coastguard Worker 		struct Identifier
147*03ce13f7SAndroid Build Coastguard Worker 		{
148*03ce13f7SAndroid Build Coastguard Worker 			uint32_t id;
149*03ce13f7SAndroid Build Coastguard Worker 			uint32_t count;  // Number of samplers sharing this state identifier.
150*03ce13f7SAndroid Build Coastguard Worker 		};
151*03ce13f7SAndroid Build Coastguard Worker 
152*03ce13f7SAndroid Build Coastguard Worker 		marl::mutex mutex;
153*03ce13f7SAndroid Build Coastguard Worker 		std::map<SamplerState, Identifier> map GUARDED_BY(mutex);
154*03ce13f7SAndroid Build Coastguard Worker 
155*03ce13f7SAndroid Build Coastguard Worker 		uint32_t nextID = 0;
156*03ce13f7SAndroid Build Coastguard Worker 	};
157*03ce13f7SAndroid Build Coastguard Worker 
158*03ce13f7SAndroid Build Coastguard Worker 	uint32_t indexSampler(const SamplerState &samplerState);
159*03ce13f7SAndroid Build Coastguard Worker 	void removeSampler(const SamplerState &samplerState);
160*03ce13f7SAndroid Build Coastguard Worker 	const SamplerState *findSampler(uint32_t samplerId) const;
161*03ce13f7SAndroid Build Coastguard Worker 
162*03ce13f7SAndroid Build Coastguard Worker 	VkResult setDebugUtilsObjectName(const VkDebugUtilsObjectNameInfoEXT *pNameInfo);
163*03ce13f7SAndroid Build Coastguard Worker 	VkResult setDebugUtilsObjectTag(const VkDebugUtilsObjectTagInfoEXT *pTagInfo);
164*03ce13f7SAndroid Build Coastguard Worker 
165*03ce13f7SAndroid Build Coastguard Worker #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
166*03ce13f7SAndroid Build Coastguard Worker 	void emitDeviceMemoryReport(VkDeviceMemoryReportEventTypeEXT type, uint64_t memoryObjectId, VkDeviceSize size, VkObjectType objectType, uint64_t objectHandle, uint32_t heapIndex = 0);
167*03ce13f7SAndroid Build Coastguard Worker #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
168*03ce13f7SAndroid Build Coastguard Worker 
169*03ce13f7SAndroid Build Coastguard Worker 	const sw::Constants constants;
170*03ce13f7SAndroid Build Coastguard Worker 
171*03ce13f7SAndroid Build Coastguard Worker private:
172*03ce13f7SAndroid Build Coastguard Worker 	PhysicalDevice *const physicalDevice = nullptr;
173*03ce13f7SAndroid Build Coastguard Worker 	Queue *const queues = nullptr;
174*03ce13f7SAndroid Build Coastguard Worker 	uint32_t queueCount = 0;
175*03ce13f7SAndroid Build Coastguard Worker 	std::unique_ptr<sw::Blitter> blitter;
176*03ce13f7SAndroid Build Coastguard Worker 	uint32_t enabledExtensionCount = 0;
177*03ce13f7SAndroid Build Coastguard Worker 	typedef char ExtensionName[VK_MAX_EXTENSION_NAME_SIZE];
178*03ce13f7SAndroid Build Coastguard Worker 	ExtensionName *extensions = nullptr;
179*03ce13f7SAndroid Build Coastguard Worker 	const VkPhysicalDeviceFeatures enabledFeatures = {};
180*03ce13f7SAndroid Build Coastguard Worker 
181*03ce13f7SAndroid Build Coastguard Worker 	std::shared_ptr<marl::Scheduler> scheduler;
182*03ce13f7SAndroid Build Coastguard Worker 	std::unique_ptr<SamplingRoutineCache> samplingRoutineCache;
183*03ce13f7SAndroid Build Coastguard Worker 	std::unique_ptr<SamplerIndexer> samplerIndexer;
184*03ce13f7SAndroid Build Coastguard Worker 
185*03ce13f7SAndroid Build Coastguard Worker 	marl::mutex imageViewSetMutex;
186*03ce13f7SAndroid Build Coastguard Worker 	std::unordered_set<ImageView *> imageViewSet GUARDED_BY(imageViewSetMutex);
187*03ce13f7SAndroid Build Coastguard Worker 
188*03ce13f7SAndroid Build Coastguard Worker 	struct PrivateDataObject
189*03ce13f7SAndroid Build Coastguard Worker 	{
190*03ce13f7SAndroid Build Coastguard Worker 		VkObjectType objectType;
191*03ce13f7SAndroid Build Coastguard Worker 		uint64_t objectHandle;
192*03ce13f7SAndroid Build Coastguard Worker 
operator ==vk::Device::PrivateDataObject193*03ce13f7SAndroid Build Coastguard Worker 		bool operator==(const PrivateDataObject &privateDataObject) const
194*03ce13f7SAndroid Build Coastguard Worker 		{
195*03ce13f7SAndroid Build Coastguard Worker 			return (objectType == privateDataObject.objectType) &&
196*03ce13f7SAndroid Build Coastguard Worker 			       (objectHandle == privateDataObject.objectHandle);
197*03ce13f7SAndroid Build Coastguard Worker 		}
198*03ce13f7SAndroid Build Coastguard Worker 
199*03ce13f7SAndroid Build Coastguard Worker 		struct Hash
200*03ce13f7SAndroid Build Coastguard Worker 		{
operator ()vk::Device::PrivateDataObject::Hash201*03ce13f7SAndroid Build Coastguard Worker 			std::size_t operator()(const PrivateDataObject &privateDataObject) const noexcept
202*03ce13f7SAndroid Build Coastguard Worker 			{
203*03ce13f7SAndroid Build Coastguard Worker 				// Since the object type is linked to the object's handle,
204*03ce13f7SAndroid Build Coastguard Worker 				// simply use the object handle as the hash value.
205*03ce13f7SAndroid Build Coastguard Worker 				return static_cast<size_t>(privateDataObject.objectHandle);
206*03ce13f7SAndroid Build Coastguard Worker 			}
207*03ce13f7SAndroid Build Coastguard Worker 		};
208*03ce13f7SAndroid Build Coastguard Worker 	};
209*03ce13f7SAndroid Build Coastguard Worker 	typedef std::unordered_map<PrivateDataObject, uint64_t, PrivateDataObject::Hash> PrivateDataSlot;
210*03ce13f7SAndroid Build Coastguard Worker 
211*03ce13f7SAndroid Build Coastguard Worker 	marl::mutex privateDataMutex;
212*03ce13f7SAndroid Build Coastguard Worker 	std::map<const PrivateData *, PrivateDataSlot> privateData;
213*03ce13f7SAndroid Build Coastguard Worker 
214*03ce13f7SAndroid Build Coastguard Worker #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
215*03ce13f7SAndroid Build Coastguard Worker 	std::vector<std::pair<PFN_vkDeviceMemoryReportCallbackEXT, void *>> deviceMemoryReportCallbacks;
216*03ce13f7SAndroid Build Coastguard Worker #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
217*03ce13f7SAndroid Build Coastguard Worker };
218*03ce13f7SAndroid Build Coastguard Worker 
219*03ce13f7SAndroid Build Coastguard Worker using DispatchableDevice = DispatchableObject<Device, VkDevice>;
220*03ce13f7SAndroid Build Coastguard Worker 
Cast(VkDevice object)221*03ce13f7SAndroid Build Coastguard Worker static inline Device *Cast(VkDevice object)
222*03ce13f7SAndroid Build Coastguard Worker {
223*03ce13f7SAndroid Build Coastguard Worker 	return DispatchableDevice::Cast(object);
224*03ce13f7SAndroid Build Coastguard Worker }
225*03ce13f7SAndroid Build Coastguard Worker 
operator ==(const Key & rhs) const226*03ce13f7SAndroid Build Coastguard Worker inline bool vk::Device::SamplingRoutineCache::Key::operator==(const Key &rhs) const
227*03ce13f7SAndroid Build Coastguard Worker {
228*03ce13f7SAndroid Build Coastguard Worker 	return instruction == rhs.instruction && sampler == rhs.sampler && imageView == rhs.imageView;
229*03ce13f7SAndroid Build Coastguard Worker }
230*03ce13f7SAndroid Build Coastguard Worker 
operator ()(const Key & key) const231*03ce13f7SAndroid Build Coastguard Worker inline std::size_t vk::Device::SamplingRoutineCache::Key::Hash::operator()(const Key &key) const noexcept
232*03ce13f7SAndroid Build Coastguard Worker {
233*03ce13f7SAndroid Build Coastguard Worker 	// Combine three 32-bit integers into a 64-bit hash.
234*03ce13f7SAndroid Build Coastguard Worker 	// 2642239 is the largest prime which when cubed is smaller than 2^64.
235*03ce13f7SAndroid Build Coastguard Worker 	uint64_t hash = key.instruction;
236*03ce13f7SAndroid Build Coastguard Worker 	hash = (hash * 2642239) ^ key.sampler;
237*03ce13f7SAndroid Build Coastguard Worker 	hash = (hash * 2642239) ^ key.imageView;
238*03ce13f7SAndroid Build Coastguard Worker 	return static_cast<std::size_t>(hash);  // Truncates to 32-bits on 32-bit platforms.
239*03ce13f7SAndroid Build Coastguard Worker }
240*03ce13f7SAndroid Build Coastguard Worker 
241*03ce13f7SAndroid Build Coastguard Worker }  // namespace vk
242*03ce13f7SAndroid Build Coastguard Worker 
243*03ce13f7SAndroid Build Coastguard Worker #endif  // VK_DEVICE_HPP_
244