Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions runtime/include/bozo/render/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Texture> 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<uint32_t>(m_building.vertices.size()); }
void drawRange(const Material &material, const float matrix[16], uint32_t first, uint32_t count,
std::shared_ptr<const Texture> 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<const Texture> texture);
Expand Down
37 changes: 35 additions & 2 deletions runtime/render/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vertex> 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<const Texture> texture)
{
if (count == 0u)
return;
if (static_cast<size_t>(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) ||
Expand Down Expand Up @@ -311,7 +345,7 @@ namespace bozo::render
if(material.imageProcessing)batch.material.imageProcessing=std::make_shared<const Material::ImageProcessing>(*material.imageProcessing);
std::memcpy(batch.matrix, matrix, sizeof(batch.matrix));
batch.texture = std::move(texture);
batch.first = static_cast<uint32_t>(m_building.vertices.size());
batch.first = first;
batch.count = count;
batch.key = m_key;
batch.base = m_key != 0u ? m_keyBase : 0u;
Expand All @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
90 changes: 90 additions & 0 deletions tests/render/draw_in_place_tests.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <cstring>
#include <stdexcept>
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;
}
Loading