// SPDX-License-Identifier: MIT // Renderer::readImage: a game's read of an auxiliary image back (a copy of the frame so far, taken in the middle of a // frame that is presented faded: Breath of Fire: Dragon Quarter's memory card picture). Hidden video-only window. #include "bozo/render/gl.h" #include "bozo/render/renderer.h" #include #include #include #include #include #include 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 image 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(SDL_GL_GetProcAddress(s)); })) return 1; auto &r = Renderer::instance(); r.setEnabled(true); r.setPresentsAlone(true); r.setClearColour(0); unsigned failures = 0; std::vector image; int width = 0, height = 0; // Nothing presented yet: no image, at once. if (r.readImage(2, width, height, image, 2000)) { std::printf("FAIL an image before any present\n"); ++failures; } 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 quad = [&](const Material &m, uint32_t colour, float x, float y, float w, float h) { const float xy[6][2] = {{0, 0}, {1, 0}, {0, 1}, {1, 0}, {1, 1}, {0, 1}}; Vertex v[6]; for (unsigned i = 0; i < 6; ++i) { v[i].x = x + xy[i][0] * w; v[i].y = y + xy[i][1] * h; v[i].z = 0; v[i].u = xy[i][0]; v[i].v = xy[i][1]; v[i].rgba = colour; } r.draw(m, id, v, 6); }; // The frame: its top half red, its bottom half blue (0x80 is full); copied whole into image 2 (8 x 6), then covered // black (a fade's last step): the window shows black, the image keeps the frame as it was at the copy. const auto frame = [&] { r.beginFrame(screen); quad(plain, 0x800000ffu, 0, 0, 64, 24); quad(plain, 0x80ff0000u, 0, 24, 64, 24); Material copy = plain; copy.target = 2; copy.targetWidth = 8; copy.targetHeight = 6; copy.fromFrame = 1; copy.source = 0; copy.nearest = true; quad(copy, 0x80808080u, 0, 0, 64, 48); quad(plain, 0x80000000u, 0, 0, 64, 48); r.endFrame(); }; const auto presentOnce = [&] { gl::api().Viewport(0, 0, 64, 48); r.present(0, 0, 64, 48); SDL_GL_SwapWindow(window); }; r.beginFrame(screen); quad(plain, 0x80000000u, 0, 0, 64, 48); r.endFrame(); presentOnce(); // the window presents (a frame without the copy): a read now waits for a frame published since frame(); // the game's frame with the copy, published: then read on the game's thread std::atomic done{false}, read{false}; std::thread reader([&] { read = r.readImage(2, width, height, image, 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((c >> (8 * k)) & 0xffu) - static_cast((want >> (8 * k)) & 0xffu)) > 4) return false; return true; }; if (!read || width != 8 || height != 6 || image.size() != 48) { std::printf("FAIL no image read back (%d, %dx%d, %zu)\n", read ? 1 : 0, width, height, image.size()); ++failures; } else for (int y = 0; y < 6; ++y) for (int x = 0; x < 8; ++x) { const uint32_t want = y < 3 ? 0x800000ffu : 0x80ff0000u; // red on top, blue below; alpha 0x80 const uint32_t got = image[static_cast(y) * 8 + static_cast(x)]; if (!near(got, want)) { std::printf("FAIL image %d,%d %08x, expected %08x\n", x, y, got, want); ++failures; } } // The window's picture is the faded frame (black): what readPicture gives, and why readImage is needed. std::vector picture; std::atomic pictureDone{false}, pictureRead{false}; std::thread pictureReader([&] { pictureRead = r.readPicture(0, 0, 64, 48, 4, 2, picture, 2000); pictureDone = true; }); for (int presents = 0; !pictureDone && presents < 400; ++presents) { presentOnce(); SDL_Delay(5); } pictureReader.join(); if (!pictureRead || picture.size() != 8 || !near(picture[0], 0x80000000u)) { std::printf("FAIL the presented picture is not the faded frame (%d, %zu, %08x)\n", pictureRead ? 1 : 0, picture.size(), picture.empty() ? 0u : picture[0]); ++failures; } // An image no frame drew into: false (served by a present, not a timeout). std::atomic neverDone{false}, neverRead{true}; std::thread neverReader([&] { neverRead = r.readImage(5, width, height, image, 2000); neverDone = true; }); int neverPresents = 0; for (; !neverDone && neverPresents < 400; ++neverPresents) { presentOnce(); SDL_Delay(5); } neverReader.join(); if (neverRead || neverPresents > 100) { std::printf("FAIL an image never drawn read back\n"); ++failures; } SDL_GL_DestroyContext(context); SDL_DestroyWindow(window); SDL_Quit(); if (failures) std::printf("%u failures\n", failures); else std::printf("image readback: ok\n"); return failures ? 1 : 0; }