From 22582dea3e20b1d3573ef92f6b629a341bdbab7b Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Thu, 7 May 2026 09:16:16 +0800 Subject: [PATCH] [Chore] (MG_Backend/DirectVulkan): get rid of 1x1 texture fallback path for unbinded sampler --- .../DirectVulkan/Renderer/UniformManager.cpp | 65 ++----------------- .../DirectVulkan/Renderer/UniformManager.h | 7 +- 2 files changed, 6 insertions(+), 66 deletions(-) diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.cpp index f0f24ea6..0ada82ba 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.cpp @@ -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 namespace MobileGL::MG_Backend::DirectVulkan { - namespace { - constexpr Uint kFallbackTextureExternalIndexBase = 0xFFFF0000u; - - SharedPtr CreateFallbackTexture2D(Uint externalIndex) { - static constexpr Array kBlackPixel = {0, 0, 0, 255}; - - auto texture = MakeShared(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(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& outTexture) const { + SharedPtr& 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(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(preferredTarget)); - return false; - } return outTexture != nullptr; } - SharedPtr - UniformManager::GetOrCreateSamplerFallbackTexture(TextureTarget target) const { - const SizeT targetIndex = static_cast(target); - MOBILEGL_ASSERT(targetIndex < m_samplerFallbackTextures.size(), - "GetOrCreateSamplerFallbackTexture: invalid texture target %d", static_cast(target)); - - auto& fallbackTexture = m_samplerFallbackTextures[targetIndex]; - if (fallbackTexture != nullptr) { - return fallbackTexture; - } - - switch (target) { - case TextureTarget::Texture2D: - fallbackTexture = CreateFallbackTexture2D(kFallbackTextureExternalIndexBase + static_cast(targetIndex)); - return fallbackTexture; - default: - return nullptr; - } - } - Bool UniformManager::CollectSampledTextures(const MG_State::GLState::ProgramObject& program, const ProgramFactory::VkProgramObject& programObj, Vector& outTextures) { diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.h index f353ee15..18dfc249 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/UniformManager.h @@ -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& outTexture) const; - SharedPtr GetOrCreateSamplerFallbackTexture(TextureTarget target) const; + SharedPtr& 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, - static_cast(TextureTarget::TextureTargetCount)> m_samplerFallbackTextures{}; }; } // namespace MobileGL::MG_Backend::DirectVulkan