VertexManagerBase.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2010 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <memory>
  5. #include <vector>
  6. #include "Common/BitSet.h"
  7. #include "Common/CommonTypes.h"
  8. #include "Common/MathUtil.h"
  9. #include "VideoCommon/CPUCull.h"
  10. #include "VideoCommon/IndexGenerator.h"
  11. #include "VideoCommon/RenderState.h"
  12. #include "VideoCommon/ShaderCache.h"
  13. #include "VideoCommon/VideoEvents.h"
  14. struct CustomPixelShaderContents;
  15. class CustomShaderCache;
  16. class DataReader;
  17. class GeometryShaderManager;
  18. class NativeVertexFormat;
  19. class PixelShaderManager;
  20. class PointerWrap;
  21. struct PortableVertexDeclaration;
  22. struct Slope
  23. {
  24. float dfdx;
  25. float dfdy;
  26. float f0;
  27. bool dirty;
  28. };
  29. // View format of the input data to the texture decoding shader.
  30. enum TexelBufferFormat : u32
  31. {
  32. TEXEL_BUFFER_FORMAT_R8_UINT,
  33. TEXEL_BUFFER_FORMAT_R16_UINT,
  34. TEXEL_BUFFER_FORMAT_RGBA8_UINT,
  35. TEXEL_BUFFER_FORMAT_R32G32_UINT,
  36. NUM_TEXEL_BUFFER_FORMATS
  37. };
  38. namespace OpcodeDecoder
  39. {
  40. enum class Primitive : u8;
  41. }
  42. class VertexManagerBase
  43. {
  44. private:
  45. // 3 pos
  46. static constexpr u32 SMALLEST_POSSIBLE_VERTEX = sizeof(float) * 3;
  47. // 3 pos, 3*3 normal, 2*u32 color, 8*4 tex, 1 posMat
  48. static constexpr u32 LARGEST_POSSIBLE_VERTEX = sizeof(float) * 45 + sizeof(u32) * 2;
  49. static constexpr u32 MAX_PRIMITIVES_PER_COMMAND = 65535;
  50. // Used for 16:9 anamorphic widescreen heuristic.
  51. struct FlushStatistics
  52. {
  53. struct ProjectionCounts
  54. {
  55. size_t normal_flush_count;
  56. size_t anamorphic_flush_count;
  57. size_t other_flush_count;
  58. size_t normal_vertex_count;
  59. size_t anamorphic_vertex_count;
  60. size_t other_vertex_count;
  61. size_t GetTotalFlushCount() const
  62. {
  63. return normal_flush_count + anamorphic_flush_count + other_flush_count;
  64. }
  65. size_t GetTotalVertexCount() const
  66. {
  67. return normal_vertex_count + anamorphic_vertex_count + other_vertex_count;
  68. }
  69. MathUtil::RunningMean<float> average_ratio;
  70. };
  71. ProjectionCounts perspective;
  72. ProjectionCounts orthographic;
  73. };
  74. public:
  75. static constexpr u32 MAXVBUFFERSIZE =
  76. MathUtil::NextPowerOf2(MAX_PRIMITIVES_PER_COMMAND * LARGEST_POSSIBLE_VERTEX);
  77. // We may convert triangle-fans to triangle-lists, almost 3x as many indices.
  78. static constexpr u32 MAXIBUFFERSIZE = MathUtil::NextPowerOf2(MAX_PRIMITIVES_PER_COMMAND * 3);
  79. // Streaming buffer sizes.
  80. // Texel buffer will fit the maximum size of an encoded GX texture. 1024x1024, RGBA8 = 4MB.
  81. static constexpr u32 VERTEX_STREAM_BUFFER_SIZE = 48 * 1024 * 1024;
  82. static constexpr u32 INDEX_STREAM_BUFFER_SIZE = 8 * 1024 * 1024;
  83. static constexpr u32 UNIFORM_STREAM_BUFFER_SIZE = 64 * 1024 * 1024;
  84. static constexpr u32 TEXEL_STREAM_BUFFER_SIZE = 16 * 1024 * 1024;
  85. VertexManagerBase();
  86. virtual ~VertexManagerBase();
  87. virtual bool Initialize();
  88. PrimitiveType GetCurrentPrimitiveType() const { return m_current_primitive_type; }
  89. void AddIndices(OpcodeDecoder::Primitive primitive, u32 num_vertices);
  90. bool AreAllVerticesCulled(VertexLoaderBase* loader, OpcodeDecoder::Primitive primitive,
  91. const u8* src, u32 count);
  92. virtual DataReader PrepareForAdditionalData(OpcodeDecoder::Primitive primitive, u32 count,
  93. u32 stride, bool cullall);
  94. /// Switch cullall off after a call to PrepareForAdditionalData with cullall true
  95. /// Expects that you will add a nonzero number of primitives before the next flush
  96. /// Returns whether cullall was changed (false if cullall was already off)
  97. DataReader DisableCullAll(u32 stride);
  98. void FlushData(u32 count, u32 stride);
  99. void Flush();
  100. bool HasSendableVertices() const { return !m_is_flushed && !m_cull_all; }
  101. void DoState(PointerWrap& p);
  102. FlushStatistics ResetFlushAspectRatioCount();
  103. // State setters, called from register update functions.
  104. void SetRasterizationStateChanged() { m_rasterization_state_changed = true; }
  105. void SetDepthStateChanged() { m_depth_state_changed = true; }
  106. void SetBlendingStateChanged() { m_blending_state_changed = true; }
  107. void InvalidatePipelineObject()
  108. {
  109. m_current_pipeline_object = nullptr;
  110. m_pipeline_config_changed = true;
  111. }
  112. void NotifyCustomShaderCacheOfHostChange(const ShaderHostConfig& host_config);
  113. // Utility pipeline drawing (e.g. EFB copies, post-processing, UI).
  114. virtual void UploadUtilityUniforms(const void* uniforms, u32 uniforms_size);
  115. void UploadUtilityVertices(const void* vertices, u32 vertex_stride, u32 num_vertices,
  116. const u16* indices, u32 num_indices, u32* out_base_vertex,
  117. u32* out_base_index);
  118. // Determine how many bytes there are in each element of the texel buffer.
  119. // Needed for alignment and stride calculations.
  120. static u32 GetTexelBufferElementSize(TexelBufferFormat buffer_format);
  121. // Texel buffer, used for palette conversion.
  122. virtual bool UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
  123. u32* out_offset);
  124. // The second set of parameters uploads a second blob in the same buffer, used for GPU texture
  125. // decoding for palette textures, as both the texture data and palette must be uploaded.
  126. virtual bool UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
  127. u32* out_offset, const void* palette_data, u32 palette_size,
  128. TexelBufferFormat palette_format, u32* out_palette_offset);
  129. // Call if active config changes
  130. void OnConfigChange();
  131. // CPU access tracking - call after a draw call is made.
  132. void OnDraw();
  133. // Call after CPU access is requested.
  134. void OnCPUEFBAccess();
  135. // Call after an EFB copy to RAM. If true, the current command buffer should be executed.
  136. void OnEFBCopyToRAM();
  137. // Call at the end of a frame.
  138. void OnEndFrame();
  139. protected:
  140. // When utility uniforms are used, the GX uniforms need to be re-written afterwards.
  141. static void InvalidateConstants();
  142. // Prepares the buffer for the next batch of vertices.
  143. virtual void ResetBuffer(u32 vertex_stride);
  144. // Commits/uploads the current batch of vertices.
  145. virtual void CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices,
  146. u32* out_base_vertex, u32* out_base_index);
  147. // Uploads uniform buffers for GX draws.
  148. virtual void UploadUniforms();
  149. // Issues the draw call for the current batch in the backend.
  150. virtual void DrawCurrentBatch(u32 base_index, u32 num_indices, u32 base_vertex);
  151. u32 GetRemainingSize() const;
  152. u32 GetRemainingIndices(OpcodeDecoder::Primitive primitive) const;
  153. void CalculateZSlope(NativeVertexFormat* format);
  154. void CalculateNormals(NativeVertexFormat* format);
  155. BitSet32 UsedTextures() const;
  156. u8* m_cur_buffer_pointer = nullptr;
  157. u8* m_base_buffer_pointer = nullptr;
  158. u8* m_end_buffer_pointer = nullptr;
  159. // Alternative buffers in CPU memory for primitives we are going to discard.
  160. std::vector<u8> m_cpu_vertex_buffer;
  161. std::vector<u16> m_cpu_index_buffer;
  162. Slope m_zslope = {};
  163. VideoCommon::GXPipelineUid m_current_pipeline_config;
  164. VideoCommon::GXUberPipelineUid m_current_uber_pipeline_config;
  165. const AbstractPipeline* m_current_pipeline_object = nullptr;
  166. PrimitiveType m_current_primitive_type = PrimitiveType::Points;
  167. bool m_pipeline_config_changed = true;
  168. bool m_rasterization_state_changed = true;
  169. bool m_depth_state_changed = true;
  170. bool m_blending_state_changed = true;
  171. bool m_cull_all = false;
  172. IndexGenerator m_index_generator;
  173. CPUCull m_cpu_cull;
  174. private:
  175. // Minimum number of draws per command buffer when attempting to preempt a readback operation.
  176. static constexpr u32 MINIMUM_DRAW_CALLS_PER_COMMAND_BUFFER_FOR_READBACK = 10;
  177. void RenderDrawCall(PixelShaderManager& pixel_shader_manager,
  178. GeometryShaderManager& geometry_shader_manager,
  179. const CustomPixelShaderContents& custom_pixel_shader_contents,
  180. std::span<u8> custom_pixel_shader_uniforms, PrimitiveType primitive_type,
  181. const AbstractPipeline* current_pipeline);
  182. void UpdatePipelineConfig();
  183. void UpdatePipelineObject();
  184. const AbstractPipeline*
  185. GetCustomPipeline(const CustomPixelShaderContents& custom_pixel_shader_contents,
  186. const VideoCommon::GXPipelineUid& current_pipeline_config,
  187. const VideoCommon::GXUberPipelineUid& current_uber_pipeline_confi,
  188. const AbstractPipeline* current_pipeline) const;
  189. bool m_is_flushed = true;
  190. FlushStatistics m_flush_statistics = {};
  191. // CPU access tracking
  192. u32 m_draw_counter = 0;
  193. u32 m_last_efb_copy_draw_counter = 0;
  194. bool m_unflushed_efb_copy = false;
  195. std::vector<u32> m_cpu_accesses_this_frame;
  196. std::vector<u32> m_scheduled_command_buffer_kicks;
  197. bool m_allow_background_execution = true;
  198. std::unique_ptr<CustomShaderCache> m_custom_shader_cache;
  199. u64 m_ticks_elapsed = 0;
  200. Common::EventHook m_frame_end_event;
  201. Common::EventHook m_after_present_event;
  202. };
  203. extern std::unique_ptr<VertexManagerBase> g_vertex_manager;