From 35b9c182978d7f585940504a4f05f12c00426d07 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 25 Mar 2018 09:27:11 -0700 Subject: Implementing texture views in Vulkan --- .../include/RenderDeviceVkImpl.h | 2 +- .../GraphicsEngineVulkan/include/SwapChainVkImpl.h | 2 +- .../include/TextureViewVkImpl.h | 9 ++- .../GraphicsEngineVulkan/include/TextureVkImpl.h | 6 +- .../src/DeviceContextVkImpl.cpp | 4 +- .../src/RenderDeviceVkImpl.cpp | 7 +-- .../GraphicsEngineVulkan/src/SwapChainVkImpl.cpp | 52 +++++++++-------- .../GraphicsEngineVulkan/src/TextureViewVkImpl.cpp | 17 ++---- .../GraphicsEngineVulkan/src/TextureVkImpl.cpp | 66 +++++----------------- 9 files changed, 66 insertions(+), 99 deletions(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/RenderDeviceVkImpl.h b/Graphics/GraphicsEngineVulkan/include/RenderDeviceVkImpl.h index 99ef1a79..dc915d75 100644 --- a/Graphics/GraphicsEngineVulkan/include/RenderDeviceVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/RenderDeviceVkImpl.h @@ -67,7 +67,7 @@ public: virtual void CreateTexture(const TextureDesc& TexDesc, const TextureData &Data, ITexture **ppTexture)override final; - //void CreateTexture(const TextureDesc& TexDesc, IVkResource *pVkTexture, class TextureVkImpl **ppTexture); + void CreateTexture(const TextureDesc& TexDesc, VkImage vkImgHandle, class TextureVkImpl **ppTexture); virtual void CreateSampler(const SamplerDesc& SamplerDesc, ISampler **ppSampler)override final; diff --git a/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.h b/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.h index 8dbb6918..72006ccc 100644 --- a/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/SwapChainVkImpl.h @@ -65,7 +65,7 @@ private: VkSwapchainKHR m_VkSwapChain = VK_NULL_HANDLE; VkFormat m_VkColorFormat = VK_FORMAT_UNDEFINED; - //std::vector< RefCntAutoPtr, STDAllocatorRawMem> > m_pBackBufferRTV; + std::vector< RefCntAutoPtr, STDAllocatorRawMem> > m_pBackBufferRTV; //RefCntAutoPtr m_pDepthBufferDSV; }; diff --git a/Graphics/GraphicsEngineVulkan/include/TextureViewVkImpl.h b/Graphics/GraphicsEngineVulkan/include/TextureViewVkImpl.h index f8411711..911d1699 100644 --- a/Graphics/GraphicsEngineVulkan/include/TextureViewVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/TextureViewVkImpl.h @@ -29,7 +29,6 @@ #include "TextureViewVk.h" #include "RenderDeviceVk.h" #include "TextureViewBase.h" -#include "DescriptorHeap.h" namespace Diligent { @@ -45,18 +44,18 @@ public: IRenderDevice *pDevice, const TextureViewDesc& ViewDesc, class ITexture *pTexture, - DescriptorHeapAllocation &&HandleAlloc, + VkImageView vkImgView, bool bIsDefaultView); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override; void GenerateMips( IDeviceContext *pContext )override; - //virtual Vk_CPU_DESCRIPTOR_HANDLE GetCPUDescriptorHandle()override{return m_Descriptor.GetCpuHandle();} + VkImageView GetVulkanImageView()const{return m_VkImageView;} protected: - /// Vk view descriptor handle - DescriptorHeapAllocation m_Descriptor; + /// Vulkan image view descriptor handle + VkImageView m_VkImageView; }; } diff --git a/Graphics/GraphicsEngineVulkan/include/TextureVkImpl.h b/Graphics/GraphicsEngineVulkan/include/TextureVkImpl.h index f050afdc..3ca2e580 100644 --- a/Graphics/GraphicsEngineVulkan/include/TextureVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/TextureVkImpl.h @@ -50,12 +50,14 @@ public: class RenderDeviceVkImpl *pDeviceVk, const TextureDesc& TexDesc, const TextureData &InitData = TextureData()); + // Attaches to an existing Vk resource TextureVkImpl(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, class RenderDeviceVkImpl *pDeviceVk, const TextureDesc& TexDesc, - void *pTexture); + VkImage VkImageHandle); + ~TextureVkImpl(); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override; @@ -112,6 +114,8 @@ protected: friend class RenderDeviceVkImpl; */ + VkImage m_VkImage; + const bool m_IsExternalHandle; }; } diff --git a/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp index 4409abdd..95d681b7 100644 --- a/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp @@ -124,9 +124,9 @@ namespace Diligent // We also need to update scissor rect if ScissorEnable state has changed CommitScissor = OldPSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable != PSODesc.GraphicsPipeline.RasterizerDesc.ScissorEnable; } - +#endif TDeviceContextBase::SetPipelineState( pPipelineState ); - +#if 0 auto *pCmdCtx = RequestCmdContext(); auto *pVkPSO = pPipelineStateVk->GetVkPipelineState(); diff --git a/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp index a2854917..cb6f9403 100644 --- a/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/RenderDeviceVkImpl.cpp @@ -590,20 +590,19 @@ void RenderDeviceVkImpl::CreateTextureFromD3DResource(IVkResource *pVkTexture, I } ); } +#endif - -void RenderDeviceVkImpl::CreateTexture(const TextureDesc& TexDesc, IVkResource *pVkTexture, class TextureVkImpl **ppTexture) +void RenderDeviceVkImpl::CreateTexture(const TextureDesc& TexDesc, VkImage vkImgHandle, class TextureVkImpl **ppTexture) { CreateDeviceObject( "texture", TexDesc, ppTexture, [&]() { - TextureVkImpl *pTextureVk = NEW_RC_OBJ(m_TexObjAllocator, "TextureVkImpl instance", TextureVkImpl)(m_TexViewObjAllocator, this, TexDesc, pVkTexture ); + TextureVkImpl *pTextureVk = NEW_RC_OBJ(m_TexObjAllocator, "TextureVkImpl instance", TextureVkImpl)(m_TexViewObjAllocator, this, TexDesc, vkImgHandle); pTextureVk->QueryInterface( IID_TextureVk, reinterpret_cast(ppTexture) ); } ); } -#endif void RenderDeviceVkImpl :: CreateTexture(const TextureDesc& TexDesc, const TextureData &Data, ITexture **ppTexture) { diff --git a/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp index 3473b73e..a85f23aa 100644 --- a/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp @@ -38,8 +38,8 @@ SwapChainVkImpl::SwapChainVkImpl(IReferenceCounters *pRefCounters, DeviceContextVkImpl* pDeviceContextVk, void* pNativeWndHandle) : TSwapChainBase(pRefCounters, pRenderDeviceVk, pDeviceContextVk, SCDesc), - m_VulkanInstance(pRenderDeviceVk->GetVulkanInstance()) - /*m_pBackBufferRTV(STD_ALLOCATOR_RAW_MEM(RefCntAutoPtr, GetRawAllocator(), "Allocator for vector>"))*/ + m_VulkanInstance(pRenderDeviceVk->GetVulkanInstance()), + m_pBackBufferRTV(STD_ALLOCATOR_RAW_MEM(RefCntAutoPtr, GetRawAllocator(), "Allocator for vector>")) { // Create OS-specific surface #if defined(VK_USE_PLATFORM_WIN32_KHR) @@ -134,7 +134,7 @@ SwapChainVkImpl::SwapChainVkImpl(IReferenceCounters *pRefCounters, { m_VkColorFormat = VkReplacementColorFormat; auto NewColorBufferFormat = VkFormatToTexFormat(VkReplacementColorFormat); - LOG_INFO_MESSAGE("Requested color buffer format ", GetTextureFormatAttribs(m_SwapChainDesc.ColorBufferFormat).Name, " is not supported by the surace, and will be replaced with ", GetTextureFormatAttribs(NewColorBufferFormat).Name); + LOG_INFO_MESSAGE("Requested color buffer format ", GetTextureFormatAttribs(m_SwapChainDesc.ColorBufferFormat).Name, " is not supported by the surace and will be replaced with ", GetTextureFormatAttribs(NewColorBufferFormat).Name); m_SwapChainDesc.ColorBufferFormat = NewColorBufferFormat; } else @@ -294,19 +294,32 @@ void SwapChainVkImpl::InitBuffersAndViews() } #endif + m_pBackBufferRTV.resize(m_SwapChainDesc.BufferCount); + uint32_t swapchainImageCount = m_SwapChainDesc.BufferCount; std::vector swapchainImages(swapchainImageCount); auto err = vkGetSwapchainImagesKHR(LogicalVkDevice, m_VkSwapChain, &swapchainImageCount, swapchainImages.data()); CHECK_VK_ERROR_AND_THROW(err, "Failed to get swap chain images"); VERIFY_EXPR(swapchainImageCount == swapchainImages.size()); - //info.buffers.resize(info.swapchainImageCount); - //for (uint32_t i = 0; i < info.swapchainImageCount; i++) { - // info.buffers[i].image = swapchainImages[i]; - //} - //free(swapchainImages); - for (uint32_t i = 0; i < swapchainImageCount; i++) { + + TextureDesc BackBufferDesc; + BackBufferDesc.Format = m_SwapChainDesc.ColorBufferFormat; + std::stringstream name_ss; + name_ss << "Main back buffer " << i; + auto name = name_ss.str(); + BackBufferDesc.Name = name.c_str(); + BackBufferDesc.Type = RESOURCE_DIM_TEX_2D; + BackBufferDesc.Width = m_SwapChainDesc.Width; + BackBufferDesc.Height = m_SwapChainDesc.Height; + BackBufferDesc.Format = m_SwapChainDesc.ColorBufferFormat; + BackBufferDesc.MipLevels = 1; + + RefCntAutoPtr pBackBufferTex; + ValidatedCast(m_pRenderDevice.RawPtr())->CreateTexture(BackBufferDesc, swapchainImages[i], &pBackBufferTex); + + UNSUPPORTED("TODO: move all this code to Texture creation"); VkImageViewCreateInfo color_image_view = {}; color_image_view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; color_image_view.pNext = NULL; @@ -327,6 +340,12 @@ void SwapChainVkImpl::InitBuffersAndViews() VkImageView vkImgView = VK_NULL_HANDLE; err = vkCreateImageView(LogicalVkDevice, &color_image_view, NULL, &vkImgView); CHECK_VK_ERROR_AND_THROW(err, "Failed to create view for a swap chain image"); + + TextureViewDesc RTVDesc; + RTVDesc.ViewType = TEXTURE_VIEW_RENDER_TARGET; + RefCntAutoPtr pRTV; + pBackBufferTex->CreateView(RTVDesc, &pRTV); + m_pBackBufferRTV[i] = RefCntAutoPtr(pRTV, IID_TextureViewVk); } #if 0 @@ -339,7 +358,7 @@ void SwapChainVkImpl::InitBuffersAndViews() #endif #if 0 - m_pBackBufferRTV.resize(m_SwapChainDesc.BufferCount); + for(Uint32 backbuff = 0; backbuff < m_SwapChainDesc.BufferCount; ++backbuff) { CComPtr pBackBuffer; @@ -350,19 +369,6 @@ void SwapChainVkImpl::InitBuffersAndViews() hr = pBackBuffer->SetName(L"Main back buffer"); VERIFY_EXPR(SUCCEEDED(hr)); - TextureDesc BackBufferDesc; - BackBufferDesc.Format = m_SwapChainDesc.ColorBufferFormat; - String Name = "Main back buffer "; - Name += std::to_string(backbuff); - BackBufferDesc.Name = Name.c_str(); - - RefCntAutoPtr pBackBufferTex; - ValidatedCast(m_pRenderDevice.RawPtr())->CreateTexture(BackBufferDesc, pBackBuffer, &pBackBufferTex); - TextureViewDesc RTVDesc; - RTVDesc.ViewType = TEXTURE_VIEW_RENDER_TARGET; - RefCntAutoPtr pRTV; - pBackBufferTex->CreateView(RTVDesc, &pRTV); - m_pBackBufferRTV[backbuff] = RefCntAutoPtr(pRTV, IID_TextureViewVk); } TextureDesc DepthBufferDesc; diff --git a/Graphics/GraphicsEngineVulkan/src/TextureViewVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/TextureViewVkImpl.cpp index 6a58538f..be36e9dd 100644 --- a/Graphics/GraphicsEngineVulkan/src/TextureViewVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/TextureViewVkImpl.cpp @@ -29,20 +29,15 @@ namespace Diligent { TextureViewVkImpl::TextureViewVkImpl( IReferenceCounters *pRefCounters, - IRenderDevice *pDevice, - const TextureViewDesc& ViewDesc, - ITexture *pTexture, - DescriptorHeapAllocation &&HandleAlloc, - bool bIsDefaultView ) : + IRenderDevice *pDevice, + const TextureViewDesc& ViewDesc, + ITexture *pTexture, + VkImageView vkImgView, + bool bIsDefaultView ) : TTextureViewBase( pRefCounters, pDevice, ViewDesc, pTexture, bIsDefaultView ), - m_Descriptor(std::move(HandleAlloc)) + m_VkImageView(vkImgView) { } -// -//IVkView* TextureViewVkImpl::GetVkView() -//{ -// return m_pVkView; -//} IMPLEMENT_QUERY_INTERFACE( TextureViewVkImpl, IID_TextureViewVk, TTextureViewBase ) diff --git a/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp index 1b452377..fabf8323 100644 --- a/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp @@ -27,7 +27,7 @@ #include "DeviceContextVkImpl.h" #include "VulkanTypeConversions.h" #include "TextureViewVkImpl.h" -//#include "DXGITypeConversions.h" +#include "VulkanTypeConversions.h" #include "EngineMemory.h" #include "StringTools.h" @@ -62,7 +62,8 @@ TextureVkImpl :: TextureVkImpl(IReferenceCounters *pRefCounters, RenderDeviceVkImpl *pRenderDeviceVk, const TextureDesc& TexDesc, const TextureData &InitData /*= TextureData()*/) : - TTextureBase(pRefCounters, TexViewObjAllocator, pRenderDeviceVk, TexDesc) + TTextureBase(pRefCounters, TexViewObjAllocator, pRenderDeviceVk, TexDesc), + m_IsExternalHandle(false) { if( m_Desc.Usage == USAGE_STATIC && InitData.pSubResources == nullptr ) LOG_ERROR_AND_THROW("Static Texture must be initialized with data at creation time"); @@ -268,63 +269,22 @@ TextureVkImpl :: TextureVkImpl(IReferenceCounters *pRefCounters, #endif } -static TextureDesc InitTexDescFromVkResource(void *pTexture, const TextureDesc& SrcTexDesc) +static TextureDesc InitTexDescFromVkImage(VkImage vkImg, const TextureDesc& SrcTexDesc) { - //auto ResourceDesc = pTexture->GetDesc(); - - TextureDesc TexDesc = SrcTexDesc; -#if 0 - if (TexDesc.Format == TEX_FORMAT_UNKNOWN) - TexDesc.Format = DXGI_FormatToTexFormat(ResourceDesc.Format); - auto RefDXGIFormat = TexFormatToDXGI_Format(TexDesc.Format); - if( RefDXGIFormat != TexDesc.Format) - LOG_ERROR_AND_THROW("Incorrect texture format (", GetTextureFormatAttribs(TexDesc.Format).Name, ")"); - - TexDesc.Width = static_cast( ResourceDesc.Width ); - TexDesc.Height = Uint32{ ResourceDesc.Height }; - TexDesc.ArraySize = Uint32{ ResourceDesc.DepthOrArraySize }; - TexDesc.MipLevels = Uint32{ ResourceDesc.MipLevels }; - switch(ResourceDesc.Dimension) - { - case Vk_RESOURCE_DIMENSION_TEXTURE1D: TexDesc.Type = TexDesc.ArraySize == 1 ? RESOURCE_DIM_TEX_1D : RESOURCE_DIM_TEX_1D_ARRAY; break; - case Vk_RESOURCE_DIMENSION_TEXTURE2D: TexDesc.Type = TexDesc.ArraySize == 1 ? RESOURCE_DIM_TEX_2D : RESOURCE_DIM_TEX_2D_ARRAY; break; - case Vk_RESOURCE_DIMENSION_TEXTURE3D: TexDesc.Type = RESOURCE_DIM_TEX_3D; break; - } - - TexDesc.SampleCount = ResourceDesc.SampleDesc.Count; - - TexDesc.Usage = USAGE_DEFAULT; - TexDesc.BindFlags = 0; - if( (ResourceDesc.Flags & Vk_RESOURCE_FLAG_ALLOW_RENDER_TARGET) != 0 ) - TexDesc.BindFlags |= BIND_RENDER_TARGET; - if( (ResourceDesc.Flags & Vk_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) != 0 ) - TexDesc.BindFlags |= BIND_DEPTH_STENCIL; - if( (ResourceDesc.Flags & Vk_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) != 0 ) - TexDesc.BindFlags |= BIND_UNORDERED_ACCESS; - if ((ResourceDesc.Flags & Vk_RESOURCE_FLAG_DENY_SHADER_RESOURCE) == 0) - { - auto FormatAttribs = GetTextureFormatAttribs(TexDesc.Format); - if (FormatAttribs.ComponentType != COMPONENT_TYPE_DEPTH && - FormatAttribs.ComponentType != COMPONENT_TYPE_DEPTH_STENCIL) - { - TexDesc.BindFlags |= BIND_SHADER_RESOURCE; - } - } -#endif - return TexDesc; + // There is no way to query any image attribute in Vulkan + return SrcTexDesc; } TextureVkImpl::TextureVkImpl(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, RenderDeviceVkImpl *pDeviceVk, - const TextureDesc& TexDesc, - void *pTexture) : - TTextureBase(pRefCounters, TexViewObjAllocator, pDeviceVk, InitTexDescFromVkResource(pTexture, TexDesc)) + const TextureDesc& TexDesc, + VkImage VkImageHandle) : + TTextureBase(pRefCounters, TexViewObjAllocator, pDeviceVk, InitTexDescFromVkImage(VkImageHandle, TexDesc)), + m_VkImage(VkImageHandle), + m_IsExternalHandle(true) { -#if 0 - m_pVkResource = pTexture; -#endif } IMPLEMENT_QUERY_INTERFACE( TextureVkImpl, IID_TextureVk, TTextureBase ) @@ -402,6 +362,10 @@ void TextureVkImpl::CreateViewInternal( const struct TextureViewDesc &ViewDesc, TextureVkImpl :: ~TextureVkImpl() { + if(!m_IsExternalHandle) + { + + } #if 0 // Vk object can only be destroyed when it is no longer used by the GPU auto *pDeviceVkImpl = ValidatedCast(GetDevice()); -- cgit v1.2.3