2020-07-09 23:36:38 -04:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-07-16 04:38:35 -04:00
|
|
|
#include <condition_variable>
|
2020-07-09 23:36:38 -04:00
|
|
|
#include <memory>
|
|
|
|
#include <shared_mutex>
|
|
|
|
#include <thread>
|
2020-08-24 01:15:48 -04:00
|
|
|
|
2020-09-10 02:43:30 -04:00
|
|
|
// This header includes both Vulkan and OpenGL headers, this has to be fixed
|
|
|
|
// Unfortunately, including OpenGL will include Windows.h that defines macros that can cause issues.
|
|
|
|
// Forcefully include glad early and undefine macros
|
|
|
|
#include <glad/glad.h>
|
|
|
|
#ifdef CreateEvent
|
|
|
|
#undef CreateEvent
|
|
|
|
#endif
|
|
|
|
#ifdef CreateSemaphore
|
|
|
|
#undef CreateSemaphore
|
|
|
|
#endif
|
|
|
|
|
2020-07-09 23:36:38 -04:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_device.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
|
2020-07-28 00:08:02 -04:00
|
|
|
#include "video_core/renderer_vulkan/vk_device.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_scheduler.h"
|
2020-07-09 23:36:38 -04:00
|
|
|
|
|
|
|
namespace Core::Frontend {
|
|
|
|
class EmuWindow;
|
|
|
|
class GraphicsContext;
|
|
|
|
} // namespace Core::Frontend
|
|
|
|
|
|
|
|
namespace Tegra {
|
|
|
|
class GPU;
|
|
|
|
}
|
|
|
|
|
2020-07-28 00:08:02 -04:00
|
|
|
namespace Vulkan {
|
|
|
|
class VKPipelineCache;
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:36:38 -04:00
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
|
|
|
|
class AsyncShaders {
|
|
|
|
public:
|
|
|
|
enum class Backend {
|
|
|
|
OpenGL,
|
|
|
|
GLASM,
|
2020-07-28 00:08:02 -04:00
|
|
|
Vulkan,
|
2020-07-09 23:36:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ResultPrograms {
|
|
|
|
OpenGL::OGLProgram opengl;
|
|
|
|
OpenGL::OGLAssemblyProgram glasm;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Result {
|
|
|
|
u64 uid;
|
|
|
|
VAddr cpu_address;
|
|
|
|
Backend backend;
|
|
|
|
ResultPrograms program;
|
|
|
|
std::vector<u64> code;
|
|
|
|
std::vector<u64> code_b;
|
|
|
|
Tegra::Engines::ShaderType shader_type;
|
|
|
|
};
|
|
|
|
|
2020-12-05 11:40:14 -05:00
|
|
|
explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window_);
|
2020-07-09 23:36:38 -04:00
|
|
|
~AsyncShaders();
|
|
|
|
|
|
|
|
/// Start up shader worker threads
|
2020-08-05 12:53:26 -04:00
|
|
|
void AllocateWorkers();
|
2020-07-09 23:36:38 -04:00
|
|
|
|
|
|
|
/// Clear the shader queue and kill all worker threads
|
|
|
|
void FreeWorkers();
|
|
|
|
|
|
|
|
// Force end all threads
|
|
|
|
void KillWorkers();
|
|
|
|
|
|
|
|
/// Check to see if any shaders have actually been compiled
|
2020-08-24 01:15:48 -04:00
|
|
|
[[nodiscard]] bool HasCompletedWork() const;
|
2020-07-09 23:36:38 -04:00
|
|
|
|
|
|
|
/// Deduce if a shader can be build on another thread of MUST be built in sync. We cannot build
|
|
|
|
/// every shader async as some shaders are only built and executed once. We try to "guess" which
|
|
|
|
/// shader would be used only once
|
2020-08-24 01:15:48 -04:00
|
|
|
[[nodiscard]] bool IsShaderAsync(const Tegra::GPU& gpu) const;
|
2020-07-09 23:36:38 -04:00
|
|
|
|
|
|
|
/// Pulls completed compiled shaders
|
2020-08-24 01:15:48 -04:00
|
|
|
[[nodiscard]] std::vector<Result> GetCompletedWork();
|
2020-07-09 23:36:38 -04:00
|
|
|
|
|
|
|
void QueueOpenGLShader(const OpenGL::Device& device, Tegra::Engines::ShaderType shader_type,
|
|
|
|
u64 uid, std::vector<u64> code, std::vector<u64> code_b, u32 main_offset,
|
2020-08-24 01:15:48 -04:00
|
|
|
CompilerSettings compiler_settings, const Registry& registry,
|
|
|
|
VAddr cpu_addr);
|
2020-07-09 23:36:38 -04:00
|
|
|
|
2020-08-02 13:05:41 -04:00
|
|
|
void QueueVulkanShader(Vulkan::VKPipelineCache* pp_cache, const Vulkan::VKDevice& device,
|
|
|
|
Vulkan::VKScheduler& scheduler,
|
|
|
|
Vulkan::VKDescriptorPool& descriptor_pool,
|
|
|
|
Vulkan::VKUpdateDescriptorQueue& update_descriptor_queue,
|
2020-07-28 00:08:02 -04:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> bindings,
|
2020-12-30 00:25:23 -05:00
|
|
|
Vulkan::SPIRVProgram program, Vulkan::GraphicsPipelineCacheKey key,
|
|
|
|
u32 num_color_buffers);
|
2020-07-28 00:08:02 -04:00
|
|
|
|
2020-07-09 23:36:38 -04:00
|
|
|
private:
|
|
|
|
void ShaderCompilerThread(Core::Frontend::GraphicsContext* context);
|
|
|
|
|
2020-07-16 04:38:35 -04:00
|
|
|
/// Check our worker queue to see if we have any work queued already
|
2020-08-24 01:15:48 -04:00
|
|
|
[[nodiscard]] bool HasWorkQueued() const;
|
2020-07-16 04:38:35 -04:00
|
|
|
|
2020-07-09 23:36:38 -04:00
|
|
|
struct WorkerParams {
|
2020-07-30 15:41:11 -04:00
|
|
|
Backend backend;
|
2020-07-28 00:08:02 -04:00
|
|
|
// For OGL
|
|
|
|
const OpenGL::Device* device;
|
2020-07-09 23:36:38 -04:00
|
|
|
Tegra::Engines::ShaderType shader_type;
|
|
|
|
u64 uid;
|
|
|
|
std::vector<u64> code;
|
|
|
|
std::vector<u64> code_b;
|
|
|
|
u32 main_offset;
|
2020-08-24 01:15:48 -04:00
|
|
|
CompilerSettings compiler_settings;
|
|
|
|
std::optional<Registry> registry;
|
2020-07-09 23:36:38 -04:00
|
|
|
VAddr cpu_address;
|
2020-07-28 00:08:02 -04:00
|
|
|
|
|
|
|
// For Vulkan
|
|
|
|
Vulkan::VKPipelineCache* pp_cache;
|
2020-08-02 13:05:41 -04:00
|
|
|
const Vulkan::VKDevice* vk_device;
|
|
|
|
Vulkan::VKScheduler* scheduler;
|
|
|
|
Vulkan::VKDescriptorPool* descriptor_pool;
|
|
|
|
Vulkan::VKUpdateDescriptorQueue* update_descriptor_queue;
|
2020-07-28 00:08:02 -04:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> bindings;
|
|
|
|
Vulkan::SPIRVProgram program;
|
2020-08-02 13:05:41 -04:00
|
|
|
Vulkan::GraphicsPipelineCacheKey key;
|
2020-12-30 00:25:23 -05:00
|
|
|
u32 num_color_buffers;
|
2020-07-09 23:36:38 -04:00
|
|
|
};
|
|
|
|
|
2020-07-16 04:38:35 -04:00
|
|
|
std::condition_variable cv;
|
2020-08-24 01:15:48 -04:00
|
|
|
mutable std::mutex queue_mutex;
|
|
|
|
mutable std::shared_mutex completed_mutex;
|
2020-07-09 23:36:38 -04:00
|
|
|
std::atomic<bool> is_thread_exiting{};
|
|
|
|
std::vector<std::unique_ptr<Core::Frontend::GraphicsContext>> context_list;
|
|
|
|
std::vector<std::thread> worker_threads;
|
2020-08-02 13:05:41 -04:00
|
|
|
std::queue<WorkerParams> pending_queue;
|
2020-08-24 01:15:48 -04:00
|
|
|
std::vector<Result> finished_work;
|
2020-07-09 23:36:38 -04:00
|
|
|
Core::Frontend::EmuWindow& emu_window;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon::Shader
|