Debian Codegraph @

Source/Core/VideoCommon/VertexManagerBase.h

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