Fix pack alignment handling in screen capture

Some incorrect arithmetic (applying padding to each pixel instead
of each _row_ of pixels) caused the pixel data to be interpreted
incorrectly. This was both in the GL2 RHI driver for read_pixels
and in the ScreenCapture module.

Fixes KartKrew/RingRacers#41
This commit is contained in:
Eidolon 2024-04-30 14:35:51 -05:00
parent d90a566ad7
commit 26fe683625
2 changed files with 3 additions and 3 deletions

View file

@ -34,7 +34,7 @@ void ScreenshotPass::capture(Rhi& rhi, Handle<GraphicsContext> ctx)
packed_data_.clear();
// Pixel data must be in pack alignment (4) so a stride of non-multiple 4 must align to 4
uint32_t stride = width_ * 3;
uint32_t read_stride = ((width_ + (kPixelRowPackAlignment - 1)) & ~(kPixelRowPackAlignment - 1)) * 3;
uint32_t read_stride = ((stride + (kPixelRowPackAlignment - 1)) & ~(kPixelRowPackAlignment - 1));
pixel_data_.resize(read_stride * height_); // 3 bytes per pixel for RGB8
packed_data_.resize(stride * height_);

View file

@ -1759,9 +1759,9 @@ void Gl2Rhi::read_pixels(Handle<GraphicsContext> ctx, const Rect& rect, PixelFor
GLint size = std::get<2>(gl_format);
// Pack alignment comes into play.
uint32_t pack_aligned_w = (rect.w + (kPixelRowPackAlignment - 1)) & ~(kPixelRowPackAlignment - 1);
uint32_t pack_stride = (rect.w * size + (kPixelRowPackAlignment - 1)) & ~(kPixelRowPackAlignment - 1);
SRB2_ASSERT(out.size_bytes() == pack_aligned_w * rect.h * size);
SRB2_ASSERT(out.size_bytes() == pack_stride * rect.h);
bool is_back;
Rect src_dim;