Repository navigation
Expand file tree
/
Copy pathPipelineLibrary.cpp
More file actions
374 lines (352 loc) · 17.4 KB
/
Copy pathPipelineLibrary.cpp
File metadata and controls
374 lines (352 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "prx/libSceAgcDriver/Graphics/include/PipelineLibrary.hpp"
#include "prx/libSceAgcDriver/Graphics/include/PipelineJournal.hpp"
#include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp"
#include <array>
#include <atomic>
#include <condition_variable>
#include <cstdlib>
#include <deque>
#include <functional>
#include <thread>
#include <map>
#include <mutex>
#include <optional>
#include <string_view>
namespace AgcDriver::Graphics {
namespace {
constexpr std::array<VkDynamicState, 15> dynamicStates{
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_DEPTH_BIAS,
VK_DYNAMIC_STATE_DEPTH_BOUNDS,
VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
VK_DYNAMIC_STATE_STENCIL_REFERENCE,
VK_DYNAMIC_STATE_CULL_MODE_EXT,
VK_DYNAMIC_STATE_FRONT_FACE_EXT,
VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT,
VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT,
VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT,
VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT,
VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT,
VK_DYNAMIC_STATE_STENCIL_OP_EXT,
};
using Key = std::vector<std::byte>;
struct DeviceLibraries {
PFN_vkDestroyPipeline destroyPipeline = nullptr;
PFN_vkDestroyPipelineLayout destroyLayout = nullptr;
PFN_vkDestroyShaderModule destroyModule = nullptr;
std::vector<VkShaderModule> modules;
std::map<Key, VkPipelineLayout> layouts;
std::array<std::map<Key, VkPipeline>, 4> parts;
PipelineLibraryCounters counters;
};
struct LibraryStore {
std::mutex mutex;
std::map<VkDevice, DeviceLibraries> devices;
std::condition_variable idle;
std::condition_variable work;
std::deque<std::pair<VkDevice, std::function<void()>>> jobs;
std::map<VkDevice, std::size_t> pending;
std::map<VkDevice, std::size_t> activeBuilds;
std::thread worker;
bool stopping = false;
};
std::atomic<unsigned> runningWorkers{0};
LibraryStore& Libraries() {
static auto* store = new LibraryStore();
return *store;
}
void runJobs(LibraryStore& store) {
std::unique_lock lock(store.mutex);
for (;;) {
store.work.wait(lock, [&] { return !store.jobs.empty() || store.stopping; });
if (store.stopping) break;
auto [device, job] = std::move(store.jobs.front());
store.jobs.pop_front();
lock.unlock();
job();
lock.lock();
if (--store.pending[device] == 0) store.idle.notify_all();
}
runningWorkers.fetch_sub(1, std::memory_order_acq_rel);
}
void enqueue(LibraryStore& store, VkDevice device, std::function<void()> job) {
if (store.stopping) return;
if (!store.worker.joinable()) {
store.worker = std::thread(runJobs, std::ref(store));
runningWorkers.fetch_add(1, std::memory_order_acq_rel);
}
auto& pending = store.pending[device];
store.jobs.emplace_back(device, std::move(job));
++pending;
store.work.notify_one();
}
Key Combined(const Key& part, const Key& context) {
Key key;
const auto append = [&](const Key& value) {
const auto size = value.size();
const auto bytes = std::as_bytes(std::span(&size, 1));
key.insert(key.end(), bytes.begin(), bytes.end());
key.insert(key.end(), value.begin(), value.end());
};
append(part);
append(context);
return key;
}
}
std::span<const VkDynamicState> PipelineLibraryDynamicStates() {
return dynamicStates;
}
VkPipeline LinkPipelineFromLibraries(const Context& context, const VkGraphicsPipelineCreateInfo& info, const VkPipelineRenderingCreateInfoKHR& rendering, const VkPipelineLayoutCreateInfo& layout, const PipelineLibraryKeys& keys, std::shared_ptr<OptimizedPipeline>* optimized, bool partsOnly, std::span<VkShaderModule> modules) {
Require(info.renderPass == VK_NULL_HANDLE && info.pDynamicState != nullptr, "pipeline library parts need dynamic rendering and the library dynamic states");
const bool split = (layout.flags & VK_PIPELINE_LAYOUT_CREATE_INDEPENDENT_SETS_BIT_EXT) != 0;
Require(!split || (layout.setLayoutCount == 2u && layout.pSetLayouts != nullptr && ((keys.preRasterizationSets | keys.fragmentSets) & ~3u) == 0u), "split pipeline library layout requires two descriptor set slots and a valid mask");
auto& store = Libraries();
std::unique_lock lock(store.mutex);
++store.activeBuilds[context.device];
struct ActiveBuild {
LibraryStore& store;
VkDevice device;
std::unique_lock<std::mutex>& lock;
~ActiveBuild() {
if (!lock.owns_lock()) lock.lock();
if (--store.activeBuilds[device] == 0) store.idle.notify_all();
}
} activeBuild{store, context.device, lock};
auto& device = store.devices[context.device];
if (device.destroyPipeline == nullptr) {
device.destroyPipeline = context.Function<PFN_vkDestroyPipeline>("vkDestroyPipeline");
device.destroyLayout = context.Function<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout");
device.destroyModule = context.Function<PFN_vkDestroyShaderModule>("vkDestroyShaderModule");
}
const auto layoutFor = [&](const Key& key, std::optional<std::uint32_t> mask) {
auto found = device.layouts.find(key);
if (found != device.layouts.end()) return found->second;
auto info = layout;
std::array<VkDescriptorSetLayout, 2> sets{VK_NULL_HANDLE, VK_NULL_HANDLE};
if (mask.has_value()) {
for (std::uint32_t set = 0; set < sets.size(); ++set) {
if ((*mask & (1u << set)) != 0u) sets[set] = layout.pSetLayouts[set];
}
info.pSetLayouts = sets.data();
}
VkPipelineLayout handle = VK_NULL_HANDLE;
Check(context.Function<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(context.device, &info, nullptr, &handle), "vkCreatePipelineLayout library");
return device.layouts.emplace(key, handle).first->second;
};
const auto fullLayout = layoutFor(keys.layout, std::nullopt);
const std::array<VkPipelineLayout, 4> partLayouts{VK_NULL_HANDLE, split ? layoutFor(keys.preRasterizationLayout, keys.preRasterizationSets) : fullLayout, split ? layoutFor(keys.fragmentLayout, keys.fragmentSets) : fullLayout, VK_NULL_HANDLE};
std::vector<VkPipelineShaderStageCreateInfo> preRasterization;
const VkPipelineShaderStageCreateInfo* fragment = nullptr;
bool mesh = false;
for (std::uint32_t index = 0; index < info.stageCount; ++index) {
const auto& stage = info.pStages[index];
if (stage.stage == VK_SHADER_STAGE_FRAGMENT_BIT) fragment = &stage;
else preRasterization.push_back(stage);
mesh |= stage.stage == VK_SHADER_STAGE_MESH_BIT_EXT;
}
Require(fragment != nullptr && !preRasterization.empty(), "a pipeline built from libraries needs a fragment and a pre-rasterization stage");
constexpr std::array<VkGraphicsPipelineLibraryFlagsEXT, 4> flags{
VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT,
VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT,
VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT,
VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT,
};
const std::array<const Key*, 4> partKeys{&keys.vertexInput, &keys.preRasterization, &keys.fragmentShader, &keys.fragmentOutput};
const Key none;
VkPipelineRenderingCreateInfoKHR shaderRendering{VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR};
shaderRendering.viewMask = rendering.viewMask;
shaderRendering.depthAttachmentFormat = rendering.depthAttachmentFormat;
shaderRendering.stencilAttachmentFormat = rendering.stencilAttachmentFormat;
const std::array<std::uint32_t, 3> attachments{shaderRendering.viewMask, static_cast<std::uint32_t>(shaderRendering.depthAttachmentFormat), static_cast<std::uint32_t>(shaderRendering.stencilAttachmentFormat)};
const auto attachmentBytes = std::as_bytes(std::span(attachments));
const Key attachmentKey(attachmentBytes.begin(), attachmentBytes.end());
const auto preRasterizationContext = Combined(keys.preRasterizationLayout, attachmentKey);
const auto fragmentContext = Combined(keys.fragmentLayout, attachmentKey);
const std::array<const Key*, 4> contextKeys{&none, &preRasterizationContext, &fragmentContext, &keys.renderPass};
VkPipelineDepthStencilStateCreateInfo dynamicDepth{VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO};
static const bool backgroundLinking = std::getenv("APS5_NO_PIPELINE_LTO") == nullptr;
const bool optimize = backgroundLinking && context.pipelineLinkTimeOptimization;
std::vector<VkPipeline> libraries;
for (std::size_t part = 0; part < flags.size(); ++part) {
if (part == 0 && mesh) continue;
const auto key = Combined(*partKeys[part], *contextKeys[part]);
auto& cache = device.parts[part];
if (const auto found = cache.find(key); found != cache.end()) {
libraries.push_back(found->second);
continue;
}
PerformanceTimer timing("Vulkan.GraphicsPipelineLibrary");
VkGraphicsPipelineLibraryCreateInfoEXT library{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_LIBRARY_CREATE_INFO_EXT, flags[part] == VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT ? &rendering : &shaderRendering};
library.flags = flags[part];
VkGraphicsPipelineCreateInfo create{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &library};
create.flags = VK_PIPELINE_CREATE_LIBRARY_BIT_KHR | VK_PIPELINE_CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT;
create.pDynamicState = info.pDynamicState;
create.layout = partLayouts[part];
switch (flags[part]) {
case VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT:
create.pVertexInputState = info.pVertexInputState;
create.pInputAssemblyState = info.pInputAssemblyState;
create.layout = VK_NULL_HANDLE;
break;
case VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT:
create.stageCount = static_cast<std::uint32_t>(preRasterization.size());
create.pStages = preRasterization.data();
create.pViewportState = info.pViewportState;
create.pRasterizationState = info.pRasterizationState;
create.pTessellationState = info.pTessellationState;
break;
case VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT:
create.stageCount = 1;
create.pStages = fragment;
create.pMultisampleState = info.pMultisampleState;
create.pDepthStencilState = info.pDepthStencilState != nullptr ? info.pDepthStencilState : &dynamicDepth;
break;
default:
create.pMultisampleState = info.pMultisampleState;
create.pColorBlendState = info.pColorBlendState;
create.layout = VK_NULL_HANDLE;
break;
}
VkPipeline handle = VK_NULL_HANDLE;
// Built without the store's mutex: a draw whose parts are resident is not held behind
// another thread's build, and builds from several threads (the registration prebuild, the
// journal replay) run at once; a part another thread built meanwhile is kept, this one dropped.
lock.unlock();
VkResult created;
{
auto pipelineCacheLock = context.LockPipelineCacheForUse();
created = context.Function<PFN_vkCreateGraphicsPipelines>("vkCreateGraphicsPipelines")(context.device, context.pipelineCache, 1, &create, nullptr, &handle);
}
lock.lock();
Check(created, "vkCreateGraphicsPipelines library");
if (const auto raced = cache.find(key); raced != cache.end()) {
device.destroyPipeline(context.device, handle, nullptr);
libraries.push_back(raced->second);
continue;
}
cache.emplace(key, handle);
if (optimize) {
for (std::uint32_t stage = 0; stage < create.stageCount; ++stage) {
for (auto& module : modules) {
if (module == VK_NULL_HANDLE || module != create.pStages[stage].module) continue;
device.modules.push_back(module);
module = VK_NULL_HANDLE;
}
}
}
++device.counters.built[part];
libraries.push_back(handle);
}
if (partsOnly) return VK_NULL_HANDLE;
PerformanceTimer timing("Vulkan.GraphicsPipelineLink");
VkPipelineLibraryCreateInfoKHR linked{VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR};
linked.libraryCount = static_cast<std::uint32_t>(libraries.size());
linked.pLibraries = libraries.data();
VkGraphicsPipelineCreateInfo create{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &linked};
create.layout = info.layout;
VkPipeline pipeline = VK_NULL_HANDLE;
lock.unlock();
{
auto pipelineCacheLock = context.LockPipelineCacheForUse();
Check(context.Function<PFN_vkCreateGraphicsPipelines>("vkCreateGraphicsPipelines")(context.device, context.pipelineCache, 1, &create, nullptr, &pipeline), "vkCreateGraphicsPipelines link");
}
lock.lock();
++device.counters.linked;
if (optimize && optimized != nullptr) {
auto slot = std::make_shared<OptimizedPipeline>();
slot->device = context.device;
slot->destroy = device.destroyPipeline;
*optimized = slot;
const auto createPipelines = context.Function<PFN_vkCreateGraphicsPipelines>("vkCreateGraphicsPipelines");
const auto cache = context.pipelineCache;
const auto cacheSynchronization = context.pipelineCacheSynchronization;
const auto libraryLayout = fullLayout;
std::function<void()> relink = [slot, createPipelines, cache, cacheSynchronization, libraryLayout, libraries, &store, devicePointer = &device] {
if (slot->released.load()) return;
VkPipelineLibraryCreateInfoKHR linked{VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR};
linked.libraryCount = static_cast<std::uint32_t>(libraries.size());
linked.pLibraries = libraries.data();
VkGraphicsPipelineCreateInfo create{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &linked};
create.flags = VK_PIPELINE_CREATE_LINK_TIME_OPTIMIZATION_BIT_EXT;
create.layout = libraryLayout;
VkPipeline result = VK_NULL_HANDLE;
const auto pipelineCreationResult = [&] {
auto pipelineCacheLock = cacheSynchronization != nullptr ? cacheSynchronization->LockForPipelineUse() : PipelineCacheSynchronization::PipelineUseLock{};
return createPipelines(slot->device, cache, 1, &create, nullptr, &result);
}();
if (pipelineCreationResult != VK_SUCCESS) return;
slot->handle.store(result);
if (slot->released.load()) {
if (const auto handle = slot->handle.exchange(VK_NULL_HANDLE)) slot->destroy(slot->device, handle, nullptr);
}
std::lock_guard lock(store.mutex);
++devicePointer->counters.optimized;
};
if (PipelineJournalReplaying()) {
slot->relink = std::move(relink);
slot->deferred.store(true, std::memory_order_release);
} else {
enqueue(store, context.device, std::move(relink));
}
}
return pipeline;
}
void OptimizedPipeline::Release() noexcept {
released.store(true);
if (const auto pipeline = handle.exchange(VK_NULL_HANDLE)) destroy(device, pipeline, nullptr);
}
void OptimizedPipeline::Schedule() {
if (!deferred.load(std::memory_order_acquire)) return;
auto& store = Libraries();
std::lock_guard lock(store.mutex);
if (!deferred.exchange(false, std::memory_order_acq_rel)) return;
if (relink && !released.load()) enqueue(store, device, std::move(relink));
relink = nullptr;
}
void WaitForOptimizedPipelines(VkDevice device) {
auto& store = Libraries();
std::unique_lock lock(store.mutex);
store.idle.wait(lock, [&] { return store.pending[device] == 0; });
}
void ClearPipelineLibraries(VkDevice device) {
auto& store = Libraries();
std::unique_lock lock(store.mutex);
store.idle.wait(lock, [&] { return store.pending[device] == 0 && store.activeBuilds[device] == 0; });
const auto found = store.devices.find(device);
if (found == store.devices.end()) return;
auto& libraries = found->second;
for (auto& part : libraries.parts) {
for (const auto& [key, handle] : part) libraries.destroyPipeline(device, handle, nullptr);
}
for (const auto& [key, handle] : libraries.layouts) libraries.destroyLayout(device, handle, nullptr);
for (const auto module : libraries.modules) libraries.destroyModule(device, module, nullptr);
store.devices.erase(found);
}
void StopPipelineLibraryWorker() {
auto& store = Libraries();
std::thread worker;
{
std::lock_guard lock(store.mutex);
store.stopping = true;
for (const auto& [device, job] : store.jobs) --store.pending[device];
store.jobs.clear();
worker = std::move(store.worker);
}
store.idle.notify_all();
store.work.notify_all();
if (!worker.joinable()) return;
if (worker.get_id() == std::this_thread::get_id()) worker.detach();
else worker.join();
}
unsigned PipelineLibraryWorkers() {
return runningWorkers.load(std::memory_order_acquire);
}
PipelineLibraryCounters PipelineLibraryCountersOf(VkDevice device) {
auto& store = Libraries();
std::lock_guard lock(store.mutex);
const auto found = store.devices.find(device);
return found == store.devices.end() ? PipelineLibraryCounters{} : found->second.counters;
}
}