vk_rasterizer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_descriptor_pool.h"
  18. #include "video_core/renderer_vulkan/vk_fence_manager.h"
  19. #include "video_core/renderer_vulkan/vk_graphics_pipeline.h"
  20. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  21. #include "video_core/renderer_vulkan/vk_query_cache.h"
  22. #include "video_core/renderer_vulkan/vk_scheduler.h"
  23. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  24. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  25. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  26. #include "video_core/shader/async_shaders.h"
  27. #include "video_core/vulkan_common/vulkan_memory_allocator.h"
  28. #include "video_core/vulkan_common/vulkan_wrapper.h"
  29. namespace Core {
  30. class System;
  31. }
  32. namespace Core::Frontend {
  33. class EmuWindow;
  34. }
  35. namespace Tegra::Engines {
  36. class Maxwell3D;
  37. }
  38. namespace Vulkan {
  39. struct VKScreenInfo;
  40. class StateTracker;
  41. class RasterizerVulkan final : public VideoCore::RasterizerAccelerated {
  42. public:
  43. explicit RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  44. Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_,
  45. VKScreenInfo& screen_info_, const Device& device_,
  46. MemoryAllocator& memory_allocator_, StateTracker& state_tracker_,
  47. VKScheduler& scheduler_);
  48. ~RasterizerVulkan() override;
  49. void Draw(bool is_indexed, bool is_instanced) override;
  50. void Clear() override;
  51. void DispatchCompute(GPUVAddr code_addr) override;
  52. void ResetCounter(VideoCore::QueryType type) override;
  53. void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
  54. void BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr, u32 size) override;
  55. void DisableGraphicsUniformBuffer(size_t stage, u32 index) 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 ModifyGPUMemory(GPUVAddr addr, u64 size) override;
  64. void SignalSemaphore(GPUVAddr addr, u32 value) override;
  65. void SignalSyncPoint(u32 value) override;
  66. void SignalReference() override;
  67. void ReleaseFences() override;
  68. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  69. void WaitForIdle() override;
  70. void FragmentBarrier() override;
  71. void TiledCacheBarrier() override;
  72. void FlushCommands() override;
  73. void TickFrame() override;
  74. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  75. const Tegra::Engines::Fermi2D::Surface& dst,
  76. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  77. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  78. u32 pixel_stride) override;
  79. VideoCommon::Shader::AsyncShaders& GetAsyncShaders() {
  80. return async_shaders;
  81. }
  82. const VideoCommon::Shader::AsyncShaders& GetAsyncShaders() const {
  83. return async_shaders;
  84. }
  85. /// Maximum supported size that a constbuffer can have in bytes.
  86. static constexpr size_t MaxConstbufferSize = 0x10000;
  87. static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0,
  88. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  89. private:
  90. static constexpr size_t MAX_TEXTURES = 192;
  91. static constexpr size_t MAX_IMAGES = 48;
  92. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  93. static constexpr VkDeviceSize DEFAULT_BUFFER_SIZE = 4 * sizeof(float);
  94. void FlushWork();
  95. /// Setup descriptors in the graphics pipeline.
  96. void SetupShaderDescriptors(const std::array<Shader*, Maxwell::MaxShaderProgram>& shaders,
  97. bool is_indexed);
  98. void UpdateDynamicStates();
  99. void BeginTransformFeedback();
  100. void EndTransformFeedback();
  101. /// Setup uniform texels in the graphics pipeline.
  102. void SetupGraphicsUniformTexels(const ShaderEntries& entries, std::size_t stage);
  103. /// Setup textures in the graphics pipeline.
  104. void SetupGraphicsTextures(const ShaderEntries& entries, std::size_t stage);
  105. /// Setup storage texels in the graphics pipeline.
  106. void SetupGraphicsStorageTexels(const ShaderEntries& entries, std::size_t stage);
  107. /// Setup images in the graphics pipeline.
  108. void SetupGraphicsImages(const ShaderEntries& entries, std::size_t stage);
  109. /// Setup texel buffers in the compute pipeline.
  110. void SetupComputeUniformTexels(const ShaderEntries& entries);
  111. /// Setup textures in the compute pipeline.
  112. void SetupComputeTextures(const ShaderEntries& entries);
  113. /// Setup storage texels in the compute pipeline.
  114. void SetupComputeStorageTexels(const ShaderEntries& entries);
  115. /// Setup images in the compute pipeline.
  116. void SetupComputeImages(const ShaderEntries& entries);
  117. void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
  118. void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
  119. void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);
  120. void UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs);
  121. void UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs);
  122. void UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs);
  123. void UpdateCullMode(Tegra::Engines::Maxwell3D::Regs& regs);
  124. void UpdateDepthBoundsTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  125. void UpdateDepthTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  126. void UpdateDepthWriteEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  127. void UpdateDepthCompareOp(Tegra::Engines::Maxwell3D::Regs& regs);
  128. void UpdateFrontFace(Tegra::Engines::Maxwell3D::Regs& regs);
  129. void UpdateStencilOp(Tegra::Engines::Maxwell3D::Regs& regs);
  130. void UpdateStencilTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  131. Tegra::GPU& gpu;
  132. Tegra::MemoryManager& gpu_memory;
  133. Tegra::Engines::Maxwell3D& maxwell3d;
  134. Tegra::Engines::KeplerCompute& kepler_compute;
  135. VKScreenInfo& screen_info;
  136. const Device& device;
  137. MemoryAllocator& memory_allocator;
  138. StateTracker& state_tracker;
  139. VKScheduler& scheduler;
  140. StagingBufferPool staging_pool;
  141. VKDescriptorPool descriptor_pool;
  142. VKUpdateDescriptorQueue update_descriptor_queue;
  143. BlitImageHelper blit_image;
  144. ASTCDecoderPass astc_decoder_pass;
  145. GraphicsPipelineCacheKey graphics_key;
  146. TextureCacheRuntime texture_cache_runtime;
  147. TextureCache texture_cache;
  148. BufferCacheRuntime buffer_cache_runtime;
  149. BufferCache buffer_cache;
  150. VKPipelineCache pipeline_cache;
  151. VKQueryCache query_cache;
  152. VKFenceManager fence_manager;
  153. vk::Event wfi_event;
  154. VideoCommon::Shader::AsyncShaders async_shaders;
  155. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  156. std::array<VideoCommon::ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  157. boost::container::static_vector<VkSampler, MAX_TEXTURES> sampler_handles;
  158. u32 draw_counter = 0;
  159. };
  160. } // namespace Vulkan