From b0db98d71aeb32d9e57315b8a06ff4068aeccbca Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 12 Dec 2021 23:44:04 +0200 Subject: [PATCH] Refactor handling of viewport Y axis Only invert it for the swapchain render target. This makes it easier to sample render target textures consistently. --- source/backends/vulkan/camera_backend.cpp | 1 - source/backends/vulkan/commands_backend.cpp | 14 +++++++------- source/backends/vulkan/commands_backend.h | 1 + source/backends/vulkan/pipelinestate_backend.cpp | 11 ++++++++--- source/backends/vulkan/pipelinestate_backend.h | 2 +- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/source/backends/vulkan/camera_backend.cpp b/source/backends/vulkan/camera_backend.cpp index 9eba4289..10e294f7 100644 --- a/source/backends/vulkan/camera_backend.cpp +++ b/source/backends/vulkan/camera_backend.cpp @@ -7,7 +7,6 @@ namespace GL { void VulkanCamera::adjust_projection_matrix(Matrix &proj_matrix) { Matrix adjust; - adjust(1, 1) = -1.0f; adjust(2, 2) = 0.5f; adjust(2, 3) = 0.5f; proj_matrix = adjust*proj_matrix; diff --git a/source/backends/vulkan/commands_backend.cpp b/source/backends/vulkan/commands_backend.cpp index 643710d5..9c074c87 100644 --- a/source/backends/vulkan/commands_backend.cpp +++ b/source/backends/vulkan/commands_backend.cpp @@ -81,14 +81,14 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value Synchronizer &sync = device.get_synchronizer(); sync.reset(); - bool to_present = false; + fb_is_swapchain = false; unsigned n_attachments = framebuffer->get_format().size(); - for(unsigned i=0; i(framebuffer->get_attachment(i))) - to_present = true; - if(!to_present) + fb_is_swapchain = true; + if(!fb_is_swapchain) framebuffer->prepare_image_layouts(clear && !viewport); - VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, (!clear_values && !viewport), to_present); + VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, (!clear_values && !viewport), fb_is_swapchain); framebuffer->refresh(); @@ -102,7 +102,7 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value if(viewport) { begin_info.renderArea.offset.x = viewport->left; - begin_info.renderArea.offset.y = framebuffer->get_height()-(viewport->bottom+viewport->height); + begin_info.renderArea.offset.y = viewport->bottom; begin_info.renderArea.extent.width = viewport->width; begin_info.renderArea.extent.height = viewport->height; } @@ -233,7 +233,7 @@ void VulkanCommands::draw_instanced(const Batch &batch, unsigned count) begin_render_pass(false, 0); pipeline_state->refresh(); - pipeline_state->apply(current_buffer); + pipeline_state->apply(current_buffer, fb_is_swapchain); unsigned first_index = batch.get_offset()/batch.get_index_size(); vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0); } diff --git a/source/backends/vulkan/commands_backend.h b/source/backends/vulkan/commands_backend.h index a89225a6..994acca2 100644 --- a/source/backends/vulkan/commands_backend.h +++ b/source/backends/vulkan/commands_backend.h @@ -42,6 +42,7 @@ protected: const PipelineState *pipeline_state = 0; const Framebuffer *framebuffer = 0; const Rect *viewport = 0; + bool fb_is_swapchain = false; VulkanCommands(); ~VulkanCommands(); diff --git a/source/backends/vulkan/pipelinestate_backend.cpp b/source/backends/vulkan/pipelinestate_backend.cpp index 419a7e4c..a1a03d3d 100644 --- a/source/backends/vulkan/pipelinestate_backend.cpp +++ b/source/backends/vulkan/pipelinestate_backend.cpp @@ -356,7 +356,7 @@ unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector(this); const VulkanFunctions &vk = device.get_functions(); @@ -387,7 +387,7 @@ void VulkanPipelineState::apply(VkCommandBuffer command_buffer) const if(self.viewport) { viewport.x = self.viewport->left; - viewport.y = self.framebuffer->get_height()-(self.viewport->bottom+self.viewport->height); + viewport.y = self.viewport->bottom; viewport.width = self.viewport->width; viewport.height = self.viewport->height; } @@ -398,6 +398,11 @@ void VulkanPipelineState::apply(VkCommandBuffer command_buffer) const viewport.width = self.framebuffer->get_width(); viewport.height = self.framebuffer->get_height(); } + if(negative_viewport) + { + viewport.y += viewport.height; + viewport.height = -viewport.height; + } viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; vk.CmdSetViewport(command_buffer, 0, 1, &viewport); @@ -406,7 +411,7 @@ void VulkanPipelineState::apply(VkCommandBuffer command_buffer) const if(self.scissor) { scissor.offset.x = self.scissor->left; - scissor.offset.y = self.framebuffer->get_height()-(self.scissor->bottom+self.scissor->height); + scissor.offset.y = self.scissor->bottom; scissor.extent.width = self.scissor->width; scissor.extent.height = self.scissor->height; } diff --git a/source/backends/vulkan/pipelinestate_backend.h b/source/backends/vulkan/pipelinestate_backend.h index e675ff33..19f7f902 100644 --- a/source/backends/vulkan/pipelinestate_backend.h +++ b/source/backends/vulkan/pipelinestate_backend.h @@ -33,7 +33,7 @@ protected: VkDescriptorSetLayout get_descriptor_set_layout(unsigned) const; unsigned fill_descriptor_writes(unsigned, std::vector &) const; - void apply(VkCommandBuffer) const; + void apply(VkCommandBuffer, bool) const; }; using PipelineStateBackend = VulkanPipelineState; -- 2.43.0