Debian Codegraph @

Source/Core/VideoBackends/Vulkan/VKGfx.cpp

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/Vulkan/VKGfx.h"
5
6 #include <cstddef>
7 #include <cstdio>
8 #include <utility>
9
10 #include "Common/CommonTypes.h"
11 #include "Common/Logging/Log.h"
12 #include "Common/MsgHandler.h"
13
14 #include "VideoBackends/Vulkan/CommandBufferManager.h"
15 #include "VideoBackends/Vulkan/ObjectCache.h"
16 #include "VideoBackends/Vulkan/StateTracker.h"
17 #include "VideoBackends/Vulkan/VKPipeline.h"
18 #include "VideoBackends/Vulkan/VKShader.h"
19 #include "VideoBackends/Vulkan/VKSwapChain.h"
20 #include "VideoBackends/Vulkan/VKTexture.h"
21 #include "VideoBackends/Vulkan/VKVertexFormat.h"
22
23 #include "VideoCommon/DriverDetails.h"
24 #include "VideoCommon/FramebufferManager.h"
25 #include "VideoCommon/Present.h"
26 #include "VideoCommon/RenderState.h"
27 #include "VideoCommon/VideoConfig.h"
28
29 namespace Vulkan
30 {
31 VKGfx::VKGfx(std::unique_ptr<SwapChain> swap_chain, float backbuffer_scale)
32 : m_swap_chain(std::move(swap_chain)), m_backbuffer_scale(backbuffer_scale)
33 {
34 UpdateActiveConfig();
35 for (SamplerState& sampler_state : m_sampler_states)
36 sampler_state = RenderState::GetPointSamplerState();
37
38 // Various initialization routines will have executed commands on the command buffer.
39 // Execute what we have done before beginning the first frame.
40 ExecuteCommandBuffer(true, false);
41 }
42
43 VKGfx::~VKGfx() = default;
44
45 bool VKGfx::IsHeadless() const
46 {
47 return m_swap_chain == nullptr;
48 }
49
50 std::unique_ptr<AbstractTexture> VKGfx::CreateTexture(const TextureConfig& config,
51 std::string_view name)
52 {
53 return VKTexture::Create(config, name);
54 }
55
56 std::unique_ptr<AbstractStagingTexture> VKGfx::CreateStagingTexture(StagingTextureType type,
57 const TextureConfig& config)
58 {
59 return VKStagingTexture::Create(type, config);
60 }
61
62 std::unique_ptr<AbstractShader>
63 VKGfx::CreateShaderFromSource(ShaderStage stage, std::string_view source,
64 VideoCommon::ShaderIncluder* shader_includer, std::string_view name)
65 {
66 return VKShader::CreateFromSource(stage, source, shader_includer, name);
67 }
68
69 std::unique_ptr<AbstractShader> VKGfx::CreateShaderFromBinary(ShaderStage stage, const void* data,
70 size_t length, std::string_view name)
71 {
72 return VKShader::CreateFromBinary(stage, data, length, name);
73 }
74
75 std::unique_ptr<NativeVertexFormat>
76 VKGfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
77 {
78 return std::make_unique<VertexFormat>(vtx_decl);
79 }
80
81 std::unique_ptr<AbstractPipeline> VKGfx::CreatePipeline(const AbstractPipelineConfig& config,
82 const void* cache_data,
83 size_t cache_data_length)
84 {
85 return VKPipeline::Create(config);
86 }
87
88 std::unique_ptr<AbstractFramebuffer>
89 VKGfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
90 std::vector<AbstractTexture*> additional_color_attachments)
91 {
92 return VKFramebuffer::Create(static_cast<VKTexture*>(color_attachment),
93 static_cast<VKTexture*>(depth_attachment),
94 std::move(additional_color_attachments));
95 }
96
97 void VKGfx::SetPipeline(const AbstractPipeline* pipeline)
98 {
99 StateTracker::GetInstance()->SetPipeline(static_cast<const VKPipeline*>(pipeline));
100 }
101
102 void VKGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool color_enable,
103 bool alpha_enable, bool z_enable, u32 color, u32 z)
104 {
105 VkRect2D target_vk_rc = {
106 {target_rc.left, target_rc.top},
107 {static_cast<uint32_t>(target_rc.GetWidth()), static_cast<uint32_t>(target_rc.GetHeight())}};
108
109 // Convert RGBA8 -> floating-point values.
110 VkClearValue clear_color_value = {};
111 VkClearValue clear_depth_value = {};
112 clear_color_value.color.float32[0] = static_cast<float>((color >> 16) & 0xFF) / 255.0f;
113 clear_color_value.color.float32[1] = static_cast<float>((color >> 8) & 0xFF) / 255.0f;
114 clear_color_value.color.float32[2] = static_cast<float>((color >> 0) & 0xFF) / 255.0f;
115 clear_color_value.color.float32[3] = static_cast<float>((color >> 24) & 0xFF) / 255.0f;
116 clear_depth_value.depthStencil.depth = static_cast<float>(z & 0xFFFFFF) / 16777216.0f;
117 if (!g_backend_info.bSupportsReversedDepthRange)
118 clear_depth_value.depthStencil.depth = 1.0f - clear_depth_value.depthStencil.depth;
119
120 // If we're not in a render pass (start of the frame), we can use a clear render pass
121 // to discard the data, rather than loading and then clearing.
122 bool use_clear_attachments = (color_enable && alpha_enable) || z_enable;
123 bool use_clear_render_pass =
124 !StateTracker::GetInstance()->InRenderPass() && color_enable && alpha_enable && z_enable;
125
126 // The NVIDIA Vulkan driver causes the GPU to lock up, or throw exceptions if MSAA is enabled,
127 // a non-full clear rect is specified, and a clear loadop or vkCmdClearAttachments is used.
128 if (g_ActiveConfig.iMultisamples > 1 &&
129 DriverDetails::HasBug(DriverDetails::BUG_BROKEN_MSAA_CLEAR))
130 {
131 use_clear_render_pass = false;
132 use_clear_attachments = false;
133 }
134
135 // This path cannot be used if the driver implementation doesn't guarantee pixels with no drawn
136 // geometry in "this" renderpass won't be cleared
137 if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_CLEAR_LOADOP_RENDERPASS))
138 use_clear_render_pass = false;
139
140 auto* vk_frame_buffer = static_cast<VKFramebuffer*>(m_current_framebuffer);
141
142 // Fastest path: Use a render pass to clear the buffers.
143 if (use_clear_render_pass)
144 {
145 vk_frame_buffer->SetAndClear(target_vk_rc, clear_color_value, clear_depth_value);
146 return;
147 }
148
149 // Fast path: Use vkCmdClearAttachments to clear the buffers within a render path
150 // We can't use this when preserving alpha but clearing color.
151 if (use_clear_attachments)
152 {
153 std::vector<VkClearAttachment> clear_attachments;
154 bool has_color = false;
155 if (color_enable && alpha_enable)
156 {
157 VkClearAttachment clear_attachment;
158 clear_attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
159 clear_attachment.colorAttachment = 0;
160 clear_attachment.clearValue = clear_color_value;
161 clear_attachments.push_back(std::move(clear_attachment));
162 color_enable = false;
163 alpha_enable = false;
164 has_color = true;
165 }
166 if (z_enable)
167 {
168 VkClearAttachment clear_attachment;
169 clear_attachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
170 clear_attachment.colorAttachment = 0;
171 clear_attachment.clearValue = clear_depth_value;
172 clear_attachments.push_back(std::move(clear_attachment));
173 z_enable = false;
174 }
175 if (has_color)
176 {
177 for (std::size_t i = 0; i < vk_frame_buffer->GetNumberOfAdditonalAttachments(); i++)
178 {
179 VkClearAttachment clear_attachment;
180 clear_attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
181 clear_attachment.colorAttachment = 0;
182 clear_attachment.clearValue = clear_color_value;
183 clear_attachments.push_back(std::move(clear_attachment));
184 }
185 }
186 if (!clear_attachments.empty())
187 {
188 VkClearRect vk_rect = {target_vk_rc, 0, g_framebuffer_manager->GetEFBLayers()};
189 if (!StateTracker::GetInstance()->IsWithinRenderArea(
190 target_vk_rc.offset.x, target_vk_rc.offset.y, target_vk_rc.extent.width,
191 target_vk_rc.extent.height))
192 {
193 StateTracker::GetInstance()->EndClearRenderPass();
194 }
195 StateTracker::GetInstance()->BeginRenderPass();
196
197 vkCmdClearAttachments(g_command_buffer_mgr->GetCurrentCommandBuffer(),
198 static_cast<uint32_t>(clear_attachments.size()),
199 clear_attachments.data(), 1, &vk_rect);
200 }
201 }
202
203 // Anything left over for the slow path?
204 if (!color_enable && !alpha_enable && !z_enable)
205 return;
206
207 AbstractGfx::ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z);
208 }
209
210 void VKGfx::Flush()
211 {
212 ExecuteCommandBuffer(true, false);
213 }
214
215 void VKGfx::WaitForGPUIdle()
216 {
217 ExecuteCommandBuffer(false, true);
218 }
219
220 bool VKGfx::BindBackbuffer(const ClearColor& clear_color)
221 {
222 StateTracker::GetInstance()->EndRenderPass();
223
224 g_command_buffer_mgr->WaitForWorkerThreadIdle();
225
226 // Handle host window resizes.
227 CheckForSurfaceChange();
228 CheckForSurfaceResize();
229
230 // Check for exclusive fullscreen request.
231 if (m_swap_chain->GetCurrentFullscreenState() != m_swap_chain->GetNextFullscreenState() &&
232 !m_swap_chain->SetFullscreenState(m_swap_chain->GetNextFullscreenState()))
233 {
234 // if it fails, don't keep trying
235 m_swap_chain->SetNextFullscreenState(m_swap_chain->GetCurrentFullscreenState());
236 }
237
238 const bool present_fail = g_command_buffer_mgr->CheckLastPresentFail();
239 VkResult res = present_fail ? g_command_buffer_mgr->GetLastPresentResult() :
240 m_swap_chain->AcquireNextImage();
241
242 if (res == VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT &&
243 !m_swap_chain->GetCurrentFullscreenState())
244 {
245 // AMD's binary driver as of 21.3 seems to return exclusive fullscreen lost even when it was
246 // never requested, so long as the caller requested it to be application controlled. Handle
247 // this ignoring the lost result and just continuing as normal if we never acquired it.
248 res = VK_SUCCESS;
249 if (present_fail)
250 {
251 // We still need to acquire an image.
252 res = m_swap_chain->AcquireNextImage();
253 }
254 }
255
256 if (res != VK_SUCCESS)
257 {
258 // Execute cmdbuffer before resizing, as the last frame could still be presenting.
259 ExecuteCommandBuffer(false, true);
260
261 // Was this a lost exclusive fullscreen?
262 if (res == VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
263 {
264 // The present keeps returning exclusive mode lost unless we re-create the swap chain.
265 INFO_LOG_FMT(VIDEO, "Lost exclusive fullscreen.");
266 m_swap_chain->RecreateSwapChain();
267 }
268 else if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR)
269 {
270 INFO_LOG_FMT(VIDEO, "Resizing swap chain due to suboptimal/out-of-date");
271 m_swap_chain->ResizeSwapChain();
272 }
273 else
274 {
275 ERROR_LOG_FMT(VIDEO, "Unknown present error {:#010X} {}, please report.",
276 std::to_underlying(res), VkResultToString(res));
277 m_swap_chain->RecreateSwapChain();
278 }
279
280 res = m_swap_chain->AcquireNextImage();
281 if (res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR)
282 {
283 if (res == VK_ERROR_OUT_OF_DATE_KHR)
284 {
285 INFO_LOG_FMT(VIDEO, "Swapchain still out of date, will try again next frame...");
286 }
287 else
288 {
289 PanicAlertFmt("Failed to grab image from swap chain: {:#010X} {}", std::to_underlying(res),
290 VkResultToString(res));
291 }
292 }
293 }
294
295 if (!m_swap_chain->IsCurrentImageValid())
296 return false;
297
298 // Transition from undefined (or present src, but it can be substituted) to
299 // color attachment ready for writing. These transitions must occur outside
300 // a render pass, unless the render pass declares a self-dependency.
301 m_swap_chain->GetCurrentTexture()->OverrideImageLayout(VK_IMAGE_LAYOUT_UNDEFINED);
302 m_swap_chain->GetCurrentTexture()->TransitionToLayout(
303 g_command_buffer_mgr->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
304 SetAndClearFramebuffer(m_swap_chain->GetCurrentFramebuffer(),
305 ClearColor{{0.0f, 0.0f, 0.0f, 1.0f}});
306 return true;
307 }
308
309 void VKGfx::PresentBackbuffer()
310 {
311 // End drawing to backbuffer
312 StateTracker::GetInstance()->EndRenderPass();
313
314 if (m_swap_chain->IsCurrentImageValid())
315 {
316 // Transition the backbuffer to PRESENT_SRC to ensure all commands drawing
317 // to it have finished before present.
318 m_swap_chain->GetCurrentTexture()->TransitionToLayout(
319 g_command_buffer_mgr->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
320
321 // Submit the current command buffer, signaling rendering finished semaphore when it's done
322 // Because this final command buffer is rendering to the swap chain, we need to wait for
323 // the available semaphore to be signaled before executing the buffer. This final submission
324 // can happen off-thread in the background while we're preparing the next frame.
325 g_command_buffer_mgr->SubmitCommandBuffer(true, false, true, m_swap_chain->GetSwapChain(),
326 m_swap_chain->GetCurrentImageIndex());
327 }
328 else
329 {
330 g_command_buffer_mgr->SubmitCommandBuffer(true, false, true);
331 }
332
333 // New cmdbuffer, so invalidate state.
334 StateTracker::GetInstance()->InvalidateCachedState();
335 }
336
337 void VKGfx::SetFullscreen(bool enable_fullscreen)
338 {
339 if (!m_swap_chain->IsFullscreenSupported())
340 return;
341
342 m_swap_chain->SetNextFullscreenState(enable_fullscreen);
343 }
344
345 bool VKGfx::IsFullscreen() const
346 {
347 return m_swap_chain && m_swap_chain->GetCurrentFullscreenState();
348 }
349
350 void VKGfx::ExecuteCommandBuffer(bool submit_off_thread, bool wait_for_completion)
351 {
352 StateTracker::GetInstance()->EndRenderPass();
353
354 g_command_buffer_mgr->SubmitCommandBuffer(submit_off_thread, wait_for_completion);
355
356 StateTracker::GetInstance()->InvalidateCachedState();
357 }
358
359 void VKGfx::CheckForSurfaceChange()
360 {
361 if (!g_presenter->SurfaceChangedTestAndClear() || !m_swap_chain)
362 return;
363
364 // Submit the current draws up until rendering the XFB.
365 ExecuteCommandBuffer(false, true);
366
367 // Clear the present failed flag, since we don't want to resize after recreating.
368 g_command_buffer_mgr->CheckLastPresentFail();
369
370 // Recreate the surface. If this fails we're in trouble.
371 if (!m_swap_chain->RecreateSurface(g_presenter->GetNewSurfaceHandle()))
372 PanicAlertFmt("Failed to recreate Vulkan surface. Cannot continue.");
373
374 // Handle case where the dimensions are now different.
375 OnSwapChainResized();
376 }
377
378 void VKGfx::CheckForSurfaceResize()
379 {
380 if (!g_presenter->SurfaceResizedTestAndClear())
381 return;
382
383 // If we don't have a surface, how can we resize the swap chain?
384 // CheckForSurfaceChange should handle this case.
385 if (!m_swap_chain)
386 {
387 WARN_LOG_FMT(VIDEO, "Surface resize event received without active surface, ignoring");
388 return;
389 }
390
391 // Wait for the GPU to catch up since we're going to destroy the swap chain.
392 ExecuteCommandBuffer(false, true);
393
394 // Clear the present failed flag, since we don't want to resize after recreating.
395 g_command_buffer_mgr->CheckLastPresentFail();
396
397 // Resize the swap chain.
398 m_swap_chain->RecreateSwapChain();
399 OnSwapChainResized();
400 }
401
402 void VKGfx::OnConfigChanged(u32 bits)
403 {
404 AbstractGfx::OnConfigChanged(bits);
405
406 if (bits & CONFIG_CHANGE_BIT_HOST_CONFIG)
407 g_object_cache->ReloadPipelineCache();
408
409 // For vsync, we need to change the present mode, which means recreating the swap chain.
410 if (m_swap_chain && (bits & CONFIG_CHANGE_BIT_VSYNC))
411 {
412 ExecuteCommandBuffer(false, true);
413 m_swap_chain->SetVSync(g_ActiveConfig.bVSyncActive);
414 }
415
416 // For quad-buffered stereo we need to change the layer count, so recreate the swap chain.
417 if (m_swap_chain && ((bits & CONFIG_CHANGE_BIT_STEREO_MODE) || (bits & CONFIG_CHANGE_BIT_HDR)))
418 {
419 ExecuteCommandBuffer(false, true);
420 m_swap_chain->RecreateSwapChain();
421 }
422
423 // Wipe sampler cache if force texture filtering or anisotropy changes.
424 if (bits & (CONFIG_CHANGE_BIT_ANISOTROPY | CONFIG_CHANGE_BIT_FORCE_TEXTURE_FILTERING))
425 {
426 ExecuteCommandBuffer(false, true);
427 ResetSamplerStates();
428 }
429 }
430
431 void VKGfx::OnSwapChainResized()
432 {
433 g_presenter->SetBackbuffer(m_swap_chain->GetWidth(), m_swap_chain->GetHeight());
434 }
435
436 void VKGfx::BindFramebuffer(VKFramebuffer* fb)
437 {
438 StateTracker::GetInstance()->EndRenderPass();
439
440 // Shouldn't be bound as a texture.
441 fb->Unbind();
442
443 fb->TransitionForRender();
444 StateTracker::GetInstance()->SetFramebuffer(fb);
445 m_current_framebuffer = fb;
446 }
447
448 void VKGfx::SetFramebuffer(AbstractFramebuffer* framebuffer)
449 {
450 if (m_current_framebuffer == framebuffer)
451 return;
452
453 VKFramebuffer* vkfb = static_cast<VKFramebuffer*>(framebuffer);
454 BindFramebuffer(vkfb);
455 }
456
457 void VKGfx::SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer)
458 {
459 if (m_current_framebuffer == framebuffer)
460 return;
461
462 VKFramebuffer* vkfb = static_cast<VKFramebuffer*>(framebuffer);
463 BindFramebuffer(vkfb);
464
465 // If we're discarding, begin the discard pass, then switch to a load pass.
466 // This way if the command buffer is flushed, we don't start another discard pass.
467 StateTracker::GetInstance()->BeginDiscardRenderPass();
468 }
469
470 void VKGfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value,
471 float depth_value)
472 {
473 VKFramebuffer* vkfb = static_cast<VKFramebuffer*>(framebuffer);
474 BindFramebuffer(vkfb);
475
476 VkClearValue clear_color_value;
477 std::memcpy(clear_color_value.color.float32, color_value.data(),
478 sizeof(clear_color_value.color.float32));
479 VkClearValue clear_depth_value;
480 clear_depth_value.depthStencil.depth = depth_value;
481 clear_depth_value.depthStencil.stencil = 0;
482 vkfb->SetAndClear(vkfb->GetRect(), clear_color_value, clear_depth_value);
483 }
484
485 void VKGfx::SetTexture(u32 index, const AbstractTexture* texture)
486 {
487 // Texture should always be in SHADER_READ_ONLY layout prior to use.
488 // This is so we don't need to transition during render passes.
489 const VKTexture* tex = static_cast<const VKTexture*>(texture);
490 if (tex)
491 {
492 if (tex->GetLayout() != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
493 {
494 if (StateTracker::GetInstance()->InRenderPass())
495 {
496 WARN_LOG_FMT(VIDEO, "Transitioning image in render pass in VKGfx::SetTexture()");
497 StateTracker::GetInstance()->EndRenderPass();
498 }
499
500 tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(),
501 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
502 }
503
504 StateTracker::GetInstance()->SetTexture(index, tex->GetView());
505 }
506 else
507 {
508 StateTracker::GetInstance()->SetTexture(0, VK_NULL_HANDLE);
509 }
510 }
511
512 void VKGfx::SetSamplerState(u32 index, const SamplerState& state)
513 {
514 // Skip lookup if the state hasn't changed.
515 if (m_sampler_states[index] == state)
516 return;
517
518 // Look up new state and replace in state tracker.
519 VkSampler sampler = g_object_cache->GetSampler(state);
520 if (sampler == VK_NULL_HANDLE)
521 {
522 ERROR_LOG_FMT(VIDEO, "Failed to create sampler");
523 sampler = g_object_cache->GetPointSampler();
524 }
525
526 StateTracker::GetInstance()->SetSampler(index, sampler);
527 m_sampler_states[index] = state;
528 }
529
530 void VKGfx::SetComputeImageTexture(u32 index, AbstractTexture* texture, bool read, bool write)
531 {
532 VKTexture* vk_texture = static_cast<VKTexture*>(texture);
533 if (vk_texture)
534 {
535 StateTracker::GetInstance()->EndRenderPass();
536 StateTracker::GetInstance()->SetImageTexture(index, vk_texture->GetView());
537 vk_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(),
538 read ? (write ? VKTexture::ComputeImageLayout::ReadWrite :
539 VKTexture::ComputeImageLayout::ReadOnly) :
540 VKTexture::ComputeImageLayout::WriteOnly);
541 }
542 else
543 {
544 StateTracker::GetInstance()->SetImageTexture(index, VK_NULL_HANDLE);
545 }
546 }
547
548 void VKGfx::UnbindTexture(const AbstractTexture* texture)
549 {
550 StateTracker::GetInstance()->UnbindTexture(static_cast<const VKTexture*>(texture)->GetView());
551 }
552
553 void VKGfx::ResetSamplerStates()
554 {
555 // Invalidate all sampler states, next draw will re-initialize them.
556 for (u32 i = 0; i < m_sampler_states.size(); i++)
557 {
558 m_sampler_states[i] = RenderState::GetPointSamplerState();
559 StateTracker::GetInstance()->SetSampler(i, g_object_cache->GetPointSampler());
560 }
561
562 // Invalidate all sampler objects (some will be unused now).
563 g_object_cache->ClearSamplerCache();
564 }
565
566 void VKGfx::SetScissorRect(const MathUtil::Rectangle<int>& rc)
567 {
568 VkRect2D scissor = {{rc.left, rc.top},
569 {static_cast<u32>(rc.GetWidth()), static_cast<u32>(rc.GetHeight())}};
570
571 // See Vulkan spec for vkCmdSetScissor:
572 // The x and y members of offset must be greater than or equal to 0.
573 if (scissor.offset.x < 0)
574 {
575 scissor.extent.width -= -scissor.offset.x;
576 scissor.offset.x = 0;
577 }
578 if (scissor.offset.y < 0)
579 {
580 scissor.extent.height -= -scissor.offset.y;
581 scissor.offset.y = 0;
582 }
583 StateTracker::GetInstance()->SetScissor(scissor);
584 }
585
586 void VKGfx::SetViewport(float x, float y, float width, float height, float near_depth,
587 float far_depth)
588 {
589 VkViewport viewport = {x, y, width, height, near_depth, far_depth};
590 StateTracker::GetInstance()->SetViewport(viewport);
591 }
592
593 void VKGfx::Draw(u32 base_vertex, u32 num_vertices)
594 {
595 if (!StateTracker::GetInstance()->Bind())
596 return;
597
598 vkCmdDraw(g_command_buffer_mgr->GetCurrentCommandBuffer(), num_vertices, 1, base_vertex, 0);
599 }
600
601 void VKGfx::DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex)
602 {
603 if (!StateTracker::GetInstance()->Bind())
604 return;
605
606 vkCmdDrawIndexed(g_command_buffer_mgr->GetCurrentCommandBuffer(), num_indices, 1, base_index,
607 base_vertex, 0);
608 }
609
610 void VKGfx::DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y,
611 u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z)
612 {
613 StateTracker::GetInstance()->SetComputeShader(static_cast<const VKShader*>(shader));
614 if (StateTracker::GetInstance()->BindCompute())
615 vkCmdDispatch(g_command_buffer_mgr->GetCurrentCommandBuffer(), groups_x, groups_y, groups_z);
616 }
617
618 SurfaceInfo VKGfx::GetSurfaceInfo() const
619 {
620 return {m_swap_chain ? m_swap_chain->GetWidth() : 1u,
621 m_swap_chain ? m_swap_chain->GetHeight() : 0u, m_backbuffer_scale,
622 m_swap_chain ? m_swap_chain->GetTextureFormat() : AbstractTextureFormat::Undefined};
623 }
624
625 } // namespace Vulkan