HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
PipelineSDF_DeviceShared.hpp
1// Copyright 2019 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/GUI/PipelineSDF_TextureMap.hpp"
7#include "TTauri/GUI/PipelineSDF_AtlasRect.hpp"
8#include "TTauri/GUI/PipelineSDF_SpecializationConstants.hpp"
9#include "TTauri/GUI/GUIDevice_forward.hpp"
10#include "TTauri/Text/FontGlyphIDs.hpp"
11#include "TTauri/Foundation/required.hpp"
12#include "TTauri/Foundation/logger.hpp"
13#include "TTauri/Foundation/vspan.hpp"
14#include "TTauri/Foundation/ivec.hpp"
15#include "TTauri/Foundation/rect.hpp"
16#include <vma/vk_mem_alloc.h>
17#include <vulkan/vulkan.hpp>
18#include <mutex>
19#include <unordered_map>
20
21namespace tt {
22template<typename T> struct PixelMap;
23class vec;
24class mat;
25}
26
27namespace tt {
28class ShapedText;
29struct AttributedGlyph;
30}
31
32namespace tt::PipelineSDF {
33
34struct Image;
35struct Vertex;
36
37struct DeviceShared 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 = SDF8::max_distance;
51 static constexpr float scaledDrawBorder = drawBorder / drawFontSize;
52
53 GUIDevice const &device;
54
55 vk::ShaderModule vertexShaderModule;
56 vk::ShaderModule fragmentShaderModule;
57
58 SpecializationConstants specializationConstants;
59 std::vector<vk::SpecializationMapEntry> fragmentShaderSpecializationMapEntries;
60 vk::SpecializationInfo fragmentShaderSpecializationInfo;
62
64 TextureMap stagingTexture;
65 std::vector<TextureMap> atlasTextures;
66
68 vk::Sampler atlasSampler;
69 vk::DescriptorImageInfo atlasSamplerDescriptorImageInfo;
70
71 ivec atlasAllocationPosition = {};
74
75
76 DeviceShared(GUIDevice const &device);
78
79 DeviceShared(DeviceShared const &) = delete;
80 DeviceShared &operator=(DeviceShared const &) = delete;
81 DeviceShared(DeviceShared &&) = delete;
82 DeviceShared &operator=(DeviceShared &&) = delete;
83
87 void destroy(GUIDevice *vulkanDevice);
88
92 [[nodiscard]] AtlasRect allocateRect(vec drawExtent) noexcept;
93
94 void drawInCommandBuffer(vk::CommandBuffer &commandBuffer);
95
100
104
108
111 void prepareAtlas(ShapedText const &text) noexcept;
112
115 static aarect getBoundingBox(FontGlyphIDs const &glyphs) noexcept;
116
124 void placeVertices(vspan<Vertex> &vertices, FontGlyphIDs const &glyphs, rect box, vec color, aarect clippingRectangle) noexcept;
125
132 void placeVertices(vspan<Vertex> &vertices, ShapedText const &text, mat transform, aarect clippingRectangle) noexcept;
133
141 void placeVertices(vspan<Vertex> &vertices, ShapedText const &text, mat transform, aarect clippingRectangle, vec color) noexcept;
142
143private:
144 void buildShaders();
145 void teardownShaders(GUIDevice_vulkan *vulkanDevice);
146 void addAtlasImage();
147 void buildAtlas();
148 void teardownAtlas(GUIDevice_vulkan *vulkanDevice);
149
160 [[nodiscard]] bool _placeVertices(vspan<Vertex> &vertices, FontGlyphIDs const &glyphs, rect box, vec color, aarect clippingRectangle) noexcept;
161
171 [[nodiscard]] bool _placeVertices(vspan<Vertex> &vertices, AttributedGlyph const &attr_glyph, mat transform, aarect clippingRectangle) noexcept;
172
183 [[nodiscard]] bool _placeVertices(vspan<Vertex> &vertices, AttributedGlyph const &attr_glyph, mat transform, aarect clippingRectangle, vec color) noexcept;
184
185 AtlasRect addGlyphToAtlas(FontGlyphIDs glyph) noexcept;
186
190 std::pair<AtlasRect,bool> getGlyphFromAtlas(FontGlyphIDs glyph) noexcept;
191
192};
193
194}
Class which represents an axis-aligned rectangle.
Definition aarect.hpp:13
A 4D vector.
Definition ivec.hpp:37
A 4x4 matrix.
Definition mat.hpp:18
Class which represents an axis-aligned rectangle.
Definition rect.hpp:15
static constexpr float max_distance
Max distance in pixels represented by the signed distance field.
Definition SDF8.hpp:20
A 4D vector.
Definition vec.hpp:37
Definition vspan.hpp:72
Definition GUIDevice_vulkan.hpp:22
Definition PipelineSDF_AtlasRect.hpp:16
Definition PipelineSDF_DeviceShared.hpp:37
AtlasRect allocateRect(vec drawExtent) noexcept
Allocate an glyph in the atlas.
void prepareAtlas(ShapedText const &text) noexcept
Prepare the atlas for drawing a text.
int atlasAllocationMaxHeight
During allocation on a row, we keep track of the tallest glyph.
Definition PipelineSDF_DeviceShared.hpp:73
void prepareAtlasForRendering()
This will transition the atlas to 'shader-read'.
void uploadStagingPixmapToAtlas(AtlasRect location)
Once drawing in the staging pixmap is completed, you can upload it to the atlas.
void placeVertices(vspan< Vertex > &vertices, FontGlyphIDs const &glyphs, rect box, vec color, aarect clippingRectangle) noexcept
Place vertices for a single glyph.
void prepareStagingPixmapForDrawing()
This will transition the staging texture to 'general' for writing by the CPU.
void destroy(GUIDevice *vulkanDevice)
void placeVertices(vspan< Vertex > &vertices, ShapedText const &text, mat transform, aarect clippingRectangle, vec color) noexcept
Draw the text on the screen.
static aarect getBoundingBox(FontGlyphIDs const &glyphs) noexcept
Get the bounding box, including draw border of a glyph.
void placeVertices(vspan< Vertex > &vertices, ShapedText const &text, mat transform, aarect clippingRectangle) noexcept
Draw the text on the screen.
Definition PipelineSDF_SpecializationConstants.hpp:11
Definition PipelineSDF_TextureMap.hpp:14
Definition AttributedGlyph.hpp:15
Definition FontGlyphIDs.hpp:77
ShapedText represent a piece of text shaped to be displayed.
Definition ShapedText.hpp:21