@@ -510,7 +510,7 @@ class VulkanDevice : public offloadtest::Device {
510510 VkFramebuffer FrameBuffer = VK_NULL_HANDLE;
511511 std::shared_ptr<VulkanTexture> RenderTarget;
512512 std::shared_ptr<VulkanBuffer> RTReadback;
513- ImageRef DepthStencil = { 0 , 0 , 0 } ;
513+ std::shared_ptr<VulkanTexture> DepthStencil;
514514 std::optional<ResourceRef> VertexBuffer = std::nullopt ;
515515
516516 VkRenderPass RenderPass = VK_NULL_HANDLE;
@@ -1118,46 +1118,12 @@ class VulkanDevice : public offloadtest::Device {
11181118 }
11191119
11201120 llvm::Error createDepthStencil (Pipeline &P, InvocationState &IS) {
1121- // Create an optimal image used as the depth stencil attachment
1122- VkImageCreateInfo ImageCi = {};
1123- ImageCi.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1124- ImageCi.imageType = getVKImageType (ResourceKind::Texture2D);
1125- ImageCi.format = VK_FORMAT_D32_SFLOAT_S8_UINT;
1126- // Use example's height and width
1127- ImageCi.extent = {
1128- static_cast <uint32_t >(P.Bindings .RTargetBufferPtr ->OutputProps .Width ),
1129- static_cast <uint32_t >(P.Bindings .RTargetBufferPtr ->OutputProps .Height ),
1130- 1 };
1131- ImageCi.mipLevels = 1 ;
1132- ImageCi.arrayLayers = 1 ;
1133- ImageCi.samples = VK_SAMPLE_COUNT_1_BIT;
1134- ImageCi.tiling = VK_IMAGE_TILING_OPTIMAL;
1135- ImageCi.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1136- ImageCi.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1137- if (vkCreateImage (Device, &ImageCi, nullptr , &IS.DepthStencil .Image ))
1138- return llvm::createStringError (std::errc::device_or_resource_busy,
1139- " Depth stencil creation failed." );
1140-
1141- // Allocate memory for the image (device local) and bind it to our image
1142- VkMemoryAllocateInfo MemAlloc{};
1143- MemAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1144- VkMemoryRequirements MemReqs;
1145- vkGetImageMemoryRequirements (Device, IS.DepthStencil .Image , &MemReqs);
1146- MemAlloc.allocationSize = MemReqs.size ;
1147- llvm::Expected<uint32_t > MemIdx =
1148- getMemoryIndex (PhysicalDevice, MemReqs.memoryTypeBits ,
1149- VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
1150- if (!MemIdx)
1151- return MemIdx.takeError ();
1152-
1153- MemAlloc.memoryTypeIndex = *MemIdx;
1154- if (vkAllocateMemory (Device, &MemAlloc, nullptr , &IS.DepthStencil .Memory ))
1155- return llvm::createStringError (std::errc::not_enough_memory,
1156- " Depth stencil memory allocation failed." );
1157- if (vkBindImageMemory (Device, IS.DepthStencil .Image , IS.DepthStencil .Memory ,
1158- 0 ))
1159- return llvm::createStringError (std::errc::not_enough_memory,
1160- " Depth stencil memory binding failed." );
1121+ auto TexOrErr = offloadtest::createDepthStencil (
1122+ *this , P.Bindings .RTargetBufferPtr ->OutputProps .Width ,
1123+ P.Bindings .RTargetBufferPtr ->OutputProps .Height );
1124+ if (!TexOrErr)
1125+ return TexOrErr.takeError ();
1126+ IS.DepthStencil = std::static_pointer_cast<VulkanTexture>(*TexOrErr);
11611127 return llvm::Error::success ();
11621128 }
11631129
@@ -1172,6 +1138,8 @@ class VulkanDevice : public offloadtest::Device {
11721138 if (P.isGraphics ()) {
11731139 if (auto Err = createRenderTarget (P, IS))
11741140 return Err;
1141+ // TODO: Always created for graphics pipelines. Consider making this
1142+ // conditional on the pipeline definition.
11751143 if (auto Err = createDepthStencil (P, IS))
11761144 return Err;
11771145
@@ -1655,7 +1623,7 @@ class VulkanDevice : public offloadtest::Device {
16551623 DepthStencilViewCi.subresourceRange .levelCount = 1 ;
16561624 DepthStencilViewCi.subresourceRange .baseArrayLayer = 0 ;
16571625 DepthStencilViewCi.subresourceRange .layerCount = 1 ;
1658- DepthStencilViewCi.image = IS.DepthStencil . Image ;
1626+ DepthStencilViewCi.image = IS.DepthStencil -> Image ;
16591627 if (vkCreateImageView (Device, &DepthStencilViewCi, nullptr , &Views[1 ]))
16601628 return llvm::createStringError (
16611629 std::errc::device_or_resource_busy,
@@ -2041,8 +2009,9 @@ class VulkanDevice : public offloadtest::Device {
20412009 VkImageLayout OldLayout,
20422010 VkAccessFlags SrcAccessMask,
20432011 VkPipelineStageFlags SrcStageMask) {
2044- VkImageAspectFlags AspectMask =
2045- isDepth (Tex.Desc .Format ) ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
2012+ VkImageAspectFlags AspectMask = isDepth (Tex.Desc .Format )
2013+ ? VK_IMAGE_ASPECT_DEPTH_BIT
2014+ : VK_IMAGE_ASPECT_COLOR_BIT;
20462015
20472016 // Transition texture to transfer source.
20482017 VkImageSubresourceRange SubRange = {};
@@ -2213,11 +2182,21 @@ class VulkanDevice : public offloadtest::Device {
22132182 copyResourceDataToDevice (IS, R);
22142183
22152184 if (P.isGraphics ()) {
2216- const auto &CV =
2217- std::get<ClearColor>(*IS.RenderTarget ->Desc .OptimizedClearValue );
2185+ const auto *ColorCV =
2186+ std::get_if<ClearColor>(&*IS.RenderTarget ->Desc .OptimizedClearValue );
2187+ if (!ColorCV)
2188+ return llvm::createStringError (
2189+ std::errc::invalid_argument,
2190+ " Render target clear value must be a ClearColor." );
2191+ const auto *DepthCV = std::get_if<ClearDepthStencil>(
2192+ &*IS.DepthStencil ->Desc .OptimizedClearValue );
2193+ if (!DepthCV)
2194+ return llvm::createStringError (
2195+ std::errc::invalid_argument,
2196+ " Depth/stencil clear value must be a ClearDepthStencil." );
22182197 VkClearValue ClearValues[2 ] = {};
2219- ClearValues[0 ].color = {{CV. R , CV. G , CV. B , CV. A }};
2220- ClearValues[1 ].depthStencil = {1 . 0f , 0 };
2198+ ClearValues[0 ].color = {{ColorCV-> R , ColorCV-> G , ColorCV-> B , ColorCV-> A }};
2199+ ClearValues[1 ].depthStencil = {DepthCV-> Depth , DepthCV-> Stencil };
22212200
22222201 VkRenderPassBeginInfo RenderPassBeginInfo = {};
22232202 RenderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
@@ -2399,9 +2378,8 @@ class VulkanDevice : public offloadtest::Device {
23992378 vkFreeMemory (Device, IS.VertexBuffer ->Host .Memory , nullptr );
24002379 }
24012380 // Render target image and readback buffer are owned by
2402- // IS.RenderTarget and IS.RTReadback (shared_ptrs).
2403- vkDestroyImage (Device, IS.DepthStencil .Image , nullptr );
2404- vkFreeMemory (Device, IS.DepthStencil .Memory , nullptr );
2381+ // Render target, readback buffer, and depth stencil are owned by
2382+ // shared_ptrs (IS.RenderTarget, IS.RTReadback, IS.DepthStencil).
24052383 vkDestroyFramebuffer (Device, IS.FrameBuffer , nullptr );
24062384 vkDestroyRenderPass (Device, IS.RenderPass , nullptr );
24072385 }
0 commit comments