From ac33292e1b5db7a4b19731e0f613a2435b62a0c9 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sat, 27 Jun 2026 12:38:26 +0800 Subject: [PATCH] [Fix] (MG_Backend/DirectGLES): fix raw depth fetch sampler on GLES --- MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp index 68faab76..20067bcd 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp @@ -41,6 +41,8 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLESCapabilities g_GLESCapabilities; static Bool QueryCurrentSurfaceSize(Int& outWidth, Int& outHeight); + static SharedPtr g_rawDepthFetchSamplerState; + static SharedPtr g_rawDepthFetchSamplerBackend; enum class DrawSyncBit : Uint32 { None = 0, @@ -73,6 +75,36 @@ namespace MobileGL::MG_Backend::DirectGLES { Uint32 baseInstance = 0; }; + SamplerImpl::BackendSamplerObject* GetRawDepthFetchSampler() { + if (!g_rawDepthFetchSamplerState) { + g_rawDepthFetchSamplerState = MakeShared(0); + g_rawDepthFetchSamplerState->SetMinFilter(SamplerFilterMode::Nearest); + g_rawDepthFetchSamplerState->SetMagFilter(SamplerFilterMode::Nearest); + g_rawDepthFetchSamplerState->SetMipmapMode(SamplerMipmapMode::None); + g_rawDepthFetchSamplerState->SetCompareMode(SamplerCompareMode::None); + g_rawDepthFetchSamplerState->SetSamplerCompareFunc(SamplerCompareFunc::Always); + g_rawDepthFetchSamplerBackend = MakeShared(); + } + g_rawDepthFetchSamplerBackend->SyncToBackend(g_rawDepthFetchSamplerState); + return g_rawDepthFetchSamplerBackend.get(); + } + + Bool NeedsRawDepthFetchSampler(const SharedPtr& samplerObject, + TextureInternalFormat textureFormat) { + if (!MG_Util::IsDepthFormatInternalFormat(textureFormat) || !samplerObject) { + return false; + } + + const auto& samplerParams = samplerObject->GetAllSamplerParameters(); + if (samplerParams.compareMode != SamplerCompareMode::None) { + return false; + } + + return samplerParams.minFilter != SamplerFilterMode::Nearest || + samplerParams.mipmapMode != SamplerMipmapMode::None || + samplerParams.magFilter != SamplerFilterMode::Nearest; + } + const Uint8* ResolveIndirectCommandBytes(const void* indirect, SizeT requiredBytes, const char* label) { auto drawBuffer = MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::DrawIndirect).GetBoundObject(); if (drawBuffer) { @@ -934,9 +966,20 @@ namespace MobileGL::MG_Backend::DirectGLES { backendProgramIt->second->GetBackendProgramId(), name.c_str()); g_GLESFuncs.glUniform1i(locAtBackend, unit); - auto& samplerObject = MG_State::pGLContext->GetTextureUnitObject(unit).GetSamplerObject(); + auto& textureUnit = MG_State::pGLContext->GetTextureUnitObject(unit); + auto& samplerObject = textureUnit.GetSamplerObject(); + const auto uniformType = currentProgram->GetUniformType(loc); + const auto& texture2D = textureUnit.GetBindingSlot(TextureTarget::Texture2D).GetBoundObject(); + const SharedPtr* rawDepthSamplerObject = &samplerObject; + if (!*rawDepthSamplerObject && texture2D) { + rawDepthSamplerObject = &texture2D->GetSamplerObject(); + } - if (samplerObject) { + if (uniformType == GL_SAMPLER_2D && texture2D && + NeedsRawDepthFetchSampler(*rawDepthSamplerObject, texture2D->GetFormat())) { + GetRawDepthFetchSampler()->Bind(unit); + MGLOG_D("Using raw depth fetch sampler for uniform %s on unit %d.", name.c_str(), unit); + } else if (samplerObject) { const auto& backendSamplerIt = SamplerImpl::g_backendSamplerObjects.find(samplerObject.get()); Bool exist = (backendSamplerIt != SamplerImpl::g_backendSamplerObjects.end());