Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/queue_vk.cc + ../../..
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/queue_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/render_pass_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/resource_manager_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/resource_manager_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/sampler_library_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/sampler_library_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/sampler_vk.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4223,6 +4225,8 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/queue_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/queue_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/render_pass_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/resource_manager_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/resource_manager_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/sampler_library_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/sampler_library_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/sampler_vk.cc
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/backend/vulkan/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ impeller_component("vulkan") {
"queue_vk.h",
"render_pass_vk.cc",
"render_pass_vk.h",
"resource_manager_vk.cc",
"resource_manager_vk.h",
"sampler_library_vk.cc",
"sampler_library_vk.h",
"sampler_vk.cc",
Expand Down
93 changes: 64 additions & 29 deletions impeller/renderer/backend/vulkan/allocator_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ static VmaAllocationCreateFlags ToVmaAllocationCreateFlags(StorageMode mode,

class AllocatedTextureSourceVK final : public TextureSourceVK {
public:
AllocatedTextureSourceVK(const TextureDescriptor& desc,
AllocatedTextureSourceVK(std::weak_ptr<ResourceManagerVK> resource_manager,
const TextureDescriptor& desc,
VmaAllocator allocator,
vk::Device device,
bool supports_memoryless_textures)
: TextureSourceVK(desc) {
: TextureSourceVK(desc), resource_(std::move(resource_manager)) {
TRACE_EVENT0("impeller", "CreateDeviceTexture");
vk::ImageCreateInfo image_info;
image_info.flags = ToVKImageCreateFlags(desc.type);
Expand Down Expand Up @@ -305,12 +306,10 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
}
}

image_ = vk::Image{vk_image};
allocator_ = allocator;
allocation_ = allocation;
auto image = vk::Image{vk_image};

vk::ImageViewCreateInfo view_info = {};
view_info.image = image_;
view_info.image = image;
view_info.viewType = ToVKImageViewType(desc.type);
view_info.format = image_info.format;
view_info.subresourceRange.aspectMask = ToVKImageAspectFlags(desc.format);
Expand All @@ -332,34 +331,65 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
<< vk::to_string(result);
return;
}
image_view_ = std::move(image_view);

resource_.Reset(
ImageResource(image, allocator, allocation, std::move(image_view)));
is_valid_ = true;
}

~AllocatedTextureSourceVK() {
TRACE_EVENT0("impeller", "DestroyDeviceTexture");
image_view_.reset();
if (image_) {
::vmaDestroyImage(
allocator_, //
static_cast<typename decltype(image_)::NativeType>(image_), //
allocation_ //
);
}
}
~AllocatedTextureSourceVK() = default;

bool IsValid() const { return is_valid_; }

vk::Image GetImage() const override { return image_; }
vk::Image GetImage() const override { return resource_->image; }

vk::ImageView GetImageView() const override { return image_view_.get(); }
vk::ImageView GetImageView() const override {
return resource_->image_view.get();
}

private:
vk::Image image_ = {};
VmaAllocator allocator_ = {};
VmaAllocation allocation_ = {};
vk::UniqueImageView image_view_;
struct ImageResource {
vk::Image image = {};
VmaAllocator allocator = {};
VmaAllocation allocation = {};
vk::UniqueImageView image_view;

ImageResource() = default;

ImageResource(vk::Image p_image,
VmaAllocator p_allocator,
VmaAllocation p_allocation,
vk::UniqueImageView p_image_view)
: image(p_image),
allocator(p_allocator),
allocation(p_allocation),
image_view(std::move(p_image_view)) {}

ImageResource(ImageResource&& o) {
std::swap(image, o.image);
std::swap(allocator, o.allocator);
std::swap(allocation, o.allocation);
std::swap(image_view, o.image_view);
}

~ImageResource() {
if (!image) {
return;
}
TRACE_EVENT0("impeller", "DestroyDeviceTexture");
image_view.reset();
if (image) {
::vmaDestroyImage(
allocator, //
static_cast<typename decltype(image)::NativeType>(image), //
allocation //
);
}
}

FML_DISALLOW_COPY_AND_ASSIGN(ImageResource);
};

UniqueResourceVKT<ImageResource> resource_;
bool is_valid_ = false;

FML_DISALLOW_COPY_AND_ASSIGN(AllocatedTextureSourceVK);
Expand All @@ -376,11 +406,16 @@ std::shared_ptr<Texture> AllocatorVK::OnCreateTexture(
if (!device_holder) {
return nullptr;
}
auto context = context_.lock();
if (!context) {
return nullptr;
}
auto source = std::make_shared<AllocatedTextureSourceVK>(
desc, //
allocator_, //
device_holder->GetDevice(), //
supports_memoryless_textures_ //
ContextVK::Cast(*context).GetResourceManager(), //
desc, //
allocator_, //
device_holder->GetDevice(), //
supports_memoryless_textures_ //
);
if (!source->IsValid()) {
return nullptr;
Expand Down
15 changes: 15 additions & 0 deletions impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "impeller/renderer/backend/vulkan/debug_report_vk.h"
#include "impeller/renderer/backend/vulkan/fence_waiter_vk.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
#include "impeller/renderer/backend/vulkan/surface_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/capabilities.h"
Expand Down Expand Up @@ -379,6 +380,15 @@ void ContextVK::Setup(Settings settings) {
return;
}

//----------------------------------------------------------------------------
/// Create the resource manager.
///
auto resource_manager = ResourceManagerVK::Create();
if (!resource_manager) {
VALIDATION_LOG << "Could not create resource manager.";
return;
}

//----------------------------------------------------------------------------
/// Fetch the queues.
///
Expand Down Expand Up @@ -408,6 +418,7 @@ void ContextVK::Setup(Settings settings) {
queues_ = std::move(queues);
device_capabilities_ = std::move(caps);
fence_waiter_ = std::move(fence_waiter);
resource_manager_ = std::move(resource_manager);
device_name_ = std::string(physical_device_properties.deviceName);
is_valid_ = true;

Expand Down Expand Up @@ -533,6 +544,10 @@ std::shared_ptr<FenceWaiterVK> ContextVK::GetFenceWaiter() const {
return fence_waiter_;
}

std::shared_ptr<ResourceManagerVK> ContextVK::GetResourceManager() const {
return resource_manager_;
}

std::unique_ptr<CommandEncoderVK> ContextVK::CreateGraphicsCommandEncoder()
const {
auto tls_pool = CommandPoolVK::GetThreadLocal(this);
Expand Down
4 changes: 4 additions & 0 deletions impeller/renderer/backend/vulkan/context_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bool HasValidationLayers();
class CommandEncoderVK;
class DebugReportVK;
class FenceWaiterVK;
class ResourceManagerVK;

class ContextVK final : public Context,
public BackendCast<ContextVK, Context>,
Expand Down Expand Up @@ -136,6 +137,8 @@ class ContextVK final : public Context,

std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;

std::shared_ptr<ResourceManagerVK> GetResourceManager() const;

private:
struct DeviceHolderImpl : public DeviceHolder {
// |DeviceHolder|
Expand All @@ -160,6 +163,7 @@ class ContextVK final : public Context,
std::shared_ptr<SwapchainVK> swapchain_;
std::shared_ptr<const Capabilities> device_capabilities_;
std::shared_ptr<FenceWaiterVK> fence_waiter_;
std::shared_ptr<ResourceManagerVK> resource_manager_;
std::string device_name_;
std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
const uint64_t hash_;
Expand Down
35 changes: 17 additions & 18 deletions impeller/renderer/backend/vulkan/device_buffer_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@ DeviceBufferVK::DeviceBufferVK(DeviceBufferDescriptor desc,
vk::Buffer buffer)
: DeviceBuffer(desc),
context_(std::move(context)),
allocator_(allocator),
allocation_(allocation),
info_(info),
buffer_(buffer) {}

DeviceBufferVK::~DeviceBufferVK() {
TRACE_EVENT0("impeller", "DestroyDeviceBuffer");
if (buffer_) {
::vmaDestroyBuffer(allocator_,
static_cast<decltype(buffer_)::NativeType>(buffer_),
allocation_);
}
}
resource_(ContextVK::Cast(*context_.lock().get()).GetResourceManager(),
BufferResource{
allocator, //
allocation, //
info, //
buffer //
}) {}

DeviceBufferVK::~DeviceBufferVK() {}

uint8_t* DeviceBufferVK::OnGetContents() const {
return static_cast<uint8_t*>(info_.pMappedData);
return static_cast<uint8_t*>(resource_->info.pMappedData);
}

bool DeviceBufferVK::OnCopyHostBuffer(const uint8_t* source,
Expand All @@ -55,14 +51,17 @@ bool DeviceBufferVK::OnCopyHostBuffer(const uint8_t* source,

bool DeviceBufferVK::SetLabel(const std::string& label) {
auto context = context_.lock();
if (!context || !buffer_) {
if (!context || !resource_->buffer) {
// The context could have died at this point.
return false;
}

::vmaSetAllocationName(allocator_, allocation_, label.c_str());
::vmaSetAllocationName(resource_->allocator, //
resource_->allocation, //
label.c_str() //
);

return ContextVK::Cast(*context).SetDebugName(buffer_, label);
return ContextVK::Cast(*context).SetDebugName(resource_->buffer, label);
}

bool DeviceBufferVK::SetLabel(const std::string& label, Range range) {
Expand All @@ -71,7 +70,7 @@ bool DeviceBufferVK::SetLabel(const std::string& label, Range range) {
}

vk::Buffer DeviceBufferVK::GetBuffer() const {
return buffer_;
return resource_->buffer;
}

} // namespace impeller
44 changes: 40 additions & 4 deletions impeller/renderer/backend/vulkan/device_buffer_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <memory>

#include "flutter/fml/macros.h"
#include "flutter/fml/trace_event.h"
#include "impeller/base/backend_cast.h"
#include "impeller/core/device_buffer.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"

namespace impeller {

Expand All @@ -31,11 +33,45 @@ class DeviceBufferVK final : public DeviceBuffer,
private:
friend class AllocatorVK;

struct BufferResource {
VmaAllocator allocator = {};
VmaAllocation allocation = {};
VmaAllocationInfo info = {};
vk::Buffer buffer = {};

BufferResource() = default;

BufferResource(VmaAllocator p_allocator,
VmaAllocation p_allocation,
VmaAllocationInfo p_info,
vk::Buffer p_buffer)
: allocator(p_allocator),
allocation(p_allocation),
info(p_info),
buffer(p_buffer) {}

BufferResource(BufferResource&& o) {
std::swap(o.allocator, allocator);
std::swap(o.allocation, allocation);
std::swap(o.info, info);
std::swap(o.buffer, buffer);
}

~BufferResource() {
if (!buffer) {
return;
}
TRACE_EVENT0("impeller", "DestroyDeviceBuffer");
::vmaDestroyBuffer(allocator,
static_cast<decltype(buffer)::NativeType>(buffer),
allocation);
}

FML_DISALLOW_COPY_AND_ASSIGN(BufferResource);
};

std::weak_ptr<Context> context_;
VmaAllocator allocator_ = {};
VmaAllocation allocation_ = {};
VmaAllocationInfo info_ = {};
vk::Buffer buffer_ = {};
UniqueResourceVKT<BufferResource> resource_;

// |DeviceBuffer|
uint8_t* OnGetContents() const override;
Expand Down
Loading