vk_rasterizer.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <boost/container/static_vector.hpp>
  6. #include "common/common_types.h"
  7. #include "video_core/control/channel_state_cache.h"
  8. #include "video_core/engines/maxwell_dma.h"
  9. #include "video_core/rasterizer_accelerated.h"
  10. #include "video_core/rasterizer_interface.h"
  11. #include "video_core/renderer_vulkan/blit_image.h"
  12. #include "video_core/renderer_vulkan/vk_buffer_cache.h"
  13. #include "video_core/renderer_vulkan/vk_descriptor_pool.h"
  14. #include "video_core/renderer_vulkan/vk_fence_manager.h"
  15. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  16. #include "video_core/renderer_vulkan/vk_query_cache.h"
  17. #include "video_core/renderer_vulkan/vk_render_pass_cache.h"
  18. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  19. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  20. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  21. #include "video_core/vulkan_common/vulkan_memory_allocator.h"
  22. #include "video_core/vulkan_common/vulkan_wrapper.h"
  23. namespace Core {
  24. class System;
  25. }
  26. namespace Core::Frontend {
  27. class EmuWindow;
  28. }
  29. namespace Tegra::Engines {
  30. class Maxwell3D;
  31. }
  32. namespace Vulkan {
  33. struct ScreenInfo;
  34. class StateTracker;
  35. class AccelerateDMA : public Tegra::Engines::AccelerateDMAInterface {
  36. public:
  37. explicit AccelerateDMA(BufferCache& buffer_cache, TextureCache& texture_cache,
  38. Scheduler& scheduler);
  39. bool BufferCopy(GPUVAddr start_address, GPUVAddr end_address, u64 amount) override;
  40. bool BufferClear(GPUVAddr src_address, u64 amount, u32 value) override;
  41. bool ImageToBuffer(const Tegra::DMA::ImageCopy& copy_info, const Tegra::DMA::ImageOperand& src,
  42. const Tegra::DMA::BufferOperand& dst) override;
  43. bool BufferToImage(const Tegra::DMA::ImageCopy& copy_info, const Tegra::DMA::BufferOperand& src,
  44. const Tegra::DMA::ImageOperand& dst) override;
  45. private:
  46. template <bool IS_IMAGE_UPLOAD>
  47. bool DmaBufferImageCopy(const Tegra::DMA::ImageCopy& copy_info,
  48. const Tegra::DMA::BufferOperand& src,
  49. const Tegra::DMA::ImageOperand& dst);
  50. BufferCache& buffer_cache;
  51. TextureCache& texture_cache;
  52. Scheduler& scheduler;
  53. };
  54. class RasterizerVulkan final : public VideoCore::RasterizerAccelerated,
  55. protected VideoCommon::ChannelSetupCaches<VideoCommon::ChannelInfo> {
  56. public:
  57. explicit RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  58. Core::Memory::Memory& cpu_memory_, ScreenInfo& screen_info_,
  59. const Device& device_, MemoryAllocator& memory_allocator_,
  60. StateTracker& state_tracker_, Scheduler& scheduler_);
  61. ~RasterizerVulkan() override;
  62. void Draw(bool is_indexed, u32 instance_count) override;
  63. void DrawIndirect() override;
  64. void DrawTexture() override;
  65. void Clear(u32 layer_count) override;
  66. void DispatchCompute() override;
  67. void ResetCounter(VideoCore::QueryType type) override;
  68. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  69. void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size) override;
  70. void DisableGraphicsUniformBuffer(size_t stage, u32 index) override;
  71. void FlushAll() override;
  72. void FlushRegion(VAddr addr, u64 size,
  73. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  74. bool MustFlushRegion(VAddr addr, u64 size,
  75. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  76. VideoCore::RasterizerDownloadArea GetFlushArea(VAddr addr, u64 size) override;
  77. void InvalidateRegion(VAddr addr, u64 size,
  78. VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  79. void InnerInvalidation(std::span<const std::pair<VAddr, std::size_t>> sequences) override;
  80. void OnCacheInvalidation(VAddr addr, u64 size) override;
  81. bool OnCPUWrite(VAddr addr, u64 size) override;
  82. void InvalidateGPUCache() override;
  83. void UnmapMemory(VAddr addr, u64 size) override;
  84. void ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) override;
  85. void SignalFence(std::function<void()>&& func) override;
  86. void SyncOperation(std::function<void()>&& func) override;
  87. void SignalSyncPoint(u32 value) override;
  88. void SignalReference() override;
  89. void ReleaseFences() override;
  90. void FlushAndInvalidateRegion(
  91. VAddr addr, u64 size, VideoCommon::CacheType which = VideoCommon::CacheType::All) override;
  92. void WaitForIdle() override;
  93. void FragmentBarrier() override;
  94. void TiledCacheBarrier() override;
  95. void FlushCommands() override;
  96. void TickFrame() override;
  97. bool AccelerateConditionalRendering() override;
  98. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  99. const Tegra::Engines::Fermi2D::Surface& dst,
  100. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  101. Tegra::Engines::AccelerateDMAInterface& AccessAccelerateDMA() override;
  102. void AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  103. std::span<const u8> memory) override;
  104. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  105. u32 pixel_stride) override;
  106. void LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  107. const VideoCore::DiskResourceLoadCallback& callback) override;
  108. void InitializeChannel(Tegra::Control::ChannelState& channel) override;
  109. void BindChannel(Tegra::Control::ChannelState& channel) override;
  110. void ReleaseChannel(s32 channel_id) override;
  111. private:
  112. static constexpr size_t MAX_TEXTURES = 192;
  113. static constexpr size_t MAX_IMAGES = 48;
  114. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  115. static constexpr VkDeviceSize DEFAULT_BUFFER_SIZE = 4 * sizeof(float);
  116. template <typename Func>
  117. void PrepareDraw(bool is_indexed, Func&&);
  118. void FlushWork();
  119. void UpdateDynamicStates();
  120. void BeginTransformFeedback();
  121. void EndTransformFeedback();
  122. void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
  123. void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
  124. void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);
  125. void UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs);
  126. void UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs);
  127. void UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs);
  128. void UpdateLineWidth(Tegra::Engines::Maxwell3D::Regs& regs);
  129. void UpdateCullMode(Tegra::Engines::Maxwell3D::Regs& regs);
  130. void UpdateDepthBoundsTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  131. void UpdateDepthTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  132. void UpdateDepthWriteEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  133. void UpdateDepthCompareOp(Tegra::Engines::Maxwell3D::Regs& regs);
  134. void UpdatePrimitiveRestartEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  135. void UpdateRasterizerDiscardEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  136. void UpdateDepthBiasEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  137. void UpdateLogicOpEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  138. void UpdateDepthClampEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  139. void UpdateFrontFace(Tegra::Engines::Maxwell3D::Regs& regs);
  140. void UpdateStencilOp(Tegra::Engines::Maxwell3D::Regs& regs);
  141. void UpdateStencilTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  142. void UpdateLogicOp(Tegra::Engines::Maxwell3D::Regs& regs);
  143. void UpdateBlending(Tegra::Engines::Maxwell3D::Regs& regs);
  144. void UpdateVertexInput(Tegra::Engines::Maxwell3D::Regs& regs);
  145. Tegra::GPU& gpu;
  146. ScreenInfo& screen_info;
  147. const Device& device;
  148. MemoryAllocator& memory_allocator;
  149. StateTracker& state_tracker;
  150. Scheduler& scheduler;
  151. StagingBufferPool staging_pool;
  152. DescriptorPool descriptor_pool;
  153. GuestDescriptorQueue guest_descriptor_queue;
  154. ComputePassDescriptorQueue compute_pass_descriptor_queue;
  155. BlitImageHelper blit_image;
  156. RenderPassCache render_pass_cache;
  157. TextureCacheRuntime texture_cache_runtime;
  158. TextureCache texture_cache;
  159. BufferCacheRuntime buffer_cache_runtime;
  160. BufferCache buffer_cache;
  161. PipelineCache pipeline_cache;
  162. QueryCache query_cache;
  163. AccelerateDMA accelerate_dma;
  164. FenceManager fence_manager;
  165. vk::Event wfi_event;
  166. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  167. std::array<VideoCommon::ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  168. boost::container::static_vector<VkSampler, MAX_TEXTURES> sampler_handles;
  169. u32 draw_counter = 0;
  170. };
  171. } // namespace Vulkan