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
12 changes: 10 additions & 2 deletions impeller/entity/contents/content_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ void ContentContextOptions::ApplyToPipelineDescriptor(
desc.SetSampleCount(sample_count);

ColorAttachmentDescriptor color0 = *desc.GetColorAttachmentDescriptor(0u);
color0.format = color_attachment_pixel_format;
if (!color_attachment_pixel_format.has_value()) {
VALIDATION_LOG << "Color attachment pixel format must be set.";
color0.format = PixelFormat::kB8G8R8A8UNormInt;
} else {
color0.format = *color_attachment_pixel_format;
}
color0.format = *color_attachment_pixel_format;
color0.alpha_blend_op = BlendOperation::kAdd;
color0.color_blend_op = BlendOperation::kAdd;

Expand Down Expand Up @@ -145,7 +151,9 @@ static std::unique_ptr<PipelineT> CreateDefaultPipeline(
return nullptr;
}
// Apply default ContentContextOptions to the descriptor.
ContentContextOptions{}.ApplyToPipelineDescriptor(*desc);
const auto default_color_fmt = context.GetColorAttachmentPixelFormat();
ContentContextOptions{.color_attachment_pixel_format = default_color_fmt}
.ApplyToPipelineDescriptor(*desc);
return std::make_unique<PipelineT>(context, desc);
}

Expand Down
3 changes: 2 additions & 1 deletion impeller/entity/contents/content_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <memory>
#include <optional>
#include <unordered_map>

#include "flutter/fml/hash_combine.h"
Expand Down Expand Up @@ -265,7 +266,7 @@ struct ContentContextOptions {
CompareFunction stencil_compare = CompareFunction::kEqual;
StencilOperation stencil_operation = StencilOperation::kKeep;
PrimitiveType primitive_type = PrimitiveType::kTriangle;
PixelFormat color_attachment_pixel_format = PixelFormat::kDefaultColor;
std::optional<PixelFormat> color_attachment_pixel_format;
bool has_stencil_attachment = true;

struct Hash {
Expand Down
9 changes: 7 additions & 2 deletions impeller/entity/contents/runtime_effect_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
/// Get or create runtime stage pipeline.
///

const auto& device_capabilities = context->GetDeviceCapabilities();
const auto color_attachment_format = context->GetColorAttachmentPixelFormat();
const auto stencil_attachment_format =
device_capabilities.GetDefaultStencilFormat();

using VS = RuntimeEffectVertexShader;
PipelineDescriptor desc;
desc.SetLabel("Runtime Stage");
Expand All @@ -115,9 +120,9 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
}
desc.SetVertexDescriptor(std::move(vertex_descriptor));
desc.SetColorAttachmentDescriptor(
0u, {.format = PixelFormat::kDefaultColor, .blending_enabled = true});
0u, {.format = color_attachment_format, .blending_enabled = true});
desc.SetStencilAttachmentDescriptors({});
desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil);
desc.SetStencilPixelFormat(stencil_attachment_format);

