vk_rasterizer.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "common/common_types.h"
  12. #include "video_core/rasterizer_accelerated.h"
  13. #include "video_core/rasterizer_interface.h"
  14. #include "video_core/renderer_vulkan/blit_image.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_scheduler.h"
  24. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  25. #include "video_core/renderer_vulkan/vk_stream_buffer.h"
  26. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  27. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  28. #include "video_core/renderer_vulkan/wrapper.h"
  29. #include "video_core/shader/async_shaders.h"
  30. namespace Core {
  31. class System;
  32. }
  33. namespace Core::Frontend {
  34. class EmuWindow;
  35. }
  36. namespace Tegra::Engines {
  37. class Maxwell3D;
  38. }
  39. namespace Vulkan {
  40. struct VKScreenInfo;
  41. class StateTracker;
  42. class BufferBindings;
  43. class RasterizerVulkan final : public VideoCore::RasterizerAccelerated {
  44. public:
  45. explicit RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  46. Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_,
  47. VKScreenInfo& screen_info_, const VKDevice& device_,
  48. VKMemoryManager& memory_manager_, StateTracker& state_tracker_,
  49. VKScheduler& scheduler_);
  50. ~RasterizerVulkan() override;
  51. void Draw(bool is_indexed, bool is_instanced) override;
  52. void Clear() override;
  53. void DispatchCompute(GPUVAddr code_addr) override;
  54. void ResetCounter(VideoCore::QueryType type) override;
  55. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  56. void FlushAll() override;
  57. void FlushRegion(VAddr addr, u64 size) override;
  58. bool MustFlushRegion(VAddr addr, u64 size) override;
  59. void InvalidateRegion(VAddr addr, u64 size) override;
  60. void OnCPUWrite(VAddr addr, u64 size) override;
  61. void SyncGuestHost() override;
  62. void UnmapMemory(VAddr addr, u64 size) override;
  63. void SignalSemaphore(GPUVAddr addr, u32 value) override;
  64. void SignalSyncPoint(u32 value) override;
  65. void ReleaseFences() override;
  66. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  67. void WaitForIdle() override;
  68. void FragmentBarrier() override;
  69. void TiledCacheBarrier() override;
  70. void FlushCommands() override;
  71. void TickFrame() override;
  72. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  73. const Tegra::Engines::Fermi2D::Surface& dst,
  74. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  75. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  76. u32 pixel_stride) override;
  77. VideoCommon::Shader::AsyncShaders& GetAsyncShaders() {
  78. return async_shaders;
  79. }
  80. const VideoCommon::Shader::AsyncShaders& GetAsyncShaders() const {
  81. return async_shaders;
  82. }
  83. /// Maximum supported size that a constbuffer can have in bytes.
  84. static constexpr size_t MaxConstbufferSize = 0x10000;
  85. static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0,
  86. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  87. private:
  88. static constexpr size_t MAX_TEXTURES = 192;
  89. static constexpr size_t MAX_IMAGES = 48;
  90. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  91. static constexpr VkDeviceSize DEFAULT_BUFFER_SIZE = 4 * sizeof(float);
  92. struct DrawParameters {
  93. void Draw(vk::CommandBuffer cmdbuf) const;
  94. u32 base_instance = 0;
  95. u32 num_instances = 0;
  96. u32 base_vertex = 0;
  97. u32 num_vertices = 0;
  98. bool is_indexed = 0;
  99. };
  100. void FlushWork();
  101. /// Setups geometry buffers and state.
  102. DrawParameters SetupGeometry(FixedPipelineState& fixed_state, BufferBindings& buffer_bindings,
  103. bool is_indexed, bool is_instanced);
  104. /// Setup descriptors in the graphics pipeline.
  105. void SetupShaderDescriptors(const std::array<Shader*, Maxwell::MaxShaderProgram>& shaders);
  106. void UpdateDynamicStates();
  107. void BeginTransformFeedback();
  108. void EndTransformFeedback();
  109. void SetupVertexArrays(BufferBindings& buffer_bindings);
  110. void SetupIndexBuffer(BufferBindings& buffer_bindings, DrawParameters& params, bool is_indexed);
  111. /// Setup constant buffers in the graphics pipeline.
  112. void SetupGraphicsConstBuffers(const ShaderEntries& entries, std::size_t stage);
  113. /// Setup global buffers in the graphics pipeline.
  114. void SetupGraphicsGlobalBuffers(const ShaderEntries& entries, std::size_t stage);
  115. /// Setup uniform texels in the graphics pipeline.
  116. void SetupGraphicsUniformTexels(const ShaderEntries& entries, std::size_t stage);
  117. /// Setup textures in the graphics pipeline.
  118. void SetupGraphicsTextures(const ShaderEntries& entries, std::size_t stage);
  119. /// Setup storage texels in the graphics pipeline.
  120. void SetupGraphicsStorageTexels(const ShaderEntries& entries, std::size_t stage);
  121. /// Setup images in the graphics pipeline.
  122. void SetupGraphicsImages(const ShaderEntries& entries, std::size_t stage);
  123. /// Setup constant buffers in the compute pipeline.
  124. void SetupComputeConstBuffers(const ShaderEntries& entries);
  125. /// Setup global buffers in the compute pipeline.
  126. void SetupComputeGlobalBuffers(const ShaderEntries& entries);
  127. /// Setup texel buffers in the compute pipeline.
  128. void SetupComputeUniformTexels(const ShaderEntries& entries);
  129. /// Setup textures in the compute pipeline.
  130. void SetupComputeTextures(const ShaderEntries& entries);
  131. /// Setup storage texels in the compute pipeline.
  132. void SetupComputeStorageTexels(const ShaderEntries& entries);
  133. /// Setup images in the compute pipeline.
  134. void SetupComputeImages(const ShaderEntries& entries);
  135. void SetupConstBuffer(const ConstBufferEntry& entry,
  136. const Tegra::Engines::ConstBufferInfo& buffer);
  137. void SetupGlobalBuffer(const GlobalBufferEntry& entry, GPUVAddr address);
  138. void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
  139. void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
  140. void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);
  141. void UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs);
  142. void UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs);
  143. void UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs);
  144. void UpdateCullMode(Tegra::Engines::Maxwell3D::Regs& regs);
  145. void UpdateDepthBoundsTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  146. void UpdateDepthTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  147. void UpdateDepthWriteEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  148. void UpdateDepthCompareOp(Tegra::Engines::Maxwell3D::Regs& regs);
  149. void UpdateFrontFace(Tegra::Engines::Maxwell3D::Regs& regs);
  150. void UpdateStencilOp(Tegra::Engines::Maxwell3D::Regs& regs);
  151. void UpdateStencilTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  152. size_t CalculateGraphicsStreamBufferSize(bool is_indexed) const;
  153. size_t CalculateComputeStreamBufferSize() const;
  154. size_t CalculateVertexArraysSize() const;
  155. size_t CalculateIndexBufferSize() const;
  156. size_t CalculateConstBufferSize(const ConstBufferEntry& entry,
  157. const Tegra::Engines::ConstBufferInfo& buffer) const;
  158. VkBuffer DefaultBuffer();
  159. Tegra::GPU& gpu;
  160. Tegra::MemoryManager& gpu_memory;
  161. Tegra::Engines::Maxwell3D& maxwell3d;
  162. Tegra::Engines::KeplerCompute& kepler_compute;
  163. VKScreenInfo& screen_info;
  164. const VKDevice& device;
  165. VKMemoryManager& memory_manager;
  166. StateTracker& state_tracker;
  167. VKScheduler& scheduler;
  168. VKStreamBuffer stream_buffer;
  169. VKStagingBufferPool staging_pool;
  170. VKDescriptorPool descriptor_pool;
  171. VKUpdateDescriptorQueue update_descriptor_queue;
  172. BlitImageHelper blit_image;
  173. QuadArrayPass quad_array_pass;
  174. QuadIndexedPass quad_indexed_pass;
  175. Uint8Pass uint8_pass;
  176. TextureCacheRuntime texture_cache_runtime;
  177. TextureCache texture_cache;
  178. VKPipelineCache pipeline_cache;
  179. VKBufferCache buffer_cache;
  180. VKQueryCache query_cache;
  181. VKFenceManager fence_manager;
  182. vk::Buffer default_buffer;
  183. VKMemoryCommit default_buffer_commit;
  184. vk::Event wfi_event;
  185. VideoCommon::Shader::AsyncShaders async_shaders;
  186. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  187. std::array<VideoCommon::ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  188. boost::container::static_vector<VkSampler, MAX_TEXTURES> sampler_handles;
  189. u32 draw_counter = 0;
  190. };
  191. } // namespace Vulkan