From 2903dbe686d775d66de476e56658b6bf599c1337 Mon Sep 17 00:00:00 2001 From: Thomas Martin Date: Tue, 24 Nov 2026 13:38:17 +0100 Subject: [PATCH] Renderer: draws assembled in place (appendVertices, drawRange, continueFrame) A port that makes its draws' vertices itself (Shinobido's GS translator: 30,000 primitives a frame) built them in its own batches and draw() copied every one into the frame being built at the flip. Now it can make them straight into that frame: appendVertices gives room at its end, drawRange adds a batch over vertices already there (two batches may share a range, as a two-pass draw's do), and continueFrame starts the frame like beginFrame but keeps the vertices appended since the last one ended. draw() is append + drawRange (a refused batch keeps none of its vertices). tests/render/draw_in_place_tests.cpp: the same frame digest as draw()'s copies, shared ranges, a range past the vertices refused, beginFrame dropping what was appended. Co-Authored-By: Claude Opus 5.5 --- runtime/include/bozo/render/renderer.h | 10 +++ runtime/render/renderer.cpp | 37 ++++++++++- tests/CMakeLists.txt | 6 ++ tests/render/draw_in_place_tests.cpp | 90 ++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 tests/render/draw_in_place_tests.cpp diff --git a/runtime/include/bozo/render/renderer.h b/runtime/include/bozo/render/renderer.h index 0848f24..8c44796 100644 --- a/runtime/include/bozo/render/renderer.h +++ b/runtime/include/bozo/render/renderer.h @@ -249,6 +249,16 @@ namespace bozo::render // The same with the texture handed over, decoded by the port from its game's own data. void draw(const Material &material, const float matrix[16], const Vertex *vertices, uint32_t count, std::shared_ptr texture); + // Draws a port assembles in place (Shinobido's GS translator), without the copy draw() makes: appendVertices + // gives room for `count` more vertices at the end of the frame being built (the pointer good until the next + // append), builtVertices how many it holds; drawRange adds a batch over vertices already there (first, count: + // two batches may share a range, as a two-pass draw's do). continueFrame starts the frame as beginFrame does + // but keeps the vertices appended since the last frame ended (beginFrame empties them). + Vertex *appendVertices(uint32_t count); + uint32_t builtVertices() const { return static_cast(m_building.vertices.size()); } + void drawRange(const Material &material, const float matrix[16], uint32_t first, uint32_t count, + std::shared_ptr texture); + void continueFrame(const Screen &screen); // A port's 2D drawn natively: screen-space triangles (the GS's primitive coordinates over 16) over the frame, in // submission order, without depth; publishOverlay at the game frame's end shows them until the next publish. void drawOverlay(const Material &material, const Vertex *vertices, uint32_t count, std::shared_ptr texture); diff --git a/runtime/render/renderer.cpp b/runtime/render/renderer.cpp index d0d9612..1a355a8 100644 --- a/runtime/render/renderer.cpp +++ b/runtime/render/renderer.cpp @@ -272,6 +272,40 @@ namespace bozo::render { if (count == 0u) return; + const uint32_t first = builtVertices(); + m_building.vertices.insert(m_building.vertices.end(), vertices, vertices + count); + try + { + drawRange(material, matrix, first, count, std::move(texture)); + } + catch (...) + { + m_building.vertices.resize(first); // a batch refused: its vertices not kept + throw; + } + } + + Vertex *Renderer::appendVertices(uint32_t count) + { + const size_t at = m_building.vertices.size(); + m_building.vertices.resize(at + count); + return m_building.vertices.data() + at; + } + + void Renderer::continueFrame(const Screen &screen) + { + std::vector kept = std::move(m_building.vertices); + startBuilding(screen); + m_building.vertices = std::move(kept); + } + + void Renderer::drawRange(const Material &material, const float matrix[16], uint32_t first, uint32_t count, + std::shared_ptr texture) + { + if (count == 0u) + return; + if (static_cast(first) + count > m_building.vertices.size()) + throw std::invalid_argument("native draw range past the frame's vertices"); if (material.lines && (count % 2u != 0u || material.imageProcessing)) throw std::invalid_argument("invalid native line batch"); if ((material.targetWidth==0)!=(material.targetHeight==0) || @@ -311,7 +345,7 @@ namespace bozo::render if(material.imageProcessing)batch.material.imageProcessing=std::make_shared(*material.imageProcessing); std::memcpy(batch.matrix, matrix, sizeof(batch.matrix)); batch.texture = std::move(texture); - batch.first = static_cast(m_building.vertices.size()); + batch.first = first; batch.count = count; batch.key = m_key; batch.base = m_key != 0u ? m_keyBase : 0u; @@ -323,7 +357,6 @@ namespace bozo::render } std::memcpy(batch.clip, m_clip, sizeof(batch.clip)); batch.group = m_streamDraws ? m_group : 0u; - m_building.vertices.insert(m_building.vertices.end(), vertices, vertices + count); m_building.batches.push_back(batch); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cec1f64..a2621e3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -132,6 +132,12 @@ if(MINGW) target_link_options(bozo-colour-level-tests PRIVATE -static) endif() add_test(NAME native_colour_levels COMMAND bozo-colour-level-tests) +add_executable(bozo-draw-in-place-tests render/draw_in_place_tests.cpp) +target_link_libraries(bozo-draw-in-place-tests PRIVATE bozo_render) +if(MINGW) + target_link_options(bozo-draw-in-place-tests PRIVATE -static) +endif() +add_test(NAME native_draw_in_place COMMAND bozo-draw-in-place-tests) add_executable(bozo-image-processing-tests render/image_processing_tests.cpp) target_link_libraries(bozo-image-processing-tests PRIVATE bozo_render) if(MINGW) diff --git a/tests/render/draw_in_place_tests.cpp b/tests/render/draw_in_place_tests.cpp new file mode 100644 index 0000000..fbe1089 --- /dev/null +++ b/tests/render/draw_in_place_tests.cpp @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: MIT +// Draws assembled in place (Renderer::appendVertices, drawRange, continueFrame): the same frame as draw()'s copies, two +// batches over one range, a range past the vertices refused, and a refused draw() keeping none of its vertices. +#include "bozo/render/renderer.h" +#include +#include +#include +using namespace bozo::render; +int main() +{ + auto &r = Renderer::instance(); + r.setEnabled(true); + const float identity[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + unsigned failures = 0; + Vertex tri[6]{}; + for (int i = 0; i < 6; ++i) + { + tri[i].x = float(i * 10); + tri[i].y = float(i * 7); + tri[i].rgba = 0x80808080u + i; + } + Material a, b; + b.depthTest = true; + + // draw(): two batches, the second over the same three vertices again (a copy) + r.beginFrame(Screen{}); + r.draw(a, identity, tri, 6); + r.draw(b, identity, tri, 3); + r.endFrame(); + const auto copied = r.takeFrameDraws().hash; + + // in place: the vertices appended before the frame starts (continueFrame keeps them), the same batches + Vertex *at = r.appendVertices(6); + std::memcpy(at, tri, sizeof(tri)); + at = r.appendVertices(3); + std::memcpy(at, tri, 3 * sizeof(Vertex)); + if (r.builtVertices() != 9) + ++failures; + r.continueFrame(Screen{}); + if (r.builtVertices() != 9) + ++failures; + r.drawRange(a, identity, 0, 6, nullptr); + r.drawRange(b, identity, 6, 3, nullptr); + r.endFrame(); + if (r.takeFrameDraws().hash != copied) + ++failures; + + // two batches over one range (a two-pass draw), and a range past the vertices refused + r.appendVertices(3); + r.continueFrame(Screen{}); + r.drawRange(a, identity, 0, 3, nullptr); + r.drawRange(b, identity, 0, 3, nullptr); + bool refused = false; + try + { + r.drawRange(a, identity, 1, 3, nullptr); + } + catch (const std::invalid_argument &) + { + refused = true; + } + if (!refused) + ++failures; + r.endFrame(); + r.takeFrameDraws(); + if (r.published()->batches.size() != 2) + ++failures; + + // beginFrame drops what was appended; a draw() refused keeps none of its vertices + r.appendVertices(3); + r.beginFrame(Screen{}); + if (r.builtVertices() != 0) + ++failures; + Material bad; + bad.lines = true; + refused = false; + try + { + r.draw(bad, identity, tri, 3); // lines: an odd count + } + catch (const std::invalid_argument &) + { + refused = true; + } + if (!refused || r.builtVertices() != 0) + ++failures; + r.endFrame(); + std::printf("native draws in place: %s\n", failures ? "FAILED" : "passed"); + return failures ? 1 : 0; +}