auto options = OptionsFromPassAndEntity(pass, entity);
if (geometry_result.prevent_overdraw) {
Expand Down
3 changes: 2 additions & 1 deletion impeller/playground/backend/metal/playground_impl_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
data_->metal_layer = [CAMetalLayer layer];
data_->metal_layer.device = ContextMTL::Cast(*context).GetMTLDevice();
// This pixel format is one of the documented supported formats.
data_->metal_layer.pixelFormat = ToMTLPixelFormat(PixelFormat::kDefaultColor);
const auto color_fmt = context->GetColorAttachmentPixelFormat();
data_->metal_layer.pixelFormat = ToMTLPixelFormat(color_fmt);
data_->metal_layer.framebufferOnly = NO;
cocoa_window.contentView.layer = data_->metal_layer;
cocoa_window.contentView.wantsLayer = YES;
Expand Down
13 changes: 8 additions & 5 deletions impeller/renderer/backend/gles/context_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ ContextGLES::ContextGLES(std::unique_ptr<ProcTableGLES> gl,

// Create the device capabilities.
{
device_capabilities_ = DeviceCapabilitiesBuilder()
.SetHasThreadingRestrictions(true)
.SetSupportsOffscreenMSAA(false)
.SetSupportsSSBO(false)
.Build();
device_capabilities_ =
DeviceCapabilitiesBuilder()
.SetHasThreadingRestrictions(true)
.SetSupportsOffscreenMSAA(false)
.SetSupportsSSBO(false)
.SetDefaultColorFormat(PixelFormat::kB8G8R8A8UNormInt)
.SetDefaultStencilFormat(PixelFormat::kS8UInt)
.Build();
}

is_valid_ = true;
Expand Down
13 changes: 8 additions & 5 deletions impeller/renderer/backend/metal/context_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@
#endif

{
device_capabilities_ = DeviceCapabilitiesBuilder()
.SetHasThreadingRestrictions(false)
.SetSupportsOffscreenMSAA(true)
.SetSupportsSSBO(true)
.Build();
device_capabilities_ =
DeviceCapabilitiesBuilder()
.SetHasThreadingRestrictions(false)
.SetSupportsOffscreenMSAA(true)
.SetSupportsSSBO(true)
.SetDefaultColorFormat(PixelFormat::kB8G8R8A8UNormInt)
.SetDefaultStencilFormat(PixelFormat::kS8UInt)
.Build();
}

is_valid_ = true;
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/metal/surface_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
stencil0_tex.storage_mode = StorageMode::kDeviceTransient;
stencil0_tex.type = TextureType::kTexture2DMultisample;
stencil0_tex.sample_count = SampleCount::kCount4;
stencil0_tex.format = PixelFormat::kDefaultStencil;
stencil0_tex.format =
context->GetDeviceCapabilities().GetDefaultStencilFormat();
stencil0_tex.size = color0_tex_desc.size;
stencil0_tex.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
Expand Down
13 changes: 8 additions & 5 deletions impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,14 @@ ContextVK::ContextVK(
transfer_queue_ =
device_->getQueue(transfer_queue->family, transfer_queue->index);

device_capabilities_ = DeviceCapabilitiesBuilder()
.SetHasThreadingRestrictions(false)
.SetSupportsOffscreenMSAA(true)
.SetSupportsSSBO(false)
.Build();
device_capabilities_ =
DeviceCapabilitiesBuilder()
.SetHasThreadingRestrictions(false)
.SetSupportsOffscreenMSAA(true)
.SetSupportsSSBO(false)
.SetDefaultColorFormat(PixelFormat::kB8G8R8A8UNormInt)
.SetDefaultStencilFormat(PixelFormat::kS8UInt)
.Build();

is_valid_ = true;
}
Expand Down
2 changes: 1 addition & 1 deletion impeller/renderer/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std::shared_ptr<GPUTracer> Context::GetGPUTracer() const {
}

PixelFormat Context::GetColorAttachmentPixelFormat() const {
return PixelFormat::kDefaultColor;
return GetDeviceCapabilities().GetDefaultColorFormat();
}

} // namespace impeller
36 changes: 33 additions & 3 deletions impeller/renderer/device_capabilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ namespace impeller {

IDeviceCapabilities::IDeviceCapabilities(bool threading_restrictions,
bool offscreen_msaa,
bool supports_ssbo)
bool supports_ssbo,
PixelFormat default_color_format,
PixelFormat default_stencil_format)
: threading_restrictions_(threading_restrictions),
offscreen_msaa_(offscreen_msaa),
supports_ssbo_(supports_ssbo) {}
supports_ssbo_(supports_ssbo),
default_color_format_(default_color_format),
default_stencil_format_(default_stencil_format) {}

IDeviceCapabilities::~IDeviceCapabilities() = default;

Expand All @@ -27,6 +31,14 @@ bool IDeviceCapabilities::SupportsSSBO() const {
return supports_ssbo_;
}

PixelFormat IDeviceCapabilities::GetDefaultColorFormat() const {
return default_color_format_;
}

PixelFormat IDeviceCapabilities::GetDefaultStencilFormat() const {
return default_stencil_format_;
}

DeviceCapabilitiesBuilder::DeviceCapabilitiesBuilder() = default;

DeviceCapabilitiesBuilder::~DeviceCapabilitiesBuilder() = default;
Expand All @@ -49,9 +61,27 @@ DeviceCapabilitiesBuilder& DeviceCapabilitiesBuilder::SetSupportsSSBO(
return *this;
}

DeviceCapabilitiesBuilder& DeviceCapabilitiesBuilder::SetDefaultColorFormat(
PixelFormat value) {
default_color_format_ = value;
return *this;
}

DeviceCapabilitiesBuilder& DeviceCapabilitiesBuilder::SetDefaultStencilFormat(
PixelFormat value) {
default_stencil_format_ = value;
return *this;
}

std::unique_ptr<IDeviceCapabilities> DeviceCapabilitiesBuilder::Build() {
FML_CHECK(default_color_format_.has_value())
<< "Default color format not set";
FML_CHECK(default_stencil_format_.has_value())
<< "Default stencil format not set";

IDeviceCapabilities* capabilities = new IDeviceCapabilities(
threading_restrictions_, offscreen_msaa_, supports_ssbo_);
threading_restrictions_, offscreen_msaa_, supports_ssbo_,
*default_color_format_, *default_stencil_format_);
return std::unique_ptr<IDeviceCapabilities>(capabilities);
}

