mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-20 08:21:56 +09:00
[Feature] (SharedImage): zero-copy Wayland windows via linux-dmabuf, EGL dma-buf import, GBM backend, and Espryt/Magma AHB present and texture binding
This commit is contained in:
@@ -262,7 +262,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
info.format = format;
|
||||
info.subresourceRange = {aspect, level, levels, layer, layers};
|
||||
if (!storage) {
|
||||
const Bool alphaIsOne = ResolveTextureFormatInfo(
|
||||
// An X-format shared image carries undefined alpha bytes, exactly like an expanded RGB.
|
||||
const Bool alphaIsOne = resource->sharedImageAlphaOne || ResolveTextureFormatInfo(
|
||||
static_cast<TextureInternalFormat>(record.Desc.InternalFormat)).expandRgbToRgba;
|
||||
const auto swizzle = [&](Uint32 channel) {
|
||||
const auto value = record.Params.Swizzle[channel];
|
||||
|
||||
@@ -22,10 +22,16 @@
|
||||
// (shape) and the server's staged-texture store (texels).
|
||||
#include <MG_Pipe/PipeApply.h>
|
||||
#include <MG_Remote/Server/StagedTextureStore.h>
|
||||
// Shared images: a texture whose level 0 is a server-allocated AHardwareBuffer.
|
||||
#include <MG_Remote/Server/SharedImageRegistry.h>
|
||||
#include "../DirectVulkan.h"
|
||||
// P7 wave 2 package B3: rule I's tally for the silent exits on this file's wire arm.
|
||||
#include "WireDeclineTally.h"
|
||||
#if defined(__ANDROID__)
|
||||
#include <android/hardware_buffer.h>
|
||||
#endif
|
||||
#endif
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -2108,6 +2114,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
Bool VkTextureManager::SyncWireTextureShape(const MG_Pipe::MGPipeResourceRecord& record,
|
||||
TextureResource& resource, Bool requireStorage) {
|
||||
const MG_Pipe::MGPResourceDesc& desc = record.Desc;
|
||||
// Level 0 is a shared image: bind its buffer. An image that cannot be bound here leaves
|
||||
// the texture the storage its NULL-data definition asked for, below.
|
||||
if (record.SharedImageId != 0) {
|
||||
const SharedImageBind bound = SyncWireSharedImage(record, resource, requireStorage);
|
||||
if (bound != SharedImageBind::Unresolved) return bound == SharedImageBind::Bound;
|
||||
}
|
||||
if (desc.StorageKind != static_cast<Uint8>(TextureStorageType::Mipmap)) {
|
||||
MGLOG_W_ONCE("Magma wire texture {slot=%u, gen=%u}: buffer-backed textures are not migrated; declined",
|
||||
desc.Resource.Slot, desc.Resource.Gen);
|
||||
@@ -2276,7 +2288,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
: (desc.Levels > 1 ? std::min(std::max<Uint32>(desc.Levels, fullMipLevels), fullMipLevels)
|
||||
: 1u);
|
||||
|
||||
const Bool compatible = resource.image != VK_NULL_HANDLE && resource.format == format &&
|
||||
// A shared image never stands in for allocated storage: the attach is over (or never
|
||||
// resolved), so the texture gets storage of its own, and none of the image's texels.
|
||||
const Bool compatible = resource.image != VK_NULL_HANDLE && resource.sharedImageId == 0 &&
|
||||
resource.format == format &&
|
||||
resource.extent.width == desc.Width && resource.extent.height == imageHeight &&
|
||||
resource.depth == shapeInfo.depth && resource.arrayLayers == shapeInfo.arrayLayers &&
|
||||
resource.viewType == shapeInfo.viewType &&
|
||||
@@ -2291,7 +2306,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
// A named-level respecify changes ONLY that level; a BindMask update changes no
|
||||
// storage at all. Preserve every compatible old level when growing/upgrading the
|
||||
// allocation. A whole-store redefinition permits (but does not require) old texels.
|
||||
const Bool preserve = resource.image != VK_NULL_HANDLE && resource.format == format &&
|
||||
const Bool preserve = resource.image != VK_NULL_HANDLE && resource.sharedImageId == 0 &&
|
||||
resource.format == format &&
|
||||
resource.extent.width == desc.Width && resource.extent.height == imageHeight &&
|
||||
resource.depth == shapeInfo.depth && resource.arrayLayers == shapeInfo.arrayLayers &&
|
||||
resource.viewType == shapeInfo.viewType &&
|
||||
@@ -2769,6 +2785,232 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
return MG_Pipe::kMGPipeNullHandle;
|
||||
}
|
||||
|
||||
Bool VkTextureManager::ImportSharedImage(void* nativeBuffer, Uint32 width, Uint32 height,
|
||||
ImportedSharedImage& out, String& why) {
|
||||
out = ImportedSharedImage{};
|
||||
#if defined(__ANDROID__)
|
||||
auto* ahb = static_cast<AHardwareBuffer*>(nativeBuffer);
|
||||
if (ahb == nullptr) {
|
||||
why = "the image has no AHardwareBuffer";
|
||||
return false;
|
||||
}
|
||||
VulkanRenderer::WireAhbImport ctx;
|
||||
if (pVulkanRenderer == nullptr || !pVulkanRenderer->GetWireAhbImport(ctx)) {
|
||||
why = "the device did not take VK_ANDROID_external_memory_android_hardware_buffer";
|
||||
return false;
|
||||
}
|
||||
AHardwareBuffer_Desc desc{};
|
||||
AHardwareBuffer_describe(ahb, &desc);
|
||||
if (desc.width != width || desc.height != height || desc.layers != 1) {
|
||||
why = "the buffer is " + std::to_string(desc.width) + "x" + std::to_string(desc.height) + "x" +
|
||||
std::to_string(desc.layers) + ", not " + std::to_string(width) + "x" + std::to_string(height);
|
||||
return false;
|
||||
}
|
||||
const auto getProperties =
|
||||
reinterpret_cast<PFN_vkGetAndroidHardwareBufferPropertiesANDROID>(ctx.getAhbProperties);
|
||||
VkAndroidHardwareBufferFormatPropertiesANDROID formatProperties{
|
||||
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID};
|
||||
VkAndroidHardwareBufferPropertiesANDROID properties{VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID};
|
||||
properties.pNext = &formatProperties;
|
||||
VkResult result = getProperties(ctx.device, ahb, &properties);
|
||||
if (result != VK_SUCCESS) {
|
||||
why = "vkGetAndroidHardwareBufferPropertiesANDROID -> " + std::to_string(static_cast<int>(result));
|
||||
return false;
|
||||
}
|
||||
// The RGBA8/RGBX8 buffers the registry allocates have a Vulkan format; an external-only
|
||||
// format would need a YCbCr conversion and is not a colour buffer this path can write.
|
||||
if (formatProperties.format == VK_FORMAT_UNDEFINED) {
|
||||
why = "the buffer has no Vulkan format (external format only)";
|
||||
return false;
|
||||
}
|
||||
// Usage is bounded by the buffer's own usage (the AHardwareBuffer usage equivalence);
|
||||
// transfers need no buffer usage bit.
|
||||
VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
if ((desc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) != 0 &&
|
||||
(formatProperties.formatFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) != 0)
|
||||
usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
if ((desc.usage & AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT) != 0 &&
|
||||
(formatProperties.formatFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) != 0)
|
||||
usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
VkExternalMemoryImageCreateInfo external{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO};
|
||||
external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
|
||||
VkImageCreateInfo imageInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
|
||||
imageInfo.pNext = &external;
|
||||
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||
imageInfo.format = formatProperties.format;
|
||||
imageInfo.extent = {width, height, 1};
|
||||
imageInfo.mipLevels = 1;
|
||||
imageInfo.arrayLayers = 1;
|
||||
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
imageInfo.usage = usage;
|
||||
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
result = vkCreateImage(ctx.device, &imageInfo, nullptr, &out.image);
|
||||
if (result != VK_SUCCESS) {
|
||||
out.image = VK_NULL_HANDLE;
|
||||
why = "vkCreateImage(external AHB) -> " + std::to_string(static_cast<int>(result));
|
||||
return false;
|
||||
}
|
||||
Int32 type = -1;
|
||||
for (Uint32 i = 0; i < ctx.memory.memoryTypeCount && type < 0; ++i) {
|
||||
if ((properties.memoryTypeBits & (1u << i)) != 0 &&
|
||||
(ctx.memory.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0)
|
||||
type = static_cast<Int32>(i);
|
||||
}
|
||||
for (Uint32 i = 0; i < ctx.memory.memoryTypeCount && type < 0; ++i) {
|
||||
if ((properties.memoryTypeBits & (1u << i)) != 0) type = static_cast<Int32>(i);
|
||||
}
|
||||
if (type < 0) {
|
||||
why = "no memory type can import it";
|
||||
DestroyImportedSharedImage(ctx.device, out);
|
||||
return false;
|
||||
}
|
||||
VkMemoryDedicatedAllocateInfo dedicated{VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO};
|
||||
dedicated.image = out.image;
|
||||
VkImportAndroidHardwareBufferInfoANDROID import{VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID};
|
||||
import.pNext = &dedicated;
|
||||
import.buffer = ahb;
|
||||
VkMemoryAllocateInfo allocateInfo{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
allocateInfo.pNext = &import;
|
||||
allocateInfo.allocationSize = properties.allocationSize;
|
||||
allocateInfo.memoryTypeIndex = static_cast<Uint32>(type);
|
||||
result = vkAllocateMemory(ctx.device, &allocateInfo, nullptr, &out.memory);
|
||||
if (result != VK_SUCCESS) {
|
||||
out.memory = VK_NULL_HANDLE;
|
||||
why = "vkAllocateMemory(import AHB) -> " + std::to_string(static_cast<int>(result));
|
||||
DestroyImportedSharedImage(ctx.device, out);
|
||||
return false;
|
||||
}
|
||||
result = vkBindImageMemory(ctx.device, out.image, out.memory, 0);
|
||||
if (result != VK_SUCCESS) {
|
||||
why = "vkBindImageMemory -> " + std::to_string(static_cast<int>(result));
|
||||
DestroyImportedSharedImage(ctx.device, out);
|
||||
return false;
|
||||
}
|
||||
out.format = formatProperties.format;
|
||||
out.usage = usage;
|
||||
out.features = formatProperties.formatFeatures;
|
||||
out.queueFamily = ctx.queueFamily;
|
||||
return true;
|
||||
#else
|
||||
(void)nativeBuffer, (void)width, (void)height;
|
||||
why = "shared images are imported on Android only";
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void VkTextureManager::DestroyImportedSharedImage(VkDevice device, ImportedSharedImage& image) {
|
||||
if (image.image != VK_NULL_HANDLE) vkDestroyImage(device, image.image, nullptr);
|
||||
if (image.memory != VK_NULL_HANDLE) vkFreeMemory(device, image.memory, nullptr);
|
||||
image = ImportedSharedImage{};
|
||||
}
|
||||
|
||||
VkTextureManager::SharedImageBind VkTextureManager::SyncWireSharedImage(
|
||||
const MG_Pipe::MGPipeResourceRecord& record, TextureResource& resource, Bool requireStorage) {
|
||||
const MG_Pipe::MGPResourceDesc& desc = record.Desc;
|
||||
const Uint64 id = record.SharedImageId;
|
||||
if (requireStorage) {
|
||||
// The buffer's usage has no storage equivalent; a reallocation would drop the image.
|
||||
MGLOG_E_ONCE("Magma wire texture {slot=%u, gen=%u}: shared image %llu is bound as a shader image, "
|
||||
"which it cannot back; declined",
|
||||
desc.Resource.Slot, desc.Resource.Gen, static_cast<unsigned long long>(id));
|
||||
return SharedImageBind::Failed;
|
||||
}
|
||||
// Already this image: the serial moved for something else (params, the bind mask).
|
||||
if (resource.sharedImageId == id && resource.image != VK_NULL_HANDLE) return SharedImageBind::Bound;
|
||||
|
||||
const auto image = MG_Remote::Server::SharedImages::Find(id);
|
||||
if (image == nullptr) {
|
||||
MGLOG_E_ONCE("Magma wire texture {slot=%u, gen=%u}: shared image %llu is no longer live; the texture "
|
||||
"keeps its own (undefined) level 0",
|
||||
desc.Resource.Slot, desc.Resource.Gen, static_cast<unsigned long long>(id));
|
||||
return SharedImageBind::Unresolved;
|
||||
}
|
||||
const Bool flat2D = desc.Target == static_cast<Uint8>(MG_Pipe::MGPipeResourceTarget::Tex2D) ||
|
||||
desc.Target == static_cast<Uint8>(MG_Pipe::MGPipeResourceTarget::TexRect);
|
||||
if (!flat2D || desc.Width != image->Width || desc.Height != image->Height ||
|
||||
!MG_Pipe::MGPipeHandleIsNull(desc.ViewOf)) {
|
||||
MGLOG_E_ONCE("Magma wire texture {slot=%u, gen=%u}: pipe target %u at %ux%u cannot take shared image "
|
||||
"%llu (%ux%u); the texture keeps its own level 0",
|
||||
desc.Resource.Slot, desc.Resource.Gen, static_cast<Uint32>(desc.Target), desc.Width,
|
||||
desc.Height, static_cast<unsigned long long>(id), image->Width, image->Height);
|
||||
return SharedImageBind::Unresolved;
|
||||
}
|
||||
ImportedSharedImage imported;
|
||||
String why;
|
||||
if (!ImportSharedImage(image->Native, image->Width, image->Height, imported, why)) {
|
||||
MGLOG_E_ONCE("Magma wire texture {slot=%u, gen=%u}: shared image %llu not imported - %s; the texture "
|
||||
"keeps its own level 0",
|
||||
desc.Resource.Slot, desc.Resource.Gen, static_cast<unsigned long long>(id), why.c_str());
|
||||
return SharedImageBind::Unresolved;
|
||||
}
|
||||
|
||||
// ACQUIRE FROM THE FOREIGN FAMILY. The writer (a client session's Present, on its own
|
||||
// device) leaves the image released to VK_QUEUE_FAMILY_FOREIGN_EXT in GENERAL, which is
|
||||
// also the layout every wire descriptor samples in. Submitted on its own and waited, like
|
||||
// the preserve copy: it runs once per attach, before any recorded use of the image.
|
||||
VkCommandBufferAllocateInfo allocInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
|
||||
allocInfo.commandPool = m_commandPool;
|
||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocInfo.commandBufferCount = 1;
|
||||
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
|
||||
VK_VERIFY(vkAllocateCommandBuffers(m_device, &allocInfo, &commandBuffer),
|
||||
"vkAllocateCommandBuffers(shared image acquire)");
|
||||
VkCommandBufferBeginInfo beginInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
VK_VERIFY(vkBeginCommandBuffer(commandBuffer, &beginInfo), "vkBeginCommandBuffer(shared image acquire)");
|
||||
VkImageMemoryBarrier acquire{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
|
||||
acquire.srcAccessMask = 0;
|
||||
acquire.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
|
||||
acquire.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
acquire.newLayout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
acquire.srcQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
|
||||
acquire.dstQueueFamilyIndex = imported.queueFamily;
|
||||
acquire.image = imported.image;
|
||||
acquire.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0,
|
||||
0, nullptr, 0, nullptr, 1, &acquire);
|
||||
VK_VERIFY(vkEndCommandBuffer(commandBuffer), "vkEndCommandBuffer(shared image acquire)");
|
||||
VkFenceCreateInfo fenceInfo{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
|
||||
VkFence fence = VK_NULL_HANDLE;
|
||||
VK_VERIFY(vkCreateFence(m_device, &fenceInfo, nullptr, &fence), "vkCreateFence(shared image acquire)");
|
||||
VkSubmitInfo submitInfo{VK_STRUCTURE_TYPE_SUBMIT_INFO};
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &commandBuffer;
|
||||
VK_VERIFY(vkQueueSubmit(m_graphicsQueue, 1, &submitInfo, fence), "vkQueueSubmit(shared image acquire)");
|
||||
VK_VERIFY(vkWaitForFences(m_device, 1, &fence, VK_TRUE, UINT64_MAX), "vkWaitForFences(shared image acquire)");
|
||||
vkDestroyFence(m_device, fence, nullptr);
|
||||
vkFreeCommandBuffers(m_device, m_commandPool, 1, &commandBuffer);
|
||||
|
||||
TextureResource replacement;
|
||||
replacement.image = imported.image;
|
||||
replacement.importedMemory = imported.memory;
|
||||
replacement.layout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
replacement.extent = {image->Width, image->Height};
|
||||
replacement.depth = 1;
|
||||
replacement.arrayLayers = 1;
|
||||
replacement.mipLevels = 1;
|
||||
replacement.sampledBaseMipLevel = 0;
|
||||
replacement.sampledLevelCount = 1;
|
||||
replacement.format = imported.format;
|
||||
replacement.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
replacement.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
replacement.sampleCount = VK_SAMPLE_COUNT_1_BIT;
|
||||
replacement.imageCreateFlags = 0;
|
||||
replacement.usageFlags = imported.usage;
|
||||
replacement.sharedImageId = id;
|
||||
replacement.sharedImageOwner = image;
|
||||
replacement.sharedImageAlphaOne = image->Fourcc == MG_Remote::Server::SharedImages::kFourccXbgr8888;
|
||||
++m_textureImageEpoch; // a new attachment image invalidates cached render passes
|
||||
DeferResourceRelease(Move(resource));
|
||||
std::destroy_at(&resource);
|
||||
std::construct_at(&resource, Move(replacement));
|
||||
MGLOG_D("Magma wire texture {slot=%u, gen=%u}: level 0 is shared image %llu (%ux%u)", desc.Resource.Slot,
|
||||
desc.Resource.Gen, static_cast<unsigned long long>(id), image->Width, image->Height);
|
||||
return SharedImageBind::Bound;
|
||||
}
|
||||
|
||||
VkTextureManager::TextureResource* VkTextureManager::SyncTextureResourceByHandle(
|
||||
MG_Pipe::MGPipeHandle handle, Bool renderbuffer, Bool requireStorage) {
|
||||
MOBILEGL_ASSERT(m_device != VK_NULL_HANDLE, "SyncTextureResourceByHandle: m_device == VK_NULL_HANDLE");
|
||||
|
||||
@@ -357,6 +357,14 @@ public:
|
||||
// on every mutation, so the first handle sync of a fresh image always re-checks.
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
Uint64 syncedWireSerial = 0;
|
||||
// SHARED IMAGES: level 0 is a server shared image (an imported AHardwareBuffer), not a
|
||||
// VMA allocation. The resource owns the image and its dedicated imported memory, and
|
||||
// holds the registry's reference so the buffer outlives every use (deferred releases
|
||||
// included). `sharedImageAlphaOne`: an X-format image, whose alpha bytes are undefined.
|
||||
VkDeviceMemory importedMemory = VK_NULL_HANDLE;
|
||||
Uint64 sharedImageId = 0;
|
||||
SharedPtr<const void> sharedImageOwner;
|
||||
Bool sharedImageAlphaOne = false;
|
||||
#endif
|
||||
|
||||
TextureResource() = default;
|
||||
@@ -393,6 +401,10 @@ public:
|
||||
std::swap(this->syncedShapeVersion, that.syncedShapeVersion);
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
std::swap(this->syncedWireSerial, that.syncedWireSerial);
|
||||
std::swap(this->importedMemory, that.importedMemory);
|
||||
std::swap(this->sharedImageId, that.sharedImageId);
|
||||
std::swap(this->sharedImageOwner, that.sharedImageOwner);
|
||||
std::swap(this->sharedImageAlphaOne, that.sharedImageAlphaOne);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -431,6 +443,16 @@ public:
|
||||
if (image != VK_NULL_HANDLE && allocation != nullptr) {
|
||||
vmaDestroyImage(s_allocator.Get(), image, allocation);
|
||||
}
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
if (importedMemory != VK_NULL_HANDLE) {
|
||||
if (image != VK_NULL_HANDLE) vkDestroyImage(s_device.Get(), image, nullptr);
|
||||
vkFreeMemory(s_device.Get(), importedMemory, nullptr);
|
||||
}
|
||||
importedMemory = VK_NULL_HANDLE;
|
||||
sharedImageId = 0;
|
||||
sharedImageOwner.reset();
|
||||
sharedImageAlphaOne = false;
|
||||
#endif
|
||||
fullView = VK_NULL_HANDLE;
|
||||
sampledView = VK_NULL_HANDLE;
|
||||
perMipViews.clear();
|
||||
@@ -592,6 +614,23 @@ public:
|
||||
// ranges select cube faces; array and 3D levels share one staged level shadow.
|
||||
void MarkWireTextureGpuWritten(MG_Pipe::MGPipeHandle handle, Uint32 mipLevel,
|
||||
Uint32 baseArrayLayer = 0, Uint32 layerCount = 1);
|
||||
|
||||
// SHARED IMAGES (docs/Disaggregated/notes/anland/plan-ahb-dmabuf.md): a server shared image's
|
||||
// AHardwareBuffer bound as a single-level 2D colour image on THIS session's device, on dedicated
|
||||
// memory imported from the buffer. Android only (false with `why` elsewhere, or when the device
|
||||
// did not take the AHardwareBuffer extension). The image is born UNDEFINED; whoever uses it
|
||||
// acquires it from the foreign queue family first, and a writer releases it back after.
|
||||
struct ImportedSharedImage {
|
||||
VkImage image = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
VkFormat format = VK_FORMAT_UNDEFINED;
|
||||
VkImageUsageFlags usage = 0;
|
||||
VkFormatFeatureFlags features = 0;
|
||||
Uint32 queueFamily = 0;
|
||||
};
|
||||
static Bool ImportSharedImage(void* nativeBuffer, Uint32 width, Uint32 height, ImportedSharedImage& out,
|
||||
String& why);
|
||||
static void DestroyImportedSharedImage(VkDevice device, ImportedSharedImage& image);
|
||||
#endif
|
||||
VkImageView GetOrCreateViewAtMipLevel(MG_State::GLState::ITextureObject& texture, Uint32 mipLevel);
|
||||
VkImageView GetOrCreateAttachmentViewAtMipLevel(MG_State::GLState::ITextureObject& texture, Uint32 mipLevel,
|
||||
@@ -778,6 +817,12 @@ private:
|
||||
Bool requireStorage);
|
||||
Bool UploadPendingWireLevels(MG_Pipe::MGPipeHandle handle, MG_Pipe::MGPipeResourceRecord& record,
|
||||
TextureResource& resource);
|
||||
// SyncWireTextureShape's arm for a record whose level 0 IS a shared image (SharedImageId):
|
||||
// binds the imported buffer instead of allocating. Unresolved = the image is gone or cannot be
|
||||
// imported here; `resource` is then untouched and the caller allocates as for any texture.
|
||||
enum class SharedImageBind : Uint8 { Bound, Failed, Unresolved };
|
||||
SharedImageBind SyncWireSharedImage(const MG_Pipe::MGPipeResourceRecord& record, TextureResource& resource,
|
||||
Bool requireStorage);
|
||||
// The handle-keyed twin of m_textureResources, keyed by StagedTextureStore::KeyForHandle.
|
||||
// Node-based for the same reason m_renderbufferResources is: callers hold TextureResource*
|
||||
// across map-touching calls.
|
||||
|
||||
@@ -48,6 +48,8 @@
|
||||
// P5c (T5 / tx): the server's staged-texture shadow GenerateMipmap defines its chain on.
|
||||
#include <MG_Remote/Server/StagedTextureStore.h>
|
||||
#include <MG_Remote/Server/ServerLoop.h>
|
||||
// Shared images: the present side resolves the image it imports (WireSharedImage.inc).
|
||||
#include <MG_Remote/Server/SharedImageRegistry.h>
|
||||
// P5c (G6): the named-blit arm's endpoint resolution runs inside the frontend-keyed scope.
|
||||
#include <MG_Impl/Pipe/SlotAllocator.h>
|
||||
// P7 wave 2 package B3: rule I's tally for WireDraw.inc's silent draw drops.
|
||||
@@ -3510,6 +3512,7 @@ void main() {
|
||||
DestroyWireColorBlitResources();
|
||||
DestroyWireDepthMipmapResources();
|
||||
DestroyWireMultisampleResolveResources();
|
||||
DestroySharedImagePresentTargets(false);
|
||||
#endif
|
||||
OnSubmitsCompletedUpTo(m_submitCounter);
|
||||
DestroySubmitFencePool();
|
||||
@@ -8082,6 +8085,8 @@ void main() {
|
||||
// that file's pass with the box filter taken out and a sample index put in.
|
||||
#include "WireMultisampleResolve.inc"
|
||||
#include "WireDraw.inc"
|
||||
// AFTER WireFramebuffer.inc: the shared-image present is that file's readback, into an image.
|
||||
#include "WireSharedImage.inc"
|
||||
|
||||
void VulkanRenderer::Clear(GLbitfield mask) {
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
|
||||
@@ -179,6 +179,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
void* getAhbProperties = nullptr; // PFN_vkGetAndroidHardwareBufferPropertiesANDROID
|
||||
};
|
||||
Bool GetWireAhbImport(WireAhbImport& out) const;
|
||||
// SHARED IMAGES (BackendObject::BlitDefaultFramebufferToSharedImage): the active surface
|
||||
// target's default framebuffer, copied into `image` top row first and scaled to its
|
||||
// extent. Returns once the copy completed on the GPU. False (logged) when it cannot be
|
||||
// done here: no AHardwareBuffer import, a quarter-turned surface, an unblittable format.
|
||||
Bool BlitDefaultFramebufferToSharedImage(const SharedImageView& image);
|
||||
#endif
|
||||
|
||||
// FrameContext::IRecordingObserver: prepares the frame's timer-query
|
||||
@@ -642,6 +647,15 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
// one entry point the import needs (PFN_vkGetAndroidHardwareBufferPropertiesANDROID).
|
||||
Bool m_wireAhbImport = false;
|
||||
void* m_wireGetAhbProperties = nullptr;
|
||||
// Shared images this session presents into, imported once each and keyed by the image's
|
||||
// id (monotonic, never reused). `owner` watches the registry's image: an entry whose image
|
||||
// died is dropped at the next present, every earlier present having completed.
|
||||
struct SharedImagePresentTarget {
|
||||
VkTextureManager::ImportedSharedImage image;
|
||||
WeakPtr<const void> owner;
|
||||
};
|
||||
UnorderedMap<Uint64, SharedImagePresentTarget> m_sharedImagePresentTargets;
|
||||
void DestroySharedImagePresentTargets(Bool onlyDead);
|
||||
// Runs the probe (memoized per device identity) and sets the member above. Called at the end of
|
||||
// device creation, after ArmPrimGenReroute: it records on m_graphicsQueue.
|
||||
void ArmWireDepthResolveOrder();
|
||||
|
||||
@@ -1390,6 +1390,14 @@
|
||||
&viewFormat, &layerCount);
|
||||
auto* resource = m_textureManager->SyncTextureResourceByHandle(handle);
|
||||
if (!resource) MagmaWireFatal("mipmap-resource");
|
||||
// A shared image is one level of a buffer someone else allocated; growing the chain would
|
||||
// reallocate it away from the buffer.
|
||||
if (resource->sharedImageId != 0) {
|
||||
MGLOG_E_ONCE("DirectVulkan::GenerateWireMipmap: the texture's level 0 is shared image %llu, which has "
|
||||
"no mip chain; levels %u..%u are left as they are",
|
||||
static_cast<unsigned long long>(resource->sharedImageId), logicalBase + 1, logicalEnd - 1);
|
||||
return;
|
||||
}
|
||||
m_textureManager->FlushPendingUploads();
|
||||
RetireWireDrawPass();
|
||||
auto& frame = m_frameContext.GetCurrent();
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
// MobileGL - MobileGL/MG_Backend/DirectVulkan/Renderer/WireSharedImage.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
|
||||
|
||||
// SHARED IMAGES, the present side (docs/Disaggregated/notes/anland/plan-ahb-dmabuf.md). Included by
|
||||
// VulkanRenderer.cpp inside the backend namespace, after WireFramebuffer.inc, whose readback this
|
||||
// copy is modelled on: the same flush, the same write-visibility barrier, the same transition of
|
||||
// the default framebuffer's image and the same submit-and-wait.
|
||||
//
|
||||
// OWNERSHIP. The image lives on more than one device: this session writes it, the compositor's
|
||||
// session (another VulkanRenderer, another VkDevice) samples it. Each present acquires it from
|
||||
// VK_QUEUE_FAMILY_FOREIGN_EXT (UNDEFINED: the copy overwrites every texel) and releases it back in
|
||||
// GENERAL, the layout the sampling side acquires it in (VkTextureManager::SyncWireSharedImage).
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
void VulkanRenderer::DestroySharedImagePresentTargets(Bool onlyDead) {
|
||||
for (auto it = m_sharedImagePresentTargets.begin(); it != m_sharedImagePresentTargets.end();) {
|
||||
if (onlyDead && !it->second.owner.expired()) {
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
VkTextureManager::DestroyImportedSharedImage(m_device, it->second.image);
|
||||
it = m_sharedImagePresentTargets.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
// A copy moves bytes; a blit converts through the format. The bytes are right only between
|
||||
// formats with one memory layout, which for the RGBA8 buffers means RGBA8 itself or its sRGB
|
||||
// twin (a blit would decode the sRGB bytes on the way).
|
||||
static Bool SharedImageCopiesAsIs(VkFormat source, VkFormat destination) {
|
||||
if (source == destination) return true;
|
||||
return destination == VK_FORMAT_R8G8B8A8_UNORM && source == VK_FORMAT_R8G8B8A8_SRGB;
|
||||
}
|
||||
|
||||
Bool VulkanRenderer::BlitDefaultFramebufferToSharedImage(const SharedImageView& view) {
|
||||
if (m_device == VK_NULL_HANDLE || view.Width == 0 || view.Height == 0) return false;
|
||||
// Every earlier present waited for its copy, so an entry whose image has died is idle.
|
||||
DestroySharedImagePresentTargets(true);
|
||||
|
||||
// The default framebuffer's image is stored in display orientation (row 0 is the top of
|
||||
// the frame) on an identity surface, and turned half round on a 180 one; a blit expresses
|
||||
// both. A quarter turn transposes, which no copy or blit can - and only the compositor's
|
||||
// own window is ever quarter-turned, which presents to the display rather than here.
|
||||
const VkSurfaceTransformFlagBitsKHR preTransform = m_swapchainObject.GetPreTransform();
|
||||
if (IsQuarterTurnPreTransform(preTransform)) {
|
||||
MGLOG_E_ONCE("Magma: shared image %llu: the current surface %llx is quarter-turned (transform %u); a "
|
||||
"present into a shared image is declined there",
|
||||
static_cast<unsigned long long>(view.Id), static_cast<unsigned long long>(m_activeTargetKey),
|
||||
static_cast<Uint32>(preTransform));
|
||||
return false;
|
||||
}
|
||||
|
||||
auto found = m_sharedImagePresentTargets.find(view.Id);
|
||||
if (found == m_sharedImagePresentTargets.end()) {
|
||||
const auto owner = MG_Remote::Server::SharedImages::Find(view.Id);
|
||||
if (owner == nullptr) {
|
||||
MGLOG_E_ONCE("Magma: shared image %llu is not live; the present is declined",
|
||||
static_cast<unsigned long long>(view.Id));
|
||||
return false;
|
||||
}
|
||||
SharedImagePresentTarget target;
|
||||
String why;
|
||||
if (!VkTextureManager::ImportSharedImage(view.NativeBuffer, view.Width, view.Height, target.image, why)) {
|
||||
MGLOG_E_ONCE("Magma: shared image %llu (%ux%u) not imported - %s; the present is declined",
|
||||
static_cast<unsigned long long>(view.Id), view.Width, view.Height, why.c_str());
|
||||
return false;
|
||||
}
|
||||
target.owner = owner;
|
||||
found = m_sharedImagePresentTargets.emplace(view.Id, Move(target)).first;
|
||||
}
|
||||
const VkTextureManager::ImportedSharedImage& destination = found->second.image;
|
||||
|
||||
// The frame's draws reach the queue first; the copy below is a later submission that the
|
||||
// readback barrier orders behind them.
|
||||
if (HasPendingRecordedWork() && !FlushPendingCommands()) {
|
||||
MGLOG_E("Magma: shared image %llu: the frame's recorded work could not be submitted",
|
||||
static_cast<unsigned long long>(view.Id));
|
||||
return false;
|
||||
}
|
||||
MG_Pipe::MGPFramebufferState defaultFramebuffer{};
|
||||
defaultFramebuffer.IsDefault = 1;
|
||||
const MG_Pipe::MGPSurface noSurface{};
|
||||
WireImage source = ResolveWireImage(defaultFramebuffer, noSurface, VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
/*isWriteTarget=*/false);
|
||||
if (source.image == VK_NULL_HANDLE || source.extent.width == 0 || source.extent.height == 0) {
|
||||
MGLOG_E_ONCE("Magma: shared image %llu: no default framebuffer is current; the present is declined",
|
||||
static_cast<unsigned long long>(view.Id));
|
||||
return false;
|
||||
}
|
||||
if (!m_swapchainObject.IsImageContentDefined(source.swapchainImageIndex)) {
|
||||
MGLOG_W_ONCE("Magma: shared image present: no write is recorded for image %u of the default "
|
||||
"framebuffer; its raw pixels are copied",
|
||||
source.swapchainImageIndex);
|
||||
}
|
||||
|
||||
const Bool sameExtent = source.extent.width == view.Width && source.extent.height == view.Height;
|
||||
const Bool halfTurn = preTransform == VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
|
||||
const Bool copy = sameExtent && !halfTurn && SharedImageCopiesAsIs(source.format, destination.format);
|
||||
VkFormatProperties sourceProperties{};
|
||||
vkGetPhysicalDeviceFormatProperties(m_physicalDevice.handle, source.format, &sourceProperties);
|
||||
if (!copy) {
|
||||
if ((sourceProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_SRC_BIT) == 0 ||
|
||||
(destination.features & VK_FORMAT_FEATURE_BLIT_DST_BIT) == 0) {
|
||||
MGLOG_E_ONCE("Magma: shared image %llu: a %ux%u format-%d default framebuffer needs a blit into "
|
||||
"the %ux%u format-%d image, which this device cannot do; the present is declined",
|
||||
static_cast<unsigned long long>(view.Id), source.extent.width, source.extent.height,
|
||||
static_cast<Int>(source.format), view.Width, view.Height,
|
||||
static_cast<Int>(destination.format));
|
||||
return false;
|
||||
}
|
||||
if (vkuFormatIsSRGB(source.format) && !vkuFormatIsSRGB(destination.format)) {
|
||||
MGLOG_W_ONCE("Magma: shared image present: the sRGB default framebuffer is blitted into a UNORM "
|
||||
"image, which stores linear values");
|
||||
}
|
||||
}
|
||||
|
||||
m_textureManager->FlushPendingUploads();
|
||||
RecordWireReadbackWriteBarrier();
|
||||
TransitionWireImage(source, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
auto& frame = m_frameContext.GetCurrent();
|
||||
|
||||
VkImageMemoryBarrier acquire{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
|
||||
acquire.srcAccessMask = 0;
|
||||
acquire.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
acquire.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
acquire.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
acquire.srcQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
|
||||
acquire.dstQueueFamilyIndex = destination.queueFamily;
|
||||
acquire.image = destination.image;
|
||||
acquire.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
vkCmdPipelineBarrier(frame.commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
|
||||
0, nullptr, 0, nullptr, 1, &acquire);
|
||||
|
||||
const VkImageSubresourceLayers layers{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
if (copy) {
|
||||
VkImageCopy region{};
|
||||
region.srcSubresource = layers;
|
||||
region.dstSubresource = layers;
|
||||
region.extent = {view.Width, view.Height, 1};
|
||||
vkCmdCopyImage(frame.commandBuffer, source.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, destination.image,
|
||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||
} else {
|
||||
// Identity: row 0 is already the top. 180: the stored image is the frame turned half
|
||||
// round, so the destination rectangle runs backwards on both axes.
|
||||
const Int32 width = static_cast<Int32>(view.Width);
|
||||
const Int32 height = static_cast<Int32>(view.Height);
|
||||
VkImageBlit region{};
|
||||
region.srcSubresource = layers;
|
||||
region.srcOffsets[1] = {static_cast<Int32>(source.extent.width), static_cast<Int32>(source.extent.height),
|
||||
1};
|
||||
region.dstSubresource = layers;
|
||||
region.dstOffsets[0] = halfTurn ? VkOffset3D{width, height, 0} : VkOffset3D{0, 0, 0};
|
||||
region.dstOffsets[1] = halfTurn ? VkOffset3D{0, 0, 1} : VkOffset3D{width, height, 1};
|
||||
const VkFilter filter =
|
||||
!sameExtent && (sourceProperties.optimalTilingFeatures &
|
||||
VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) != 0
|
||||
? VK_FILTER_LINEAR
|
||||
: VK_FILTER_NEAREST;
|
||||
vkCmdBlitImage(frame.commandBuffer, source.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, destination.image,
|
||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion, filter);
|
||||
}
|
||||
|
||||
VkImageMemoryBarrier release{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
|
||||
release.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
release.dstAccessMask = 0;
|
||||
release.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
release.newLayout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
release.srcQueueFamilyIndex = destination.queueFamily;
|
||||
release.dstQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
|
||||
release.image = destination.image;
|
||||
release.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
vkCmdPipelineBarrier(frame.commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||||
0, 0, nullptr, 0, nullptr, 1, &release);
|
||||
|
||||
// The client attaches the buffer to its surface as soon as this returns, and nothing else
|
||||
// orders the compositor's read after the copy: wait for it here.
|
||||
if (!SubmitReadbackCommandsAndWait(frame)) {
|
||||
MGLOG_E("Magma: shared image %llu: the copy could not be submitted or waited for",
|
||||
static_cast<unsigned long long>(view.Id));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // MOBILEGL_BUILD_DISAGGREGATED
|
||||
Reference in New Issue
Block a user