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_vertex.hpp"
9#include "pipeline_SDF_specialization_constants.hpp"
10#include "../font/module.hpp"
11#include "../utility/module.hpp"
12#include "../log.hpp"
13#include "../vector_span.hpp"
14#include "../geometry/module.hpp"
15#include "../color/module.hpp"
16#include <vma/vk_mem_alloc.h>
17#include <vulkan/vulkan.hpp>
18#include <mutex>
19#include <unordered_map>
20
21namespace hi::inline v1 {
22class mat;
23class gfx_device_vulkan;
24struct attributed_glyph;
25
26namespace pipeline_SDF {
27struct Image;
28struct vertex;
29
31 // Studies in China have shown that literate individuals know and use between 3,000 and 4,000 characters.
32 // Handle up to 7 * 7 * 128 == 6321 characters with a 16 x 1024 x 1024, 16 x 1 MByte
33 //
34 // For latin characters we can store about 7 * 12 == 84 characters in a single image, which is enough
35 // for the full alpha numeric range that an application will use.
36
37 static constexpr int atlasImageWidth = 256; // 7-12 characters, of 34 pixels wide.
38 static constexpr int atlasImageHeight = 256; // 7 characters, of 34 pixels height.
39 static_assert(atlasImageWidth == atlasImageHeight, "needed for fwidth(textureCoord)");
40
41 static constexpr int atlasMaximumNrImages = 128; // 128 * 49 characters.
42 static constexpr int stagingImageWidth = 64; // One 'em' is 28 pixels, with edges 34 pixels.
43 static constexpr int stagingImageHeight = 64;
44
45 static constexpr float atlasTextureCoordinateMultiplier = 1.0f / atlasImageWidth;
46 static constexpr float drawfontSize = 28.0f;
47 static constexpr float drawBorder = sdf_r8::max_distance;
48 static constexpr float scaledDrawBorder = drawBorder / drawfontSize;
49
50 gfx_device_vulkan const &device;
51
52 vk::ShaderModule vertexShaderModule;
53 vk::ShaderModule fragmentShaderModule;
54
55 specialization_constants specializationConstants;
56 std::vector<vk::SpecializationMapEntry> fragmentShaderSpecializationMapEntries;
57 vk::SpecializationInfo fragmentShaderSpecializationInfo;
59
60 texture_map stagingTexture;
61 std::vector<texture_map> atlasTextures;
62
64 vk::Sampler atlasSampler;
65 vk::DescriptorImageInfo atlasSamplerDescriptorImageInfo;
66
67 point3 atlas_allocation_position = {};
69 int atlasAllocationMaxHeight = 0;
70
71 device_shared(gfx_device_vulkan const &device);
73
74 device_shared(device_shared const &) = delete;
75 device_shared &operator=(device_shared const &) = delete;
76 device_shared(device_shared &&) = delete;
77 device_shared &operator=(device_shared &&) = delete;
78
83 void destroy(gfx_device_vulkan const *vulkanDevice);
84
88 [[nodiscard]] glyph_atlas_info allocate_rect(extent2 draw_extent, scale2 draw_scale) noexcept;
89
90 void drawInCommandBuffer(vk::CommandBuffer const &commandBuffer);
91
96
100
104
107 aarectangle get_bounding_box(glyph_ids const &glyphs) const noexcept;
108
120 vector_span<vertex> &vertices,
121 aarectangle const &clipping_rectangle,
122 quad const &box,
123 glyph_ids const &glyphs,
124 quad_color colors) noexcept;
125
126private:
127 void buildShaders();
128 void teardownShaders(gfx_device_vulkan const*vulkanDevice);
129 void addAtlasImage();
130 void buildAtlas();
131 void teardownAtlas(gfx_device_vulkan const*vulkanDevice);
132 void add_glyph_to_atlas(glyph_ids const &glyph, glyph_atlas_info &info) noexcept;
133
137 hi_force_inline std::pair<glyph_atlas_info const *, bool> get_glyph_from_atlas(glyph_ids const &glyph) noexcept
138 {
139 auto &info = glyph.atlas_info();
140
141 if (info) [[likely]] {
142 return {&info, false};
143
144 } else {
145 add_glyph_to_atlas(glyph, info);
146 return {&info, true};
147 }
148 }
149};
150
151} // namespace pipeline_SDF
152} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:13
Definition glyph_atlas_info.hpp:12
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:134
Definition quad.hpp:17
Definition gfx_device_vulkan.hpp:21
Definition pipeline_SDF_device_shared.hpp:30
void prepareStagingPixmapForDrawing()
This will transition the staging texture to 'general' for writing by the CPU.
void destroy(gfx_device_vulkan const *vulkanDevice)
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:16
Definition vector_span.hpp:133