async_shaders.h 4.7 KB

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