vk_rasterizer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <bitset>
  7. #include <memory>
  8. #include <utility>
  9. #include <vector>
  10. #include <boost/container/static_vector.hpp>
  11. #include <boost/functional/hash.hpp>
  12. #include "common/common_types.h"
  13. #include "video_core/rasterizer_accelerated.h"
  14. #include "video_core/rasterizer_interface.h"
  15. #include "video_core/renderer_vulkan/fixed_pipeline_state.h"
  16. #include "video_core/renderer_vulkan/vk_buffer_cache.h"
  17. #include "video_core/renderer_vulkan/vk_compute_pass.h"
  18. #include "video_core/renderer_vulkan/vk_descriptor_pool.h"
  19. #include "video_core/renderer_vulkan/vk_fence_manager.h"
  20. #include "video_core/renderer_vulkan/vk_memory_manager.h"
  21. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  22. #include "video_core/renderer_vulkan/vk_query_cache.h"
  23. #include "video_core/renderer_vulkan/vk_renderpass_cache.h"
  24. #include "video_core/renderer_vulkan/vk_resource_manager.h"
  25. #include "video_core/renderer_vulkan/vk_sampler_cache.h"
  26. #include "video_core/renderer_vulkan/vk_scheduler.h"
  27. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  28. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  29. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  30. #include "video_core/renderer_vulkan/wrapper.h"
  31. namespace Core {
  32. class System;
  33. }
  34. namespace Core::Frontend {
  35. class EmuWindow;
  36. }
  37. namespace Tegra::Engines {
  38. class Maxwell3D;
  39. }
  40. namespace Vulkan {
  41. struct VKScreenInfo;
  42. using ImageViewsPack = boost::container::static_vector<VkImageView, Maxwell::NumRenderTargets + 1>;
  43. struct FramebufferCacheKey {
  44. VkRenderPass renderpass{};
  45. u32 width = 0;
  46. u32 height = 0;
  47. u32 layers = 0;
  48. ImageViewsPack views;
  49. std::size_t Hash() const noexcept {
  50. std::size_t hash = 0;
  51. boost::hash_combine(hash, static_cast<VkRenderPass>(renderpass));
  52. for (const auto& view : views) {
  53. boost::hash_combine(hash, static_cast<VkImageView>(view));
  54. }
  55. boost::hash_combine(hash, width);
  56. boost::hash_combine(hash, height);
  57. boost::hash_combine(hash, layers);
  58. return hash;
  59. }
  60. bool operator==(const FramebufferCacheKey& rhs) const noexcept {
  61. return std::tie(renderpass, views, width, height, layers) ==
  62. std::tie(rhs.renderpass, rhs.views, rhs.width, rhs.height, rhs.layers);
  63. }
  64. bool operator!=(const FramebufferCacheKey& rhs) const noexcept {
  65. return !operator==(rhs);
  66. }
  67. };
  68. } // namespace Vulkan
  69. namespace std {
  70. template <>
  71. struct hash<Vulkan::FramebufferCacheKey> {
  72. std::size_t operator()(const Vulkan::FramebufferCacheKey& k) const noexcept {
  73. return k.Hash();
  74. }
  75. };
  76. } // namespace std
  77. namespace Vulkan {
  78. class StateTracker;
  79. class BufferBindings;
  80. struct ImageView {
  81. View view;
  82. VkImageLayout* layout = nullptr;
  83. };
  84. class RasterizerVulkan final : public VideoCore::RasterizerAccelerated {
  85. public:
  86. explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window,
  87. VKScreenInfo& screen_info, const VKDevice& device,
  88. VKResourceManager& resource_manager, VKMemoryManager& memory_manager,
  89. StateTracker& state_tracker, VKScheduler& scheduler);
  90. ~RasterizerVulkan() override;
  91. void Draw(bool is_indexed, bool is_instanced) override;
  92. void Clear() override;
  93. void DispatchCompute(GPUVAddr code_addr) override;
  94. void ResetCounter(VideoCore::QueryType type) override;
  95. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  96. void FlushAll() override;
  97. void FlushRegion(VAddr addr, u64 size) override;
  98. bool MustFlushRegion(VAddr addr, u64 size) override;
  99. void InvalidateRegion(VAddr addr, u64 size) override;
  100. void OnCPUWrite(VAddr addr, u64 size) override;
  101. void SyncGuestHost() override;
  102. void SignalSemaphore(GPUVAddr addr, u32 value) override;
  103. void SignalSyncPoint(u32 value) override;
  104. void ReleaseFences() override;
  105. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  106. void WaitForIdle() override;
  107. void FlushCommands() override;
  108. void TickFrame() override;
  109. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  110. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  111. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  112. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  113. u32 pixel_stride) override;
  114. void SetupDirtyFlags() override;
  115. /// Maximum supported size that a constbuffer can have in bytes.
  116. static constexpr std::size_t MaxConstbufferSize = 0x10000;
  117. static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0,
  118. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  119. private:
  120. struct DrawParameters {
  121. void Draw(vk::CommandBuffer cmdbuf) const;
  122. u32 base_instance = 0;
  123. u32 num_instances = 0;
  124. u32 base_vertex = 0;
  125. u32 num_vertices = 0;
  126. bool is_indexed = 0;
  127. };
  128. using Texceptions = std::bitset<Maxwell::NumRenderTargets + 1>;
  129. static constexpr std::size_t ZETA_TEXCEPTION_INDEX = 8;
  130. static constexpr VkDeviceSize DEFAULT_BUFFER_SIZE = 4 * sizeof(float);
  131. void FlushWork();
  132. /// @brief Updates the currently bound attachments
  133. /// @param is_clear True when the framebuffer is updated as a clear
  134. /// @return Bitfield of attachments being used as sampled textures
  135. Texceptions UpdateAttachments(bool is_clear);
  136. std::tuple<VkFramebuffer, VkExtent2D> ConfigureFramebuffers(VkRenderPass renderpass);
  137. /// Setups geometry buffers and state.
  138. DrawParameters SetupGeometry(FixedPipelineState& fixed_state, BufferBindings& buffer_bindings,
  139. bool is_indexed, bool is_instanced);
  140. /// Setup descriptors in the graphics pipeline.
  141. void SetupShaderDescriptors(const std::array<Shader*, Maxwell::MaxShaderProgram>& shaders);
  142. void SetupImageTransitions(Texceptions texceptions,
  143. const std::array<View, Maxwell::NumRenderTargets>& color_attachments,
  144. const View& zeta_attachment);
  145. void UpdateDynamicStates();
  146. void BeginTransformFeedback();
  147. void EndTransformFeedback();
  148. bool WalkAttachmentOverlaps(const CachedSurfaceView& attachment);
  149. void SetupVertexArrays(FixedPipelineState::VertexInput& vertex_input,
  150. BufferBindings& buffer_bindings);
  151. void SetupIndexBuffer(BufferBindings& buffer_bindings, DrawParameters& params, bool is_indexed);
  152. /// Setup constant buffers in the graphics pipeline.
  153. void SetupGraphicsConstBuffers(const ShaderEntries& entries, std::size_t stage);
  154. /// Setup global buffers in the graphics pipeline.
  155. void SetupGraphicsGlobalBuffers(const ShaderEntries& entries, std::size_t stage);
  156. /// Setup uniform texels in the graphics pipeline.
  157. void SetupGraphicsUniformTexels(const ShaderEntries& entries, std::size_t stage);
  158. /// Setup textures in the graphics pipeline.
  159. void SetupGraphicsTextures(const ShaderEntries& entries, std::size_t stage);
  160. /// Setup storage texels in the graphics pipeline.
  161. void SetupGraphicsStorageTexels(const ShaderEntries& entries, std::size_t stage);
  162. /// Setup images in the graphics pipeline.
  163. void SetupGraphicsImages(const ShaderEntries& entries, std::size_t stage);
  164. /// Setup constant buffers in the compute pipeline.
  165. void SetupComputeConstBuffers(const ShaderEntries& entries);
  166. /// Setup global buffers in the compute pipeline.
  167. void SetupComputeGlobalBuffers(const ShaderEntries& entries);
  168. /// Setup texel buffers in the compute pipeline.
  169. void SetupComputeUniformTexels(const ShaderEntries& entries);
  170. /// Setup textures in the compute pipeline.
  171. void SetupComputeTextures(const ShaderEntries& entries);
  172. /// Setup storage texels in the compute pipeline.
  173. void SetupComputeStorageTexels(const ShaderEntries& entries);
  174. /// Setup images in the compute pipeline.
  175. void SetupComputeImages(const ShaderEntries& entries);
  176. void SetupConstBuffer(const ConstBufferEntry& entry,
  177. const Tegra::Engines::ConstBufferInfo& buffer);
  178. void SetupGlobalBuffer(const GlobalBufferEntry& entry, GPUVAddr address);
  179. void SetupUniformTexels(const Tegra::Texture::TICEntry& image, const UniformTexelEntry& entry);
  180. void SetupTexture(const Tegra::Texture::FullTextureInfo& texture, const SamplerEntry& entry);
  181. void SetupStorageTexel(const Tegra::Texture::TICEntry& tic, const StorageTexelEntry& entry);
  182. void SetupImage(const Tegra::Texture::TICEntry& tic, const ImageEntry& entry);
  183. void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
  184. void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
  185. void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);
  186. void UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs);
  187. void UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs);
  188. void UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs);
  189. std::size_t CalculateGraphicsStreamBufferSize(bool is_indexed) const;
  190. std::size_t CalculateComputeStreamBufferSize() const;
  191. std::size_t CalculateVertexArraysSize() const;
  192. std::size_t CalculateIndexBufferSize() const;
  193. std::size_t CalculateConstBufferSize(const ConstBufferEntry& entry,
  194. const Tegra::Engines::ConstBufferInfo& buffer) const;
  195. RenderPassParams GetRenderPassParams(Texceptions texceptions) const;
  196. VkBuffer DefaultBuffer();
  197. Core::System& system;
  198. Core::Frontend::EmuWindow& render_window;
  199. VKScreenInfo& screen_info;
  200. const VKDevice& device;
  201. VKResourceManager& resource_manager;
  202. VKMemoryManager& memory_manager;
  203. StateTracker& state_tracker;
  204. VKScheduler& scheduler;
  205. VKStagingBufferPool staging_pool;
  206. VKDescriptorPool descriptor_pool;
  207. VKUpdateDescriptorQueue update_descriptor_queue;
  208. VKRenderPassCache renderpass_cache;
  209. QuadArrayPass quad_array_pass;
  210. QuadIndexedPass quad_indexed_pass;
  211. Uint8Pass uint8_pass;
  212. VKTextureCache texture_cache;
  213. VKPipelineCache pipeline_cache;
  214. VKBufferCache buffer_cache;
  215. VKSamplerCache sampler_cache;
  216. VKFenceManager fence_manager;
  217. VKQueryCache query_cache;
  218. vk::Buffer default_buffer;
  219. VKMemoryCommit default_buffer_commit;
  220. vk::Event wfi_event;
  221. std::array<View, Maxwell::NumRenderTargets> color_attachments;
  222. View zeta_attachment;
  223. std::vector<ImageView> sampled_views;
  224. std::vector<ImageView> image_views;
  225. u32 draw_counter = 0;
  226. // TODO(Rodrigo): Invalidate on image destruction
  227. std::unordered_map<FramebufferCacheKey, vk::Framebuffer> framebuffer_cache;
  228. };
  229. } // namespace Vulkan