|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "impeller/renderer/backend/vulkan/blit_command_vk.h" |
| 6 | + |
| 7 | +#include "impeller/renderer/backend/vulkan/commands_vk.h" |
| 8 | +#include "impeller/renderer/backend/vulkan/texture_vk.h" |
| 9 | + |
| 10 | +namespace impeller { |
| 11 | + |
| 12 | +BlitEncodeVK::~BlitEncodeVK() = default; |
| 13 | + |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | +/// BlitCopyTextureToTextureCommandVK |
| 16 | +/// |
| 17 | + |
| 18 | +BlitCopyTextureToTextureCommandVK::~BlitCopyTextureToTextureCommandVK() = |
| 19 | + default; |
| 20 | + |
| 21 | +std::string BlitCopyTextureToTextureCommandVK::GetLabel() const { |
| 22 | + return label; |
| 23 | +} |
| 24 | + |
| 25 | +[[nodiscard]] bool BlitCopyTextureToTextureCommandVK::Encode( |
| 26 | + FencedCommandBufferVK* fenced_command_buffer) const { |
| 27 | + // cast source and destination to TextureVK |
| 28 | + const auto& source_tex_vk = TextureVK::Cast(*source); |
| 29 | + const auto& dest_tex_vk = TextureVK::Cast(*destination); |
| 30 | + |
| 31 | + // get the vulkan image and image view |
| 32 | + const auto source_image = source_tex_vk.GetImage(); |
| 33 | + const auto dest_image = dest_tex_vk.GetImage(); |
| 34 | + |
| 35 | + // copy the source image to the destination image, from source_region to |
| 36 | + // destination_origin. |
| 37 | + vk::ImageCopy image_copy; |
| 38 | + image_copy.setSrcSubresource( |
| 39 | + vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1)); |
| 40 | + image_copy.setDstSubresource( |
| 41 | + vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1)); |
| 42 | + |
| 43 | + image_copy.srcOffset = |
| 44 | + vk::Offset3D(source_region.origin.x, source_region.origin.y, 0); |
| 45 | + image_copy.dstOffset = |
| 46 | + vk::Offset3D(destination_origin.x, destination_origin.y, 0); |
| 47 | + image_copy.extent = |
| 48 | + vk::Extent3D(source_region.size.width, source_region.size.height, 1); |
| 49 | + |
| 50 | + // get single use command buffer |
| 51 | + auto copy_cmd = fenced_command_buffer->GetSingleUseChild(); |
| 52 | + |
| 53 | + vk::CommandBufferBeginInfo begin_info; |
| 54 | + begin_info.setFlags(vk::CommandBufferUsageFlagBits::eOneTimeSubmit); |
| 55 | + auto res = copy_cmd.begin(begin_info); |
| 56 | + |
| 57 | + if (res != vk::Result::eSuccess) { |
| 58 | + VALIDATION_LOG << "Failed to begin command buffer: " << vk::to_string(res); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + // transition the source image to transfer source optimal |
| 63 | + TransitionImageLayoutCommandVK transition_source_cmd = |
| 64 | + TransitionImageLayoutCommandVK(source_image, vk::ImageLayout::eUndefined, |
| 65 | + vk::ImageLayout::eTransferSrcOptimal); |
| 66 | + bool success = transition_source_cmd.Submit(fenced_command_buffer); |
| 67 | + if (!success) { |
| 68 | + VALIDATION_LOG << "Failed to transition source image layout"; |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + // transition the destination image to transfer destination optimal |
| 73 | + TransitionImageLayoutCommandVK transition_dest_cmd = |
| 74 | + TransitionImageLayoutCommandVK(dest_image, vk::ImageLayout::eUndefined, |
| 75 | + vk::ImageLayout::eTransferDstOptimal); |
| 76 | + success = transition_dest_cmd.Submit(fenced_command_buffer); |
| 77 | + if (!success) { |
| 78 | + VALIDATION_LOG << "Failed to transition destination image layout"; |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // issue the copy command |
| 83 | + copy_cmd.copyImage(source_image, vk::ImageLayout::eTransferSrcOptimal, |
| 84 | + dest_image, vk::ImageLayout::eTransferDstOptimal, |
| 85 | + image_copy); |
| 86 | + res = copy_cmd.end(); |
| 87 | + if (res != vk::Result::eSuccess) { |
| 88 | + VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(res); |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +//------------------------------------------------------------------------------ |
| 96 | +/// BlitCopyTextureToBufferCommandVK |
| 97 | +/// |
| 98 | + |
| 99 | +BlitCopyTextureToBufferCommandVK::~BlitCopyTextureToBufferCommandVK() = default; |
| 100 | + |
| 101 | +std::string BlitCopyTextureToBufferCommandVK::GetLabel() const { |
| 102 | + return label; |
| 103 | +} |
| 104 | + |
| 105 | +[[nodiscard]] bool BlitCopyTextureToBufferCommandVK::Encode( |
| 106 | + FencedCommandBufferVK* fenced_command_buffer) const { |
| 107 | + // cast source and destination to TextureVK |
| 108 | + const auto& source_tex_vk = TextureVK::Cast(*source); |
| 109 | + const auto& dest_buf_vk = DeviceBufferVK::Cast(*destination); |
| 110 | + |
| 111 | + // get the vulkan image and image view |
| 112 | + const auto source_image = source_tex_vk.GetImage(); |
| 113 | + |
| 114 | + // get buffer image handle |
| 115 | + const auto dest_buffer = dest_buf_vk.GetVKBufferHandle(); |
| 116 | + |
| 117 | + // copy the source image to the destination buffer, from source_region to |
| 118 | + // destination_origin. |
| 119 | + vk::BufferImageCopy image_copy{}; |
| 120 | + image_copy.setBufferOffset(destination_offset); |
| 121 | + image_copy.setBufferRowLength(0); |
| 122 | + image_copy.setBufferImageHeight(0); |
| 123 | + image_copy.setImageSubresource( |
| 124 | + vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1)); |
| 125 | + image_copy.setImageOffset( |
| 126 | + vk::Offset3D(source_region.origin.x, source_region.origin.y, 0)); |
| 127 | + image_copy.setImageExtent( |
| 128 | + vk::Extent3D(source_region.size.width, source_region.size.height, 1)); |
| 129 | + |
| 130 | + // transition the source image to transfer source optimal |
| 131 | + TransitionImageLayoutCommandVK transition_source_cmd = |
| 132 | + TransitionImageLayoutCommandVK(source_image, vk::ImageLayout::eUndefined, |
| 133 | + vk::ImageLayout::eTransferSrcOptimal); |
| 134 | + bool success = transition_source_cmd.Submit(fenced_command_buffer); |
| 135 | + if (!success) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + // get single use command buffer |
| 140 | + auto copy_cmd = fenced_command_buffer->GetSingleUseChild(); |
| 141 | + |
| 142 | + vk::CommandBufferBeginInfo begin_info; |
| 143 | + begin_info.setFlags(vk::CommandBufferUsageFlagBits::eOneTimeSubmit); |
| 144 | + auto res = copy_cmd.begin(begin_info); |
| 145 | + |
| 146 | + if (res != vk::Result::eSuccess) { |
| 147 | + VALIDATION_LOG << "Failed to begin command buffer: " << vk::to_string(res); |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + // issue the copy command |
| 152 | + copy_cmd.copyImageToBuffer(source_image, vk::ImageLayout::eTransferSrcOptimal, |
| 153 | + dest_buffer, image_copy); |
| 154 | + res = copy_cmd.end(); |
| 155 | + if (res != vk::Result::eSuccess) { |
| 156 | + VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(res); |
| 157 | + } |
| 158 | + |
| 159 | + return true; |
| 160 | +} |
| 161 | + |
| 162 | +//------------------------------------------------------------------------------ |
| 163 | +/// BlitGenerateMipmapCommandVK |
| 164 | +/// |
| 165 | + |
| 166 | +BlitGenerateMipmapCommandVK::~BlitGenerateMipmapCommandVK() = default; |
| 167 | + |
| 168 | +std::string BlitGenerateMipmapCommandVK::GetLabel() const { |
| 169 | + return label; |
| 170 | +} |
| 171 | + |
| 172 | +[[nodiscard]] bool BlitGenerateMipmapCommandVK::Encode( |
| 173 | + FencedCommandBufferVK* fenced_command_buffer) const { |
| 174 | + // TODO(https://github.com/flutter/flutter/issues/120134): Support generating |
| 175 | + // mipmaps on Vulkan. |
| 176 | + IMPELLER_UNIMPLEMENTED; |
| 177 | + return true; |
| 178 | +} |
| 179 | + |
| 180 | +// END: BlitGenerateMipmapCommandVK |
| 181 | + |
| 182 | +} // namespace impeller |
0 commit comments