[Impeller] use const std::unique_ptr ref for Sampler type. (flutter/engine#49974)

The backend specific sampler libraries hold a strong reference to the native sampler objects and never clear this cache. As a result of this, we don't theoretically need rendering commands to increment a shared_ptr ref count - instead the sampler library can provide the Sampler object as a const ref and guarantee that it continues to be valid.

This allows us to reduce the amount of refcount ops for commands that use samplers.

Additionally, the sampler library uses nullptr as a sentinel for failing to construct a sampler object. Since sampler already has an isValid member that is checked - we can replace this with a specific invalid object subtype.
This commit is contained in:
Jonah Williams
2024-01-24 10:14:42 -08:00
committed by GitHub
parent f37eeba0cd
commit 31a5d5d5c9
42 changed files with 166 additions and 161 deletions

View File

@@ -1364,7 +1364,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
.argument_name = "texture",
});
proto.args.push_back(BindPrototypeArgument{
.type_name = "std::shared_ptr<const Sampler>",
.type_name = "const std::unique_ptr<const Sampler>&",
.argument_name = "sampler",
});
}

View File

@@ -34,7 +34,7 @@ struct ResourceBinder {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) = 0;
const std::unique_ptr<const Sampler>& sampler) = 0;
};
} // namespace impeller

View File

@@ -16,8 +16,6 @@ class Sampler {
public:
virtual ~Sampler();
virtual bool IsValid() const = 0;
const SamplerDescriptor& GetDescriptor() const;
protected:
@@ -32,7 +30,7 @@ class Sampler {
};
using SamplerMap = std::unordered_map<SamplerDescriptor,
std::shared_ptr<const Sampler>,
std::unique_ptr<const Sampler>,
ComparableHash<SamplerDescriptor>,
ComparableEqual<SamplerDescriptor>>;

View File

@@ -252,8 +252,9 @@ bool AtlasContents::Render(const ContentContext& renderer,
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
const std::unique_ptr<const Sampler>& dst_sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(pass, texture_, dst_sampler);
frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();

View File

@@ -179,8 +179,9 @@ static std::optional<Entity> AdvancedBlend(
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
const std::unique_ptr<const Sampler>& dst_sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
blend_info.dst_input_alpha =
@@ -201,8 +202,9 @@ static std::optional<Entity> AdvancedBlend(
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
src_sampler_descriptor);
const std::unique_ptr<const Sampler>& src_sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
src_sampler_descriptor);
blend_info.color_factor = 0;
blend_info.src_input_alpha = src_snapshot->opacity;
FS::BindTextureSamplerSrc(pass, src_snapshot->texture, src_sampler);
@@ -351,8 +353,9 @@ std::optional<Entity> BlendFilterContents::CreateForegroundAdvancedBlend(
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
const std::unique_ptr<const Sampler>& dst_sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
blend_info.dst_input_alpha =
@@ -473,8 +476,9 @@ std::optional<Entity> BlendFilterContents::CreateForegroundPorterDuffBlend(
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
const std::unique_ptr<const Sampler>& dst_sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
frame_info.texture_sampler_y_coord_scale =
dst_snapshot->texture->GetYCoordScale();
@@ -570,8 +574,9 @@ static std::optional<Entity> PipelineBlend(
return false;
}
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
input->sampler_descriptor);
const std::unique_ptr<const Sampler>& sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
input->sampler_descriptor);
FS::BindTextureSamplerSrc(pass, input->texture, sampler);
auto size = input->texture->GetSize();

View File

@@ -123,7 +123,8 @@ std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindTextureSampler(pass, input_snapshot->texture, sampler);
return pass.Draw().ok();

View File

@@ -96,7 +96,8 @@ std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
? input_snapshot->opacity
: 1.0f;
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(pass, input_snapshot->texture, sampler);
FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));

View File

@@ -77,7 +77,8 @@ std::optional<Entity> SrgbToLinearFilterContents::RenderFilter(
? input_snapshot->opacity
: 1.0f;
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(pass, input_snapshot->texture, sampler);
FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));

View File

@@ -111,7 +111,8 @@ std::optional<Entity> YUVToRGBFilterContents::RenderFilter(
break;
}
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindYTexture(pass, y_input_snapshot->texture, sampler);
FS::BindUvTexture(pass, uv_input_snapshot->texture, sampler);

