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_DEVICE_HPP_
16 #define VK_DEVICE_HPP_
17 
18 #include "VkObject.hpp"
19 #include "VkSampler.hpp"
20 #include "Device/LRUCache.hpp"
21 #include "Reactor/Routine.hpp"
22 
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 
27 namespace marl {
28 class Scheduler;
29 }
30 namespace sw {
31 class Blitter;
32 }
33 
34 namespace vk {
35 
36 class PhysicalDevice;
37 class Queue;
38 
39 namespace dbg {
40 class Context;
41 class Server;
42 }  // namespace dbg
43 
44 class Device
45 {
46 public:
GetAllocationScope()47 	static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; }
48 
49 	Device(const VkDeviceCreateInfo *pCreateInfo, void *mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, const std::shared_ptr<marl::Scheduler> &scheduler);
50 	void destroy(const VkAllocationCallbacks *pAllocator);
51 
52 	static size_t ComputeRequiredAllocationSize(const VkDeviceCreateInfo *pCreateInfo);
53 
54 	bool hasExtension(const char *extensionName) const;
55 	VkQueue getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const;
56 	VkResult waitForFences(uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout);
57 	VkResult waitIdle();
58 	void getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
59 	                                   VkDescriptorSetLayoutSupport *pSupport) const;
getPhysicalDevice() const60 	PhysicalDevice *getPhysicalDevice() const { return physicalDevice; }
61 	void updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
62 	                          uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies);
63 	void getRequirements(VkMemoryDedicatedRequirements *requirements) const;
getEnabledFeatures() const64 	const VkPhysicalDeviceFeatures &getEnabledFeatures() const { return enabledFeatures; }
getBlitter() const65 	sw::Blitter *getBlitter() const { return blitter.get(); }
66 
67 	class SamplingRoutineCache
68 	{
69 	public:
SamplingRoutineCache()70 		SamplingRoutineCache()
71 		    : cache(1024)
72 		{}
~SamplingRoutineCache()73 		~SamplingRoutineCache() {}
74 
75 		struct Key
76 		{
77 			uint32_t instruction;
78 			uint32_t sampler;
79 			uint32_t imageView;
80 
81 			inline bool operator==(const Key &rhs) const;
82 
83 			struct Hash
84 			{
85 				inline std::size_t operator()(const Key &key) const noexcept;
86 			};
87 		};
88 
89 		template<typename Function>
getOrCreate(const Key & key,Function createRoutine)90 		std::shared_ptr<rr::Routine> getOrCreate(const Key &key, Function createRoutine)
91 		{
92 			std::lock_guard<std::mutex> lock(mutex);
93 
94 			if(auto existingRoutine = cache.query(key))
95 			{
96 				return existingRoutine;
97 			}
98 
99 			std::shared_ptr<rr::Routine> newRoutine = createRoutine(key);
100 			cache.add(key, newRoutine);
101 
102 			return newRoutine;
103 		}
104 
105 		rr::Routine *querySnapshot(const Key &key) const;
106 		void updateSnapshot();
107 
108 	private:
109 		sw::LRUSnapshotCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache;  // guarded by mutex
110 		std::mutex mutex;
111 	};
112 
113 	SamplingRoutineCache *getSamplingRoutineCache() const;
114 	rr::Routine *querySnapshotCache(const SamplingRoutineCache::Key &key) const;
115 	void updateSamplingRoutineSnapshotCache();
116 
117 	class SamplerIndexer
118 	{
119 	public:
120 		~SamplerIndexer();
121 
122 		uint32_t index(const SamplerState &samplerState);
123 		void remove(const SamplerState &samplerState);
124 
125 	private:
126 		struct Identifier
127 		{
128 			uint32_t id;
129 			uint32_t count;  // Number of samplers sharing this state identifier.
130 		};
131 
132 		std::map<SamplerState, Identifier> map;  // guarded by mutex
133 		std::mutex mutex;
134 
135 		uint32_t nextID = 0;
136 	};
137 
138 	uint32_t indexSampler(const SamplerState &samplerState);
139 	void removeSampler(const SamplerState &samplerState);
140 
getDebuggerContext() const141 	std::shared_ptr<vk::dbg::Context> getDebuggerContext() const
142 	{
143 #ifdef ENABLE_VK_DEBUGGER
144 		return debugger.context;
145 #else
146 		return nullptr;
147 #endif  // ENABLE_VK_DEBUGGER
148 	}
149 
150 private:
151 	PhysicalDevice *const physicalDevice = nullptr;
152 	Queue *const queues = nullptr;
153 	uint32_t queueCount = 0;
154 	std::unique_ptr<sw::Blitter> blitter;
155 	uint32_t enabledExtensionCount = 0;
156 	typedef char ExtensionName[VK_MAX_EXTENSION_NAME_SIZE];
157 	ExtensionName *extensions = nullptr;
158 	const VkPhysicalDeviceFeatures enabledFeatures = {};
159 
160 	std::shared_ptr<marl::Scheduler> scheduler;
161 	std::unique_ptr<SamplingRoutineCache> samplingRoutineCache;
162 	std::unique_ptr<SamplerIndexer> samplerIndexer;
163 
164 #ifdef ENABLE_VK_DEBUGGER
165 	struct
166 	{
167 		std::shared_ptr<vk::dbg::Context> context;
168 		std::shared_ptr<vk::dbg::Server> server;
169 	} debugger;
170 #endif  // ENABLE_VK_DEBUGGER
171 };
172 
173 using DispatchableDevice = DispatchableObject<Device, VkDevice>;
174 
Cast(VkDevice object)175 static inline Device *Cast(VkDevice object)
176 {
177 	return DispatchableDevice::Cast(object);
178 }
179 
operator ==(const Key & rhs) const180 inline bool vk::Device::SamplingRoutineCache::Key::operator==(const Key &rhs) const
181 {
182 	return instruction == rhs.instruction && sampler == rhs.sampler && imageView == rhs.imageView;
183 }
184 
operator ()(const Key & key) const185 inline std::size_t vk::Device::SamplingRoutineCache::Key::Hash::operator()(const Key &key) const noexcept
186 {
187 	// Combine three 32-bit integers into a 64-bit hash.
188 	// 2642239 is the largest prime which when cubed is smaller than 2^64.
189 	uint64_t hash = key.instruction;
190 	hash = (hash * 2642239) ^ key.sampler;
191 	hash = (hash * 2642239) ^ key.imageView;
192 	return static_cast<std::size_t>(hash);  // Truncates to 32-bits on 32-bit platforms.
193 }
194 
195 }  // namespace vk
196 
197 #endif  // VK_DEVICE_HPP_
198