-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurface.cpp
More file actions
81 lines (72 loc) · 2.34 KB
/
Surface.cpp
File metadata and controls
81 lines (72 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "Surface.hpp"
#include "Device.hpp"
#include <algorithm>
Surface::Surface(Window& window, VulkanInstance& instance)
: m_Surface(instance.Get(), createSDLVulkanSurface(window.Get(), instance.Get()))
{
}
VkSurfaceKHR Surface::createSDLVulkanSurface(SDL_Window* window, vk::raii::Instance& instance)
{
VkSurfaceKHR surface;
if (!window)
{
LogError("Window pointer not initialized correctly");
}
if (SDL_Vulkan_CreateSurface(
window, static_cast<VkInstance>(*instance), &surface) !=
SDL_TRUE)
{
LogError(
fmt::format("Could not create SDL surface: {}", SDL_GetError()));
}
return surface;
}
void Surface::GetSurfaceCapabilities(Device& device)
{
surfaceCapabilities = device.GetSurfaceCapabilities(m_Surface);
LogDebug(fmt::format(
"Surface capabiltiies:\t{}", surfaceCapabilities.currentExtent));
}
const std::vector<vk::SurfaceFormatKHR>
Surface::GetCompatableSurfaceFormats(Device& device)
{
return device.GetCompatableSurfaceFormats(m_Surface);
}
vk::SurfaceFormatKHR Surface::PickSurfaceFormat(
const std::vector<vk::SurfaceFormatKHR>& surfaceFormats,
const vk::Format requestedFormat)
{
vk::SurfaceFormatKHR surfaceFormat(vk::Format::eUndefined, vk::ColorSpaceKHR::eSrgbNonlinear);
if (surfaceFormats.size() == 1 &&
surfaceFormats.front().format == vk::Format::eUndefined)
{
surfaceFormat = {vk::Format::eR8G8B8A8Unorm, vk::ColorSpaceKHR::eSrgbNonlinear};
}
else if (
std::find_if(
surfaceFormats.begin(), surfaceFormats.end(),
[requestedFormat](vk::SurfaceFormatKHR surfaceFormat) {
return surfaceFormat.format == requestedFormat;
}) != surfaceFormats.end())
{
surfaceFormat.format = requestedFormat;
}
// fallback
if (surfaceFormat.format == vk::Format::eUndefined)
{
surfaceFormat = surfaceFormats.front();
LogWarning(fmt::format(
"Requested format not found! Falling back to: {} {}",
surfaceFormat.format, surfaceFormat.colorSpace));
}
return surfaceFormat;
}
vk::raii::SurfaceKHR& Surface::Get()
{
return m_Surface;
}
void Surface::GetSurfaceFormat(Device& device)
{
surfaceFormat = PickSurfaceFormat(
GetCompatableSurfaceFormats(device), vk::Format::eB8G8R8A8Unorm);
}