Files
BozoEngine/tests/render/picture_readback_gpu_tests.cpp
Thomas MartinandClaude Opus 5.5 bfac857af0 Renderer: readPicture, a game's read of its presented picture
Steambot Chronicles' menus are drawn over a half-size copy of the screen
(sceGsExecStoreImage of the frame buffer). Renderer::readPicture asks the
next present for a rectangle of the frame's screen, read back with one
ReadPixels after the picture is drawn and before the window draws over it,
box-averaged to the size asked for (RGBA, rows top-down, alpha 0x80). It
waits up to a timeout for that present and answers false at once when
nothing has presented in the last second (headless).

GPU test (BOZO_APP, label gpu): a hidden window presents red and blue
halves while another thread reads them back at 4 x 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 16:01:28 +01:00

108 lines
3.9 KiB
C++

// SPDX-License-Identifier: MIT
// Renderer::readPicture: a game's read of its frame back (a menu over a copy of the screen). Hidden video-only window.
#include "bozo/render/gl.h"
#include "bozo/render/renderer.h"
#include <SDL3/SDL.h>
#include <atomic>
#include <cstdio>
#include <cstdlib>
#include <thread>
#include <vector>
using namespace bozo::render;
int main()
{
if (!SDL_Init(SDL_INIT_VIDEO))
return 1;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
auto *window = SDL_CreateWindow("Bozo picture readback tests", 64, 48, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
if (!window)
return 1;
const auto context = SDL_GL_CreateContext(window);
if (!context || !gl::load([](const char *s) { return reinterpret_cast<void *>(SDL_GL_GetProcAddress(s)); }))
return 1;
auto &r = Renderer::instance();
r.setEnabled(true);
r.setPresentsAlone(true);
r.setClearColour(0);
unsigned failures = 0;
std::vector<uint32_t> picture;
// Nothing presented yet: no picture, at once.
if (r.readPicture(0, 0, 64, 48, 4, 2, picture, 2000))
{
std::printf("FAIL a picture before any present\n");
++failures;
}
// The left half red, the right half blue (0x80 is full: the frame's colours are doubled on the way to the window).
const Screen screen{0, 0, 64, 48};
const float id[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
Material plain;
plain.textured = false;
plain.depthTest = false;
plain.depthWrite = false;
plain.blend = Material::Blend::Opaque;
const auto frame = [&] {
r.beginFrame(screen);
const float xy[6][2] = {{0, 0}, {1, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 1}};
for (int half = 0; half < 2; ++half)
{
Vertex v[6];
for (unsigned i = 0; i < 6; ++i)
{
v[i].x = static_cast<float>(half * 32) + xy[i][0] * 32.0f;
v[i].y = xy[i][1] * 48.0f;
v[i].z = 0;
v[i].rgba = half ? 0x80800000u : 0x80000080u;
}
r.draw(plain, id, v, 6);
}
r.endFrame();
};
const auto presentOnce = [&] {
frame();
gl::api().Viewport(0, 0, 64, 48);
r.present(0, 0, 64, 48);
SDL_GL_SwapWindow(window);
};
presentOnce(); // the window presents: a read now waits for the next present
std::atomic<bool> done{false}, read{false};
std::thread reader([&] {
read = r.readPicture(0, 0, 64, 48, 4, 2, picture, 2000);
done = true;
});
for (int presents = 0; !done && presents < 400; ++presents)
{
presentOnce();
SDL_Delay(5);
}
reader.join();
const auto near = [](uint32_t c, uint32_t want) {
for (int k = 0; k < 4; ++k)
if (std::abs(static_cast<int>((c >> (8 * k)) & 0xffu) - static_cast<int>((want >> (8 * k)) & 0xffu)) > 4)
return false;
return true;
};
if (!read || picture.size() != 8)
{
std::printf("FAIL no picture read back (%d, %zu)\n", read ? 1 : 0, picture.size());
++failures;
}
else
for (int y = 0; y < 2; ++y)
for (int x = 0; x < 4; ++x)
{
const uint32_t want = x < 2 ? 0x800000ffu : 0x80ff0000u; // red left, blue right; alpha 0x80
if (!near(picture[static_cast<size_t>(y) * 4 + static_cast<size_t>(x)], want))
{
std::printf("FAIL picture %d,%d %08x, expected %08x\n", x, y, picture[static_cast<size_t>(y) * 4 + static_cast<size_t>(x)],
want);
++failures;
}
}
SDL_GL_DestroyContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return failures ? 1 : 0;
}