HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Pipeline_SDF_device_shared.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "pipeline_SDF_texture_map.hpp"
8#include "pipeline_SDF_atlas_rect.hpp"
9#include "pipeline_SDF_specialization_constants.hpp"
10#include "../text/font_glyph_ids.hpp"
11#include "../required.hpp"
12#include "../logger.hpp"
13#include "../vspan.hpp"
14#include "../numeric_array.hpp"
15#include "../rect.hpp"
16#include <vma/vk_mem_alloc.h>
17#include <vulkan/vulkan.hpp>
18#include <mutex>
19#include <unordered_map>
20
21namespace tt {
22class gui_device_vulkan;
23template<typename T>
24class pixel_map;
25class mat;
26} // namespace tt
27
28namespace tt {
29class shaped_text;
30struct attributed_glyph;
31} // namespace tt
32
33namespace tt::pipeline_SDF {
34
35struct Image;
36struct vertex;
37
38struct device_shared final {
39 // Studies in China have shown that literate individuals know and use between 3,000 and 4,000 characters.
40 // Handle up to 4096 characters with a 16 x 1024 x 1024, 16 x 1 MByte
41 static constexpr int atlasImageWidth = 1024; // 16 characters, of 64 pixels wide.
42 static constexpr int atlasImageHeight = 1024; // 16 characters, of 64 pixels height.
43 static_assert(atlasImageWidth == atlasImageHeight, "needed for fwidth(textureCoord)");
44
45 static constexpr int atlasMaximumNrImages = 16; // 16 * 512 characters, of 64x64 pixels.
46 static constexpr int stagingImageWidth = 128; // maximum size of character that can be uploaded is 128x128
47 static constexpr int stagingImageHeight = 128;
48
49 static constexpr float atlasTextureCoordinateMultiplier = 1.0f / atlasImageWidth;
50 static constexpr float drawfontSize = 28.0f;
51 static constexpr float drawBorder = sdf_r8::max_distance;
52 static constexpr float scaledDrawBorder = drawBorder / drawfontSize;
53
54 gui_device_vulkan const &device;
55
56 vk::ShaderModule vertexShaderModule;
57 vk::ShaderModule fragmentShaderModule;
58
59 specialization_constants specializationConstants;
60 std::vector<vk::SpecializationMapEntry> fragmentShaderSpecializationMapEntries;
61 vk::SpecializationInfo fragmentShaderSpecializationInfo;
63
65 texture_map stagingTexture;
66 std::vector<texture_map> atlasTextures;
67
69 vk::Sampler atlasSampler;
70 vk::DescriptorImageInfo atlasSamplerDescriptorImageInfo;
71
72 i32x4 atlasAllocationPosition = {};
75
76 device_shared(gui_device_vulkan const &device);
78
79 device_shared(device_shared const &) = delete;
80 device_shared &operator=(device_shared const &) = delete;
81 device_shared(device_shared &&) = delete;
82 device_shared &operator=(device_shared &&) = delete;
83
88 void destroy(gui_device_vulkan *vulkanDevice);
89
93 [[nodiscard]] atlas_rect allocateRect(f32x4 drawExtent) noexcept;
94
95 void drawInCommandBuffer(vk::CommandBuffer &commandBuffer);
96
101
105
109
112 void prepareAtlas(shaped_text const &text) noexcept;
113
116 static aarect getBoundingBox(font_glyph_ids const &glyphs) noexcept;
117
126 vspan<vertex> &vertices,
127 font_glyph_ids const &glyphs,
128 rect box,
129 color color,
130 aarect clippingRectangle) noexcept;
131
138 void placeVertices(vspan<vertex> &vertices, shaped_text const &text, matrix3 transform, aarect clippingRectangle) noexcept;
139
148 vspan<vertex> &vertices,
149 shaped_text const &text,
150 matrix3 transform,
151 aarect clippingRectangle,
152 color color) noexcept;
153
154private:
155 void buildShaders();
156 void teardownShaders(gui_device_vulkan *vulkanDevice);
157 void addAtlasImage();
158 void buildAtlas();
159 void teardownAtlas(gui_device_vulkan *vulkanDevice);
160
171 [[nodiscard]] bool _placeVertices(
172 vspan<vertex> &vertices,
173 font_glyph_ids const &glyphs,
174 rect box,
175 color color,
176 aarect clippingRectangle) noexcept;
177
187 [[nodiscard]] bool
188 _placeVertices(vspan<vertex> &vertices, attributed_glyph const &attr_glyph, matrix3 transform, aarect clippingRectangle) noexcept;
189
200 [[nodiscard]] bool _placeVertices(
201 vspan<vertex> &vertices,
202 attributed_glyph const &attr_glyph,
203 matrix3 transform,
204 aarect clippingRectangle,
205 color color) noexcept;
206
207 atlas_rect addGlyphToAtlas(font_glyph_ids glyph) noexcept;
208
212 std::pair<atlas_rect, bool> getGlyphFromAtlas(font_glyph_ids glyph) noexcept;
213};
214
215} // namespace tt::pipeline_SDF
This is a RGBA floating point color.
Definition color.hpp:39
static constexpr float max_distance
Max distance in pixels represented by the signed distance field.
Definition sdf_r8.hpp:21
Definition gui_device_vulkan.hpp:23
Definition Pipeline_SDF_atlas_rect.hpp:16
Definition Pipeline_SDF_device_shared.hpp:38
void placeVertices(vspan< vertex > &vertices, font_glyph_ids const &glyphs, rect box, color color, aarect clippingRectangle) noexcept
Place vertices for a single glyph.
static aarect getBoundingBox(font_glyph_ids const &glyphs) noexcept
Get the bounding box, including draw border of a glyph.
int atlasAllocationMaxHeight
During allocation on a row, we keep track of the tallest glyph.
Definition Pipeline_SDF_device_shared.hpp:74
void prepareStagingPixmapForDrawing()
This will transition the staging texture to 'general' for writing by the CPU.
atlas_rect allocateRect(f32x4 drawExtent) noexcept
Allocate an glyph in the atlas.
void placeVertices(vspan< vertex > &vertices, shaped_text const &text, matrix3 transform, aarect clippingRectangle, color color) noexcept
Draw the text on the screen.
void destroy(gui_device_vulkan *vulkanDevice)
void uploadStagingPixmapToAtlas(atlas_rect location)
Once drawing in the staging pixmap is completed, you can upload it to the atlas.
void placeVertices(vspan< vertex > &vertices, shaped_text const &text, matrix3 transform, aarect clippingRectangle) noexcept
Draw the text on the screen.
void prepareAtlas(shaped_text const &text) noexcept
Prepare the atlas for drawing a text.
void prepareAtlasForRendering()
This will transition the atlas to 'shader-read'.
Definition Pipeline_SDF_specialization_constants.hpp:12
Definition Pipeline_SDF_texture_map.hpp:18
A 4D vector.
Definition ivec.hpp:38
Class which represents an rectangle.
Definition rect.hpp:16
Definition attributed_glyph.hpp:17
Definition font_glyph_ids.hpp:78
shaped_text represent a piece of text shaped to be displayed.
Definition shaped_text.hpp:22
Definition vspan.hpp:73