@@ -570,9 +570,9 @@ class VulkanDevice : public offloadtest::Device {
570570
571571 // FrameBuffer associated data for offscreen rendering.
572572 VkFramebuffer FrameBuffer = VK_NULL_HANDLE;
573- ImageRef DepthStencil = {0 , 0 , 0 };
574573 std::shared_ptr<VulkanTexture> RenderTarget;
575574 std::shared_ptr<VulkanBuffer> RTReadback;
575+ std::shared_ptr<VulkanTexture> DepthStencil;
576576 std::optional<ResourceRef> VertexBuffer = std::nullopt ;
577577
578578 VkRenderPass RenderPass = VK_NULL_HANDLE;
@@ -1215,46 +1215,12 @@ class VulkanDevice : public offloadtest::Device {
12151215 }
12161216
12171217 llvm::Error createDepthStencil (Pipeline &P, InvocationState &IS) {
1218- // Create an optimal image used as the depth stencil attachment
1219- VkImageCreateInfo ImageCi = {};
1220- ImageCi.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1221- ImageCi.imageType = getVKImageType (ResourceKind::Texture2D);
1222- ImageCi.format = VK_FORMAT_D32_SFLOAT_S8_UINT;
1223- // Use example's height and width
1224- ImageCi.extent = {
1225- static_cast <uint32_t >(P.Bindings .RTargetBufferPtr ->OutputProps .Width ),
1226- static_cast <uint32_t >(P.Bindings .RTargetBufferPtr ->OutputProps .Height ),
1227- 1 };
1228- ImageCi.mipLevels = 1 ;
1229- ImageCi.arrayLayers = 1 ;
1230- ImageCi.samples = VK_SAMPLE_COUNT_1_BIT;
1231- ImageCi.tiling = VK_IMAGE_TILING_OPTIMAL;
1232- ImageCi.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1233- ImageCi.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1234- if (vkCreateImage (Device, &ImageCi, nullptr , &IS.DepthStencil .Image ))
1235- return llvm::createStringError (std::errc::device_or_resource_busy,
1236- " Depth stencil creation failed." );
1237-
1238- // Allocate memory for the image (device local) and bind it to our image
1239- VkMemoryAllocateInfo MemAlloc{};
1240- MemAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1241- VkMemoryRequirements MemReqs;
1242- vkGetImageMemoryRequirements (Device, IS.DepthStencil .Image , &MemReqs);
1243- MemAlloc.allocationSize = MemReqs.size ;
1244- llvm::Expected<uint32_t > MemIdx =
1245- getMemoryIndex (PhysicalDevice, MemReqs.memoryTypeBits ,
1246- VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
1247- if (!MemIdx)
1248- return MemIdx.takeError ();
1249-
1250- MemAlloc.memoryTypeIndex = *MemIdx;
1251- if (vkAllocateMemory (Device, &MemAlloc, nullptr , &IS.DepthStencil .Memory ))
1252- return llvm::createStringError (std::errc::not_enough_memory,
1253- " Depth stencil memory allocation failed." );
1254- if (vkBindImageMemory (Device, IS.DepthStencil .Image , IS.DepthStencil .Memory ,
1255- 0 ))
1256- return llvm::createStringError (std::errc::not_enough_memory,
1257- " Depth stencil memory binding failed." );
1218+ auto TexOrErr = offloadtest::createDefaultDepthStencilTarget (
1219+ *this , P.Bindings .RTargetBufferPtr ->OutputProps .Width ,
1220+ P.Bindings .RTargetBufferPtr ->OutputProps .Height );
1221+ if (!TexOrErr)
1222+ return TexOrErr.takeError ();
1223+ IS.DepthStencil = std::static_pointer_cast<VulkanTexture>(*TexOrErr);
12581224 return llvm::Error::success ();
12591225 }
12601226
@@ -1657,7 +1623,7 @@ class VulkanDevice : public offloadtest::Device {
16571623 Attachments[0 ].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
16581624 Attachments[0 ].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
16591625
1660- Attachments[1 ].format = VK_FORMAT_D32_SFLOAT_S8_UINT ;
1626+ Attachments[1 ].format = getVulkanFormat (IS. DepthStencil -> Desc . Format ) ;
16611627 Attachments[1 ].samples = VK_SAMPLE_COUNT_1_BIT;
16621628 Attachments[1 ].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
16631629 Attachments[1 ].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
@@ -1727,53 +1693,17 @@ class VulkanDevice : public offloadtest::Device {
17271693 return llvm::Error::success ();
17281694 }
17291695
1730- llvm::Error createFrameBuffer (Pipeline &P, InvocationState &IS) {
1731- std::array<VkImageView, 2 > Views = {};
1732- VkImageViewCreateInfo ViewCreateInfo = {};
1733- ViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1734- ViewCreateInfo.viewType = getImageViewType (ResourceKind::Texture2D);
1735- ViewCreateInfo.format = getVKFormat (P.Bindings .RTargetBufferPtr ->Format ,
1736- P.Bindings .RTargetBufferPtr ->Channels );
1737- ViewCreateInfo.components = {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1738- VK_COMPONENT_SWIZZLE_B,
1739- VK_COMPONENT_SWIZZLE_A};
1740- ViewCreateInfo.subresourceRange .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1741- ViewCreateInfo.subresourceRange .baseMipLevel = 0 ;
1742- ViewCreateInfo.subresourceRange .baseArrayLayer = 0 ;
1743- ViewCreateInfo.subresourceRange .layerCount = 1 ;
1744- ViewCreateInfo.subresourceRange .levelCount = 1 ;
1745- ViewCreateInfo.image = IS.FrameBufferResource .ResourceRefs [0 ].Image .Image ;
1746- if (vkCreateImageView (Device, &ViewCreateInfo, nullptr , &Views[0 ]))
1747- return llvm::createStringError (
1748- std::errc::device_or_resource_busy,
1749- " Failed to create frame buffer image view." );
1750- IS.ImageViews .push_back (Views[0 ]);
1751-
1752- VkImageViewCreateInfo DepthStencilViewCi = {};
1753- DepthStencilViewCi.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1754- DepthStencilViewCi.viewType = getImageViewType (ResourceKind::Texture2D);
1755- DepthStencilViewCi.format = VK_FORMAT_D32_SFLOAT_S8_UINT;
1756- DepthStencilViewCi.subresourceRange = {};
1757- DepthStencilViewCi.subresourceRange .aspectMask =
1758- VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
1759- DepthStencilViewCi.subresourceRange .baseMipLevel = 0 ;
1760- DepthStencilViewCi.subresourceRange .levelCount = 1 ;
1761- DepthStencilViewCi.subresourceRange .baseArrayLayer = 0 ;
1762- DepthStencilViewCi.subresourceRange .layerCount = 1 ;
1763- DepthStencilViewCi.image = IS.DepthStencil .Image ;
1764- if (vkCreateImageView (Device, &DepthStencilViewCi, nullptr , &Views[1 ]))
1765- return llvm::createStringError (
1766- std::errc::device_or_resource_busy,
1767- " Failed to create depth stencil image view." );
1768- IS.ImageViews .push_back (Views[1 ]);
1696+ llvm::Error createFrameBuffer (InvocationState &IS) {
1697+ std::array<VkImageView, 2 > Views = {IS.RenderTarget ->View ,
1698+ IS.DepthStencil ->View };
17691699
17701700 VkFramebufferCreateInfo FbufCreateInfo = {};
17711701 FbufCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
17721702 FbufCreateInfo.renderPass = IS.RenderPass ;
17731703 FbufCreateInfo.attachmentCount = Views.size ();
17741704 FbufCreateInfo.pAttachments = Views.data ();
1775- FbufCreateInfo.width = P. Bindings . RTargetBufferPtr -> OutputProps .Width ;
1776- FbufCreateInfo.height = P. Bindings . RTargetBufferPtr -> OutputProps .Height ;
1705+ FbufCreateInfo.width = IS. RenderTarget -> Desc .Width ;
1706+ FbufCreateInfo.height = IS. RenderTarget -> Desc .Height ;
17771707 FbufCreateInfo.layers = 1 ;
17781708
17791709 if (vkCreateFramebuffer (Device, &FbufCreateInfo, nullptr , &IS.FrameBuffer ))
@@ -2319,9 +2249,21 @@ class VulkanDevice : public offloadtest::Device {
23192249 copyResourceDataToDevice (IS, R);
23202250
23212251 if (P.isGraphics ()) {
2252+ const auto *ColorCV =
2253+ std::get_if<ClearColor>(&*IS.RenderTarget ->Desc .OptimizedClearValue );
2254+ if (!ColorCV)
2255+ return llvm::createStringError (
2256+ std::errc::invalid_argument,
2257+ " Render target clear value must be a ClearColor." );
2258+ const auto *DepthCV = std::get_if<ClearDepthStencil>(
2259+ &*IS.DepthStencil ->Desc .OptimizedClearValue );
2260+ if (!DepthCV)
2261+ return llvm::createStringError (
2262+ std::errc::invalid_argument,
2263+ " Depth/stencil clear value must be a ClearDepthStencil." );
23222264 VkClearValue ClearValues[2 ] = {};
2323- ClearValues[0 ].color = {{0 . 0f , 0 . 0f , 0 . 0f , 0 . 0f }};
2324- ClearValues[1 ].depthStencil = {1 . 0f , 0 };
2265+ ClearValues[0 ].color = {{ColorCV-> R , ColorCV-> G , ColorCV-> B , ColorCV-> A }};
2266+ ClearValues[1 ].depthStencil = {DepthCV-> Depth , DepthCV-> Stencil };
23252267
23262268 VkRenderPassBeginInfo RenderPassBeginInfo = {};
23272269 RenderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
@@ -2502,8 +2444,6 @@ class VulkanDevice : public offloadtest::Device {
25022444 vkDestroyBuffer (Device, IS.VertexBuffer ->Host .Buffer , nullptr );
25032445 vkFreeMemory (Device, IS.VertexBuffer ->Host .Memory , nullptr );
25042446 }
2505- vkDestroyImage (Device, IS.DepthStencil .Image , nullptr );
2506- vkFreeMemory (Device, IS.DepthStencil .Memory , nullptr );
25072447 vkDestroyFramebuffer (Device, IS.FrameBuffer , nullptr );
25082448 vkDestroyRenderPass (Device, IS.RenderPass , nullptr );
25092449 }
0 commit comments