| Flutter Impeller |

texture_vk.cc
Go to the documentation of this file.
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/texture_vk.h"
6
7 #include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
8 #include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
9 #include "impeller/renderer/backend/vulkan/formats_vk.h"
10
11 namespace impeller {
12
13 TextureVK::TextureVK(std::weak_ptr<Context> context,
14 std::shared_ptr<TextureSourceVK> source)
15 : Texture(source->GetTextureDescriptor()),
16 context_(std::move(context)),
17 source_(std::move(source)) {}
18
19 TextureVK::~TextureVK() = default;
20
21 void TextureVK::SetLabel(std::string_view label) {
22 auto context = context_.lock();
23 if (!context) {
24 // The context may have died.
25 return;
26 }
27 ContextVK::Cast(*context).SetDebugName(GetImage(), label);
28 ContextVK::Cast(*context).SetDebugName(GetImageView(), label);
29 }
30
31 bool TextureVK::OnSetContents(const uint8_t* contents,
32 size_t length,
33 size_t slice) {
34 if (!IsValid() || !contents) {
35 return false;
36 }
37
38 const auto& desc = GetTextureDescriptor();
39
40 // Out of bounds access.
41 if (length != desc.GetByteSizeOfBaseMipLevel()) {
42 VALIDATION_LOG << "Illegal to set contents for invalid size.";
43 return false;
44 }
45
46 auto context = context_.lock();
47 if (!context) {
48 VALIDATION_LOG << "Context died before setting contents on texture.";
49 return false;
50 }
51
52 auto staging_buffer =
53 context->GetResourceAllocator()->CreateBufferWithCopy(contents, length);
54
55 if (!staging_buffer) {
56 VALIDATION_LOG << "Could not create staging buffer.";
57 return false;
58 }
59
60 auto cmd_buffer = context->CreateCommandBuffer();
61
62 if (!cmd_buffer) {
63 return false;
64 }
65
66 const auto encoder = CommandBufferVK::Cast(*cmd_buffer).GetEncoder();
67
68 if (!encoder->Track(staging_buffer) || !encoder->Track(source_)) {
69 return false;
70 }
71
72 const auto& vk_cmd_buffer = encoder->GetCommandBuffer();
73
74 BarrierVK barrier;
75 barrier.cmd_buffer = vk_cmd_buffer;
76 barrier.new_layout = vk::ImageLayout::eTransferDstOptimal;
77 barrier.src_access = {};
78 barrier.src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
79 barrier.dst_access = vk::AccessFlagBits::eTransferWrite;
80 barrier.dst_stage = vk::PipelineStageFlagBits::eTransfer;
81
82 if (!SetLayout(barrier)) {
83 return false;
84 }
85
86 vk::BufferImageCopy copy;
87 copy.bufferOffset = 0u;
88 copy.bufferRowLength = 0u; // 0u means tightly packed per spec.
89 copy.bufferImageHeight = 0u; // 0u means tightly packed per spec.
90 copy.imageOffset.x = 0u;
91 copy.imageOffset.y = 0u;
92 copy.imageOffset.z = 0u;
93 copy.imageExtent.width = desc.size.width;
94 copy.imageExtent.height = desc.size.height;
95 copy.imageExtent.depth = 1u;
96 copy.imageSubresource.aspectMask =
97 ToImageAspectFlags(GetTextureDescriptor().format);
98 copy.imageSubresource.mipLevel = 0u;
99 copy.imageSubresource.baseArrayLayer = slice;
100 copy.imageSubresource.layerCount = 1u;
101
102 vk_cmd_buffer.copyBufferToImage(
103 DeviceBufferVK::Cast(*staging_buffer).GetBuffer(), // src buffer
104 GetImage(), // dst image
105 barrier.new_layout, // dst image layout
106 1u, // region count
107 &copy // regions
108 );
109
110 return cmd_buffer->SubmitCommands();
111 }
112
113 bool TextureVK::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
114 size_t slice) {
115 // Vulkan has no threading restrictions. So we can pass this data along to the
116 // client rendering API immediately.
117 return OnSetContents(mapping->GetMapping(), mapping->GetSize(), slice);
118 }
119
120 bool TextureVK::IsValid() const {
121 return !!source_;
122 }
123
124 ISize TextureVK::GetSize() const {
125 return GetTextureDescriptor().size;
126 }
127
128 vk::Image TextureVK::GetImage() const {
129 return source_->GetImage();
130 }
131
132 vk::ImageView TextureVK::GetImageView() const {
133 return source_->GetImageView();
134 }
135
136 std::shared_ptr<const TextureSourceVK> TextureVK::GetTextureSource() const {
137 return source_;
138 }
139
140 bool TextureVK::SetLayout(const BarrierVK& barrier) const {
141 return source_ ? source_->SetLayout(barrier).ok() : false;
142 }
143
144 vk::ImageLayout TextureVK::SetLayoutWithoutEncoding(
145 vk::ImageLayout layout) const {
146 return source_ ? source_->SetLayoutWithoutEncoding(layout)
147 : vk::ImageLayout::eUndefined;
148 }
149
150 vk::ImageLayout TextureVK::GetLayout() const {
151 return source_ ? source_->GetLayout() : vk::ImageLayout::eUndefined;
152 }
153
154 } // namespace impeller
impeller::DeviceBufferVK::GetBuffer
vk::Buffer GetBuffer() const
Definition: device_buffer_vk.cc:71
impeller::CommandBufferVK::GetEncoder
const std::shared_ptr< CommandEncoderVK > & GetEncoder()
Definition: command_buffer_vk.cc:47
command_encoder_vk.h
impeller::Texture::GetTextureDescriptor
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
formats_vk.h
impeller::TextureVK::GetTextureSource
std::shared_ptr< const TextureSourceVK > GetTextureSource() const
Definition: texture_vk.cc:136
impeller::TextureVK::SetLayout
bool SetLayout(const BarrierVK &barrier) const
Definition: texture_vk.cc:140
command_buffer_vk.h
impeller::Texture
Definition: texture.h:17
impeller::TextureVK::GetImage
vk::Image GetImage() const
Definition: texture_vk.cc:128
impeller::BarrierVK
Defines an operations and memory access barrier on a resource.
Definition: barrier_vk.h:20
impeller::TextureVK::GetImageView
vk::ImageView GetImageView() const
Definition: texture_vk.cc:132
impeller::TextureVK::~TextureVK
~TextureVK() override
impeller::TextureVK::GetLayout
vk::ImageLayout GetLayout() const
Definition: texture_vk.cc:150
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:96
texture_vk.h
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:136
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:43
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
std
Definition: comparable.h:98
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:14
impeller::TextureVK::SetLayoutWithoutEncoding
vk::ImageLayout SetLayoutWithoutEncoding(vk::ImageLayout layout) const
Definition: texture_vk.cc:144
impeller::TextureVK::TextureVK
TextureVK(std::weak_ptr< Context > context, std::shared_ptr< TextureSourceVK > source)
Definition: texture_vk.cc:13
impeller
Definition: aiks_context.cc:10
impeller::ToImageAspectFlags
constexpr vk::ImageAspectFlags ToImageAspectFlags(PixelFormat format)
Definition: formats_vk.h:638