Skip to content

Commit

Permalink
Setup default pixel formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 687f189 commit 8748c76
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 21 deletions.
1 change: 1 addition & 0 deletions impeller/compiler/compiler_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion impeller/playground/playground.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -127,7 +128,8 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
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;

Expand Down Expand Up @@ -175,6 +177,7 @@ CompressedImage compressed_image(
}

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;
Expand Down
22 changes: 22 additions & 0 deletions impeller/renderer/backend/metal/formats_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 31 additions & 16 deletions impeller/renderer/backend/metal/surface_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -27,46 +28,60 @@
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;
Expand Down
9 changes: 9 additions & 0 deletions impeller/renderer/formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions impeller/renderer/pipeline_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions impeller/renderer/render_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8748c76

Please sign in to comment.