@@ -474,7 +474,7 @@ class VulkanDevice : public offloadtest::Device {
474474 VkFramebuffer FrameBuffer;
475475 std::shared_ptr<VulkanTexture> RenderTarget;
476476 std::shared_ptr<VulkanBuffer> RTReadback;
477- ImageRef DepthStencil = { 0 , 0 , 0 } ;
477+ std::shared_ptr<VulkanTexture> DepthStencil;
478478 std::optional<ResourceRef> VertexBuffer = std::nullopt ;
479479
480480 VkRenderPass RenderPass;
@@ -1070,46 +1070,12 @@ class VulkanDevice : public offloadtest::Device {
10701070 }
10711071
10721072 llvm::Error createDepthStencil (Pipeline &P, InvocationState &IS) {
1073- // Create an optimal image used as the depth stencil attachment
1074- VkImageCreateInfo ImageCi = {};
1075- ImageCi.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1076- ImageCi.imageType = VK_IMAGE_TYPE_2D;
1077- ImageCi.format = VK_FORMAT_D32_SFLOAT_S8_UINT;
1078- // Use example's height and width
1079- ImageCi.extent = {
1080- static_cast <uint32_t >(P.Bindings .RTargetBufferPtr ->OutputProps .Width ),
1081- static_cast <uint32_t >(P.Bindings .RTargetBufferPtr ->OutputProps .Height ),
1082- 1 };
1083- ImageCi.mipLevels = 1 ;
1084- ImageCi.arrayLayers = 1 ;
1085- ImageCi.samples = VK_SAMPLE_COUNT_1_BIT;
1086- ImageCi.tiling = VK_IMAGE_TILING_OPTIMAL;
1087- ImageCi.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1088- ImageCi.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1089- if (vkCreateImage (Device, &ImageCi, nullptr , &IS.DepthStencil .Image ))
1090- return llvm::createStringError (std::errc::device_or_resource_busy,
1091- " Depth stencil creation failed." );
1092-
1093- // Allocate memory for the image (device local) and bind it to our image
1094- VkMemoryAllocateInfo MemAlloc{};
1095- MemAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1096- VkMemoryRequirements MemReqs;
1097- vkGetImageMemoryRequirements (Device, IS.DepthStencil .Image , &MemReqs);
1098- MemAlloc.allocationSize = MemReqs.size ;
1099- llvm::Expected<uint32_t > MemIdx =
1100- getMemoryIndex (PhysicalDevice, MemReqs.memoryTypeBits ,
1101- VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
1102- if (!MemIdx)
1103- return MemIdx.takeError ();
1104-
1105- MemAlloc.memoryTypeIndex = *MemIdx;
1106- if (vkAllocateMemory (Device, &MemAlloc, nullptr , &IS.DepthStencil .Memory ))
1107- return llvm::createStringError (std::errc::not_enough_memory,
1108- " Depth stencil memory allocation failed." );
1109- if (vkBindImageMemory (Device, IS.DepthStencil .Image , IS.DepthStencil .Memory ,
1110- 0 ))
1111- return llvm::createStringError (std::errc::not_enough_memory,
1112- " Depth stencil memory binding failed." );
1073+ auto TexOrErr = offloadtest::createDepthStencil (
1074+ *this , P.Bindings .RTargetBufferPtr ->OutputProps .Width ,
1075+ P.Bindings .RTargetBufferPtr ->OutputProps .Height );
1076+ if (!TexOrErr)
1077+ return TexOrErr.takeError ();
1078+ IS.DepthStencil = std::static_pointer_cast<VulkanTexture>(*TexOrErr);
11131079 return llvm::Error::success ();
11141080 }
11151081
@@ -1124,6 +1090,8 @@ class VulkanDevice : public offloadtest::Device {
11241090 if (P.isGraphics ()) {
11251091 if (auto Err = createRenderTarget (P, IS))
11261092 return Err;
1093+ // TODO: Always created for graphics pipelines. Consider making this
1094+ // conditional on the pipeline definition.
11271095 if (auto Err = createDepthStencil (P, IS))
11281096 return Err;
11291097
@@ -1607,7 +1575,7 @@ class VulkanDevice : public offloadtest::Device {
16071575 DepthStencilViewCi.subresourceRange .levelCount = 1 ;
16081576 DepthStencilViewCi.subresourceRange .baseArrayLayer = 0 ;
16091577 DepthStencilViewCi.subresourceRange .layerCount = 1 ;
1610- DepthStencilViewCi.image = IS.DepthStencil . Image ;
1578+ DepthStencilViewCi.image = IS.DepthStencil -> Image ;
16111579 if (vkCreateImageView (Device, &DepthStencilViewCi, nullptr , &Views[1 ]))
16121580 return llvm::createStringError (
16131581 std::errc::device_or_resource_busy,
@@ -1993,8 +1961,9 @@ class VulkanDevice : public offloadtest::Device {
19931961 VkImageLayout OldLayout,
19941962 VkAccessFlags SrcAccessMask,
19951963 VkPipelineStageFlags SrcStageMask) {
1996- VkImageAspectFlags AspectMask =
1997- isDepth (Tex.Desc .Format ) ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1964+ VkImageAspectFlags AspectMask = isDepth (Tex.Desc .Format )
1965+ ? VK_IMAGE_ASPECT_DEPTH_BIT
1966+ : VK_IMAGE_ASPECT_COLOR_BIT;
19981967
19991968 // Transition texture to transfer source.
20001969 VkImageSubresourceRange SubRange = {};
@@ -2165,11 +2134,21 @@ class VulkanDevice : public offloadtest::Device {
21652134 copyResourceDataToDevice (IS, R);
21662135
21672136 if (P.isGraphics ()) {
2168- const auto &CV =
2169- std::get<ClearColor>(*IS.RenderTarget ->Desc .OptimizedClearValue );
2137+ const auto *ColorCV =
2138+ std::get_if<ClearColor>(&*IS.RenderTarget ->Desc .OptimizedClearValue );
2139+ if (!ColorCV)
2140+ return llvm::createStringError (
2141+ std::errc::invalid_argument,
2142+ " Render target clear value must be a ClearColor." );
2143+ const auto *DepthCV = std::get_if<ClearDepthStencil>(
2144+ &*IS.DepthStencil ->Desc .OptimizedClearValue );
2145+ if (!DepthCV)
2146+ return llvm::createStringError (
2147+ std::errc::invalid_argument,
2148+ " Depth/stencil clear value must be a ClearDepthStencil." );
21702149 VkClearValue ClearValues[2 ] = {};
2171- ClearValues[0 ].color = {{CV. R , CV. G , CV. B , CV. A }};
2172- ClearValues[1 ].depthStencil = {1 . 0f , 0 };
2150+ ClearValues[0 ].color = {{ColorCV-> R , ColorCV-> G , ColorCV-> B , ColorCV-> A }};
2151+ ClearValues[1 ].depthStencil = {DepthCV-> Depth , DepthCV-> Stencil };
21732152
21742153 VkRenderPassBeginInfo RenderPassBeginInfo = {};
21752154 RenderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
@@ -2346,9 +2325,8 @@ class VulkanDevice : public offloadtest::Device {
23462325 vkFreeMemory (Device, IS.VertexBuffer ->Host .Memory , nullptr );
23472326 }
23482327 // Render target image and readback buffer are owned by
2349- // IS.RenderTarget and IS.RTReadback (shared_ptrs).
2350- vkDestroyImage (Device, IS.DepthStencil .Image , nullptr );
2351- vkFreeMemory (Device, IS.DepthStencil .Memory , nullptr );
2328+ // Render target, readback buffer, and depth stencil are owned by
2329+ // shared_ptrs (IS.RenderTarget, IS.RTReadback, IS.DepthStencil).
23522330 vkDestroyFramebuffer (Device, IS.FrameBuffer , nullptr );
23532331 vkDestroyRenderPass (Device, IS.RenderPass , nullptr );
23542332 }
0 commit comments