Setup default pixel formats.

This commit is contained in:
Chinmay Garde
2022-01-06 12:16:50 -08:00
committed by Dan Field
parent b65ff957d1
commit 7d52f15ab7
7 changed files with 71 additions and 21 deletions

View File

@@ -30,6 +30,7 @@ class CompilerTest : public ::testing::Test {
return false;
}
Compiler::SourceOptions compiler_options(fixture_name);
compiler_options.target_platform = Compiler::TargetPlatform::kMacOS;
compiler_options.working_directory = std::make_shared<fml::UniqueFD>(
flutter::testing::OpenFixturesDirectory());
Reflector::Options reflector_options;

View File

@@ -13,6 +13,7 @@
#include "impeller/playground/playground.h"
#include "impeller/renderer/allocator.h"
#include "impeller/renderer/backend/metal/context_mtl.h"
#include "impeller/renderer/backend/metal/formats_mtl.h"
#include "impeller/renderer/backend/metal/surface_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
#include "impeller/renderer/context.h"
@@ -127,7 +128,8 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) {
NSWindow* cocoa_window = ::glfwGetCocoaWindow(window);
CAMetalLayer* layer = [CAMetalLayer layer];
layer.device = ContextMTL::Cast(*renderer_.GetContext()).GetMTLDevice();
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
// This pixel format is one of the documented supported formats.
layer.pixelFormat = ToMTLPixelFormat(PixelFormat::kDefaultColor);
cocoa_window.contentView.layer = layer;
cocoa_window.contentView.wantsLayer = YES;
@@ -175,6 +177,7 @@ std::shared_ptr<Texture> Playground::CreateTextureForFixture(
}
auto texture_descriptor = TextureDescriptor{};
// We just converted to RGBA above.
texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
texture_descriptor.size = image.GetSize();
texture_descriptor.mip_count = 1u;

View File

@@ -17,6 +17,28 @@ namespace impeller {
class RenderTarget;
constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format) {
switch (format) {
case MTLPixelFormatInvalid:
return PixelFormat::kUnknown;
case MTLPixelFormatBGRA8Unorm:
return PixelFormat::kB8G8R8A8UNormInt;
case MTLPixelFormatBGRA8Unorm_sRGB:
return PixelFormat::kB8G8R8A8UNormIntSRGB;
case MTLPixelFormatDepth32Float_Stencil8:
return PixelFormat::kD32FloatS8UNormInt;
case MTLPixelFormatRGBA8Unorm:
return PixelFormat::kR8G8B8A8UNormInt;
case MTLPixelFormatStencil8:
return PixelFormat::kS8UInt;
case MTLPixelFormatRGBA8Unorm_sRGB:
return PixelFormat::kR8G8B8A8UNormIntSRGB;
default:
return PixelFormat::kUnknown;
}
return PixelFormat::kUnknown;
}
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
switch (format) {
case PixelFormat::kUnknown:

View File

@@ -6,6 +6,7 @@
#include "flutter/fml/trace_event.h"
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/metal/formats_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
#include "impeller/renderer/render_target.h"
@@ -27,46 +28,60 @@ std::unique_ptr<Surface> SurfaceMTL::WrapCurrentMetalLayerDrawable(
return nullptr;
}
TextureDescriptor msaa_tex_desc;
msaa_tex_desc.type = TextureType::k2DMultisample;
msaa_tex_desc.sample_count = SampleCount::kCount4;
msaa_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
msaa_tex_desc.size = {
const auto color_format =
FromMTLPixelFormat(current_drawable.texture.pixelFormat);
if (color_format == PixelFormat::kUnknown) {
VALIDATION_LOG << "Unknown drawable color format.";
return nullptr;
}
TextureDescriptor color0_tex_desc;
color0_tex_desc.type = TextureType::k2DMultisample;
color0_tex_desc.sample_count = SampleCount::kCount4;
color0_tex_desc.format = color_format;
color0_tex_desc.size = {
static_cast<ISize::Type>(current_drawable.texture.width),
static_cast<ISize::Type>(current_drawable.texture.height)};
msaa_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
color0_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
auto msaa_tex = context->GetPermanentsAllocator()->CreateTexture(
StorageMode::kDeviceTransient, msaa_tex_desc);
StorageMode::kDeviceTransient, color0_tex_desc);
if (!msaa_tex) {
FML_LOG(ERROR) << "Could not allocate MSAA resolve texture.";
VALIDATION_LOG << "Could not allocate MSAA resolve texture.";
return nullptr;
}
msaa_tex->SetLabel("ImpellerOnscreenColor4xMSAA");
TextureDescriptor onscreen_tex_desc;
onscreen_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
onscreen_tex_desc.size = msaa_tex_desc.size;
onscreen_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
TextureDescriptor color0_resolve_tex_desc;
color0_resolve_tex_desc.format = color_format;
color0_resolve_tex_desc.size = color0_tex_desc.size;
color0_resolve_tex_desc.usage =
static_cast<uint64_t>(TextureUsage::kRenderTarget);
ColorAttachment color0;
color0.texture = msaa_tex;
color0.clear_color = Color::DarkSlateGray();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kMultisampleResolve;
color0.resolve_texture =
std::make_shared<TextureMTL>(onscreen_tex_desc, current_drawable.texture);
color0.resolve_texture = std::make_shared<TextureMTL>(
color0_resolve_tex_desc, current_drawable.texture);
TextureDescriptor stencil0_tex;
stencil0_tex.type = TextureType::k2DMultisample;
stencil0_tex.sample_count = SampleCount::kCount4;
stencil0_tex.format = PixelFormat::kS8UInt;
stencil0_tex.size = msaa_tex_desc.size;
stencil0_tex.format = PixelFormat::kDefaultStencil;
stencil0_tex.size = color0_tex_desc.size;
stencil0_tex.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
auto stencil_texture = context->GetPermanentsAllocator()->CreateTexture(
StorageMode::kDeviceTransient, stencil0_tex);
if (!stencil_texture) {
VALIDATION_LOG << "Could not create stencil texture.";
return nullptr;
}
stencil_texture->SetLabel("ImpellerOnscreenStencil");
StencilAttachment stencil0;

View File

@@ -51,8 +51,17 @@ enum class PixelFormat {
kB8G8R8A8UNormInt,
kB8G8R8A8UNormIntSRGB,
kS8UInt,
// Esoteric formats only used as render targets.
kD32FloatS8UNormInt,
// 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 {

View File

@@ -99,7 +99,7 @@ struct PipelineBuilder {
// Configure the sole color attachments pixel format. This is by
// convention.
ColorAttachmentDescriptor color0;
color0.format = PixelFormat::kB8G8R8A8UNormInt;
color0.format = PixelFormat::kDefaultColor;
color0.blending_enabled = true;
desc.SetColorAttachmentDescriptor(0u, std::move(color0));
}
@@ -109,7 +109,7 @@ struct PipelineBuilder {
StencilAttachmentDescriptor stencil0;
stencil0.stencil_compare = CompareFunction::kLessEqual;
desc.SetStencilAttachmentDescriptors(stencil0);
desc.SetStencilPixelFormat(PixelFormat::kS8UInt);
desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil);
}
return true;

View File

@@ -182,13 +182,13 @@ RenderTarget RenderTarget::CreateOffscreen(const Context& context,
}
TextureDescriptor color_tex0;
color_tex0.format = PixelFormat::kB8G8R8A8UNormInt;
color_tex0.format = PixelFormat::kDefaultColor;
color_tex0.size = size;
color_tex0.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget) |
static_cast<uint64_t>(TextureUsage::kShaderRead);
TextureDescriptor stencil_tex0;
stencil_tex0.format = PixelFormat::kS8UInt;
stencil_tex0.format = PixelFormat::kDefaultStencil;
stencil_tex0.size = size;
stencil_tex0.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);