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