Debian Codegraph @

Source/Core/VideoBackends/D3D/D3DGfx.cpp

raw symbols SCIP
1 // Copyright 2010 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/D3D/D3DGfx.h"
5
6 #include <algorithm>
7 #include <array>
8 #include <cmath>
9 #include <cstring>
10 #include <memory>
11 #include <string>
12 #include <strsafe.h>
13 #include <tuple>
14
15 #include "Common/Assert.h"
16 #include "Common/CommonTypes.h"
17 #include "Common/Logging/Log.h"
18 #include "Common/MathUtil.h"
19
20 #include "Core/Core.h"
21
22 #include "VideoBackends/D3D/D3DBase.h"
23 #include "VideoBackends/D3D/D3DBoundingBox.h"
24 #include "VideoBackends/D3D/D3DState.h"
25 #include "VideoBackends/D3D/D3DSwapChain.h"
26 #include "VideoBackends/D3D/DXPipeline.h"
27 #include "VideoBackends/D3D/DXShader.h"
28 #include "VideoBackends/D3D/DXTexture.h"
29
30 #include "VideoCommon/BPFunctions.h"
31 #include "VideoCommon/FramebufferManager.h"
32 #include "VideoCommon/PostProcessing.h"
33 #include "VideoCommon/Present.h"
34 #include "VideoCommon/RenderState.h"
35 #include "VideoCommon/VideoConfig.h"
36 #include "VideoCommon/XFMemory.h"
37
38 namespace DX11
39 {
40 Gfx::Gfx(std::unique_ptr<SwapChain> swap_chain, float backbuffer_scale)
41 : m_backbuffer_scale(backbuffer_scale), m_swap_chain(std::move(swap_chain))
42 {
43 }
44
45 Gfx::~Gfx() = default;
46
47 bool Gfx::IsHeadless() const
48 {
49 return !m_swap_chain;
50 }
51
52 std::unique_ptr<AbstractTexture> Gfx::CreateTexture(const TextureConfig& config,
53 std::string_view name)
54 {
55 return DXTexture::Create(config, name);
56 }
57
58 std::unique_ptr<AbstractStagingTexture> Gfx::CreateStagingTexture(StagingTextureType type,
59 const TextureConfig& config)
60 {
61 return DXStagingTexture::Create(type, config);
62 }
63
64 std::unique_ptr<AbstractFramebuffer>
65 Gfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
66 std::vector<AbstractTexture*> additional_color_attachments)
67 {
68 return DXFramebuffer::Create(static_cast<DXTexture*>(color_attachment),
69 static_cast<DXTexture*>(depth_attachment),
70 std::move(additional_color_attachments));
71 }
72
73 std::unique_ptr<AbstractShader>
74 Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source,
75 VideoCommon::ShaderIncluder* shader_includer, std::string_view name)
76 {
77 auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source, shader_includer);
78 if (!bytecode)
79 return nullptr;
80
81 return DXShader::CreateFromBytecode(stage, std::move(*bytecode), name);
82 }
83
84 std::unique_ptr<AbstractShader> Gfx::CreateShaderFromBinary(ShaderStage stage, const void* data,
85 size_t length, std::string_view name)
86 {
87 return DXShader::CreateFromBytecode(stage, DXShader::CreateByteCode(data, length), name);
88 }
89
90 std::unique_ptr<AbstractPipeline> Gfx::CreatePipeline(const AbstractPipelineConfig& config,
91 const void* cache_data,
92 size_t cache_data_length)
93 {
94 return DXPipeline::Create(config);
95 }
96
97 void Gfx::SetPipeline(const AbstractPipeline* pipeline)
98 {
99 const DXPipeline* dx_pipeline = static_cast<const DXPipeline*>(pipeline);
100 if (m_current_pipeline == dx_pipeline)
101 return;
102
103 if (dx_pipeline)
104 {
105 D3D::stateman->SetRasterizerState(dx_pipeline->GetRasterizerState());
106 D3D::stateman->SetDepthState(dx_pipeline->GetDepthState());
107 D3D::stateman->SetBlendState(dx_pipeline->GetBlendState());
108 D3D::stateman->SetPrimitiveTopology(dx_pipeline->GetPrimitiveTopology());
109 D3D::stateman->SetInputLayout(dx_pipeline->GetInputLayout());
110 D3D::stateman->SetVertexShader(dx_pipeline->GetVertexShader());
111 D3D::stateman->SetGeometryShader(dx_pipeline->GetGeometryShader());
112 D3D::stateman->SetPixelShader(dx_pipeline->GetPixelShader());
113 D3D::stateman->SetIntegerRTV(dx_pipeline->UseLogicOp());
114 }
115 else
116 {
117 // These will be destroyed at pipeline destruction.
118 D3D::stateman->SetInputLayout(nullptr);
119 D3D::stateman->SetVertexShader(nullptr);
120 D3D::stateman->SetGeometryShader(nullptr);
121 D3D::stateman->SetPixelShader(nullptr);
122 }
123 }
124
125 void Gfx::SetScissorRect(const MathUtil::Rectangle<int>& rc)
126 {
127 // TODO: Move to stateman
128 const CD3D11_RECT rect(rc.left, rc.top, std::max(rc.right, rc.left + 1),
129 std::max(rc.bottom, rc.top + 1));
130 D3D::context->RSSetScissorRects(1, &rect);
131 }
132
133 void Gfx::SetViewport(float x, float y, float width, float height, float near_depth,
134 float far_depth)
135 {
136 // TODO: Move to stateman
137 const CD3D11_VIEWPORT vp(x, y, width, height, near_depth, far_depth);
138 D3D::context->RSSetViewports(1, &vp);
139 }
140
141 void Gfx::Draw(u32 base_vertex, u32 num_vertices)
142 {
143 D3D::stateman->Apply();
144 D3D::context->Draw(num_vertices, base_vertex);
145 }
146
147 void Gfx::DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex)
148 {
149 D3D::stateman->Apply();
150 D3D::context->DrawIndexed(num_indices, base_index, base_vertex);
151 }
152
153 void Gfx::DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y,
154 u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z)
155 {
156 D3D::stateman->SetComputeShader(static_cast<const DXShader*>(shader)->GetD3DComputeShader());
157 D3D::stateman->SyncComputeBindings();
158 D3D::context->Dispatch(groups_x, groups_y, groups_z);
159 }
160
161 bool Gfx::BindBackbuffer(const ClearColor& clear_color)
162 {
163 CheckForSwapChainChanges();
164 SetAndClearFramebuffer(m_swap_chain->GetFramebuffer(), clear_color);
165 return true;
166 }
167
168 void Gfx::PresentBackbuffer()
169 {
170 m_swap_chain->Present();
171 }
172
173 void Gfx::OnConfigChanged(u32 bits)
174 {
175 AbstractGfx::OnConfigChanged(bits);
176
177 // Quad-buffer changes require swap chain recreation.
178 if (bits & CONFIG_CHANGE_BIT_STEREO_MODE && m_swap_chain)
179 m_swap_chain->SetStereo(SwapChain::WantsStereo());
180
181 if (bits & CONFIG_CHANGE_BIT_HDR && m_swap_chain)
182 m_swap_chain->SetHDR(SwapChain::WantsHDR());
183 }
184
185 void Gfx::CheckForSwapChainChanges()
186 {
187 const bool surface_changed = g_presenter->SurfaceChangedTestAndClear();
188 const bool surface_resized =
189 g_presenter->SurfaceResizedTestAndClear() || m_swap_chain->CheckForFullscreenChange();
190 if (!surface_changed && !surface_resized)
191 return;
192
193 if (surface_changed)
194 {
195 m_swap_chain->ChangeSurface(g_presenter->GetNewSurfaceHandle());
196 }
197 else
198 {
199 m_swap_chain->ResizeSwapChain();
200 }
201
202 g_presenter->SetBackbuffer(m_swap_chain->GetWidth(), m_swap_chain->GetHeight());
203 }
204
205 void Gfx::SetFramebuffer(AbstractFramebuffer* framebuffer)
206 {
207 if (m_current_framebuffer == framebuffer)
208 return;
209
210 // We can't leave the framebuffer bound as a texture and a render target.
211 DXFramebuffer* fb = static_cast<DXFramebuffer*>(framebuffer);
212 fb->Unbind();
213
214 D3D::stateman->SetFramebuffer(fb);
215 m_current_framebuffer = fb;
216 }
217
218 void Gfx::SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer)
219 {
220 SetFramebuffer(framebuffer);
221 }
222
223 void Gfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value,
224 float depth_value)
225 {
226 SetFramebuffer(framebuffer);
227 D3D::stateman->Apply();
228
229 DXFramebuffer* fb = static_cast<DXFramebuffer*>(framebuffer);
230 fb->Clear(color_value, depth_value);
231 }
232
233 void Gfx::SetTexture(u32 index, const AbstractTexture* texture)
234 {
235 D3D::stateman->SetTexture(index, texture ? static_cast<const DXTexture*>(texture)->GetD3DSRV() :
236 nullptr);
237 }
238
239 void Gfx::SetSamplerState(u32 index, const SamplerState& state)
240 {
241 D3D::stateman->SetSampler(index, m_state_cache.Get(state));
242 }
243
244 void Gfx::SetComputeImageTexture(u32 index, AbstractTexture* texture, bool read, bool write)
245 {
246 D3D::stateman->SetComputeUAV(index,
247 texture ? static_cast<DXTexture*>(texture)->GetD3DUAV() : nullptr);
248 }
249
250 void Gfx::UnbindTexture(const AbstractTexture* texture)
251 {
252 if (D3D::stateman->UnsetTexture(static_cast<const DXTexture*>(texture)->GetD3DSRV()) != 0)
253 D3D::stateman->ApplyTextures();
254 }
255
256 void Gfx::Flush()
257 {
258 D3D::context->Flush();
259 }
260
261 void Gfx::WaitForGPUIdle()
262 {
263 // There is no glFinish() equivalent in D3D.
264 D3D::context->Flush();
265 }
266
267 void Gfx::SetFullscreen(bool enable_fullscreen)
268 {
269 if (m_swap_chain)
270 m_swap_chain->SetFullscreen(enable_fullscreen);
271 }
272
273 bool Gfx::IsFullscreen() const
274 {
275 return m_swap_chain && m_swap_chain->GetFullscreen();
276 }
277
278 SurfaceInfo Gfx::GetSurfaceInfo() const
279 {
280 return {m_swap_chain ? static_cast<u32>(m_swap_chain->GetWidth()) : 0,
281 m_swap_chain ? static_cast<u32>(m_swap_chain->GetHeight()) : 0, m_backbuffer_scale,
282 m_swap_chain ? m_swap_chain->GetFormat() : AbstractTextureFormat::Undefined};
283 }
284
285 } // namespace DX11