mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-26 19:22:16 +09:00
[Perf] (MG_Backend/DirectGLES): batch sparse texture uploads on Mali
- Pack dirty rectangles across mip levels into a shared staging texture. - Keep a bounded fallback when mapped unpack space is busy. - https://bugzilla.mozilla.org/show_bug.cgi?id=1661528 - https://searchfox.org/firefox-main/source/gfx/wr/webrender/src/renderer/upload.rs#474
This commit is contained in:
1 parent
9df3600f87
commit
10deb9cea4
6 files changed
+489
-43
No files matched your search
@@ -1878,15 +1878,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
Uint64 m_sharedImageId = 0;
|
||||
SharedImageImpl::EglImageRef m_sharedImage;
|
||||
#endif
|
||||
// Whether the driver texture may hold texels the shadow never saw. Set the first time
|
||||
// anything other than a texel upload can write it - an application framebuffer
|
||||
// attachment, a writable image binding, a copy or blit into it, a driver-side mipmap
|
||||
// generation - and never cleared. Until then the driver holds, outside a level's
|
||||
// pending writes, exactly the shadow bytes it was last sent, so those writes may go up
|
||||
// as their union box (one unpack-ring job, which is what Mali needs); from then on
|
||||
// only the texels the client wrote may go up, since the shadow is stale everywhere
|
||||
// else. See SyncMipmapsToBackend.
|
||||
// Once a framebuffer, image binding, copy, or mipmap generation may write this texture,
|
||||
// the CPU shadow can be stale. Upload only the texels the client wrote from then on.
|
||||
Bool m_driverMayHoldGpuWrites = false;
|
||||
// Retry the packed path when the texture's storage changes.
|
||||
Bool m_packedUploadRejected = false;
|
||||
// Latches the "this driver has no buffer textures" report to once per texture. The
|
||||
// report is emitted from the respecify path, which bails before recording the state
|
||||
// it was asked to apply - so without the latch the texture stays permanently dirty
|
||||
|
||||
@@ -2741,16 +2741,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Bump-allocate `size` bytes out of `ring`.
|
||||
//
|
||||
// Fast path: a live store under the current context with room before both
|
||||
// the wrap boundary and the in-flight tail. Touches no GL and probes no
|
||||
// frame marks - callers sit behind the ring's availability gate, so the
|
||||
// context checks have already run for this draw/upload. `tail` may be stale
|
||||
// here (marks are only retired on Present and on the slow path); staleness
|
||||
// is conservative - the in-flight span reads too large, the check fails,
|
||||
// and the slow path retires marks and re-tries.
|
||||
Bool RingAllocate(PersistentRing& ring, SizeT size, SizeT& outOffset) {
|
||||
// Allocate within the mapped ring without GL calls or polling fences. A stale tail
|
||||
// accounts for extra bytes in flight, so it can only make this check more conservative.
|
||||
static Bool TryAllocateMappedRing(PersistentRing& ring, SizeT size, SizeT& outOffset) {
|
||||
if (size == 0) return false;
|
||||
auto& store = ring.store;
|
||||
if (store.id != 0 && store.contextGeneration == g_bufferContextGeneration) {
|
||||
@@ -2770,7 +2763,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return RingAllocateSlow(ring, size, outOffset);
|
||||
return false;
|
||||
}
|
||||
|
||||
Bool RingAllocate(PersistentRing& ring, SizeT size, SizeT& outOffset) {
|
||||
return TryAllocateMappedRing(ring, size, outOffset) ||
|
||||
(size != 0 && RingAllocateSlow(ring, size, outOffset));
|
||||
}
|
||||
|
||||
Bool RingAllocateSlow(PersistentRing& ring, SizeT size, SizeT& outOffset) {
|
||||
@@ -2969,6 +2967,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
return RingAllocate(UnpackRing(), size, outOffset);
|
||||
}
|
||||
|
||||
Bool TryAllocateMappedUnpackRing(SizeT size, SizeT& outOffset) {
|
||||
return size <= kUnpackRingMaxBytes && TryAllocateMappedRing(UnpackRing(), size, outOffset);
|
||||
}
|
||||
|
||||
void* UnpackRingMappedPtr() {
|
||||
return UnpackRing().store.mappedPtr;
|
||||
}
|
||||
|
||||
@@ -382,6 +382,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
|
||||
Bool UnpackRingAllocate(SizeT size, SizeT& outOffset);
|
||||
|
||||
// Only use space in the current mapping. Optional uploads can fall back
|
||||
// immediately when the ring would have to grow or wait.
|
||||
Bool TryAllocateMappedUnpackRing(SizeT size, SizeT& outOffset);
|
||||
|
||||
void* UnpackRingMappedPtr();
|
||||
|
||||
Uint UnpackRingBufferId();
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
// End of Source File Header
|
||||
|
||||
#include "ManagersInternal.h"
|
||||
#include "DirectGLESInternal.h"
|
||||
#include "PackedTextureUpload.h"
|
||||
#include "TextureUploadBoxes.h"
|
||||
|
||||
namespace MobileGL::MG_Backend::DirectGLES {
|
||||
namespace TextureImpl {
|
||||
@@ -97,6 +100,148 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
return reinterpret_cast<const void*>(static_cast<std::uintptr_t>(offset));
|
||||
}
|
||||
|
||||
namespace {
|
||||
static Bool SplitMipUpload(const MG_Pipe::MGPipeResourceRecord::PendingUpload& pending,
|
||||
const IntVec3& levelSize, MG_State::GLState::MipmapDirtyRegion* boxes) {
|
||||
using namespace TextureUploadBoxes;
|
||||
const auto& bounds = pending.UnionBox;
|
||||
if (bounds.X < 0 || bounds.Y < 0 || bounds.Z != 0 || bounds.D != 1 || bounds.X >= levelSize.x() ||
|
||||
bounds.Y >= levelSize.y() || bounds.W > static_cast<Uint>(levelSize.x() - bounds.X) ||
|
||||
bounds.H > static_cast<Uint>(levelSize.y() - bounds.Y)) {
|
||||
return false;
|
||||
}
|
||||
const Box unionBox{bounds.X, bounds.Y, bounds.X + static_cast<Int>(bounds.W),
|
||||
bounds.Y + static_cast<Int>(bounds.H)};
|
||||
if (!WorthSearching(unionBox, pending.Regions.size())) return false;
|
||||
std::array<Box, MaxRegions> regions;
|
||||
for (SizeT i = 0; i < pending.Regions.size(); ++i) {
|
||||
const auto& region = pending.Regions[i];
|
||||
if (region.X < unionBox.left || region.X >= unionBox.right || region.Y < unionBox.top ||
|
||||
region.Y >= unionBox.bottom || region.Z != 0 || region.D != 1 || region.W == 0 ||
|
||||
region.H == 0 || region.W > static_cast<Uint>(unionBox.right - region.X) ||
|
||||
region.H > static_cast<Uint>(unionBox.bottom - region.Y)) {
|
||||
return false;
|
||||
}
|
||||
regions[i] = {region.X, region.Y, region.X + static_cast<Int>(region.W),
|
||||
region.Y + static_cast<Int>(region.H)};
|
||||
}
|
||||
const auto split = Split(unionBox, regions, pending.Regions.size());
|
||||
if (!split) return false;
|
||||
for (SizeT i = 0; i < split->size(); ++i) {
|
||||
const auto& box = (*split)[i];
|
||||
boxes[i] = {IntVec3{box.left, box.top, 0}, IntVec3{box.right, box.bottom, 1}};
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct PackedUploadPage {
|
||||
GLuint texture = 0;
|
||||
Uint generation = 0;
|
||||
Uint64 context = 0;
|
||||
Uint64 retireSerial = 0;
|
||||
};
|
||||
|
||||
// The names live in the share group, while reuse is restricted to the
|
||||
// EGL context that submitted the copies. Any retired context's page
|
||||
// can be deleted the next time a shared context acquires its slot.
|
||||
struct PackedUploadPages final : PerNativeContextBase {
|
||||
std::mutex mutex;
|
||||
std::array<PackedUploadPage, 4> pages;
|
||||
|
||||
void ForgetContext(Uint64 context) override {
|
||||
const std::lock_guard<std::mutex> lock(mutex);
|
||||
for (auto& page : pages) {
|
||||
if (page.context == context) page.context = 0;
|
||||
}
|
||||
}
|
||||
|
||||
PackedUploadPage* FindIdle(Uint64 context) {
|
||||
const Uint64 completed = CompletedFrameSerial();
|
||||
PackedUploadPage* vacant = nullptr;
|
||||
for (auto& page : pages) {
|
||||
if (page.generation != g_backendContextGeneration) page = {};
|
||||
if (page.context == context && page.retireSerial <= completed) return &page;
|
||||
if (page.context == 0 && vacant == nullptr) vacant = &page;
|
||||
}
|
||||
return vacant;
|
||||
}
|
||||
};
|
||||
|
||||
enum class PackedUploadResult {
|
||||
Unavailable,
|
||||
Rejected,
|
||||
Committed
|
||||
};
|
||||
|
||||
static PackedUploadResult SubmitPackedUpload(const PackedTextureUpload::Batch& batch, GLuint destination,
|
||||
Uint64 unionTexels) {
|
||||
const Uint64 context = CurrentNativeContextSerial();
|
||||
if (context == 0) return PackedUploadResult::Unavailable;
|
||||
static PackedUploadPages pool;
|
||||
const std::unique_lock<std::mutex> lock(pool.mutex, std::try_to_lock);
|
||||
if (!lock.owns_lock()) return PackedUploadResult::Unavailable;
|
||||
auto* page = pool.FindIdle(context);
|
||||
if (page == nullptr) return PackedUploadResult::Unavailable;
|
||||
|
||||
SizeT offset = 0;
|
||||
if (!BufferImpl::TryAllocateMappedUnpackRing(batch.ByteSize(), offset)) {
|
||||
return PackedUploadResult::Unavailable;
|
||||
}
|
||||
auto* mapping = static_cast<Uint8*>(BufferImpl::UnpackRingMappedPtr());
|
||||
PackedTextureUpload::WritePage(batch, mapping + offset);
|
||||
|
||||
DrainDriverErrors("packed texture upload");
|
||||
if (page->context != context) {
|
||||
if (page->texture != 0) g_GLESFuncs.glDeleteTextures(1, &page->texture);
|
||||
*page = {0, g_backendContextGeneration, context, 0};
|
||||
g_GLESFuncs.glGenTextures(1, &page->texture);
|
||||
if (page->texture == 0) {
|
||||
*page = {};
|
||||
return PackedUploadResult::Rejected;
|
||||
}
|
||||
g_GLESFuncs.glBindTexture(GL_TEXTURE_2D, page->texture);
|
||||
g_GLESFuncs.glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, PackedTextureUpload::PageSide,
|
||||
PackedTextureUpload::PageSide);
|
||||
g_GLESFuncs.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
g_GLESFuncs.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
if (g_GLESFuncs.glGetError() != GL_NO_ERROR) {
|
||||
g_GLESFuncs.glBindTexture(GL_TEXTURE_2D, destination);
|
||||
g_GLESFuncs.glDeleteTextures(1, &page->texture);
|
||||
*page = {};
|
||||
return PackedUploadResult::Rejected;
|
||||
}
|
||||
} else {
|
||||
g_GLESFuncs.glBindTexture(GL_TEXTURE_2D, page->texture);
|
||||
}
|
||||
|
||||
// SyncMipmapsToBackend has the destination bound on the temp unit.
|
||||
// Restore that raw binding before returning to the cached GL state.
|
||||
BufferImpl::BindPixelUnpackBufferId(BufferImpl::UnpackRingBufferId());
|
||||
g_GLESFuncs.glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, batch.width, batch.height, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, UnpackRingPixelOffset(offset));
|
||||
BufferImpl::BindPixelUnpackBufferId(0);
|
||||
g_GLESFuncs.glBindTexture(GL_TEXTURE_2D, destination);
|
||||
page->retireSerial = CurrentFrameSerial() + 1;
|
||||
if (g_GLESFuncs.glGetError() != GL_NO_ERROR) return PackedUploadResult::Rejected;
|
||||
if (MG_Util::PipeStats::Enabled()) {
|
||||
MG_Util::PipeStats::AddBytes(MG_Util::PipeStats::ByteClass::StageTexture, batch.ByteSize());
|
||||
}
|
||||
|
||||
for (SizeT i = 0; i < batch.count; ++i) {
|
||||
const auto& region = batch.regions[i];
|
||||
const auto& source = batch.offsets[i];
|
||||
g_GLESFuncs.glCopyImageSubData(page->texture, GL_TEXTURE_2D, 0, source.x, source.y, 0, destination,
|
||||
GL_TEXTURE_2D, region.level, region.x, region.y, 0, region.width,
|
||||
region.height, 1);
|
||||
}
|
||||
if (g_GLESFuncs.glGetError() != GL_NO_ERROR) return PackedUploadResult::Rejected;
|
||||
MGLOG_I_ONCE("Packed texture upload: %zu regions, %zu packed bytes, %llu bounding-box bytes",
|
||||
batch.count, batch.ByteSize(),
|
||||
static_cast<unsigned long long>(unionTexels * PackedTextureUpload::BytesPerTexel));
|
||||
return PackedUploadResult::Committed;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
static Uint GetNormFallbackComponentCount(TextureInternalFormat format) {
|
||||
switch (format) {
|
||||
case TextureInternalFormat::R8Snorm:
|
||||
@@ -755,6 +900,67 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
(pushedStorage != nullptr ? static_cast<TextureStorageType>(pushedStorage->Desc.StorageKind) \
|
||||
: (obj)->GetStorageType())
|
||||
|
||||
static PackedUploadResult TryPackedMipUpload(MG_State::GLState::TextureObjectMipmap* textureMipmapObject,
|
||||
const MG_Pipe::MGPipeResourceRecord* pushedStorage,
|
||||
MG_Pipe::MGPipeHandle pushedRes, TextureUploadTarget uploadTarget,
|
||||
SizeT mipmapCount, GLuint destination) {
|
||||
using namespace PackedTextureUpload;
|
||||
Batch batch;
|
||||
Uint64 unionTexels = 0;
|
||||
const auto targetCode = static_cast<Uint16>(uploadTarget);
|
||||
for (SizeT level = 0; level < mipmapCount; ++level) {
|
||||
const auto levelCode = static_cast<Uint16>(level);
|
||||
const auto* pending = FindPipeTextureUpload(*pushedStorage, targetCode, levelCode);
|
||||
if (pending == nullptr) continue;
|
||||
if (pending->Regions.empty() || pending->Regions.size() > MaxRegions - batch.count) {
|
||||
return PackedUploadResult::Unavailable;
|
||||
}
|
||||
const auto size = MGB_LEVEL_TEXEL_SIZE(textureMipmapObject, uploadTarget, level);
|
||||
if (size.x() <= 0 || size.y() <= 0 || size.z() > 1 ||
|
||||
MGB_LEVEL_BYTE_SIZE(textureMipmapObject, uploadTarget, level) !=
|
||||
static_cast<Uint64>(size.x()) * size.y() * BytesPerTexel) {
|
||||
return PackedUploadResult::Unavailable;
|
||||
}
|
||||
if (MGB_STAGED_TEXTURE_LIVE &&
|
||||
!MG_Record::ServerStagedTexture().IsCovered(MG_Record::StagedTextureStore::KeyForHandle(pushedRes),
|
||||
targetCode, levelCode)) {
|
||||
return PackedUploadResult::Unavailable;
|
||||
}
|
||||
const auto* pixels = static_cast<const Uint8*>(
|
||||
MGB_LEVEL_TEXELS(textureMipmapObject, uploadTarget, level, "packed-dirty-level"));
|
||||
if (pixels == nullptr) return PackedUploadResult::Unavailable;
|
||||
const SizeT pitch = static_cast<SizeT>(size.x()) * BytesPerTexel;
|
||||
for (const auto& region : pending->Regions) {
|
||||
if (region.Z != 0 || region.D != 1 || region.W == 0 || region.H == 0 || region.W > PageSide ||
|
||||
region.H > PageSide || region.X < 0 || region.Y < 0 || region.X >= size.x() ||
|
||||
region.Y >= size.y() || region.W > static_cast<Uint>(size.x() - region.X) ||
|
||||
region.H > static_cast<Uint>(size.y() - region.Y)) {
|
||||
return PackedUploadResult::Unavailable;
|
||||
}
|
||||
// Read the final shadow using level coordinates. Carried offsets can address smaller slabs.
|
||||
batch.regions[batch.count++] = {pixels + region.Y * pitch + region.X * BytesPerTexel,
|
||||
pitch,
|
||||
static_cast<int>(level),
|
||||
region.X,
|
||||
region.Y,
|
||||
static_cast<int>(region.W),
|
||||
static_cast<int>(region.H)};
|
||||
}
|
||||
unionTexels += static_cast<Uint64>(pending->UnionBox.W) * pending->UnionBox.H;
|
||||
}
|
||||
|
||||
if (!Pack(batch, unionTexels)) return PackedUploadResult::Unavailable;
|
||||
const auto result = SubmitPackedUpload(batch, destination, unionTexels);
|
||||
if (result == PackedUploadResult::Committed) {
|
||||
for (SizeT level = 0; level < mipmapCount; ++level) {
|
||||
if (FindPipeTextureUpload(*pushedStorage, targetCode, static_cast<Uint16>(level)) != nullptr) {
|
||||
MGB_LEVEL_UPLOAD_DONE(textureMipmapObject, uploadTarget, level);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void BackendTextureObject::SyncMipmapsToBackend(
|
||||
const SharedPtr<MG_State::GLState::ITextureObject>& stateTextureObject) {
|
||||
// P5e (tx2): null IS the by-handle arm once a handle has been noted on this twin;
|
||||
@@ -1038,7 +1244,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
const auto mipmapCount = MGB_STORAGE_LEVELS(textureMipmapObject);
|
||||
currentTextureInfo.mipmapLevels = mipmapCount;
|
||||
|
||||
Bool needsRegeneration = !m_isInitialized || (currentTextureInfo != m_prevTextureInfo);
|
||||
const Bool stableStorage = m_isInitialized && currentTextureInfo == m_prevTextureInfo;
|
||||
Bool needsRegeneration = !stableStorage;
|
||||
if (needsRegeneration) m_packedUploadRejected = false;
|
||||
if (needsRegeneration && m_backendStorageImmutable) {
|
||||
RecreateBackendTexture();
|
||||
Bind(target);
|
||||
@@ -1439,6 +1647,24 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
ApplyImageBindableStorageWidening(imageWidening, &glInternalFormat, &glFormat, &glType);
|
||||
const auto& uploadTargets = MGB_UPLOAD_TARGETS(textureMipmapObject);
|
||||
ScopedDefaultUnpackState unpackState;
|
||||
// Pack the small writes across all dirty mip levels before their
|
||||
// per-level union boxes can pull in untouched atlas texels.
|
||||
if (stableStorage && !m_driverMayHoldGpuWrites && !m_packedUploadRejected &&
|
||||
!m_imageBindableStorageRequired && target == GL_TEXTURE_2D &&
|
||||
(glInternalFormat == GL_RGBA8 || glInternalFormat == GL_RGBA) && glFormat == GL_RGBA &&
|
||||
glType == GL_UNSIGNED_BYTE && pushedStorage != nullptr && uploadTargets.size() == 1 &&
|
||||
g_GLESFuncs.glCopyImageSubData && g_GLESFuncs.glTexStorage2D &&
|
||||
g_GLESCapabilities.GLESRendererString.find("Mali") != String::npos &&
|
||||
BufferImpl::UnpackRingAvailable()) {
|
||||
const auto result = TryPackedMipUpload(textureMipmapObject, pushedStorage, pushedRes,
|
||||
uploadTargets.front(), mipmapCount, m_backendTextureId);
|
||||
if (result == PackedUploadResult::Rejected) {
|
||||
// Replay all regions from the shadow after a rejected copy.
|
||||
DrainDriverErrors("packed texture upload fallback");
|
||||
m_packedUploadRejected = true;
|
||||
MGLOG_D("Packed texture upload rejected for texture %u", m_backendTextureId);
|
||||
}
|
||||
}
|
||||
// Per-level scratch of the exact-footprint path, hoisted to reuse capacity.
|
||||
Vector<MG_State::GLState::MipmapDirtyRegion> dirtyFootprint;
|
||||
Vector<UnpackStagingBlock> stagingBlockOverflow;
|
||||
@@ -1707,7 +1933,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
MG_State::GLState::MipmapDirtyRegion
|
||||
dirtyRects[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
|
||||
SizeT dirtyRectCount = 0;
|
||||
if (subRectEligible) {
|
||||
const Bool ringUsable = BufferImpl::UnpackRingAvailable();
|
||||
if (subRectEligible && !ringUsable) {
|
||||
if (pendingUpload != nullptr) {
|
||||
// RegionCount == 0 is LEGAL and means "the union box is the
|
||||
// whole story" (D-D3), which is the same statement the
|
||||
@@ -1751,16 +1978,23 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
dirtyRectCount = textureMipmapObject->GetStorageDirtyRects(
|
||||
uploadTarget, level, dirtyRects,
|
||||
MG_State::GLState::MipmapStorage::kMaxDirtyRects);
|
||||
// The scatter refinement pays only on the client-pointer path,
|
||||
// where fewer bytes mean less driver-side copying. Through the
|
||||
// unpack ring every glTexSubImage is a GPU copy job (Mali), so
|
||||
// ~100 sprite rects become ~100 jobs whose fixed cost dwarfs
|
||||
// the union box's extra bytes - measured +6 ms/frame of GPU
|
||||
// time in MC's animated-atlas ticks. One box, one job.
|
||||
if (BufferImpl::UnpackRingAvailable()) {
|
||||
dirtyRectCount = 0;
|
||||
}
|
||||
}
|
||||
// Bound the unpack-ring fallback to two jobs per level. The
|
||||
// boxes may include gaps, so require an authoritative CPU shadow.
|
||||
const Bool splitBoxUpload =
|
||||
ringUsable && regionsAddressable && stableStorage && !m_driverMayHoldGpuWrites &&
|
||||
!m_imageBindableStorageRequired && target == GL_TEXTURE_2D &&
|
||||
(glInternalFormat == GL_RGBA8 || glInternalFormat == GL_RGBA) && glFormat == GL_RGBA &&
|
||||
glType == GL_UNSIGNED_BYTE && bpp == 4 && texelSize.z() == 1 && mipData != nullptr &&
|
||||
uploadData == mipData && levelRowBytes == static_cast<SizeT>(texelSize.x()) * bpp &&
|
||||
levelSliceBytes == static_cast<SizeT>(texelSize.y()) * levelRowBytes &&
|
||||
pendingUpload != nullptr &&
|
||||
(!MGB_STAGED_TEXTURE_LIVE ||
|
||||
MG_Record::ServerStagedTexture().IsCovered(
|
||||
MG_Record::StagedTextureStore::KeyForHandle(pushedRes),
|
||||
static_cast<Uint16>(uploadTarget), static_cast<Uint16>(level))) &&
|
||||
SplitMipUpload(*pendingUpload, texelSize, dirtyRects);
|
||||
if (splitBoxUpload) dirtyRectCount = 2;
|
||||
// Everything above - the union box, the ring's one-box rule, the rect
|
||||
// list with its merges, fold and 3/4 cut-off - may upload shadow texels
|
||||
// the client did not write, which is only harmless while the driver
|
||||
@@ -1816,19 +2050,15 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
// is the level shadow itself and never a conversion buffer: since dev's 8966d5c2 a converted
|
||||
// level is sub-rect eligible too, and its rects are cut at the conversion's bpp (rectUploadPtr).
|
||||
//
|
||||
// THREE USE SITES, AND THE FIRST OF THEM IS UNREACHABLE (N-5's second half, and the correction
|
||||
// to what esprytobj-v2.md m-1 claimed): dirtyRectCount is forced to 0 whenever
|
||||
// UnpackRingAvailable(), and the staging-block loop that takes MGB_RECT_SRC_PTR(r, rect) first
|
||||
// requires `ringUsable && dirtyRectCount >= 2`. The two conditions cannot both hold. That shape
|
||||
// is pre-existing (the ring/scatter decision, not this package's), so the loop is left as it
|
||||
// stands and the count is corrected here instead of the loop being deleted underneath it.
|
||||
// A pair of bounding boxes need not start at the corresponding recorded region's origin.
|
||||
// Read those boxes by coordinate, including when ring staging falls back to a client pointer.
|
||||
//
|
||||
// A MACRO AND NOT A SECOND LAMBDA, for DV-9's measured reason: this function's nine ErrorLopper
|
||||
// lambdas are mangled ...::$_N BY POSITION, so one more renumbers every one of them and moves
|
||||
// the PULL build's symbol set, whose admitted-change set is EMPTY. The pull expansion is the
|
||||
// pre-P4a call with one pair of parentheses. #undef'd with the rest.
|
||||
#define MGB_RECT_SRC_PTR(idx, rect) \
|
||||
((pendingUpload != nullptr && pendingOffsetsAgree && carriedLayoutApplies && \
|
||||
((!splitBoxUpload && pendingUpload != nullptr && pendingOffsetsAgree && carriedLayoutApplies && \
|
||||
static_cast<SizeT>(idx) < pendingUpload->Regions.size()) \
|
||||
? static_cast<const Uint8*>(uploadData) + \
|
||||
static_cast<SizeT>(pendingUpload->Regions[static_cast<SizeT>(idx)].SrcOffset) \
|
||||
@@ -1864,7 +2094,6 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
UnpackStagingBlock stagingBlockArray[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
|
||||
UnpackStagingBlock* stagingBlocks = stagingBlockArray;
|
||||
SizeT stagingBlockCount = 0;
|
||||
const Bool ringUsable = BufferImpl::UnpackRingAvailable();
|
||||
if (!ringUsable) {
|
||||
// Nothing to plan: every branch below keeps the client
|
||||
// pointer and its ROW_LENGTH striding, unchanged.
|
||||
@@ -1925,11 +2154,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
// separately from the bytes on purpose: SSIM is blind to it
|
||||
// and the +6 ms/frame Mali cliff was a shape regression, not
|
||||
// a byte regression (plan section 7.3).
|
||||
const Bool rectShape = subRectEligible && dirtyRectCount >= 2;
|
||||
const Bool rectShape = perRectUpload;
|
||||
Uint64 shippedBytes = 0;
|
||||
if (rectShape) {
|
||||
for (SizeT r = 0; r < dirtyRectCount; ++r) {
|
||||
const auto& rect = dirtyRects[r];
|
||||
for (SizeT r = 0; r < uploadRectCount; ++r) {
|
||||
const auto& rect = uploadRects[r];
|
||||
shippedBytes += static_cast<Uint64>(rect.hi.x() - rect.lo.x()) *
|
||||
static_cast<Uint64>(rect.hi.y() - rect.lo.y()) *
|
||||
static_cast<Uint64>(std::max(rect.hi.z() - rect.lo.z(), 1)) *
|
||||
@@ -1940,7 +2169,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
static_cast<Uint64>(regionSize.x()) * static_cast<Uint64>(regionSize.y()) *
|
||||
static_cast<Uint64>(std::max(regionSize.z(), 1)) * static_cast<Uint64>(bpp);
|
||||
} else {
|
||||
shippedBytes = static_cast<Uint64>(byteSize);
|
||||
shippedBytes = static_cast<Uint64>(uploadBytes == 0 ? byteSize : uploadBytes);
|
||||
}
|
||||
MG_Util::PipeStats::AddBytes(MG_Util::PipeStats::ByteClass::StageTexture, shippedBytes);
|
||||
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::TextureUploadEmissions, 1);
|
||||
@@ -1949,7 +2178,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
: MG_Util::PipeStats::CallClass::TextureUploadBoxEmissions,
|
||||
1);
|
||||
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::TextureUploadJobs,
|
||||
rectShape ? static_cast<Uint64>(dirtyRectCount) : 1u);
|
||||
rectShape ? static_cast<Uint64>(uploadRectCount) : 1u);
|
||||
}
|
||||
switch (MapToBackendTextureTarget(MGB_TEXTURE_TARGET(stateTextureObject))) {
|
||||
case TextureTarget::Texture2D:
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
// MobileGL - MobileGL/MG_Backend/DirectGLES/PackedTextureUpload.h
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
namespace MobileGL::MG_Backend::DirectGLES::TextureImpl::PackedTextureUpload {
|
||||
// A small RGBA8 page bounds both temporary storage and the packing search.
|
||||
constexpr int PageSide = 128;
|
||||
constexpr std::size_t MaxRegions = 32;
|
||||
constexpr std::size_t BytesPerTexel = 4;
|
||||
|
||||
struct Region {
|
||||
const std::uint8_t* source;
|
||||
std::size_t sourcePitch;
|
||||
int level, x, y, width, height;
|
||||
};
|
||||
|
||||
struct Point {
|
||||
int x, y;
|
||||
};
|
||||
|
||||
struct Batch {
|
||||
std::array<Region, MaxRegions> regions;
|
||||
std::array<Point, MaxRegions> offsets;
|
||||
std::size_t count = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
std::size_t ByteSize() const { return BytesPerTexel * width * height; }
|
||||
};
|
||||
|
||||
namespace Detail {
|
||||
struct Rect {
|
||||
int x, y, width, height;
|
||||
int Area() const { return width * height; }
|
||||
};
|
||||
|
||||
// Best-area-fit guillotine packing. Splitting on the shorter remainder
|
||||
// leaves the larger free rectangle available for the next sprite.
|
||||
inline bool TryWidth(const Batch& batch, int width, Batch& result) {
|
||||
std::array<Rect, MaxRegions + 1> free;
|
||||
std::size_t freeCount = 1;
|
||||
free[0] = {0, 0, width, PageSide};
|
||||
int usedWidth = 0, usedHeight = 0;
|
||||
|
||||
for (std::size_t i = 0; i < batch.count; ++i) {
|
||||
const Region& region = batch.regions[i];
|
||||
std::size_t best = freeCount;
|
||||
for (std::size_t j = 0; j < freeCount; ++j) {
|
||||
const Rect& rect = free[j];
|
||||
if (region.width <= rect.width && region.height <= rect.height &&
|
||||
(best == freeCount || rect.Area() < free[best].Area())) {
|
||||
best = j;
|
||||
}
|
||||
}
|
||||
if (best == freeCount) return false;
|
||||
|
||||
const Rect rect = free[best];
|
||||
free[best] = free[--freeCount];
|
||||
result.offsets[i] = {rect.x, rect.y};
|
||||
usedWidth = std::max(usedWidth, rect.x + region.width);
|
||||
usedHeight = std::max(usedHeight, rect.y + region.height);
|
||||
|
||||
const int right = rect.width - region.width;
|
||||
const int below = rect.height - region.height;
|
||||
if (right > 0) {
|
||||
free[freeCount++] = {rect.x + region.width, rect.y, right,
|
||||
right <= below ? region.height : rect.height};
|
||||
}
|
||||
if (below > 0) {
|
||||
free[freeCount++] = {rect.x, rect.y + region.height, right <= below ? rect.width : region.width,
|
||||
below};
|
||||
}
|
||||
}
|
||||
|
||||
result.width = usedWidth;
|
||||
result.height = usedHeight;
|
||||
return true;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
inline bool Pack(Batch& batch, std::uint64_t unionTexels) {
|
||||
batch.width = 0;
|
||||
batch.height = 0;
|
||||
if (batch.count < 2 || batch.count > batch.regions.size()) return false;
|
||||
// Require a full page of traffic savings before taking on extra GPU copy
|
||||
// commands. Dense or single-region uploads already cost little.
|
||||
constexpr int PageTexels = PageSide * PageSide;
|
||||
if (unionTexels <= PageTexels) return false;
|
||||
|
||||
int widest = 1, dirtyTexels = 0;
|
||||
for (std::size_t i = 0; i < batch.count; ++i) {
|
||||
const Region& region = batch.regions[i];
|
||||
if (region.source == nullptr || region.width <= 0 || region.width > PageSide || region.height <= 0 ||
|
||||
region.height > PageSide) {
|
||||
return false;
|
||||
}
|
||||
widest = std::max(widest, region.width);
|
||||
dirtyTexels += region.width * region.height;
|
||||
}
|
||||
if (dirtyTexels > PageTexels) return false;
|
||||
|
||||
std::sort(batch.regions.begin(), batch.regions.begin() + batch.count, [](const Region& a, const Region& b) {
|
||||
const int aArea = a.width * a.height;
|
||||
const int bArea = b.width * b.height;
|
||||
if (aArea != bArea) return aArea > bArea;
|
||||
return std::max(a.width, a.height) > std::max(b.width, b.height);
|
||||
});
|
||||
|
||||
int width = 1;
|
||||
while (width < widest)
|
||||
width *= 2;
|
||||
Batch layout;
|
||||
for (; width <= PageSide; width *= 2) {
|
||||
if (!Detail::TryWidth(batch, width, layout)) continue;
|
||||
if (batch.width > 0 && layout.ByteSize() >= batch.ByteSize()) continue;
|
||||
std::copy_n(layout.offsets.begin(), batch.count, batch.offsets.begin());
|
||||
batch.width = layout.width;
|
||||
batch.height = layout.height;
|
||||
}
|
||||
return batch.width > 0 && unionTexels >= std::uint64_t(PageTexels + dirtyTexels + batch.width * batch.height);
|
||||
}
|
||||
|
||||
inline void WritePage(const Batch& batch, std::uint8_t* page) {
|
||||
std::memset(page, 0, batch.ByteSize());
|
||||
const std::size_t pitch = batch.width * BytesPerTexel;
|
||||
for (std::size_t i = 0; i < batch.count; ++i) {
|
||||
const Region& region = batch.regions[i];
|
||||
const Point offset = batch.offsets[i];
|
||||
auto* dest = page + offset.y * pitch + offset.x * BytesPerTexel;
|
||||
for (int y = 0; y < region.height; ++y) {
|
||||
std::memcpy(dest + y * pitch, region.source + y * region.sourcePitch, region.width * BytesPerTexel);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace MobileGL::MG_Backend::DirectGLES::TextureImpl::PackedTextureUpload
|
||||
@@ -0,0 +1,68 @@
|
||||
// MobileGL - MobileGL/MG_Backend/DirectGLES/TextureUploadBoxes.h
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
|
||||
namespace MobileGL::MG_Backend::DirectGLES::TextureImpl::TextureUploadBoxes {
|
||||
constexpr std::size_t MaxRegions = 64;
|
||||
constexpr std::uint64_t MinSavedTexels = 16 * 1024 / 4;
|
||||
|
||||
struct Box {
|
||||
int left, top, right, bottom;
|
||||
|
||||
std::uint64_t Texels() const { return static_cast<std::uint64_t>(right - left) * (bottom - top); }
|
||||
};
|
||||
|
||||
inline Box Union(Box a, Box b) {
|
||||
return {std::min(a.left, b.left), std::min(a.top, b.top), std::max(a.right, b.right),
|
||||
std::max(a.bottom, b.bottom)};
|
||||
}
|
||||
|
||||
// A second GLES upload is worth taking only for separated groups with a
|
||||
// substantial absolute and relative reduction. Small mip levels keep one job.
|
||||
inline bool WorthSearching(Box bounds, std::size_t count) {
|
||||
return count >= 2 && count <= MaxRegions && bounds.Texels() > MinSavedTexels;
|
||||
}
|
||||
|
||||
// Every input rectangle belongs to one group. Try cuts on both axes and
|
||||
// keep the least expensive pair of non-overlapping bounding boxes.
|
||||
inline std::optional<std::array<Box, 2>> Split(Box bounds, std::array<Box, MaxRegions>& regions,
|
||||
std::size_t count) {
|
||||
if (!WorthSearching(bounds, count)) return std::nullopt;
|
||||
const auto totalTexels = bounds.Texels();
|
||||
auto bestTexels = std::min(totalTexels / 2, totalTexels - MinSavedTexels);
|
||||
std::optional<std::array<Box, 2>> best;
|
||||
std::array<Box, MaxRegions> suffix;
|
||||
for (bool horizontal : {true, false}) {
|
||||
std::sort(regions.begin(), regions.begin() + count,
|
||||
[horizontal](Box a, Box b) { return horizontal ? a.left < b.left : a.top < b.top; });
|
||||
suffix[count - 1] = regions[count - 1];
|
||||
for (std::size_t i = count - 1; i > 1; --i) {
|
||||
suffix[i - 1] = Union(regions[i - 1], suffix[i]);
|
||||
}
|
||||
Box prefix = regions[0];
|
||||
for (std::size_t i = 1; i < count; ++i) {
|
||||
const Box rest = suffix[i];
|
||||
const bool separated = horizontal ? prefix.right < rest.left : prefix.bottom < rest.top;
|
||||
const auto texels = prefix.Texels() + rest.Texels();
|
||||
if (separated && texels <= bestTexels) {
|
||||
bestTexels = texels;
|
||||
best = std::array<Box, 2>{prefix, rest};
|
||||
}
|
||||
prefix = Union(prefix, regions[i]);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
} // namespace MobileGL::MG_Backend::DirectGLES::TextureImpl::TextureUploadBoxes
|
||||
Reference in new issue
Block a user