83 lines
2.7 KiB
C++
83 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#include "texture_backend.h"
|
|
#include <vulkan/vulkan.h>
|
|
#include <vulkan/vulkan_android.h>
|
|
#include <IUnityGraphicsVulkan.h>
|
|
|
|
/**
|
|
* Vulkan implementation of ITextureBackend.
|
|
* Imports AHardwareBuffer via VK_ANDROID_external_memory_android_hardware_buffer
|
|
* and copies to a local VkImage for safe rendering.
|
|
*/
|
|
class VulkanTextureBackend : public ITextureBackend
|
|
{
|
|
public:
|
|
VulkanTextureBackend() = default;
|
|
~VulkanTextureBackend() override { Destroy(); }
|
|
|
|
/**
|
|
* Initialize the Vulkan backend with Unity's Vulkan interface.
|
|
* Must be called during UnityPluginLoad.
|
|
* @param unityInterfaces Unity's interface registry
|
|
* @return true if Vulkan is available and initialization succeeded
|
|
*/
|
|
bool Initialize(IUnityInterfaces* unityInterfaces);
|
|
|
|
bool Create(AHardwareBuffer* buffer) override;
|
|
void Update() override;
|
|
void Destroy() override;
|
|
void* GetNativeTexturePtr() override;
|
|
int GetWidth() const override { return m_Width; }
|
|
int GetHeight() const override { return m_Height; }
|
|
bool IsVulkan() const override { return true; }
|
|
|
|
/**
|
|
* Get the VkImageView for Unity texture binding.
|
|
*/
|
|
VkImageView GetLocalImageView() const { return m_LocalImageView; }
|
|
|
|
private:
|
|
bool ImportHardwareBuffer(AHardwareBuffer* buffer);
|
|
bool CreateLocalImage();
|
|
void DestroyImportedResources();
|
|
void DestroyLocalResources();
|
|
uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
|
void TransitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout);
|
|
void CopyImage();
|
|
|
|
// Unity Vulkan interface
|
|
IUnityGraphicsVulkan* m_UnityVulkan = nullptr;
|
|
|
|
// Vulkan device objects (from Unity)
|
|
VkInstance m_Instance = VK_NULL_HANDLE;
|
|
VkPhysicalDevice m_PhysicalDevice = VK_NULL_HANDLE;
|
|
VkDevice m_Device = VK_NULL_HANDLE;
|
|
VkQueue m_Queue = VK_NULL_HANDLE;
|
|
uint32_t m_QueueFamilyIndex = 0;
|
|
|
|
// Command pool for copy operations
|
|
VkCommandPool m_CommandPool = VK_NULL_HANDLE;
|
|
VkCommandBuffer m_CommandBuffer = VK_NULL_HANDLE;
|
|
|
|
// Imported hardware buffer resources
|
|
VkImage m_ImportedImage = VK_NULL_HANDLE;
|
|
VkDeviceMemory m_ImportedMemory = VK_NULL_HANDLE;
|
|
|
|
// Local copy resources (for safe rendering)
|
|
VkImage m_LocalImage = VK_NULL_HANDLE;
|
|
VkDeviceMemory m_LocalMemory = VK_NULL_HANDLE;
|
|
VkImageView m_LocalImageView = VK_NULL_HANDLE;
|
|
|
|
// Texture format info
|
|
VkFormat m_Format = VK_FORMAT_R8G8B8A8_UNORM;
|
|
int m_Width = 0;
|
|
int m_Height = 0;
|
|
|
|
bool m_Initialized = false;
|
|
bool m_Created = false;
|
|
|
|
// Extension function pointers
|
|
PFN_vkGetAndroidHardwareBufferPropertiesANDROID vkGetAndroidHardwareBufferPropertiesANDROID = nullptr;
|
|
};
|