@@ -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;
0 commit comments