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 "../log.hpp"
13#include "../vspan.hpp"
14#include "../geometry/rectangle.hpp"
15#include <vk_mem_alloc.h>
16#include <vulkan/vulkan.hpp>
17#include <mutex>
18#include <unordered_map>
19
20namespace tt {
21class gfx_device_vulkan;
22template<typename T>
23class pixel_map;
24class mat;
25} // namespace tt
26
27namespace tt {
28class shaped_text;
29struct attributed_glyph;
30} // namespace tt
31
32namespace tt::pipeline_SDF {
33
34struct Image;
35struct vertex;
36
37struct device_shared final {
38 // Studies in China have shown that literate individuals know and use between 3,000 and 4,000 characters.
39 // Handle up to 4096 characters with a 16 x 1024 x 1024, 16 x 1 MByte
40 static constexpr int atlasImageWidth = 1024; // 16 characters, of 64 pixels wide.
41 static constexpr int atlasImageHeight = 1024; // 16 characters, of 64 pixels height.
42 static_assert(atlasImageWidth == atlasImageHeight, "needed for fwidth(textureCoord)");
43
44 static constexpr int atlasMaximumNrImages = 16; // 16 * 512 characters, of 64x64 pixels.
45 static constexpr int stagingImageWidth = 128; // maximum size of character that can be uploaded is 128x128
46 static constexpr int stagingImageHeight = 128;
47
48 static constexpr float atlasTextureCoordinateMultiplier = 1.0f / atlasImageWidth;
49 static constexpr float drawfontSize = 28.0f;
50 static constexpr float drawBorder = sdf_r8::max_distance;
51 static constexpr float scaledDrawBorder = drawBorder / drawfontSize;
52
53 gfx_device_vulkan const &device;
54
55 vk::ShaderModule vertexShaderModule;
56 vk::ShaderModule fragmentShaderModule;
57
58 specialization_constants specializationConstants;
59 std::vector<vk::SpecializationMapEntry> fragmentShaderSpecializationMapEntries;
60 vk::SpecializationInfo fragmentShaderSpecializationInfo;
62
64 texture_map stagingTexture;
65 std::vector<texture_map> atlasTextures;
66
68 vk::Sampler atlasSampler;
69 vk::DescriptorImageInfo atlasSamplerDescriptorImageInfo;
70
71 point3 atlas_allocation_position = {};
74
75 device_shared(gfx_device_vulkan const &device);
77
78 device_shared(device_shared const &) = delete;
79 device_shared &operator=(device_shared const &) = delete;
80 device_shared(device_shared &&) = delete;
81 device_shared &operator=(device_shared &&) = delete;
82
87 void destroy(gfx_device_vulkan *vulkanDevice);
88
92 [[nodiscard]] atlas_rect allocateRect(extent2 drawExtent) noexcept;
93
94 void drawInCommandBuffer(vk::CommandBuffer &commandBuffer);
95
100
104
108
111 void prepareAtlas(shaped_text const &text) noexcept;
112
115 aarectangle get_bounding_box(font_glyph_ids const &glyphs) const noexcept;
116
127 vspan<vertex> &vertices,
128 aarectangle clipping_rectangle,
129 rectangle box,
130 font_glyph_ids const &glyphs,
131 float glyph_size,
132 color color) noexcept;
133
141 vspan<vertex> &vertices,
142 aarectangle clipping_rectangle,
143 matrix3 transform,
144 shaped_text const &text) noexcept;
145
154 vspan<vertex> &vertices,
155 aarectangle clipping_rectangle,
156 matrix3 transform,
157 shaped_text const &text,
158 color color) noexcept;
159
160private:
161 void buildShaders();
162 void teardownShaders(gfx_device_vulkan *vulkanDevice);
163 void addAtlasImage();
164 void buildAtlas();
165 void teardownAtlas(gfx_device_vulkan *vulkanDevice);
166
177 [[nodiscard]] bool _place_vertices(
178 vspan<vertex> &vertices,
179 aarectangle clipping_rectangle,
180 rectangle box,
181 font_glyph_ids const &glyphs,
183 ) noexcept;
184
194 [[nodiscard]] bool _place_vertices(
195 vspan<vertex> &vertices,
196 aarectangle clippingRectangle,
197 matrix3 transform,
198 attributed_glyph const &attr_glyph
199 ) noexcept;
200
211 [[nodiscard]] bool _place_vertices(
212 vspan<vertex> &vertices,
213 aarectangle clippingRectangle,
214 matrix3 transform,
215 attributed_glyph const &attr_glyph,
216 color color) noexcept;
217
218 atlas_rect add_glyph_to_atlas(font_glyph_ids glyph) noexcept;
219
223 std::pair<atlas_rect, bool> get_glyph_from_atlas(font_glyph_ids glyph) noexcept;
224};
225
226} // namespace tt::pipeline_SDF
This is a RGBA floating point color.
Definition color.hpp:36
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Class which represents an rectangle.
Definition rectangle.hpp:16
Definition gfx_device_vulkan.hpp:23
Definition pipeline_SDF_atlas_rect.hpp:17
Definition pipeline_SDF_device_shared.hpp:37
int atlasAllocationMaxHeight
During allocation on a row, we keep track of the tallest glyph.
Definition pipeline_SDF_device_shared.hpp:73
void prepareStagingPixmapForDrawing()
This will transition the staging texture to 'general' for writing by the CPU.
atlas_rect allocateRect(extent2 drawExtent) noexcept
Allocate an glyph in the atlas.
void place_vertices(vspan< vertex > &vertices, aarectangle clipping_rectangle, matrix3 transform, shaped_text const &text) noexcept
Draw the text on the screen.
aarectangle get_bounding_box(font_glyph_ids const &glyphs) const noexcept
Get the bounding box, including draw border of a glyph.
void uploadStagingPixmapToAtlas(atlas_rect location)
Once drawing in the staging pixmap is completed, you can upload it to the atlas.
void place_vertices(vspan< vertex > &vertices, aarectangle clipping_rectangle, matrix3 transform, shaped_text const &text, color color) noexcept
Draw the text on the screen.
void destroy(gfx_device_vulkan *vulkanDevice)
void place_vertices(vspan< vertex > &vertices, aarectangle clipping_rectangle, rectangle box, font_glyph_ids const &glyphs, float glyph_size, color color) noexcept
Place vertices for a single glyph.
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
static constexpr float max_distance
Max distance in pixels represented by the signed distance field.
Definition sdf_r8.hpp:21
Definition attributed_glyph.hpp:17
Definition font_glyph_ids.hpp:80
shaped_text represent a piece of text shaped to be displayed.
Definition shaped_text.hpp:23
Definition vspan.hpp:73