TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_image_set_text_atlas.h
Go to the documentation of this file.
1#pragma once
2
3// The text system's atlas pages as images in a backend's image set: each page an image made
4// with GraphicsContext::CreateImage, at an index reserved for it. Changed rows go into the page's
5// image; a page that grows is a new image under the same index, the old one let go once no
6// frame draws with it. For backends
7// whose image set reserves an index and points it at an image (ReserveIndex, SetImage).
8
12
13#include <cassert>
14#include <vector>
15
16namespace tachyon
17{
20
21 template <class ImageSetT, class ImageT>
23 {
24 public:
25 bool Initialize(GraphicsContext* graphicsContext, ImageSet* imageSet) override
26 {
27 m_graphics = graphicsContext;
28 m_imageSet = static_cast<ImageSetT*>(imageSet);
29 return m_graphics != nullptr && m_imageSet != nullptr;
30 }
31
32 TextureIndex CreateTexture(const uint8_t* imageData, uint2 imageSize, uint8_t bpp) override
33 {
34 assert(bpp == 1 || bpp == 4);
35 size_t i = 0;
36 while (i < m_pages.size() && m_pages[i].used)
37 ++i;
38 if (i == m_pages.size())
39 m_pages.emplace_back();
40 Page& page = m_pages[i];
41 page.bpp = bpp;
42 page.used = true;
43 Replace(page, imageData, imageSize);
44 return TextureIndex(uint32_t(i) + 1);
45 }
46
47 void UpdateTexture(TextureIndex textureIndex, const uint8_t* imageData, uint2 imageSize) override
48 {
49 if (Page* page = Find(textureIndex))
50 Replace(*page, imageData, imageSize);
51 }
52
53 // The rows go into the page's image when it's this size; a page that grew, or a backend
54 // that can't write rows, is a new image.
55 void UpdateTextureRows(TextureIndex textureIndex, const uint8_t* imageData, uint2 imageSize,
56 uint32_t firstRow, uint32_t rowCount) override
57 {
58 Page* page = Find(textureIndex);
59 if (page == nullptr)
60 return;
61 const size_t rowBytes = size_t(imageSize.x) * page->bpp;
62 if (page->image != nullptr && page->size.x == imageSize.x && page->size.y == imageSize.y && rowCount > 0 &&
63 m_graphics->UpdateImageRows(page->image, firstRow, rowCount, imageData + rowBytes * firstRow))
64 return;
65 Replace(*page, imageData, imageSize);
66 }
67
68 ImageIndex GetImageIndex(TextureIndex textureIndex) override
69 {
70 Page* page = Find(textureIndex);
71 if (page == nullptr)
73 if (page->index == ImageIndex::Invalid)
74 page->index = m_imageSet->ReserveIndex();
75 return page->index;
76 }
77
78 void SyncTextureIndices() override
79 {
80 for (Page& page : m_pages)
81 {
82 if (!page.used)
83 continue;
84 if (page.index == ImageIndex::Invalid)
85 page.index = m_imageSet->ReserveIndex();
86 m_imageSet->SetImage(page.index, static_cast<ImageT*>(page.image));
87 }
88 }
89
90 private:
91 struct Page
92 {
93 Image* image = nullptr;
94 uint2 size {};
95 uint8_t bpp = 1;
97 bool used = false;
98 };
99
100 Page* Find(TextureIndex textureIndex)
101 {
102 const uint32_t i = uint32_t(textureIndex) - 1;
103 if (textureIndex == TextureIndex::Invalid || i >= m_pages.size() || !m_pages[i].used)
104 {
105 TACHYON_LOG_ERROR(Diagnostics::MakeLogCategory("text"), "text atlas: no page {}", uint32_t(textureIndex));
106 return nullptr;
107 }
108 return &m_pages[i];
109 }
110
111 // A new image for the page; the old one goes once no frame draws with it. Glyphs draw at
112 // their rasterized size, so pages need no mip chain.
113 void Replace(Page& page, const uint8_t* imageData, uint2 imageSize)
114 {
115 if (page.image != nullptr)
116 m_graphics->DestroyImageAtEnd(page.image, DeviceScope::Frame);
117 page.image = m_graphics->CreateImage(const_cast<uint8_t*>(imageData), imageSize,
119 page.size = imageSize;
120 }
121
122 GraphicsContext* m_graphics = nullptr;
123 ImageSetT* m_imageSet = nullptr;
124 std::vector<Page> m_pages;
125 };
126
127}
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:53
Definition gui_image_set_text_atlas.h:23
void UpdateTextureRows(TextureIndex textureIndex, const uint8_t *imageData, uint2 imageSize, uint32_t firstRow, uint32_t rowCount) override
The page's pixels, all of them, of which only rows [firstRow, firstRow + rowCount) changed: where the...
Definition gui_image_set_text_atlas.h:55
void SyncTextureIndices() override
Definition gui_image_set_text_atlas.h:78
void UpdateTexture(TextureIndex textureIndex, const uint8_t *imageData, uint2 imageSize) override
Definition gui_image_set_text_atlas.h:47
bool Initialize(GraphicsContext *graphicsContext, ImageSet *imageSet) override
Definition gui_image_set_text_atlas.h:25
ImageIndex GetImageIndex(TextureIndex textureIndex) override
Definition gui_image_set_text_atlas.h:68
TextureIndex CreateTexture(const uint8_t *imageData, uint2 imageSize, uint8_t bpp) override
Definition gui_image_set_text_atlas.h:32
The images the shaders can sample, each at an ImageIndex. Implemented by the backend.
Definition gui_image.h:106
An image made by GraphicsContext::CreateImage; the backend defines its contents.
Definition gui_image.h:102
Definition gui_text_atlas_manager.h:17
constexpr LogCategory MakeLogCategory(std::string_view staticName)
A category named staticName, which must be a static string.
Definition gui_logging.h:41
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:99
@ LinearClampToEdge
Definition gui_image.h:94
@ R8G8B8A8
Definition gui_image.h:83
@ R8
Definition gui_image.h:77
@ Invalid
Definition gui_image.h:99
@ Frame
Definition gui_graphics_context.h:45
TextureIndex
Definition gui_text_atlas_manager.h:14
@ Invalid
Definition gui_text_atlas_manager.h:14
#define TACHYON_LOG_ERROR(category,...)
Definition gui_logging.h:201
Definition gui_logging.h:24
Definition gui_base_types.h:74
uint32_t x
Definition gui_base_types.h:75
uint32_t y
Definition gui_base_types.h:75