Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
gpu.cpp
Go to the documentation of this file.
1#include <haio_util.hpp>
2
3namespace Haio::Util::GPU {
4
6 if (size.width <= 0 || size.height <= 0) {
7 return std::unexpected(Error{ErrorCode::InvalidInput, "gpu container requires positive dimensions"});
8 }
9
10 const auto pixels = static_cast<size_t>(size.width) * static_cast<size_t>(size.height);
11 switch (color) {
12 case Color::ETC1: return etc1Size(size);
13 case Color::RGB565: return pixels * 2;
14 case Color::RGBA8888: return pixels * 4;
15 default: break;
16 }
17 return std::unexpected(Error{ErrorCode::UnsupportedFormat, "unsupported gpu payload colour"});
18}
19
21 HAIO_TRY(bytes, payloadSize(color, size));
22 switch (color) {
23 case Color::ETC1:
25 case Color::RGB565:
27 pvrRaw('r', 'g', 'b', 0, 5, 6, 5, 0), bytes};
28 default:
30 pvrRaw('r', 'g', 'b', 'a', 8, 8, 8, 8), bytes};
31 }
32}
33
34uint32_t typeSize(const Profile& profile) {
35 return profile.glType == GL_UNSIGNED_SHORT_5_6_5 ? 2u : 1u;
36}
37
38Result<Color> colorFromGL(uint32_t glType, uint32_t glFormat, uint32_t glInternal) {
39 if (glType == 0 && glFormat == 0 && glInternal == GL_ETC1_RGB8_OES) return Color::ETC1;
40 if (glType == GL_UNSIGNED_SHORT_5_6_5 && glFormat == GL_RGB && glInternal == GL_RGB565) return Color::RGB565;
41 if (glType == GL_UNSIGNED_BYTE && glFormat == GL_RGBA && glInternal == GL_RGBA8) return Color::RGBA8888;
42 return std::unexpected(Error{ErrorCode::UnsupportedFormat, "unsupported ktx payload colour"});
43}
44
45Result<Color> colorFromVK(uint32_t format) {
47 if (format == VK_FORMAT_R5G6B5_UNORM_PACK16) return Color::RGB565;
48 if (format == VK_FORMAT_R8G8B8A8_UNORM) return Color::RGBA8888;
49 return std::unexpected(Error{ErrorCode::UnsupportedFormat, "unsupported ktx2 payload colour"});
50}
51
52Result<Color> colorFromPVR(uint64_t format) {
53 if (format == 6) return Color::ETC1;
54 if (format == pvrRaw('r', 'g', 'b', 0, 5, 6, 5, 0)) return Color::RGB565;
55 if (format == pvrRaw('r', 'g', 'b', 'a', 8, 8, 8, 8)) return Color::RGBA8888;
56 return std::unexpected(Error{ErrorCode::UnsupportedFormat, "unsupported pvr payload colour"});
57}
58
59}
#define HAIO_TRY(name, expr)
constexpr uint64_t pvrRaw(char a, char b, char c, char d, uint8_t ba, uint8_t bb, uint8_t bc, uint8_t bd)
Result< Profile > profileFor(Color color, Size size)
Definition gpu.cpp:20
constexpr uint32_t GL_RGB565
Definition haio_util.hpp:99
Result< Color > colorFromPVR(uint64_t format)
Definition gpu.cpp:52
constexpr uint32_t GL_UNSIGNED_BYTE
Definition haio_util.hpp:94
constexpr uint32_t GL_ETC1_RGB8_OES
Result< Color > colorFromGL(uint32_t glType, uint32_t glFormat, uint32_t glInternal)
Definition gpu.cpp:38
constexpr uint32_t VK_FORMAT_R5G6B5_UNORM_PACK16
constexpr size_t etc1Size(Size size)
uint32_t typeSize(const Profile &profile)
Definition gpu.cpp:34
constexpr uint32_t VK_FORMAT_R8G8B8A8_UNORM
constexpr uint32_t GL_RGB
Definition haio_util.hpp:96
Result< Color > colorFromVK(uint32_t format)
Definition gpu.cpp:45
constexpr uint32_t GL_UNSIGNED_SHORT_5_6_5
Definition haio_util.hpp:95
constexpr uint32_t VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
constexpr uint32_t GL_RGBA8
Definition haio_util.hpp:98
Result< size_t > payloadSize(Color color, Size size)
Definition gpu.cpp:5
constexpr uint32_t GL_RGBA
Definition haio_util.hpp:97
std::expected< T, Error > Result