Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 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_PIPELINE_CACHE_HPP_ |
| 16 | #define VK_PIPELINE_CACHE_HPP_ |
| 17 | |
| 18 | #include "VkObject.hpp" |
Ben Clayton | 4d23aa3 | 2020-04-14 20:33:52 +0100 | [diff] [blame] | 19 | #include "VkSpecializationInfo.hpp" |
Ben Clayton | fa0175c | 2019-07-30 11:49:08 +0100 | [diff] [blame] | 20 | |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 21 | #include "marl/mutex.h" |
| 22 | #include "marl/tsa.h" |
| 23 | |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 24 | #include <cstring> |
| 25 | #include <functional> |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 26 | #include <map> |
Ben Clayton | fa0175c | 2019-07-30 11:49:08 +0100 | [diff] [blame] | 27 | #include <memory> |
Ben Clayton | fa0175c | 2019-07-30 11:49:08 +0100 | [diff] [blame] | 28 | #include <string> |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 31 | namespace sw { |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 32 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 33 | class ComputeProgram; |
| 34 | class SpirvShader; |
| 35 | |
| 36 | } // namespace sw |
| 37 | |
| 38 | namespace vk { |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 39 | |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 40 | class PipelineLayout; |
| 41 | class RenderPass; |
| 42 | |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 43 | class PipelineCache : public Object<PipelineCache, VkPipelineCache> |
| 44 | { |
| 45 | public: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 46 | PipelineCache(const VkPipelineCacheCreateInfo *pCreateInfo, void *mem); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 47 | virtual ~PipelineCache(); |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 48 | void destroy(const VkAllocationCallbacks *pAllocator); |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 49 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 50 | static size_t ComputeRequiredAllocationSize(const VkPipelineCacheCreateInfo *pCreateInfo); |
Alexis Hetu | 1424ef6 | 2019-04-05 18:03:53 -0400 | [diff] [blame] | 51 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 52 | VkResult getData(size_t *pDataSize, void *pData); |
| 53 | VkResult merge(uint32_t srcCacheCount, const VkPipelineCache *pSrcCaches); |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 54 | |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 55 | struct SpirvShaderKey |
| 56 | { |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 57 | SpirvShaderKey(const VkShaderStageFlagBits pipelineStage, |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 58 | const std::string &entryPointName, |
| 59 | const std::vector<uint32_t> &insns, |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 60 | const vk::RenderPass *renderPass, |
| 61 | const uint32_t subpassIndex, |
Ben Clayton | 4d23aa3 | 2020-04-14 20:33:52 +0100 | [diff] [blame] | 62 | const vk::SpecializationInfo &specializationInfo); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 63 | |
| 64 | bool operator<(const SpirvShaderKey &other) const; |
| 65 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 66 | const VkShaderStageFlagBits &getPipelineStage() const { return pipelineStage; } |
| 67 | const std::string &getEntryPointName() const { return entryPointName; } |
| 68 | const std::vector<uint32_t> &getInsns() const { return insns; } |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 69 | const vk::RenderPass *getRenderPass() const { return renderPass; } |
| 70 | uint32_t getSubpassIndex() const { return subpassIndex; } |
| 71 | const VkSpecializationInfo *getSpecializationInfo() const { return specializationInfo.get(); } |
| 72 | |
| 73 | private: |
| 74 | const VkShaderStageFlagBits pipelineStage; |
| 75 | const std::string entryPointName; |
| 76 | const std::vector<uint32_t> insns; |
| 77 | const vk::RenderPass *renderPass; |
| 78 | const uint32_t subpassIndex; |
Ben Clayton | 4d23aa3 | 2020-04-14 20:33:52 +0100 | [diff] [blame] | 79 | const vk::SpecializationInfo specializationInfo; |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
Ben Clayton | 094974d | 2020-04-08 16:55:02 +0100 | [diff] [blame] | 82 | // getOrCreateShader() queries the cache for a shader with the given key. |
| 83 | // If one is found, it is returned, otherwise create() is called, the |
| 84 | // returned shader is added to the cache, and it is returned. |
| 85 | // Function must be a function of the signature: |
| 86 | // std::shared_ptr<sw::SpirvShader>() |
| 87 | template<typename Function> |
| 88 | inline std::shared_ptr<sw::SpirvShader> getOrCreateShader(const PipelineCache::SpirvShaderKey &key, Function &&create); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 89 | |
| 90 | struct ComputeProgramKey |
| 91 | { |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 92 | ComputeProgramKey(const sw::SpirvShader *shader, const vk::PipelineLayout *layout) |
| 93 | : shader(shader) |
| 94 | , layout(layout) |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 95 | {} |
| 96 | |
Ben Clayton | e6092f3 | 2019-07-29 19:44:13 +0100 | [diff] [blame] | 97 | bool operator<(const ComputeProgramKey &other) const |
| 98 | { |
Antonio Maiorano | 126d9d6 | 2019-09-13 09:58:58 -0400 | [diff] [blame] | 99 | return std::tie(shader, layout) < std::tie(other.shader, other.layout); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 102 | const sw::SpirvShader *getShader() const { return shader; } |
| 103 | const vk::PipelineLayout *getLayout() const { return layout; } |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 104 | |
| 105 | private: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 106 | const sw::SpirvShader *shader; |
| 107 | const vk::PipelineLayout *layout; |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
Ben Clayton | 094974d | 2020-04-08 16:55:02 +0100 | [diff] [blame] | 110 | // getOrCreateComputeProgram() queries the cache for a compute program with |
| 111 | // the given key. |
| 112 | // If one is found, it is returned, otherwise create() is called, the |
| 113 | // returned program is added to the cache, and it is returned. |
| 114 | // Function must be a function of the signature: |
| 115 | // std::shared_ptr<sw::ComputeProgram>() |
| 116 | template<typename Function> |
| 117 | inline std::shared_ptr<sw::ComputeProgram> getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 118 | |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 119 | private: |
Alexis Hetu | 1424ef6 | 2019-04-05 18:03:53 -0400 | [diff] [blame] | 120 | struct CacheHeader |
| 121 | { |
| 122 | uint32_t headerLength; |
| 123 | uint32_t headerVersion; |
| 124 | uint32_t vendorID; |
| 125 | uint32_t deviceID; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 126 | uint8_t pipelineCacheUUID[VK_UUID_SIZE]; |
Alexis Hetu | 1424ef6 | 2019-04-05 18:03:53 -0400 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | size_t dataSize = 0; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 130 | uint8_t *data = nullptr; |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 131 | |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 132 | marl::mutex spirvShadersMutex; |
| 133 | std::map<SpirvShaderKey, std::shared_ptr<sw::SpirvShader>> spirvShaders GUARDED_BY(spirvShadersMutex); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 134 | |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 135 | marl::mutex computeProgramsMutex; |
| 136 | std::map<ComputeProgramKey, std::shared_ptr<sw::ComputeProgram>> computePrograms GUARDED_BY(computeProgramsMutex); |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 137 | }; |
| 138 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 139 | static inline PipelineCache *Cast(VkPipelineCache object) |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 140 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 141 | return PipelineCache::Cast(object); |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Ben Clayton | 094974d | 2020-04-08 16:55:02 +0100 | [diff] [blame] | 144 | template<typename Function> |
| 145 | std::shared_ptr<sw::ComputeProgram> PipelineCache::getOrCreateComputeProgram(const PipelineCache::ComputeProgramKey &key, Function &&create) |
| 146 | { |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 147 | marl::lock lock(computeProgramsMutex); |
Ben Clayton | 094974d | 2020-04-08 16:55:02 +0100 | [diff] [blame] | 148 | |
| 149 | auto it = computePrograms.find(key); |
| 150 | if(it != computePrograms.end()) { return it->second; } |
| 151 | |
| 152 | auto created = create(); |
| 153 | computePrograms.emplace(key, created); |
| 154 | return created; |
| 155 | } |
| 156 | |
| 157 | template<typename Function> |
| 158 | std::shared_ptr<sw::SpirvShader> PipelineCache::getOrCreateShader(const PipelineCache::SpirvShaderKey &key, Function &&create) |
| 159 | { |
Ben Clayton | 377573c | 2020-04-03 20:36:40 +0100 | [diff] [blame] | 160 | marl::lock lock(spirvShadersMutex); |
Ben Clayton | 094974d | 2020-04-08 16:55:02 +0100 | [diff] [blame] | 161 | |
| 162 | auto it = spirvShaders.find(key); |
| 163 | if(it != spirvShaders.end()) { return it->second; } |
| 164 | |
| 165 | auto created = create(); |
| 166 | spirvShaders.emplace(key, created); |
| 167 | return created; |
| 168 | } |
| 169 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 170 | } // namespace vk |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 171 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 172 | #endif // VK_PIPELINE_CACHE_HPP_ |