Expand Down
17 changes: 16 additions & 1 deletion impeller/renderer/device_capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>

#include "flutter/fml/macros.h"
#include "impeller/renderer/formats.h"

namespace impeller {

Expand All @@ -20,16 +21,24 @@ class IDeviceCapabilities {

bool SupportsSSBO() const;

PixelFormat GetDefaultColorFormat() const;

PixelFormat GetDefaultStencilFormat() const;

private:
IDeviceCapabilities(bool threading_restrictions,
bool offscreen_msaa,
bool supports_ssbo);
bool supports_ssbo,
PixelFormat default_color_format,
PixelFormat default_stencil_format);

friend class DeviceCapabilitiesBuilder;

bool threading_restrictions_ = false;
bool offscreen_msaa_ = false;
bool supports_ssbo_ = false;
PixelFormat default_color_format_;
PixelFormat default_stencil_format_;

FML_DISALLOW_COPY_AND_ASSIGN(IDeviceCapabilities);
};
Expand All @@ -46,12 +55,18 @@ class DeviceCapabilitiesBuilder {

DeviceCapabilitiesBuilder& SetSupportsSSBO(bool value);

DeviceCapabilitiesBuilder& SetDefaultColorFormat(PixelFormat value);

DeviceCapabilitiesBuilder& SetDefaultStencilFormat(PixelFormat value);

std::unique_ptr<IDeviceCapabilities> Build();

private:
bool threading_restrictions_ = false;
bool offscreen_msaa_ = false;
bool supports_ssbo_ = false;
std::optional<PixelFormat> default_color_format_ = std::nullopt;
std::optional<PixelFormat> default_stencil_format_ = std::nullopt;

FML_DISALLOW_COPY_AND_ASSIGN(DeviceCapabilitiesBuilder);
};
Expand Down
8 changes: 0 additions & 8 deletions impeller/renderer/formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ enum class PixelFormat {
// Depth and stencil formats.
kS8UInt,
kD32FloatS8UInt,

// Defaults. If you don't know which ones to use, these are usually a safe
// bet.
//
// On Metal, this is a support format for layer drawable and can be used to
// specify the format of the resolve texture if needed.
kDefaultColor = kB8G8R8A8UNormInt,
kDefaultStencil = kS8UInt,
};

enum class BlendFactor {
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/pipeline_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ struct PipelineBuilder {
StencilAttachmentDescriptor stencil0;
stencil0.stencil_compare = CompareFunction::kEqual;
desc.SetStencilAttachmentDescriptors(stencil0);
desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil);
desc.SetStencilPixelFormat(
context.GetDeviceCapabilities().GetDefaultStencilFormat());
}

return true;
Expand Down
6 changes: 4 additions & 2 deletions impeller/renderer/render_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ RenderTarget RenderTarget::CreateOffscreen(
if (stencil_attachment_config.has_value()) {
TextureDescriptor stencil_tex0;
stencil_tex0.storage_mode = stencil_attachment_config->storage_mode;
stencil_tex0.format = PixelFormat::kDefaultStencil;
stencil_tex0.format =
context.GetDeviceCapabilities().GetDefaultStencilFormat();
stencil_tex0.size = size;
stencil_tex0.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
Expand Down Expand Up @@ -318,7 +319,8 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
stencil_tex0.storage_mode = stencil_attachment_config->storage_mode;
stencil_tex0.type = TextureType::kTexture2DMultisample;
stencil_tex0.sample_count = SampleCount::kCount4;
stencil_tex0.format = PixelFormat::kDefaultStencil;
stencil_tex0.format =
context.GetDeviceCapabilities().GetDefaultStencilFormat();
stencil_tex0.size = size;
stencil_tex0.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
Expand Down
6 changes: 4 additions & 2 deletions impeller/runtime_stage/runtime_stage_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,14 @@ TEST_P(RuntimeStageTest, CanCreatePipelineFromRuntimeStage) {
ASSERT_TRUE(vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs));
desc.SetVertexDescriptor(std::move(vertex_descriptor));
ColorAttachmentDescriptor color0;
color0.format = PixelFormat::kDefaultColor;
color0.format = GetContext()->GetColorAttachmentPixelFormat();
StencilAttachmentDescriptor stencil0;
stencil0.stencil_compare = CompareFunction::kEqual;
desc.SetColorAttachmentDescriptor(0u, color0);
desc.SetStencilAttachmentDescriptors(stencil0);
desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil);
const auto stencil_fmt =
GetContext()->GetDeviceCapabilities().GetDefaultStencilFormat();
desc.SetStencilPixelFormat(stencil_fmt);
auto pipeline = GetContext()->GetPipelineLibrary()->GetPipeline(desc).Get();
ASSERT_NE(pipeline, nullptr);
}
Expand Down