[Impeller] make sure binding nullptr texture does not crash. (flutter/engine#56381)

Fixes https://github.com/flutter/flutter/issues/158074

binding a nullptr texture should fail but not crash.
This commit is contained in:
Jonah Williams
2024-11-06 14:14:15 -08:00
committed by GitHub
parent d0bb78ccaf
commit 2edc85cc0f
3 changed files with 19 additions and 2 deletions

View File

@@ -408,6 +408,9 @@ bool RenderPassMTL::BindResource(
const ShaderMetadata& metadata,
std::shared_ptr<const Texture> texture,
const std::unique_ptr<const Sampler>& sampler) {
if (!texture) {
return false;
}
return Bind(pass_bindings_, stage, slot.texture_index, sampler, *texture);
}

View File

@@ -602,7 +602,7 @@ bool RenderPassVK::BindResource(ShaderStage stage,
if (bound_buffer_offset_ >= kMaxBindings) {
return false;
}
if (!texture->IsValid() || !sampler) {
if (!texture || !texture->IsValid() || !sampler) {
return false;
}
const TextureVK& texture_vk = TextureVK::Cast(*texture);

View File

@@ -31,7 +31,6 @@
#include "impeller/fixtures/swizzle.frag.h"
#include "impeller/fixtures/texture.frag.h"
#include "impeller/fixtures/texture.vert.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/playground/playground.h"
#include "impeller/playground/playground_test.h"
#include "impeller/renderer/command_buffer.h"
@@ -1626,6 +1625,21 @@ TEST_P(RendererTest, CanSepiaToneThenSwizzleWithSubpasses) {
OpenPlaygroundHere(callback);
}
TEST_P(RendererTest, BindingNullTexturesDoesNotCrash) {
using FS = BoxFadeFragmentShader;
auto context = GetContext();
const std::unique_ptr<const Sampler>& sampler =
context->GetSamplerLibrary()->GetSampler({});
auto command_buffer = context->CreateCommandBuffer();
RenderTargetAllocator allocator(context->GetResourceAllocator());
RenderTarget target = allocator.CreateOffscreen(*context, {1, 1}, 1);
auto pass = command_buffer->CreateRenderPass(target);
EXPECT_FALSE(FS::BindContents2(*pass, nullptr, sampler));
}
} // namespace testing
} // namespace impeller