[Chore] (MG_Backend/DirectVulkan): get rid of 1x1 texture fallback path for unbinded sampler

This commit is contained in:
2026-05-07 09:30:10 +08:00
parent f600a07404
commit 22582dea3e
2 changed files with 6 additions and 66 deletions
@@ -10,38 +10,10 @@
#include "MG_State/GLState/Core.h"
#include "MG_State/GLState/ProgramState/ProgramObject.h"
#include "MG_State/GLState/TextureState/TextureObject2D.h"
#include "MG_Util/Converters/MGToStr/FramebufferEnumConverter.h"
#include <limits>
namespace MobileGL::MG_Backend::DirectVulkan {
namespace {
constexpr Uint kFallbackTextureExternalIndexBase = 0xFFFF0000u;
SharedPtr<MG_State::GLState::ITextureObject> CreateFallbackTexture2D(Uint externalIndex) {
static constexpr Array<Uint8, 4> kBlackPixel = {0, 0, 0, 255};
auto texture = MakeShared<MG_State::GLState::TextureObject2D>(externalIndex);
texture->SetInternalFormat(TextureInternalFormat::RGBA8);
texture->AllocateStorage(TextureUploadTarget::Texture2D, 0,
{.texelSize = {1, 1, 1}, .byteSize = kBlackPixel.size()});
texture->UpdateMipmapSubData(TextureUploadTarget::Texture2D, 0,
{.data = const_cast<Uint8*>(kBlackPixel.data()),
.size = kBlackPixel.size()});
texture->SetBaseLevel(0);
texture->SetMaxLevel(0);
const auto& sampler = texture->GetSamplerObject();
MOBILEGL_ASSERT(sampler != nullptr, "CreateFallbackTexture2D: texture sampler is null");
sampler->SetWrapS(SamplerWrapMode::ClampToEdge);
sampler->SetWrapT(SamplerWrapMode::ClampToEdge);
sampler->SetMinFilter(SamplerFilterMode::Nearest);
sampler->SetMagFilter(SamplerFilterMode::Nearest);
sampler->SetMipmapMode(SamplerMipmapMode::None);
return texture;
}
}
static Bool FindFramebufferAttachmentForTexture(const MG_State::GLState::FramebufferObject& framebuffer,
const MG_State::GLState::ITextureObject& texture,
FramebufferAttachmentType& outAttachment, Int& outLevel) {
@@ -173,9 +145,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_peakDescriptorSetsObserved = 0;
m_textureManager = nullptr;
m_samplerManager = nullptr;
for (auto& fallbackTexture : m_samplerFallbackTextures) {
fallbackTexture.reset();
}
}
void UniformManager::BeginFrame(Uint32 frameIndex) {
@@ -307,7 +276,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool UniformManager::ResolveSamplerTexture(const MG_State::GLState::ProgramObject& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 binding,
SharedPtr<MG_State::GLState::ITextureObject>& outTexture) const {
SharedPtr<MG_State::GLState::ITextureObject>& outTexture) {
outTexture.reset();
MOBILEGL_ASSERT(MG_State::pGLContext != nullptr, "ResolveSamplerTexture: GL context is null");
MOBILEGL_ASSERT(binding < programObj.samplerUniformLocationByBinding.size(),
@@ -322,39 +291,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const TextureTarget preferredTarget = programObj.samplerTextureTargetByBinding[binding];
outTexture = textureUnit.GetBindingSlot(preferredTarget).GetBoundObject();
if (!outTexture) {
outTexture = GetOrCreateSamplerFallbackTexture(preferredTarget);
}
MOBILEGL_ASSERT(outTexture,
"ResolveSamplerTexture: no texture bound for active sampler binding=%u location=%d unit=%d target=%d",
binding, location, unit, static_cast<Int>(preferredTarget));
if (!outTexture) {
MGLOG_E(
"ResolveSamplerTexture: no texture bound and no fallback available for sampler binding=%u location=%d unit=%d target=%d",
binding, location, unit, static_cast<Int>(preferredTarget));
return false;
}
return outTexture != nullptr;
}
SharedPtr<MG_State::GLState::ITextureObject>
UniformManager::GetOrCreateSamplerFallbackTexture(TextureTarget target) const {
const SizeT targetIndex = static_cast<SizeT>(target);
MOBILEGL_ASSERT(targetIndex < m_samplerFallbackTextures.size(),
"GetOrCreateSamplerFallbackTexture: invalid texture target %d", static_cast<Int>(target));
auto& fallbackTexture = m_samplerFallbackTextures[targetIndex];
if (fallbackTexture != nullptr) {
return fallbackTexture;
}
switch (target) {
case TextureTarget::Texture2D:
fallbackTexture = CreateFallbackTexture2D(kFallbackTextureExternalIndexBase + static_cast<Uint>(targetIndex));
return fallbackTexture;
default:
return nullptr;
}
}
Bool UniformManager::CollectSampledTextures(const MG_State::GLState::ProgramObject& program,
const ProgramFactory::VkProgramObject& programObj,
Vector<MG_State::GLState::ITextureObject*>& outTextures) {
@@ -62,10 +62,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 peakAllocatedSetsThisFrame = 0;
};
Bool ResolveSamplerTexture(const MG_State::GLState::ProgramObject& program,
static Bool ResolveSamplerTexture(const MG_State::GLState::ProgramObject& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 binding,
SharedPtr<MG_State::GLState::ITextureObject>& outTexture) const;
SharedPtr<MG_State::GLState::ITextureObject> GetOrCreateSamplerFallbackTexture(TextureTarget target) const;
SharedPtr<MG_State::GLState::ITextureObject>& outTexture);
Bool ResolveSamplerDescriptor(VkCommandBuffer commandBuffer, const MG_State::GLState::ProgramObject& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 binding,
VkDescriptorImageInfo& outImageInfo) const;
@@ -91,8 +90,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 m_peakDescriptorSetsObserved = 0;
VkTextureManager* m_textureManager = nullptr;
VkSamplerManager* m_samplerManager = nullptr;
mutable Array<SharedPtr<MG_State::GLState::ITextureObject>,
static_cast<SizeT>(TextureTarget::TextureTargetCount)> m_samplerFallbackTextures{};
};
} // namespace MobileGL::MG_Backend::DirectVulkan