async_shaders.h 4.3 KB

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