Skip to content

Commit

Permalink
Core: Allow more formats for screenshot.
Browse files Browse the repository at this point in the history
This allows depth and stencil formats too, so we can use for debugger
export.
  • Loading branch information
unknownbrackets committed Dec 22, 2017
1 parent 092cbc2 commit b56aceb
Showing 1 changed file with 126 additions and 52 deletions.
178 changes: 126 additions & 52 deletions Core/Screenshot.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -125,33 +125,129 @@ static bool WriteScreenshotToPNG(png_imagep image, const char *filename, int con
} }
#endif #endif


const u8 *ConvertBufferTo888RGB(const GPUDebugBuffer &buf, u8 *&temp, u32 &w, u32 &h) { static bool ConvertPixelTo888RGB(GPUDebugBufferFormat fmt, u8 &r, u8 &g, u8 &b, const void *buffer, int offset, bool rev) {
// The temp buffer will be freed by the caller if set, and can be the return value. const u8 *buf8 = (const u8 *)buffer;
temp = nullptr; const u16 *buf16 = (const u16 *)buffer;
const u32 *buf32 = (const u32 *)buffer;
const float *fbuf = (const float *)buffer;

u32 src;
double fsrc;
switch (fmt) {
case GPU_DBG_FORMAT_565:
src = buf16[offset];
if (rev) {
src = bswap16(src);
}
r = Convert5To8((src >> 0) & 0x1F);
g = Convert6To8((src >> 5) & 0x3F);
b = Convert5To8((src >> 11) & 0x1F);
break;
case GPU_DBG_FORMAT_5551:
src = buf16[offset];
if (rev) {
src = bswap16(src);
}
r = Convert5To8((src >> 0) & 0x1F);
g = Convert5To8((src >> 5) & 0x1F);
b = Convert5To8((src >> 10) & 0x1F);
break;
case GPU_DBG_FORMAT_4444:
src = buf16[offset];
if (rev) {
src = bswap16(src);
}
r = Convert4To8((src >> 0) & 0xF);
g = Convert4To8((src >> 4) & 0xF);
b = Convert4To8((src >> 8) & 0xF);
break;
case GPU_DBG_FORMAT_8888:
src = buf32[offset];
if (rev) {
src = bswap32(src);
}
r = (src >> 0) & 0xFF;
g = (src >> 8) & 0xFF;
b = (src >> 16) & 0xFF;
break;
case GPU_DBG_FORMAT_FLOAT:
fsrc = fbuf[offset];
r = fsrc >= 1.0 ? 255 : (fsrc < 0.0 ? 0 : (int)(fsrc * 255.0));
g = 0;
b = 0;
break;
case GPU_DBG_FORMAT_16BIT:
src = buf16[offset];
r = src >> 8;
g = 0;
b = 0;
break;
case GPU_DBG_FORMAT_8BIT:
src = buf8[offset];
r = src;
g = 0;
b = 0;
break;
case GPU_DBG_FORMAT_24BIT_8X:
src = buf32[offset];
r = (src >> 16) & 0xFF;
g = 0;
b = 0;
break;
case GPU_DBG_FORMAT_24X_8BIT:
src = buf32[offset];
r = (src >> 24) & 0xFF;
g = 0;
b = 0;
break;
case GPU_DBG_FORMAT_24BIT_8X_DIV_256:
src = buf32[offset]& 0x00FFFFFF;
src = src - 0x800000 + 0x8000;
r = (src >> 8) & 0xFF;
g = 0;
b = 0;
break;
case GPU_DBG_FORMAT_FLOAT_DIV_256:
fsrc = fbuf[offset];
src = (int)(fsrc * 16777215.0);
src = src - 0x800000 + 0x8000;
r = (src >> 8) & 0xFF;
g = 0;
b = 0;
break;
default:
_assert_msg_(SYSTEM, false, "Unsupported framebuffer format for screenshot: %d", fmt);
return false;
}

return true;
}


const u8 *ConvertBufferTo888RGB(const GPUDebugBuffer &buf, u8 *&temp, u32 &w, u32 &h) {
w = std::min(w, buf.GetStride()); w = std::min(w, buf.GetStride());
h = std::min(h, buf.GetHeight()); h = std::min(h, buf.GetHeight());


// The temp buffer will be freed by the caller if set, and can be the return value.
if (buf.GetFlipped() || buf.GetFormat() != GPU_DBG_FORMAT_888_RGB) {
temp = new u8[3 * w * h];
} else {
temp = nullptr;
}

const u8 *buffer = buf.GetData(); const u8 *buffer = buf.GetData();
if (buf.GetFlipped() && buf.GetFormat() == GPU_DBG_FORMAT_888_RGB) { if (buf.GetFlipped() && buf.GetFormat() == GPU_DBG_FORMAT_888_RGB) {
// Silly OpenGL reads upside down, we flip to another buffer for simplicity. // Silly OpenGL reads upside down, we flip to another buffer for simplicity.
temp = new u8[3 * w * h];
for (u32 y = 0; y < h; y++) { for (u32 y = 0; y < h; y++) {
memcpy(temp + y * w * 3, buffer + (buf.GetHeight() - y - 1) * buf.GetStride() * 3, w * 3); memcpy(temp + y * w * 3, buffer + (buf.GetHeight() - y - 1) * buf.GetStride() * 3, w * 3);
} }
buffer = temp; } else if (buf.GetFormat() < GPU_DBG_FORMAT_FLOAT) {
} else if (buf.GetFormat() != GPU_DBG_FORMAT_888_RGB) {
// Let's boil it down to how we need to interpret the bits. // Let's boil it down to how we need to interpret the bits.
int baseFmt = buf.GetFormat() & ~(GPU_DBG_FORMAT_REVERSE_FLAG | GPU_DBG_FORMAT_BRSWAP_FLAG); int baseFmt = buf.GetFormat() & ~(GPU_DBG_FORMAT_REVERSE_FLAG | GPU_DBG_FORMAT_BRSWAP_FLAG);
bool rev = (buf.GetFormat() & GPU_DBG_FORMAT_REVERSE_FLAG) != 0; bool rev = (buf.GetFormat() & GPU_DBG_FORMAT_REVERSE_FLAG) != 0;
bool brswap = (buf.GetFormat() & GPU_DBG_FORMAT_BRSWAP_FLAG) != 0; bool brswap = (buf.GetFormat() & GPU_DBG_FORMAT_BRSWAP_FLAG) != 0;
bool flip = buf.GetFlipped(); bool flip = buf.GetFlipped();


temp = new u8[3 * w * h];

// This is pretty inefficient. // This is pretty inefficient.
const u16 *buf16 = (const u16 *)buffer;
const u32 *buf32 = (const u32 *)buffer;
for (u32 y = 0; y < h; y++) { for (u32 y = 0; y < h; y++) {
for (u32 x = 0; x < w; x++) { for (u32 x = 0; x < w; x++) {
u8 *dst; u8 *dst;
Expand All @@ -165,54 +261,32 @@ const u8 *ConvertBufferTo888RGB(const GPUDebugBuffer &buf, u8 *&temp, u32 &w, u3
u8 &g = dst[1]; u8 &g = dst[1];
u8 &b = brswap ? dst[0] : dst[2]; u8 &b = brswap ? dst[0] : dst[2];


u32 src; if (!ConvertPixelTo888RGB(GPUDebugBufferFormat(baseFmt), r, g, b, buffer, y * buf.GetStride() + x, rev)) {
switch (baseFmt) { return nullptr;
case GPU_DBG_FORMAT_565: }
src = buf16[y * buf.GetStride() + x]; }
if (rev) { }
src = bswap16(src); } else if (buf.GetFormat() != GPU_DBG_FORMAT_888_RGB) {
} bool flip = buf.GetFlipped();
r = Convert5To8((src >> 0) & 0x1F);
g = Convert6To8((src >> 5) & 0x3F); // This is pretty inefficient.
b = Convert5To8((src >> 11) & 0x1F); for (u32 y = 0; y < h; y++) {
break; for (u32 x = 0; x < w; x++) {
case GPU_DBG_FORMAT_5551: u8 *dst;
src = buf16[y * buf.GetStride() + x]; if (flip) {
if (rev) { dst = &temp[(h - y - 1) * w * 3 + x * 3];
src = bswap16(src); } else {
} dst = &temp[y * w * 3 + x * 3];
r = Convert5To8((src >> 0) & 0x1F); }
g = Convert5To8((src >> 5) & 0x1F);
b = Convert5To8((src >> 10) & 0x1F); if (!ConvertPixelTo888RGB(buf.GetFormat(), dst[0], dst[1], dst[2], buffer, y * buf.GetStride() + x, false)) {
break;
case GPU_DBG_FORMAT_4444:
src = buf16[y * buf.GetStride() + x];
if (rev) {
src = bswap16(src);
}
r = Convert4To8((src >> 0) & 0xF);
g = Convert4To8((src >> 4) & 0xF);
b = Convert4To8((src >> 8) & 0xF);
break;
case GPU_DBG_FORMAT_8888:
src = buf32[y * buf.GetStride() + x];
if (rev) {
src = bswap32(src);
}
r = (src >> 0) & 0xFF;
g = (src >> 8) & 0xFF;
b = (src >> 16) & 0xFF;
break;
default:
ERROR_LOG(G3D, "Unsupported framebuffer format for screenshot: %d", buf.GetFormat());
return nullptr; return nullptr;
} }
} }
} }
buffer = temp;
} }


return buffer; return temp ? temp : buffer;
} }


bool TakeGameScreenshot(const char *filename, ScreenshotFormat fmt, ScreenshotType type, int *width, int *height, int maxRes) { bool TakeGameScreenshot(const char *filename, ScreenshotFormat fmt, ScreenshotType type, int *width, int *height, int maxRes) {
Expand Down

0 comments on commit b56aceb

Please sign in to comment.