blob: 7f67ffa5012da29faa3117e8782d350baac381b4 [file] [log] [blame]
Alexis Hetu767b41b2018-09-26 11:25:46 -04001// 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
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040018#include "VkImageView.hpp"
Nicolas Capens73c4a0c2020-03-17 17:29:11 -040019#include "VkSampler.hpp"
Alexis Hetu6448bd62019-06-11 15:58:59 -040020#include "Reactor/Routine.hpp"
Ben Claytonac43aa72020-04-04 00:48:13 +010021#include "System/LRUCache.hpp"
Nicolas Capens73c4a0c2020-03-17 17:29:11 -040022
Ben Clayton377573c2020-04-03 20:36:40 +010023#include "marl/mutex.h"
24#include "marl/tsa.h"
25
Nicolas Capens73c4a0c2020-03-17 17:29:11 -040026#include <map>
Alexis Hetu6448bd62019-06-11 15:58:59 -040027#include <memory>
Ben Claytonac43aa72020-04-04 00:48:13 +010028#include <unordered_map>
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040029#include <unordered_set>
Alexis Hetu767b41b2018-09-26 11:25:46 -040030
Ben Clayton2ed93ab2019-12-17 20:38:03 +000031namespace marl {
32class Scheduler;
33}
34namespace sw {
35class Blitter;
36}
Ben Claytond6c61362019-08-14 18:16:01 +010037
Nicolas Capens157ba262019-12-10 17:49:14 -050038namespace vk {
Alexis Hetu767b41b2018-09-26 11:25:46 -040039
Nicolas Capens0c736802019-05-27 12:53:31 -040040class PhysicalDevice;
Alexis Hetu9e4d0402018-10-16 15:44:12 -040041class Queue;
42
Ben Claytona4e06ca2019-12-03 12:38:14 +000043namespace dbg {
44class Context;
45class Server;
46} // namespace dbg
47
Alexis Hetu767b41b2018-09-26 11:25:46 -040048class Device
49{
50public:
Alexis Hetu767b41b2018-09-26 11:25:46 -040051 static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; }
52
Ben Clayton2ed93ab2019-12-17 20:38:03 +000053 Device(const VkDeviceCreateInfo *pCreateInfo, void *mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, const std::shared_ptr<marl::Scheduler> &scheduler);
54 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetu767b41b2018-09-26 11:25:46 -040055
Ben Clayton2ed93ab2019-12-17 20:38:03 +000056 static size_t ComputeRequiredAllocationSize(const VkDeviceCreateInfo *pCreateInfo);
Alexis Hetu767b41b2018-09-26 11:25:46 -040057
Ben Clayton2ed93ab2019-12-17 20:38:03 +000058 bool hasExtension(const char *extensionName) const;
Alexis Hetu767b41b2018-09-26 11:25:46 -040059 VkQueue getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000060 VkResult waitForFences(uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout);
Alexis Hetue1f51b92019-04-23 15:34:34 -040061 VkResult waitIdle();
Ben Clayton2ed93ab2019-12-17 20:38:03 +000062 void getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
63 VkDescriptorSetLayoutSupport *pSupport) const;
Nicolas Capens0c736802019-05-27 12:53:31 -040064 PhysicalDevice *getPhysicalDevice() const { return physicalDevice; }
Ben Clayton2ed93ab2019-12-17 20:38:03 +000065 void updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
66 uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies);
67 void getRequirements(VkMemoryDedicatedRequirements *requirements) const;
Nicolas Capensa29aa772019-06-26 00:36:28 -040068 const VkPhysicalDeviceFeatures &getEnabledFeatures() const { return enabledFeatures; }
Ben Clayton2ed93ab2019-12-17 20:38:03 +000069 sw::Blitter *getBlitter() const { return blitter.get(); }
Alexis Hetu6448bd62019-06-11 15:58:59 -040070
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040071 void registerImageView(ImageView *imageView);
72 void unregisterImageView(ImageView *imageView);
73 void prepareForSampling(ImageView *imageView);
74 void contentsChanged(ImageView *imageView);
75
Alexis Hetu6448bd62019-06-11 15:58:59 -040076 class SamplingRoutineCache
77 {
78 public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +000079 SamplingRoutineCache()
80 : cache(1024)
81 {}
Alexis Hetu6448bd62019-06-11 15:58:59 -040082 ~SamplingRoutineCache() {}
83
84 struct Key
85 {
86 uint32_t instruction;
87 uint32_t sampler;
88 uint32_t imageView;
Ben Claytonf0464022019-08-08 18:33:41 +010089
Ben Clayton2ed93ab2019-12-17 20:38:03 +000090 inline bool operator==(const Key &rhs) const;
Ben Claytonf0464022019-08-08 18:33:41 +010091
92 struct Hash
93 {
Ben Clayton2ed93ab2019-12-17 20:38:03 +000094 inline std::size_t operator()(const Key &key) const noexcept;
Ben Claytonf0464022019-08-08 18:33:41 +010095 };
Alexis Hetu6448bd62019-06-11 15:58:59 -040096 };
97
Ben Clayton094974d2020-04-08 16:55:02 +010098 // 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 &)
Nicolas Capens1c294772020-03-28 23:04:55 -0400104 template<typename Function>
Ben Clayton094974d2020-04-08 16:55:02 +0100105 std::shared_ptr<rr::Routine> getOrCreate(const Key &key, Function &&createRoutine)
Nicolas Capens1c294772020-03-28 23:04:55 -0400106 {
Ben Claytonac43aa72020-04-04 00:48:13 +0100107 auto it = snapshot.find(key);
108 if(it != snapshot.end()) { return it->second; }
Nicolas Capens1c294772020-03-28 23:04:55 -0400109
Ben Clayton377573c2020-04-03 20:36:40 +0100110 marl::lock lock(mutex);
Ben Claytonac43aa72020-04-04 00:48:13 +0100111 if(auto existingRoutine = cache.lookup(key))
Nicolas Capens1c294772020-03-28 23:04:55 -0400112 {
113 return existingRoutine;
114 }
115
116 std::shared_ptr<rr::Routine> newRoutine = createRoutine(key);
117 cache.add(key, newRoutine);
Ben Claytonac43aa72020-04-04 00:48:13 +0100118 snapshotNeedsUpdate = true;
Nicolas Capens1c294772020-03-28 23:04:55 -0400119
120 return newRoutine;
121 }
Alexis Hetu6448bd62019-06-11 15:58:59 -0400122
Nicolas Capensd53eeca2020-03-19 13:31:41 -0400123 void updateSnapshot();
Alexis Hetu35755502019-07-22 13:51:49 -0400124
Alexis Hetu6448bd62019-06-11 15:58:59 -0400125 private:
Ben Claytonac43aa72020-04-04 00:48:13 +0100126 bool snapshotNeedsUpdate = false;
127 std::unordered_map<Key, std::shared_ptr<rr::Routine>, Key::Hash> snapshot;
128
Ben Clayton377573c2020-04-03 20:36:40 +0100129 marl::mutex mutex;
130 sw::LRUCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache GUARDED_BY(mutex);
Alexis Hetu6448bd62019-06-11 15:58:59 -0400131 };
132
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000133 SamplingRoutineCache *getSamplingRoutineCache() const;
Nicolas Capensd53eeca2020-03-19 13:31:41 -0400134 void updateSamplingRoutineSnapshotCache();
Alexis Hetu767b41b2018-09-26 11:25:46 -0400135
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400136 class SamplerIndexer
137 {
138 public:
139 ~SamplerIndexer();
140
141 uint32_t index(const SamplerState &samplerState);
142 void remove(const SamplerState &samplerState);
143
144 private:
145 struct Identifier
146 {
147 uint32_t id;
148 uint32_t count; // Number of samplers sharing this state identifier.
149 };
150
Ben Clayton377573c2020-04-03 20:36:40 +0100151 marl::mutex mutex;
152 std::map<SamplerState, Identifier> map GUARDED_BY(mutex);
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400153
154 uint32_t nextID = 0;
155 };
156
157 uint32_t indexSampler(const SamplerState &samplerState);
158 void removeSampler(const SamplerState &samplerState);
159
Ben Claytona4e06ca2019-12-03 12:38:14 +0000160 std::shared_ptr<vk::dbg::Context> getDebuggerContext() const
161 {
Ben Clayton7d0ce412019-12-03 13:26:31 +0000162#ifdef ENABLE_VK_DEBUGGER
Ben Claytona4e06ca2019-12-03 12:38:14 +0000163 return debugger.context;
Ben Clayton7d0ce412019-12-03 13:26:31 +0000164#else
165 return nullptr;
Ben Claytona4e06ca2019-12-03 12:38:14 +0000166#endif // ENABLE_VK_DEBUGGER
Ben Clayton7d0ce412019-12-03 13:26:31 +0000167 }
Ben Claytona4e06ca2019-12-03 12:38:14 +0000168
Alexis Hetu74f6c652020-07-22 21:57:05 -0400169 VkResult setDebugUtilsObjectName(const VkDebugUtilsObjectNameInfoEXT *pNameInfo);
170 VkResult setDebugUtilsObjectTag(const VkDebugUtilsObjectTagInfoEXT *pTagInfo);
171
Alexis Hetu767b41b2018-09-26 11:25:46 -0400172private:
Nicolas Capensa29aa772019-06-26 00:36:28 -0400173 PhysicalDevice *const physicalDevice = nullptr;
174 Queue *const queues = nullptr;
Alexis Hetu767b41b2018-09-26 11:25:46 -0400175 uint32_t queueCount = 0;
Alexis Hetu6448bd62019-06-11 15:58:59 -0400176 std::unique_ptr<sw::Blitter> blitter;
Alexis Hetu6448bd62019-06-11 15:58:59 -0400177 uint32_t enabledExtensionCount = 0;
Alexis Hetu352791e2019-05-17 16:42:34 -0400178 typedef char ExtensionName[VK_MAX_EXTENSION_NAME_SIZE];
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000179 ExtensionName *extensions = nullptr;
Nicolas Capensa29aa772019-06-26 00:36:28 -0400180 const VkPhysicalDeviceFeatures enabledFeatures = {};
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400181
Ben Clayton6fff5122019-10-03 16:41:21 +0100182 std::shared_ptr<marl::Scheduler> scheduler;
Nicolas Capens1c294772020-03-28 23:04:55 -0400183 std::unique_ptr<SamplingRoutineCache> samplingRoutineCache;
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400184 std::unique_ptr<SamplerIndexer> samplerIndexer;
Ben Claytona4e06ca2019-12-03 12:38:14 +0000185
Alexis Hetu72ea2ee2020-08-06 10:08:37 -0400186 marl::mutex imageViewSetMutex;
187 std::unordered_set<ImageView *> imageViewSet GUARDED_BY(imageViewSetMutex);
188
Ben Claytona4e06ca2019-12-03 12:38:14 +0000189#ifdef ENABLE_VK_DEBUGGER
190 struct
191 {
192 std::shared_ptr<vk::dbg::Context> context;
193 std::shared_ptr<vk::dbg::Server> server;
194 } debugger;
195#endif // ENABLE_VK_DEBUGGER
Alexis Hetu767b41b2018-09-26 11:25:46 -0400196};
197
198using DispatchableDevice = DispatchableObject<Device, VkDevice>;
199
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000200static inline Device *Cast(VkDevice object)
Alexis Hetu767b41b2018-09-26 11:25:46 -0400201{
202 return DispatchableDevice::Cast(object);
203}
204
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000205inline bool vk::Device::SamplingRoutineCache::Key::operator==(const Key &rhs) const
Ben Claytonf0464022019-08-08 18:33:41 +0100206{
207 return instruction == rhs.instruction && sampler == rhs.sampler && imageView == rhs.imageView;
208}
209
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000210inline std::size_t vk::Device::SamplingRoutineCache::Key::Hash::operator()(const Key &key) const noexcept
Ben Claytonf0464022019-08-08 18:33:41 +0100211{
Nicolas Capense662bb42019-08-08 14:57:53 -0400212 // Combine three 32-bit integers into a 64-bit hash.
213 // 2642239 is the largest prime which when cubed is smaller than 2^64.
214 uint64_t hash = key.instruction;
215 hash = (hash * 2642239) ^ key.sampler;
216 hash = (hash * 2642239) ^ key.imageView;
217 return static_cast<std::size_t>(hash); // Truncates to 32-bits on 32-bit platforms.
Ben Claytonf0464022019-08-08 18:33:41 +0100218}
219
Nicolas Capens157ba262019-12-10 17:49:14 -0500220} // namespace vk
Alexis Hetu767b41b2018-09-26 11:25:46 -0400221
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000222#endif // VK_DEVICE_HPP_