blob: 8d4149909a3daba1083d032db9ed80c14f8a0e6a [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
18#include "VkObject.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 Hetu767b41b2018-09-26 11:25:46 -040029
Ben Clayton2ed93ab2019-12-17 20:38:03 +000030namespace marl {
31class Scheduler;
32}
33namespace sw {
34class Blitter;
35}
Ben Claytond6c61362019-08-14 18:16:01 +010036
Nicolas Capens157ba262019-12-10 17:49:14 -050037namespace vk {
Alexis Hetu767b41b2018-09-26 11:25:46 -040038
Nicolas Capens0c736802019-05-27 12:53:31 -040039class PhysicalDevice;
Alexis Hetu9e4d0402018-10-16 15:44:12 -040040class Queue;
41
Ben Claytona4e06ca2019-12-03 12:38:14 +000042namespace dbg {
43class Context;
44class Server;
45} // namespace dbg
46
Alexis Hetu767b41b2018-09-26 11:25:46 -040047class Device
48{
49public:
Alexis Hetu767b41b2018-09-26 11:25:46 -040050 static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; }
51
Ben Clayton2ed93ab2019-12-17 20:38:03 +000052 Device(const VkDeviceCreateInfo *pCreateInfo, void *mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, const std::shared_ptr<marl::Scheduler> &scheduler);
53 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetu767b41b2018-09-26 11:25:46 -040054
Ben Clayton2ed93ab2019-12-17 20:38:03 +000055 static size_t ComputeRequiredAllocationSize(const VkDeviceCreateInfo *pCreateInfo);
Alexis Hetu767b41b2018-09-26 11:25:46 -040056
Ben Clayton2ed93ab2019-12-17 20:38:03 +000057 bool hasExtension(const char *extensionName) const;
Alexis Hetu767b41b2018-09-26 11:25:46 -040058 VkQueue getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000059 VkResult waitForFences(uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout);
Alexis Hetue1f51b92019-04-23 15:34:34 -040060 VkResult waitIdle();
Ben Clayton2ed93ab2019-12-17 20:38:03 +000061 void getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
62 VkDescriptorSetLayoutSupport *pSupport) const;
Nicolas Capens0c736802019-05-27 12:53:31 -040063 PhysicalDevice *getPhysicalDevice() const { return physicalDevice; }
Ben Clayton2ed93ab2019-12-17 20:38:03 +000064 void updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
65 uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies);
66 void getRequirements(VkMemoryDedicatedRequirements *requirements) const;
Nicolas Capensa29aa772019-06-26 00:36:28 -040067 const VkPhysicalDeviceFeatures &getEnabledFeatures() const { return enabledFeatures; }
Ben Clayton2ed93ab2019-12-17 20:38:03 +000068 sw::Blitter *getBlitter() const { return blitter.get(); }
Alexis Hetu6448bd62019-06-11 15:58:59 -040069
70 class SamplingRoutineCache
71 {
72 public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +000073 SamplingRoutineCache()
74 : cache(1024)
75 {}
Alexis Hetu6448bd62019-06-11 15:58:59 -040076 ~SamplingRoutineCache() {}
77
78 struct Key
79 {
80 uint32_t instruction;
81 uint32_t sampler;
82 uint32_t imageView;
Ben Claytonf0464022019-08-08 18:33:41 +010083
Ben Clayton2ed93ab2019-12-17 20:38:03 +000084 inline bool operator==(const Key &rhs) const;
Ben Claytonf0464022019-08-08 18:33:41 +010085
86 struct Hash
87 {
Ben Clayton2ed93ab2019-12-17 20:38:03 +000088 inline std::size_t operator()(const Key &key) const noexcept;
Ben Claytonf0464022019-08-08 18:33:41 +010089 };
Alexis Hetu6448bd62019-06-11 15:58:59 -040090 };
91
Ben Clayton094974d2020-04-08 16:55:02 +010092 // getOrCreate() queries the cache for a Routine with the given key.
93 // If one is found, it is returned, otherwise createRoutine(key) is
94 // called, the returned Routine is added to the cache, and it is
95 // returned.
96 // Function must be a function of the signature:
97 // std::shared_ptr<rr::Routine>(const Key &)
Nicolas Capens1c294772020-03-28 23:04:55 -040098 template<typename Function>
Ben Clayton094974d2020-04-08 16:55:02 +010099 std::shared_ptr<rr::Routine> getOrCreate(const Key &key, Function &&createRoutine)
Nicolas Capens1c294772020-03-28 23:04:55 -0400100 {
Ben Claytonac43aa72020-04-04 00:48:13 +0100101 auto it = snapshot.find(key);
102 if(it != snapshot.end()) { return it->second; }
Nicolas Capens1c294772020-03-28 23:04:55 -0400103
Ben Clayton377573c2020-04-03 20:36:40 +0100104 marl::lock lock(mutex);
Ben Claytonac43aa72020-04-04 00:48:13 +0100105 if(auto existingRoutine = cache.lookup(key))
Nicolas Capens1c294772020-03-28 23:04:55 -0400106 {
107 return existingRoutine;
108 }
109
110 std::shared_ptr<rr::Routine> newRoutine = createRoutine(key);
111 cache.add(key, newRoutine);
Ben Claytonac43aa72020-04-04 00:48:13 +0100112 snapshotNeedsUpdate = true;
Nicolas Capens1c294772020-03-28 23:04:55 -0400113
114 return newRoutine;
115 }
Alexis Hetu6448bd62019-06-11 15:58:59 -0400116
Nicolas Capensd53eeca2020-03-19 13:31:41 -0400117 void updateSnapshot();
Alexis Hetu35755502019-07-22 13:51:49 -0400118
Alexis Hetu6448bd62019-06-11 15:58:59 -0400119 private:
Ben Claytonac43aa72020-04-04 00:48:13 +0100120 bool snapshotNeedsUpdate = false;
121 std::unordered_map<Key, std::shared_ptr<rr::Routine>, Key::Hash> snapshot;
122
Ben Clayton377573c2020-04-03 20:36:40 +0100123 marl::mutex mutex;
124 sw::LRUCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache GUARDED_BY(mutex);
Alexis Hetu6448bd62019-06-11 15:58:59 -0400125 };
126
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000127 SamplingRoutineCache *getSamplingRoutineCache() const;
Nicolas Capensd53eeca2020-03-19 13:31:41 -0400128 void updateSamplingRoutineSnapshotCache();
Alexis Hetu767b41b2018-09-26 11:25:46 -0400129
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400130 class SamplerIndexer
131 {
132 public:
133 ~SamplerIndexer();
134
135 uint32_t index(const SamplerState &samplerState);
136 void remove(const SamplerState &samplerState);
137
138 private:
139 struct Identifier
140 {
141 uint32_t id;
142 uint32_t count; // Number of samplers sharing this state identifier.
143 };
144
Ben Clayton377573c2020-04-03 20:36:40 +0100145 marl::mutex mutex;
146 std::map<SamplerState, Identifier> map GUARDED_BY(mutex);
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400147
148 uint32_t nextID = 0;
149 };
150
151 uint32_t indexSampler(const SamplerState &samplerState);
152 void removeSampler(const SamplerState &samplerState);
153
Ben Claytona4e06ca2019-12-03 12:38:14 +0000154 std::shared_ptr<vk::dbg::Context> getDebuggerContext() const
155 {
Ben Clayton7d0ce412019-12-03 13:26:31 +0000156#ifdef ENABLE_VK_DEBUGGER
Ben Claytona4e06ca2019-12-03 12:38:14 +0000157 return debugger.context;
Ben Clayton7d0ce412019-12-03 13:26:31 +0000158#else
159 return nullptr;
Ben Claytona4e06ca2019-12-03 12:38:14 +0000160#endif // ENABLE_VK_DEBUGGER
Ben Clayton7d0ce412019-12-03 13:26:31 +0000161 }
Ben Claytona4e06ca2019-12-03 12:38:14 +0000162
Alexis Hetu767b41b2018-09-26 11:25:46 -0400163private:
Nicolas Capensa29aa772019-06-26 00:36:28 -0400164 PhysicalDevice *const physicalDevice = nullptr;
165 Queue *const queues = nullptr;
Alexis Hetu767b41b2018-09-26 11:25:46 -0400166 uint32_t queueCount = 0;
Alexis Hetu6448bd62019-06-11 15:58:59 -0400167 std::unique_ptr<sw::Blitter> blitter;
Alexis Hetu6448bd62019-06-11 15:58:59 -0400168 uint32_t enabledExtensionCount = 0;
Alexis Hetu352791e2019-05-17 16:42:34 -0400169 typedef char ExtensionName[VK_MAX_EXTENSION_NAME_SIZE];
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000170 ExtensionName *extensions = nullptr;
Nicolas Capensa29aa772019-06-26 00:36:28 -0400171 const VkPhysicalDeviceFeatures enabledFeatures = {};
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400172
Ben Clayton6fff5122019-10-03 16:41:21 +0100173 std::shared_ptr<marl::Scheduler> scheduler;
Nicolas Capens1c294772020-03-28 23:04:55 -0400174 std::unique_ptr<SamplingRoutineCache> samplingRoutineCache;
Nicolas Capens73c4a0c2020-03-17 17:29:11 -0400175 std::unique_ptr<SamplerIndexer> samplerIndexer;
Ben Claytona4e06ca2019-12-03 12:38:14 +0000176
177#ifdef ENABLE_VK_DEBUGGER
178 struct
179 {
180 std::shared_ptr<vk::dbg::Context> context;
181 std::shared_ptr<vk::dbg::Server> server;
182 } debugger;
183#endif // ENABLE_VK_DEBUGGER
Alexis Hetu767b41b2018-09-26 11:25:46 -0400184};
185
186using DispatchableDevice = DispatchableObject<Device, VkDevice>;
187
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000188static inline Device *Cast(VkDevice object)
Alexis Hetu767b41b2018-09-26 11:25:46 -0400189{
190 return DispatchableDevice::Cast(object);
191}
192
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000193inline bool vk::Device::SamplingRoutineCache::Key::operator==(const Key &rhs) const
Ben Claytonf0464022019-08-08 18:33:41 +0100194{
195 return instruction == rhs.instruction && sampler == rhs.sampler && imageView == rhs.imageView;
196}
197
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000198inline std::size_t vk::Device::SamplingRoutineCache::Key::Hash::operator()(const Key &key) const noexcept
Ben Claytonf0464022019-08-08 18:33:41 +0100199{
Nicolas Capense662bb42019-08-08 14:57:53 -0400200 // Combine three 32-bit integers into a 64-bit hash.
201 // 2642239 is the largest prime which when cubed is smaller than 2^64.
202 uint64_t hash = key.instruction;
203 hash = (hash * 2642239) ^ key.sampler;
204 hash = (hash * 2642239) ^ key.imageView;
205 return static_cast<std::size_t>(hash); // Truncates to 32-bits on 32-bit platforms.
Ben Claytonf0464022019-08-08 18:33:41 +0100206}
207
Nicolas Capens157ba262019-12-10 17:49:14 -0500208} // namespace vk
Alexis Hetu767b41b2018-09-26 11:25:46 -0400209
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000210#endif // VK_DEVICE_HPP_