From 59bad771762dff45e83853cc4c1125587504e748 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Wed, 25 Feb 2026 09:42:09 +0800 Subject: [PATCH] [Chore] (MG_Backend/DirectVulkan): rename functions in VkTextureManager --- .../Renderer/VkTextureManager.cpp | 55 ++++++++++--------- .../DirectVulkan/Renderer/VkTextureManager.h | 22 +++++--- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp index 7e2c845f..5f3feb41 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp @@ -40,7 +40,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { m_graphicsQueue = VK_NULL_HANDLE; } - Bool VkTextureManager::SyncTextureAndGetDescriptor(const MG_State::GLState::ITextureObject& texture, + Bool VkTextureManager::SyncTextureAndGetDescriptor(MG_State::GLState::ITextureObject& texture, VkDescriptorImageInfo& outImageInfo) { MOBILEGL_ASSERT(m_device != VK_NULL_HANDLE, "SyncTextureAndGetDescriptor: m_device == VK_NULL_HANDLE"); @@ -52,7 +52,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { it = insertIt; } - if (!EnsureTextureSynced(it->second, texture)) { + if (!SyncTexture(texture, it->second)) { return false; } @@ -66,19 +66,20 @@ namespace MobileGL::MG_Backend::DirectVulkan { return true; } - Bool VkTextureManager::EnsureTextureSynced(TextureResource& resource, const MG_State::GLState::ITextureObject& texture) { + Bool VkTextureManager::SyncTexture(MG_State::GLState::ITextureObject &texture, + TextureResource &outResource) { TextureUploadTarget level0Target = TextureUploadTarget::Unknown; IntVec3 texelSize{0, 0, 0}; SizeT byteSize = 0; - if (!ResolveLevel0(texture, level0Target, texelSize, byteSize)) { + if (!CheckLevel0Completeness(texture, level0Target, texelSize, byteSize)) { return false; } - if (!EnsureTextureResource(resource, texture, level0Target, texelSize, byteSize)) { + if (!SyncTextureResource(texture, level0Target, texelSize, byteSize, outResource)) { return false; } - const auto* mipTexture = dynamic_cast(&texture); + auto* mipTexture = dynamic_cast(&texture); if (!mipTexture) { return false; } @@ -87,19 +88,20 @@ namespace MobileGL::MG_Backend::DirectVulkan { return true; } - if (!UploadLevel0(resource, *mipTexture, level0Target, byteSize)) { + if (!UploadLevel0(*mipTexture, level0Target, byteSize, outResource)) { return false; } - auto& mutableTexture = const_cast(*mipTexture); + auto& mutableTexture = *mipTexture; mutableTexture.MarkStorageDirty(level0Target, 0, false); return true; } - Bool VkTextureManager::EnsureTextureResource(TextureResource& resource, const MG_State::GLState::ITextureObject& texture, - TextureUploadTarget level0Target, const IntVec3& texelSize, - SizeT byteSize) { - const VkFormat format = ResolveTextureFormat(texture.GetFormat()); + Bool VkTextureManager::SyncTextureResource(const MG_State::GLState::ITextureObject &texture, + TextureUploadTarget level0Target, + const IntVec3 &texelSize, SizeT byteSize, + TextureResource &resource) { + const VkFormat format = GetVkFormat(texture.GetFormat()); if (format == VK_FORMAT_UNDEFINED) { return false; } @@ -158,10 +160,10 @@ namespace MobileGL::MG_Backend::DirectVulkan { return true; } - Bool VkTextureManager::UploadLevel0(TextureResource& resource, const MG_State::GLState::TextureObjectMipmap& mipmapTexture, - TextureUploadTarget level0Target, SizeT byteSize) { - auto& mutableTexture = const_cast(mipmapTexture); - const void* source = mutableTexture.MapMipmapData(level0Target, 0); + Bool VkTextureManager::UploadLevel0(MG_State::GLState::TextureObjectMipmap &mipmapTexture, + TextureUploadTarget level0Target, SizeT byteSize, + TextureResource &outResource) { + const void* source = mipmapTexture.MapMipmapData(level0Target, 0); if (source == nullptr || byteSize == 0) { return false; } @@ -186,16 +188,17 @@ namespace MobileGL::MG_Backend::DirectVulkan { std::memcpy(mapped, source, byteSize); vmaUnmapMemory(m_allocator, stagingAllocation); - const Bool ok = ExecuteImmediate([&](VkCommandBuffer commandBuffer) { + // Could be uploaded asynchronously? + const Bool ok = ExecuteCmdBufImmediate([&](VkCommandBuffer commandBuffer) { VkImageMemoryBarrier toTransferDst{}; toTransferDst.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; toTransferDst.srcAccessMask = 0; toTransferDst.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - toTransferDst.oldLayout = resource.layout; + toTransferDst.oldLayout = outResource.layout; toTransferDst.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; toTransferDst.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; toTransferDst.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - toTransferDst.image = resource.image; + toTransferDst.image = outResource.image; toTransferDst.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; toTransferDst.subresourceRange.baseMipLevel = 0; toTransferDst.subresourceRange.levelCount = 1; @@ -213,8 +216,8 @@ namespace MobileGL::MG_Backend::DirectVulkan { copy.imageSubresource.baseArrayLayer = 0; copy.imageSubresource.layerCount = 1; copy.imageOffset = {0, 0, 0}; - copy.imageExtent = {resource.extent.width, resource.extent.height, 1}; - vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, resource.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, + copy.imageExtent = {outResource.extent.width, outResource.extent.height, 1}; + vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, outResource.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©); VkImageMemoryBarrier toSampled{}; @@ -225,7 +228,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { toSampled.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; toSampled.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; toSampled.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - toSampled.image = resource.image; + toSampled.image = outResource.image; toSampled.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; toSampled.subresourceRange.baseMipLevel = 0; toSampled.subresourceRange.levelCount = 1; @@ -240,11 +243,11 @@ namespace MobileGL::MG_Backend::DirectVulkan { if (!ok) { return false; } - resource.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + outResource.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; return true; } - Bool VkTextureManager::ExecuteImmediate(const std::function& recorder) const { + Bool VkTextureManager::ExecuteCmdBufImmediate(const std::function& recorder) const { VkCommandBufferAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.commandPool = m_commandPool; @@ -289,7 +292,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { resource.format = VK_FORMAT_UNDEFINED; } - Bool VkTextureManager::ResolveLevel0(const MG_State::GLState::ITextureObject& texture, TextureUploadTarget& outTarget, + Bool VkTextureManager::CheckLevel0Completeness(const MG_State::GLState::ITextureObject& texture, TextureUploadTarget& outTarget, IntVec3& outTexelSize, SizeT& outByteSize) { const auto* mipTexture = dynamic_cast(&texture); if (!mipTexture) { @@ -305,7 +308,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { return outTexelSize.x() > 0 && outTexelSize.y() > 0 && outByteSize > 0; } - VkFormat VkTextureManager::ResolveTextureFormat(TextureInternalFormat format) { + VkFormat VkTextureManager::GetVkFormat(TextureInternalFormat format) { switch (format) { case TextureInternalFormat::RGBA: case TextureInternalFormat::RGBA8: diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h index 0fb59738..58fac3a7 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h @@ -31,7 +31,7 @@ public: Bool Initialize(const InitInfo& initInfo); void Shutdown(); - Bool SyncTextureAndGetDescriptor(const MG_State::GLState::ITextureObject& texture, VkDescriptorImageInfo& outImageInfo); + Bool SyncTextureAndGetDescriptor(MG_State::GLState::ITextureObject& texture, VkDescriptorImageInfo& outImageInfo); private: struct TextureResource { @@ -44,16 +44,20 @@ private: Uint textureExternalIndex = 0; }; - Bool EnsureTextureSynced(TextureResource& resource, const MG_State::GLState::ITextureObject& texture); - Bool EnsureTextureResource(TextureResource& resource, const MG_State::GLState::ITextureObject& texture, - TextureUploadTarget level0Target, const IntVec3& texelSize, SizeT byteSize); - Bool UploadLevel0(TextureResource& resource, const MG_State::GLState::TextureObjectMipmap& mipmapTexture, - TextureUploadTarget level0Target, SizeT byteSize); - Bool ExecuteImmediate(const std::function& recorder) const; + Bool SyncTexture(MG_State::GLState::ITextureObject &texture, + TextureResource &outResource); + Bool SyncTextureResource(const MG_State::GLState::ITextureObject &texture, + TextureUploadTarget level0Target, + const IntVec3 &texelSize, SizeT byteSize, + TextureResource &resource); + Bool UploadLevel0(MG_State::GLState::TextureObjectMipmap &mipmapTexture, + TextureUploadTarget level0Target, SizeT byteSize, + TextureResource &outResource); + Bool ExecuteCmdBufImmediate(const std::function& recorder) const; void DestroyTextureResource(TextureResource& resource) const; - static Bool ResolveLevel0(const MG_State::GLState::ITextureObject& texture, TextureUploadTarget& outTarget, + static Bool CheckLevel0Completeness(const MG_State::GLState::ITextureObject& texture, TextureUploadTarget& outTarget, IntVec3& outTexelSize, SizeT& outByteSize); - static VkFormat ResolveTextureFormat(TextureInternalFormat format); + static VkFormat GetVkFormat(TextureInternalFormat format); VkDevice m_device = VK_NULL_HANDLE; VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;