Debian Codegraph @

Source/Core/VideoCommon/FramebufferManager.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 <array>
7 #include <memory>
8 #include <optional>
9 #include <tuple>
10
11 #include "Common/CommonTypes.h"
12 #include "Common/EnumFormatter.h"
13
14 #include "VideoCommon/AbstractFramebuffer.h"
15 #include "VideoCommon/AbstractPipeline.h"
16 #include "VideoCommon/AbstractStagingTexture.h"
17 #include "VideoCommon/AbstractTexture.h"
18 #include "VideoCommon/RenderState.h"
19 #include "VideoCommon/TextureConfig.h"
20 #include "VideoCommon/VideoEvents.h"
21
22 class NativeVertexFormat;
23 class PointerWrap;
24
25 enum class EFBReinterpretType
26 {
27 RGB8ToRGB565 = 0,
28 RGB8ToRGBA6 = 1,
29 RGBA6ToRGB8 = 2,
30 RGBA6ToRGB565 = 3,
31 RGB565ToRGB8 = 4,
32 RGB565ToRGBA6 = 5
33 };
34 constexpr u32 NUM_EFB_REINTERPRET_TYPES = 6;
35 template <>
36 struct fmt::formatter<EFBReinterpretType> : EnumFormatter<EFBReinterpretType::RGB565ToRGBA6>
37 {
38 static constexpr array_type names = {"RGB8 to RGB565", "RGB8 to RGBA6", "RGBA6 to RGB8",
39 "RGB6 to RGB565", "RGB565 to RGB8", "RGB565 to RGBA6"};
40 constexpr formatter() : EnumFormatter(names) {}
41 };
42
43 inline bool AddressRangesOverlap(u32 aLower, u32 aUpper, u32 bLower, u32 bUpper)
44 {
45 return !((aLower >= bUpper) || (bLower >= aUpper));
46 }
47
48 class FramebufferManager final
49 {
50 public:
51 FramebufferManager();
52 ~FramebufferManager();
53
54 // Does not require the framebuffer to be created. Slower than direct queries.
55 static AbstractTextureFormat GetEFBColorFormat();
56 static AbstractTextureFormat GetEFBDepthFormat();
57 static AbstractTextureFormat GetEFBDepthCopyFormat();
58 static TextureConfig GetEFBColorTextureConfig(u32 width, u32 height);
59 static TextureConfig GetEFBDepthTextureConfig(u32 width, u32 height);
60
61 // Accessors.
62 AbstractTexture* GetEFBColorTexture() const { return m_efb_color_texture.get(); }
63 AbstractTexture* GetEFBDepthTexture() const { return m_efb_depth_texture.get(); }
64 AbstractFramebuffer* GetEFBFramebuffer() const { return m_efb_framebuffer.get(); }
65 u32 GetEFBWidth() const { return m_efb_color_texture->GetWidth(); }
66 u32 GetEFBHeight() const { return m_efb_color_texture->GetHeight(); }
67 u32 GetEFBLayers() const { return m_efb_color_texture->GetLayers(); }
68 u32 GetEFBSamples() const { return m_efb_color_texture->GetSamples(); }
69 bool IsEFBMultisampled() const { return m_efb_color_texture->IsMultisampled(); }
70 bool IsEFBStereo() const { return m_efb_color_texture->GetLayers() > 1; }
71 FramebufferState GetEFBFramebufferState() const;
72
73 // EFB coordinate conversion functions
74 // Use this to convert a whole native EFB rect to backbuffer coordinates
75 MathUtil::Rectangle<int> ConvertEFBRectangle(const MathUtil::Rectangle<int>& rc) const;
76
77 unsigned int GetEFBScale() const;
78
79 // Use this to upscale native EFB coordinates to IDEAL internal resolution
80 int EFBToScaledX(int x) const;
81 int EFBToScaledY(int y) const;
82
83 // Floating point versions of the above - only use them if really necessary
84 float EFBToScaledXf(float x) const;
85 float EFBToScaledYf(float y) const;
86
87 // First-time setup.
88 bool Initialize(int efb_scale);
89
90 // Recreate EFB framebuffers, call when the EFB size (IR) changes.
91 void RecreateEFBFramebuffer(int efb_scale);
92
93 // Recompile shaders, use when MSAA mode changes.
94 void RecompileShaders();
95
96 // This is virtual, because D3D has both normalized and integer framebuffers.
97 void BindEFBFramebuffer();
98
99 // Resolve color/depth textures to a non-msaa texture, and return it.
100 AbstractTexture* ResolveEFBColorTexture(const MathUtil::Rectangle<int>& region);
101 AbstractTexture* ResolveEFBDepthTexture(const MathUtil::Rectangle<int>& region,
102 bool force_r32f = false);
103
104 // Reinterpret pixel format of EFB color texture.
105 // Assumes no render pass is currently in progress.
106 // Swaps EFB framebuffers, so re-bind afterwards.
107 bool ReinterpretPixelData(EFBReinterpretType convtype);
108 PixelFormat GetPrevPixelFormat() const { return m_prev_efb_format; }
109 void StorePixelFormat(PixelFormat new_format) { m_prev_efb_format = new_format; }
110
111 // Clears the EFB using shaders.
112 void ClearEFB(const MathUtil::Rectangle<int>& rc, bool clear_color, bool clear_alpha,
113 bool clear_z, u32 color, u32 z, PixelFormat pixel_format);
114
115 AbstractPipeline* GetClearPipeline(bool clear_color, bool clear_alpha, bool clear_z) const;
116
117 // Reads a framebuffer value back from the GPU. This may block if the cache is not current.
118 u32 PeekEFBColor(u32 x, u32 y);
119 float PeekEFBDepth(u32 x, u32 y);
120 void SetEFBCacheTileSize(u32 size);
121 void InvalidatePeekCache(bool forced = true);
122 void RefreshPeekCache();
123 void FlagPeekCacheAsOutOfDate();
124 void EndOfFrame();
125
126 // Writes a value to the framebuffer. This will never block, and writes will be batched.
127 void PokeEFBColor(u32 x, u32 y, u32 color);
128 void PokeEFBDepth(u32 x, u32 y, float depth);
129 void FlushEFBPokes();
130
131 // Save state load/save.
132 void DoState(PointerWrap& p);
133
134 protected:
135 struct EFBPokeVertex
136 {
137 float position[4];
138 u32 color;
139 };
140 static_assert(std::is_standard_layout<EFBPokeVertex>::value, "EFBPokeVertex is standard-layout");
141
142 struct EFBCacheTile
143 {
144 bool present;
145 u8 frame_access_mask;
146 };
147
148 // EFB cache - for CPU EFB access
149 // Tiles are ordered left-to-right, then top-to-bottom
150 struct EFBCacheData
151 {
152 std::unique_ptr<AbstractTexture> texture;
153 std::unique_ptr<AbstractFramebuffer> framebuffer;
154 std::unique_ptr<AbstractStagingTexture> readback_texture;
155 std::unique_ptr<AbstractPipeline> copy_pipeline;
156 std::vector<EFBCacheTile> tiles;
157 bool out_of_date;
158 bool has_active_tiles;
159 bool needs_refresh;
160 bool needs_flush;
161 };
162
163 bool CreateEFBFramebuffer(int efb_scale);
164 void DestroyEFBFramebuffer();
165
166 bool CompileConversionPipelines();
167 void DestroyConversionPipelines();
168
169 bool CompileReadbackPipelines();
170 void DestroyReadbackPipelines();
171
172 bool CreateReadbackFramebuffer();
173 void DestroyReadbackFramebuffer();
174
175 bool CompileClearPipelines();
176 void DestroyClearPipelines();
177
178 bool CompilePokePipelines();
179 void DestroyPokePipelines();
180
181 bool IsUsingTiledEFBCache() const;
182 bool IsEFBCacheTilePresent(bool depth, u32 x, u32 y, u32* tile_index) const;
183 MathUtil::Rectangle<int> GetEFBCacheTileRect(u32 tile_index) const;
184 void PopulateEFBCache(bool depth, u32 tile_index, bool async = false);
185
186 void CreatePokeVertices(std::vector<EFBPokeVertex>* destination_list, u32 x, u32 y, float z,
187 u32 color);
188
189 void DrawPokeVertices(const EFBPokeVertex* vertices, u32 vertex_count,
190 const AbstractPipeline* pipeline);
191
192 std::tuple<u32, u32> CalculateTargetSize(int efb_scale);
193
194 void DoLoadState(PointerWrap& p);
195 void DoSaveState(PointerWrap& p);
196
197 float m_efb_scale = 1.0f;
198 PixelFormat m_prev_efb_format;
199
200 std::unique_ptr<AbstractTexture> m_efb_color_texture;
201 std::unique_ptr<AbstractTexture> m_efb_convert_color_texture;
202 std::unique_ptr<AbstractTexture> m_efb_depth_texture;
203 std::unique_ptr<AbstractTexture> m_efb_resolve_color_texture;
204 std::unique_ptr<AbstractTexture> m_efb_depth_resolve_texture;
205
206 std::unique_ptr<AbstractFramebuffer> m_efb_framebuffer;
207 std::unique_ptr<AbstractFramebuffer> m_efb_convert_framebuffer;
208 std::unique_ptr<AbstractFramebuffer> m_efb_color_resolve_framebuffer;
209 std::unique_ptr<AbstractFramebuffer> m_efb_depth_resolve_framebuffer;
210 std::unique_ptr<AbstractPipeline> m_efb_color_resolve_pipeline;
211 std::unique_ptr<AbstractPipeline> m_efb_depth_resolve_pipeline;
212
213 // Pipeline for restoring the contents of the EFB from a save state
214 std::unique_ptr<AbstractPipeline> m_efb_restore_pipeline;
215
216 // Format conversion shaders
217 std::array<std::unique_ptr<AbstractPipeline>, 6> m_format_conversion_pipelines;
218
219 // EFB cache - for CPU EFB access (EFB peeks/pokes), not for EFB copies
220
221 // Width and height of a tile in pixels at 1x IR. 0 indicates non-tiled, in which case a single
222 // tile is used for the entire EFB.
223 // Note that as EFB peeks and pokes are a CPU feature, they always operate at 1x IR.
224 u32 m_efb_cache_tile_size = 0;
225 // Number of tiles that make up a row in m_efb_color_cache.tiles / m_efb_depth_cache.tiles.
226 u32 m_efb_cache_tile_row_stride = 1;
227 EFBCacheData m_efb_color_cache = {};
228 EFBCacheData m_efb_depth_cache = {};
229
230 // EFB clear pipelines
231 // Indexed by [color_write_enabled][alpha_write_enabled][depth_write_enabled]
232 std::array<std::array<std::array<std::unique_ptr<AbstractPipeline>, 2>, 2>, 2> m_clear_pipelines;
233
234 // EFB poke drawing setup
235 std::unique_ptr<NativeVertexFormat> m_poke_vertex_format;
236 std::unique_ptr<AbstractPipeline> m_color_poke_pipeline;
237 std::unique_ptr<AbstractPipeline> m_depth_poke_pipeline;
238 std::vector<EFBPokeVertex> m_color_poke_vertices;
239 std::vector<EFBPokeVertex> m_depth_poke_vertices;
240
241 Common::EventHook m_end_of_frame_event;
242 };
243
244 extern std::unique_ptr<FramebufferManager> g_framebuffer_manager;