blob: 533b56f42d6269a499cd3163da7be1b71db4e629 [file] [log] [blame]
Alexis Hetu18a84252018-11-19 11:30:43 -05001// 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 Claytonfa0175c2019-07-30 11:49:08 +010019
Alexis Hetu52edb172019-06-26 10:17:18 -040020#include <cstring>
21#include <functional>
Alexis Hetu52edb172019-06-26 10:17:18 -040022#include <map>
Ben Claytonfa0175c2019-07-30 11:49:08 +010023#include <memory>
Alexis Hetu52edb172019-06-26 10:17:18 -040024#include <mutex>
Ben Claytonfa0175c2019-07-30 11:49:08 +010025#include <string>
Alexis Hetu52edb172019-06-26 10:17:18 -040026#include <vector>
27
28namespace sw
29{
30 class ComputeProgram;
31 class SpirvShader;
32}
Alexis Hetu18a84252018-11-19 11:30:43 -050033
34namespace vk
35{
36
Alexis Hetu52edb172019-06-26 10:17:18 -040037class PipelineLayout;
38class RenderPass;
39
Alexis Hetu18a84252018-11-19 11:30:43 -050040class PipelineCache : public Object<PipelineCache, VkPipelineCache>
41{
42public:
Alexis Hetu1424ef62019-04-05 18:03:53 -040043 PipelineCache(const VkPipelineCacheCreateInfo* pCreateInfo, void* mem);
Alexis Hetu52edb172019-06-26 10:17:18 -040044 virtual ~PipelineCache();
Alexis Hetu1424ef62019-04-05 18:03:53 -040045 void destroy(const VkAllocationCallbacks* pAllocator);
Alexis Hetu18a84252018-11-19 11:30:43 -050046
Alexis Hetu1424ef62019-04-05 18:03:53 -040047 static size_t ComputeRequiredAllocationSize(const VkPipelineCacheCreateInfo* pCreateInfo);
48
49 VkResult getData(size_t* pDataSize, void* pData);
50 VkResult merge(uint32_t srcCacheCount, const VkPipelineCache* pSrcCaches);
Alexis Hetu18a84252018-11-19 11:30:43 -050051
Alexis Hetu52edb172019-06-26 10:17:18 -040052 struct SpirvShaderKey
53 {
54 struct SpecializationInfo
55 {
56 SpecializationInfo(const VkSpecializationInfo* specializationInfo);
Alexis Hetu52edb172019-06-26 10:17:18 -040057
58 bool operator<(const SpecializationInfo& specializationInfo) const;
59
Ben Claytone6092f32019-07-29 19:44:13 +010060 const VkSpecializationInfo* get() const { return info.get(); }
Alexis Hetu52edb172019-06-26 10:17:18 -040061
62 private:
Ben Claytone6092f32019-07-29 19:44:13 +010063 struct Deleter
64 {
65 void operator()(VkSpecializationInfo*) const;
66 };
67
68 std::shared_ptr<VkSpecializationInfo> info;
Alexis Hetu52edb172019-06-26 10:17:18 -040069 };
70
71 SpirvShaderKey(const VkShaderStageFlagBits pipelineStage,
72 const std::string& entryPointName,
73 const std::vector<uint32_t>& insns,
74 const vk::RenderPass *renderPass,
75 const uint32_t subpassIndex,
76 const VkSpecializationInfo* specializationInfo);
77
78 bool operator<(const SpirvShaderKey &other) const;
79
80 const VkShaderStageFlagBits& getPipelineStage() const { return pipelineStage; }
81 const std::string& getEntryPointName() const { return entryPointName; }
82 const std::vector<uint32_t>& getInsns() const { return insns; }
83 const vk::RenderPass *getRenderPass() const { return renderPass; }
84 uint32_t getSubpassIndex() const { return subpassIndex; }
85 const VkSpecializationInfo *getSpecializationInfo() const { return specializationInfo.get(); }
86
87 private:
88 const VkShaderStageFlagBits pipelineStage;
89 const std::string entryPointName;
90 const std::vector<uint32_t> insns;
91 const vk::RenderPass *renderPass;
92 const uint32_t subpassIndex;
93 const SpecializationInfo specializationInfo;
94 };
95
96 std::mutex& getShaderMutex() { return spirvShadersMutex; }
97 const std::shared_ptr<sw::SpirvShader>* operator[](const PipelineCache::SpirvShaderKey& key) const;
98 void insert(const PipelineCache::SpirvShaderKey& key, const std::shared_ptr<sw::SpirvShader> &shader);
99
100 struct ComputeProgramKey
101 {
102 ComputeProgramKey(const sw::SpirvShader* shader, const vk::PipelineLayout* layout) :
103 shader(shader), layout(layout)
104 {}
105
Ben Claytone6092f32019-07-29 19:44:13 +0100106 bool operator<(const ComputeProgramKey &other) const
107 {
108 return (shader < other.shader) || (layout < other.layout);
Alexis Hetu52edb172019-06-26 10:17:18 -0400109 }
110
111 const sw::SpirvShader* getShader() const { return shader; }
112 const vk::PipelineLayout* getLayout() const { return layout; }
113
114 private:
115 const sw::SpirvShader* shader;
116 const vk::PipelineLayout* layout;
117 };
118
119 std::mutex& getProgramMutex() { return computeProgramsMutex; }
120 const std::shared_ptr<sw::ComputeProgram>* operator[](const PipelineCache::ComputeProgramKey& key) const;
121 void insert(const PipelineCache::ComputeProgramKey& key, const std::shared_ptr<sw::ComputeProgram> &computeProgram);
122
Alexis Hetu18a84252018-11-19 11:30:43 -0500123private:
Alexis Hetu1424ef62019-04-05 18:03:53 -0400124 struct CacheHeader
125 {
126 uint32_t headerLength;
127 uint32_t headerVersion;
128 uint32_t vendorID;
129 uint32_t deviceID;
130 uint8_t pipelineCacheUUID[VK_UUID_SIZE];
131 };
132
133 size_t dataSize = 0;
134 uint8_t* data = nullptr;
Alexis Hetu52edb172019-06-26 10:17:18 -0400135
136 std::mutex spirvShadersMutex;
137 std::map<SpirvShaderKey, std::shared_ptr<sw::SpirvShader>> spirvShaders;
138
139 std::mutex computeProgramsMutex;
140 std::map<ComputeProgramKey, std::shared_ptr<sw::ComputeProgram>> computePrograms;
Alexis Hetu18a84252018-11-19 11:30:43 -0500141};
142
143static inline PipelineCache* Cast(VkPipelineCache object)
144{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400145 return PipelineCache::Cast(object);
Alexis Hetu18a84252018-11-19 11:30:43 -0500146}
147
148} // namespace vk
149
150#endif // VK_PIPELINE_CACHE_HPP_