vk_rasterizer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ReleaseFences() override;
  67. void FlushAndInvalidateRegion(VAddr addr, u64 size) override;
  68. void WaitForIdle() override;
  69. void FragmentBarrier() override;
  70. void TiledCacheBarrier() override;
  71. void FlushCommands() override;
  72. void TickFrame() override;
  73. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  74. const Tegra::Engines::Fermi2D::Surface& dst,
  75. const Tegra::Engines::Fermi2D::Config& copy_config) override;
  76. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  77. u32 pixel_stride) override;
  78. VideoCommon::Shader::AsyncShaders& GetAsyncShaders() {
  79. return async_shaders;
  80. }
  81. const VideoCommon::Shader::AsyncShaders& GetAsyncShaders() const {
  82. return async_shaders;
  83. }
  84. /// Maximum supported size that a constbuffer can have in bytes.
  85. static constexpr size_t MaxConstbufferSize = 0x10000;
  86. static_assert(MaxConstbufferSize % (4 * sizeof(float)) == 0,
  87. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  88. private:
  89. static constexpr size_t MAX_TEXTURES = 192;
  90. static constexpr size_t MAX_IMAGES = 48;
  91. static constexpr size_t MAX_IMAGE_VIEWS = MAX_TEXTURES + MAX_IMAGES;
  92. static constexpr VkDeviceSize DEFAULT_BUFFER_SIZE = 4 * sizeof(float);
  93. void FlushWork();
  94. /// Setup descriptors in the graphics pipeline.
  95. void SetupShaderDescriptors(const std::array<Shader*, Maxwell::MaxShaderProgram>& shaders,
  96. bool is_indexed);
  97. void UpdateDynamicStates();
  98. void BeginTransformFeedback();
  99. void EndTransformFeedback();
  100. /// Setup uniform texels in the graphics pipeline.
  101. void SetupGraphicsUniformTexels(const ShaderEntries& entries, std::size_t stage);
  102. /// Setup textures in the graphics pipeline.
  103. void SetupGraphicsTextures(const ShaderEntries& entries, std::size_t stage);
  104. /// Setup storage texels in the graphics pipeline.
  105. void SetupGraphicsStorageTexels(const ShaderEntries& entries, std::size_t stage);
  106. /// Setup images in the graphics pipeline.
  107. void SetupGraphicsImages(const ShaderEntries& entries, std::size_t stage);
  108. /// Setup texel buffers in the compute pipeline.
  109. void SetupComputeUniformTexels(const ShaderEntries& entries);
  110. /// Setup textures in the compute pipeline.
  111. void SetupComputeTextures(const ShaderEntries& entries);
  112. /// Setup storage texels in the compute pipeline.
  113. void SetupComputeStorageTexels(const ShaderEntries& entries);
  114. /// Setup images in the compute pipeline.
  115. void SetupComputeImages(const ShaderEntries& entries);
  116. void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
  117. void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
  118. void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);
  119. void UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs);
  120. void UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs);
  121. void UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs);
  122. void UpdateCullMode(Tegra::Engines::Maxwell3D::Regs& regs);
  123. void UpdateDepthBoundsTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  124. void UpdateDepthTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  125. void UpdateDepthWriteEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  126. void UpdateDepthCompareOp(Tegra::Engines::Maxwell3D::Regs& regs);
  127. void UpdateFrontFace(Tegra::Engines::Maxwell3D::Regs& regs);
  128. void UpdateStencilOp(Tegra::Engines::Maxwell3D::Regs& regs);
  129. void UpdateStencilTestEnable(Tegra::Engines::Maxwell3D::Regs& regs);
  130. Tegra::GPU& gpu;
  131. Tegra::MemoryManager& gpu_memory;
  132. Tegra::Engines::Maxwell3D& maxwell3d;
  133. Tegra::Engines::KeplerCompute& kepler_compute;
  134. VKScreenInfo& screen_info;
  135. const Device& device;
  136. MemoryAllocator& memory_allocator;
  137. StateTracker& state_tracker;
  138. VKScheduler& scheduler;
  139. StagingBufferPool staging_pool;
  140. VKDescriptorPool descriptor_pool;
  141. VKUpdateDescriptorQueue update_descriptor_queue;
  142. BlitImageHelper blit_image;
  143. ASTCDecoderPass astc_decoder_pass;
  144. GraphicsPipelineCacheKey graphics_key;
  145. TextureCacheRuntime texture_cache_runtime;
  146. TextureCache texture_cache;
  147. BufferCacheRuntime buffer_cache_runtime;
  148. BufferCache buffer_cache;
  149. VKPipelineCache pipeline_cache;
  150. VKQueryCache query_cache;
  151. VKFenceManager fence_manager;
  152. vk::Event wfi_event;
  153. VideoCommon::Shader::AsyncShaders async_shaders;
  154. boost::container::static_vector<u32, MAX_IMAGE_VIEWS> image_view_indices;
  155. std::array<VideoCommon::ImageViewId, MAX_IMAGE_VIEWS> image_view_ids;
  156. boost::container::static_vector<VkSampler, MAX_TEXTURES> sampler_handles;
  157. u32 draw_counter = 0;
  158. };
  159. } // namespace Vulkan