SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Helpers.h
1#pragma once
2
3#include "spark/render/Format.h"
4#include "spark/render/vk/Export.h"
5
6#include "spark/base/Exception.h"
7
8#include <algorithm>
9#include <vector>
10
15#define SPARK_FWD_DECLARE_VK_HANDLE(handle) using handle = struct handle##_T*;
16
17namespace spark::render::vk::helpers
18{
24 [[nodiscard]] SPARK_RENDER_VK_EXPORT constexpr bool has_depth(const Format format)
25 {
26 const std::vector depth_formats = {
27 Format::D16_UNORM,
28 Format::D32_SFLOAT,
29 Format::X8_D24_UNORM,
30 Format::D16_UNORM_S8_UINT,
31 Format::D24_UNORM_S8_UINT,
32 Format::D32_SFLOAT_S8_UINT
33 };
34
35 return std::ranges::any_of(depth_formats, [&](const Format f) { return f == format; });
36 }
37
43 [[nodiscard]] SPARK_RENDER_VK_EXPORT constexpr bool has_stencil(const Format format)
44 {
45 const std::vector stencil_formats = {
46 Format::D16_UNORM_S8_UINT,
47 Format::D24_UNORM_S8_UINT,
48 Format::D32_SFLOAT_S8_UINT,
49 Format::S8_UINT
50 };
51
52 return std::ranges::any_of(stencil_formats, [&](const Format f) { return f == format; });
53 }
54
60 [[nodiscard]] SPARK_RENDER_VK_EXPORT constexpr std::size_t format_size(const Format format)
61 {
62 switch (format)
63 {
64 case Format::None:
65 return 0;
66 case Format::R4G4_UNORM:
67 case Format::R8_UNORM:
68 case Format::R8_SNORM:
69 case Format::R8_USCALED:
70 case Format::R8_SSCALED:
71 case Format::R8_UINT:
72 case Format::R8_SINT:
73 case Format::R8_SRGB:
74 case Format::S8_UINT:
75 return 1;
76 case Format::R4G4B4A4_UNORM:
77 case Format::B4G4R4A4_UNORM:
78 case Format::R5G6B5_UNORM:
79 case Format::B5G6R5_UNORM:
80 case Format::R5G5B5A1_UNORM:
81 case Format::B5G5R5A1_UNORM:
82 case Format::A1R5G5B5_UNORM:
83 case Format::R8G8_UNORM:
84 case Format::R8G8_SNORM:
85 case Format::R8G8_USCALED:
86 case Format::R8G8_SSCALED:
87 case Format::R8G8_UINT:
88 case Format::R8G8_SINT:
89 case Format::R8G8_SRGB:
90 case Format::R16_UNORM:
91 case Format::R16_SNORM:
92 case Format::R16_USCALED:
93 case Format::R16_SSCALED:
94 case Format::R16_UINT:
95 case Format::R16_SINT:
96 case Format::R16_SFLOAT:
97 case Format::D16_UNORM:
98 return 2;
99 case Format::R8G8B8_UNORM:
100 case Format::R8G8B8_SNORM:
101 case Format::R8G8B8_USCALED:
102 case Format::R8G8B8_SSCALED:
103 case Format::R8G8B8_UINT:
104 case Format::R8G8B8_SINT:
105 case Format::R8G8B8_SRGB:
106 case Format::B8G8R8_UNORM:
107 case Format::B8G8R8_SNORM:
108 case Format::B8G8R8_USCALED:
109 case Format::B8G8R8_SSCALED:
110 case Format::B8G8R8_UINT:
111 case Format::B8G8R8_SINT:
112 case Format::B8G8R8_SRGB:
113 case Format::D16_UNORM_S8_UINT:
114 return 3;
115 case Format::R8G8B8A8_UNORM:
116 case Format::R8G8B8A8_SNORM:
117 case Format::R8G8B8A8_USCALED:
118 case Format::R8G8B8A8_SSCALED:
119 case Format::R8G8B8A8_UINT:
120 case Format::R8G8B8A8_SINT:
121 case Format::R8G8B8A8_SRGB:
122 case Format::B8G8R8A8_UNORM:
123 case Format::B8G8R8A8_SNORM:
124 case Format::B8G8R8A8_USCALED:
125 case Format::B8G8R8A8_SSCALED:
126 case Format::B8G8R8A8_UINT:
127 case Format::B8G8R8A8_SINT:
128 case Format::B8G8R8A8_SRGB:
129 case Format::A8B8G8R8_UNORM:
130 case Format::A8B8G8R8_SNORM:
131 case Format::A8B8G8R8_USCALED:
132 case Format::A8B8G8R8_SSCALED:
133 case Format::A8B8G8R8_UINT:
134 case Format::A8B8G8R8_SINT:
135 case Format::A8B8G8R8_SRGB:
136 case Format::A2R10G10B10_UNORM:
137 case Format::A2R10G10B10_SNORM:
138 case Format::A2R10G10B10_USCALED:
139 case Format::A2R10G10B10_SSCALED:
140 case Format::A2R10G10B10_UINT:
141 case Format::A2R10G10B10_SINT:
142 case Format::A2B10G10R10_UNORM:
143 case Format::A2B10G10R10_SNORM:
144 case Format::A2B10G10R10_USCALED:
145 case Format::A2B10G10R10_SSCALED:
146 case Format::A2B10G10R10_UINT:
147 case Format::A2B10G10R10_SINT:
148 case Format::R16G16_UNORM:
149 case Format::R16G16_SNORM:
150 case Format::R16G16_USCALED:
151 case Format::R16G16_SSCALED:
152 case Format::R16G16_UINT:
153 case Format::R16G16_SINT:
154 case Format::R16G16_SFLOAT:
155 case Format::R32_UINT:
156 case Format::R32_SINT:
157 case Format::R32_SFLOAT:
158 case Format::B10G11R11_UFLOAT:
159 case Format::E5B9G9R9_UFLOAT:
160 case Format::X8_D24_UNORM:
161 case Format::D32_SFLOAT:
162 case Format::D24_UNORM_S8_UINT:
163 return 4;
164 case Format::R16G16B16_UNORM:
165 case Format::R16G16B16_SNORM:
166 case Format::R16G16B16_USCALED:
167 case Format::R16G16B16_SSCALED:
168 case Format::R16G16B16_UINT:
169 case Format::R16G16B16_SINT:
170 case Format::R16G16B16_SFLOAT:
171 return 6;
172 case Format::R16G16B16A16_UNORM:
173 case Format::R16G16B16A16_SNORM:
174 case Format::R16G16B16A16_USCALED:
175 case Format::R16G16B16A16_SSCALED:
176 case Format::R16G16B16A16_UINT:
177 case Format::R16G16B16A16_SINT:
178 case Format::R16G16B16A16_SFLOAT:
179 case Format::R32G32_UINT:
180 case Format::R32G32_SINT:
181 case Format::R32G32_SFLOAT:
182 case Format::R64_UINT:
183 case Format::R64_SINT:
184 case Format::R64_SFLOAT:
185 case Format::D32_SFLOAT_S8_UINT:
186 case Format::BC1_RGB_UNORM:
187 case Format::BC1_RGB_SRGB:
188 case Format::BC1_RGBA_UNORM:
189 case Format::BC1_RGBA_SRGB:
190 case Format::BC4_UNORM:
191 case Format::BC4_SNORM:
192 return 8;
193 case Format::R32G32B32_UINT:
194 case Format::R32G32B32_SINT:
195 case Format::R32G32B32_SFLOAT:
196 return 12;
197 case Format::R32G32B32A32_UINT:
198 case Format::R32G32B32A32_SINT:
199 case Format::R32G32B32A32_SFLOAT:
200 case Format::R64G64_UINT:
201 case Format::R64G64_SINT:
202 case Format::R64G64_SFLOAT:
203 case Format::BC2_UNORM:
204 case Format::BC2_SRGB:
205 case Format::BC3_UNORM:
206 case Format::BC3_SRGB:
207 case Format::BC5_UNORM:
208 case Format::BC5_SNORM:
209 case Format::BC6H_UFLOAT:
210 case Format::BC6H_SFLOAT:
211 case Format::BC7_UNORM:
212 case Format::BC7_SRGB:
213 return 16;
214 case Format::R64G64B64_UINT:
215 case Format::R64G64B64_SINT:
216 case Format::R64G64B64_SFLOAT:
217 return 24;
218 case Format::R64G64B64A64_UINT:
219 case Format::R64G64B64A64_SINT:
220 case Format::R64G64B64A64_SFLOAT:
221 return 32;
222 default:
223 break;
224 }
225 throw base::BadArgumentException("Unsupported texel format.");
226 }
227}