Skip to content

Commit e91c38b

Browse files
authored
[Impeller] Scope the broken mipmap workaround to the textures it affects (flutter#190186)
Fixes bdero/flutter_scene#241. Related to flutter#161283. Adreno drivers corrupt blit-generated mip chains. The workaround for that rewrote every Vulkan sampler to `MipFilter::kBase`, so nothing in the process could be sampled past its base mip level. But the corruption belongs to the texture whose levels were generated, not to the sampler, so textures with hand-uploaded mip chains (prefiltered environment maps, for one) lost their mip filtering for no reason. Flutter GPU is hit hardest, since it exposes no mipmap generation at all and every mip chain it binds was uploaded by the app. The workaround now lives with the texture. On affected drivers a mipped sampled texture also gets an image view covering only the base level, and `TextureVK::GetSampledImageView` hands that view out for textures those drivers mipped. Generated chains stay unreadable past the base level, every other texture samples as requested, and the sampler library, `SamplerDescriptor`, and `lib/gpu` no longer carry any of it. A texture with `mip_count > 1` whose levels were never written is no longer masked on these devices, which matches its behavior on every other platform. flutter#190264 fixes the generation itself and will delete what is left here. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 5074afa commit e91c38b

15 files changed

Lines changed: 173 additions & 40 deletions

‎engine/src/flutter/impeller/renderer/backend/vulkan/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impeller_component("vulkan_unittests") {
4444
"test/pipeline_compile_queue_vulkan_unittests.cc",
4545
"test/sampler_library_vk_unittests.cc",
4646
"test/swapchain_unittests.cc",
47+
"texture_vk_unittests.cc",
4748
]
4849
deps = [
4950
":mock_vulkan",

‎engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,33 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
457457
}
458458
}
459459

460+
// Drivers that generate corrupt mip levels leave only the base level
461+
// trustworthy. Keep a view that stops there so those textures can still be
462+
// sampled. See `TextureVK::GetSampledImageView`.
463+
vk::UniqueImageView base_mip_image_view;
464+
if (context.GetWorkarounds().broken_mipmap_generation &&
465+
image_info.mipLevels > 1u &&
466+
!!(desc.usage & TextureUsage::kShaderRead)) {
467+
view_info.viewType = ToVKImageViewType(desc.type);
468+
view_info.subresourceRange.baseMipLevel = 0u;
469+
view_info.subresourceRange.baseArrayLayer = 0u;
470+
view_info.subresourceRange.levelCount = 1u;
471+
view_info.subresourceRange.layerCount = ToArrayLayerCount(desc);
472+
auto [base_result, base_view] = device.createImageViewUnique(view_info);
473+
if (base_result == vk::Result::eSuccess) {
474+
base_mip_image_view = std::move(base_view);
475+
} else {
476+
// The texture stays usable without it, so keep it rather than failing
477+
// the allocation.
478+
VALIDATION_LOG << "Unable to create a base mip image view: "
479+
<< vk::to_string(base_result);
480+
}
481+
}
482+
460483
resource_.Swap(ImageResource(
461484
ImageVMA{allocator, allocation, image}, std::move(image_view),
462-
std::move(rt_image_views), context.GetResourceAllocator(),
463-
context.GetDeviceHolder()));
485+
std::move(base_mip_image_view), std::move(rt_image_views),
486+
context.GetResourceAllocator(), context.GetDeviceHolder()));
464487
is_valid_ = true;
465488
}
466489

@@ -474,6 +497,10 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
474497
return resource_->image_view.get();
475498
}
476499

