From ce4e11686ea598f22b7392b18c6308f937bf11e7 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Mon, 25 Sep 2023 18:09:43 +0100 Subject: [PATCH] c/main: Use scratch image for layer renderer --- src/xrt/compositor/main/comp_layer_renderer.c | 115 +++++------------- src/xrt/compositor/main/comp_layer_renderer.h | 19 +-- src/xrt/compositor/main/comp_renderer.c | 73 ++++++----- 3 files changed, 66 insertions(+), 141 deletions(-) diff --git a/src/xrt/compositor/main/comp_layer_renderer.c b/src/xrt/compositor/main/comp_layer_renderer.c index 394bd643e..9d5b4a07d 100644 --- a/src/xrt/compositor/main/comp_layer_renderer.c +++ b/src/xrt/compositor/main/comp_layer_renderer.c @@ -354,49 +354,6 @@ _render_eye(struct comp_layer_renderer *self, } } -static bool -_init_frame_buffer(struct comp_layer_renderer *self, VkFormat format, VkRenderPass rp, uint32_t eye) -{ - struct vk_bundle *vk = self->vk; - - VkImageUsageFlags usage = // - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | // - VK_IMAGE_USAGE_SAMPLED_BIT | // - VK_IMAGE_USAGE_TRANSFER_SRC_BIT; // - - VkResult res = vk_create_image_simple(vk, self->extent, format, usage, &self->framebuffers[eye].memory, - &self->framebuffers[eye].image); - vk_check_error("vk_create_image_simple", res, false); - - VkImageSubresourceRange subresource_range = { - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - }; - - res = vk_create_view(vk, self->framebuffers[eye].image, VK_IMAGE_VIEW_TYPE_2D, format, subresource_range, - &self->framebuffers[eye].view); - - vk_check_error("vk_create_view", res, false); - - VkFramebufferCreateInfo framebuffer_info = { - .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, - .renderPass = rp, - .attachmentCount = 1, - .pAttachments = (VkImageView[]){self->framebuffers[eye].view}, - .width = self->extent.width, - .height = self->extent.height, - .layers = 1, - }; - - res = vk->vkCreateFramebuffer(vk->device, &framebuffer_info, NULL, &self->framebuffers[eye].handle); - vk_check_error("vkCreateFramebuffer", res, false); - - return true; -} - void comp_layer_renderer_allocate_layers(struct comp_layer_renderer *self, uint32_t layer_count) { @@ -426,8 +383,7 @@ static bool _init(struct comp_layer_renderer *self, struct vk_bundle *vk, struct render_shaders *s, - struct render_gfx_render_pass *rgrp, - VkExtent2D extent) + struct render_gfx_render_pass *rgrp) { self->vk = vk; @@ -436,8 +392,6 @@ _init(struct comp_layer_renderer *self, self->layer_count = 0; - self->extent = extent; - self->rgrp = rgrp; // binding indices used in layer.vert, layer.frag @@ -457,16 +411,6 @@ _init(struct comp_layer_renderer *self, } - for (uint32_t i = 0; i < 2; i++) { - if (!_init_frame_buffer( // - self, // - self->rgrp->format, // - self->rgrp->render_pass, // - i)) { // - return false; - } - } - if (!_init_descriptor_layout(self)) return false; if (!_init_descriptor_layout_equirect(self)) @@ -506,13 +450,10 @@ _init(struct comp_layer_renderer *self, } struct comp_layer_renderer * -comp_layer_renderer_create(struct vk_bundle *vk, - struct render_shaders *s, - struct render_gfx_render_pass *rgrp, - VkExtent2D extent) +comp_layer_renderer_create(struct vk_bundle *vk, struct render_shaders *s, struct render_gfx_render_pass *rgrp) { struct comp_layer_renderer *r = U_TYPED_CALLOC(struct comp_layer_renderer); - _init(r, vk, s, rgrp, extent); + _init(r, vk, s, rgrp); return r; } @@ -560,28 +501,30 @@ static void _render_stereo(struct comp_layer_renderer *self, struct vk_bundle *vk, VkCommandBuffer cmd_buffer, + VkExtent2D extent, + VkFramebuffer framebuffers[2], const VkClearColorValue *color) { COMP_TRACE_MARKER(); VkViewport viewport = { - 0.0f, 0.0f, (float)self->extent.width, (float)self->extent.height, 0.0f, 1.0f, + 0.0f, 0.0f, (float)extent.width, (float)extent.height, 0.0f, 1.0f, }; vk->vkCmdSetViewport(cmd_buffer, 0, 1, &viewport); VkRect2D scissor = { .offset = {0, 0}, - .extent = self->extent, + .extent = extent, }; vk->vkCmdSetScissor(cmd_buffer, 0, 1, &scissor); for (uint32_t eye = 0; eye < 2; eye++) { - _render_pass_begin( // - vk, // - self->rgrp->render_pass, // - self->extent, // - *color, // - self->framebuffers[eye].handle, // - cmd_buffer); // + _render_pass_begin( // + vk, // + self->rgrp->render_pass, // + extent, // + *color, // + framebuffers[eye], // + cmd_buffer); // _render_eye(self, eye, cmd_buffer, self->pipeline_layout); @@ -590,7 +533,9 @@ _render_stereo(struct comp_layer_renderer *self, } void -comp_layer_renderer_draw(struct comp_layer_renderer *self) +comp_layer_renderer_draw(struct comp_layer_renderer *self, + struct render_gfx_target_resources *rtr_left, + struct render_gfx_target_resources *rtr_right) { COMP_TRACE_MARKER(); VkResult ret; @@ -608,10 +553,19 @@ comp_layer_renderer_draw(struct comp_layer_renderer *self) return; } + VkExtent2D extent = rtr_left->extent; + assert(extent.width == rtr_right->extent.width); + assert(extent.height == rtr_right->extent.height); + + VkFramebuffer framebuffers[2] = { + rtr_left->framebuffer, + rtr_right->framebuffer, + }; + if (self->layer_count == 0) { - _render_stereo(self, vk, cmd_buffer, &background_color_idle); + _render_stereo(self, vk, cmd_buffer, extent, framebuffers, &background_color_idle); } else { - _render_stereo(self, vk, cmd_buffer, &background_color_active); + _render_stereo(self, vk, cmd_buffer, extent, framebuffers, &background_color_active); } // Done writing commands, submit to queue, waits for command to finish. @@ -624,16 +578,6 @@ comp_layer_renderer_draw(struct comp_layer_renderer *self) vk_check_error("vk_submit_cmd_buffer", ret, ); } -static void -_destroy_framebuffer(struct comp_layer_renderer *self, uint32_t i) -{ - struct vk_bundle *vk = self->vk; - vk->vkDestroyImageView(vk->device, self->framebuffers[i].view, NULL); - vk->vkDestroyImage(vk->device, self->framebuffers[i].image, NULL); - vk->vkFreeMemory(vk->device, self->framebuffers[i].memory, NULL); - vk->vkDestroyFramebuffer(vk->device, self->framebuffers[i].handle, NULL); -} - void comp_layer_renderer_destroy(struct comp_layer_renderer **ptr_clr) { @@ -655,9 +599,6 @@ comp_layer_renderer_destroy(struct comp_layer_renderer **ptr_clr) comp_layer_renderer_destroy_layers(self); - for (uint32_t i = 0; i < 2; i++) - _destroy_framebuffer(self, i); - vk->vkDestroyPipelineLayout(vk->device, self->pipeline_layout, NULL); vk->vkDestroyDescriptorSetLayout(vk->device, self->descriptor_set_layout, NULL); vk->vkDestroyDescriptorSetLayout(vk->device, self->descriptor_set_layout_equirect, NULL); diff --git a/src/xrt/compositor/main/comp_layer_renderer.h b/src/xrt/compositor/main/comp_layer_renderer.h index 71920e267..d43830ded 100644 --- a/src/xrt/compositor/main/comp_layer_renderer.h +++ b/src/xrt/compositor/main/comp_layer_renderer.h @@ -20,21 +20,11 @@ struct comp_layer_renderer { struct vk_bundle *vk; - struct - { - VkImage image; - VkDeviceMemory memory; - VkImageView view; - VkFramebuffer handle; - } framebuffers[2]; - struct vk_cmd_pool pool; //! Render pass used to create all pipelines. struct render_gfx_render_pass *rgrp; - VkExtent2D extent; - VkShaderModule shader_modules[2]; VkPipeline pipeline_premultiplied_alpha; VkPipeline pipeline_unpremultiplied_alpha; @@ -69,10 +59,7 @@ struct comp_layer_renderer * @public @memberof comp_layer_renderer */ struct comp_layer_renderer * -comp_layer_renderer_create(struct vk_bundle *vk, - struct render_shaders *s, - struct render_gfx_render_pass *rgrp, - VkExtent2D extent); +comp_layer_renderer_create(struct vk_bundle *vk, struct render_shaders *s, struct render_gfx_render_pass *rgrp); /*! * Destroy the layer renderer and set the pointer to NULL. @@ -90,7 +77,9 @@ comp_layer_renderer_destroy(struct comp_layer_renderer **ptr_clr); * @public @memberof comp_layer_renderer */ void -comp_layer_renderer_draw(struct comp_layer_renderer *self); +comp_layer_renderer_draw(struct comp_layer_renderer *self, + struct render_gfx_target_resources *rtr_left, + struct render_gfx_target_resources *rtr_right); /*! * Update the internal members derived from the field of view. diff --git a/src/xrt/compositor/main/comp_renderer.c b/src/xrt/compositor/main/comp_renderer.c index 2d8e2928b..d40d7ac1c 100644 --- a/src/xrt/compositor/main/comp_renderer.c +++ b/src/xrt/compositor/main/comp_renderer.c @@ -473,12 +473,10 @@ renderer_create_layer_renderer(struct comp_renderer *r) comp_layer_renderer_destroy(&r->lr); } - VkExtent2D extent = r->scratch.extent; r->lr = comp_layer_renderer_create( // vk, // &r->c->shaders, // - &r->scratch_render_pass, // - extent); // + &r->scratch_render_pass); // if (layer_count != 0) { comp_layer_renderer_allocate_layers(r->lr, layer_count); } @@ -603,7 +601,11 @@ renderer_init(struct comp_renderer *r, struct comp_compositor *c, VkExtent2D scr struct vk_bundle *vk = &r->c->base.vk; - VkResult ret = comp_mirror_init(&r->mirror_to_debug_gui, vk, &c->shaders, r->lr->extent); + VkResult ret = comp_mirror_init( // + &r->mirror_to_debug_gui, // + vk, // + &c->shaders, // + r->scratch.extent); // if (ret != VK_SUCCESS) { COMP_ERROR(c, "comp_mirror_init: %s", vk_result_string(ret)); assert(false && "Whelp, can't return a error. But should never really fail."); @@ -941,7 +943,10 @@ dispatch_graphics(struct comp_renderer *r, struct render_gfx *rr) comp_target_mark_submit(ct, c->frame.rendering.id, os_monotonic_get_ns()); renderer_get_view_projection(r); - comp_layer_renderer_draw(r->lr); + comp_layer_renderer_draw( // + r->lr, // + &r->scratch_targets[0], // + &r->scratch_targets[1]); // VkSampler clamp_to_border_black = r->c->nr.samplers.clamp_to_border_black; VkSampler src_samplers[2] = { @@ -949,8 +954,8 @@ dispatch_graphics(struct comp_renderer *r, struct render_gfx *rr) clamp_to_border_black, }; VkImageView src_image_views[2] = { - r->lr->framebuffers[0].view, - r->lr->framebuffers[1].view, + r->scratch.color[0].srgb_view, + r->scratch.color[1].srgb_view, }; struct xrt_normalized_rect src_norm_rects[2] = { @@ -1441,12 +1446,18 @@ comp_renderer_draw(struct comp_renderer *r) if (c->peek) { switch (comp_window_peek_get_eye(c->peek)) { case COMP_WINDOW_PEEK_EYE_LEFT: - comp_window_peek_blit(c->peek, r->lr->framebuffers[0].image, r->lr->extent.width, - r->lr->extent.height); + comp_window_peek_blit( // + c->peek, // + r->scratch.color[0].image, // + r->scratch.extent.width, // + r->scratch.extent.height); // break; case COMP_WINDOW_PEEK_EYE_RIGHT: - comp_window_peek_blit(c->peek, r->lr->framebuffers[1].image, r->lr->extent.width, - r->lr->extent.height); + comp_window_peek_blit( // + c->peek, // + r->scratch.color[1].image, // + r->scratch.extent.width, // + r->scratch.extent.height); // break; case COMP_WINDOW_PEEK_EYE_BOTH: /* TODO: display the undistorted image */ @@ -1474,35 +1485,19 @@ comp_renderer_draw(struct comp_renderer *r) // Used for both, want clamp to edge to no bring in black. VkSampler clamp_to_edge = c->nr.samplers.clamp_to_edge; - if (use_compute) { - // Covers only the first half of the view. - struct xrt_normalized_rect rect = {0, 0, 1.0f, 1.0f}; + // Covers the whole view. + struct xrt_normalized_rect rect = {0, 0, 1.0f, 1.0f}; - comp_mirror_do_blit( // - &r->mirror_to_debug_gui, // - &c->base.vk, // - frame_id, // - predicted_display_time_ns, // - r->scratch.color[0].image, // - r->scratch.color[0].srgb_view, // - clamp_to_edge, // - r->scratch.extent, // - rect); // - } else { - // Covers the whole view. - struct xrt_normalized_rect rect = {0, 0, 1.0f, 1.0f}; - - comp_mirror_do_blit( // - &r->mirror_to_debug_gui, // - &c->base.vk, // - frame_id, // - predicted_display_time_ns, // - r->lr->framebuffers[0].image, // - r->lr->framebuffers[0].view, // - clamp_to_edge, // - r->lr->extent, // - rect); // - } + comp_mirror_do_blit( // + &r->mirror_to_debug_gui, // + &c->base.vk, // + frame_id, // + predicted_display_time_ns, // + r->scratch.color[0].image, // + r->scratch.color[0].srgb_view, // + clamp_to_edge, // + r->scratch.extent, // + rect); // } /*