Create textures from buffers.
This commit is contained in:
@@ -56,6 +56,8 @@ impeller_component("compositor") {
|
||||
"surface.mm",
|
||||
"texture.h",
|
||||
"texture.mm",
|
||||
"texture_descriptor.h",
|
||||
"texture_descriptor.mm",
|
||||
"vertex_buffer.h",
|
||||
"vertex_buffer.mm",
|
||||
"vertex_buffer_builder.h",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/mapping.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -57,6 +58,9 @@ class Allocator {
|
||||
std::shared_ptr<DeviceBuffer> CreateBufferWithCopy(const uint8_t* buffer,
|
||||
size_t length);
|
||||
|
||||
std::shared_ptr<DeviceBuffer> CreateBufferWithCopy(
|
||||
const fml::Mapping& mapping);
|
||||
|
||||
static bool RequiresExplicitHostSynchronization(StorageMode mode);
|
||||
|
||||
private:
|
||||
|
||||
@@ -77,6 +77,11 @@ std::shared_ptr<DeviceBuffer> Allocator::CreateBufferWithCopy(
|
||||
return new_buffer;
|
||||
}
|
||||
|
||||
std::shared_ptr<DeviceBuffer> Allocator::CreateBufferWithCopy(
|
||||
const fml::Mapping& mapping) {
|
||||
return CreateBufferWithCopy(mapping.GetMapping(), mapping.GetSize());
|
||||
}
|
||||
|
||||
std::shared_ptr<DeviceBuffer> Allocator::CreateBuffer(StorageMode mode,
|
||||
size_t length) {
|
||||
auto buffer =
|
||||
|
||||
@@ -27,6 +27,18 @@ class DeviceBuffer final : public Buffer,
|
||||
Range source_range,
|
||||
size_t offset = 0u);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Create a texture whose contents are the same as that of this
|
||||
/// buffer. Changes to either the contents of the texture or the
|
||||
/// buffer will be shared. When using buffer backed textures,
|
||||
/// implementations may have to disable certain optimizations.
|
||||
///
|
||||
/// @param[in] desc The description of the texture.
|
||||
/// @param[in] offset The offset of the texture data within buffer.
|
||||
///
|
||||
/// @return The texture whose contents are backed by (a part of) this
|
||||
/// buffer.
|
||||
///
|
||||
std::shared_ptr<Texture> MakeTexture(TextureDescriptor desc,
|
||||
size_t offset = 0u) const;
|
||||
|
||||
|
||||
@@ -65,7 +65,9 @@ std::shared_ptr<Texture> DeviceBuffer::MakeTexture(TextureDescriptor desc,
|
||||
return false;
|
||||
}
|
||||
|
||||
::memmove(dest + offset, source + source_range.offset, source_range.length);
|
||||
if (source) {
|
||||
::memmove(dest + offset, source + source_range.offset, source_range.length);
|
||||
}
|
||||
|
||||
if (Allocator::RequiresExplicitHostSynchronization(mode_)) {
|
||||
[buffer_ didModifyRange:NSMakeRange(offset, source_range.length)];
|
||||
|
||||
@@ -8,37 +8,11 @@
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/compositor/formats.h"
|
||||
#include "impeller/compositor/texture_descriptor.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
struct TextureDescriptor {
|
||||
PixelFormat format = PixelFormat::kUnknown;
|
||||
ISize size;
|
||||
size_t mip_count = 1u; // Size::MipCount is usually appropriate.
|
||||
|
||||
constexpr size_t GetSizeOfBaseMipLevel() const {
|
||||
if (!IsValid()) {
|
||||
return 0u;
|
||||
}
|
||||
return size.Area() * BytesPerPixelForPixelFormat(format);
|
||||
}
|
||||
|
||||
constexpr size_t GetBytesPerRow() const {
|
||||
if (!IsValid()) {
|
||||
return 0u;
|
||||
}
|
||||
return size.width * BytesPerPixelForPixelFormat(format);
|
||||
}
|
||||
|
||||
bool IsValid() const {
|
||||
return format != PixelFormat::kUnknown && //
|
||||
size.IsPositive() && //
|
||||
mip_count >= 1u //
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
Texture(id<MTLTexture> texture);
|
||||
|
||||
45
engine/src/flutter/impeller/compositor/texture_descriptor.h
Normal file
45
engine/src/flutter/impeller/compositor/texture_descriptor.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "impeller/compositor/formats.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
#include "impeller/image/image.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
struct TextureDescriptor {
|
||||
PixelFormat format = PixelFormat::kUnknown;
|
||||
ISize size;
|
||||
size_t mip_count = 1u; // Size::MipCount is usually appropriate.
|
||||
|
||||
constexpr size_t GetSizeOfBaseMipLevel() const {
|
||||
if (!IsValid()) {
|
||||
return 0u;
|
||||
}
|
||||
return size.Area() * BytesPerPixelForPixelFormat(format);
|
||||
}
|
||||
|
||||
constexpr size_t GetBytesPerRow() const {
|
||||
if (!IsValid()) {
|
||||
return 0u;
|
||||
}
|
||||
return size.width * BytesPerPixelForPixelFormat(format);
|
||||
}
|
||||
|
||||
bool IsValid() const {
|
||||
return format != PixelFormat::kUnknown && //
|
||||
size.IsPositive() && //
|
||||
mip_count >= 1u //
|
||||
;
|
||||
}
|
||||
|
||||
static std::optional<TextureDescriptor> MakeFromImageResult(
|
||||
const ImageResult& result);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
50
engine/src/flutter/impeller/compositor/texture_descriptor.mm
Normal file
50
engine/src/flutter/impeller/compositor/texture_descriptor.mm
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/compositor/texture_descriptor.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
std::optional<PixelFormat> FormatForImageResultComponents(
|
||||
ImageResult::Components comp) {
|
||||
switch (comp) {
|
||||
case ImageResult::Components::Invalid:
|
||||
return std::nullopt;
|
||||
case ImageResult::Components::Grey:
|
||||
return std::nullopt;
|
||||
case ImageResult::Components::GreyAlpha:
|
||||
return std::nullopt;
|
||||
case ImageResult::Components::RGB:
|
||||
return std::nullopt;
|
||||
case ImageResult::Components::RGBA:
|
||||
return PixelFormat::kPixelFormat_B8G8R8A8_UNormInt;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<TextureDescriptor> TextureDescriptor::MakeFromImageResult(
|
||||
const ImageResult& result) {
|
||||
if (!result.IsValid()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto pixel_format =
|
||||
FormatForImageResultComponents(result.GetComponents());
|
||||
if (!pixel_format.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
TextureDescriptor desc;
|
||||
desc.format = pixel_format.value();
|
||||
desc.size = result.GetSize();
|
||||
desc.mip_count = result.GetSize().MipCount();
|
||||
|
||||
if (!desc.IsValid()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -22,19 +22,19 @@ ImageResult Image::Decode() const {
|
||||
int comps = 0;
|
||||
|
||||
stbi_uc* decoded =
|
||||
stbi_load_from_memory(source_->GetMapping(), // Source Data
|
||||
source_->GetSize(), // Source Data Size
|
||||
&width, // Out: Width
|
||||
&height, // Out: Height
|
||||
&comps, // Out: Components
|
||||
STBI_default);
|
||||
::stbi_load_from_memory(source_->GetMapping(), // Source Data
|
||||
source_->GetSize(), // Source Data Size
|
||||
&width, // Out: Width
|
||||
&height, // Out: Height
|
||||
&comps, // Out: Components
|
||||
STBI_default);
|
||||
|
||||
if (decoded == nullptr) {
|
||||
FML_LOG(ERROR) << "Could not decode image from host memory.";
|
||||
return {};
|
||||
}
|
||||
|
||||
auto destinationAllocation = std::make_shared<const fml::NonOwnedMapping>(
|
||||
auto dest_allocation = std::make_shared<const fml::NonOwnedMapping>(
|
||||
decoded, // bytes
|
||||
width * height * comps * sizeof(stbi_uc), // byte size
|
||||
[](const uint8_t* data, size_t size) {
|
||||
@@ -71,9 +71,9 @@ ImageResult Image::Decode() const {
|
||||
}
|
||||
|
||||
return ImageResult{
|
||||
Size{static_cast<Scalar>(width), static_cast<Scalar>(height)}, // size
|
||||
components, // components
|
||||
std::move(destinationAllocation) // allocation
|
||||
ISize{width, height}, // size
|
||||
components, // components
|
||||
std::move(dest_allocation) // allocation
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class Image {
|
||||
|
||||
~Image();
|
||||
|
||||
ImageResult Decode() const;
|
||||
[[nodiscard]] ImageResult Decode() const;
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
|
||||
@@ -8,21 +8,23 @@ namespace impeller {
|
||||
|
||||
ImageResult::ImageResult() = default;
|
||||
|
||||
ImageResult::ImageResult(Size size,
|
||||
ImageResult::ImageResult(ISize size,
|
||||
Components components,
|
||||
std::shared_ptr<const fml::Mapping> allocation)
|
||||
: success_(true),
|
||||
size_(size),
|
||||
components_(components),
|
||||
allocation_(std::move(allocation)) {}
|
||||
: size_(size), components_(components), allocation_(std::move(allocation)) {
|
||||
if (!allocation_ || !size.IsPositive()) {
|
||||
return;
|
||||
}
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
ImageResult::~ImageResult() = default;
|
||||
|
||||
bool ImageResult::WasSuccessful() const {
|
||||
return success_;
|
||||
bool ImageResult::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
const Size& ImageResult::GetSize() const {
|
||||
const ISize& ImageResult::GetSize() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
@@ -30,7 +32,7 @@ ImageResult::Components ImageResult::GetComponents() const {
|
||||
return components_;
|
||||
}
|
||||
|
||||
const std::shared_ptr<const fml::Mapping>& ImageResult::Allocation() const {
|
||||
const std::shared_ptr<const fml::Mapping>& ImageResult::GetAllocation() const {
|
||||
return allocation_;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,25 +24,25 @@ class ImageResult {
|
||||
|
||||
ImageResult();
|
||||
|
||||
ImageResult(Size size,
|
||||
ImageResult(ISize size,
|
||||
Components components,
|
||||
std::shared_ptr<const fml::Mapping> allocation);
|
||||
|
||||
~ImageResult();
|
||||
|
||||
const Size& GetSize() const;
|
||||
const ISize& GetSize() const;
|
||||
|
||||
bool WasSuccessful() const;
|
||||
bool IsValid() const;
|
||||
|
||||
Components GetComponents() const;
|
||||
|
||||
const std::shared_ptr<const fml::Mapping>& Allocation() const;
|
||||
const std::shared_ptr<const fml::Mapping>& GetAllocation() const;
|
||||
|
||||
private:
|
||||
bool success_ = false;
|
||||
Size size_;
|
||||
ISize size_;
|
||||
Components components_ = Components::Invalid;
|
||||
std::shared_ptr<const fml::Mapping> allocation_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ImageResult);
|
||||
};
|
||||
|
||||
@@ -76,8 +76,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
|
||||
::glfwWindowHint(GLFW_RESIZABLE, false);
|
||||
|
||||
auto window_title = GetWindowTitle(flutter::testing::GetCurrentTestName());
|
||||
auto window =
|
||||
::glfwCreateWindow(1920, 1080, window_title.c_str(), NULL, NULL);
|
||||
auto window = ::glfwCreateWindow(1024, 768, window_title.c_str(), NULL, NULL);
|
||||
if (!window) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ impeller_component("primitives_unittests") {
|
||||
sources = [ "primitives_unittests.mm" ]
|
||||
deps = [
|
||||
":primitives",
|
||||
"../image",
|
||||
"../playground",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -3,9 +3,12 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
in vec4 color;
|
||||
in vec2 interporlated_texture_coordinates;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
uniform sampler2D contents_texture;
|
||||
|
||||
void main() {
|
||||
frag_color = color;
|
||||
frag_color = texture(contents_texture, interporlated_texture_coordinates);
|
||||
}
|
||||
|
||||
@@ -8,10 +8,13 @@ uniform UniformBuffer {
|
||||
|
||||
in vec3 vertex_position;
|
||||
in vec4 vertex_color;
|
||||
in vec2 texture_coordinates;
|
||||
|
||||
out vec4 color;
|
||||
out vec2 interporlated_texture_coordinates;
|
||||
|
||||
void main() {
|
||||
gl_Position = uniforms.mvp * vec4(vertex_position, 1.0);
|
||||
color = vertex_color;
|
||||
interporlated_texture_coordinates = texture_coordinates;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "impeller/compositor/sampler_descriptor.h"
|
||||
#include "impeller/compositor/surface.h"
|
||||
#include "impeller/compositor/vertex_buffer_builder.h"
|
||||
#include "impeller/image/image.h"
|
||||
#include "impeller/playground/playground.h"
|
||||
#include "impeller/primitives/box.frag.h"
|
||||
#include "impeller/primitives/box.vert.h"
|
||||
@@ -29,20 +30,37 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
|
||||
context->GetPipelineLibrary()->GetRenderPipeline(std::move(desc)).get();
|
||||
ASSERT_TRUE(box_pipeline);
|
||||
|
||||
// Vertex buffer.
|
||||
VertexBufferBuilder<BoxVertexShader::PerVertexData> vertex_builder;
|
||||
vertex_builder.SetLabel("Box");
|
||||
vertex_builder.AddVertices({
|
||||
{{100, 100, 0.0}, {Color::Red()}}, // 1
|
||||
{{800, 100, 0.0}, {Color::Green()}}, // 2
|
||||
{{800, 800, 0.0}, {Color::Blue()}}, // 3
|
||||
{{100, 100, 0.0}, {Color::Red()}, {0.0, 0.0}}, // 1
|
||||
{{800, 100, 0.0}, {Color::Green()}, {1.0, 0.0}}, // 2
|
||||
{{800, 800, 0.0}, {Color::Blue()}, {1.0, 1.0}}, // 3
|
||||
|
||||
{{100, 100, 0.0}, {Color::Cyan()}}, // 1
|
||||
{{800, 800, 0.0}, {Color::White()}}, // 3
|
||||
{{100, 800, 0.0}, {Color::Purple()}}, // 4
|
||||
{{100, 100, 0.0}, {Color::Cyan()}, {0.0, 0.0}}, // 1
|
||||
{{800, 800, 0.0}, {Color::White()}, {1.0, 1.0}}, // 3
|
||||
{{100, 800, 0.0}, {Color::Purple()}, {0.0, 1.0}}, // 4
|
||||
});
|
||||
auto vertex_buffer =
|
||||
vertex_builder.CreateVertexBuffer(*context->GetPermanentsAllocator());
|
||||
ASSERT_TRUE(vertex_buffer);
|
||||
|
||||
// Contents texture.
|
||||
Image image(flutter::testing::OpenFixtureAsMapping("image.png"));
|
||||
auto result = image.Decode();
|
||||
ASSERT_TRUE(result.IsValid());
|
||||
auto device_image_allocation =
|
||||
context->GetPermanentsAllocator()->CreateBufferWithCopy(
|
||||
*result.GetAllocation());
|
||||
device_image_allocation->SetLabel("Bay Bridge");
|
||||
ASSERT_TRUE(device_image_allocation);
|
||||
auto texture_descriptor = TextureDescriptor::MakeFromImageResult(result);
|
||||
ASSERT_TRUE(texture_descriptor.has_value());
|
||||
auto texture =
|
||||
device_image_allocation->MakeTexture(texture_descriptor.value());
|
||||
ASSERT_TRUE(texture);
|
||||
|
||||
Renderer::RenderCallback callback = [&](const Surface& surface,
|
||||
RenderPass& pass) {
|
||||
BoxVertexShader::UniformBuffer uniforms;
|
||||
|
||||
Reference in New Issue
Block a user