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