HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gfx_pipeline_alpha_vulkan_impl.hpp
1// Copyright Take Vos 2022.
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_alpha_vulkan.hpp"
8#include "gfx_device_vulkan_impl.hpp"
9#include "../macros.hpp"
10
11namespace hi { inline namespace v1 {
12
13/* Do not blend, simply use just the alpha channel and overwrite the pixels in the color attachment directly.
14 */
15inline std::vector<vk::PipelineColorBlendAttachmentState> gfx_pipeline_alpha::getPipelineColorBlendAttachmentStates() const
16{
17 return {
18 {VK_FALSE, // blendEnable
19 vk::BlendFactor::eOne, // srcColorBlendFactor
20 vk::BlendFactor::eZero, // dstColorBlendFactor
21 vk::BlendOp::eAdd, // colorBlendOp
22 vk::BlendFactor::eOne, // srcAlphaBlendFactor
23 vk::BlendFactor::eZero, // dstAlphaBlendFactor
24 vk::BlendOp::eAdd, // aphaBlendOp
25 vk::ColorComponentFlagBits::eA}};
26}
27
28inline void gfx_pipeline_alpha::draw_in_command_buffer(vk::CommandBuffer commandBuffer, draw_context const& context)
29{
30 gfx_pipeline::draw_in_command_buffer(commandBuffer, context);
31
32 hi_axiom_not_null(device());
33 device()->flushAllocation(vertexBufferAllocation, 0, vertexBufferData.size() * sizeof(vertex));
34
37 hi_assert(tmpvertexBuffers.size() == tmpOffsets.size());
38
39 device()->alpha_pipeline->drawInCommandBuffer(commandBuffer);
40
41 commandBuffer.bindVertexBuffers(0, tmpvertexBuffers, tmpOffsets);
42
43 pushConstants.windowExtent = extent2{narrow_cast<float>(extent.width), narrow_cast<float>(extent.height)};
44 pushConstants.viewportScale = scale2{2.0f / extent.width, 2.0f / extent.height};
45 commandBuffer.pushConstants(
46 pipelineLayout,
47 vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
48 0,
49 sizeof(push_constants),
50 &pushConstants);
51
52 hilet numberOfRectangles = vertexBufferData.size() / 4;
54
55 device()->cmdBeginDebugUtilsLabelEXT(commandBuffer, "draw alpha overlays");
56 commandBuffer.drawIndexed(narrow_cast<uint32_t>(numberOfTriangles * 3), 1, 0, 0, 0);
57 device()->cmdEndDebugUtilsLabelEXT(commandBuffer);
58}
59
60inline std::vector<vk::PipelineShaderStageCreateInfo> gfx_pipeline_alpha::createShaderStages() const
61{
62 hi_axiom_not_null(device());
63 return device()->alpha_pipeline->shaderStages;
64}
65
66inline std::vector<vk::DescriptorSetLayoutBinding> gfx_pipeline_alpha::createDescriptorSetLayoutBindings() const
67{
68 return {};
69}
70
71inline std::vector<vk::WriteDescriptorSet> gfx_pipeline_alpha::createWriteDescriptorSet() const
72{
73 return {};
74}
75
76inline size_t gfx_pipeline_alpha::getDescriptorSetVersion() const
77{
78 return 0;
79}
80
81inline std::vector<vk::PushConstantRange> gfx_pipeline_alpha::createPushConstantRanges() const
82{
83 return push_constants::pushConstantRanges();
84}
85
86inline vk::VertexInputBindingDescription gfx_pipeline_alpha::createVertexInputBindingDescription() const
87{
88 return vertex::inputBindingDescription();
89}
90
91inline std::vector<vk::VertexInputAttributeDescription> gfx_pipeline_alpha::createVertexInputAttributeDescriptions() const
92{
93 return vertex::inputAttributeDescriptions();
94}
95
96inline void gfx_pipeline_alpha::build_vertex_buffers()
97{
98 using vertexIndexType = uint16_t;
99 constexpr ssize_t numberOfVertices = 1 << (sizeof(vertexIndexType) * CHAR_BIT);
100
101 vk::BufferCreateInfo const bufferCreateInfo = {
102 vk::BufferCreateFlags(),
103 sizeof(vertex) * numberOfVertices,
104 vk::BufferUsageFlagBits::eVertexBuffer,
105 vk::SharingMode::eExclusive};
108 allocationCreateInfo.pUserData = const_cast<char *>("alpha-pipeline vertex buffer");
110
111 hi_axiom_not_null(device());
112 std::tie(vertexBuffer, vertexBufferAllocation) = device()->createBuffer(bufferCreateInfo, allocationCreateInfo);
113 device()->setDebugUtilsObjectNameEXT(vertexBuffer, "alpha-pipeline vertex buffer");
114 vertexBufferData = device()->mapMemory<vertex>(vertexBufferAllocation);
115}
116
117inline void gfx_pipeline_alpha::teardown_vertex_buffers()
118{
119 hi_axiom_not_null(device());
120 device()->unmapMemory(vertexBufferAllocation);
121 device()->destroyBuffer(vertexBuffer, vertexBufferAllocation);
122}
123
124inline gfx_pipeline_alpha::device_shared::device_shared(gfx_device const& device) : device(device)
125{
126 buildShaders();
127}
128
129inline gfx_pipeline_alpha::device_shared::~device_shared() {}
130
131inline void gfx_pipeline_alpha::device_shared::destroy(gfx_device const*vulkanDevice)
132{
133 hi_assert_not_null(vulkanDevice);
134 teardownShaders(vulkanDevice);
135}
136
137inline void gfx_pipeline_alpha::device_shared::drawInCommandBuffer(vk::CommandBuffer const& commandBuffer)
138{
139 commandBuffer.bindIndexBuffer(device.quadIndexBuffer, 0, vk::IndexType::eUint16);
140}
141
142inline void gfx_pipeline_alpha::device_shared::place_vertices(vector_span<vertex>& vertices, aarectangle clipping_rectangle, quad box, float alpha)
143{
144 hilet clipping_rectangle_ = sfloat_rgba32{clipping_rectangle};
145
146 vertices.emplace_back(box.p0, clipping_rectangle_, alpha);
147 vertices.emplace_back(box.p1, clipping_rectangle_, alpha);
148 vertices.emplace_back(box.p2, clipping_rectangle_, alpha);
149 vertices.emplace_back(box.p3, clipping_rectangle_, alpha);
150}
151
152inline void gfx_pipeline_alpha::device_shared::buildShaders()
153{
154 vertexShaderModule = device.loadShader(URL("resource:alpha_vulkan.vert.spv"));
155 device.setDebugUtilsObjectNameEXT(vertexShaderModule, "alpha-pipeline vertex shader");
156
157 fragmentShaderModule = device.loadShader(URL("resource:alpha_vulkan.frag.spv"));
158 device.setDebugUtilsObjectNameEXT(vertexShaderModule, "alpha-pipeline fragment shader");
159
160 shaderStages = {
161 {vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eVertex, vertexShaderModule, "main"},
162 {vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eFragment, fragmentShaderModule, "main"}};
163}
164
165inline void gfx_pipeline_alpha::device_shared::teardownShaders(gfx_device const*vulkanDevice)
166{
167 hi_assert_not_null(vulkanDevice);
168 vulkanDevice->destroy(vertexShaderModule);
169 vulkanDevice->destroy(fragmentShaderModule);
170}
171
172}} // namespace hi::v1
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:33
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
T tie(T... args)