500+
vk::ImageView GetBaseMipImageView() const override {
501+
return resource_->base_mip_image_view.get();
502+
}
503+
477504
vk::ImageView GetRenderTargetView(uint32_t mip_level,
478505
uint32_t array_layer) const override {
479506
const auto& views = resource_->rt_image_views;
@@ -494,20 +521,25 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
494521
std::shared_ptr<Allocator> allocator;
495522
UniqueImageVMA image;
496523
vk::UniqueImageView image_view;
524+
// Covers the base mip level only. Null unless the driver generates corrupt
525+
// mip levels.
526+
vk::UniqueImageView base_mip_image_view;
497527
// One attachment view per (mip level, array layer), row-major by mip.
498528
std::vector<vk::UniqueImageView> rt_image_views;
499529

500530
ImageResource() = default;
501531

502532
ImageResource(ImageVMA p_image,
503533
vk::UniqueImageView p_image_view,
534+
vk::UniqueImageView p_base_mip_image_view,
504535
std::vector<vk::UniqueImageView> p_rt_image_views,
505536
std::shared_ptr<Allocator> allocator,
506537
std::shared_ptr<DeviceHolderVK> device_holder)
507538
: device_holder(std::move(device_holder)),
508539
allocator(std::move(allocator)),
509540
image(p_image),
510541
image_view(std::move(p_image_view)),
542+
base_mip_image_view(std::move(p_base_mip_image_view)),
511543
rt_image_views(std::move(p_rt_image_views)) {}
512544

513545
ImageResource(ImageResource&& o) = default;

‎engine/src/flutter/impeller/renderer/backend/vulkan/compute_pass_vk.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ bool ComputePassVK::BindResource(ShaderStage stage,
148148
vk::DescriptorImageInfo image_info;
149149
image_info.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
150150
image_info.sampler = sampler_vk.GetSampler();
151-
image_info.imageView = texture_vk.GetImageView();
151+
image_info.imageView = texture_vk.GetSampledImageView();
152152
image_workspace_[bound_image_offset_++] = image_info;
153153

154154
vk::WriteDescriptorSet write_set;

‎engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ void ContextVK::Setup(Settings settings) {
470470
std::make_unique<DriverInfoVK>(device_holder->physical_device);
471471
workarounds_ = GetWorkaroundsFromDriverInfo(*driver_info);
472472
caps->ApplyWorkarounds(workarounds_);
473-
sampler_library->ApplyWorkarounds(workarounds_);
474473

475474
device_holder_ = std::move(device_holder);
476475
idle_waiter_vk_ = std::make_shared<IdleWaiterVK>(device_holder_);

‎engine/src/flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ bool RenderPassVK::BindResource(ShaderStage stage,
693693
vk::DescriptorImageInfo image_info;
694694
image_info.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
695695
image_info.sampler = sampler_vk.GetSampler();
696-
image_info.imageView = texture_vk.GetImageView();
696+
image_info.imageView = texture_vk.GetSampledImageView();
697697
image_workspace_[bound_image_offset_++] = image_info;
698698

699699
vk::WriteDescriptorSet write_set;

‎engine/src/flutter/impeller/renderer/backend/vulkan/sampler_library_vk.cc‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <algorithm>
88

9-
#include "impeller/core/formats.h"
109
#include "impeller/renderer/backend/vulkan/sampler_vk.h"
1110

1211
namespace impeller {
@@ -19,16 +18,9 @@ SamplerLibraryVK::SamplerLibraryVK(
1918

2019
SamplerLibraryVK::~SamplerLibraryVK() = default;
2120

22-
void SamplerLibraryVK::ApplyWorkarounds(const WorkaroundsVK& workarounds) {
23-
mips_disabled_workaround_ = workarounds.broken_mipmap_generation;
24-
}
25-
2621
raw_ptr<const Sampler> SamplerLibraryVK::GetSampler(
2722
const SamplerDescriptor& desc) {
2823
SamplerDescriptor desc_copy = desc;
29-
if (mips_disabled_workaround_) {
30-
desc_copy.mip_filter = MipFilter::kBase;
31-
}
3224
// Clamp to the device limit before keying the cache so that all values
3325
// beyond the limit share one sampler. The limit is 1 (disabled) when the
3426
// samplerAnisotropy feature is unavailable. The upper bound is floored at 1

‎engine/src/flutter/impeller/renderer/backend/vulkan/sampler_library_vk.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "impeller/core/sampler.h"
1010
#include "impeller/core/sampler_descriptor.h"
1111
#include "impeller/renderer/backend/vulkan/device_holder_vk.h"
12-
#include "impeller/renderer/backend/vulkan/workarounds_vk.h"
1312
#include "impeller/renderer/sampler_library.h"
1413

1514
namespace impeller {
@@ -24,15 +23,12 @@ class SamplerLibraryVK final
2423
SamplerLibraryVK(const std::weak_ptr<DeviceHolderVK>& device_holder,
2524
uint32_t max_sampler_anisotropy);
2625

27-
void ApplyWorkarounds(const WorkaroundsVK& workarounds);
28-
2926
private:
3027
friend class ContextVK;
3128

3229
std::weak_ptr<DeviceHolderVK> device_holder_;
3330
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_;
3431
uint32_t max_sampler_anisotropy_ = 1;
35-
bool mips_disabled_workaround_ = false;
3632

3733
// |SamplerLibrary|
3834
raw_ptr<const Sampler> GetSampler(

‎engine/src/flutter/impeller/renderer/backend/vulkan/sampler_vk.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class SamplerVK final : public Sampler, public BackendCast<SamplerVK, Sampler> {
3737
const vk::Device device_;
3838
SharedHandleVK<vk::Sampler> sampler_;
3939
std::shared_ptr<YUVConversionVK> yuv_conversion_;
40-
bool mips_disabled_workaround_ = false;
4140
bool is_valid_ = false;
4241

4342
SamplerVK(const SamplerVK&) = delete;

‎engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
66

7+
#include <atomic>
78
#include <cstdint>
89
#include <cstring>
910
#include <utility>
@@ -557,7 +558,9 @@ VkResult vkCreateImageView(VkDevice device,
557558
const VkImageViewCreateInfo* pCreateInfo,
558559
const VkAllocationCallbacks* pAllocator,
559560
VkImageView* pView) {
560-
*pView = reinterpret_cast<VkImageView>(0xFEE1DEAD);
561+
// Handles are distinct so tests can tell two views of the same image apart.
562+
static std::atomic_uint64_t next_handle = 0xFEE1DEAD;
563+
*pView = reinterpret_cast<VkImageView>(next_handle.fetch_add(1));
561564
return VK_SUCCESS;
562565
}
563566

‎engine/src/flutter/impeller/renderer/backend/vulkan/test/sampler_library_vk_unittests.cc‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,10 @@
1010
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
1111
#include "impeller/renderer/backend/vulkan/sampler_library_vk.h"
1212
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
13-
#include "impeller/renderer/backend/vulkan/workarounds_vk.h"
1413

1514
namespace impeller {
1615
namespace testing {
1716

18-
TEST(SamplerLibraryVK, WorkaroundsCanDisableReadingFromMipLevels) {
19-
auto const context = MockVulkanContextBuilder().Build();
20-
21-
auto library_vk = std::make_shared<SamplerLibraryVK>(
22-
context->GetDeviceHolder(), /*max_sampler_anisotropy=*/1u);
23-
std::shared_ptr<SamplerLibrary> library = library_vk;
24-
25-
SamplerDescriptor desc;
26-
desc.mip_filter = MipFilter::kLinear;
27-
28-
auto sampler = library->GetSampler(desc);
29-
EXPECT_EQ(sampler->GetDescriptor().mip_filter, MipFilter::kLinear);
30-
31-
// Apply mips disabled workaround.
32-
library_vk->ApplyWorkarounds(WorkaroundsVK{.broken_mipmap_generation = true});
33-
34-
sampler = library->GetSampler(desc);
35-
EXPECT_EQ(sampler->GetDescriptor().mip_filter, MipFilter::kBase);
36-
}
37-
3817
TEST(SamplerLibraryVK, MaxAnisotropyIsClampedToTheDeviceLimit) {
3918
auto const context = MockVulkanContextBuilder().Build();
4019

0 commit comments

Comments
 (0)