7#include "gfx_pipeline_box_vulkan_intf.hpp"
8#include "gfx_device_vulkan_impl.hpp"
9#include "draw_context_intf.hpp"
10#include "../macros.hpp"
11#include <vulkan/vulkan.hpp>
13hi_export_module(hikogui.GFX : gfx_pipeline_box_impl);
15hi_export
namespace hi {
inline namespace v1 {
17inline void gfx_pipeline_box::draw_in_command_buffer(vk::CommandBuffer commandBuffer, draw_context
const& context)
19 gfx_pipeline::draw_in_command_buffer(commandBuffer, context);
21 hi_axiom_not_null(device());
22 device()->flushAllocation(vertexBufferAllocation, 0, vertexBufferData.size() *
sizeof(vertex));
26 hi_assert(tmpvertexBuffers.
size() == tmpOffsets.
size());
28 device()->box_pipeline->drawInCommandBuffer(commandBuffer);
30 commandBuffer.bindVertexBuffers(0, tmpvertexBuffers, tmpOffsets);
32 pushConstants.windowExtent = extent2{narrow_cast<float>(extent.width), narrow_cast<float>(extent.height)};
33 pushConstants.viewportScale = scale2{2.0f / extent.width, 2.0f / extent.height};
34 commandBuffer.pushConstants(
36 vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
38 sizeof(push_constants),
41 auto const numberOfRectangles = vertexBufferData.size() / 4;
42 auto const numberOfTriangles = numberOfRectangles * 2;
44 device()->cmdBeginDebugUtilsLabelEXT(commandBuffer,
"draw boxes");
45 commandBuffer.drawIndexed(narrow_cast<uint32_t>(numberOfTriangles * 3), 1, 0, 0, 0);
46 device()->cmdEndDebugUtilsLabelEXT(commandBuffer);
51 hi_axiom_not_null(device());
52 return device()->box_pipeline->shaderStages;
65inline size_t gfx_pipeline_box::getDescriptorSetVersion()
const
72 return push_constants::pushConstantRanges();
75inline vk::VertexInputBindingDescription gfx_pipeline_box::createVertexInputBindingDescription()
const
77 return vertex::inputBindingDescription();
82 return vertex::inputAttributeDescriptions();
85inline void gfx_pipeline_box::build_vertex_buffers()
87 using vertexIndexType = uint16_t;
88 constexpr ssize_t numberOfVertices = 1 << (
sizeof(vertexIndexType) * CHAR_BIT);
90 vk::BufferCreateInfo
const bufferCreateInfo = {
91 vk::BufferCreateFlags(),
92 sizeof(vertex) * numberOfVertices,
93 vk::BufferUsageFlagBits::eVertexBuffer,
94 vk::SharingMode::eExclusive};
95 VmaAllocationCreateInfo allocationCreateInfo = {};
96 allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;
97 allocationCreateInfo.pUserData =
const_cast<char *
>(
"box-pipeline vertex buffer");
98 allocationCreateInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
100 hi_axiom_not_null(device());
101 std::tie(vertexBuffer, vertexBufferAllocation) = device()->createBuffer(bufferCreateInfo, allocationCreateInfo);
102 device()->setDebugUtilsObjectNameEXT(vertexBuffer,
"box-pipeline vertex buffer");
103 vertexBufferData = device()->mapMemory<vertex>(vertexBufferAllocation);
106inline void gfx_pipeline_box::teardown_vertex_buffers()
108 hi_axiom_not_null(device());
109 device()->unmapMemory(vertexBufferAllocation);
110 device()->destroyBuffer(vertexBuffer, vertexBufferAllocation);
113inline gfx_pipeline_box::device_shared::device_shared(gfx_device
const &device) : device(device)
118inline gfx_pipeline_box::device_shared::~device_shared() {}
120inline void gfx_pipeline_box::device_shared::destroy(gfx_device
const *vulkanDevice)
122 hi_assert_not_null(vulkanDevice);
123 teardownShaders(vulkanDevice);
126inline void gfx_pipeline_box::device_shared::drawInCommandBuffer(vk::CommandBuffer
const &commandBuffer)
128 commandBuffer.bindIndexBuffer(device.quadIndexBuffer, 0, vk::IndexType::eUint16);
131inline void gfx_pipeline_box::device_shared::place_vertices(
132 vector_span<vertex> &vertices,
143 auto const extra_space = (line_width * 0.5f) + 1.0f;
144 auto const[box_, lengths] = expand_and_edge_hypots(box,
extent2{extra_space, extra_space});
153 auto const t0 = sfloat_rgba32{lengths._00xy()};
154 auto const t1 = sfloat_rgba32{lengths.x00w()};
155 auto const t2 = sfloat_rgba32{lengths._0yz0()};
156 auto const t3 = sfloat_rgba32{lengths.zw00()};
158 auto const clipping_rectangle_ = sfloat_rgba32{clipping_rectangle};
159 auto const corner_radii_ = sfloat_rgba32{corner_radii};
161 vertices.emplace_back(box_.p0, clipping_rectangle_, t0, corner_radii_, fill_colors.
p0, line_colors.
p0, line_width);
162 vertices.emplace_back(box_.p1, clipping_rectangle_, t1, corner_radii_, fill_colors.
p1, line_colors.
p1, line_width);
163 vertices.emplace_back(box_.p2, clipping_rectangle_, t2, corner_radii_, fill_colors.
p2, line_colors.
p2, line_width);
164 vertices.emplace_back(box_.p3, clipping_rectangle_, t3, corner_radii_, fill_colors.
p3, line_colors.
p3, line_width);
167inline void gfx_pipeline_box::device_shared::buildShaders()
169 vertexShaderModule = device.loadShader(URL(
"resource:box_vulkan.vert.spv"));
170 device.setDebugUtilsObjectNameEXT(vertexShaderModule,
"box-pipeline vertex shader");
172 fragmentShaderModule = device.loadShader(URL(
"resource:box_vulkan.frag.spv"));
173 device.setDebugUtilsObjectNameEXT(vertexShaderModule,
"box-pipeline fragment shader");
176 {vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eVertex, vertexShaderModule,
"main"},
177 {vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eFragment, fragmentShaderModule,
"main"}};
180inline void gfx_pipeline_box::device_shared::teardownShaders(gfx_device
const*vulkanDevice)
182 hi_assert_not_null(vulkanDevice);
183 vulkanDevice->destroy(vertexShaderModule);
184 vulkanDevice->destroy(fragmentShaderModule);
The HikoGUI namespace.
Definition array_generic.hpp:20
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:32
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A color for each corner of a quad.
Definition quad_color.hpp:22
color p0
left-bottom
Definition quad_color.hpp:24
color p2
left-top
Definition quad_color.hpp:26
color p3
right-top
Definition quad_color.hpp:27
color p1
right-bottom
Definition quad_color.hpp:25
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