HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gfx_pipeline_box_vulkan_intf.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 "gfx_pipeline_vulkan_intf.hpp"
8#include "../container/container.hpp"
9#include "../geometry/geometry.hpp"
10#include "../color/color.hpp"
11#include "../image/image.hpp"
12#include "../macros.hpp"
13#include <vulkan/vulkan.hpp>
14#include <vma/vk_mem_alloc.h>
15#include <span>
16
17hi_export_module(hikogui.GFX : gfx_pipeline_box_intf);
18
19hi_export namespace hi { inline namespace v1 {
20
23class gfx_pipeline_box : public gfx_pipeline {
24public:
28 struct alignas(16) vertex {
31 sfloat_rgba32 position;
32
36 sfloat_rgba32 clipping_rectangle;
37
45 sfloat_rgba32 corner_coordinate;
46
49 sfloat_rgba32 corner_radii;
50
53 sfloat_rgba16 fill_color;
54
57 sfloat_rgba16 line_color;
58
59 float line_width;
60
61 vertex(
62 sfloat_rgba32 position,
63 sfloat_rgba32 clipping_rectangle,
64 sfloat_rgba32 corner_coordinate,
65 sfloat_rgba32 corner_radii,
66 sfloat_rgba16 fill_color,
67 sfloat_rgba16 line_color,
68 float line_width) noexcept :
75 line_width(line_width)
76 {
77 }
78
79 static vk::VertexInputBindingDescription inputBindingDescription()
80 {
81 return {0, sizeof(vertex), vk::VertexInputRate::eVertex};
82 }
83
84 static std::vector<vk::VertexInputAttributeDescription> inputAttributeDescriptions()
85 {
86 return {
87 {0, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(vertex, position)},
88 {1, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(vertex, clipping_rectangle)},
89 {2, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(vertex, corner_coordinate)},
90 {3, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(vertex, corner_radii)},
91 {4, 0, vk::Format::eR16G16B16A16Sfloat, offsetof(vertex, fill_color)},
92 {5, 0, vk::Format::eR16G16B16A16Sfloat, offsetof(vertex, line_color)},
93 {6, 0, vk::Format::eR32Sfloat, offsetof(vertex, line_width)},
94 };
95 }
96 };
97
99 sfloat_rg32 windowExtent = extent2{0.0, 0.0};
100 sfloat_rg32 viewportScale = scale2{0.0, 0.0};
101
102 static std::vector<vk::PushConstantRange> pushConstantRanges()
103 {
104 return {{vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0, sizeof(push_constants)}};
105 }
106 };
107
108 struct device_shared final {
109 gfx_device const& device;
110
111 vk::ShaderModule vertexShaderModule;
112 vk::ShaderModule fragmentShaderModule;
114
115 device_shared(gfx_device const& device);
117
118 device_shared(device_shared const&) = delete;
119 device_shared& operator=(device_shared const&) = delete;
120 device_shared(device_shared&&) = delete;
121 device_shared& operator=(device_shared&&) = delete;
122
126 void destroy(gfx_device const *vulkanDevice);
127
128 void drawInCommandBuffer(vk::CommandBuffer const& commandBuffer);
129
130 static void place_vertices(
131 vector_span<vertex>& vertices,
132 aarectangle clipping_rectangle,
133 quad box,
134 quad_color fill_colors,
135 quad_color line_colors,
136 float line_width,
138
139 private:
140 void buildShaders();
141 void teardownShaders(gfx_device const *vulkanDevice);
142 };
143
144 vector_span<vertex> vertexBufferData;
145
146 ~gfx_pipeline_box() = default;
147 gfx_pipeline_box(const gfx_pipeline_box&) = delete;
148 gfx_pipeline_box& operator=(const gfx_pipeline_box&) = delete;
150 gfx_pipeline_box& operator=(gfx_pipeline_box&&) = delete;
151
152 gfx_pipeline_box(gfx_surface *surface) : gfx_pipeline(surface) {}
153
154 void draw_in_command_buffer(vk::CommandBuffer commandBuffer, draw_context const& context) override;
155
156protected:
157 push_constants pushConstants;
158
159 vk::Buffer vertexBuffer;
160 VmaAllocation vertexBufferAllocation;
161
162 [[nodiscard]] std::vector<vk::PipelineShaderStageCreateInfo> createShaderStages() const override;
163 [[nodiscard]] std::vector<vk::DescriptorSetLayoutBinding> createDescriptorSetLayoutBindings() const override;
164 [[nodiscard]] std::vector<vk::WriteDescriptorSet> createWriteDescriptorSet() const override;
165 [[nodiscard]] size_t getDescriptorSetVersion() const override;
166 [[nodiscard]] std::vector<vk::PushConstantRange> createPushConstantRanges() const override;
167 [[nodiscard]] vk::VertexInputBindingDescription createVertexInputBindingDescription() const override;
168 [[nodiscard]] std::vector<vk::VertexInputAttributeDescription> createVertexInputAttributeDescriptions() const override;
169
170private:
171 void build_vertex_buffers() override;
172 void teardown_vertex_buffers() override;
173};
174
175}} // namespace hi::v1
Defined the color type.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A color for each corner of a quad.
Definition quad_color.hpp:22
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
The 4 radii of the corners of a quad or rectangle.
Definition corner_radii.hpp:26
A high-level geometric extent.
Definition extent2.hpp:32
Definition scale2.hpp:18
Draw context for drawing using the HikoGUI shaders.
Definition draw_context_intf.hpp:209
Definition gfx_pipeline_box_vulkan_intf.hpp:23
Definition gfx_pipeline_box_vulkan_intf.hpp:28
sfloat_rgba16 line_color
border color of the box.
Definition gfx_pipeline_box_vulkan_intf.hpp:57
sfloat_rgba32 corner_radii
Shape of each corner, negative values are cut corners, positive values are rounded corners.
Definition gfx_pipeline_box_vulkan_intf.hpp:49
sfloat_rgba32 corner_coordinate
Double 2D coordinates inside the quad, used to determine the distance from the sides and corner insid...
Definition gfx_pipeline_box_vulkan_intf.hpp:45
sfloat_rgba16 fill_color
background color of the box.
Definition gfx_pipeline_box_vulkan_intf.hpp:53
sfloat_rgba32 position
The pixel-coordinates where the origin is located relative to the bottom-left corner of the window.
Definition gfx_pipeline_box_vulkan_intf.hpp:31
sfloat_rgba32 clipping_rectangle
The position in pixels of the clipping rectangle relative to the bottom-left corner of the window,...
Definition gfx_pipeline_box_vulkan_intf.hpp:36
Definition gfx_pipeline_box_vulkan_intf.hpp:98
Definition gfx_pipeline_box_vulkan_intf.hpp:108
void destroy(gfx_device const *vulkanDevice)
Definition gfx_pipeline_box_vulkan_impl.hpp:120