[Impeller] Add validation for mipmap generation enforced at binding time (flutter/engine#40515)

This commit is contained in:
Brandon DeRosier
2023-03-22 15:23:53 -07:00
committed by GitHub
parent 86aaebef19
commit 99e492100c
14 changed files with 56 additions and 12 deletions

View File

@@ -163,7 +163,7 @@ std::string BlitGenerateMipmapCommandGLES::GetLabel() const {
bool BlitGenerateMipmapCommandGLES::Encode(const ReactorGLES& reactor) const {
auto texture_gles = TextureGLES::Cast(texture.get());
if (!texture_gles->GenerateMipmaps()) {
if (!texture_gles->GenerateMipmap()) {
return false;
}

View File

@@ -3,8 +3,10 @@
// found in the LICENSE file.
#include "impeller/renderer/backend/gles/sampler_gles.h"
#include <iostream>
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/gles/formats_gles.h"
#include "impeller/renderer/backend/gles/proc_table_gles.h"
#include "impeller/renderer/backend/gles/texture_gles.h"
@@ -69,6 +71,13 @@ bool SamplerGLES::ConfigureBoundTexture(const TextureGLES& texture,
return false;
}
if (texture.NeedsMipmapGeneration()) {
VALIDATION_LOG
<< "Texture mip count is > 1, but the mipmap has not been generated. "
"Texture can not be sampled safely.";
return false;
}
auto target = ToTextureTarget(texture.GetTextureDescriptor().type);
if (!target.has_value()) {

View File

@@ -425,7 +425,7 @@ bool TextureGLES::Bind() const {
return true;
}
bool TextureGLES::GenerateMipmaps() const {
bool TextureGLES::GenerateMipmap() {
if (!IsValid()) {
return false;
}
@@ -453,6 +453,7 @@ bool TextureGLES::GenerateMipmaps() const {
const auto& gl = reactor_->GetProcTable();
gl.GenerateMipmap(ToTextureType(type));
mipmap_generated_ = true;
return true;
}

View File

@@ -37,7 +37,7 @@ class TextureGLES final : public Texture,
[[nodiscard]] bool Bind() const;
[[nodiscard]] bool GenerateMipmaps() const;
[[nodiscard]] bool GenerateMipmap();
enum class AttachmentPoint {
kColor0,

View File

@@ -147,14 +147,7 @@ std::string BlitGenerateMipmapCommandMTL::GetLabel() const {
bool BlitGenerateMipmapCommandMTL::Encode(
id<MTLBlitCommandEncoder> encoder) const {
auto texture_mtl = TextureMTL::Cast(*texture).GetMTLTexture();
if (!texture_mtl) {
return false;
}
[encoder generateMipmapsForTexture:texture_mtl];
return true;
return TextureMTL::Cast(*texture).GenerateMipmap(encoder);
};
} // namespace impeller

View File

@@ -382,6 +382,13 @@ static bool Bind(PassBindingsCache& pass,
return false;
}
if (texture.NeedsMipmapGeneration()) {
VALIDATION_LOG
<< "Texture at binding index " << bind_index
<< " has a mip count > 1, but the mipmap has not been generated.";
return false;
}
return pass.SetTexture(stage, bind_index,
TextureMTL::Cast(texture).GetMTLTexture());
}

View File

@@ -29,6 +29,8 @@ class TextureMTL final : public Texture,
bool IsWrapped() const;
bool GenerateMipmap(id<MTLBlitCommandEncoder> encoder);
private:
id<MTLTexture> texture_ = nullptr;
bool is_valid_ = false;

View File

@@ -92,4 +92,15 @@ bool TextureMTL::IsWrapped() const {
return is_wrapped_;
}
bool TextureMTL::GenerateMipmap(id<MTLBlitCommandEncoder> encoder) {
if (!texture_) {
return false;
}
[encoder generateMipmapsForTexture:texture_];
mipmap_generated_ = true;
return true;
}
} // namespace impeller

View File

@@ -148,7 +148,7 @@ static void InsertImageMemoryBarrier(const vk::CommandBuffer& cmd,
}
bool BlitGenerateMipmapCommandVK::Encode(CommandEncoderVK& encoder) const {
const auto& src = TextureVK::Cast(*texture);
auto& src = TextureVK::Cast(*texture);
const auto size = src.GetTextureDescriptor().size;
uint32_t mip_count = src.GetTextureDescriptor().mip_count;
@@ -249,6 +249,7 @@ bool BlitGenerateMipmapCommandVK::Encode(CommandEncoderVK& encoder) const {
// state so it doesn't try to perform redundant transitions under the hood.
src.SetLayoutWithoutEncoding(vk::ImageLayout::eShaderReadOnlyOptimal);
src.SetMipmapGenerated();
return true;
}

View File

@@ -351,6 +351,12 @@ static bool UpdateDescriptorSets(vk::Device device,
}
auto texture = bindings.textures.at(index).resource;
if (texture->NeedsMipmapGeneration()) {
VALIDATION_LOG
<< "Texture at binding index " << index
<< " has a mip count > 1, but the mipmap has not been generated.";
return false;
}
const auto& texture_vk = TextureVK::Cast(*texture);
const SamplerVK& sampler = SamplerVK::Cast(*sampler_handle.resource);

View File

@@ -85,6 +85,10 @@ vk::ImageLayout TextureVK::GetLayout() const {
return layout_;
}
void TextureVK::SetMipmapGenerated() {
mipmap_generated_ = true;
}
vk::ImageLayout TextureVK::SetLayoutWithoutEncoding(
vk::ImageLayout layout) const {
WriterLock lock(layout_mutex_);

View File

@@ -36,6 +36,8 @@ class TextureVK final : public Texture, public BackendCast<TextureVK, Texture> {
vk::ImageLayout GetLayout() const;
void SetMipmapGenerated();
private:
std::weak_ptr<Context> context_;
std::shared_ptr<TextureSourceVK> source_;

View File

@@ -73,4 +73,8 @@ Scalar Texture::GetYCoordScale() const {
return 1.0;
}
bool Texture::NeedsMipmapGeneration() const {
return !mipmap_generated_ && desc_.mip_count > 1;
}
} // namespace impeller

View File

@@ -41,6 +41,8 @@ class Texture {
virtual Scalar GetYCoordScale() const;
bool NeedsMipmapGeneration() const;
protected:
explicit Texture(TextureDescriptor desc);
@@ -52,6 +54,8 @@ class Texture {
std::shared_ptr<const fml::Mapping> mapping,
size_t slice) = 0;
bool mipmap_generated_ = false;
private:
TextureIntent intent_ = TextureIntent::kRenderToTexture;
const TextureDescriptor desc_;