Debian Codegraph @

Source/Core/VideoCommon/AbstractGfx.h

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include "Common/HookableEvent.h"
7 #include "Common/MathUtil.h"
8
9 #include "VideoCommon/RenderState.h"
10
11 #include <array>
12 #include <memory>
13 #include <vector>
14
15 class AbstractFramebuffer;
16 class AbstractPipeline;
17 class AbstractShader;
18 class AbstractTexture;
19 class AbstractStagingTexture;
20 class NativeVertexFormat;
21 struct ComputePipelineConfig;
22 struct AbstractPipelineConfig;
23 struct PortableVertexDeclaration;
24 struct TextureConfig;
25 enum class AbstractTextureFormat : u32;
26 enum class ShaderStage;
27 enum class StagingTextureType;
28
29 struct SurfaceInfo
30 {
31 u32 width = 0;
32 u32 height = 0;
33 float scale = 0.0f;
34 AbstractTextureFormat format = {};
35 };
36
37 namespace VideoCommon
38 {
39 class AsyncShaderCompiler;
40 class ShaderIncluder;
41 } // namespace VideoCommon
42
43 using ClearColor = std::array<float, 4>;
44
45 // AbstractGfx is the root of Dolphin's Graphics API abstraction layer.
46 //
47 // Abstract knows nothing about the internals of the GameCube/Wii, that is all handled elsewhere in
48 // VideoCommon.
49
50 class AbstractGfx
51 {
52 public:
53 AbstractGfx();
54 virtual ~AbstractGfx() = default;
55
56 virtual bool IsHeadless() const = 0;
57
58 // Does the backend support drawing a UI or doing post-processing
59 virtual bool SupportsUtilityDrawing() const { return true; }
60
61 virtual void SetPipeline(const AbstractPipeline* pipeline) {}
62 virtual void SetScissorRect(const MathUtil::Rectangle<int>& rc) {}
63 virtual void SetTexture(u32 index, const AbstractTexture* texture) {}
64 virtual void SetSamplerState(u32 index, const SamplerState& state) {}
65 virtual void SetComputeImageTexture(u32 index, AbstractTexture* texture, bool read, bool write) {}
66 virtual void UnbindTexture(const AbstractTexture* texture) {}
67 virtual void SetViewport(float x, float y, float width, float height, float near_depth,
68 float far_depth)
69 {
70 }
71 virtual void SetFullscreen(bool enable_fullscreen) {}
72 virtual bool IsFullscreen() const { return false; }
73 virtual void BeginUtilityDrawing();
74 virtual void EndUtilityDrawing();
75 virtual std::unique_ptr<AbstractTexture> CreateTexture(const TextureConfig& config,
76 std::string_view name = "") = 0;
77 virtual std::unique_ptr<AbstractStagingTexture>
78 CreateStagingTexture(StagingTextureType type, const TextureConfig& config) = 0;
79 virtual std::unique_ptr<AbstractFramebuffer>
80 CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
81 std::vector<AbstractTexture*> additional_color_attachments = {}) = 0;
82
83 // Framebuffer operations.
84 virtual void SetFramebuffer(AbstractFramebuffer* framebuffer);
85 virtual void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer);
86 virtual void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer,
87 const ClearColor& color_value = {}, float depth_value = 0.0f);
88
89 virtual void ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
90 bool alphaEnable, bool zEnable, u32 color, u32 z);
91
92 // Drawing with currently-bound pipeline state.
93 virtual void Draw(u32 base_vertex, u32 num_vertices) {}
94 virtual void DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) {}
95
96 // Dispatching compute shaders with currently-bound state.
97 virtual void DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y,
98 u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z)
99 {
100 }
101
102 // Binds the backbuffer for rendering. The buffer will be cleared immediately after binding.
103 // This is where any window size changes are detected, therefore m_backbuffer_width and/or
104 // m_backbuffer_height may change after this function returns.
105 // If this returns false, a problem occurred binding the backbuffer.
106 // Don't render anything to it, but still call `PresentBackbuffer`, which will reset any
107 // per-frame resources and prepare for the next frame.
108 virtual bool BindBackbuffer(const ClearColor& clear_color = {}) { return true; }
109
110 // Presents the backbuffer to the window system, or "swaps buffers".
111 virtual void PresentBackbuffer() {}
112
113 // Shader modules/objects.
114 virtual std::unique_ptr<AbstractShader>
115 CreateShaderFromSource(ShaderStage stage, std::string_view source,
116 VideoCommon::ShaderIncluder* shader_includer = nullptr,
117 std::string_view name = "") = 0;
118 virtual std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage,
119 const void* data, size_t length,
120 std::string_view name = "") = 0;
121 virtual std::unique_ptr<NativeVertexFormat>
122 CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) = 0;
123 virtual std::unique_ptr<AbstractPipeline> CreatePipeline(const AbstractPipelineConfig& config,
124 const void* cache_data = nullptr,
125 size_t cache_data_length = 0) = 0;
126
127 AbstractFramebuffer* GetCurrentFramebuffer() const { return m_current_framebuffer; }
128
129 struct ViewportAndScissor
130 {
131 MathUtil::Rectangle<int> scissor_rect;
132 float viewport_x = 0;
133 float viewport_y = 0;
134 float viewport_width = 0;
135 float viewport_height = 0;
136 float viewport_near_depth = 0;
137 float viewport_far_depth = 0;
138 };
139
140 // Stores the last viewport and scissor, the stored data is restored in 'EndUtilityDrawing'
141 void StoreViewportAndScissor(const ViewportAndScissor& viewport_and_scissor);
142
143 // Sets viewport and scissor to the specified rectangle. rect is assumed to be in framebuffer
144 // coordinates, i.e. lower-left origin in OpenGL.
145 void SetViewportAndScissor(const MathUtil::Rectangle<int>& rect, float min_depth = 0.0f,
146 float max_depth = 1.0f);
147
148 // Scales a GPU texture using a copy shader.
149 virtual void ScaleTexture(AbstractFramebuffer* dst_framebuffer,
150 const MathUtil::Rectangle<int>& dst_rect,
151 const AbstractTexture* src_texture,
152 const MathUtil::Rectangle<int>& src_rect);
153
154 // Converts an upper-left to lower-left if required by the backend, optionally
155 // clamping to the framebuffer size.
156 MathUtil::Rectangle<int> ConvertFramebufferRectangle(const MathUtil::Rectangle<int>& rect,
157 u32 fb_width, u32 fb_height) const;
158 MathUtil::Rectangle<int>
159 ConvertFramebufferRectangle(const MathUtil::Rectangle<int>& rect,
160 const AbstractFramebuffer* framebuffer) const;
161
162 virtual void Flush() {}
163 virtual void WaitForGPUIdle() {}
164
165 // For opengl's glDrawBuffer
166 virtual void SelectLeftBuffer() {}
167 virtual void SelectRightBuffer() {}
168 virtual void SelectMainBuffer() {}
169
170 // A simple presentation fallback, only used by video software
171 virtual void ShowImage(const AbstractTexture* source_texture,
172 const MathUtil::Rectangle<int>& source_rc)
173 {
174 }
175
176 virtual std::unique_ptr<VideoCommon::AsyncShaderCompiler> CreateAsyncShaderCompiler();
177
178 // Called when the configuration changes, and backend structures need to be updated.
179 virtual void OnConfigChanged(u32 changed_bits);
180
181 // Returns true if a layer-expanding geometry shader should be used when rendering
182 // the user interface on the output buffer.
183 bool UseGeometryShaderForUI() const;
184
185 // Returns info about the main surface (aka backbuffer)
186 virtual SurfaceInfo GetSurfaceInfo() const = 0;
187
188 protected:
189 AbstractFramebuffer* m_current_framebuffer = nullptr;
190 const AbstractPipeline* m_current_pipeline = nullptr;
191
192 private:
193 Common::EventHook m_config_changed;
194 ViewportAndScissor m_viewport_and_scissor;
195 };
196
197 extern std::unique_ptr<AbstractGfx> g_gfx;