From 0f6b0b86d8483a3fe2bd1aa19064b67c8eae6dd4 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 6 Jul 2021 17:05:06 -0700 Subject: [PATCH] Wire up texture sampling in unit-tests. --- .../src/flutter/impeller/compositor/texture.h | 4 +-- .../flutter/impeller/compositor/texture.mm | 34 +++++++++++++------ .../primitives/primitives_unittests.mm | 12 +++++-- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/engine/src/flutter/impeller/compositor/texture.h b/engine/src/flutter/impeller/compositor/texture.h index a4fe87f818..e9b9241909 100644 --- a/engine/src/flutter/impeller/compositor/texture.h +++ b/engine/src/flutter/impeller/compositor/texture.h @@ -23,9 +23,7 @@ class Texture { void SetLabel(const std::string_view& label); - [[nodiscard]] bool SetContents(const uint8_t* contents, - size_t length, - size_t mip_level = 0u); + [[nodiscard]] bool SetContents(const uint8_t* contents, size_t length); bool IsValid() const; diff --git a/engine/src/flutter/impeller/compositor/texture.mm b/engine/src/flutter/impeller/compositor/texture.mm index b1dff469be..0723d8dad5 100644 --- a/engine/src/flutter/impeller/compositor/texture.mm +++ b/engine/src/flutter/impeller/compositor/texture.mm @@ -12,28 +12,40 @@ Texture::Texture(TextureDescriptor desc, id texture) return; } + if (desc_.size != GetSize()) { + FML_DLOG(ERROR) + << "The texture and its descriptor disagree about its size."; + return; + } + is_valid_ = true; } Texture::~Texture() = default; -bool Texture::SetContents(const uint8_t* contents, - size_t length, - size_t mip_level) { +bool Texture::SetContents(const uint8_t* contents, size_t length) { if (!IsValid() || !contents) { return false; } - FML_CHECK(false); - return false; + // Out of bounds access. + if (length != desc_.GetSizeOfBaseMipLevel()) { + return false; + } - // MTLRegionMake2D(NSUInteger x, NSUInteger y, NSUInteger width, - // NSUInteger height) + // TODO(csg): Perhaps the storage mode should be added to the texture + // descriptor so that invalid region replacements on potentially non-host + // visible textures are disallowed. The annoying bit about the API below is + // that there seems to be no error handling guidance. + const auto region = + MTLRegionMake2D(0u, 0u, desc_.size.width, desc_.size.height); + [texture_ replaceRegion:region // + mipmapLevel:0u // + withBytes:contents // + bytesPerRow:desc_.GetBytesPerRow() // + ]; - // [texture_ replaceRegion:(MTLRegion) - // mipmapLevel:(NSUInteger)withBytes:(nonnull const - // void*)bytesPerRow - // :(NSUInteger)]; + return true; } ISize Texture::GetSize() const { diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index 2715cd3938..653db6b1d1 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -58,7 +58,8 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { auto uploaded = texture->SetContents(result.GetAllocation()->GetMapping(), result.GetAllocation()->GetSize()); ASSERT_TRUE(uploaded); - + auto sampler = context->GetSamplerLibrary()->GetSampler({}); + ASSERT_TRUE(sampler); Renderer::RenderCallback callback = [&](const Surface& surface, RenderPass& pass) { BoxVertexShader::UniformBuffer uniforms; @@ -67,11 +68,18 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { Command cmd; cmd.label = "Box"; cmd.pipeline = box_pipeline; + cmd.vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = vertex_buffer.vertex_buffer; cmd.vertex_bindings .buffers[BoxVertexShader::kUniformUniformBuffer.binding] = pass.GetTransientsBuffer().EmplaceUniform(uniforms); + + cmd.fragment_bindings + .textures[BoxFragmentShader::kInputContentsTexture.location] = texture; + cmd.fragment_bindings + .samplers[BoxFragmentShader::kInputContentsTexture.location] = sampler; + cmd.index_buffer = vertex_buffer.index_buffer; cmd.index_count = vertex_buffer.index_count; cmd.primitive_type = PrimitiveType::kTriange; @@ -80,7 +88,7 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { } return true; }; - // OpenPlaygroundHere(callback); + OpenPlaygroundHere(callback); } } // namespace testing