vk_rasterizer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/memory_manager.h"
  14. #include "video_core/rasterizer_accelerated.h"
  15. #include "video_core/rasterizer_interface.h"
  16. #include "video_core/renderer_vulkan/declarations.h"
  17. #include "video_core/renderer_vulkan/fixed_pipeline_state.h"
  18. #include "video_core/renderer_vulkan/vk_buffer_cache.h"
  19. #include "video_core/renderer_vulkan/vk_compute_pass.h"
  20. #include "video_core/renderer_vulkan/vk_descriptor_pool.h"
  21. #include "video_core/renderer_vulkan/vk_memory_manager.h"
  22. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  23. #include "video_core/renderer_vulkan/vk_query_cache.h"
  24. #include "video_core/renderer_vulkan/vk_renderpass_cache.h"
  25. #include "video_core/renderer_vulkan/vk_resource_manager.h"
  26. #include "video_core/renderer_vulkan/vk_sampler_cache.h"
  27. #include "video_core/renderer_vulkan/vk_scheduler.h"
  28. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  29. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  30. #include "video_core/renderer_vulkan/vk_update_descriptor.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 =
  43. boost::container::static_vector<vk::ImageView, Maxwell::NumRenderTargets + 1>;
  44. struct FramebufferCacheKey {
  45. vk::RenderPass renderpass{};
  46. u32 width = 0;
  47. u32 height = 0;
  48. u32 layers = 0;
  49. ImageViewsPack views;
  50. std::size_t Hash() const noexcept {
  51. std::size_t hash = 0;
  52. boost::hash_combine(hash, static_cast<VkRenderPass>(renderpass));
  53. for (const auto& view : views) {
  54. boost::hash_combine(hash, static_cast<VkImageView>(view));
  55. }
  56. boost::hash_combine(hash, width);
  57. boost::hash_combine(hash, height);
  58. boost::hash_combine(hash, layers);
  59. return hash;
  60. }
  61. bool operator==(const FramebufferCacheKey& rhs) const noexcept {
  62. return std::tie(renderpass, views, width, height, layers) ==
  63. std::tie(rhs.renderpass, rhs.views, rhs.width, rhs.height, rhs.layers);
  64. }
  65. bool operator!=(const FramebufferCacheKey& rhs) const noexcept {
  66. return !operator==(rhs);
  67. }
  68. };
  69. } // namespace Vulkan
  70. namespace std {
  71. template <>
  72. struct hash<Vulkan::FramebufferCacheKey> {
  73. std::size_t operator()(const Vulkan::FramebufferCacheKey& k) const noexcept {
  74. return k.Hash();
  75. }
  76. };
  77. } // namespace std
  78. namespace Vulkan {
  79. class StateTracker;
  80. class BufferBindings;
  81. struct ImageView {
  82. View view;
  83. vk::ImageLayout* layout = nullptr;
  84. };
  85. class RasterizerVulkan final : public VideoCore::RasterizerAccelerated {
  86. public:
  87. explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window,
  88. VKScreenInfo& screen_info, const VKDevice& device,
  89. VKResourceManager& resource_manager, VKMemoryManager& memory_manager,
  90. StateTracker& state_tracker, VKScheduler& scheduler);
  91. ~RasterizerVulkan() override;
  92. void Draw(bool is_indexed, bool is_instanced) override;
  93. void Clear() override;
  94. void DispatchCompute(GPUVAddr code_addr) override;
  95. void ResetCounter(VideoCore::QueryType type) override;
  96. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  97. void FlushAll() override;
  98. void FlushRegion(CacheAddr addr, u64 size) override;
  99. void InvalidateRegion(CacheAddr addr, u64 size) override;
  100. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  101. void FlushCommands() override;
  102. void TickFrame() override;
  103. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  104. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  105. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  106. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  107. u32 pixel_stride) override;
  108. void SetupDirtyFlags() override;
  109. /// Maximum supported size that a constbuffer can have in bytes.
  110. static constexpr std::size_t MaxConstbufferSize = 0x10000;
  111. static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0,
  112. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  113. private:
  114. struct DrawParameters {
  115. void Draw(vk::CommandBuffer cmdbuf, const vk::DispatchLoaderDynamic& dld) const;
  116. u32 base_instance = 0;
  117. u32 num_instances = 0;
  118. u32 base_vertex = 0;
  119. u32 num_vertices = 0;
  120. bool is_indexed = 0;
  121. };
  122. using Texceptions = std::bitset<Maxwell::NumRenderTargets + 1>;
  123. static constexpr std::size_t ZETA_TEXCEPTION_INDEX = 8;
  124. void FlushWork();
  125. Texceptions UpdateAttachments();
  126. std::tuple<vk::Framebuffer, vk::Extent2D> ConfigureFramebuffers(vk::RenderPass renderpass);
  127. /// Setups geometry buffers and state.
  128. DrawParameters SetupGeometry(FixedPipelineState& fixed_state, BufferBindings& buffer_bindings,
  129. bool is_indexed, bool is_instanced);
  130. /// Setup descriptors in the graphics pipeline.
  131. void SetupShaderDescriptors(const std::array<Shader, Maxwell::MaxShaderProgram>& shaders);
  132. void SetupImageTransitions(Texceptions texceptions,
  133. const std::array<View, Maxwell::NumRenderTargets>& color_attachments,
  134. const View& zeta_attachment);
  135. void UpdateDynamicStates();
  136. bool WalkAttachmentOverlaps(const CachedSurfaceView& attachment);
  137. void SetupVertexArrays(FixedPipelineState::VertexInput& vertex_input,
  138. BufferBindings& buffer_bindings);
  139. void SetupIndexBuffer(BufferBindings& buffer_bindings, DrawParameters& params, bool is_indexed);
  140. /// Setup constant buffers in the graphics pipeline.
  141. void SetupGraphicsConstBuffers(const ShaderEntries& entries, std::size_t stage);
  142. /// Setup global buffers in the graphics pipeline.
  143. void SetupGraphicsGlobalBuffers(const ShaderEntries& entries, std::size_t stage);
  144. /// Setup texel buffers in the graphics pipeline.
  145. void SetupGraphicsTexelBuffers(const ShaderEntries& entries, std::size_t stage);
  146. /// Setup textures in the graphics pipeline.
  147. void SetupGraphicsTextures(const ShaderEntries& entries, std::size_t stage);
  148. /// Setup images in the graphics pipeline.
  149. void SetupGraphicsImages(const ShaderEntries& entries, std::size_t stage);
  150. /// Setup constant buffers in the compute pipeline.
  151. void SetupComputeConstBuffers(const ShaderEntries& entries);
  152. /// Setup global buffers in the compute pipeline.
  153. void SetupComputeGlobalBuffers(const ShaderEntries& entries);
  154. /// Setup texel buffers in the compute pipeline.
  155. void SetupComputeTexelBuffers(const ShaderEntries& entries);
  156. /// Setup textures in the compute pipeline.
  157. void SetupComputeTextures(const ShaderEntries& entries);
  158. /// Setup images in the compute pipeline.
  159. void SetupComputeImages(const ShaderEntries& entries);
  160. void SetupConstBuffer(const ConstBufferEntry& entry,
  161. const Tegra::Engines::ConstBufferInfo& buffer);
  162. void SetupGlobalBuffer(const GlobalBufferEntry& entry, GPUVAddr address);
  163. void SetupTexelBuffer(const Tegra::Texture::TICEntry& image, const TexelBufferEntry& entry);
  164. void SetupTexture(const Tegra::Texture::FullTextureInfo& texture, const SamplerEntry& entry);
  165. void SetupImage(const Tegra::Texture::TICEntry& tic, const ImageEntry& entry);
  166. void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
  167. void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
  168. void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);
  169. void UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs);
  170. void UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs);
  171. void UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs);
  172. std::size_t CalculateGraphicsStreamBufferSize(bool is_indexed) const;
  173. std::size_t CalculateComputeStreamBufferSize() const;
  174. std::size_t CalculateVertexArraysSize() const;
  175. std::size_t CalculateIndexBufferSize() const;
  176. std::size_t CalculateConstBufferSize(const ConstBufferEntry& entry,
  177. const Tegra::Engines::ConstBufferInfo& buffer) const;
  178. RenderPassParams GetRenderPassParams(Texceptions texceptions) const;
  179. Core::System& system;
  180. Core::Frontend::EmuWindow& render_window;
  181. VKScreenInfo& screen_info;
  182. const VKDevice& device;
  183. VKResourceManager& resource_manager;
  184. VKMemoryManager& memory_manager;
  185. StateTracker& state_tracker;
  186. VKScheduler& scheduler;
  187. VKStagingBufferPool staging_pool;
  188. VKDescriptorPool descriptor_pool;
  189. VKUpdateDescriptorQueue update_descriptor_queue;
  190. QuadArrayPass quad_array_pass;
  191. Uint8Pass uint8_pass;
  192. VKTextureCache texture_cache;
  193. VKPipelineCache pipeline_cache;
  194. VKBufferCache buffer_cache;
  195. VKSamplerCache sampler_cache;
  196. VKQueryCache query_cache;
  197. std::array<View, Maxwell::NumRenderTargets> color_attachments;
  198. View zeta_attachment;
  199. std::vector<ImageView> sampled_views;
  200. std::vector<ImageView> image_views;
  201. u32 draw_counter = 0;
  202. // TODO(Rodrigo): Invalidate on image destruction
  203. std::unordered_map<FramebufferCacheKey, UniqueFramebuffer> framebuffer_cache;
  204. };
  205. } // namespace Vulkan