MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "TextureBuffer.hpp"
4
5namespace MayaFlux::Buffers {
6
7/**
8 * @class TextBuffer
9 * @brief TextureBuffer specialization for CPU-composited glyph textures.
10 *
11 * Encodes two invariants that hold for all pre-multiplied RGBA glyph
12 * coverage textures: streaming mode is enabled on the TextureProcessor
13 * to avoid per-frame staging buffer allocation under animated text, and
14 * alpha blending is enabled on the RenderProcessor so glyph coverage
15 * composites correctly over whatever is behind it.
16 *
17 * Produced by Portal::Text::InkPress. Callers interact with it identically
18 * to TextureBuffer -- set_position, set_scale, setup_rendering -- with no
19 * knowledge of the invariants required.
20 *
21 * When constructed with a budget (via Portal::Text::press overload accepting
22 * budget_width/budget_height), the GPU texture is allocated at the budget
23 * dimensions and the unused region is zero-filled (fully transparent).
24 * Subsequent Portal::Text::repress calls composite into the allocated region
25 * without reallocating unless the new content exceeds the budget and the
26 * policy is RedrawPolicy::Fit.
27 *
28 * Prefer pre-allocating a budget and using Portal::Text::impress for
29 * incremental text updates. RedrawPolicy::Fit on every repress call
30 * reallocates the VKImage and rebuilds the descriptor on each update.
31 */
32class MAYAFLUX_API TextBuffer : public TextureBuffer {
33public:
34 using TextureBuffer::TextureBuffer;
35
36 /**
37 * @brief Whether this buffer was created with a pre-allocated budget
38 * exceeding the initial content dimensions.
39 */
40 [[nodiscard]] bool has_budget() const { return m_budget_width > 0; }
41
42 /**
43 * @brief Full allocated GPU texture width. Equal to get_width() when
44 * no budget was set.
45 */
46 [[nodiscard]] uint32_t get_budget_width() const
47 {
48 return m_budget_width > 0 ? m_budget_width : get_width();
49 }
50
51 /**
52 * @brief Full allocated GPU texture height. Equal to get_height() when
53 * no budget was set.
54 */
55 [[nodiscard]] uint32_t get_budget_height() const
56 {
57 return m_budget_height > 0 ? m_budget_height : get_height();
58 }
59
60 /**
61 * @brief Sets the budget dimensions for this buffer. Does not allocate
62 * GPU texture or modify the buffer in any way; the budget is
63 * only used as a hint by Portal::Text::repress when deciding
64 * whether to reallocate.
65 * @param w Budget width in pixels.
66 * @param h Budget height in pixels.
67 */
68 void set_budget(uint32_t w, uint32_t h)
69 {
70 m_budget_width = w;
71 m_budget_height = h;
72 }
73
74 /**
75 * @brief Horizontal pen position in pixels for the next impress() run.
76 * Reset to zero by repress(). Advanced by impress().
77 */
78 [[nodiscard]] const uint32_t& get_cursor_x() const { return m_cursor_x; }
79 [[nodiscard]] uint32_t& get_cursor_x() { return m_cursor_x; }
80
81 /**
82 * @brief Vertical baseline position in pixels for the next impress() run.
83 * Reset to zero by repress(). Advanced by impress() on line wrap.
84 */
85 [[nodiscard]] const uint32_t& get_cursor_y() const { return m_cursor_y; }
86 [[nodiscard]] uint32_t& get_cursor_y() { return m_cursor_y; }
87
88 /**
89 * @brief Resets the write cursor to the origin. Called internally by
90 * repress(). Callers may also reset manually between logical blocks.
91 */
93 {
94 m_cursor_x = 0;
95 m_cursor_y = 0;
96 }
97
98 /**
99 * @brief Hard render bounds. impress() wraps at render_bounds_w and
100 * returns Overflow when render_bounds_h is exhausted.
101 */
102 void set_render_bounds(uint32_t w, uint32_t h)
103 {
104 m_render_bounds_w = w;
105 m_render_bounds_h = h;
106 }
107
108 [[nodiscard]] uint32_t get_render_bounds_w() const { return m_render_bounds_w; }
109 [[nodiscard]] uint32_t get_render_bounds_h() const { return m_render_bounds_h; }
110
111 /**
112 * @brief Full accumulated text composited into this buffer since the
113 * last repress(). Seeded by press(), appended by impress(),
114 * cleared by repress().
115 */
116 [[nodiscard]] const std::string& get_accumulated_text() const { return m_accumulated_text; }
117 void append_accumulated_text(std::string_view s) { m_accumulated_text += s; }
118 void clear_accumulated_text() { m_accumulated_text.clear(); }
119 void set_accumulated_text(std::string_view s) { m_accumulated_text = s; }
120
121 /**
122 * @brief Delegates to TextureBuffer::setup_rendering, then enables
123 * streaming mode on the TextureProcessor and alpha blending
124 * on the RenderProcessor.
125 * @param config Same as TextureBuffer::setup_rendering.
126 */
127 void setup_rendering(const RenderConfig& config) override;
128
129private:
130 /// @brief Allocated GPU texture width when a budget was requested.
131 /// Zero when the buffer was created without a budget.
132 uint32_t m_budget_width {};
133 /// @brief Allocated GPU texture height when a budget was requested.
134 uint32_t m_budget_height {};
135
136 uint32_t m_cursor_x {};
137 uint32_t m_cursor_y {};
138
139 uint32_t m_render_bounds_w { 1280 };
140 uint32_t m_render_bounds_h { 720 };
142};
143
144} // namespace MayaFlux::Buffers
uint32_t h
Definition InkPress.cpp:25
void set_render_bounds(uint32_t w, uint32_t h)
Hard render bounds.
uint32_t get_render_bounds_h() const
bool has_budget() const
Whether this buffer was created with a pre-allocated budget exceeding the initial content dimensions.
uint32_t get_render_bounds_w() const
const uint32_t & get_cursor_x() const
Horizontal pen position in pixels for the next impress() run.
uint32_t get_budget_width() const
Full allocated GPU texture width.
const uint32_t & get_cursor_y() const
Vertical baseline position in pixels for the next impress() run.
void set_budget(uint32_t w, uint32_t h)
Sets the budget dimensions for this buffer.
void reset_cursor()
Resets the write cursor to the origin.
const std::string & get_accumulated_text() const
Full accumulated text composited into this buffer since the last repress().
void append_accumulated_text(std::string_view s)
void set_accumulated_text(std::string_view s)
uint32_t get_budget_height() const
Full allocated GPU texture height.
TextureBuffer specialization for CPU-composited glyph textures.
A hybrid buffer managing both a textured quad geometry and its pixel data.