summaryrefslogtreecommitdiff
path: root/src/video_core
diff options
context:
space:
mode:
authorGravatar ameerj2020-07-28 00:08:02 -0400
committerGravatar ameerj2020-08-16 12:02:22 -0400
commit6ac97405df021d5d2bd9a529253bd5c5a418c1a9 (patch)
tree0abc29657e9187bcfd3484ed7b091d0ea0f89f0f /src/video_core
parentMerge pull request #4528 from lioncash/discard (diff)
downloadyuzu-6ac97405df021d5d2bd9a529253bd5c5a418c1a9.tar.gz
yuzu-6ac97405df021d5d2bd9a529253bd5c5a418c1a9.tar.xz
yuzu-6ac97405df021d5d2bd9a529253bd5c5a418c1a9.zip
Vk Async pipeline compilation
Diffstat (limited to 'src/video_core')
-rw-r--r--src/video_core/renderer_vulkan/vk_device.cpp2
-rw-r--r--src/video_core/renderer_vulkan/vk_device.h5
-rw-r--r--src/video_core/renderer_vulkan/vk_fence_manager.cpp2
-rw-r--r--src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp5
-rw-r--r--src/video_core/renderer_vulkan/vk_graphics_pipeline.h6
-rw-r--r--src/video_core/renderer_vulkan/vk_pipeline_cache.cpp24
-rw-r--r--src/video_core/renderer_vulkan/vk_pipeline_cache.h27
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.cpp27
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.h10
-rw-r--r--src/video_core/renderer_vulkan/wrapper.cpp2
-rw-r--r--src/video_core/renderer_vulkan/wrapper.h2
-rw-r--r--src/video_core/shader/async_shaders.cpp59
-rw-r--r--src/video_core/shader/async_shaders.h31
13 files changed, 182 insertions, 20 deletions
diff --git a/src/video_core/renderer_vulkan/vk_device.cpp b/src/video_core/renderer_vulkan/vk_device.cpp
index 0c03e4d83..ebcfaa0e3 100644
--- a/src/video_core/renderer_vulkan/vk_device.cpp
+++ b/src/video_core/renderer_vulkan/vk_device.cpp
@@ -382,6 +382,8 @@ bool VKDevice::Create() {
382 382
383 graphics_queue = logical.GetQueue(graphics_family); 383 graphics_queue = logical.GetQueue(graphics_family);
384 present_queue = logical.GetQueue(present_family); 384 present_queue = logical.GetQueue(present_family);
385
386 use_asynchronous_shaders = Settings::values.use_asynchronous_shaders.GetValue();
385 return true; 387 return true;
386} 388}
387 389
diff --git a/src/video_core/renderer_vulkan/vk_device.h b/src/video_core/renderer_vulkan/vk_device.h
index 529744f2d..30cd3e189 100644
--- a/src/video_core/renderer_vulkan/vk_device.h
+++ b/src/video_core/renderer_vulkan/vk_device.h
@@ -202,6 +202,10 @@ public:
202 return reported_extensions; 202 return reported_extensions;
203 } 203 }
204 204
205 bool UseAsynchronousShaders() const {
206 return use_asynchronous_shaders;
207 }
208
205 /// Checks if the physical device is suitable. 209 /// Checks if the physical device is suitable.
206 static bool IsSuitable(vk::PhysicalDevice physical, VkSurfaceKHR surface); 210 static bool IsSuitable(vk::PhysicalDevice physical, VkSurfaceKHR surface);
207 211
@@ -251,6 +255,7 @@ private:
251 bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color. 255 bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
252 bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state. 256 bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
253 bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config. 257 bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
258 bool use_asynchronous_shaders{};
254 259
255 // Telemetry parameters 260 // Telemetry parameters
256 std::string vendor_name; ///< Device's driver name. 261 std::string vendor_name; ///< Device's driver name.
diff --git a/src/video_core/renderer_vulkan/vk_fence_manager.cpp b/src/video_core/renderer_vulkan/vk_fence_manager.cpp
index a02be5487..d7f65d435 100644
--- a/src/video_core/renderer_vulkan/vk_fence_manager.cpp
+++ b/src/video_core/renderer_vulkan/vk_fence_manager.cpp
@@ -29,7 +29,7 @@ void InnerFence::Queue() {
29 } 29 }
30 ASSERT(!event); 30 ASSERT(!event);
31 31
32 event = device.GetLogical().CreateEvent(); 32 event = device.GetLogical().CreateNewEvent();
33 ticks = scheduler.Ticks(); 33 ticks = scheduler.Ticks();
34 34
35 scheduler.RequestOutsideRenderPassOperationContext(); 35 scheduler.RequestOutsideRenderPassOperationContext();
diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
index aaf930b90..7d51b9836 100644
--- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
+++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
@@ -84,9 +84,8 @@ VKGraphicsPipeline::VKGraphicsPipeline(const VKDevice& device, VKScheduler& sche
84 update_descriptor_queue{update_descriptor_queue}, layout{CreatePipelineLayout()}, 84 update_descriptor_queue{update_descriptor_queue}, layout{CreatePipelineLayout()},
85 descriptor_template{CreateDescriptorUpdateTemplate(program)}, modules{CreateShaderModules( 85 descriptor_template{CreateDescriptorUpdateTemplate(program)}, modules{CreateShaderModules(
86 program)}, 86 program)},
87 renderpass{renderpass_cache.GetRenderPass(key.renderpass_params)}, pipeline{CreatePipeline( 87 renderpass{renderpass_cache.GetRenderPass(key.renderpass_params)},
88 key.renderpass_params, 88 pipeline{CreatePipeline(key.renderpass_params, program)}, m_key{key} {}
89 program)} {}
90 89
91VKGraphicsPipeline::~VKGraphicsPipeline() = default; 90VKGraphicsPipeline::~VKGraphicsPipeline() = default;
92 91
diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.h b/src/video_core/renderer_vulkan/vk_graphics_pipeline.h
index a1d699a6c..39c73a139 100644
--- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.h
+++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.h
@@ -54,6 +54,10 @@ public:
54 return renderpass; 54 return renderpass;
55 } 55 }
56 56
57 const GraphicsPipelineCacheKey& GetCacheKey() {
58 return m_key;
59 }
60
57private: 61private:
58 vk::DescriptorSetLayout CreateDescriptorSetLayout( 62 vk::DescriptorSetLayout CreateDescriptorSetLayout(
59 vk::Span<VkDescriptorSetLayoutBinding> bindings) const; 63 vk::Span<VkDescriptorSetLayoutBinding> bindings) const;
@@ -82,6 +86,8 @@ private:
82 86
83 VkRenderPass renderpass; 87 VkRenderPass renderpass;
84 vk::Pipeline pipeline; 88 vk::Pipeline pipeline;
89
90 const GraphicsPipelineCacheKey& m_key;
85}; 91};
86 92
87} // namespace Vulkan 93} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
index 418c62bc4..45d4dcb8c 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
@@ -205,7 +205,8 @@ std::array<Shader*, Maxwell::MaxShaderProgram> VKPipelineCache::GetShaders() {
205 return last_shaders = shaders; 205 return last_shaders = shaders;
206} 206}
207 207
208VKGraphicsPipeline& VKPipelineCache::GetGraphicsPipeline(const GraphicsPipelineCacheKey& key) { 208VKGraphicsPipeline& VKPipelineCache::GetGraphicsPipeline(
209 const GraphicsPipelineCacheKey& key, VideoCommon::Shader::AsyncShaders& async_shaders) {
209 MICROPROFILE_SCOPE(Vulkan_PipelineCache); 210 MICROPROFILE_SCOPE(Vulkan_PipelineCache);
210 211
211 if (last_graphics_pipeline && last_graphics_key == key) { 212 if (last_graphics_pipeline && last_graphics_key == key) {
@@ -213,11 +214,27 @@ VKGraphicsPipeline& VKPipelineCache::GetGraphicsPipeline(const GraphicsPipelineC
213 } 214 }
214 last_graphics_key = key; 215 last_graphics_key = key;
215 216
217 if (device.UseAsynchronousShaders()) {
218 auto work = async_shaders.GetCompletedWork();
219 for (std::size_t i = 0; i < work.size(); ++i) {
220 auto& entry = graphics_cache.at(work[i].pipeline->GetCacheKey());
221 entry = std::move(work[i].pipeline);
222 }
223 const auto [pair, is_cache_miss] = graphics_cache.try_emplace(key);
224 if (is_cache_miss) {
225 LOG_INFO(Render_Vulkan, "Compile 0x{:016X}", key.Hash());
226 const auto [program, bindings] = DecompileShaders(key.fixed_state);
227 async_shaders.QueueVulkanShader(this, bindings, program, key.renderpass_params,
228 key.padding, key.shaders, key.fixed_state);
229 }
230 return *(last_graphics_pipeline = graphics_cache.at(key).get());
231 }
232
216 const auto [pair, is_cache_miss] = graphics_cache.try_emplace(key); 233 const auto [pair, is_cache_miss] = graphics_cache.try_emplace(key);
217 auto& entry = pair->second; 234 auto& entry = pair->second;
218 if (is_cache_miss) { 235 if (is_cache_miss) {
219 LOG_INFO(Render_Vulkan, "Compile 0x{:016X}", key.Hash()); 236 LOG_INFO(Render_Vulkan, "Compile 0x{:016X}", key.Hash());
220 const auto [program, bindings] = DecompileShaders(key); 237 const auto [program, bindings] = DecompileShaders(key.fixed_state);
221 entry = std::make_unique<VKGraphicsPipeline>(device, scheduler, descriptor_pool, 238 entry = std::make_unique<VKGraphicsPipeline>(device, scheduler, descriptor_pool,
222 update_descriptor_queue, renderpass_cache, key, 239 update_descriptor_queue, renderpass_cache, key,
223 bindings, program); 240 bindings, program);
@@ -312,8 +329,7 @@ void VKPipelineCache::OnShaderRemoval(Shader* shader) {
312} 329}
313 330
314std::pair<SPIRVProgram, std::vector<VkDescriptorSetLayoutBinding>> 331std::pair<SPIRVProgram, std::vector<VkDescriptorSetLayoutBinding>>
315VKPipelineCache::DecompileShaders(const GraphicsPipelineCacheKey& key) { 332VKPipelineCache::DecompileShaders(const FixedPipelineState& fixed_state) {
316 const auto& fixed_state = key.fixed_state;
317 auto& memory_manager = system.GPU().MemoryManager(); 333 auto& memory_manager = system.GPU().MemoryManager();
318 const auto& gpu = system.GPU().Maxwell3D(); 334 const auto& gpu = system.GPU().Maxwell3D();
319 335
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.h b/src/video_core/renderer_vulkan/vk_pipeline_cache.h
index 0a3fe65fb..c70da6da4 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.h
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.h
@@ -22,6 +22,7 @@
22#include "video_core/renderer_vulkan/vk_renderpass_cache.h" 22#include "video_core/renderer_vulkan/vk_renderpass_cache.h"
23#include "video_core/renderer_vulkan/vk_shader_decompiler.h" 23#include "video_core/renderer_vulkan/vk_shader_decompiler.h"
24#include "video_core/renderer_vulkan/wrapper.h" 24#include "video_core/renderer_vulkan/wrapper.h"
25#include "video_core/shader/async_shaders.h"
25#include "video_core/shader/memory_util.h" 26#include "video_core/shader/memory_util.h"
26#include "video_core/shader/registry.h" 27#include "video_core/shader/registry.h"
27#include "video_core/shader/shader_ir.h" 28#include "video_core/shader/shader_ir.h"
@@ -152,16 +153,37 @@ public:
152 153
153 std::array<Shader*, Maxwell::MaxShaderProgram> GetShaders(); 154 std::array<Shader*, Maxwell::MaxShaderProgram> GetShaders();
154 155
155 VKGraphicsPipeline& GetGraphicsPipeline(const GraphicsPipelineCacheKey& key); 156 VKGraphicsPipeline& GetGraphicsPipeline(const GraphicsPipelineCacheKey& key,
157 VideoCommon::Shader::AsyncShaders& async_shaders);
156 158
157 VKComputePipeline& GetComputePipeline(const ComputePipelineCacheKey& key); 159 VKComputePipeline& GetComputePipeline(const ComputePipelineCacheKey& key);
158 160
161 const VKDevice& GetDevice() {
162 return device;
163 }
164
165 VKScheduler& GetScheduler() {
166 return scheduler;
167 }
168
169 VKDescriptorPool& GetDescriptorPool() {
170 return descriptor_pool;
171 }
172
173 VKUpdateDescriptorQueue& GetUpdateDescriptorQueue() {
174 return update_descriptor_queue;
175 }
176
177 VKRenderPassCache& GetRenderpassCache() {
178 return renderpass_cache;
179 }
180
159protected: 181protected:
160 void OnShaderRemoval(Shader* shader) final; 182 void OnShaderRemoval(Shader* shader) final;
161 183
162private: 184private:
163 std::pair<SPIRVProgram, std::vector<VkDescriptorSetLayoutBinding>> DecompileShaders( 185 std::pair<SPIRVProgram, std::vector<VkDescriptorSetLayoutBinding>> DecompileShaders(
164 const GraphicsPipelineCacheKey& key); 186 const FixedPipelineState& fixed_state);
165 187
166 Core::System& system; 188 Core::System& system;
167 const VKDevice& device; 189 const VKDevice& device;
@@ -177,6 +199,7 @@ private:
177 199
178 GraphicsPipelineCacheKey last_graphics_key; 200 GraphicsPipelineCacheKey last_graphics_key;
179 VKGraphicsPipeline* last_graphics_pipeline = nullptr; 201 VKGraphicsPipeline* last_graphics_pipeline = nullptr;
202 std::vector<std::unique_ptr<VKGraphicsPipeline>> duplicates;
180 203
181 std::unordered_map<GraphicsPipelineCacheKey, std::unique_ptr<VKGraphicsPipeline>> 204 std::unordered_map<GraphicsPipelineCacheKey, std::unique_ptr<VKGraphicsPipeline>>
182 graphics_cache; 205 graphics_cache;
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
index 7500e8244..6310e898c 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
@@ -400,8 +400,25 @@ RasterizerVulkan::RasterizerVulkan(Core::System& system, Core::Frontend::EmuWind
400 buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool), 400 buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool),
401 sampler_cache(device), 401 sampler_cache(device),
402 fence_manager(system, *this, device, scheduler, texture_cache, buffer_cache, query_cache), 402 fence_manager(system, *this, device, scheduler, texture_cache, buffer_cache, query_cache),
403 query_cache(system, *this, device, scheduler), wfi_event{device.GetLogical().CreateEvent()} { 403 query_cache(system, *this, device, scheduler),
404 wfi_event{device.GetLogical().CreateNewEvent()}, async_shaders{renderer} {
404 scheduler.SetQueryCache(query_cache); 405 scheduler.SetQueryCache(query_cache);
406 if (device.UseAsynchronousShaders()) {
407 // Max worker threads we should allow
408 constexpr auto MAX_THREADS = 2u;
409 // Amount of threads we should reserve for other parts of yuzu
410 constexpr auto RESERVED_THREADS = 6u;
411 // Get the amount of threads we can use(this can return zero)
412 const auto cpu_thread_count =
413 std::max(RESERVED_THREADS, std::thread::hardware_concurrency());
414 // Deduce how many "extra" threads we have to use.
415 const auto max_threads_unused = cpu_thread_count - RESERVED_THREADS;
416 // Always allow at least 1 thread regardless of our settings
417 const auto max_worker_count = std::max(1u, max_threads_unused);
418 // Don't use more than MAX_THREADS
419 const auto worker_count = std::min(max_worker_count, MAX_THREADS);
420 async_shaders.AllocateWorkers(worker_count);
421 }
405} 422}
406 423
407RasterizerVulkan::~RasterizerVulkan() = default; 424RasterizerVulkan::~RasterizerVulkan() = default;
@@ -439,7 +456,13 @@ void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
439 key.renderpass_params = GetRenderPassParams(texceptions); 456 key.renderpass_params = GetRenderPassParams(texceptions);
440 key.padding = 0; 457 key.padding = 0;
441 458
442 auto& pipeline = pipeline_cache.GetGraphicsPipeline(key); 459 auto& pipeline = pipeline_cache.GetGraphicsPipeline(key, async_shaders);
460 if (&pipeline == nullptr || pipeline.GetHandle() == VK_NULL_HANDLE) {
461 // Async graphics pipeline was not ready.
462 system.GPU().TickWork();
463 return;
464 }
465
443 scheduler.BindGraphicsPipeline(pipeline.GetHandle()); 466 scheduler.BindGraphicsPipeline(pipeline.GetHandle());
444 467
445 const auto renderpass = pipeline.GetRenderPass(); 468 const auto renderpass = pipeline.GetRenderPass();
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h
index 923178b0b..27604b9a3 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.h
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.h
@@ -32,6 +32,7 @@
32#include "video_core/renderer_vulkan/vk_texture_cache.h" 32#include "video_core/renderer_vulkan/vk_texture_cache.h"
33#include "video_core/renderer_vulkan/vk_update_descriptor.h" 33#include "video_core/renderer_vulkan/vk_update_descriptor.h"
34#include "video_core/renderer_vulkan/wrapper.h" 34#include "video_core/renderer_vulkan/wrapper.h"
35#include "video_core/shader/async_shaders.h"
35 36
36namespace Core { 37namespace Core {
37class System; 38class System;
@@ -136,6 +137,14 @@ public:
136 u32 pixel_stride) override; 137 u32 pixel_stride) override;
137 void SetupDirtyFlags() override; 138 void SetupDirtyFlags() override;
138 139
140 VideoCommon::Shader::AsyncShaders& GetAsyncShaders() {
141 return async_shaders;
142 }
143
144 const VideoCommon::Shader::AsyncShaders& GetAsyncShaders() const {
145 return async_shaders;
146 }
147
139 /// Maximum supported size that a constbuffer can have in bytes. 148 /// Maximum supported size that a constbuffer can have in bytes.
140 static constexpr std::size_t MaxConstbufferSize = 0x10000; 149 static constexpr std::size_t MaxConstbufferSize = 0x10000;
141 static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0, 150 static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0,
@@ -278,6 +287,7 @@ private:
278 VKMemoryManager& memory_manager; 287 VKMemoryManager& memory_manager;
279 StateTracker& state_tracker; 288 StateTracker& state_tracker;
280 VKScheduler& scheduler; 289 VKScheduler& scheduler;
290 VideoCommon::Shader::AsyncShaders async_shaders;
281 291
282 VKStagingBufferPool staging_pool; 292 VKStagingBufferPool staging_pool;
283 VKDescriptorPool descriptor_pool; 293 VKDescriptorPool descriptor_pool;
diff --git a/src/video_core/renderer_vulkan/wrapper.cpp b/src/video_core/renderer_vulkan/wrapper.cpp
index 14cac38ea..c43d60adf 100644
--- a/src/video_core/renderer_vulkan/wrapper.cpp
+++ b/src/video_core/renderer_vulkan/wrapper.cpp
@@ -644,7 +644,7 @@ ShaderModule Device::CreateShaderModule(const VkShaderModuleCreateInfo& ci) cons
644 return ShaderModule(object, handle, *dld); 644 return ShaderModule(object, handle, *dld);
645} 645}
646 646
647Event Device::CreateEvent() const { 647Event Device::CreateNewEvent() const {
648 static constexpr VkEventCreateInfo ci{ 648 static constexpr VkEventCreateInfo ci{
649 .sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO, 649 .sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
650 .pNext = nullptr, 650 .pNext = nullptr,
diff --git a/src/video_core/renderer_vulkan/wrapper.h b/src/video_core/renderer_vulkan/wrapper.h
index 31885ef42..b9d3fedc1 100644
--- a/src/video_core/renderer_vulkan/wrapper.h
+++ b/src/video_core/renderer_vulkan/wrapper.h
@@ -721,7 +721,7 @@ public:
721 721
722 ShaderModule CreateShaderModule(const VkShaderModuleCreateInfo& ci) const; 722 ShaderModule CreateShaderModule(const VkShaderModuleCreateInfo& ci) const;
723 723
724 Event CreateEvent() const; 724 Event CreateNewEvent() const;
725 725
726 SwapchainKHR CreateSwapchainKHR(const VkSwapchainCreateInfoKHR& ci) const; 726 SwapchainKHR CreateSwapchainKHR(const VkSwapchainCreateInfoKHR& ci) const;
727 727
diff --git a/src/video_core/shader/async_shaders.cpp b/src/video_core/shader/async_shaders.cpp
index b7f66d7ee..335a0d05b 100644
--- a/src/video_core/shader/async_shaders.cpp
+++ b/src/video_core/shader/async_shaders.cpp
@@ -113,15 +113,38 @@ void AsyncShaders::QueueOpenGLShader(const OpenGL::Device& device,
113 VAddr cpu_addr) { 113 VAddr cpu_addr) {
114 WorkerParams params{device.UseAssemblyShaders() ? AsyncShaders::Backend::GLASM 114 WorkerParams params{device.UseAssemblyShaders() ? AsyncShaders::Backend::GLASM
115 : AsyncShaders::Backend::OpenGL, 115 : AsyncShaders::Backend::OpenGL,
116 device, 116 &device,
117 shader_type, 117 shader_type,
118 uid, 118 uid,
119 std::move(code), 119 std::move(code),
120 std::move(code_b), 120 std::move(code_b),
121 main_offset, 121 main_offset,
122 compiler_settings, 122 compiler_settings,
123 registry, 123 &registry,
124 cpu_addr}; 124 cpu_addr};
125
126 std::unique_lock lock(queue_mutex);
127 pending_queue.push_back(std::move(params));
128 cv.notify_one();
129}
130
131void AsyncShaders::QueueVulkanShader(
132 Vulkan::VKPipelineCache* pp_cache, std::vector<VkDescriptorSetLayoutBinding> bindings,
133 Vulkan::SPIRVProgram program, Vulkan::RenderPassParams renderpass_params, u32 padding,
134 std::array<GPUVAddr, Vulkan::Maxwell::MaxShaderProgram> shaders,
135 Vulkan::FixedPipelineState fixed_state) {
136
137 WorkerParams params{
138 .backend = AsyncShaders::Backend::Vulkan,
139 .pp_cache = pp_cache,
140 .bindings = bindings,
141 .program = program,
142 .renderpass_params = renderpass_params,
143 .padding = padding,
144 .shaders = shaders,
145 .fixed_state = fixed_state,
146 };
147
125 std::unique_lock lock(queue_mutex); 148 std::unique_lock lock(queue_mutex);
126 pending_queue.push_back(std::move(params)); 149 pending_queue.push_back(std::move(params));
127 cv.notify_one(); 150 cv.notify_one();
@@ -140,6 +163,7 @@ void AsyncShaders::ShaderCompilerThread(Core::Frontend::GraphicsContext* context
140 if (!HasWorkQueued()) { 163 if (!HasWorkQueued()) {
141 continue; 164 continue;
142 } 165 }
166
143 // Another thread beat us, just unlock and wait for the next load 167 // Another thread beat us, just unlock and wait for the next load
144 if (pending_queue.empty()) { 168 if (pending_queue.empty()) {
145 continue; 169 continue;
@@ -152,10 +176,11 @@ void AsyncShaders::ShaderCompilerThread(Core::Frontend::GraphicsContext* context
152 176
153 if (work.backend == AsyncShaders::Backend::OpenGL || 177 if (work.backend == AsyncShaders::Backend::OpenGL ||
154 work.backend == AsyncShaders::Backend::GLASM) { 178 work.backend == AsyncShaders::Backend::GLASM) {
155 const ShaderIR ir(work.code, work.main_offset, work.compiler_settings, work.registry); 179 VideoCommon::Shader::Registry registry = *work.registry;
180 const ShaderIR ir(work.code, work.main_offset, work.compiler_settings, registry);
156 const auto scope = context->Acquire(); 181 const auto scope = context->Acquire();
157 auto program = 182 auto program =
158 OpenGL::BuildShader(work.device, work.shader_type, work.uid, ir, work.registry); 183 OpenGL::BuildShader(*work.device, work.shader_type, work.uid, ir, registry);
159 Result result{}; 184 Result result{};
160 result.backend = work.backend; 185 result.backend = work.backend;
161 result.cpu_address = work.cpu_address; 186 result.cpu_address = work.cpu_address;
@@ -174,6 +199,32 @@ void AsyncShaders::ShaderCompilerThread(Core::Frontend::GraphicsContext* context
174 std::unique_lock complete_lock(completed_mutex); 199 std::unique_lock complete_lock(completed_mutex);
175 finished_work.push_back(std::move(result)); 200 finished_work.push_back(std::move(result));
176 } 201 }
202
203 } else if (work.backend == AsyncShaders::Backend::Vulkan) {
204 Vulkan::GraphicsPipelineCacheKey params_key{
205 work.renderpass_params,
206 work.padding,
207 work.shaders,
208 work.fixed_state,
209 };
210 {
211 std::unique_lock complete_lock(completed_mutex);
212
213 // Duplicate creation of pipelines leads to instability and crashing, caused by a
214 // race condition but band-aid solution is locking the making of the pipeline
215 // results in only one pipeline created at a time.
216 Result result{
217 .backend = work.backend,
218 .pipeline = std::make_unique<Vulkan::VKGraphicsPipeline>(
219 work.pp_cache->GetDevice(), work.pp_cache->GetScheduler(),
220 work.pp_cache->GetDescriptorPool(),
221 work.pp_cache->GetUpdateDescriptorQueue(),
222 work.pp_cache->GetRenderpassCache(), params_key, work.bindings,
223 work.program),
224 };
225
226 finished_work.push_back(std::move(result));
227 }
177 } 228 }
178 } 229 }
179} 230}
diff --git a/src/video_core/shader/async_shaders.h b/src/video_core/shader/async_shaders.h
index 2f5ee94ad..702026ce2 100644
--- a/src/video_core/shader/async_shaders.h
+++ b/src/video_core/shader/async_shaders.h
@@ -14,6 +14,10 @@
14#include "video_core/renderer_opengl/gl_device.h" 14#include "video_core/renderer_opengl/gl_device.h"
15#include "video_core/renderer_opengl/gl_resource_manager.h" 15#include "video_core/renderer_opengl/gl_resource_manager.h"
16#include "video_core/renderer_opengl/gl_shader_decompiler.h" 16#include "video_core/renderer_opengl/gl_shader_decompiler.h"
17#include "video_core/renderer_vulkan/vk_device.h"
18#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
19#include "video_core/renderer_vulkan/vk_scheduler.h"
20#include "video_core/renderer_vulkan/vk_update_descriptor.h"
17 21
18namespace Core::Frontend { 22namespace Core::Frontend {
19class EmuWindow; 23class EmuWindow;
@@ -24,6 +28,10 @@ namespace Tegra {
24class GPU; 28class GPU;
25} 29}
26 30
31namespace Vulkan {
32class VKPipelineCache;
33}
34
27namespace VideoCommon::Shader { 35namespace VideoCommon::Shader {
28 36
29class AsyncShaders { 37class AsyncShaders {
@@ -31,6 +39,7 @@ public:
31 enum class Backend { 39 enum class Backend {
32 OpenGL, 40 OpenGL,
33 GLASM, 41 GLASM,
42 Vulkan,
34 }; 43 };
35 44
36 struct ResultPrograms { 45 struct ResultPrograms {
@@ -46,6 +55,7 @@ public:
46 std::vector<u64> code; 55 std::vector<u64> code;
47 std::vector<u64> code_b; 56 std::vector<u64> code_b;
48 Tegra::Engines::ShaderType shader_type; 57 Tegra::Engines::ShaderType shader_type;
58 std::unique_ptr<Vulkan::VKGraphicsPipeline> pipeline;
49 }; 59 };
50 60
51 explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window); 61 explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window);
@@ -76,6 +86,13 @@ public:
76 VideoCommon::Shader::CompilerSettings compiler_settings, 86 VideoCommon::Shader::CompilerSettings compiler_settings,
77 const VideoCommon::Shader::Registry& registry, VAddr cpu_addr); 87 const VideoCommon::Shader::Registry& registry, VAddr cpu_addr);
78 88
89 void QueueVulkanShader(Vulkan::VKPipelineCache* pp_cache,
90 std::vector<VkDescriptorSetLayoutBinding> bindings,
91 Vulkan::SPIRVProgram program, Vulkan::RenderPassParams renderpass_params,
92 u32 padding,
93 std::array<GPUVAddr, Vulkan::Maxwell::MaxShaderProgram> shaders,
94 Vulkan::FixedPipelineState fixed_state);
95
79private: 96private:
80 void ShaderCompilerThread(Core::Frontend::GraphicsContext* context); 97 void ShaderCompilerThread(Core::Frontend::GraphicsContext* context);
81 98
@@ -84,15 +101,25 @@ private:
84 101
85 struct WorkerParams { 102 struct WorkerParams {
86 AsyncShaders::Backend backend; 103 AsyncShaders::Backend backend;
87 OpenGL::Device device; 104 // For OGL
105 const OpenGL::Device* device;
88 Tegra::Engines::ShaderType shader_type; 106 Tegra::Engines::ShaderType shader_type;
89 u64 uid; 107 u64 uid;
90 std::vector<u64> code; 108 std::vector<u64> code;
91 std::vector<u64> code_b; 109 std::vector<u64> code_b;
92 u32 main_offset; 110 u32 main_offset;
93 VideoCommon::Shader::CompilerSettings compiler_settings; 111 VideoCommon::Shader::CompilerSettings compiler_settings;
94 VideoCommon::Shader::Registry registry; 112 const VideoCommon::Shader::Registry* registry;
95 VAddr cpu_address; 113 VAddr cpu_address;
114
115 // For Vulkan
116 Vulkan::VKPipelineCache* pp_cache;
117 std::vector<VkDescriptorSetLayoutBinding> bindings;
118 Vulkan::SPIRVProgram program;
119 Vulkan::RenderPassParams renderpass_params;
120 u32 padding;
121 std::array<GPUVAddr, Vulkan::Maxwell::MaxShaderProgram> shaders;
122 Vulkan::FixedPipelineState fixed_state;
96 }; 123 };
97 124
98 std::condition_variable cv; 125 std::condition_variable cv;