[MG_Backend] (Disaggregated): reuse exact wire image views within a frame slot

This commit is contained in:
2026-09-20 08:16:21 -04:00
parent 21d8c456e7
commit ed507b1a6a
2 changed files with 73 additions and 2 deletions
@@ -41,6 +41,30 @@
namespace MobileGL::MG_Backend::DirectVulkan {
#if MOBILEGL_BUILD_DISAGGREGATED
SizeT UniformManager::WireImageViewKeyHash::operator()(const WireImageViewKey& key) const {
SizeT hash = std::hash<VkImage>{}(key.image);
const auto mix = [&hash](Uint64 value) {
hash ^= std::hash<Uint64>{}(value) + static_cast<SizeT>(0x9e3779b97f4a7c15ULL) +
(hash << 6) + (hash >> 2);
};
mix(key.root.Slot);
mix(key.root.Gen);
mix(key.imageEpoch);
mix(key.flags);
mix(key.type);
mix(key.format);
mix(key.components.r);
mix(key.components.g);
mix(key.components.b);
mix(key.components.a);
mix(key.range.aspectMask);
mix(key.range.baseMipLevel);
mix(key.range.levelCount);
mix(key.range.baseArrayLayer);
mix(key.range.layerCount);
return hash;
}
[[noreturn]] static void WireDescriptorFatal(const char* detail) {
MGLOG_F("MGPipe: Fatal{UnmigratedVerb, \"Magma:%s\"}", detail);
std::abort();
@@ -192,8 +216,19 @@ namespace MobileGL::MG_Backend::DirectVulkan {
info.components = {swizzle(0), swizzle(1), swizzle(2), swizzle(3)};
}
VkImageView view = VK_NULL_HANDLE;
if (vkCreateImageView(m_device, &info, nullptr, &view) != VK_SUCCESS) return false;
m_frames[m_wireFrameIndex].wireImageViews.push_back(view);
const WireImageViewKey key{root, info.image, m_textureManager->GetTextureImageEpoch(),
info.flags, info.viewType, info.format, info.components, info.subresourceRange};
const auto& frame = m_frames[m_wireFrameIndex];
const auto cached = frame.wireImageViewCache.find(key);
if (cached != frame.wireImageViewCache.end()) {
view = cached->second;
} else {
if (vkCreateImageView(m_device, &info, nullptr, &view) != VK_SUCCESS) return false;
frame.wireImageViews.push_back(view);
frame.wireImageViewCache.emplace(key, view);
}
// Only view creation is memoized. Layout/memory dependencies above and
// write tracking below still run for every descriptor, including hits.
out.imageView = view;
out.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
if (storage) {
@@ -539,6 +574,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
for (auto& frame : m_frames) {
if (m_device != VK_NULL_HANDLE) {
#if MOBILEGL_BUILD_DISAGGREGATED
frame.wireImageViewCache.clear();
for (const auto view : frame.wireImageViews) vkDestroyImageView(m_device, view, nullptr);
frame.wireImageViews.clear();
#endif
@@ -585,6 +621,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
auto& frame = m_frames[frameIndex];
#if MOBILEGL_BUILD_DISAGGREGATED
m_wireFrameIndex = frameIndex;
frame.wireImageViewCache.clear();
for (const auto view : frame.wireImageViews) vkDestroyImageView(m_device, view, nullptr);
frame.wireImageViews.clear();
#endif
@@ -159,12 +159,46 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 cursor = 0;
};
#if MOBILEGL_BUILD_DISAGGREGATED
// ResolveWireImageDescriptor emits no pNext chain. Key every view-create
// value explicitly: hash collisions must never alias different windows,
// formats or swizzles. The root handle and allocation epoch also prevent
// a recycled native image handle from reviving an older view.
struct WireImageViewKey {
MG_Pipe::MGPipeHandle root{};
VkImage image = VK_NULL_HANDLE;
Uint64 imageEpoch = 0;
VkImageViewCreateFlags flags = 0;
VkImageViewType type = VK_IMAGE_VIEW_TYPE_2D;
VkFormat format = VK_FORMAT_UNDEFINED;
VkComponentMapping components{};
VkImageSubresourceRange range{};
Bool operator==(const WireImageViewKey& other) const {
return root == other.root && image == other.image && imageEpoch == other.imageEpoch &&
flags == other.flags && type == other.type && format == other.format &&
components.r == other.components.r && components.g == other.components.g &&
components.b == other.components.b && components.a == other.components.a &&
range.aspectMask == other.range.aspectMask && range.baseMipLevel == other.range.baseMipLevel &&
range.levelCount == other.range.levelCount && range.baseArrayLayer == other.range.baseArrayLayer &&
range.layerCount == other.range.layerCount;
}
};
struct WireImageViewKeyHash {
SizeT operator()(const WireImageViewKey& key) const;
};
#endif
struct FrameResources {
Vector<DescriptorPoolBucket> descriptorPools;
UnorderedMap<VkDescriptorSetLayout, DescriptorSetCacheEntry> descriptorSetCacheByLayout;
Vector<VkBufferView> texelBufferViews;
#if MOBILEGL_BUILD_DISAGGREGATED
mutable Vector<VkImageView> wireImageViews;
// Own views in the vector above until this slot's fence/idle proof.
// Cache hits preserve the handle, enabling descriptor-content reuse.
mutable UnorderedMap<WireImageViewKey, VkImageView, WireImageViewKeyHash> wireImageViewCache;
#endif
Uint32 activeDescriptorPoolIndex = 0;
Uint32 allocatedSetsThisFrame = 0;