23#include <unordered_map>
31 using PipelineSet = std::array<RHI::GpuPipeline, kPrimitiveKindCount>;
52 std::vector<std::byte>
cpu;
55 std::array<Stream, kPrimitiveKindCount>
streams;
102 void RenderGlyph(
tachyon::float2 pos,
tachyon::float2 size,
tachyon::float2 uvMin,
tachyon::float2 uvMax,
float scale,
int bpp,
int mode,
tachyon::VisualColorStyleIndex textColorIndex,
tachyon::IntRect clipRect,
tachyon::ImageIndex textureIndex =
tachyon::ImageIndex::Invalid,
float shadowBlur = -1.0f);
123 int32_t x0 = 0, y0 = 0, x1 = 0, y1 = 0;
125 bool Empty()
const {
return x1 <= x0 || y1 <= y0; }
126 bool Overlaps(
const ScreenBox& o)
const
128 return !Empty() && !o.Empty() && x0 < o.x1 && o.x0 < x1 && y0 < o.y1 && o.y0 < y1;
131 ScreenBox Cover(
const ScreenBox& o)
const
133 if (Empty())
return o;
134 if (o.Empty())
return *
this;
135 return { std::min(x0, o.x0), std::min(y0, o.y0), std::max(x1, o.x1), std::max(y1, o.y1) };
139 return { std::max(x0, clip.x), std::max(y0, clip.y), std::min(x1, clip.x + clip.w), std::min(y1, clip.y + clip.h) };
144 auto move = [limit](int32_t v, int32_t d)
146 return v <= -limit || v >= limit ? v : int32_t(std::clamp<int64_t>(int64_t(v) + d, -limit, limit));
148 return { move(x0, delta.
x), move(y0, delta.
y), move(x1, delta.
x), move(y1, delta.
y) };
152 ScreenBox Covered()
const;
156 std::vector<ScreenBox> passBounds;
157 ScreenBox openBounds {};
159 std::array<int, kPrimitiveKindCount> lastPass = MakeNoPasses();
160 static constexpr std::array<int, kPrimitiveKindCount> MakeNoPasses()
162 std::array<int, kPrimitiveKindCount> none {};
171 void PlaceInPass(
PrimitiveKind kind, ScreenBox bounds,
int count = 1);
174 ScreenBox Bounds(tachyon::float2 min, tachyon::float2 max,
float margin, tachyon::IntRect clipRect)
const
177 float x0, x1, y0, y1;
178 if (t.
r0.
y == 0.0f && t.
r1.
x == 0.0f)
181 const float ax = t.
r0.
x * min.
x + t.
r0.
z, bx = t.
r0.
x * max.
x + t.
r0.
z;
182 const float ay = t.
r1.
y * min.
y + t.
r1.
z, by = t.
r1.
y * max.
y + t.
r1.
z;
183 x0 = std::min(ax, bx); x1 = std::max(ax, bx);
184 y0 = std::min(ay, by); y1 = std::max(ay, by);
188 const float cx[4] = { min.
x, max.
x, min.
x, max.
x }, cy[4] = { min.
y, min.
y, max.
y, max.
y };
191 for (
int i = 0; i < 4; ++i)
193 const float x = t.
r0.
x * cx[i] + t.
r0.
y * cy[i] + t.
r0.
z, y = t.
r1.
x * cx[i] + t.
r1.
y * cy[i] + t.
r1.
z;
194 x0 = std::min(x0, x); x1 = std::max(x1, x);
195 y0 = std::min(y0, y); y1 = std::max(y1, y);
200 static constexpr float kFar = float(1 << 28);
201 auto px = [](
float v) {
return int32_t(std::clamp(v, -kFar, kFar)); };
202 const ScreenBox box{ px(std::floor(x0 - margin)), px(std::floor(y0 - margin)),
203 px(std::ceil(x1 + margin)), px(std::ceil(y1 + margin)) };
204 return box.Within(clipRect);
209 template <
typename T>
210 void Append(
PrimitiveKind kind,
const T& primitive, ScreenBox bounds)
212 static_assert(std::is_trivially_copyable_v<T>);
214 PlaceInPass(kind, bounds);
215 auto& cpu =
streams[size_t(kind)].cpu;
216 T ordered = primitive;
217 ordered.transform0.w = float(
nextOrder++);
219 const size_t at = cpu.size();
220 cpu.resize(at +
sizeof(T));
221 std::memcpy(cpu.data() + at, &ordered,
sizeof(T));
228 std::unordered_map<tachyon::ID, PrimitiveRenderTarget>
idTargets;
238 void RenderGlyph(
tachyon::float2 pos,
tachyon::float2 size,
tachyon::float2 uvMin,
tachyon::float2 uvMax,
float scale,
int bpp,
int mode,
tachyon::VisualColorStyleIndex textColorIndex,
tachyon::IntRect clipRect,
tachyon::ImageIndex textureIndex =
tachyon::ImageIndex::Invalid,
float shadowBlur = -1.0f)
override;
258 return target->second;
319 std::array<DeviceAddress, kPrimitiveKindCount> addresses{};
320 uint64_t lastUsedFrame = 0;
325 std::array<DeviceAddress, kPrimitiveKindCount> CaptureAddresses(ScopeCapture& capture,
RHI::RenderCommandList& cmdList);
327 void ReleaseResident(ScopeCapture& capture);
330 std::unordered_map<tachyon::ID, ScopeCapture> scopeCaptures;
332 std::vector<RHI::ResidentBufferId> m_releasedResidents;
335 struct ResolvedReplay
338 std::array<DeviceAddress, kPrimitiveKindCount> addresses{};
339 GPUDrawPushConstants push{};
341 std::vector<ResolvedReplay> m_resolvedReplays;
343 PrimitiveRenderTarget* m_captureSaved =
nullptr;
344 uint64_t m_frame = 0;
345 size_t m_lastUploadBytes = 0;
350 PrimitiveRenderTarget draws;
354 std::unordered_map<tachyon::ID, DrawSnapshot> m_lastDraws;
357 void NoteDraws(tachyon::ID window,
const PrimitiveRenderTarget& target);
Definition gui_renderer.h:78
virtual void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, const StyleBlend &styles, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})=0
Draws a styled box, styles mixed.
Definition gui_primitive_renderer.h:226
void RenderCircle(tachyon::float2 center, float radius, tachyon::IntRect clipRect, const tachyon::LineStyle &lineStyle) override
A circle outline lineStyle.lineWidth wide, exactly round.
size_t LastFrameUploadBytes() const
Primitive bytes the last Render uploaded with the frame; cached scopes drawn from their GPU copy add ...
Definition gui_primitive_renderer.h:304
void RenderOccluder(tachyon::IntRect rect, tachyon::IntRect clipRect) override
Draws nothing: marks rect as covered by opaque content drawn after this call, so what was drawn befor...
void EndWindow() override
bool ReplayScopeCapture(tachyon::ID scopeId, tachyon::int2 delta) override
Draws the capture of scopeId again, moved by delta pixels; false if there is none.
PrimitiveRenderTarget & GetTarget(tachyon::ID id)
Definition gui_primitive_renderer.h:250
void InvalidateScopeCapture(tachyon::ID scopeId) override
Drops the capture of scopeId.
void ClearScopeCaptures() override
Drops every capture.
std::unordered_map< tachyon::ID, PrimitiveRenderTarget > idTargets
Definition gui_primitive_renderer.h:228
void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, const tachyon::StyleBlend &styles, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={}) override
Draws a styled box, styles mixed.
bool HasScopeCapture(tachyon::ID scopeId) const override
True when scopeId has a non-empty capture.
void RenderLine(tachyon::int2 p0, tachyon::int2 p1, tachyon::int2 p2, tachyon::int2 p3, tachyon::IntRect clipRect, const tachyon::LineStyle &lineStyle) override
void RenderGlyphs(std::span< const GlyphInstance > glyphs, tachyon::float2 offset, tachyon::IntRect clipRect) override
RenderGlyph for each of glyphs, moved by offset, in order: a text's glyphs at once.
bool DrawsChanged(tachyon::ID window) const override
Whether the window's draws this frame differ from its last frame's (as of its EndWindow); true when t...
void RenderGlyph(tachyon::float2 pos, tachyon::float2 size, tachyon::float2 uvMin, tachyon::float2 uvMax, float scale, int bpp, int mode, tachyon::VisualColorStyleIndex textColorIndex, tachyon::IntRect clipRect, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, float shadowBlur=-1.0f) override
A glyph from the text atlas.
PrimitiveRenderTarget * currentTarget
Definition gui_primitive_renderer.h:229
bool StartResizing(tachyon::ID id) override
Definition gui_primitive_renderer.h:266
void RenderTriangle(tachyon::float2 p0, tachyon::float2 p1, tachyon::float2 p2, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, TriangleEdges hardEdges=TriangleEdges::None) override
A filled triangle.
void RenderShadow(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius, int blur) override
A box shadow: rect is the shadow's shape, which fades out over blur pixels on either side of its edge...
void RenderQuad(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius=0.0f, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, const tachyon::UVRect &uv={}, const tachyon::ImageDisplay &display={}) override
Draws a filled, optionally rounded and textured rect: a box without borders, style blending or elemen...
void RenderDots(std::span< const tachyon::float2 > positions, tachyon::IntRect clipRect, float size, tachyon::color4f color) override
RenderDot at each of positions, in order, all alike.
size_t ResidentScopeCount() const
Captured scopes with a GPU copy (made or on its way).
void ReleaseTarget(tachyon::ID id)
Drops the CPU-side target of window id, when the window is destroyed.
PrimitiveRenderTarget * resizingTarget
Definition gui_primitive_renderer.h:230
static constexpr uint64_t kEvictAfterFrames
A capture neither recorded nor replayed for this many frames is dropped, its GPU copy freed (as UICac...
Definition gui_primitive_renderer.h:310
void RenderDot(tachyon::float2 pos, tachyon::IntRect clipRect, float size, tachyon::color4f color) override
void Render(tachyon::ID id, RHI::RenderCommandList &cmdList, const PipelineSet &pipelines, GPUDrawPushConstants &push_constants)
bool BeginWindow(tachyon::ID id) override
void StopResizing() override
Definition gui_primitive_renderer.h:280
void SetOpacity(float opacity) override
How opaque what is drawn from now on is, 0 to 1 (1 until set).
void SetTransform(tachyon::float3x2 transform) override
void BeginScopeCapture(tachyon::ID scopeId) override
Starts recording primitives under scopeId, replacing its previous capture.
void EndScopeCapture(tachyon::ID scopeId) override
Ends the capture of scopeId and draws it into the current window.
One frame's recording surface for one window.
Definition gui_rhi.h:62
uint64_t ResidentBufferId
GPU storage kept across frames (UploadResidentBuffers); 0 is none.
Definition gui_rhi.h:36
TriangleEdges
A triangle's edges, for the ones drawn without a soft edge: where it meets another triangle of the sa...
Definition gui_renderer.h:24
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:99
std::array< RHI::GpuPipeline, kPrimitiveKindCount > PipelineSet
Definition gui_primitive_renderer.h:31
PrimitiveKind
Definition gui_render_primitives.h:209
constexpr PrimitiveSpec kPrimitiveSpecs[kPrimitiveKindCount]
Push-constant slot order matches the shaders' (all_common.slang: box, glyph, dot, line,...
Definition gui_render_primitives.h:245
@ None
Definition gui_renderer.h:25
@ Invalid
Definition gui_image.h:99
VisualColorStyleIndex
Index into StyleManager's color style pool; Invalid (0) draws nothing (transparent).
Definition gui_style.h:33
Definition gui_logging.h:24
Definition gui_render_primitives.h:257
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
How an image is shown, in this order:
Definition gui_image.h:42
Definition gui_base_types.h:406
Definition gui_renderer.h:33
One contiguous same-kind run of primitives, drawn with one call; or, with replay set,...
Definition gui_primitive_renderer.h:36
int replay
index into PrimitiveRenderTarget::replays
Definition gui_primitive_renderer.h:40
PrimitiveKind kind
Definition gui_primitive_renderer.h:39
int firstPrimitive
Definition gui_primitive_renderer.h:37
bool operator==(const PrimitiveRenderPass &) const =default
int primitiveCount
Definition gui_primitive_renderer.h:38
A cached scope drawn here, moved delta window pixels from where it was captured; its primitives' draw...
Definition gui_primitive_renderer.h:65
uint32_t baseOrder
Definition gui_primitive_renderer.h:68
bool operator==(const ScopeReplay &) const =default
tachyon::int2 delta
Definition gui_primitive_renderer.h:67
tachyon::ID scope
Definition gui_primitive_renderer.h:66
Per-kind primitive stream: CPU bytes appended by the emit methods and packed together into the curren...
Definition gui_primitive_renderer.h:51
int passStart
index of the open pass's first primitive
Definition gui_primitive_renderer.h:53
std::vector< std::byte > cpu
Definition gui_primitive_renderer.h:52
Definition gui_primitive_renderer.h:46
void RenderQuad(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius=0.0f, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, const tachyon::UVRect &uv={}, const tachyon::ImageDisplay &display={})
tachyon::float3x2 currentTransform
Definition gui_primitive_renderer.h:59
std::vector< ScopeReplay > replays
Definition gui_primitive_renderer.h:71
float currentOpacity
0 to 1, stamped into each primitive
Definition gui_primitive_renderer.h:60
void StartRenderPass(PrimitiveKind kind)
void RenderGlyph(tachyon::float2 pos, tachyon::float2 size, tachyon::float2 uvMin, tachyon::float2 uvMax, float scale, int bpp, int mode, tachyon::VisualColorStyleIndex textColorIndex, tachyon::IntRect clipRect, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, float shadowBlur=-1.0f)
void AddReplay(tachyon::ID scope, tachyon::int2 delta, const PrimitiveRenderTarget &captured)
Closes the open pass and adds a pass that draws cached scope scope, moved by delta; captured is what ...
void RenderCircle(tachyon::float2 center, float radius, tachyon::IntRect clipRect, const tachyon::LineStyle &lineStyle)
void RenderShadow(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius, int blur)
void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, const tachyon::StyleBlend &styles, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})
std::vector< PrimitiveRenderPass > renderPasses
Definition gui_primitive_renderer.h:57
void RenderGlyphs(std::span< const GUIRenderer::GlyphInstance > glyphs, tachyon::float2 offset, tachyon::IntRect clipRect)
RenderGlyph for each of glyphs, moved by offset: one pass placement for them all, over the box they c...
void AppendCaptured(const PrimitiveRenderTarget &src, tachyon::int2 delta)
Appends another target's passes, translated by delta pixels.
void RenderOccluder(tachyon::IntRect rect, tachyon::IntRect clipRect)
int currentKind
kind of the open pass; -1 when none
Definition gui_primitive_renderer.h:58
void RenderLine(tachyon::int2 p0, tachyon::int2 p1, tachyon::int2 p2, tachyon::int2 p3, tachyon::IntRect clipRect, const tachyon::LineStyle &lineStyle)
bool recaptured
A cached scope captured again into this target this frame drew something else than before: what it re...
Definition gui_primitive_renderer.h:74
void SetTransform(tachyon::float3x2 transform)
uint32_t nextOrder
The draw order the next primitive gets; also counts a replay's primitives.
Definition gui_primitive_renderer.h:117
void RenderDots(std::span< const tachyon::float2 > positions, tachyon::IntRect clipRect, float size, tachyon::color4f color)
RenderDot at each of positions: one pass placement for them all.
std::array< Stream, kPrimitiveKindCount > streams
Definition gui_primitive_renderer.h:55
void SetOpacity(float opacity)
Definition gui_primitive_renderer.h:96
void RenderTriangle(tachyon::float2 p0, tachyon::float2 p1, tachyon::float2 p2, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, TriangleEdges hardEdges=TriangleEdges::None)
void RenderDot(tachyon::float2 pos, tachyon::IntRect clipRect, float size, tachyon::color4f color)
size_t Render(RHI::RenderCommandList &cmdList, const PipelineSet &pipelines, GPUDrawPushConstants &push_constants)
Uploads the streams and draws the passes; replay passes are left to the caller (PrimitiveRenderer).
void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, uint32_t fromStyleIndex, uint32_t toStyleIndex, float blendT, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})
Definition gui_primitive_renderer.h:98
A box's look as a mix of up to four of its state styles (flat GPU style indices, as GUIRenderer::Styl...
Definition gui_renderer.h:47
static StyleBlend Between(uint32_t from, uint32_t to, float t)
from mixed with to, t (0 to 1) of the way to to.
Definition gui_renderer.h:55
The part of an image to draw, in UV: 0,0 is its top-left corner and 1,1 its bottom-right.
Definition gui_image.h:15
Definition gui_base_types.h:394
Definition gui_base_types.h:120
float y
Definition gui_base_types.h:121
float x
Definition gui_base_types.h:121
float y
Definition gui_base_types.h:150
float x
Definition gui_base_types.h:150
float z
Definition gui_base_types.h:150
2D affine transform matrix (2 rows x 3 columns): x' = r0.x*x + r0.y*y + r0.z y' = r1....
Definition gui_base_types.h:218
float3 r1
Definition gui_base_types.h:219
float3 r0
Definition gui_base_types.h:219
Integer 2D vector.
Definition gui_base_types.h:18
int32_t x
Definition gui_base_types.h:19
int32_t y
Definition gui_base_types.h:19