feat(render): Renderer with LOD selection, 3.3 and 4.3+ upload paths, headless smoke test

This commit is contained in:
Martino Ferrari committed 2026-09-27 23:03:08 +02:00
1 parent e0cdd95a3b
commit 3555ab63d7
20 files changed
+1544 -25

No files matched your search

+80 -10
View File
@@ -28,26 +28,84 @@ struct TextInstance {
static_assert(sizeof(LineInstance) == 4 * sizeof(f32), "line instance must stay 4 floats");
static_assert(sizeof(TextInstance) == 4 * sizeof(f32), "text instance must stay 4 floats");
/// How a store's staged instances reach the GPU. Semantics are identical either
/// way -- only the transfer differs, and `Renderer::init()` picks one from
/// `plot::gl::caps()`:
///
/// - `Orphan`: the GL 3.3 baseline. Every upload re-specifies the store with a
/// null `glBufferData` (orphaning it, so the driver never has to wait for the
/// draw that is still reading the previous one) and then refills it.
/// - `PersistentMapped`: GL 4.4 / ARB_buffer_storage. Three slots of immutable
/// storage are mapped once and written directly, and the fence recorded after
/// each slot's draw guards its reuse, so the render thread never waits on the
/// GPU. The vertex array's attribute pointers move with the slot.
enum class UploadMode { Orphan, PersistentMapped };
namespace detail {
/// The growth half of an instance buffer, shared by both pipelines so neither
/// duplicates the GL ownership or the "grow only when the frame needs it" rule.
/// duplicates the GL ownership or the transfer strategy. This is the one place
/// vertex data is handed to the GPU: `LineBuffer::upload` and
/// `TextBuffer::upload` both land here, and no caller of either can tell which
/// `UploadMode` is live.
class InstanceStore {
public:
/// Creates the vertex buffer. Reports `ErrorCode::Unsupported` while the GL
/// entry points are unresolved, `GLFailure` if the driver hands out no name.
bool create(Status* st = nullptr) noexcept;
/// Slots the persistent ring rotates through. Three is the smallest ring that
/// lets a frame's draw finish while the CPU fills two more.
static constexpr u32 kSlots = 3;
/// Upper bound on the wait for a slot's draw to retire: a driver that has
/// stopped making progress must not stall the render thread forever.
static constexpr u64 kFenceWaitNs = 1'000'000'000ull;
/// Uploads `count` instances of `instance_bytes`. The store only reallocates
/// when the frame's workload outgrows it, so steady-state frames write into
/// the existing allocation.
/// Creates the vertex buffer in `mode`. Reports `ErrorCode::Unsupported` while
/// the GL entry points are unresolved and `GLFailure` if the driver hands out
/// no name. A caller asking for `PersistentMapped` must already have checked
/// that the context and the entry points can do it (see Renderer::init): a
/// store created that way on a machine without them would never draw.
bool create(UploadMode mode, Status* st = nullptr) noexcept;
/// Uploads `count` instances of `instance_bytes`. `Orphan` mode orphans and
/// refills; `PersistentMapped` mode writes the next slot of the ring, waiting
/// out any draw still reading it.
void upload(const void* data, u32 count, usize instance_bytes) noexcept;
/// Records that the draw the caller just issued read the current slot, so the
/// fence guarding that slot's reuse ends up in the command stream behind the
/// draw. A no-op in `Orphan` mode, where nothing reads the store after the
/// orphan.
void mark_drawn() noexcept;
/// Byte offset the current slot's instances start at: 0 in `Orphan` mode,
/// `slot * slot_bytes` in `PersistentMapped` mode. The vertex array re-points
/// its attribute pointers whenever `epoch()` changes.
usize slot_offset() const noexcept {
return mode_ == UploadMode::PersistentMapped ? usize(slot_) * slot_bytes_ : 0u;
}
/// Changes whenever the staged instances moved (a new slot) or the GL buffer
/// was recreated, i.e. whenever attribute pointers into this store went stale.
u32 epoch() const noexcept { return epoch_; }
void bind() const noexcept { buffer_.bind(); }
bool persistent() const noexcept { return mode_ == UploadMode::PersistentMapped; }
private:
/// Grows the mapped ring to `bytes` per slot. False when the driver refuses the
/// storage or the map, in which case the store has fallen back to a fresh
/// `Orphan` buffer and the frame still draws.
bool grow_persistent(usize bytes) noexcept;
/// Deletes every fence, unmaps and releases the GL buffer. `slot_bytes_` drops
/// to 0 so the next upload regrows.
void release_storage() noexcept;
gl::Buffer buffer_;
u32 capacity_ = 0; ///< instances the store can hold
UploadMode mode_ = UploadMode::Orphan;
u32 capacity_ = 0; ///< instances the orphan store can hold
usize slot_bytes_ = 0; ///< bytes per slot of the persistent ring
u32 slot_ = kSlots - 1u; ///< last slot written; an upload advances it first
u32 epoch_ = 0;
u8* mapped_ = nullptr; ///< base of the mapped ring, null in `Orphan` mode
gl::Fence fences_[kSlots];
};
} // namespace detail
@@ -55,10 +113,16 @@ class InstanceStore {
/// Instance buffer of the line pipeline: one `LineInstance` per segment.
class LineBuffer {
public:
bool create(Status* st = nullptr) noexcept { return store_.create(st); }
bool create(Status* st = nullptr, UploadMode mode = UploadMode::Orphan) noexcept {
return store_.create(mode, st);
}
void upload(const f32* interleaved, u32 count) noexcept {
store_.upload(interleaved, count, sizeof(LineInstance));
}
void mark_drawn() noexcept { store_.mark_drawn(); }
usize slot_offset() const noexcept { return store_.slot_offset(); }
u32 epoch() const noexcept { return store_.epoch(); }
bool persistent() const noexcept { return store_.persistent(); }
void bind() const noexcept { store_.bind(); }
private:
@@ -68,10 +132,16 @@ class LineBuffer {
/// Instance buffer of the text pipeline: one `TextInstance` per glyph.
class TextBuffer {
public:
bool create(Status* st = nullptr) noexcept { return store_.create(st); }
bool create(Status* st = nullptr, UploadMode mode = UploadMode::Orphan) noexcept {
return store_.create(mode, st);
}
void upload(const f32* interleaved, u32 count) noexcept {
store_.upload(interleaved, count, sizeof(TextInstance));
}
void mark_drawn() noexcept { store_.mark_drawn(); }
usize slot_offset() const noexcept { return store_.slot_offset(); }
u32 epoch() const noexcept { return store_.epoch(); }
bool persistent() const noexcept { return store_.persistent(); }
void bind() const noexcept { store_.bind(); }
private: