async_shaders.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <condition_variable>
  6. #include <deque>
  7. #include <memory>
  8. #include <shared_mutex>
  9. #include <thread>
  10. #include "common/bit_field.h"
  11. #include "common/common_types.h"
  12. #include "video_core/renderer_opengl/gl_device.h"
  13. #include "video_core/renderer_opengl/gl_resource_manager.h"
  14. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  15. #include "video_core/renderer_vulkan/vk_device.h"
  16. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  17. #include "video_core/renderer_vulkan/vk_scheduler.h"
  18. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  19. namespace Core::Frontend {
  20. class EmuWindow;
  21. class GraphicsContext;
  22. } // namespace Core::Frontend
  23. namespace Tegra {
  24. class GPU;
  25. }
  26. namespace Vulkan {
  27. class VKPipelineCache;
  28. }
  29. namespace VideoCommon::Shader {
  30. class AsyncShaders {
  31. public:
  32. enum class Backend {
  33. OpenGL,
  34. GLASM,
  35. Vulkan,
  36. };
  37. struct ResultPrograms {
  38. OpenGL::OGLProgram opengl;
  39. OpenGL::OGLAssemblyProgram glasm;
  40. };
  41. struct Result {
  42. u64 uid;
  43. VAddr cpu_address;
  44. Backend backend;
  45. ResultPrograms program;
  46. std::vector<u64> code;
  47. std::vector<u64> code_b;
  48. Tegra::Engines::ShaderType shader_type;
  49. std::unique_ptr<Vulkan::VKGraphicsPipeline> pipeline;
  50. };
  51. explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window);
  52. ~AsyncShaders();
  53. /// Start up shader worker threads
  54. void AllocateWorkers(std::size_t num_workers);
  55. /// Clear the shader queue and kill all worker threads
  56. void FreeWorkers();
  57. // Force end all threads
  58. void KillWorkers();
  59. /// Check to see if any shaders have actually been compiled
  60. bool HasCompletedWork();
  61. /// Deduce if a shader can be build on another thread of MUST be built in sync. We cannot build
  62. /// every shader async as some shaders are only built and executed once. We try to "guess" which
  63. /// shader would be used only once
  64. bool IsShaderAsync(const Tegra::GPU& gpu) const;
  65. /// Pulls completed compiled shaders
  66. std::vector<Result> GetCompletedWork();
  67. void QueueOpenGLShader(const OpenGL::Device& device, Tegra::Engines::ShaderType shader_type,
  68. u64 uid, std::vector<u64> code, std::vector<u64> code_b, u32 main_offset,
  69. VideoCommon::Shader::CompilerSettings compiler_settings,
  70. const VideoCommon::Shader::Registry& registry, VAddr cpu_addr);
  71. void QueueVulkanShader(Vulkan::VKPipelineCache* pp_cache, const Vulkan::VKDevice& device,
  72. Vulkan::VKScheduler& scheduler,
  73. Vulkan::VKDescriptorPool& descriptor_pool,
  74. Vulkan::VKUpdateDescriptorQueue& update_descriptor_queue,
  75. Vulkan::VKRenderPassCache& renderpass_cache,
  76. std::vector<VkDescriptorSetLayoutBinding> bindings,
  77. Vulkan::SPIRVProgram program, Vulkan::GraphicsPipelineCacheKey key);
  78. private:
  79. void ShaderCompilerThread(Core::Frontend::GraphicsContext* context);
  80. /// Check our worker queue to see if we have any work queued already
  81. bool HasWorkQueued();
  82. struct WorkerParams {
  83. Backend backend;
  84. // For OGL
  85. const OpenGL::Device* device;
  86. Tegra::Engines::ShaderType shader_type;
  87. u64 uid;
  88. std::vector<u64> code;
  89. std::vector<u64> code_b;
  90. u32 main_offset;
  91. VideoCommon::Shader::CompilerSettings compiler_settings;
  92. const VideoCommon::Shader::Registry* registry;
  93. VAddr cpu_address;
  94. // For Vulkan
  95. Vulkan::VKPipelineCache* pp_cache;
  96. const Vulkan::VKDevice* vk_device;
  97. Vulkan::VKScheduler* scheduler;
  98. Vulkan::VKDescriptorPool* descriptor_pool;
  99. Vulkan::VKUpdateDescriptorQueue* update_descriptor_queue;
  100. Vulkan::VKRenderPassCache* renderpass_cache;
  101. std::vector<VkDescriptorSetLayoutBinding> bindings;
  102. Vulkan::SPIRVProgram program;
  103. Vulkan::GraphicsPipelineCacheKey key;
  104. };
  105. std::condition_variable cv;
  106. std::mutex queue_mutex;
  107. std::shared_mutex completed_mutex;
  108. std::atomic<bool> is_thread_exiting{};
  109. std::vector<std::unique_ptr<Core::Frontend::GraphicsContext>> context_list;
  110. std::vector<std::thread> worker_threads;
  111. std::queue<WorkerParams> pending_queue;
  112. std::vector<AsyncShaders::Result> finished_work;
  113. Core::Frontend::EmuWindow& emu_window;
  114. };
  115. } // namespace VideoCommon::Shader