|
4 | 4 | #include <bx/string.h> |
5 | 5 | #include <cassert> |
6 | 6 | #include <stdarg.h> |
| 7 | +#include <stdexcept> |
7 | 8 |
|
8 | 9 | namespace Babylon::Graphics |
9 | 10 | { |
@@ -85,25 +86,47 @@ namespace Babylon::Graphics |
85 | 86 | { |
86 | 87 | } |
87 | 88 |
|
88 | | - void BgfxCallback::screenShot(const char* /*filePath*/, uint32_t width, uint32_t height, uint32_t pitch, const void* data, uint32_t /*size*/, bool yflip) |
| 89 | + void BgfxCallback::screenShot(const char* /*filePath*/, uint32_t width, uint32_t height, uint32_t pitch, bgfx::TextureFormat::Enum format, const void* data, uint32_t /*size*/, bool yflip) |
89 | 90 | { |
90 | 91 | assert(!m_screenShotCallbacks.empty()); // addScreenShotCallback not called before doing the screenshot call on bgfx |
91 | 92 |
|
92 | 93 | std::vector<uint8_t> array(width * height * 4); // do not use pitch to define output size because it's padded |
93 | 94 | uint8_t* bitmap{array.data()}; |
94 | 95 |
|
95 | | - for (uint32_t py = 0; py < height; py++) |
| 96 | + if (format == bgfx::TextureFormat::BGRA8) |
96 | 97 | { |
97 | | - const uint8_t* ptr = static_cast<const uint8_t*>(data) + (yflip ? (height - py - 1) : py) * pitch; |
98 | | - for (uint32_t px = 0; px < width; px++) |
| 98 | + for (uint32_t py = 0; py < height; py++) |
99 | 99 | { |
100 | | - // bgfx screenshot is BGRA |
101 | | - *bitmap++ = ptr[px * 4 + 2]; |
102 | | - *bitmap++ = ptr[px * 4 + 1]; |
103 | | - *bitmap++ = ptr[px * 4 + 0]; |
104 | | - *bitmap++ = ptr[px * 4 + 3]; |
| 100 | + const uint8_t* ptr = static_cast<const uint8_t*>(data) + (yflip ? (height - py - 1) : py) * pitch; |
| 101 | + for (uint32_t px = 0; px < width; px++) |
| 102 | + { |
| 103 | + // bgfx screenshot is BGRA |
| 104 | + *bitmap++ = ptr[px * 4 + 2]; |
| 105 | + *bitmap++ = ptr[px * 4 + 1]; |
| 106 | + *bitmap++ = ptr[px * 4 + 0]; |
| 107 | + *bitmap++ = ptr[px * 4 + 3]; |
| 108 | + } |
105 | 109 | } |
106 | 110 | } |
| 111 | + else if (format == bgfx::TextureFormat::RGBA8) |
| 112 | + { |
| 113 | + for (uint32_t py = 0; py < height; py++) |
| 114 | + { |
| 115 | + const uint8_t* ptr = static_cast<const uint8_t*>(data) + (yflip ? (height - py - 1) : py) * pitch; |
| 116 | + for (uint32_t px = 0; px < width; px++) |
| 117 | + { |
| 118 | + // bgfx screenshot is RGBA |
| 119 | + *bitmap++ = ptr[px * 4 + 0]; |
| 120 | + *bitmap++ = ptr[px * 4 + 1]; |
| 121 | + *bitmap++ = ptr[px * 4 + 2]; |
| 122 | + *bitmap++ = ptr[px * 4 + 3]; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + throw std::runtime_error{"Unsupported format for screenshot"}; |
| 129 | + } |
107 | 130 |
|
108 | 131 | m_screenShotCallbacks.front()(std::move(array)); |
109 | 132 | m_screenShotCallbacks.pop(); |
|
0 commit comments