Skip to content

Commit 8748c76

Browse files
chinmaygardednfield
authored andcommitted
Setup default pixel formats.
1 parent 687f189 commit 8748c76

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

impeller/compiler/compiler_unittests.cc

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CompilerTest : public ::testing::Test {
3030
return false;
3131
}
3232
Compiler::SourceOptions compiler_options(fixture_name);
33+
compiler_options.target_platform = Compiler::TargetPlatform::kMacOS;
3334
compiler_options.working_directory = std::make_shared<fml::UniqueFD>(
3435
flutter::testing::OpenFixturesDirectory());
3536
Reflector::Options reflector_options;

impeller/playground/playground.mm

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "impeller/playground/playground.h"
1414
#include "impeller/renderer/allocator.h"
1515
#include "impeller/renderer/backend/metal/context_mtl.h"
16+
#include "impeller/renderer/backend/metal/formats_mtl.h"
1617
#include "impeller/renderer/backend/metal/surface_mtl.h"
1718
#include "impeller/renderer/backend/metal/texture_mtl.h"
1819
#include "impeller/renderer/context.h"
@@ -127,7 +128,8 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
127128
NSWindow* cocoa_window = ::glfwGetCocoaWindow(window);
128129
CAMetalLayer* layer = [CAMetalLayer layer];
129130
layer.device = ContextMTL::Cast(*renderer_.GetContext()).GetMTLDevice();
130-
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
131+
// This pixel format is one of the documented supported formats.
132+
layer.pixelFormat = ToMTLPixelFormat(PixelFormat::kDefaultColor);
131133
cocoa_window.contentView.layer = layer;
132134
cocoa_window.contentView.wantsLayer = YES;
133135

@@ -175,6 +177,7 @@ CompressedImage compressed_image(
175177
}
176178

177179
auto texture_descriptor = TextureDescriptor{};
180+
// We just converted to RGBA above.
178181
texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
179182
texture_descriptor.size = image.GetSize();
180183
texture_descriptor.mip_count = 1u;

impeller/renderer/backend/metal/formats_mtl.h

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ namespace impeller {
1717

1818
class RenderTarget;
1919

20+
constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format) {
21+
switch (format) {
22+
case MTLPixelFormatInvalid:
23+
return PixelFormat::kUnknown;
24+
case MTLPixelFormatBGRA8Unorm:
25+
return PixelFormat::kB8G8R8A8UNormInt;
26+
case MTLPixelFormatBGRA8Unorm_sRGB:
27+
return PixelFormat::kB8G8R8A8UNormIntSRGB;
28+
case MTLPixelFormatDepth32Float_Stencil8:
29+
return PixelFormat::kD32FloatS8UNormInt;
30+
case MTLPixelFormatRGBA8Unorm:
31+
return PixelFormat::kR8G8B8A8UNormInt;
32+
case MTLPixelFormatStencil8:
33+
return PixelFormat::kS8UInt;
34+
case MTLPixelFormatRGBA8Unorm_sRGB:
35+
return PixelFormat::kR8G8B8A8UNormIntSRGB;
36+
default:
37+
return PixelFormat::kUnknown;
38+
}
39+
return PixelFormat::kUnknown;
40+
}
41+
2042
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
2143
switch (format) {
2244
case PixelFormat::kUnknown:

impeller/renderer/backend/metal/surface_mtl.mm

+31-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "flutter/fml/trace_event.h"
88
#include "impeller/base/validation.h"
9+
#include "impeller/renderer/backend/metal/formats_mtl.h"
910
#include "impeller/renderer/backend/metal/texture_mtl.h"
1011
#include "impeller/renderer/render_target.h"
1112

@@ -27,46 +28,60 @@
2728
return nullptr;
2829
}
2930

30-
TextureDescriptor msaa_tex_desc;
31-
msaa_tex_desc.type = TextureType::k2DMultisample;
32-
msaa_tex_desc.sample_count = SampleCount::kCount4;
33-
msaa_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
34-
msaa_tex_desc.size = {
31+
const auto color_format =
32+
FromMTLPixelFormat(current_drawable.texture.pixelFormat);
33+
34+
if (color_format == PixelFormat::kUnknown) {
35+
VALIDATION_LOG << "Unknown drawable color format.";
36+
return nullptr;
37+
}
38+
39+
TextureDescriptor color0_tex_desc;
40+
color0_tex_desc.type = TextureType::k2DMultisample;
41+
color0_tex_desc.sample_count = SampleCount::kCount4;
42+
color0_tex_desc.format = color_format;
43+
color0_tex_desc.size = {
3544
static_cast<ISize::Type>(current_drawable.texture.width),
3645
static_cast<ISize::Type>(current_drawable.texture.height)};
37-
msaa_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
46+
color0_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
3847

3948
auto msaa_tex = context->GetPermanentsAllocator()->CreateTexture(
40-
StorageMode::kDeviceTransient, msaa_tex_desc);
49+
StorageMode::kDeviceTransient, color0_tex_desc);
4150
if (!msaa_tex) {
42-
FML_LOG(ERROR) << "Could not allocate MSAA resolve texture.";
51+
VALIDATION_LOG << "Could not allocate MSAA resolve texture.";
4352
return nullptr;
4453
}
4554

