TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_gradient_manager.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <stdint.h>
7#include <algorithm>
8#include <vector>
9#include <span>
10#include <unordered_map>
11
12namespace tachyon
13{
16
17 class Image;
18 class GraphicsContext;
19
27 {
28 uint32_t value = 0;
29
30 constexpr GradientHandle() = default;
31 constexpr explicit GradientHandle(uint32_t v) : value(v) {}
32
34 constexpr bool IsValid() const { return value != 0; }
35 constexpr uint32_t SlotIndex() const { return value >> 8; }
36 constexpr uint8_t Generation() const { return value & 0xFF; }
37
38 constexpr bool operator==(const GradientHandle&) const noexcept = default;
39
40 static constexpr GradientHandle Invalid() { return GradientHandle(0); }
42 static constexpr GradientHandle Make(uint32_t slotIndex1Based, uint8_t generation)
43 {
44 return GradientHandle((slotIndex1Based << 8) | uint32_t(generation));
45 }
46 };
47
50 {
51 uint16_t col = 0;
52 uint16_t row = 0;
53 uint16_t width = 0;
54 uint8_t page = 0;
55 };
56
65 {
66 public:
69 static constexpr uint32_t kMaxPageHeight = 8192;
70
72 static constexpr int32_t kUnregisteredPage = -1;
73
74 public:
76 GradientManager(size_t width = 256);
77
80 void ReleaseAll(GraphicsContext* graphicsContext);
81
89
92
93 GradientHandle AcquireColors(const color4b& c0, const color4b& c1, const color4b& c2);
94 GradientHandle AcquireColors(const color4f& c0, const color4f& c1, const color4f& c2);
95
96 GradientHandle AcquireColors(const color4b& c0, const color4b& c1, const color4b& c2, const color4b& c3);
97 GradientHandle AcquireColors(const color4f& c0, const color4f& c1, const color4f& c2, const color4f& c3);
98
102 GradientHandle AcquireRowPixels(std::span<const color4b> pixels);
104
106 void AddRef(GradientHandle handle);
109
112
114 bool IsValid(GradientHandle handle) const;
115
117 uint16_t GetRefcount(GradientHandle handle) const;
118
120 inline size_t Width() const noexcept { return m_width; }
121
123 inline uint32_t PageCount() const noexcept { return static_cast<uint32_t>(m_pages.size()); }
124
126 size_t PageHeight(uint32_t pageIndex) const noexcept;
127
129 std::span<const color4b> PageTextureData(uint32_t pageIndex) const noexcept;
130
132 std::span<color4b> PageTextureData(uint32_t pageIndex) noexcept;
134 void MarkPageModified(uint32_t pageIndex) noexcept;
135
142 int32_t GetPagePhysicalImageIndex(uint32_t pageIndex) const noexcept;
143 void SetPagePhysicalImageIndex(uint32_t pageIndex, int32_t shaderSlotZeroBased) noexcept;
144 bool HasPageTableChanged() const noexcept { return m_pageTableDirty; }
145 void ClearPageTableChanged() noexcept { m_pageTableDirty = false; }
147
150 void ClearAll() noexcept;
151
155 tachyon::Image* GetGradientImage(GraphicsContext* graphicsContext, uint32_t pageIndex = 0);
156
157 private:
158 struct Slot
159 {
160 uint64_t contentHash = 0;
161 uint16_t refcount = 0;
162 uint16_t row = 0;
163 uint16_t col = 0;
164 uint16_t width = 0;
165 uint8_t page = 0;
166 uint8_t generation = 0;
167 bool inUse = false;
168 };
169
170 struct Page
171 {
172 uint32_t width = 0;
173 uint32_t height = 0;
174 uint32_t rowsUsed = 0;
175 std::vector<color4b> data;
176 Image* image = nullptr;
178 bool resized = true;
181 uint32_t dirtyFirst = UINT32_MAX;
182 uint32_t dirtyLast = 0;
183 void MarkRows(uint32_t first, uint32_t last) noexcept
184 {
185 dirtyFirst = std::min(dirtyFirst, first);
186 dirtyLast = std::max(dirtyLast, last);
187 }
188 void MarkAll() noexcept { if (height > 0) MarkRows(0, height - 1); }
189 bool HasDirtyRows() const noexcept { return dirtyFirst <= dirtyLast; }
190 void ClearDirty() noexcept { dirtyFirst = UINT32_MAX; dirtyLast = 0; }
191
194 int32_t smallRow = -1;
195 int32_t smallCursor = 0;
196
197 int32_t physicalImageIndex = kUnregisteredPage;
198 };
199
200 // --- helpers ---
201 GradientHandle AcquireSmall(uint16_t width, uint64_t contentHash,
202 const color4b* colors);
203 GradientHandle AcquireRow(uint64_t contentHash,
204 std::span<const color4b> rowPixels);
205
207 int32_t AllocateSmallSlot(uint16_t width);
208 int32_t AllocateRowSlot();
209
210 uint32_t AllocateSlotStorage(); // returns slot index (into m_slots)
211 void FreeSlotStorage(uint32_t slotIdx);
212
213 bool GrowOrAddPage(uint32_t& outPageIdx, uint32_t& outRow); // for full-row allocs
214 bool EnsureSmallRowAvailable(uint32_t& outPageIdx, uint16_t width);
215
216 Slot* GetSlot(GradientHandle handle);
217 const Slot* GetSlot(GradientHandle handle) const;
218
219 void ReleaseSlot(uint32_t slotIdx);
220
221 static uint64_t HashGradient(const Gradient& g);
222 static uint64_t HashSmall(uint16_t width, const color4b* colors);
223
224 size_t m_width;
225 std::vector<Page> m_pages;
226 std::vector<Slot> m_slots;
227 std::vector<uint32_t> m_freeSlotStorage;
228
232 static constexpr uint32_t kFreeListCount = 6;
233 std::vector<uint32_t> m_freeLists[kFreeListCount];
234
236 std::unordered_map<uint64_t, uint32_t> m_contentDedup;
237
238 bool m_pageTableDirty = true;
239 };
240
241}
void MarkPageModified(uint32_t pageIndex) noexcept
Makes the next GetGradientImage upload the whole page.
void AddRef(GradientHandle handle)
Raises the refcount of a live handle; pair with one Release.
size_t Width() const noexcept
Page width in texels, the same for every page.
Definition gui_gradient_manager.h:120
void ClearAll() noexcept
Releases every slot; every outstanding GradientHandle becomes stale, so holders (StyleManager,...
static constexpr uint32_t kMaxPageHeight
Page height cap; a full page gets a new page instead of growing.
Definition gui_gradient_manager.h:69
int32_t GetPagePhysicalImageIndex(uint32_t pageIndex) const noexcept
GradientHandle AcquireColors(const color4f &c0, const color4f &c1, const color4f &c2)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
GradientHandle AcquireColors(const color4b &c0, const color4b &c1)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
GradientHandle AcquireSolidColor(const color4f &color)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
bool Release(GradientHandle handle)
Lowers the refcount; true when it reached zero and the slot was reclaimed.
GradientHandle AcquireColors(const color4b &c0, const color4b &c1, const color4b &c2, const color4b &c3)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
GradientManager(size_t width=256)
An empty atlas whose pages are width texels wide.
bool IsValid(GradientHandle handle) const
True if the handle is live (slot occupied and generation matches).
std::span< const color4b > PageTextureData(uint32_t pageIndex) const noexcept
CPU texels of a page, PageHeight x Width.
GradientHandle AcquireRowPixels(std::span< const color4b > pixels)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
void ClearPageTableChanged() noexcept
Definition gui_gradient_manager.h:145
GradientHandle AcquireColors(const color4f &c0, const color4f &c1)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
GradientHandle AcquireGradient(const Gradient &g)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
GradientHandle AcquireSolidColor(const color4b &color)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
void SetPagePhysicalImageIndex(uint32_t pageIndex, int32_t shaderSlotZeroBased) noexcept
tachyon::Image * GetGradientImage(GraphicsContext *graphicsContext, uint32_t pageIndex=0)
The GPU image of a page, made when it's new or has grown; otherwise the rows written since go up into...
void ReleaseAll(GraphicsContext *graphicsContext)
Destroys every GPU image of the atlas (deferred through graphicsContext).
bool HasPageTableChanged() const noexcept
Definition gui_gradient_manager.h:144
size_t PageHeight(uint32_t pageIndex) const noexcept
Allocated height of a page in rows; the rows in use are at most this.
uint16_t GetRefcount(GradientHandle handle) const
Refcount of a live handle; 0 for an invalid or stale handle.
uint32_t PageCount() const noexcept
Number of pages allocated.
Definition gui_gradient_manager.h:123
std::span< color4b > PageTextureData(uint32_t pageIndex) noexcept
Writable CPU texels of a page; call MarkPageModified after writing.
GradientHandle AcquireColors(const color4f &c0, const color4f &c1, const color4f &c2, const color4f &c3)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
AtlasLocation GetLocation(GradientHandle handle) const
The atlas position of a handle; all zero for an invalid or stale handle.
GradientHandle AcquireColors(const color4b &c0, const color4b &c1, const color4b &c2)
A full row of these pixels (a GradientF's WriteTo, say), resampled to Width() when it's another size.
static constexpr int32_t kUnregisteredPage
GetPagePhysicalImageIndex of a page the renderer hasn't registered.
Definition gui_gradient_manager.h:72
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:53
An image made by GraphicsContext::CreateImage; the backend defines its contents.
Definition gui_image.h:102
Definition gui_logging.h:24
Where a handle's texels are in the atlas.
Definition gui_gradient_manager.h:50
uint8_t page
logical page index (0..PageCount()-1)
Definition gui_gradient_manager.h:54
uint16_t width
number of columns (1..page.width)
Definition gui_gradient_manager.h:53
uint16_t col
starting column within the row (0..page.width-1)
Definition gui_gradient_manager.h:51
uint16_t row
row index within the page (0..page.height-1)
Definition gui_gradient_manager.h:52
Handle to a reference-counted GradientManager atlas slot.
Definition gui_gradient_manager.h:27
constexpr uint8_t Generation() const
wraps around
Definition gui_gradient_manager.h:36
static constexpr GradientHandle Invalid()
the invalid handle
Definition gui_gradient_manager.h:40
constexpr uint32_t SlotIndex() const
1-based; 0 = invalid
Definition gui_gradient_manager.h:35
constexpr bool IsValid() const
Not the invalid handle; GradientManager::IsValid also checks it's still live.
Definition gui_gradient_manager.h:34
constexpr bool operator==(const GradientHandle &) const noexcept=default
constexpr GradientHandle(uint32_t v)
Definition gui_gradient_manager.h:31
uint32_t value
encoded slot index and generation
Definition gui_gradient_manager.h:28
static constexpr GradientHandle Make(uint32_t slotIndex1Based, uint8_t generation)
Encodes a slot index (1-based) and generation.
Definition gui_gradient_manager.h:42
constexpr GradientHandle()=default
Up to kMaxStops color stops plus an angle and interpolation color space.
Definition gui_style_gradient.h:93
Definition gui_base_types.h:383
Definition gui_base_types.h:394