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_specialization_constants.hpp"
9#include "../text/glyph_ids.hpp"
10#include "../text/glyph_atlas_info.hpp"
11#include "../utility.hpp"
12#include "../log.hpp"
13#include "../vector_span.hpp"
14#include "../geometry/rectangle.hpp"
15#include "../geometry/scale.hpp"
16#include "../geometry/transform.hpp"
17#include "../color/quad_color.hpp"
18#include <vma/vk_mem_alloc.h>
19#include <vulkan/vulkan.hpp>
20#include <mutex>
21#include <unordered_map>
22
23namespace hi::inline v1 {
24template<typename T>
25class pixel_map;
26class mat;
27class gfx_device_vulkan;
28struct attributed_glyph;
29
30namespace pipeline_SDF {
31struct Image;
32struct vertex;
33
34struct device_shared final {
35 // Studies in China have shown that literate individuals know and use between 3,000 and 4,000 characters.
36 // Handle up to 7 * 7 * 128 == 6321 characters with a 16 x 1024 x 1024, 16 x 1 MByte
37 //
38 // For latin characters we can store about 7 * 12 == 84 characters in a single image, which is enough
39 // for the full alpha numeric range that an application will use.
40
41 static constexpr int atlasImageWidth = 256; // 7-12 characters, of 34 pixels wide.
42 static constexpr int atlasImageHeight = 256; // 7 characters, of 34 pixels height.
43 static_assert(atlasImageWidth == atlasImageHeight, "needed for fwidth(textureCoord)");
44
45 static constexpr int atlasMaximumNrImages = 128; // 128 * 49 characters.
46 static constexpr int stagingImageWidth = 64; // One 'em' is 28 pixels, with edges 34 pixels.
47 static constexpr int stagingImageHeight = 64;
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 gfx_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
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 = {};
73 int atlasAllocationMaxHeight = 0;
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]] glyph_atlas_info allocate_rect(extent2 draw_extent, scale2 draw_scale) noexcept;
93
94 void drawInCommandBuffer(vk::CommandBuffer &commandBuffer);
95
100
104
108
111 aarectangle get_bounding_box(glyph_ids const &glyphs) const noexcept;
112
124 vector_span<vertex> &vertices,
125 aarectangle const &clipping_rectangle,
126 quad const &box,
127 glyph_ids const &glyphs,
128 quad_color colors) noexcept;
129
130private:
131 void buildShaders();
132 void teardownShaders(gfx_device_vulkan *vulkanDevice);
133 void addAtlasImage();
134 void buildAtlas();
135 void teardownAtlas(gfx_device_vulkan *vulkanDevice);
136 void add_glyph_to_atlas(glyph_ids const &glyph, glyph_atlas_info &info) noexcept;
137
141 hi_force_inline std::pair<glyph_atlas_info const *, bool> get_glyph_from_atlas(glyph_ids const &glyph) noexcept
142 {
143 auto &info = glyph.atlas_info();
144
145 if (info) [[likely]] {
146 return {&info, false};
147
148 } else {
149 add_glyph_to_atlas(glyph, info);
150 return {&info, true};
151 }
152 }
153};
154
155} // namespace pipeline_SDF
156} // namespace hi::inline v1
Utilities used by the HikoGUI library itself.
DOXYGEN BUG.
Definition algorithm.hpp:15
Definition quad_color.hpp:12
Definition quad.hpp:17
Definition gfx_device_vulkan.hpp:21
Definition pipeline_SDF_device_shared.hpp:34
void destroy(gfx_device_vulkan *vulkanDevice)
void prepareStagingPixmapForDrawing()
This will transition the staging texture to 'general' for writing by the CPU.
glyph_atlas_info allocate_rect(extent2 draw_extent, scale2 draw_scale) noexcept
Allocate an glyph in the atlas.
aarectangle get_bounding_box(glyph_ids const &glyphs) const noexcept
Get the bounding box, including draw border of a glyph.
bool place_vertices(vector_span< vertex > &vertices, aarectangle const &clipping_rectangle, quad const &box, glyph_ids const &glyphs, quad_color colors) noexcept
Place vertices for a single glyph.
void uploadStagingPixmapToAtlas(glyph_atlas_info const &location)
Once drawing in the staging pixmap is completed, you can upload it to the atlas.
void prepare_atlas_for_rendering()
This will transition the atlas to 'shader-read'.
Definition pipeline_SDF_specialization_constants.hpp:12
Definition pipeline_SDF_texture_map.hpp:17
Definition glyph_atlas_info.hpp:15
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:135
Definition vector_span.hpp:134