// MobileGL - MobileGL/MG_Backend/DirectGLES/WireTextureReadback.inc // Copyright (c) 2025-2026 MobileGL-Dev // Licensed under the GNU Lesser General Public License v3.0: // https://www.gnu.org/licenses/gpl-3.0.txt // https://www.gnu.org/licenses/lgpl-3.0.txt // SPDX-License-Identifier: LGPL-3.0-only // End of Source File Header // Included in DirectGLES.cpp after the native packed-word readback helper. // A storage promotion needs the driver's own texels as owned tight rows, not // client PACK layout or a fallback into a frontend texture's potentially stale // shadow. class ScopedWireTextureReadLevel { public: ScopedWireTextureReadLevel(GLuint texture, GLenum target, GLint level) : m_texture(texture), m_target(target) { switch (target) { case GL_TEXTURE_2D: m_binding = GL_TEXTURE_BINDING_2D; break; case GL_TEXTURE_3D: m_binding = GL_TEXTURE_BINDING_3D; break; case GL_TEXTURE_2D_ARRAY: m_binding = GL_TEXTURE_BINDING_2D_ARRAY; break; case GL_TEXTURE_CUBE_MAP: m_binding = GL_TEXTURE_BINDING_CUBE_MAP; break; case GL_TEXTURE_CUBE_MAP_ARRAY: m_binding = GL_TEXTURE_BINDING_CUBE_MAP_ARRAY; break; default: return; } GLint previous = 0; DrainESErrors(); g_GLESFuncs.glGetIntegerv(m_binding, &previous); g_GLESFuncs.glBindTexture(m_target, m_texture); g_GLESFuncs.glGetTexParameteriv(m_target, GL_TEXTURE_BASE_LEVEL, &m_baseLevel); g_GLESFuncs.glGetTexParameteriv(m_target, GL_TEXTURE_MAX_LEVEL, &m_maxLevel); m_saved = g_GLESFuncs.glGetError() == GL_NO_ERROR; if (m_saved) { // ES framebuffer completeness can reject a defined level whose // base level or intervening mip chain is missing/inconsistent. // Read this existing native image as the sole base level. The // logical GL parameters and every cached copy remain unchanged. g_GLESFuncs.glTexParameteri(m_target, GL_TEXTURE_BASE_LEVEL, level); g_GLESFuncs.glTexParameteri(m_target, GL_TEXTURE_MAX_LEVEL, level); m_valid = g_GLESFuncs.glGetError() == GL_NO_ERROR; } g_GLESFuncs.glBindTexture(m_target, static_cast(previous)); } ~ScopedWireTextureReadLevel() { if (!m_saved) return; // The packed-word path can use its own texture scratch binding. // Save the binding at restoration time too; do not assume an active // texture unit or make the backend's binding cache forget a bind. GLint previous = 0; g_GLESFuncs.glGetIntegerv(m_binding, &previous); g_GLESFuncs.glBindTexture(m_target, m_texture); g_GLESFuncs.glTexParameteri(m_target, GL_TEXTURE_BASE_LEVEL, m_baseLevel); g_GLESFuncs.glTexParameteri(m_target, GL_TEXTURE_MAX_LEVEL, m_maxLevel); g_GLESFuncs.glBindTexture(m_target, static_cast(previous)); } Bool Valid() const { return m_valid; } private: GLuint m_texture; GLenum m_target; GLenum m_binding = GL_NONE; GLint m_baseLevel = 0, m_maxLevel = 0; Bool m_saved = false, m_valid = false; }; static Bool ReadWireColorRowsTight(GLsizei width, GLsizei height, GLenum format, GLenum type, Bool forceOpaqueAlpha, const NormalizedImageCarrierRead& carrier, Vector& bytes) { ReadbackChannelMapping mapping{}; if (!GetReadbackChannelMapping(format, mapping)) return false; const SizeT pixelBytes = GetReadbackDstPixelSize(mapping, type); if (!pixelBytes || width <= 0 || height <= 0) return false; const GLenum componentType = QueryReadAttachmentComponentType(); const Bool integerAttachment = componentType == GL_INT || componentType == GL_UNSIGNED_INT; if (carrier.Active() ? (mapping.isInteger || !integerAttachment) : mapping.isInteger != integerAttachment) return false; GLint implFormat = 0, implType = 0; g_GLESFuncs.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &implFormat); g_GLESFuncs.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &implType); struct Candidate { GLenum format, type; }; Vector candidates; if (carrier.Active()) { candidates.push_back({GL_RGBA_INTEGER, GL_UNSIGNED_INT}); } else if (mapping.isInteger) { if (GetWideReadChannelCount(implFormat) > 0 && IsIntegerReadFormat(implFormat) && (implType == GL_INT || implType == GL_UNSIGNED_INT)) candidates.push_back({static_cast(implFormat), static_cast(implType)}); candidates.push_back({GL_RGBA_INTEGER, componentType == GL_INT ? GLenum(GL_INT) : GLenum(GL_UNSIGNED_INT)}); } else { if (GetWideReadChannelCount(implFormat) > 0 && !IsIntegerReadFormat(implFormat) && (CanDecodeWideSourceType(implType) || (implFormat == GL_RGBA && implType == GL_UNSIGNED_INT_2_10_10_10_REV))) candidates.push_back({static_cast(implFormat), static_cast(implType)}); if (componentType == GL_FLOAT) candidates.push_back({GL_RGBA, GL_FLOAT}); if (componentType == GL_SIGNED_NORMALIZED) { candidates.push_back({GL_RGBA, GL_SHORT}); candidates.push_back({GL_RGBA, GL_BYTE}); } else candidates.push_back({GL_RGBA, GL_UNSIGNED_BYTE}); } ScopedPixelPackBuffer packBuffer(0); ScopedPackState packState(PixelStoreImpl::PackState{1, 0, 0, 0}); const SizeT pixels = static_cast(width) * static_cast(height); Vector wide; GLenum wideType = GL_NONE; Int channels = 0; DrainESErrors(); for (const auto candidate : candidates) { channels = GetWideReadChannelCount(candidate.format); wide.resize(pixels * static_cast(channels) * GetReadbackComponentSize(candidate.type)); g_GLESFuncs.glReadPixels(0, 0, width, height, candidate.format, candidate.type, wide.data()); if (g_GLESFuncs.glGetError() == GL_NO_ERROR) { wideType = candidate.type; break; } } if (wideType == GL_NONE) return false; if (carrier.Active()) { Vector decoded(pixels * 4 * sizeof(Float)); auto* dst = reinterpret_cast(decoded.data()); const auto* src = reinterpret_cast(wide.data()); for (SizeT i = 0; i < pixels; ++i) { for (SizeT c = 0; c < 4; ++c) { const Float divisor = static_cast(carrier.ChannelMax[c]); dst[i * 4 + c] = carrier.SignedNormalized ? std::max(static_cast(static_cast(static_cast(src[i * 4 + c]))) / divisor, -1.0f) : static_cast(src[i * 4 + c]) / divisor; } } wide = Move(decoded); wideType = GL_FLOAT; channels = 4; } else if (wideType == GL_UNSIGNED_INT_2_10_10_10_REV) { Vector decoded(pixels * 4 * sizeof(Float)); auto* dst = reinterpret_cast(decoded.data()); for (SizeT i = 0; i < pixels; ++i) { Uint32 word = 0; Memcpy(&word, wide.data() + i * sizeof(word), sizeof(word)); dst[i * 4] = static_cast(word & 1023u) / 1023.0f; dst[i * 4 + 1] = static_cast((word >> 10) & 1023u) / 1023.0f; dst[i * 4 + 2] = static_cast((word >> 20) & 1023u) / 1023.0f; dst[i * 4 + 3] = static_cast((word >> 30) & 3u) / 3.0f; } wide = Move(decoded); wideType = GL_FLOAT; channels = 4; } if (channels < 4) ExpandNarrowWideRead(wide, pixels, channels, wideType); if (forceOpaqueAlpha) ForceWideReadAlphaToOne(wide, pixels, wideType); Vector converted(pixels * pixelBytes); const SizeT sourceRowBytes = static_cast(width) * 4 * GetReadbackComponentSize(wideType); for (GLsizei row = 0; row < height; ++row) ReadbackImpl::ConvertWideReadbackRow(wide.data() + static_cast(row) * sourceRowBytes, converted.data() + static_cast(row) * width * pixelBytes, width, wideType, mapping, type); bytes = Move(converted); return true; } static Bool ReadWireDepthStencilLevelTight(GLuint texture, TextureTarget target, TextureUploadTarget uploadTarget, GLint level, const IntVec3& extent, GLenum format, GLenum type, Vector& bytes) { const SizeT pixelBytes = MG_Util::GetInputBytesPerPixel( MG_Util::ConvertGLEnumToTextureInputFormat(format), MG_Util::ConvertGLEnumToTexturePixelDataType(type)); if (!pixelBytes) return false; const Bool needDepth = format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL; const Bool needStencil = format == GL_STENCIL_INDEX || format == GL_DEPTH_STENCIL; ScopedFramebufferBinding bindings(true, false); GLuint fbo = 0; g_GLESFuncs.glGenFramebuffers(1, &fbo); struct DeleteFbo { GLuint id; ~DeleteFbo() { g_GLESFuncs.glDeleteFramebuffers(1, &id); } } cleanup{fbo}; FramebufferImpl::BindFramebufferId(GL_READ_FRAMEBUFFER, fbo); ScopedPixelPackBuffer packBuffer(0); ScopedPackState packState(PixelStoreImpl::PackState{1, 0, 0, 0}); const GLenum nativeTarget = TextureImpl::ConvertTextureTargetToBackendGLEnum(target); const GLenum point = needDepth ? (needStencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT) : GL_STENCIL_ATTACHMENT; const SizeT count = static_cast(extent.x()) * extent.y(); Vector result(count * extent.z() * pixelBytes); if (g_GLESFuncs.glMemoryBarrier) g_GLESFuncs.glMemoryBarrier(GL_ALL_BARRIER_BITS); g_GLESFuncs.glFinish(); for (GLint z = 0; z < extent.z(); ++z) { if (nativeTarget == GL_TEXTURE_3D || nativeTarget == GL_TEXTURE_2D_ARRAY || nativeTarget == GL_TEXTURE_CUBE_MAP_ARRAY) g_GLESFuncs.glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, point, texture, level, z); else g_GLESFuncs.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, point, TextureImpl::ConvertTextureUploadTargetToBackendGLEnum(uploadTarget), texture, level); g_GLESFuncs.glReadBuffer(GL_NONE); if (g_GLESFuncs.glCheckFramebufferStatus(GL_READ_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) return false; Vector depth; Vector stencil; if (needDepth && !ReadDepthValuesNative(0, 0, extent.x(), extent.y(), depth)) return false; if (needStencil && !ReadStencilBytesNative(0, 0, extent.x(), extent.y(), stencil)) return false; auto* dst = result.data() + count * z * pixelBytes; for (SizeT i = 0; i < count; ++i) { if (format == GL_DEPTH_STENCIL) { const Uint32 s = stencil[i]; if (type == GL_UNSIGNED_INT_24_8) { const Uint32 packed = (NormalizedDepthToUnsigned(depth[i], 16777215.0) << 8) | s; Memcpy(dst + i * pixelBytes, &packed, sizeof(packed)); } else if (type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) { Memcpy(dst + i * pixelBytes, &depth[i], sizeof(Float)); Memcpy(dst + i * pixelBytes + sizeof(Float), &s, sizeof(s)); } else return false; } else if (type == GL_FLOAT) { const Float value = needDepth ? depth[i] : static_cast(stencil[i]); Memcpy(dst + i * pixelBytes, &value, sizeof(value)); } else if (type == GL_HALF_FLOAT) { const Uint16 value = MG_Util::EncodeFloatToHalfBits( needDepth ? depth[i] : static_cast(stencil[i])); Memcpy(dst + i * pixelBytes, &value, sizeof(value)); } else { const Bool signedType = type == GL_BYTE || type == GL_SHORT || type == GL_INT; const Double maximum = signedType ? (pixelBytes == 1 ? 127.0 : pixelBytes == 2 ? 32767.0 : 2147483647.0) : (pixelBytes == 1 ? 255.0 : pixelBytes == 2 ? 65535.0 : 4294967295.0); const Uint32 value = needDepth ? NormalizedDepthToUnsigned(depth[i], maximum) : stencil[i]; if (pixelBytes == 1) dst[i] = static_cast(value); else if (pixelBytes == 2) { const Uint16 v = static_cast(value); Memcpy(dst + i * 2, &v, 2); } else if (pixelBytes == 4) Memcpy(dst + i * 4, &value, 4); else return false; } } } bytes = Move(result); return true; } Bool ReadTextureLevelTight(GLuint texture, TextureTarget target, TextureUploadTarget uploadTarget, TextureInternalFormat logicalFormat, GLint level, const IntVec3& logicalExtent, Bool sourceUsesImageCarrier, GLenum format, GLenum type, Vector& bytes) { if (!texture || level < 0 || TextureImpl::IsMultisampleTextureTarget(target)) return false; const IntVec3 extent = TextureImpl::GetBackendUploadSize(target, logicalExtent); const GLenum nativeTarget = TextureImpl::ConvertTextureTargetToBackendGLEnum(target); ScopedWireTextureReadLevel readLevel(texture, nativeTarget, level); if (!readLevel.Valid()) return false; if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) return ReadWireDepthStencilLevelTight(texture, target, uploadTarget, level, extent, format, type, bytes); ReadbackChannelMapping mapping{}; if (!GetReadbackChannelMapping(format, mapping)) return false; const SizeT pixelBytes = GetReadbackDstPixelSize(mapping, type); if (!pixelBytes || extent.x() <= 0 || extent.y() <= 0 || extent.z() <= 0) return false; const GLenum faceTarget = TextureImpl::ConvertTextureUploadTargetToBackendGLEnum(uploadTarget); const auto widening = sourceUsesImageCarrier ? TextureImpl::GetImageBindableStorageWidening(logicalFormat) : TextureImpl::ImageBindableStorageWidening{}; const SizeT sliceBytes = static_cast(extent.x()) * extent.y() * pixelBytes; Vector result(sliceBytes * static_cast(extent.z())); if (g_GLESFuncs.glMemoryBarrier) g_GLESFuncs.glMemoryBarrier(GL_ALL_BARRIER_BITS); g_GLESFuncs.glFinish(); // A raw packed word may have redundant encodings or NaN payloads. Read // it through R32UI when the source really still uses that native layout. const Bool rawPacked = !widening && MG_Util::PixelStoreProcessor::IsRawPackedPixelTransfer( logicalFormat, MG_Util::ConvertGLEnumToTextureInputFormat(format), MG_Util::ConvertGLEnumToTexturePixelDataType(type)); if (rawPacked && pixelBytes == sizeof(Uint32)) { const GLint face = uploadTarget >= TextureUploadTarget::CubeMapPositiveX && uploadTarget <= TextureUploadTarget::CubeMapNegativeZ ? static_cast(uploadTarget) - static_cast(TextureUploadTarget::CubeMapPositiveX) : 0; for (GLint layer = 0; layer < extent.z(); ++layer) if (!ReadPackedLevelWordsViaScratch(texture, nativeTarget, level, face + layer, extent.x(), extent.y(), reinterpret_cast(result.data() + static_cast(layer) * sliceBytes))) return false; bytes = Move(result); return true; } NormalizedImageCarrierRead carrier{}; if (widening && widening.CarriesNormalizedCodes()) { for (SizeT c = 0; c < 4; ++c) carrier.ChannelMax[c] = widening.ChannelMax[c]; carrier.SignedNormalized = widening.SignedNormalized; } TempFBOBinder binder(true); auto& framebuffer = binder.Framebuffer(); for (GLint layer = 0; layer < extent.z(); ++layer) { if (nativeTarget == GL_TEXTURE_3D || nativeTarget == GL_TEXTURE_2D_ARRAY || nativeTarget == GL_TEXTURE_CUBE_MAP_ARRAY) ScratchFBOImpl::EnsureColorAttachmentLayer(framebuffer, GL_READ_FRAMEBUFFER, texture, level, layer); else ScratchFBOImpl::EnsureColorAttachment2D(framebuffer, GL_READ_FRAMEBUFFER, texture, faceTarget, level); ScratchFBOImpl::EnsureReadBuffer(framebuffer, GL_COLOR_ATTACHMENT0); const GLenum status = g_GLESFuncs.glCheckFramebufferStatus(GL_READ_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) { MGLOG_E_ONCE("Espryt wire texture readback: framebuffer status=0x%x texture=%u level=%d target=0x%x", status, texture, level, nativeTarget); return false; } Vector slice; if (!ReadWireColorRowsTight(extent.x(), extent.y(), format, type, TextureImpl::BackendTextureFormatAddsAlpha(logicalFormat, target), carrier, slice) || slice.size() != sliceBytes) return false; Memcpy(result.data() + static_cast(layer) * sliceBytes, slice.data(), sliceBytes); } bytes = Move(result); return true; } // P9 W2-a. The store's Defined-ness follows the client's respecifies; the driver's follows the // twin's last sync. A re-mint asks this before reading a level back, because a level defined // after that sync (null data, or null data plus a partial upload) is not on the driver yet and // its FBO attachment is incomplete - which used to end the session at // Fatal{ResourceUnavailable, "image-promotion-readback"} on legal GL. Bool NativeTextureLevelHasExtent(GLuint texture, TextureTarget target, TextureUploadTarget uploadTarget, GLint level, const IntVec3& logicalExtent) { if (!texture || level < 0 || !g_GLESFuncs.glGetTexLevelParameteriv) return true; const IntVec3 extent = TextureImpl::GetBackendUploadSize(target, logicalExtent); const GLenum nativeTarget = TextureImpl::ConvertTextureTargetToBackendGLEnum(target); GLenum binding = GL_NONE; switch (nativeTarget) { case GL_TEXTURE_2D: binding = GL_TEXTURE_BINDING_2D; break; case GL_TEXTURE_3D: binding = GL_TEXTURE_BINDING_3D; break; case GL_TEXTURE_2D_ARRAY: binding = GL_TEXTURE_BINDING_2D_ARRAY; break; case GL_TEXTURE_CUBE_MAP: binding = GL_TEXTURE_BINDING_CUBE_MAP; break; case GL_TEXTURE_CUBE_MAP_ARRAY: binding = GL_TEXTURE_BINDING_CUBE_MAP_ARRAY; break; default: return true; } // A cube's levels are per face; every other target answers for the whole level. const GLenum queryTarget = nativeTarget == GL_TEXTURE_CUBE_MAP ? TextureImpl::ConvertTextureUploadTargetToBackendGLEnum(uploadTarget) : nativeTarget; DrainESErrors(); GLint previous = 0; g_GLESFuncs.glGetIntegerv(binding, &previous); g_GLESFuncs.glBindTexture(nativeTarget, texture); GLint width = 0, height = 0, depth = 0; g_GLESFuncs.glGetTexLevelParameteriv(queryTarget, level, GL_TEXTURE_WIDTH, &width); g_GLESFuncs.glGetTexLevelParameteriv(queryTarget, level, GL_TEXTURE_HEIGHT, &height); g_GLESFuncs.glGetTexLevelParameteriv(queryTarget, level, GL_TEXTURE_DEPTH, &depth); const Bool answered = g_GLESFuncs.glGetError() == GL_NO_ERROR; g_GLESFuncs.glBindTexture(nativeTarget, static_cast(previous)); if (!answered) return true; const Bool layered = nativeTarget == GL_TEXTURE_3D || nativeTarget == GL_TEXTURE_2D_ARRAY || nativeTarget == GL_TEXTURE_CUBE_MAP_ARRAY; return width == extent.x() && height == extent.y() && (!layered || depth == extent.z()); } // P8-E (docs/archive/notes/p8/E.md). MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT=x, READ BY THE SERVER: // a colour level of exactly that extent is treated as one the driver refused to attach - what an // Adreno does for RGB16 and the 16-bit SNORM formats, and what no host driver can be made to do. // Scoped by the extent so the tcp lane's shared supervisor can carry it for the whole lane: only // the scenario that builds that extent (CopyImageStoreReadbackScenario) ever meets it. static Bool WireTextureReadbackRefusedForTesting(const IntVec3& extent) { static const IntVec3 s_refused = [] { const char* value = std::getenv("MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT"); if (value == nullptr) return IntVec3{0, 0, 0}; char* end = nullptr; const long width = std::strtol(value, &end, 10); if (end == value || *end != 'x') return IntVec3{0, 0, 0}; const long height = std::strtol(end + 1, nullptr, 10); if (width <= 0 || height <= 0) return IntVec3{0, 0, 0}; return IntVec3{static_cast(width), static_cast(height), 1}; }(); return s_refused.x() > 0 && extent == s_refused; } // P8-E: THE STORE ROUTE, for a level the driver refused to read back. The server's staged store // is the only CPU copy of the level on this side, and it answers only while its bytes ARE the // level: every byte crossed (IsCovered), at the extent asked for, and the texture was never // written by the driver except by copies the store followed (!IsDriverWritten - see // StagedTextureStore's P8-E block and FollowCopyImageInStagedStore). The conversion is // GetTexImageViaShadowConversion's, into tight rows: a packed format read with its own type // takes the stored words verbatim, everything else decodes to wide RGBA and converts row by row. // Anything else DECLINES BY NAME: the reply is an error, and the client's // Fatal{ReplyError, "ReadPixels"} ends the session instead of handing back bytes the driver has // since overwritten. static Bool ReadWireLevelFromStagedStore(MG_Pipe::MGPipeHandle res, TextureInternalFormat logicalFormat, TextureUploadTarget uploadTarget, GLint level, const IntVec3& extent, GLenum format, GLenum type, Vector& bytes) { auto& store = MG_Record::ServerStagedTexture(); const Uint64 key = MG_Record::StagedTextureStore::KeyForHandle(res); const auto target = static_cast(uploadTarget); const auto mip = static_cast(level); ReadbackChannelMapping mapping{}; const SizeT pixelBytes = GetReadbackChannelMapping(format, mapping) ? GetReadbackDstPixelSize(mapping, type) : 0; const SizeT texels = static_cast(std::max(extent.x(), 0)) * static_cast(std::max(extent.y(), 0)) * static_cast(std::max(extent.z(), 0)); const char* declined = nullptr; if (!store.Holds()) { declined = "this arm keeps no staged store"; } else if (store.IsDriverWritten(key)) { declined = "the driver has written the texture since its bytes crossed"; } else if (!store.IsCovered(key, target, mip)) { declined = "not every byte of the level crossed"; } else if (store.LevelExtentOrUndefined(key, target, mip) != extent) { declined = "the staged level is not the extent asked for"; } else if (pixelBytes == 0 || texels == 0) { declined = "the format/type pair has no readback mapping"; } Vector out; if (declined == nullptr) { const Uint8* staged = store.RequireLevelBytes(key, target, mip, "texture-readback-from-store"); if (MG_Util::PixelStoreProcessor::IsRawPackedPixelTransfer( logicalFormat, MG_Util::ConvertGLEnumToTextureInputFormat(format), MG_Util::ConvertGLEnumToTexturePixelDataType(type))) { if (store.LevelByteSize(key, target, mip) != texels * pixelBytes) { declined = "the staged words are not the client's word size"; } else { out.assign(staged, staged + texels * pixelBytes); } } else { Vector wide; Bool isInteger = false; Bool isSigned = false; if (!MG_Util::PixelStoreProcessor::DecodeShadowDataToWideRGBA(logicalFormat, staged, texels, wide, isInteger, isSigned)) { declined = "the staged format has no wide decoding"; } else if (mapping.isInteger != isInteger) { declined = "an integer read of a normalized level, or the reverse"; } else { const GLenum wideType = isInteger ? (isSigned ? GL_INT : GL_UNSIGNED_INT) : GL_FLOAT; const SizeT rowTexels = static_cast(extent.x()); const SizeT rows = texels / rowTexels; const SizeT wideRowBytes = rowTexels * 4 * GetReadbackComponentSize(wideType); out.resize(texels * pixelBytes); for (SizeT row = 0; row < rows; ++row) { ReadbackImpl::ConvertWideReadbackRow(wide.data() + row * wideRowBytes, out.data() + row * rowTexels * pixelBytes, rowTexels, wideType, mapping, type); } } } } if (declined != nullptr) { MGLOG_E_ONCE("MGPipe: texture {%u,%u} level %d: the driver refused the readback and the staged store " "cannot answer it (%s) - DECLINED (texture-readback-store-declined)", res.Slot, res.Gen, level, declined); return false; } MGLOG_W_ONCE("MGPipe: texture {%u,%u} level %d: the driver refused the readback (internal format %u); " "answered from the server's staged store (texture-readback-from-store)", res.Slot, res.Gen, level, static_cast(logicalFormat)); bytes = Move(out); return true; } Bool ReadTextureImageWire(const MG_Pipe::MGPReadbackInfo& info, Vector& bytes) { const auto* record = PipeTextureRecordForHandle(info.Res); if (!record) return false; TextureTarget target = TextureTarget::Unknown; for (Int i = 0; i < static_cast(TextureTarget::TextureTargetCount); ++i) if (MG_Pipe::MGPipeResourceTargetForTextureTarget(static_cast(i)) == record->Desc.Target) target = static_cast(i); if (target == TextureTarget::Unknown) return false; const auto twin = TextureImpl::SyncTextureToBackendByHandle(info.Res); if (!twin) return false; const Bool wholeCube = info.Target == GL_TEXTURE_CUBE_MAP; const IntVec3 extent{static_cast(info.Box.W), static_cast(info.Box.H), wholeCube ? 1 : static_cast(info.Box.D)}; const auto logicalFormat = static_cast(record->Desc.InternalFormat); // The R32UI word leg never goes through a framebuffer, so the knob leaves it alone (it is // ReadTextureLevelTight's first choice for exactly these reads). const Bool packedWordRead = !twin->RequiresImageBindableStorage() && MG_Util::PixelStoreProcessor::IsRawPackedPixelTransfer( logicalFormat, MG_Util::ConvertGLEnumToTextureInputFormat(info.Format), MG_Util::ConvertGLEnumToTexturePixelDataType(info.Type)) && MG_Util::GetInputBytesPerPixel(MG_Util::ConvertGLEnumToTextureInputFormat(info.Format), MG_Util::ConvertGLEnumToTexturePixelDataType(info.Type)) == sizeof(Uint32); const Bool refusedForTesting = !packedWordRead && WireTextureReadbackRefusedForTesting(extent); Vector result; for (Int face = 0; face < (wholeCube ? 6 : 1); ++face) { const auto uploadTarget = wholeCube ? static_cast(static_cast(TextureUploadTarget::CubeMapPositiveX) + face) : MG_Util::ConvertGLEnumToTextureUploadTarget(info.Target); Vector slice; if (refusedForTesting || !ReadTextureLevelTight(twin->GetBackendTextureId(), target, uploadTarget, logicalFormat, info.Level, extent, twin->RequiresImageBindableStorage(), info.Format, info.Type, slice)) { if (!ReadWireLevelFromStagedStore(info.Res, logicalFormat, uploadTarget, info.Level, extent, info.Format, info.Type, slice)) return false; } result.insert(result.end(), slice.begin(), slice.end()); } bytes = Move(result); return true; }