View File

@@ -132,8 +132,9 @@ bool FramebufferBlendContents::Render(const ContentContext& renderer,
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
src_sampler_descriptor);
const std::unique_ptr<const Sampler>& src_sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
src_sampler_descriptor);
FS::BindTextureSamplerSrc(pass, src_snapshot->texture, src_sampler);
frame_info.mvp = pass.GetOrthographicTransform() * src_snapshot->transform;

View File

@@ -254,7 +254,7 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
FML_DCHECK(sampler_index < texture_inputs_.size());
auto& input = texture_inputs_[sampler_index];
auto sampler =
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler(input.sampler_descriptor);
SampledImageSlot image_slot;

View File

@@ -103,12 +103,13 @@ bool RecordingRenderPass::BindResource(
}
// |RenderPass|
bool RecordingRenderPass::BindResource(ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
bool RecordingRenderPass::BindResource(
ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
const std::unique_ptr<const Sampler>& sampler) {
pending_.BindResource(stage, type, slot, metadata, texture, sampler);
return delegate_->BindResource(stage, type, slot, metadata, texture, sampler);
}

View File

@@ -66,7 +66,7 @@ class RecordingRenderPass : public RenderPass {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
const std::unique_ptr<const Sampler>& sampler) override;
// |RenderPass|
void OnSetLabel(std::string label) override;

View File

@@ -36,10 +36,14 @@
#include "impeller/renderer/render_pass.h"
struct ImGui_ImplImpeller_Data {
explicit ImGui_ImplImpeller_Data(
const std::unique_ptr<const impeller::Sampler>& p_sampler)
: sampler(p_sampler) {}
std::shared_ptr<impeller::Context> context;
std::shared_ptr<impeller::Texture> font_texture;
std::shared_ptr<impeller::Pipeline<impeller::PipelineDescriptor>> pipeline;
std::shared_ptr<const impeller::Sampler> sampler;
const std::unique_ptr<const impeller::Sampler>& sampler;
};
static ImGui_ImplImpeller_Data* ImGui_ImplImpeller_GetBackendData() {
@@ -56,7 +60,8 @@ bool ImGui_ImplImpeller_Init(
"Already initialized a renderer backend!");
// Setup backend capabilities flags
auto* bd = new ImGui_ImplImpeller_Data();
auto* bd =
new ImGui_ImplImpeller_Data(context->GetSamplerLibrary()->GetSampler({}));
io.BackendRendererUserData = reinterpret_cast<void*>(bd);
io.BackendRendererName = "imgui_impl_impeller";
io.BackendFlags |=
@@ -105,8 +110,6 @@ bool ImGui_ImplImpeller_Init(
bd->pipeline =
context->GetPipelineLibrary()->GetPipeline(std::move(desc)).Get();
IM_ASSERT(bd->pipeline != nullptr && "Could not create ImGui pipeline.");
bd->sampler = context->GetSamplerLibrary()->GetSampler({});
IM_ASSERT(bd->pipeline != nullptr && "Could not create ImGui sampler.");
}

View File

@@ -4,8 +4,6 @@
#include "impeller/renderer/backend/gles/sampler_gles.h"
#include <iostream>
#include "impeller/base/validation.h"
#include "impeller/core/formats.h"
#include "impeller/renderer/backend/gles/formats_gles.h"
@@ -18,10 +16,6 @@ SamplerGLES::SamplerGLES(SamplerDescriptor desc) : Sampler(std::move(desc)) {}
SamplerGLES::~SamplerGLES() = default;
bool SamplerGLES::IsValid() const {
return true;
}
static GLint ToParam(MinMagFilter minmag_filter,
std::optional<MipFilter> mip_filter = std::nullopt) {
if (!mip_filter.has_value()) {
@@ -74,10 +68,6 @@ static GLint ToAddressMode(SamplerAddressMode mode,
bool SamplerGLES::ConfigureBoundTexture(const TextureGLES& texture,
const ProcTableGLES& gl) const {
if (!IsValid()) {
return false;
}
if (texture.NeedsMipmapGeneration()) {
VALIDATION_LOG
<< "Texture mip count is > 1, but the mipmap has not been generated. "

View File

@@ -28,9 +28,6 @@ class SamplerGLES final : public Sampler,
explicit SamplerGLES(SamplerDescriptor desc);
// |Sampler|
bool IsValid() const override;
SamplerGLES(const SamplerGLES&) = delete;
SamplerGLES& operator=(const SamplerGLES&) = delete;

View File

@@ -11,6 +11,8 @@
namespace impeller {
static const std::unique_ptr<const Sampler> kNullSampler = nullptr;
SamplerLibraryGLES::SamplerLibraryGLES(bool supports_decal_sampler_address_mode)
: supports_decal_sampler_address_mode_(
supports_decal_sampler_address_mode) {}
@@ -19,7 +21,7 @@ SamplerLibraryGLES::SamplerLibraryGLES(bool supports_decal_sampler_address_mode)
SamplerLibraryGLES::~SamplerLibraryGLES() = default;
// |SamplerLibrary|
std::shared_ptr<const Sampler> SamplerLibraryGLES::GetSampler(
const std::unique_ptr<const Sampler>& SamplerLibraryGLES::GetSampler(
SamplerDescriptor descriptor) {
if (!supports_decal_sampler_address_mode_ &&
(descriptor.width_address_mode == SamplerAddressMode::kDecal ||
@@ -27,15 +29,15 @@ std::shared_ptr<const Sampler> SamplerLibraryGLES::GetSampler(
descriptor.depth_address_mode == SamplerAddressMode::kDecal)) {
VALIDATION_LOG << "SamplerAddressMode::kDecal is not supported by the "
"current OpenGLES backend.";
return nullptr;
return kNullSampler;
}
auto found = samplers_.find(descriptor);
if (found != samplers_.end()) {
return found->second;
}
return samplers_[descriptor] =
std::shared_ptr<SamplerGLES>(new SamplerGLES(descriptor));
return (samplers_[descriptor] =
std::unique_ptr<SamplerGLES>(new SamplerGLES(descriptor)));
}
} // namespace impeller

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_SAMPLER_LIBRARY_GLES_H_
#define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_SAMPLER_LIBRARY_GLES_H_
#include "impeller/core/sampler.h"
#include "impeller/core/sampler_descriptor.h"
#include "impeller/renderer/sampler_library.h"
@@ -24,7 +25,7 @@ class SamplerLibraryGLES final : public SamplerLibrary {
SamplerLibraryGLES();
// |SamplerLibrary|
std::shared_ptr<const Sampler> GetSampler(
const std::unique_ptr<const Sampler>& GetSampler(
SamplerDescriptor descriptor) override;
bool supports_decal_sampler_address_mode_ = false;

View File

@@ -61,7 +61,7 @@ class ComputePassMTL final : public ComputePass {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
const std::unique_ptr<const Sampler>& sampler) override;
// |ComputePass|
bool EncodeCommands() const override;

View File

@@ -93,13 +93,14 @@ bool ComputePassMTL::BindResource(ShaderStage stage,
}
// |ComputePass|
bool ComputePassMTL::BindResource(ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
if (!sampler->IsValid() || !texture->IsValid()) {
bool ComputePassMTL::BindResource(
ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
const std::unique_ptr<const Sampler>& sampler) {
if (!sampler || !texture->IsValid()) {
return false;
}

View File

@@ -109,7 +109,7 @@ class RenderPassMTL final : public RenderPass {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
const std::unique_ptr<const Sampler>& sampler) override;
RenderPassMTL(const RenderPassMTL&) = delete;

View File

@@ -211,9 +211,9 @@ static bool Bind(PassBindingsCacheMTL& pass,
static bool Bind(PassBindingsCacheMTL& pass,
ShaderStage stage,
size_t bind_index,
const Sampler& sampler,
const std::unique_ptr<const Sampler>& sampler,
const Texture& texture) {
if (!sampler.IsValid() || !texture.IsValid()) {
if (!sampler || !texture.IsValid()) {
return false;
}
@@ -230,7 +230,7 @@ static bool Bind(PassBindingsCacheMTL& pass,
return pass.SetTexture(stage, bind_index,
TextureMTL::Cast(texture).GetMTLTexture()) &&
pass.SetSampler(stage, bind_index,
SamplerMTL::Cast(sampler).GetMTLSamplerState());
SamplerMTL::Cast(*sampler).GetMTLSamplerState());
}
// |RenderPass|
@@ -384,13 +384,14 @@ bool RenderPassMTL::BindResource(
}
// |RenderPass|
bool RenderPassMTL::BindResource(ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
return Bind(pass_bindings_, stage, slot.texture_index, *sampler, *texture);
bool RenderPassMTL::BindResource(
ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
const std::unique_ptr<const Sampler>& sampler) {
return Bind(pass_bindings_, stage, slot.texture_index, sampler, *texture);
}
} // namespace impeller

View File

@@ -33,7 +33,7 @@ class SamplerLibraryMTL final
explicit SamplerLibraryMTL(id<MTLDevice> device);
// |SamplerLibrary|
std::shared_ptr<const Sampler> GetSampler(
const std::unique_ptr<const Sampler>& GetSampler(
SamplerDescriptor descriptor) override;
SamplerLibraryMTL(const SamplerLibraryMTL&) = delete;

View File

@@ -13,14 +13,16 @@ SamplerLibraryMTL::SamplerLibraryMTL(id<MTLDevice> device) : device_(device) {}
SamplerLibraryMTL::~SamplerLibraryMTL() = default;
std::shared_ptr<const Sampler> SamplerLibraryMTL::GetSampler(
static const std::unique_ptr<const Sampler> kNullSampler = nullptr;
const std::unique_ptr<const Sampler>& SamplerLibraryMTL::GetSampler(
SamplerDescriptor descriptor) {
auto found = samplers_.find(descriptor);
if (found != samplers_.end()) {
return found->second;
}
if (!device_) {
return nullptr;
return kNullSampler;
}
auto desc = [[MTLSamplerDescriptor alloc] init];
desc.minFilter = ToMTLSamplerMinMagFilter(descriptor.min_filter);
@@ -38,15 +40,12 @@ std::shared_ptr<const Sampler> SamplerLibraryMTL::GetSampler(
auto mtl_sampler = [device_ newSamplerStateWithDescriptor:desc];
if (!mtl_sampler) {
return nullptr;
return kNullSampler;
}
auto sampler =
std::shared_ptr<SamplerMTL>(new SamplerMTL(descriptor, mtl_sampler));
if (!sampler->IsValid()) {
return nullptr;
}
samplers_[descriptor] = sampler;
return sampler;
std::unique_ptr<SamplerMTL>(new SamplerMTL(descriptor, mtl_sampler));
return (samplers_[descriptor] = std::move(sampler));
}
} // namespace impeller

View File

@@ -32,9 +32,6 @@ class SamplerMTL final : public Sampler,
SamplerMTL(SamplerDescriptor desc, id<MTLSamplerState> state);
// |Sampler|
bool IsValid() const override;
SamplerMTL(const SamplerMTL&) = delete;
SamplerMTL& operator=(const SamplerMTL&) = delete;

View File

@@ -7,14 +7,12 @@
namespace impeller {
SamplerMTL::SamplerMTL(SamplerDescriptor desc, id<MTLSamplerState> state)
: Sampler(std::move(desc)), state_(state) {}
: Sampler(std::move(desc)), state_(state) {
FML_DCHECK(state_);
}
SamplerMTL::~SamplerMTL() = default;
bool SamplerMTL::IsValid() const {
return state_;
}
id<MTLSamplerState> SamplerMTL::GetMTLSamplerState() const {
return state_;
}

View File

@@ -145,20 +145,23 @@ bool ComputePassVK::BindResource(ShaderStage stage,
}
// |ResourceBinder|
bool ComputePassVK::BindResource(ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
bool ComputePassVK::BindResource(
ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
const std::unique_ptr<const Sampler>& sampler) {
if (bound_image_offset_ >= kMaxBindings) {
return false;
}
if (!texture->IsValid() || !sampler) {
return false;
}
const TextureVK& texture_vk = TextureVK::Cast(*texture);
const SamplerVK& sampler_vk = SamplerVK::Cast(*sampler);
if (!command_buffer_->GetEncoder()->Track(texture) ||
!command_buffer_->GetEncoder()->Track(sampler_vk.GetSharedSampler())) {
if (!command_buffer_->GetEncoder()->Track(texture)) {
return false;
}

View File

@@ -73,7 +73,7 @@ class ComputePassVK final : public ComputePass {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
const std::unique_ptr<const Sampler>& sampler) override;
bool BindResource(size_t binding,
DescriptorType type,

View File

@@ -637,15 +637,17 @@ bool RenderPassVK::BindResource(ShaderStage stage,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
const std::unique_ptr<const Sampler>& sampler) {
if (bound_buffer_offset_ >= kMaxBindings) {
return false;
}
if (!texture->IsValid() || !sampler) {
return false;
}
const TextureVK& texture_vk = TextureVK::Cast(*texture);
const SamplerVK& sampler_vk = SamplerVK::Cast(*sampler);
if (!command_buffer_->GetEncoder()->Track(texture) ||
!command_buffer_->GetEncoder()->Track(sampler_vk.GetSharedSampler())) {
if (!command_buffer_->GetEncoder()->Track(texture)) {
return false;
}

View File

@@ -106,7 +106,7 @@ class RenderPassVK final : public RenderPass {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
const std::unique_ptr<const Sampler>& sampler) override;
bool BindResource(size_t binding,
DescriptorType type,

View File

@@ -16,7 +16,9 @@ SamplerLibraryVK::SamplerLibraryVK(
SamplerLibraryVK::~SamplerLibraryVK() = default;
std::shared_ptr<const Sampler> SamplerLibraryVK::GetSampler(
static const std::unique_ptr<const Sampler> kNullSampler = nullptr;
const std::unique_ptr<const Sampler>& SamplerLibraryVK::GetSampler(
SamplerDescriptor desc) {
auto found = samplers_.find(desc);
if (found != samplers_.end()) {
@@ -24,7 +26,7 @@ std::shared_ptr<const Sampler> SamplerLibraryVK::GetSampler(
}
auto device_holder = device_holder_.lock();
if (!device_holder || !device_holder->GetDevice()) {
return nullptr;
return kNullSampler;
}
const auto mip_map = ToVKSamplerMipmapMode(desc.mip_filter);
@@ -50,22 +52,17 @@ std::shared_ptr<const Sampler> SamplerLibraryVK::GetSampler(
device_holder->GetDevice().createSamplerUnique(sampler_create_info);
if (res.result != vk::Result::eSuccess) {
FML_LOG(ERROR) << "Failed to create sampler: " << vk::to_string(res.result);
return nullptr;
return kNullSampler;
}
auto sampler = std::make_shared<SamplerVK>(desc, std::move(res.value));
if (!sampler->IsValid()) {
return nullptr;
}
auto sampler = std::make_unique<SamplerVK>(desc, std::move(res.value));
if (!desc.label.empty()) {
ContextVK::SetDebugName(device_holder->GetDevice(), sampler->GetSampler(),
desc.label.c_str());
}
samplers_[desc] = sampler;
return sampler;
return (samplers_[desc] = std::move(sampler));
}
} // namespace impeller

View File

@@ -28,7 +28,7 @@ class SamplerLibraryVK final
explicit SamplerLibraryVK(const std::weak_ptr<DeviceHolder>& device_holder);
// |SamplerLibrary|
std::shared_ptr<const Sampler> GetSampler(
const std::unique_ptr<const Sampler>& GetSampler(
SamplerDescriptor descriptor) override;
SamplerLibraryVK(const SamplerLibraryVK&) = delete;

View File

@@ -8,9 +8,7 @@ namespace impeller {
SamplerVK::SamplerVK(SamplerDescriptor desc, vk::UniqueSampler sampler)
: Sampler(std::move(desc)),
sampler_(MakeSharedVK<vk::Sampler>(std::move(sampler))) {
is_valid_ = true;
}
sampler_(MakeSharedVK<vk::Sampler>(std::move(sampler))) {}
SamplerVK::~SamplerVK() = default;
@@ -18,13 +16,4 @@ vk::Sampler SamplerVK::GetSampler() const {
return *sampler_;
}
const std::shared_ptr<SharedObjectVKT<vk::Sampler>>&
SamplerVK::GetSharedSampler() const {
return sampler_;
}
bool SamplerVK::IsValid() const {
return is_valid_;
}
} // namespace impeller

View File

@@ -24,17 +24,12 @@ class SamplerVK final : public Sampler, public BackendCast<SamplerVK, Sampler> {
vk::Sampler GetSampler() const;
const std::shared_ptr<SharedObjectVKT<vk::Sampler>>& GetSharedSampler() const;
private:
friend SamplerLibraryVK;
std::shared_ptr<SharedObjectVKT<vk::Sampler>> sampler_;
bool is_valid_ = false;
// |Sampler|
bool IsValid() const override;
SamplerVK(const SamplerVK&) = delete;
SamplerVK& operator=(const SamplerVK&) = delete;

View File

@@ -72,8 +72,8 @@ bool Command::BindResource(ShaderStage stage,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
if (!sampler || !sampler->IsValid()) {
const std::unique_ptr<const Sampler>& sampler) {
if (!sampler) {
return false;
}
if (!texture || !texture->IsValid()) {
@@ -85,14 +85,14 @@ bool Command::BindResource(ShaderStage stage,
vertex_bindings.sampled_images.emplace_back(TextureAndSampler{
.slot = slot,
.texture = {&metadata, std::move(texture)},
.sampler = std::move(sampler),
.sampler = sampler,
});
return true;
case ShaderStage::kFragment:
fragment_bindings.sampled_images.emplace_back(TextureAndSampler{
.slot = slot,
.texture = {&metadata, std::move(texture)},
.sampler = std::move(sampler),
.sampler = sampler,
});
return true;
case ShaderStage::kCompute:

View File

@@ -61,7 +61,7 @@ using TextureResource = Resource<std::shared_ptr<const Texture>>;
struct TextureAndSampler {
SampledImageSlot slot;
TextureResource texture;
std::shared_ptr<const Sampler> sampler;
const std::unique_ptr<const Sampler>& sampler;
};
/// @brief combines the buffer resource and its uniform slot information.
@@ -179,7 +179,7 @@ struct Command : public ResourceBinder {
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
const std::unique_ptr<const Sampler>& sampler) override;
bool IsValid() const { return pipeline && pipeline->IsValid(); }

View File

@@ -153,9 +153,9 @@ bool RenderPass::BindResource(ShaderStage stage,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
const std::unique_ptr<const Sampler>& sampler) {
return pending_.BindResource(stage, type, slot, metadata, std::move(texture),
std::move(sampler));
sampler);
}
} // namespace impeller

View File

@@ -125,12 +125,13 @@ class RenderPass : public ResourceBinder {
BufferView view);
// |ResourceBinder|
virtual bool BindResource(ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) override;
virtual bool BindResource(
ShaderStage stage,
DescriptorType type,
const SampledImageSlot& slot,
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
const std::unique_ptr<const Sampler>& sampler) override;
//----------------------------------------------------------------------------
/// @brief Encode the recorded commands to the underlying command buffer.

View File

@@ -69,7 +69,8 @@ TEST_P(RendererTest, CanCreateBoxPrimitive) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(bridge && boston);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
auto host_buffer = HostBuffer::Create(context->GetResourceAllocator());
@@ -164,7 +165,8 @@ TEST_P(RendererTest, CanRenderPerspectiveCube) {
vertex_buffer.index_type = IndexType::k16bit;
}
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
Vector3 euler_angles;
@@ -232,7 +234,8 @@ TEST_P(RendererTest, CanRenderMultiplePrimitives) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(bridge && boston);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
auto host_buffer = HostBuffer::Create(context->GetResourceAllocator());
@@ -305,8 +308,10 @@ TEST_P(RendererTest, CanRenderToTexture) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(bridge && boston);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
std::shared_ptr<RenderPass> r2t_pass;
auto cmd_buffer = context->CreateCommandBuffer();
ASSERT_TRUE(cmd_buffer);
@@ -472,7 +477,8 @@ TEST_P(RendererTest, CanBlitTextureToTexture) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(bridge && boston);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
// Vertex buffer.
@@ -540,7 +546,7 @@ TEST_P(RendererTest, CanBlitTextureToTexture) {
frag_info.lod = 0;
FS::BindFragInfo(*pass, host_buffer->EmplaceUniform(frag_info));
auto sampler = context->GetSamplerLibrary()->GetSampler({});
auto& sampler = context->GetSamplerLibrary()->GetSampler({});
FS::BindTex(*pass, texture, sampler);
pass->Draw();
@@ -577,7 +583,8 @@ TEST_P(RendererTest, CanBlitTextureToBuffer) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(bridge && boston);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
TextureDescriptor texture_desc;
@@ -668,7 +675,8 @@ TEST_P(RendererTest, CanBlitTextureToBuffer) {
frag_info.lod = 0;
FS::BindFragInfo(*pass, host_buffer->EmplaceUniform(frag_info));
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
auto buffer_view = DeviceBuffer::AsBufferView(device_buffer);
auto texture =
context->GetResourceAllocator()->CreateTexture(texture_desc);
@@ -795,7 +803,8 @@ TEST_P(RendererTest, CanGenerateMipmaps) {
SamplerDescriptor sampler_desc;
sampler_desc.mip_filter = mip_filters[selected_mip_filter];
sampler_desc.min_filter = min_filters[selected_min_filter];
auto sampler = context->GetSamplerLibrary()->GetSampler(sampler_desc);
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler(sampler_desc);
FS::BindTex(*pass, boston, sampler);
pass->Draw();
@@ -830,14 +839,15 @@ TEST_P(RendererTest, TheImpeller) {
SamplerDescriptor noise_sampler_desc;
noise_sampler_desc.width_address_mode = SamplerAddressMode::kRepeat;
noise_sampler_desc.height_address_mode = SamplerAddressMode::kRepeat;
auto noise_sampler =
const std::unique_ptr<const Sampler>& noise_sampler =
context->GetSamplerLibrary()->GetSampler(noise_sampler_desc);
auto cube_map = CreateTextureCubeForFixture(
{"table_mountain_px.png", "table_mountain_nx.png",
"table_mountain_py.png", "table_mountain_ny.png",
"table_mountain_pz.png", "table_mountain_nz.png"});
auto cube_map_sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& cube_map_sampler =
context->GetSamplerLibrary()->GetSampler({});
auto host_buffer = HostBuffer::Create(context->GetResourceAllocator());
SinglePassCallback callback = [&](RenderPass& pass) {
@@ -1132,7 +1142,8 @@ TEST_P(RendererTest, StencilMask) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
auto boston = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(bridge && boston);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
static bool mirror = false;

View File

@@ -14,7 +14,16 @@ class SamplerLibrary {
public:
virtual ~SamplerLibrary();
virtual std::shared_ptr<const Sampler> GetSampler(
/// @brief Retrieve a backend specific sampler object for the given sampler
/// descriptor.
///
/// If the descriptor is invalid or there is a loss of rendering
/// context, this method may return a nullptr.
///
/// The sampler library implementations must cache this sampler object
/// and guarantee that the reference will continue to be valid
/// throughout the lifetime of the Impeller context.
virtual const std::unique_ptr<const Sampler>& GetSampler(
SamplerDescriptor descriptor) = 0;
protected:

View File

@@ -202,7 +202,7 @@ class MockCapabilities : public Capabilities {
class MockSamplerLibrary : public SamplerLibrary {
public:
MOCK_METHOD(std::shared_ptr<const Sampler>,
MOCK_METHOD(const std::unique_ptr<const Sampler>&,
GetSampler,
(SamplerDescriptor descriptor),
(override));
@@ -211,7 +211,6 @@ class MockSamplerLibrary : public SamplerLibrary {
class MockSampler : public Sampler {
public:
explicit MockSampler(const SamplerDescriptor& desc) : Sampler(desc) {}
MOCK_METHOD(bool, IsValid, (), (const, override));
};
} // namespace testing

View File

@@ -448,7 +448,7 @@ bool InternalFlutterGpu_RenderPass_BindTexture(
flutter::gpu::ToImpellerSamplerAddressMode(width_address_mode);
sampler_desc.height_address_mode =
flutter::gpu::ToImpellerSamplerAddressMode(height_address_mode);
auto sampler =
const std::unique_ptr<const impeller::Sampler>& sampler =
wrapper->GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
return command.BindResource(