4655
msaa_tex->SetLabel("ImpellerOnscreenColor4xMSAA");
4756

48-
TextureDescriptor onscreen_tex_desc;
49-
onscreen_tex_desc.format = PixelFormat::kB8G8R8A8UNormInt;
50-
onscreen_tex_desc.size = msaa_tex_desc.size;
51-
onscreen_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
57+
TextureDescriptor color0_resolve_tex_desc;
58+
color0_resolve_tex_desc.format = color_format;
59+
color0_resolve_tex_desc.size = color0_tex_desc.size;
60+
color0_resolve_tex_desc.usage =
61+
static_cast<uint64_t>(TextureUsage::kRenderTarget);
5262

5363
ColorAttachment color0;
5464
color0.texture = msaa_tex;
5565
color0.clear_color = Color::DarkSlateGray();
5666
color0.load_action = LoadAction::kClear;
5767
color0.store_action = StoreAction::kMultisampleResolve;
58-
color0.resolve_texture =
59-
std::make_shared<TextureMTL>(onscreen_tex_desc, current_drawable.texture);
68+
color0.resolve_texture = std::make_shared<TextureMTL>(
69+
color0_resolve_tex_desc, current_drawable.texture);
6070

6171
TextureDescriptor stencil0_tex;
6272
stencil0_tex.type = TextureType::k2DMultisample;
6373
stencil0_tex.sample_count = SampleCount::kCount4;
64-
stencil0_tex.format = PixelFormat::kS8UInt;
65-
stencil0_tex.size = msaa_tex_desc.size;
74+
stencil0_tex.format = PixelFormat::kDefaultStencil;
75+
stencil0_tex.size = color0_tex_desc.size;
6676
stencil0_tex.usage =
6777
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
6878
auto stencil_texture = context->GetPermanentsAllocator()->CreateTexture(
6979
StorageMode::kDeviceTransient, stencil0_tex);
80+
81+
if (!stencil_texture) {
82+
VALIDATION_LOG << "Could not create stencil texture.";
83+
return nullptr;
84+
}
7085
stencil_texture->SetLabel("ImpellerOnscreenStencil");
7186

7287
StencilAttachment stencil0;

impeller/renderer/formats.h

+9
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ enum class PixelFormat {
5151
kB8G8R8A8UNormInt,
5252
kB8G8R8A8UNormIntSRGB,
5353
kS8UInt,
54+
5455
// Esoteric formats only used as render targets.
5556
kD32FloatS8UNormInt,
57+
58+
// Defaults. If you don't know which ones to use, these are usually a safe
59+
// bet.
60+
//
61+
// On Metal, this is a support format for layer drawable and can be used to
62+
// specify the format of the resolve texture if needed.
63+
kDefaultColor = kB8G8R8A8UNormInt,
64+
kDefaultStencil = kS8UInt,
5665
};
5766

5867
enum class BlendFactor {

impeller/renderer/pipeline_builder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct PipelineBuilder {
9999
// Configure the sole color attachments pixel format. This is by
100100
// convention.
101101
ColorAttachmentDescriptor color0;
102-
color0.format = PixelFormat::kB8G8R8A8UNormInt;
102+
color0.format = PixelFormat::kDefaultColor;
103103
color0.blending_enabled = true;
104104
desc.SetColorAttachmentDescriptor(0u, std::move(color0));
105105
}
@@ -109,7 +109,7 @@ struct PipelineBuilder {
109109
StencilAttachmentDescriptor stencil0;
110110
stencil0.stencil_compare = CompareFunction::kLessEqual;
111111
desc.SetStencilAttachmentDescriptors(stencil0);
112-
desc.SetStencilPixelFormat(PixelFormat::kS8UInt);
112+
desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil);
113113
}
114114

115115
return true;

impeller/renderer/render_target.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ RenderTarget RenderTarget::CreateOffscreen(const Context& context,
182182
}
183183

184184
TextureDescriptor color_tex0;
185-
color_tex0.format = PixelFormat::kB8G8R8A8UNormInt;
185+
color_tex0.format = PixelFormat::kDefaultColor;
186186
color_tex0.size = size;
187187
color_tex0.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget) |
188188
static_cast<uint64_t>(TextureUsage::kShaderRead);
189189

190190
TextureDescriptor stencil_tex0;
191-
stencil_tex0.format = PixelFormat::kS8UInt;
191+
stencil_tex0.format = PixelFormat::kDefaultStencil;
192192
stencil_tex0.size = size;
193193
stencil_tex0.usage =
194194
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);

0 commit comments

Comments
 (0)