7#include "gfx_pipeline_vulkan.hpp"
8#include "../container/module.hpp"
9#include "../image/module.hpp"
10#include "../codec/codec.hpp"
11#include "../macros.hpp"
12#include <vma/vk_mem_alloc.h>
14namespace hi {
inline namespace v1 {
24 struct alignas(16) vertex {
40 static vk::VertexInputBindingDescription inputBindingDescription()
42 return {0,
sizeof(
vertex), vk::VertexInputRate::eVertex};
48 {0, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(vertex,
position)},
50 {2, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(vertex,
atlas_position)},
56 sfloat_rg32 windowExtent =
extent2{0.0, 0.0};
57 sfloat_rg32 viewportScale =
scale2{0.0, 0.0};
58 sfloat_rg32 atlasExtent =
extent2{0.0, 0.0};
59 sfloat_rg32 atlasScale =
scale2{0.0, 0.0};
63 return {{vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0,
sizeof(
push_constants)}};
69 VmaAllocation allocation = {};
72 vk::ImageLayout layout = vk::ImageLayout::eUndefined;
74 void transitionLayout(
const gfx_device& device, vk::Format format, vk::ImageLayout nextLayout);
80 enum class state_type { uninitialized, drawing, uploaded };
85 gfx_device *device =
nullptr;
91 constexpr paged_image()
noexcept =
default;
92 paged_image(paged_image&&
other)
noexcept;
93 paged_image& operator=(paged_image&&
other)
noexcept;
94 paged_image(paged_image
const&
other) =
delete;
95 paged_image& operator=(paged_image
const&
other) =
delete;
104 paged_image(gfx_surface
const *surface,
png const& image)
noexcept;
106 [[nodiscard]]
constexpr explicit operator bool()
const noexcept
108 return device !=
nullptr;
111 [[nodiscard]]
constexpr extent2 size()
const noexcept
118 hilet num_columns = (width + page_size - 1) / page_size;
119 hilet num_rows = (height + page_size - 1) / page_size;
120 return {num_columns, num_rows};
123 [[nodiscard]]
constexpr extent2 size_in_float_pages()
const noexcept
127 return extent2{size / page_size_};
139 struct device_shared {
140 constexpr static std::size_t atlas_num_pages_per_axis = 8;
141 constexpr static std::size_t atlas_num_pages_per_image = atlas_num_pages_per_axis * atlas_num_pages_per_axis;
142 constexpr static std::size_t atlas_image_axis_size = atlas_num_pages_per_axis * (paged_image::page_size + 2);
143 constexpr static std::size_t atlas_maximum_num_images = 64;
144 constexpr static std::size_t staging_image_width = 1024;
145 constexpr static std::size_t staging_image_height = 1024;
147 gfx_device
const& device;
149 vk::ShaderModule vertex_shader_module;
150 vk::ShaderModule fragment_shader_module;
157 vk::Sampler atlas_sampler;
158 vk::DescriptorImageInfo atlas_sampler_descriptor_image_info;
160 device_shared(gfx_device
const& device);
163 device_shared(device_shared
const&) =
delete;
164 device_shared& operator=(device_shared
const&) =
delete;
165 device_shared(device_shared&&) =
delete;
166 device_shared& operator=(device_shared&&) =
delete;
171 void destroy(gfx_device
const *vulkanDevice);
181 void draw_in_command_buffer(vk::CommandBuffer
const& commandBuffer);
202 vector_span<vertex>& vertices,
205 paged_image
const& image)
noexcept;
221 void make_staging_border_transparent(
aarectangle border_rectangle)
noexcept;
228 void clear_staging_between_border_and_upload(
aarectangle border_rectangle,
aarectangle upload_rectangle)
noexcept;
240 void prepare_staging_for_upload(paged_image
const& image)
noexcept;
244 void update_atlas_with_staging_pixmap(paged_image
const& image)
noexcept;
246 void build_shaders();
247 void teardown_shaders(gfx_device
const *device);
248 void add_atlas_image();
250 void teardown_atlas(gfx_device
const *device);
255 vector_span<vertex> vertexBufferData;
257 ~gfx_pipeline_image() =
default;
258 gfx_pipeline_image(
const gfx_pipeline_image&) =
delete;
259 gfx_pipeline_image& operator=(
const gfx_pipeline_image&) =
delete;
260 gfx_pipeline_image(gfx_pipeline_image&&) =
delete;
261 gfx_pipeline_image& operator=(gfx_pipeline_image&&) =
delete;
263 gfx_pipeline_image(gfx_surface *surface) : gfx_pipeline(surface) {}
265 void draw_in_command_buffer(vk::CommandBuffer commandBuffer,
draw_context const& context)
override;
268 push_constants pushConstants;
269 int numberOfAtlasImagesInDescriptor = 0;
271 vk::Buffer vertexBuffer;
272 VmaAllocation vertexBufferAllocation;
277 [[nodiscard]]
size_t getDescriptorSetVersion()
const override;
279 [[nodiscard]] vk::VertexInputBindingDescription createVertexInputBindingDescription()
const override;
283 void build_vertex_buffers()
override;
284 void teardown_vertex_buffers()
override;
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:22
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:29
A high-level geometric extent.
Definition extent2.hpp:29
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:208
Definition gfx_pipeline_image_vulkan.hpp:19
Definition gfx_pipeline_image_vulkan.hpp:24
sfloat_rgba32 atlas_position
The x, y coordinate inside the texture-atlas, z is used as an index in the texture-atlas array.
Definition gfx_pipeline_image_vulkan.hpp:33
sfloat_rgba32 position
The pixel-coordinates where the origin is located relative to the bottom-left corner of the window.
Definition gfx_pipeline_image_vulkan.hpp:26
sfloat_rgba32 clipping_rectangle
Definition gfx_pipeline_image_vulkan.hpp:30
Definition gfx_pipeline_image_vulkan.hpp:55
Definition gfx_pipeline_image_vulkan.hpp:67
void upload(pixmap_span< sfloat_rgba16 const > image) noexcept
Upload image to atlas.
Definition gfx_pipeline_image_vulkan_impl.hpp:240
void destroy(gfx_device const *vulkanDevice)
Definition gfx_pipeline_image_vulkan_impl.hpp:265
void place_vertices(vector_span< vertex > &vertices, aarectangle const &clipping_rectangle, quad const &box, paged_image const &image) noexcept
Place vertices for a single image.
Definition gfx_pipeline_image_vulkan_impl.hpp:623
std::vector< std::size_t > allocate_pages(std::size_t num_pages) noexcept
Allocate pages from the atlas.
Definition gfx_pipeline_image_vulkan_impl.hpp:272
void prepare_atlas_for_rendering()
Prepare the atlas so that it can be used as a texture map by the shaders.
Definition gfx_pipeline_image_vulkan_impl.hpp:460
hi::pixmap_span< sfloat_rgba16 > get_staging_pixmap()
Get the full staging pixel map excluding border.
Definition gfx_pipeline_image_vulkan_impl.hpp:292
void free_pages(std::vector< std::size_t > const &pages) noexcept
Deallocate pages back to the atlas.
Definition gfx_pipeline_image_vulkan_impl.hpp:287
A non-owning 2D pixel-based image.
Definition pixmap_span.hpp:34
A 2D pixel-based image.
Definition pixmap.hpp:38