UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
vulkan_transfer_image.hpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2021-2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#pragma once
39#include "vulkan_context.hpp"
40#include <functional>
41
42namespace vulkan_display {
43
56
57struct ImageDescription;
58
59} // vulkan_display-----------------------------------------
60
61namespace vulkan_display_detail {
62
69
71
73 using VkF = vk::Format;
74
75 static std::array<FormatInfo, 11> format_infos = {{
76{F::uninitialized, VkF::eUndefined, },
77{F::RGBA8, VkF::eR8G8B8A8Unorm, },
78{F::RGB8, VkF::eR8G8B8Srgb, },
79{F::UYVY8_422, VkF::eB8G8R8G8422Unorm, },
80{F::UYVY8_422_conv, VkF::eR8G8B8A8Unorm, {"UYVY8_conv"}, VkF::eR8G8B8A8Unorm},
81{F::YUYV8_422, VkF::eG8B8G8R8422Unorm, },
82{F::YUYV16_422, VkF::eG16B16G16R16422Unorm, },
83{F::UYVA16_422_conv, VkF::eR16G16B16A16Uint, {"UYVA16_conv"}, VkF::eR16G16B16A16Sfloat},
84{F::RGB10A2_conv, VkF::eR8G8B8A8Uint, {"RGB10A2_conv"}, VkF::eA2B10G10R10UnormPack32},
85{F::RGB16, VkF::eR16G16B16Unorm },
86 }};
87
88 auto& result = format_infos[static_cast<size_t>(format)];
89 assert(result.format == format);
90 return result;
91}
92
94
95} //vulkan_display_detail
96
97namespace vulkan_display{
98
100 vk::Extent2D size{};
102
103 ImageDescription() = default;
105 size{ size }, format{ format } { }
106 ImageDescription(uint32_t width, uint32_t height, Format format) :
107 ImageDescription{ vk::Extent2D{width, height}, format } { }
108
109 bool operator==(const ImageDescription& other) const {
110 return size.width == other.size.width
111 && size.height == other.size.height
112 && format == other.format;
113 }
114
115 bool operator!=(const ImageDescription& other) const {
116 return !(*this == other);
117 }
118
120};
121
122class TransferImage;
123
124} //vulkan_display
125
126namespace vulkan_display_detail{
127
131
135
137public:
138 vk::DeviceMemory memory{};
139 vk::Image image{};
140 vk::ImageLayout layout{};
141 vk::AccessFlags access{};
142
143 vk::ImageView view{};
144
145 size_t byte_size{};
146
147 vk::Extent2D size{};
148 vk::Format format{};
149public:
150 void init(VulkanContext& context,
151 vk::Extent2D size, vk::Format format, vk::ImageUsageFlags usage,
152 vk::AccessFlags initial_access, InitialImageData preinitialised, MemoryLocation memory_location);
153
154 void init(VulkanContext& context,
155 vk::Extent2D size, vk::Format format, vk::ImageUsageFlags usage,
156 vk::AccessFlags initial_access, InitialImageData preinitialised, vk::ImageTiling tiling,
157 vk::MemoryPropertyFlags requested_properties, vk::MemoryPropertyFlags optional_properties);
158
159 vk::ImageView get_image_view(vk::Device device, vk::SamplerYcbcrConversion conversion);
160
161 void destroy(vk::Device device);
162
163 vk::ImageMemoryBarrier create_memory_barrier(vk::ImageLayout new_layout, vk::AccessFlags new_access_mask,
164 uint32_t src_queue_family_index = VK_QUEUE_FAMILY_IGNORED,
165 uint32_t dst_queue_family_index = VK_QUEUE_FAMILY_IGNORED);
166};
167
170 uint32_t id = NO_ID;
171 unsigned char* ptr = nullptr;
172
173 vk::DeviceSize row_pitch = 0;
174
175 using PreprocessFunction = std::function<void(vulkan_display::TransferImage& image)>;
177
179
180public:
182
183 static constexpr uint32_t NO_ID = UINT32_MAX;
184
185 vk::Fence is_available_fence; // is_available_fence becames signalled when gpu releases the image
186
187 uint32_t get_id() const { return id; }
188
192
193 void init(vk::Device device, uint32_t id);
194
196
198 return buffer;
199 }
200
201 vk::ImageView get_image_view(vk::Device device, vk::SamplerYcbcrConversion conversion) {
202 return buffer.get_image_view(device, conversion);
203 }
204
205 void preprocess();
206
207 void destroy(vk::Device device);
208
209 TransferImageImpl() = default;
210 TransferImageImpl(vk::Device device, uint32_t id) {
211 init(device, id);
212 }
213};
214
215} // vulkan_display_detail
216
217
218namespace vulkan_display {
219
221
223public:
224 TransferImage() = default;
225 TransferImage(std::nullptr_t){}
226
228 transfer_image{ &image }
229 {
230 assert(image.id != detail::TransferImageImpl::NO_ID);
231 }
232
233 uint32_t get_id() const {
234 assert(transfer_image);
235 return transfer_image->id;
236 }
237
238 unsigned char* get_memory_ptr() {
239 assert(transfer_image);
240 return transfer_image->ptr;
241 }
242
246
247 vk::DeviceSize get_row_pitch() const {
248 assert(transfer_image);
250 }
251
252 vk::Extent2D get_size() const {
254 }
255
259
260 void set_process_function(std::function<void(TransferImage& image)> function) {
261 transfer_image->preprocess_fun = std::move(function);
262 }
263
264 bool operator==(TransferImage other) const {
265 return transfer_image == other.transfer_image;
266 }
267};
268
269} //namespace vulkan_display
270
void set_process_function(std::function< void(TransferImage &image)> function)
bool operator==(TransferImage other) const
detail::TransferImageImpl * transfer_image
vulkan_display_detail::TransferImageImpl * get_transfer_image()
TransferImage(detail::TransferImageImpl &image)
void init(VulkanContext &context, vk::Extent2D size, vk::Format format, vk::ImageUsageFlags usage, vk::AccessFlags initial_access, InitialImageData preinitialised, MemoryLocation memory_location)
vk::ImageMemoryBarrier create_memory_barrier(vk::ImageLayout new_layout, vk::AccessFlags new_access_mask, uint32_t src_queue_family_index=VK_QUEUE_FAMILY_IGNORED, uint32_t dst_queue_family_index=VK_QUEUE_FAMILY_IGNORED)
vk::ImageView get_image_view(vk::Device device, vk::SamplerYcbcrConversion conversion)
std::function< void(vulkan_display::TransferImage &image)> PreprocessFunction
void recreate(VulkanContext &context, vulkan_display::ImageDescription description)
vk::ImageView get_image_view(vk::Device device, vk::SamplerYcbcrConversion conversion)
vulkan_display::ImageDescription get_image_description() const
void init(vk::Device device, uint32_t id)
vulkan_display::ImageDescription image_description
TransferImageImpl(vk::Device device, uint32_t id)
void usage()
Definition main.cpp:28
#define F(x, y, z)
Definition md5.c:63
vk::Extent2D get_buffer_size(const vulkan_display::ImageDescription &description)
const FormatInfo & format_info(vulkan_display::Format format)
bool operator!=(const ImageDescription &other) const
bool operator==(const ImageDescription &other) const
const detail::FormatInfo & format_info() const
ImageDescription(uint32_t width, uint32_t height, Format format)
ImageDescription(vk::Extent2D size, Format format)
const char * description
Definition cineform.cpp:126