Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 74d93dc

Browse files
[Impeller] Set RGBA8888 as the default Vulkan color format before the app acquires a surface (#51770)
An app may render offscreen images before it gets a window. The ContextVK needs to have a default color format so that render passes can be created before the ContextVK is updated to use the window's format.
1 parent dfced6d commit 74d93dc

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

impeller/renderer/backend/vulkan/capabilities_vk.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static bool HasSuitableDepthStencilFormat(const vk::PhysicalDevice& device,
297297
static bool PhysicalDeviceSupportsRequiredFormats(
298298
const vk::PhysicalDevice& device) {
299299
const auto has_color_format =
300-
HasSuitableColorFormat(device, vk::Format::eB8G8R8A8Unorm);
300+
HasSuitableColorFormat(device, vk::Format::eR8G8B8A8Unorm);
301301
const auto has_stencil_format =
302302
HasSuitableDepthStencilFormat(device, vk::Format::eD32SfloatS8Uint) ||
303303
HasSuitableDepthStencilFormat(device, vk::Format::eD24UnormS8Uint);
@@ -412,6 +412,12 @@ void CapabilitiesVK::SetOffscreenFormat(PixelFormat pixel_format) const {
412412
}
413413

414414
bool CapabilitiesVK::SetPhysicalDevice(const vk::PhysicalDevice& device) {
415+
if (HasSuitableColorFormat(device, vk::Format::eR8G8B8A8Unorm)) {
416+
default_color_format_ = PixelFormat::kR8G8B8A8UNormInt;
417+
} else {
418+
default_color_format_ = PixelFormat::kUnknown;
419+
}
420+
415421
if (HasSuitableDepthStencilFormat(device, vk::Format::eD32SfloatS8Uint)) {
416422
default_depth_stencil_format_ = PixelFormat::kD32FloatS8UInt;
417423
} else if (HasSuitableDepthStencilFormat(device,

impeller/renderer/backend/vulkan/context_vk_unittests.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ TEST(CapabilitiesVKTest, ContextInitializesWithNoStencilFormat) {
167167
.SetPhysicalDeviceFormatPropertiesCallback(
168168
[](VkPhysicalDevice physicalDevice, VkFormat format,
169169
VkFormatProperties* pFormatProperties) {
170-
if (format == VK_FORMAT_B8G8R8A8_UNORM) {
170+
if (format == VK_FORMAT_R8G8B8A8_UNORM) {
171171
pFormatProperties->optimalTilingFeatures =
172172
static_cast<VkFormatFeatureFlags>(
173173
vk::FormatFeatureFlagBits::eColorAttachment);
@@ -200,7 +200,7 @@ TEST(CapabilitiesVKTest,
200200
.SetPhysicalDeviceFormatPropertiesCallback(
201201
[](VkPhysicalDevice physicalDevice, VkFormat format,
202202
VkFormatProperties* pFormatProperties) {
203-
if (format == VK_FORMAT_B8G8R8A8_UNORM) {
203+
if (format == VK_FORMAT_R8G8B8A8_UNORM) {
204204
pFormatProperties->optimalTilingFeatures =
205205
static_cast<VkFormatFeatureFlags>(
206206
vk::FormatFeatureFlagBits::eColorAttachment);
@@ -222,5 +222,13 @@ TEST(ContextVKTest, WarmUpFunctionCreatesRenderPass) {
222222
"vkCreateRenderPass") != functions->end());
223223
}
224224

225+
TEST(ContextVKTest, HasDefaultColorFormat) {
226+
std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
227+
228+
const CapabilitiesVK* capabilites_vk =
229+
reinterpret_cast<const CapabilitiesVK*>(context->GetCapabilities().get());
230+
ASSERT_NE(capabilites_vk->GetDefaultColorFormat(), PixelFormat::kUnknown);
231+
}
232+
225233
} // namespace testing
226234
} // namespace impeller

impeller/renderer/backend/vulkan/test/mock_vulkan.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ MockVulkanContextBuilder::MockVulkanContextBuilder()
894894
format_properties_callback_([](VkPhysicalDevice physicalDevice,
895895
VkFormat format,
896896
VkFormatProperties* pFormatProperties) {
897-
if (format == VK_FORMAT_B8G8R8A8_UNORM) {
897+
if (format == VK_FORMAT_R8G8B8A8_UNORM) {
898898
pFormatProperties->optimalTilingFeatures =
899899
static_cast<VkFormatFeatureFlags>(
900900
vk::FormatFeatureFlagBits::eColorAttachment);

0 commit comments

Comments
 (0)