mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-20 00:11:56 +09:00
[Feature] (SharedImage): sample NV12/P010 dma-bufs through GL_OES_EGL_image_external on both backends, copying foreign buffers into server YUV images
This commit is contained in:
@@ -2529,6 +2529,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
return SamplerNumericDomain::UnsignedInteger;
|
||||
case GL_SAMPLER_1D:
|
||||
case GL_SAMPLER_2D:
|
||||
case GL_SAMPLER_EXTERNAL_OES:
|
||||
case GL_SAMPLER_3D:
|
||||
case GL_SAMPLER_CUBE:
|
||||
case GL_SAMPLER_2D_RECT:
|
||||
@@ -2719,6 +2720,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
case GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY:
|
||||
return TextureTarget::TextureCubeMapArray;
|
||||
case GL_SAMPLER_2D:
|
||||
case GL_SAMPLER_EXTERNAL_OES: // a 2D texture below the frontend
|
||||
case GL_SAMPLER_2D_SHADOW:
|
||||
case GL_INT_SAMPLER_2D:
|
||||
case GL_UNSIGNED_INT_SAMPLER_2D:
|
||||
|
||||
@@ -3198,7 +3198,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
const auto* resource = m_textureManager->SyncTextureResourceByHandle(handle, false, storage);
|
||||
if (resource == nullptr) return false;
|
||||
// Another session writes it: the renderer acquires it before this use.
|
||||
if (resource->sharedImageId != 0) m_textureManager->NoteSharedImageUse(*resource);
|
||||
if (resource->sharedImageId != 0 || resource->yuvImageId != 0)
|
||||
m_textureManager->NoteSharedImageUse(*resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2115,9 +2115,41 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
Bool VkTextureManager::SyncWireTextureShape(const MG_Pipe::MGPipeResourceRecord& record,
|
||||
TextureResource& resource, Bool requireStorage) {
|
||||
const MG_Pipe::MGPResourceDesc& desc = record.Desc;
|
||||
// A YUV shared image is converted INTO the texture's own RGBA8 storage, which the rest of
|
||||
// this function defines as for any texture; the image is only named here, and the
|
||||
// renderer converts it at its first use in each frame (WireYuvImage.inc).
|
||||
if (record.SharedImageId != resource.yuvImageId && !m_syncingYuvTextureStorage) {
|
||||
// Respecified away from (or to another) YUV image: its source goes with it, once the
|
||||
// GPU is done with it (a released resource's Reset is deferred).
|
||||
if (resource.yuvSource != nullptr) {
|
||||
TextureResource retired;
|
||||
retired.yuvSource = std::move(resource.yuvSource);
|
||||
DeferResourceRelease(Move(retired));
|
||||
}
|
||||
resource.yuvImageId = 0;
|
||||
resource.yuvOwner.reset();
|
||||
resource.yuvSource.reset();
|
||||
resource.yuvConvertedFrame = 0;
|
||||
}
|
||||
if (record.SharedImageId != 0 && !m_syncingYuvTextureStorage) {
|
||||
const auto yuv = MG_Remote::Server::SharedImages::Find(record.SharedImageId);
|
||||
if (yuv != nullptr && MG_Remote::Server::SharedImages::FourccIsYuv(yuv->Fourcc)) {
|
||||
m_syncingYuvTextureStorage = true;
|
||||
const Bool defined = SyncWireTextureShape(record, resource, requireStorage);
|
||||
m_syncingYuvTextureStorage = false;
|
||||
if (!defined) return false;
|
||||
if (resource.yuvImageId != record.SharedImageId) {
|
||||
resource.yuvImageId = record.SharedImageId;
|
||||
resource.yuvOwner = yuv;
|
||||
resource.yuvSource.reset();
|
||||
resource.yuvConvertedFrame = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// 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) {
|
||||
if (record.SharedImageId != 0 && !m_syncingYuvTextureStorage) {
|
||||
const SharedImageBind bound = SyncWireSharedImage(record, resource, requireStorage);
|
||||
if (bound != SharedImageBind::Unresolved) return bound == SharedImageBind::Bound;
|
||||
}
|
||||
@@ -3057,7 +3089,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
handle.Slot, handle.Gen);
|
||||
return nullptr;
|
||||
}
|
||||
resource.sharedImageKey = resource.sharedImageId != 0 ? key : 0;
|
||||
resource.sharedImageKey = resource.sharedImageId != 0 || resource.yuvImageId != 0 ? key : 0;
|
||||
resource.syncedWireSerial = record.Serial;
|
||||
return &resource;
|
||||
}
|
||||
@@ -3774,7 +3806,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
resource.sampledView == VK_NULL_HANDLE &&
|
||||
resource.perMipViews.empty() && resource.perMipSampledViews.empty() &&
|
||||
resource.attachmentViews.empty() && resource.alternateSampledViews.empty() &&
|
||||
resource.storageImageViews.empty()) {
|
||||
resource.storageImageViews.empty()
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
&& resource.yuvSource == nullptr
|
||||
#endif
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -372,6 +372,15 @@ public:
|
||||
Bool sharedImageHeld = false;
|
||||
Uint64 sharedImageGeneration = 0;
|
||||
Uint64 sharedImageKey = 0;
|
||||
// A YUV shared image (NV12/P010) is never the texture's image: the texture keeps its own
|
||||
// RGBA8 level 0, into which the renderer converts the image at its first use in each frame
|
||||
// (WireYuvImage.inc) - `yuvSource` being that conversion's import of the buffer, made on
|
||||
// first use, and `yuvConvertedFrame` the frame it last ran in. `yuvOwner` holds the
|
||||
// registry's image for as long as the texture names it.
|
||||
Uint64 yuvImageId = 0;
|
||||
SharedPtr<const void> yuvOwner;
|
||||
SharedPtr<void> yuvSource;
|
||||
Uint64 yuvConvertedFrame = 0;
|
||||
#endif
|
||||
|
||||
TextureResource() = default;
|
||||
@@ -415,6 +424,10 @@ public:
|
||||
std::swap(this->sharedImageHeld, that.sharedImageHeld);
|
||||
std::swap(this->sharedImageGeneration, that.sharedImageGeneration);
|
||||
std::swap(this->sharedImageKey, that.sharedImageKey);
|
||||
std::swap(this->yuvImageId, that.yuvImageId);
|
||||
std::swap(this->yuvOwner, that.yuvOwner);
|
||||
std::swap(this->yuvSource, that.yuvSource);
|
||||
std::swap(this->yuvConvertedFrame, that.yuvConvertedFrame);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -465,6 +478,10 @@ public:
|
||||
sharedImageHeld = false;
|
||||
sharedImageGeneration = 0;
|
||||
sharedImageKey = 0;
|
||||
yuvImageId = 0;
|
||||
yuvOwner.reset();
|
||||
yuvSource.reset();
|
||||
yuvConvertedFrame = 0;
|
||||
#endif
|
||||
fullView = VK_NULL_HANDLE;
|
||||
sampledView = VK_NULL_HANDLE;
|
||||
@@ -882,6 +899,8 @@ private:
|
||||
// Active only between BeginDrawSyncScope/EndDrawSyncScope; identities of
|
||||
// textures already fully synced in the current draw (small N -> flat scan).
|
||||
Bool m_drawSyncScopeActive = false;
|
||||
// SyncWireTextureShape defining the RGBA8 storage a YUV shared image is converted into.
|
||||
Bool m_syncingYuvTextureStorage = false;
|
||||
// Per-draw sync memo: the identity plus the resolved resource pointer. The pointer is stable
|
||||
// across rehash in the node-based m_textureResources and stays valid for the draw (a texture
|
||||
// synced this draw is alive and is not erased mid-draw), so a repeat sync of the same texture
|
||||
|
||||
@@ -3525,6 +3525,7 @@ void main() {
|
||||
CollectWireObjects(m_submitCounter, true);
|
||||
ClearAllWireDrawPassCaches();
|
||||
DestroyWireColorBlitResources();
|
||||
DestroyWireYuvResources();
|
||||
DestroyWireDepthMipmapResources();
|
||||
DestroyWireMultisampleResolveResources();
|
||||
DestroySharedImagePresentTargets(false);
|
||||
@@ -8107,6 +8108,8 @@ void main() {
|
||||
#include "WireDraw.inc"
|
||||
// AFTER WireFramebuffer.inc: the shared-image present is that file's readback, into an image.
|
||||
#include "WireSharedImage.inc"
|
||||
// AFTER WireSharedImage.inc: a YUV image is that file's reading side, converted on the way.
|
||||
#include "WireYuvImage.inc"
|
||||
|
||||
void VulkanRenderer::Clear(GLbitfield mask) {
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
@@ -16032,6 +16035,23 @@ void main() {
|
||||
}
|
||||
}
|
||||
|
||||
// YUV shared images (WireYuvImage.inc) are sampled through a VkSamplerYcbcrConversion, a
|
||||
// feature of its own; asked only where AHardwareBuffers are imported at all.
|
||||
m_samplerYcbcrConversion = false;
|
||||
VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcrFeatures{};
|
||||
ycbcrFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
|
||||
if (m_wireAhbImport && getPhysicalDeviceFeatures2 != nullptr) {
|
||||
VkPhysicalDeviceFeatures2 featureQuery{};
|
||||
featureQuery.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
|
||||
featureQuery.pNext = &ycbcrFeatures;
|
||||
getPhysicalDeviceFeatures2(m_physicalDevice.handle, &featureQuery);
|
||||
if (ycbcrFeatures.samplerYcbcrConversion == VK_TRUE) {
|
||||
ycbcrFeatures.pNext = const_cast<void*>(deviceCreateInfo.pNext);
|
||||
deviceCreateInfo.pNext = &ycbcrFeatures;
|
||||
m_samplerYcbcrConversion = true;
|
||||
}
|
||||
}
|
||||
|
||||
// VK_EXT_multi_draw: tier 1 of the multi-draw dispatch - one vkCmdDrawMulti(Indexed)EXT
|
||||
// for a whole glMultiDraw* batch (VkMultiDrawIndexedInfoEXT carries per-draw
|
||||
// firstIndex/indexCount/vertexOffset, so glMultiDrawElementsBaseVertex fits natively).
|
||||
|
||||
@@ -717,6 +717,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
// one entry point the import needs (PFN_vkGetAndroidHardwareBufferPropertiesANDROID).
|
||||
Bool m_wireAhbImport = false;
|
||||
void* m_wireGetAhbProperties = nullptr;
|
||||
// YUV shared images (WireYuvImage.inc): the device enabled samplerYcbcrConversion, and the
|
||||
// conversion pipelines made so far (one per buffer format and colour conversion).
|
||||
Bool m_samplerYcbcrConversion = false;
|
||||
struct WireYuvResources;
|
||||
WireYuvResources* m_wireYuvResources = nullptr;
|
||||
void DestroyWireYuvResources();
|
||||
// A YUV texture's first use in a frame converts its image (yuvConvertedFrame against this,
|
||||
// which every frame boundary - present, publishing flush, native fence - moves).
|
||||
Uint64 m_yuvFrameSerial = 1;
|
||||
Bool ConvertYuvSharedImage(VkTextureManager::TextureResource& resource);
|
||||
// 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 a later present once `lastSubmit`, its last copy, has completed.
|
||||
|
||||
@@ -499,6 +499,11 @@
|
||||
|
||||
void VulkanRenderer::AcquireSharedImage(VkTextureManager::TextureResource& resource) {
|
||||
namespace SI = MG_Remote::Server::SharedImages;
|
||||
// A YUV image is converted into the texture's own image instead (WireYuvImage.inc).
|
||||
if (resource.yuvImageId != 0) {
|
||||
(void)ConvertYuvSharedImage(resource);
|
||||
return;
|
||||
}
|
||||
if (resource.sharedImageId == 0 || resource.image == VK_NULL_HANDLE || resource.sharedImageOwner == nullptr)
|
||||
return;
|
||||
const auto image = std::static_pointer_cast<const SI::Image>(resource.sharedImageOwner);
|
||||
@@ -567,6 +572,8 @@
|
||||
// family in the frame's last recording, and that recording's submission signals the semaphore
|
||||
// whose sync_file is published as the frame's read fence for all of them at once.
|
||||
void VulkanRenderer::ReleaseHeldSharedImages() {
|
||||
// A frame boundary: a YUV texture's next use converts its image again.
|
||||
++m_yuvFrameSerial;
|
||||
if (m_sharedImagesHeld.empty()) return;
|
||||
const Uint32 family = static_cast<Uint32>(m_physicalDevice.queueFamilies.graphicsFamily);
|
||||
Vector<VkImageMemoryBarrier> releases;
|
||||
@@ -633,6 +640,9 @@
|
||||
// submission, and it is tracked like any other (the semaphore's reuse waits for it).
|
||||
Bool VulkanRenderer::ExportNativeFence(int* fence) {
|
||||
*fence = -1;
|
||||
// Every fence the application asks for ends one of its frames as far as a YUV texture is
|
||||
// concerned (an offscreen producer - a browser's GPU process - has no present to do it).
|
||||
++m_yuvFrameSerial;
|
||||
if (m_device == VK_NULL_HANDLE || m_graphicsQueue == VK_NULL_HANDLE || m_frameContext.GetFrameCount() == 0)
|
||||
return true; // nothing was ever submitted: nothing to fence
|
||||
// A session that writes images ends its frame here, as at a flush; a compositor that only
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 450
|
||||
// The image is bound through an immutable sampler that carries its VkSamplerYcbcrConversion, so
|
||||
// a plain sample already returns RGB.
|
||||
layout(set = 0, binding = 0) uniform sampler2D yuvImage;
|
||||
layout(location = 0) in vec2 texCoord;
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = vec4(textureLod(yuvImage, texCoord, 0.0).rgb, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#version 450
|
||||
// A YUV shared image converted into an RGBA texture (WireYuvImage.inc): one triangle over the
|
||||
// whole target, its first row at the image's first row.
|
||||
layout(location = 0) out vec2 texCoord;
|
||||
|
||||
void main() {
|
||||
const vec2 corners[3] = vec2[](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
|
||||
vec2 p = corners[gl_VertexIndex];
|
||||
gl_Position = vec4(p, 0.0, 1.0);
|
||||
texCoord = (p + 1.0) * 0.5;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// Generated from WireYuvConvert.vert / WireYuvConvert.frag (Vulkan 1.0 / SPIR-V 1.0) by
|
||||
// scripts/bake_internal_shaders.py, which runs MG_Test's BakedInternalShadersTest with the
|
||||
// IN-TREE glslang and splices the words it emits into the arrays below. Do not edit the words
|
||||
// by hand: MOBILEGL_BAKED_INTERNAL_SHADERS (the same test, run as a gate) recompiles every
|
||||
// source in its table and reds on the first word that disagrees.
|
||||
#pragma once
|
||||
|
||||
static constexpr Uint32 kWireYuvConvertVertexSpirv[] = {
|
||||
0x07230203u, 0x00010000u, 0x0008000bu, 0x00000031u, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu,
|
||||
0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u,
|
||||
0x0008000fu, 0x00000000u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00000015u, 0x00000020u, 0x0000002bu,
|
||||
0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00030005u,
|
||||
0x00000009u, 0x00000070u, 0x00060005u, 0x00000015u, 0x565f6c67u, 0x65747265u, 0x646e4978u, 0x00007865u,
|
||||
0x00050005u, 0x00000018u, 0x65646e69u, 0x6c626178u, 0x00000065u, 0x00060005u, 0x0000001eu, 0x505f6c67u,
|
||||
0x65567265u, 0x78657472u, 0x00000000u, 0x00060006u, 0x0000001eu, 0x00000000u, 0x505f6c67u, 0x7469736fu,
|
||||
0x006e6f69u, 0x00070006u, 0x0000001eu, 0x00000001u, 0x505f6c67u, 0x746e696fu, 0x657a6953u, 0x00000000u,
|
||||
0x00070006u, 0x0000001eu, 0x00000002u, 0x435f6c67u, 0x4470696cu, 0x61747369u, 0x0065636eu, 0x00070006u,
|
||||
0x0000001eu, 0x00000003u, 0x435f6c67u, 0x446c6c75u, 0x61747369u, 0x0065636eu, 0x00030005u, 0x00000020u,
|
||||
0x00000000u, 0x00050005u, 0x0000002bu, 0x43786574u, 0x64726f6fu, 0x00000000u, 0x00040047u, 0x00000015u,
|
||||
0x0000000bu, 0x0000002au, 0x00030047u, 0x0000001eu, 0x00000002u, 0x00050048u, 0x0000001eu, 0x00000000u,
|
||||
0x0000000bu, 0x00000000u, 0x00050048u, 0x0000001eu, 0x00000001u, 0x0000000bu, 0x00000001u, 0x00050048u,
|
||||
0x0000001eu, 0x00000002u, 0x0000000bu, 0x00000003u, 0x00050048u, 0x0000001eu, 0x00000003u, 0x0000000bu,
|
||||
0x00000004u, 0x00040047u, 0x0000002bu, 0x0000001eu, 0x00000000u, 0x00020013u, 0x00000002u, 0x00030021u,
|
||||
0x00000003u, 0x00000002u, 0x00030016u, 0x00000006u, 0x00000020u, 0x00040017u, 0x00000007u, 0x00000006u,
|
||||
0x00000002u, 0x00040020u, 0x00000008u, 0x00000007u, 0x00000007u, 0x00040015u, 0x0000000au, 0x00000020u,
|
||||
0x00000000u, 0x0004002bu, 0x0000000au, 0x0000000bu, 0x00000003u, 0x0004001cu, 0x0000000cu, 0x00000007u,
|
||||
0x0000000bu, 0x0004002bu, 0x00000006u, 0x0000000du, 0xbf800000u, 0x0005002cu, 0x00000007u, 0x0000000eu,
|
||||
0x0000000du, 0x0000000du, 0x0004002bu, 0x00000006u, 0x0000000fu, 0x40400000u, 0x0005002cu, 0x00000007u,
|
||||
0x00000010u, 0x0000000fu, 0x0000000du, 0x0005002cu, 0x00000007u, 0x00000011u, 0x0000000du, 0x0000000fu,
|
||||
0x0006002cu, 0x0000000cu, 0x00000012u, 0x0000000eu, 0x00000010u, 0x00000011u, 0x00040015u, 0x00000013u,
|
||||
0x00000020u, 0x00000001u, 0x00040020u, 0x00000014u, 0x00000001u, 0x00000013u, 0x0004003bu, 0x00000014u,
|
||||
0x00000015u, 0x00000001u, 0x00040020u, 0x00000017u, 0x00000007u, 0x0000000cu, 0x00040017u, 0x0000001bu,
|
||||
0x00000006u, 0x00000004u, 0x0004002bu, 0x0000000au, 0x0000001cu, 0x00000001u, 0x0004001cu, 0x0000001du,
|
||||
0x00000006u, 0x0000001cu, 0x0006001eu, 0x0000001eu, 0x0000001bu, 0x00000006u, 0x0000001du, 0x0000001du,
|
||||
0x00040020u, 0x0000001fu, 0x00000003u, 0x0000001eu, 0x0004003bu, 0x0000001fu, 0x00000020u, 0x00000003u,
|
||||
0x0004002bu, 0x00000013u, 0x00000021u, 0x00000000u, 0x0004002bu, 0x00000006u, 0x00000023u, 0x00000000u,
|
||||
0x0004002bu, 0x00000006u, 0x00000024u, 0x3f800000u, 0x00040020u, 0x00000028u, 0x00000003u, 0x0000001bu,
|
||||
0x00040020u, 0x0000002au, 0x00000003u, 0x00000007u, 0x0004003bu, 0x0000002au, 0x0000002bu, 0x00000003u,
|
||||
0x0004002bu, 0x00000006u, 0x0000002fu, 0x3f000000u, 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u,
|
||||
0x00000003u, 0x000200f8u, 0x00000005u, 0x0004003bu, 0x00000008u, 0x00000009u, 0x00000007u, 0x0004003bu,
|
||||
0x00000017u, 0x00000018u, 0x00000007u, 0x0004003du, 0x00000013u, 0x00000016u, 0x00000015u, 0x0003003eu,
|
||||
0x00000018u, 0x00000012u, 0x00050041u, 0x00000008u, 0x00000019u, 0x00000018u, 0x00000016u, 0x0004003du,
|
||||
0x00000007u, 0x0000001au, 0x00000019u, 0x0003003eu, 0x00000009u, 0x0000001au, 0x0004003du, 0x00000007u,
|
||||
0x00000022u, 0x00000009u, 0x00050051u, 0x00000006u, 0x00000025u, 0x00000022u, 0x00000000u, 0x00050051u,
|
||||
0x00000006u, 0x00000026u, 0x00000022u, 0x00000001u, 0x00070050u, 0x0000001bu, 0x00000027u, 0x00000025u,
|
||||
0x00000026u, 0x00000023u, 0x00000024u, 0x00050041u, 0x00000028u, 0x00000029u, 0x00000020u, 0x00000021u,
|
||||
0x0003003eu, 0x00000029u, 0x00000027u, 0x0004003du, 0x00000007u, 0x0000002cu, 0x00000009u, 0x00050050u,
|
||||
0x00000007u, 0x0000002du, 0x00000024u, 0x00000024u, 0x00050081u, 0x00000007u, 0x0000002eu, 0x0000002cu,
|
||||
0x0000002du, 0x0005008eu, 0x00000007u, 0x00000030u, 0x0000002eu, 0x0000002fu, 0x0003003eu, 0x0000002bu,
|
||||
0x00000030u, 0x000100fdu, 0x00010038u,
|
||||
};
|
||||
|
||||
static constexpr Uint32 kWireYuvConvertFragmentSpirv[] = {
|
||||
0x07230203u, 0x00010000u, 0x0008000bu, 0x0000001cu, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu,
|
||||
0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u,
|
||||
0x0007000fu, 0x00000004u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00000009u, 0x00000011u, 0x00030010u,
|
||||
0x00000004u, 0x00000007u, 0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, 0x00000004u, 0x6e69616du,
|
||||
0x00000000u, 0x00050005u, 0x00000009u, 0x4374756fu, 0x726f6c6fu, 0x00000000u, 0x00050005u, 0x0000000du,
|
||||
0x49767579u, 0x6567616du, 0x00000000u, 0x00050005u, 0x00000011u, 0x43786574u, 0x64726f6fu, 0x00000000u,
|
||||
0x00040047u, 0x00000009u, 0x0000001eu, 0x00000000u, 0x00040047u, 0x0000000du, 0x00000021u, 0x00000000u,
|
||||
0x00040047u, 0x0000000du, 0x00000022u, 0x00000000u, 0x00040047u, 0x00000011u, 0x0000001eu, 0x00000000u,
|
||||
0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00030016u, 0x00000006u, 0x00000020u,
|
||||
0x00040017u, 0x00000007u, 0x00000006u, 0x00000004u, 0x00040020u, 0x00000008u, 0x00000003u, 0x00000007u,
|
||||
0x0004003bu, 0x00000008u, 0x00000009u, 0x00000003u, 0x00090019u, 0x0000000au, 0x00000006u, 0x00000001u,
|
||||
0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x0003001bu, 0x0000000bu, 0x0000000au,
|
||||
0x00040020u, 0x0000000cu, 0x00000000u, 0x0000000bu, 0x0004003bu, 0x0000000cu, 0x0000000du, 0x00000000u,
|
||||
0x00040017u, 0x0000000fu, 0x00000006u, 0x00000002u, 0x00040020u, 0x00000010u, 0x00000001u, 0x0000000fu,
|
||||
0x0004003bu, 0x00000010u, 0x00000011u, 0x00000001u, 0x0004002bu, 0x00000006u, 0x00000013u, 0x00000000u,
|
||||
0x00040017u, 0x00000015u, 0x00000006u, 0x00000003u, 0x0004002bu, 0x00000006u, 0x00000017u, 0x3f800000u,
|
||||
0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u, 0x000200f8u, 0x00000005u, 0x0004003du,
|
||||
0x0000000bu, 0x0000000eu, 0x0000000du, 0x0004003du, 0x0000000fu, 0x00000012u, 0x00000011u, 0x00070058u,
|
||||
0x00000007u, 0x00000014u, 0x0000000eu, 0x00000012u, 0x00000002u, 0x00000013u, 0x0008004fu, 0x00000015u,
|
||||
0x00000016u, 0x00000014u, 0x00000014u, 0x00000000u, 0x00000001u, 0x00000002u, 0x00050051u, 0x00000006u,
|
||||
0x00000018u, 0x00000016u, 0x00000000u, 0x00050051u, 0x00000006u, 0x00000019u, 0x00000016u, 0x00000001u,
|
||||
0x00050051u, 0x00000006u, 0x0000001au, 0x00000016u, 0x00000002u, 0x00070050u, 0x00000007u, 0x0000001bu,
|
||||
0x00000018u, 0x00000019u, 0x0000001au, 0x00000017u, 0x0003003eu, 0x00000009u, 0x0000001bu, 0x000100fdu,
|
||||
0x00010038u,
|
||||
};
|
||||
@@ -0,0 +1,513 @@
|
||||
// Included by VulkanRenderer.cpp inside the backend namespace, after WireSharedImage.inc.
|
||||
//
|
||||
// YUV SHARED IMAGES (NV12, P010; MG_Remote/Server/SharedImageYuv.h). A YUV image is never a
|
||||
// texture's image: the texture keeps its own RGBA8 level 0 (VkTextureManager::SyncWireTextureShape)
|
||||
// and, at its first use in each frame, the image is CONVERTED into it here - the image's
|
||||
// AHardwareBuffer imported with its external format, sampled through an immutable sampler that
|
||||
// carries a VkSamplerYcbcrConversion built from the import's colour hints, one triangle over the
|
||||
// target. Zero CPU copies for an image this server allocated; a foreign one (a video decoder's
|
||||
// buffer) is first brought up to date by the registry's labelled CPU copy (RefreshForeign).
|
||||
//
|
||||
// Why converted rather than sampled in place: a Y'CbCr conversion is an immutable sampler, which
|
||||
// lives in the descriptor set layout and therefore in every application pipeline that samples the
|
||||
// texture. A conversion pass keeps those pipelines exactly what they are for an RGBA texture, at
|
||||
// the cost of one small pass per new frame of the video.
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
#include "WireYuvConvertSpirv.h"
|
||||
|
||||
struct VulkanRenderer::WireYuvResources {
|
||||
PFN_vkCreateSamplerYcbcrConversion createConversion = nullptr;
|
||||
PFN_vkDestroySamplerYcbcrConversion destroyConversion = nullptr;
|
||||
VkShaderModule vertex = VK_NULL_HANDLE, fragment = VK_NULL_HANDLE;
|
||||
// One per (buffer format, conversion, target format). Never dropped before shutdown: a
|
||||
// device sees a handful of them.
|
||||
struct Pipeline {
|
||||
Uint64 externalFormat = 0;
|
||||
VkFormat format = VK_FORMAT_UNDEFINED;
|
||||
VkSamplerYcbcrModelConversion model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
|
||||
VkSamplerYcbcrRange range = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
|
||||
VkComponentMapping components{};
|
||||
VkChromaLocation xOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
|
||||
VkChromaLocation yOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
|
||||
VkFilter chromaFilter = VK_FILTER_NEAREST;
|
||||
VkFormat target = VK_FORMAT_UNDEFINED;
|
||||
VkSamplerYcbcrConversion conversion = VK_NULL_HANDLE;
|
||||
VkSampler sampler = VK_NULL_HANDLE;
|
||||
VkDescriptorSetLayout descriptorLayout = VK_NULL_HANDLE;
|
||||
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
|
||||
VkRenderPass renderPass = VK_NULL_HANDLE;
|
||||
VkPipeline pipeline = VK_NULL_HANDLE;
|
||||
};
|
||||
Vector<Pipeline> pipelines;
|
||||
};
|
||||
|
||||
namespace {
|
||||
// One texture's import of its YUV buffer (TextureResource::yuvSource): the image on the
|
||||
// buffer's memory and a view through the conversion of `pipeline`. Destroyed by the
|
||||
// resource's (deferred) Reset, so never under a submission still reading it.
|
||||
struct WireYuvSource {
|
||||
VkDevice device = VK_NULL_HANDLE;
|
||||
VkImage image = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
VkImageView view = VK_NULL_HANDLE;
|
||||
SizeT pipeline = 0;
|
||||
MG_Remote::Server::SharedImages::Yuv::Hints hints;
|
||||
~WireYuvSource() {
|
||||
if (device == VK_NULL_HANDLE) return;
|
||||
if (view != VK_NULL_HANDLE) vkDestroyImageView(device, view, nullptr);
|
||||
if (image != VK_NULL_HANDLE) vkDestroyImage(device, image, nullptr);
|
||||
if (memory != VK_NULL_HANDLE) vkFreeMemory(device, memory, nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
VkSamplerYcbcrModelConversion YcbcrModelFor(const MG_Remote::Server::SharedImages::Yuv::Hints& hints) {
|
||||
namespace Yuv = MG_Remote::Server::SharedImages::Yuv;
|
||||
switch (hints.EffectiveColorSpace()) {
|
||||
case Yuv::kEglItuRec709:
|
||||
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
|
||||
case Yuv::kEglItuRec2020:
|
||||
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020;
|
||||
default:
|
||||
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void VulkanRenderer::DestroyWireYuvResources() {
|
||||
if (!m_wireYuvResources) return;
|
||||
auto& cache = *m_wireYuvResources;
|
||||
for (const auto& entry : cache.pipelines) {
|
||||
vkDestroyPipeline(m_device, entry.pipeline, nullptr);
|
||||
vkDestroyRenderPass(m_device, entry.renderPass, nullptr);
|
||||
vkDestroyPipelineLayout(m_device, entry.pipelineLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(m_device, entry.descriptorLayout, nullptr);
|
||||
vkDestroySampler(m_device, entry.sampler, nullptr);
|
||||
if (cache.destroyConversion != nullptr) cache.destroyConversion(m_device, entry.conversion, nullptr);
|
||||
}
|
||||
vkDestroyShaderModule(m_device, cache.vertex, nullptr);
|
||||
vkDestroyShaderModule(m_device, cache.fragment, nullptr);
|
||||
delete m_wireYuvResources;
|
||||
m_wireYuvResources = nullptr;
|
||||
}
|
||||
|
||||
Bool VulkanRenderer::ConvertYuvSharedImage(VkTextureManager::TextureResource& resource) {
|
||||
#if defined(__ANDROID__)
|
||||
namespace SI = MG_Remote::Server::SharedImages;
|
||||
if (resource.yuvConvertedFrame == m_yuvFrameSerial) return true;
|
||||
const auto image = std::static_pointer_cast<const SI::Image>(resource.yuvOwner);
|
||||
if (image == nullptr || image->Native == nullptr || resource.image == VK_NULL_HANDLE) return false;
|
||||
if (!m_samplerYcbcrConversion) {
|
||||
MGLOG_E_ONCE("Magma: YUV shared image %llu cannot be sampled: the device has no samplerYcbcrConversion",
|
||||
static_cast<unsigned long long>(image->Id));
|
||||
return false;
|
||||
}
|
||||
// Whatever happens below, this frame does not try again.
|
||||
resource.yuvConvertedFrame = m_yuvFrameSerial;
|
||||
// THE CPU-COPY FALLBACK, for a buffer this server did not allocate (SharedImageRegistry.h).
|
||||
String why;
|
||||
if (image->IsForeign() && !SI::RefreshForeign(*image, why)) {
|
||||
MGLOG_E_ONCE("Magma: foreign YUV image %llu could not be refreshed: %s",
|
||||
static_cast<unsigned long long>(image->Id), why.c_str());
|
||||
}
|
||||
const SI::Yuv::Hints hints = SI::GetYuvHints(*image);
|
||||
|
||||
if (!m_wireYuvResources) {
|
||||
m_wireYuvResources = new WireYuvResources;
|
||||
auto& cache = *m_wireYuvResources;
|
||||
cache.createConversion = reinterpret_cast<PFN_vkCreateSamplerYcbcrConversion>(
|
||||
vkGetDeviceProcAddr(m_device, "vkCreateSamplerYcbcrConversion"));
|
||||
if (cache.createConversion == nullptr)
|
||||
cache.createConversion = reinterpret_cast<PFN_vkCreateSamplerYcbcrConversion>(
|
||||
vkGetDeviceProcAddr(m_device, "vkCreateSamplerYcbcrConversionKHR"));
|
||||
cache.destroyConversion = reinterpret_cast<PFN_vkDestroySamplerYcbcrConversion>(
|
||||
vkGetDeviceProcAddr(m_device, "vkDestroySamplerYcbcrConversion"));
|
||||
if (cache.destroyConversion == nullptr)
|
||||
cache.destroyConversion = reinterpret_cast<PFN_vkDestroySamplerYcbcrConversion>(
|
||||
vkGetDeviceProcAddr(m_device, "vkDestroySamplerYcbcrConversionKHR"));
|
||||
const auto shader = [&](const Uint32* code, SizeT bytes, VkShaderModule& module) {
|
||||
VkShaderModuleCreateInfo info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
|
||||
info.codeSize = bytes;
|
||||
info.pCode = code;
|
||||
VK_VERIFY(vkCreateShaderModule(m_device, &info, nullptr, &module));
|
||||
};
|
||||
shader(kWireYuvConvertVertexSpirv, sizeof(kWireYuvConvertVertexSpirv), cache.vertex);
|
||||
shader(kWireYuvConvertFragmentSpirv, sizeof(kWireYuvConvertFragmentSpirv), cache.fragment);
|
||||
}
|
||||
auto& cache = *m_wireYuvResources;
|
||||
if (cache.createConversion == nullptr || cache.destroyConversion == nullptr) {
|
||||
MGLOG_E_ONCE("Magma: vkCreateSamplerYcbcrConversion is not available; YUV images cannot be sampled");
|
||||
return false;
|
||||
}
|
||||
|
||||
// ---- the texture's import of the buffer, and the pipeline its conversion needs ----
|
||||
auto* source = static_cast<WireYuvSource*>(resource.yuvSource.get());
|
||||
if (source != nullptr && !(source->hints == hints)) {
|
||||
// Re-imported with other colour hints: the conversion (and so the view) changes.
|
||||
auto retired = MakeUnique<VkTextureManager::TextureResource>();
|
||||
retired->yuvSource = std::move(resource.yuvSource);
|
||||
WireRetiredObjects objects{};
|
||||
objects.textures.push_back(Move(retired));
|
||||
RetireWireObjects(Move(objects));
|
||||
source = nullptr;
|
||||
}
|
||||
if (source == nullptr) {
|
||||
WireAhbImport ctx;
|
||||
if (!GetWireAhbImport(ctx)) return false;
|
||||
auto* ahb = static_cast<AHardwareBuffer*>(image->Native);
|
||||
const auto getProperties =
|
||||
reinterpret_cast<PFN_vkGetAndroidHardwareBufferPropertiesANDROID>(ctx.getAhbProperties);
|
||||
VkAndroidHardwareBufferFormatPropertiesANDROID format{
|
||||
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID};
|
||||
VkAndroidHardwareBufferPropertiesANDROID properties{
|
||||
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID};
|
||||
properties.pNext = &format;
|
||||
if (getProperties(m_device, ahb, &properties) != VK_SUCCESS) {
|
||||
MGLOG_E_ONCE("Magma: YUV image %llu: vkGetAndroidHardwareBufferPropertiesANDROID failed",
|
||||
static_cast<unsigned long long>(image->Id));
|
||||
return false;
|
||||
}
|
||||
// The conversion: the import's hints where it gave them, else what the buffer suggests.
|
||||
WireYuvResources::Pipeline key;
|
||||
key.externalFormat = format.format == VK_FORMAT_UNDEFINED ? format.externalFormat : 0;
|
||||
key.format = format.format;
|
||||
key.model = hints.ColorSpace != 0 ? YcbcrModelFor(hints) : format.suggestedYcbcrModel;
|
||||
key.range = hints.Range != 0 ? (hints.FullRange() ? VK_SAMPLER_YCBCR_RANGE_ITU_FULL
|
||||
: VK_SAMPLER_YCBCR_RANGE_ITU_NARROW)
|
||||
: format.suggestedYcbcrRange;
|
||||
key.components = format.samplerYcbcrConversionComponents;
|
||||
const auto chroma = [&format](Uint32 siting, VkChromaLocation suggested) {
|
||||
const Bool midpoint = siting == SI::Yuv::kEglYuvChromaSiting0_5;
|
||||
const Bool cosited = siting == SI::Yuv::kEglYuvChromaSiting0;
|
||||
if (midpoint && (format.formatFeatures & VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT))
|
||||
return VK_CHROMA_LOCATION_MIDPOINT;
|
||||
if (cosited && (format.formatFeatures & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT))
|
||||
return VK_CHROMA_LOCATION_COSITED_EVEN;
|
||||
return suggested;
|
||||
};
|
||||
key.xOffset = chroma(hints.SitingX, format.suggestedXChromaOffset);
|
||||
key.yOffset = chroma(hints.SitingY, format.suggestedYChromaOffset);
|
||||
key.chromaFilter =
|
||||
(format.formatFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT) != 0
|
||||
? VK_FILTER_LINEAR
|
||||
: VK_FILTER_NEAREST;
|
||||
key.target = resource.format;
|
||||
SizeT index = cache.pipelines.size();
|
||||
for (SizeT i = 0; i < cache.pipelines.size(); ++i) {
|
||||
const auto& p = cache.pipelines[i];
|
||||
if (p.externalFormat == key.externalFormat && p.format == key.format && p.model == key.model &&
|
||||
p.range == key.range && p.components.r == key.components.r &&
|
||||
p.components.g == key.components.g && p.components.b == key.components.b &&
|
||||
p.components.a == key.components.a && p.xOffset == key.xOffset && p.yOffset == key.yOffset &&
|
||||
p.chromaFilter == key.chromaFilter && p.target == key.target) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index == cache.pipelines.size()) {
|
||||
VkExternalFormatANDROID external{VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID};
|
||||
external.externalFormat = key.externalFormat;
|
||||
VkSamplerYcbcrConversionCreateInfo conversion{VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO};
|
||||
conversion.pNext = key.externalFormat != 0 ? &external : nullptr;
|
||||
conversion.format = key.format;
|
||||
conversion.ycbcrModel = key.model;
|
||||
conversion.ycbcrRange = key.range;
|
||||
conversion.components = key.components;
|
||||
conversion.xChromaOffset = key.xOffset;
|
||||
conversion.yChromaOffset = key.yOffset;
|
||||
conversion.chromaFilter = key.chromaFilter;
|
||||
conversion.forceExplicitReconstruction = VK_FALSE;
|
||||
if (cache.createConversion(m_device, &conversion, nullptr, &key.conversion) != VK_SUCCESS) {
|
||||
MGLOG_E_ONCE("Magma: YUV image %llu: vkCreateSamplerYcbcrConversion failed",
|
||||
static_cast<unsigned long long>(image->Id));
|
||||
return false;
|
||||
}
|
||||
VkSamplerYcbcrConversionInfo conversionInfo{VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO};
|
||||
conversionInfo.conversion = key.conversion;
|
||||
VkSamplerCreateInfo sampler{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
|
||||
sampler.pNext = &conversionInfo;
|
||||
// Without separate reconstruction filters the sampler filters as the conversion does.
|
||||
sampler.magFilter = sampler.minFilter = key.chromaFilter;
|
||||
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
|
||||
sampler.addressModeU = sampler.addressModeV = sampler.addressModeW =
|
||||
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
sampler.maxLod = 0.0f;
|
||||
VK_VERIFY(vkCreateSampler(m_device, &sampler, nullptr, &key.sampler));
|
||||
const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT, &key.sampler};
|
||||
VkDescriptorSetLayoutCreateInfo layout{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
||||
layout.bindingCount = 1;
|
||||
layout.pBindings = &binding;
|
||||
VK_VERIFY(vkCreateDescriptorSetLayout(m_device, &layout, nullptr, &key.descriptorLayout));
|
||||
VkPipelineLayoutCreateInfo pipelineLayout{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
||||
pipelineLayout.setLayoutCount = 1;
|
||||
pipelineLayout.pSetLayouts = &key.descriptorLayout;
|
||||
VK_VERIFY(vkCreatePipelineLayout(m_device, &pipelineLayout, nullptr, &key.pipelineLayout));
|
||||
// Every texel of the target is written: nothing to load.
|
||||
VkAttachmentDescription attachment{};
|
||||
attachment.format = key.target;
|
||||
attachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
attachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
attachment.initialLayout = attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
const VkAttachmentReference color{0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
|
||||
VkSubpassDescription subpass{};
|
||||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||
subpass.colorAttachmentCount = 1;
|
||||
subpass.pColorAttachments = &color;
|
||||
VkRenderPassCreateInfo pass{VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO};
|
||||
pass.attachmentCount = 1;
|
||||
pass.pAttachments = &attachment;
|
||||
pass.subpassCount = 1;
|
||||
pass.pSubpasses = &subpass;
|
||||
VK_VERIFY(vkCreateRenderPass(m_device, &pass, nullptr, &key.renderPass));
|
||||
VkPipelineShaderStageCreateInfo stages[2]{};
|
||||
for (auto& stage : stages) {
|
||||
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
stage.pName = "main";
|
||||
}
|
||||
stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
stages[0].module = cache.vertex;
|
||||
stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
stages[1].module = cache.fragment;
|
||||
const VkPipelineVertexInputStateCreateInfo vertex{
|
||||
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
|
||||
VkPipelineInputAssemblyStateCreateInfo assembly{
|
||||
VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
|
||||
assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
VkPipelineViewportStateCreateInfo viewport{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
|
||||
viewport.viewportCount = viewport.scissorCount = 1;
|
||||
VkPipelineRasterizationStateCreateInfo raster{
|
||||
VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO};
|
||||
raster.polygonMode = VK_POLYGON_MODE_FILL;
|
||||
raster.cullMode = VK_CULL_MODE_NONE;
|
||||
raster.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
||||
raster.lineWidth = 1.0f;
|
||||
VkPipelineMultisampleStateCreateInfo samples{
|
||||
VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO};
|
||||
samples.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
VkPipelineColorBlendAttachmentState colorMask{};
|
||||
colorMask.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
|
||||
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
||||
VkPipelineColorBlendStateCreateInfo blend{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
|
||||
blend.attachmentCount = 1;
|
||||
blend.pAttachments = &colorMask;
|
||||
const VkDynamicState dynamicStates[]{VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
|
||||
VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
|
||||
dynamic.dynamicStateCount = 2;
|
||||
dynamic.pDynamicStates = dynamicStates;
|
||||
VkGraphicsPipelineCreateInfo pipeline{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO};
|
||||
pipeline.stageCount = 2;
|
||||
pipeline.pStages = stages;
|
||||
pipeline.pVertexInputState = &vertex;
|
||||
pipeline.pInputAssemblyState = &assembly;
|
||||
pipeline.pViewportState = &viewport;
|
||||
pipeline.pRasterizationState = &raster;
|
||||
pipeline.pMultisampleState = &samples;
|
||||
pipeline.pColorBlendState = &blend;
|
||||
pipeline.pDynamicState = &dynamic;
|
||||
pipeline.layout = key.pipelineLayout;
|
||||
pipeline.renderPass = key.renderPass;
|
||||
VK_VERIFY(vkCreateGraphicsPipelines(m_device, VK_NULL_HANDLE, 1, &pipeline, nullptr, &key.pipeline));
|
||||
cache.pipelines.push_back(key);
|
||||
MGLOG_I("Magma: YUV conversion pipeline %zu: %s format %llu, model %d, range %d, chroma %d/%d, "
|
||||
"filter %d",
|
||||
index, key.externalFormat != 0 ? "external" : "Vulkan",
|
||||
static_cast<unsigned long long>(key.externalFormat != 0 ? key.externalFormat
|
||||
: static_cast<Uint64>(key.format)),
|
||||
static_cast<Int>(key.model), static_cast<Int>(key.range), static_cast<Int>(key.xOffset),
|
||||
static_cast<Int>(key.yOffset), static_cast<Int>(key.chromaFilter));
|
||||
}
|
||||
const auto& chosen = cache.pipelines[index];
|
||||
|
||||
// The buffer, imported with its external format, sampled only.
|
||||
auto created = std::make_shared<WireYuvSource>();
|
||||
created->device = m_device;
|
||||
created->pipeline = index;
|
||||
created->hints = hints;
|
||||
VkExternalFormatANDROID external{VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID};
|
||||
external.externalFormat = chosen.externalFormat;
|
||||
VkExternalMemoryImageCreateInfo externalMemory{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO};
|
||||
externalMemory.pNext = chosen.externalFormat != 0 ? &external : nullptr;
|
||||
externalMemory.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
|
||||
VkImageCreateInfo imageInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
|
||||
imageInfo.pNext = &externalMemory;
|
||||
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||
imageInfo.format = chosen.format;
|
||||
imageInfo.extent = {image->Width, image->Height, 1};
|
||||
imageInfo.mipLevels = 1;
|
||||
imageInfo.arrayLayers = 1;
|
||||
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
if (vkCreateImage(m_device, &imageInfo, nullptr, &created->image) != VK_SUCCESS) {
|
||||
created->image = VK_NULL_HANDLE;
|
||||
MGLOG_E_ONCE("Magma: YUV image %llu: vkCreateImage(external format) failed",
|
||||
static_cast<unsigned long long>(image->Id));
|
||||
return false;
|
||||
}
|
||||
Int32 type = -1;
|
||||
for (Uint32 i = 0; i < ctx.memory.memoryTypeCount && type < 0; ++i) {
|
||||
if ((properties.memoryTypeBits & (1u << i)) != 0) type = static_cast<Int32>(i);
|
||||
}
|
||||
VkMemoryDedicatedAllocateInfo dedicated{VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO};
|
||||
dedicated.image = created->image;
|
||||
VkImportAndroidHardwareBufferInfoANDROID import{
|
||||
VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID};
|
||||
import.pNext = &dedicated;
|
||||
import.buffer = ahb;
|
||||
VkMemoryAllocateInfo allocate{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
allocate.pNext = &import;
|
||||
allocate.allocationSize = properties.allocationSize;
|
||||
allocate.memoryTypeIndex = static_cast<Uint32>(type < 0 ? 0 : type);
|
||||
if (type < 0 || vkAllocateMemory(m_device, &allocate, nullptr, &created->memory) != VK_SUCCESS ||
|
||||
vkBindImageMemory(m_device, created->image, created->memory, 0) != VK_SUCCESS) {
|
||||
MGLOG_E_ONCE("Magma: YUV image %llu: its buffer could not be imported",
|
||||
static_cast<unsigned long long>(image->Id));
|
||||
return false;
|
||||
}
|
||||
VkSamplerYcbcrConversionInfo conversionInfo{VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO};
|
||||
conversionInfo.conversion = chosen.conversion;
|
||||
VkImageViewCreateInfo view{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
|
||||
view.pNext = &conversionInfo;
|
||||
view.image = created->image;
|
||||
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
view.format = chosen.format;
|
||||
view.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
|
||||
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY};
|
||||
view.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
if (vkCreateImageView(m_device, &view, nullptr, &created->view) != VK_SUCCESS) {
|
||||
created->view = VK_NULL_HANDLE;
|
||||
MGLOG_E_ONCE("Magma: YUV image %llu: its view could not be created",
|
||||
static_cast<unsigned long long>(image->Id));
|
||||
return false;
|
||||
}
|
||||
MGLOG_I("Magma: YUV image %llu (%ux%u%s) sampled through conversion pipeline %zu",
|
||||
static_cast<unsigned long long>(image->Id), image->Width, image->Height,
|
||||
image->IsForeign() ? ", foreign: CPU-copied" : ", zero-copy", index);
|
||||
resource.yuvSource = created;
|
||||
source = created.get();
|
||||
}
|
||||
const auto& chosen = cache.pipelines[source->pipeline];
|
||||
|
||||
// ---- the pass ----
|
||||
auto& frame = m_frameContext.GetCurrent();
|
||||
if (!frame.isCommandRecording) m_frameContext.BeginCommandRecording();
|
||||
if (VkRenderPassManager::GetActiveRenderPass()) VkRenderPassManager::EndRenderPass(frame.commandBuffer);
|
||||
m_textureManager->FlushPendingUploads();
|
||||
const Uint32 family = static_cast<Uint32>(m_physicalDevice.queueFamilies.graphicsFamily);
|
||||
// The buffer's content is its producer's (the CPU's copy, or the allocator's writer): it is
|
||||
// acquired from the foreign family for the read and handed back after it.
|
||||
VkImageMemoryBarrier acquire{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
|
||||
acquire.srcAccessMask = 0;
|
||||
acquire.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
acquire.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
acquire.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
acquire.srcQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
|
||||
acquire.dstQueueFamilyIndex = family;
|
||||
acquire.image = source->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_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &acquire);
|
||||
|
||||
WireImage target{};
|
||||
target.image = resource.image;
|
||||
target.format = resource.format;
|
||||
target.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
target.extent = resource.extent;
|
||||
target.layout = resource.layout;
|
||||
target.trackedLayout = &resource.layout;
|
||||
target.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
target.usageFlags = resource.usageFlags;
|
||||
target.levels = resource.mipLevels;
|
||||
TransitionWireImage(target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
|
||||
VkImageViewCreateInfo targetViewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
|
||||
targetViewInfo.image = resource.image;
|
||||
targetViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
targetViewInfo.format = resource.format;
|
||||
targetViewInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
VkImageView targetView = VK_NULL_HANDLE;
|
||||
VK_VERIFY(vkCreateImageView(m_device, &targetViewInfo, nullptr, &targetView));
|
||||
VkFramebufferCreateInfo framebufferInfo{VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO};
|
||||
framebufferInfo.renderPass = chosen.renderPass;
|
||||
framebufferInfo.attachmentCount = 1;
|
||||
framebufferInfo.pAttachments = &targetView;
|
||||
framebufferInfo.width = resource.extent.width;
|
||||
framebufferInfo.height = resource.extent.height;
|
||||
framebufferInfo.layers = 1;
|
||||
VkFramebuffer framebuffer = VK_NULL_HANDLE;
|
||||
VK_VERIFY(vkCreateFramebuffer(m_device, &framebufferInfo, nullptr, &framebuffer));
|
||||
// A multi-planar image may take more than one descriptor of its binding.
|
||||
VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
|
||||
VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
|
||||
const VkDescriptorPoolSize poolSize{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3};
|
||||
VkDescriptorPoolCreateInfo pool{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
|
||||
pool.maxSets = 1;
|
||||
pool.poolSizeCount = 1;
|
||||
pool.pPoolSizes = &poolSize;
|
||||
VK_VERIFY(vkCreateDescriptorPool(m_device, &pool, nullptr, &descriptorPool));
|
||||
VkDescriptorSetAllocateInfo alloc{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
|
||||
alloc.descriptorPool = descriptorPool;
|
||||
alloc.descriptorSetCount = 1;
|
||||
alloc.pSetLayouts = &chosen.descriptorLayout;
|
||||
VK_VERIFY(vkAllocateDescriptorSets(m_device, &alloc, &descriptorSet));
|
||||
const VkDescriptorImageInfo sampled{chosen.sampler, source->view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
|
||||
VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
|
||||
write.dstSet = descriptorSet;
|
||||
write.dstBinding = 0;
|
||||
write.descriptorCount = 1;
|
||||
write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
write.pImageInfo = &sampled;
|
||||
vkUpdateDescriptorSets(m_device, 1, &write, 0, nullptr);
|
||||
|
||||
VkRenderPassBeginInfo begin{VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO};
|
||||
begin.renderPass = chosen.renderPass;
|
||||
begin.framebuffer = framebuffer;
|
||||
begin.renderArea = {{0, 0}, resource.extent};
|
||||
vkCmdBeginRenderPass(frame.commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
|
||||
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, chosen.pipeline);
|
||||
const VkViewport viewport{0.0f, 0.0f, static_cast<float>(resource.extent.width),
|
||||
static_cast<float>(resource.extent.height), 0.0f, 1.0f};
|
||||
const VkRect2D scissor{{0, 0}, resource.extent};
|
||||
vkCmdSetViewport(frame.commandBuffer, 0, 1, &viewport);
|
||||
vkCmdSetScissor(frame.commandBuffer, 0, 1, &scissor);
|
||||
vkCmdBindDescriptorSets(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, chosen.pipelineLayout, 0, 1,
|
||||
&descriptorSet, 0, nullptr);
|
||||
vkCmdDraw(frame.commandBuffer, 3, 1, 0, 0);
|
||||
vkCmdEndRenderPass(frame.commandBuffer);
|
||||
|
||||
VkImageMemoryBarrier release = acquire;
|
||||
release.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
release.dstAccessMask = 0;
|
||||
release.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
release.newLayout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
release.srcQueueFamilyIndex = family;
|
||||
release.dstQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
|
||||
vkCmdPipelineBarrier(frame.commandBuffer, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &release);
|
||||
// GENERAL, the layout every wire descriptor samples in, with the pass's writes made visible.
|
||||
target.layout = resource.layout;
|
||||
TransitionWireImage(target, VK_IMAGE_LAYOUT_GENERAL);
|
||||
ResetDynamicStateShadow();
|
||||
InvalidatePipelineMemo();
|
||||
if (m_uniformManager) m_uniformManager->OnCommandBufferBoundary();
|
||||
m_textureManager->StampResourceRecordingUse(resource);
|
||||
m_textureManager->StampResourceRecordingWrite(resource);
|
||||
WireRetiredObjects retired{};
|
||||
retired.framebuffers.push_back(framebuffer);
|
||||
retired.imageViews = {targetView};
|
||||
retired.descriptorPools.push_back(descriptorPool);
|
||||
RetireWireObjects(Move(retired));
|
||||
// The frame's fence orders the next refresh of a foreign buffer after this read.
|
||||
m_sharedImageReads.NoteRead(image);
|
||||
return true;
|
||||
#else
|
||||
(void)resource;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#endif // MOBILEGL_BUILD_DISAGGREGATED
|
||||
Reference in New Issue
Block a user