From 9042f628d48de780cd7e079e7c1ee672ac7dddbf Mon Sep 17 00:00:00 2001 From: Grant Commodore Date: Mon, 10 Feb 2025 17:50:45 -0800 Subject: [PATCH] vk: Adds the layercount to the VulkanTexture and SwapChainBundle (#8392) --- .../include/backend/platforms/VulkanPlatform.h | 6 ++++++ filament/backend/src/vulkan/VulkanDriver.cpp | 2 +- filament/backend/src/vulkan/VulkanHandles.cpp | 16 ++++++++++++---- filament/backend/src/vulkan/VulkanSwapChain.cpp | 9 ++++++--- filament/backend/src/vulkan/VulkanSwapChain.h | 1 + filament/backend/src/vulkan/VulkanTexture.cpp | 16 +++++++++++++--- filament/backend/src/vulkan/VulkanTexture.h | 2 +- 7 files changed, 40 insertions(+), 12 deletions(-) diff --git a/filament/backend/include/backend/platforms/VulkanPlatform.h b/filament/backend/include/backend/platforms/VulkanPlatform.h index d61818a883..dc458ddb98 100644 --- a/filament/backend/include/backend/platforms/VulkanPlatform.h +++ b/filament/backend/include/backend/platforms/VulkanPlatform.h @@ -88,6 +88,7 @@ public: VkFormat colorFormat = VK_FORMAT_UNDEFINED; VkFormat depthFormat = VK_FORMAT_UNDEFINED; VkExtent2D extent = {0, 0}; + uint32_t layerCount = 1; bool isProtected = false; }; @@ -303,6 +304,11 @@ public: */ uint32_t height; + /** + * The layerCount of the external image + */ + uint32_t layerCount; + /** * The layer count of the external image */ diff --git a/filament/backend/src/vulkan/VulkanDriver.cpp b/filament/backend/src/vulkan/VulkanDriver.cpp index 615cf05221..7e4a2b619d 100644 --- a/filament/backend/src/vulkan/VulkanDriver.cpp +++ b/filament/backend/src/vulkan/VulkanDriver.cpp @@ -579,7 +579,7 @@ void VulkanDriver::createTextureExternalImageR(Handle th, backend::Sa auto texture = resource_ptr::make(&mResourceManager, th, mPlatform->getDevice(), mAllocator, &mResourceManager, &mCommands, data.first, data.second, metadata.format, - 1, metadata.width, metadata.height, usage, mStagePool); + 1, metadata.width, metadata.height, /*depth=*/1, usage, mStagePool); texture.inc(); } diff --git a/filament/backend/src/vulkan/VulkanHandles.cpp b/filament/backend/src/vulkan/VulkanHandles.cpp index 3a7ac6b134..d2782ede1f 100644 --- a/filament/backend/src/vulkan/VulkanHandles.cpp +++ b/filament/backend/src/vulkan/VulkanHandles.cpp @@ -128,6 +128,15 @@ fvkmemory::resource_ptr initMsaaTexture( return msTexture; } +VulkanAttachment createSwapchainAttachment(const fvkmemory::resource_ptr texture) { + return VulkanAttachment { + .texture = texture, + .level = 0, + .layerCount = static_cast(texture ? texture->getPrimaryViewRange().layerCount : 1), + .layer = 0, + }; +} + } // anonymous namespace void VulkanDescriptorSet::acquire(fvkmemory::resource_ptr texture) { @@ -274,22 +283,21 @@ void VulkanRenderTarget::bindToSwapChain(fvkmemory::resource_ptrisProtected(); - VulkanAttachment color = {}; - color.texture = swapchain->getCurrentColor(); + VulkanAttachment color = createSwapchainAttachment(swapchain->getCurrentColor()); mInfo->attachments = {color}; auto& fbkey = mInfo->fbkey; auto& rpkey = mInfo->rpkey; rpkey.colorFormat[0] = color.getFormat(); + rpkey.viewCount = color.layerCount; fbkey.width = width; fbkey.height = height; fbkey.color[0] = color.getImageView(); fbkey.resolve[0] = VK_NULL_HANDLE; if (swapchain->getDepth()) { - VulkanAttachment depth = {}; - depth.texture = swapchain->getDepth(); + VulkanAttachment depth = createSwapchainAttachment(swapchain->getDepth()); mInfo->attachments.push_back(depth); mInfo->depthIndex = 1; diff --git a/filament/backend/src/vulkan/VulkanSwapChain.cpp b/filament/backend/src/vulkan/VulkanSwapChain.cpp index a0505f7583..0b418c9650 100644 --- a/filament/backend/src/vulkan/VulkanSwapChain.cpp +++ b/filament/backend/src/vulkan/VulkanSwapChain.cpp @@ -40,6 +40,8 @@ VulkanSwapChain::VulkanSwapChain(VulkanPlatform* platform, VulkanContext const& mFlushAndWaitOnResize(platform->getCustomization().flushAndWaitOnWindowResize), mTransitionSwapChainImageLayoutForPresent( platform->getCustomization().transitionSwapChainImageLayoutForPresent), + mLayerCount(1), + mCurrentSwapIndex(0), mAcquired(false), mIsFirstRenderPass(true) { swapChain = mPlatform->createSwapChain(nativeWindow, flags, extent); @@ -76,17 +78,18 @@ void VulkanSwapChain::update() { for (auto const color: bundle.colors) { auto colorTexture = fvkmemory::resource_ptr::construct(mResourceManager, device, mAllocator, mResourceManager, mCommands, color, VK_NULL_HANDLE, - bundle.colorFormat, 1, bundle.extent.width, bundle.extent.height, colorUsage, + bundle.colorFormat, 1, bundle.extent.width, bundle.extent.height, bundle.layerCount, colorUsage, mStagePool); mColors.push_back(colorTexture); } mDepth = fvkmemory::resource_ptr::construct(mResourceManager, device, mAllocator, mResourceManager, mCommands, bundle.depth, VK_NULL_HANDLE, - bundle.depthFormat, 1, bundle.extent.width, bundle.extent.height, depthUsage, + bundle.depthFormat, 1, bundle.extent.width, bundle.extent.height, bundle.layerCount, depthUsage, mStagePool); mExtent = bundle.extent; + mLayerCount = bundle.layerCount; } void VulkanSwapChain::present() { @@ -97,7 +100,7 @@ void VulkanSwapChain::present() { .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, - .layerCount = 1, + .layerCount = mLayerCount, }; mColors[mCurrentSwapIndex]->transitionLayout(&commands, subresources, VulkanLayout::PRESENT); } diff --git a/filament/backend/src/vulkan/VulkanSwapChain.h b/filament/backend/src/vulkan/VulkanSwapChain.h index bf1c26945b..ad44930a0a 100644 --- a/filament/backend/src/vulkan/VulkanSwapChain.h +++ b/filament/backend/src/vulkan/VulkanSwapChain.h @@ -96,6 +96,7 @@ private: utils::FixedCapacityVector> mColors; fvkmemory::resource_ptr mDepth; VkExtent2D mExtent; + uint32_t mLayerCount; uint32_t mCurrentSwapIndex; std::function mExplicitImageReadyWait = nullptr; bool mAcquired; diff --git a/filament/backend/src/vulkan/VulkanTexture.cpp b/filament/backend/src/vulkan/VulkanTexture.cpp index c81c228b29..dc4f071b8b 100644 --- a/filament/backend/src/vulkan/VulkanTexture.cpp +++ b/filament/backend/src/vulkan/VulkanTexture.cpp @@ -144,6 +144,16 @@ inline VulkanLayout getDefaultLayoutImpl(VkImageUsageFlags vkusage) { return getDefaultLayoutImpl(usage); } +SamplerType getSamplerTypeFromDepth(uint32_t const depth) { + return depth > 1 ? SamplerType::SAMPLER_2D_ARRAY + : SamplerType::SAMPLER_2D; +} + +uint8_t getLayerCountFromDepth(uint32_t const depth) { + return getLayerCount(getSamplerTypeFromDepth(depth), depth); +} + + } // anonymous namespace VulkanTextureState::VulkanTextureState(VkDevice device, VmaAllocator allocator, @@ -165,12 +175,12 @@ VulkanTextureState::VulkanTextureState(VkDevice device, VmaAllocator allocator, VulkanTexture::VulkanTexture(VkDevice device, VmaAllocator allocator, fvkmemory::ResourceManager* resourceManager, VulkanCommands* commands, VkImage image, VkDeviceMemory memory, VkFormat format, uint8_t samples, uint32_t width, - uint32_t height, TextureUsage tusage, VulkanStagePool& stagePool) - : HwTexture(SamplerType::SAMPLER_2D, 1, samples, width, height, 1, TextureFormat::UNUSED, + uint32_t height, uint32_t depth, TextureUsage tusage, VulkanStagePool& stagePool) + : HwTexture(getSamplerTypeFromDepth(depth), 1, samples, width, height, depth, TextureFormat::UNUSED, tusage), mState(fvkmemory::resource_ptr::construct(resourceManager, device, allocator, commands, stagePool, format, fvkutils::getViewType(SamplerType::SAMPLER_2D), - 1, 1, getDefaultLayoutImpl(tusage), any(usage & TextureUsage::PROTECTED))) { + /*mipLevels=*/1, getLayerCountFromDepth(depth), getDefaultLayoutImpl(tusage), any(usage & TextureUsage::PROTECTED))) { mState->mTextureImage = image; mState->mTextureImageMemory = memory; mPrimaryViewRange = mState->mFullViewRange; diff --git a/filament/backend/src/vulkan/VulkanTexture.h b/filament/backend/src/vulkan/VulkanTexture.h index 7f1cfe4e42..a99d257d78 100644 --- a/filament/backend/src/vulkan/VulkanTexture.h +++ b/filament/backend/src/vulkan/VulkanTexture.h @@ -94,7 +94,7 @@ struct VulkanTexture : public HwTexture, fvkmemory::Resource { // The texture will never destroy the given VkImage, but it does manages its subresources. VulkanTexture(VkDevice device, VmaAllocator allocator, fvkmemory::ResourceManager* resourceManager, VulkanCommands* commands, VkImage image, - VkDeviceMemory memory, VkFormat format, uint8_t samples, uint32_t width, uint32_t height, + VkDeviceMemory memory, VkFormat format, uint8_t samples, uint32_t width, uint32_t height, uint32_t depth, TextureUsage tusage, VulkanStagePool& stagePool); // Constructor for creating a texture view for wrt specific mip range