MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ setup_backend_service()

void MayaFlux::Core::BackendWindowHandler::setup_backend_service ( const std::shared_ptr< Registry::Service::DisplayService > &  display_service)

Definition at line 76 of file BackendWindowHandler.cpp.

77{
78 display_service->submit_and_present = [this](
79 const std::shared_ptr<void>& window_ptr,
80 uint64_t primary_cmd_bits) {
81 auto window = std::static_pointer_cast<Window>(window_ptr);
82 vk::CommandBuffer primary_cmd = *reinterpret_cast<vk::CommandBuffer*>(&primary_cmd_bits);
83
84 this->submit_and_present(window, primary_cmd);
85 };
86
87 display_service->wait_idle = [this]() {
88 m_context.get_device().waitIdle();
89 };
90
91 display_service->resize_surface = [this](const std::shared_ptr<void>& window_ptr, uint32_t width, uint32_t height) {
92 auto window = std::static_pointer_cast<Window>(window_ptr);
93 window->set_size(width, height);
94 for (auto& ctx : m_window_contexts) {
95 if (ctx.window == window) {
96 ctx.needs_recreation = true;
97 break;
98 }
99 }
100 };
101
102 display_service->get_swapchain_image_count = [this](const std::shared_ptr<void>& window_ptr) -> uint32_t {
103 auto window = std::static_pointer_cast<Window>(window_ptr);
104 for (const auto& ctx : m_window_contexts) {
105 if (ctx.window == window) {
106 return static_cast<uint32_t>(ctx.swapchain->get_image_count());
107 }
108 }
109 return 0;
110 };
111
112 display_service->get_swapchain_format = [this](const std::shared_ptr<void>& window_ptr) -> uint32_t {
113 auto window = std::static_pointer_cast<Window>(window_ptr);
114 for (const auto& ctx : m_window_contexts) {
115 if (ctx.window == window) {
116 return static_cast<int>(ctx.swapchain->get_image_format());
117 }
118 }
119 return 0;
120 };
121
122 display_service->get_swapchain_extent = [this](
123 const std::shared_ptr<void>& window_ptr,
124 uint32_t& out_width,
125 uint32_t& out_height) {
126 auto window = std::static_pointer_cast<Window>(window_ptr);
127 auto* context = find_window_context(window);
128
129 if (context && context->swapchain) {
130 auto extent = context->swapchain->get_extent();
131 out_width = extent.width;
132 out_height = extent.height;
133 } else {
134 out_width = 0;
135 out_height = 0;
136 }
137 };
138
139 display_service->acquire_next_swapchain_image = [this](const std::shared_ptr<void>& window_ptr) -> uint64_t {
140 auto window = std::static_pointer_cast<Window>(window_ptr);
141 auto* ctx = find_window_context(window);
142 if (!ctx) {
144 "Window '{}' not registered for swapchain acquisition",
145 window->get_create_info().title);
146 return 0;
147 }
148
149 auto device = m_context.get_device();
150 size_t frame_index = ctx->current_frame;
151 auto& in_flight = ctx->in_flight[frame_index];
152 auto& image_available = ctx->image_available[frame_index];
153
154 if (device.waitForFences(1, &in_flight, VK_TRUE, UINT64_MAX) == vk::Result::eTimeout) {
156 "Fence timeout during swapchain acquisition for window '{}'",
157 window->get_create_info().title);
158 return 0;
159 }
160
161 auto image_index_opt = ctx->swapchain->acquire_next_image(image_available);
162 if (!image_index_opt.has_value()) {
163 ctx->needs_recreation = true;
164 return 0;
165 }
166
167 ctx->current_image_index = image_index_opt.value();
168
169 if (device.resetFences(1, &in_flight) != vk::Result::eSuccess) {
171 "Failed to reset fence for window '{}'",
172 window->get_create_info().title);
173 return 0;
174 }
175
176 const auto& images = ctx->swapchain->get_images();
177 VkImage raw = images[ctx->current_image_index];
178 return reinterpret_cast<uint64_t>(raw);
179 };
180
181 display_service->get_current_image_view = [this](const std::shared_ptr<void>& window_ptr) -> void* {
182 auto window = std::static_pointer_cast<Window>(window_ptr);
183 auto* context = find_window_context(window);
184
185 if (!context || !context->swapchain) {
186 return nullptr;
187 }
188
189 const auto& image_views = context->swapchain->get_image_views();
190 if (context->current_image_index >= image_views.size()) {
192 "Invalid current_image_index {} for window '{}' (swapchain has {} images)",
193 context->current_image_index,
194 window->get_create_info().title,
195 image_views.size());
196 return nullptr;
197 }
198
199 static thread_local vk::ImageView view;
200 view = image_views[context->current_image_index];
201 return static_cast<void*>(&view);
202 };
203
204 display_service->get_current_swapchain_image = [this](const std::shared_ptr<void>& window_ptr) -> uint64_t {
205 auto window = std::static_pointer_cast<Window>(window_ptr);
206 auto* ctx = find_window_context(window);
207 if (!ctx || !ctx->swapchain)
208 return 0;
209
210 const auto& images = ctx->swapchain->get_images();
211 if (ctx->current_image_index >= images.size())
212 return 0;
213
214 return reinterpret_cast<uint64_t>(static_cast<VkImage>(images[ctx->current_image_index]));
215 };
216
217 display_service->readback_swapchain_region = [this](
218 const std::shared_ptr<void>& window_ptr,
219 void* dst,
220 uint32_t /*x_offset*/,
221 uint32_t /*y_offset*/,
222 uint32_t pixel_width,
223 uint32_t pixel_height,
224 size_t byte_count) -> bool {
225 auto window = std::static_pointer_cast<Window>(window_ptr);
226 auto* ctx = find_window_context(window);
227
228 if (!ctx || !ctx->swapchain) {
230 "readback_swapchain_region: window '{}' has no swapchain",
231 window->get_create_info().title);
232 return false;
233 }
234
235 if (!m_resource_manager) {
237 "readback_swapchain_region: resource manager not set");
238 return false;
239 }
240
241 const auto& images = ctx->swapchain->get_images();
242 if (ctx->current_image_index >= images.size()) {
244 "readback_swapchain_region: invalid image index {} for '{}'",
245 ctx->current_image_index, window->get_create_info().title);
246 return false;
247 }
248
249 auto proxy = std::make_shared<VKImage>(
250 pixel_width, pixel_height, 1U,
251 ctx->swapchain->get_image_format(),
254 1U, 1U,
256
257 VKImageResources res {};
258 res.image = images[ctx->current_image_index];
259 proxy->set_image_resources(res);
260 proxy->set_current_layout(vk::ImageLayout::ePresentSrcKHR);
261
263 proxy,
264 dst,
265 byte_count,
266 vk::ImageLayout::ePresentSrcKHR,
267 vk::PipelineStageFlagBits::eBottomOfPipe);
268
269 return true;
270 };
271
272 display_service->ensure_depth_attachment = [this](const std::shared_ptr<void>& window_ptr) {
273 auto window = std::static_pointer_cast<Window>(window_ptr);
274 auto* ctx = find_window_context(window);
275 if (!ctx) {
277 "ensure_depth_attachment: window '{}' not registered",
278 window->get_create_info().title);
279 return;
280 }
281 ensure_depth_image(*ctx);
282 };
283
284 display_service->get_depth_image_view = [this](const std::shared_ptr<void>& window_ptr) -> void* {
285 auto window = std::static_pointer_cast<Window>(window_ptr);
286 auto* ctx = find_window_context(window);
287 if (!ctx || !ctx->depth_image || !ctx->depth_image->is_initialized()) {
288 return nullptr;
289 }
290 static thread_local vk::ImageView view;
291 view = ctx->depth_image->get_image_view();
292 return static_cast<void*>(&view);
293 };
294
295 display_service->get_depth_format = [this](const std::shared_ptr<void>& window_ptr) -> uint32_t {
296 auto window = std::static_pointer_cast<Window>(window_ptr);
297 auto* ctx = find_window_context(window);
298 if (!ctx || !ctx->depth_image || !ctx->depth_image->is_initialized()) {
299 return static_cast<uint32_t>(vk::Format::eUndefined);
300 }
301 return static_cast<uint32_t>(ctx->depth_image->get_format());
302 };
303}
#define MF_RT_WARN(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
void download_image_data(std::shared_ptr< VKImage > image, void *data, size_t size, vk::ImageLayout restore_layout=vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlags restore_stage=vk::PipelineStageFlagBits::eFragmentShader)
Download data from an image into a caller-supplied buffer.
void ensure_depth_image(WindowRenderContext &ctx)
Ensure depth image exists at current swapchain extent.
std::vector< WindowRenderContext > m_window_contexts
void submit_and_present(const std::shared_ptr< Window > &window, const vk::CommandBuffer &command_buffer)
WindowRenderContext * find_window_context(const std::shared_ptr< Window > &window)
vk::Device get_device() const
Get logical device.
Definition VKContext.hpp:49
@ TEXTURE_2D
Sampled texture (shader read)
@ GraphicsCallback
Graphics/visual rendering callback - frame-rate real-time.
@ Core
Core engine, backend, subsystems.
@ IMAGE_COLOR
2D RGB/RGBA image

References MayaFlux::Journal::Core, MayaFlux::Core::BackendResourceManager::download_image_data(), ensure_depth_image(), find_window_context(), MayaFlux::Core::VKContext::get_device(), MayaFlux::Journal::GraphicsCallback, MayaFlux::Core::VKImageResources::image, MayaFlux::Kakshya::IMAGE_COLOR, m_context, m_resource_manager, m_window_contexts, MF_RT_ERROR, MF_RT_WARN, submit_and_present(), MayaFlux::Core::VKImage::TEXTURE_2D, and MayaFlux::Core::VKImage::TYPE_2D.

+ Here is the call graph for this function: