Skip to content

Commit

Permalink
vulkan: Use image row pitch
Browse files Browse the repository at this point in the history
When importing a Vulkan external image, query and
pass OpenCL a row pitch if OpenCL is assuming linear
for the imported external handle type.  Additionally
fix a bug where OpenCL is being told to create mipmapped
images at all times.
  • Loading branch information
joshqti committed Sep 10, 2024
1 parent c32a767 commit edc2dc4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
18 changes: 17 additions & 1 deletion test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ getCLImageInfoFromVkImageInfo(const VkImageCreateInfo *VulkanImageCreateInfo,
img_desc->image_row_pitch = 0; // Row pitch set to zero as host_ptr is NULL
img_desc->image_slice_pitch =
img_desc->image_row_pitch * img_desc->image_height;
img_desc->num_mip_levels = 1;
img_desc->num_mip_levels = 0;
img_desc->num_samples = 0;
img_desc->buffer = NULL;

Expand Down Expand Up @@ -676,6 +676,14 @@ clExternalMemoryImage::clExternalMemoryImage(
std::vector<cl_mem_properties> extMemProperties1;
cl_device_id devList[] = { deviceId, NULL };

VulkanImageTiling vulkanImageTiling =
vkClExternalMemoryHandleTilingAssumption(
deviceId, externalMemoryHandleType, &errcode_ret);
if (CL_SUCCESS != errcode_ret)
{
throw std::runtime_error("Failed to query OpenCL tiling mode");
}

#ifdef _WIN32
if (!is_extension_available(devList[0], "cl_khr_external_memory_win32"))
{
Expand Down Expand Up @@ -747,6 +755,14 @@ clExternalMemoryImage::clExternalMemoryImage(
throw std::runtime_error("getCLImageInfoFromVkImageInfo failed!!!");
}

// If OpenCL will assume linear, query the Vulkan image's row pitch,
// otherwise it may not match OpenCL's assumption of the row pitch.
if (vulkanImageTiling == VULKAN_IMAGE_TILING_LINEAR)
{
VkSubresourceLayout subresourceLayout = image2D.getSubresourceLayout();
image_desc.image_row_pitch = subresourceLayout.rowPitch;
}

extMemProperties1.push_back(
(cl_mem_properties)CL_MEM_DEVICE_HANDLE_LIST_KHR);
extMemProperties1.push_back((cl_mem_properties)devList[0]);
Expand Down
4 changes: 3 additions & 1 deletion test_conformance/common/vulkan_wrapper/vulkan_api_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
VK_FUNC_DECL(vkEnumerateDeviceExtensionProperties) \
VK_FUNC_DECL(vkGetPhysicalDeviceSurfaceSupportKHR) \
VK_FUNC_DECL(vkImportSemaphoreFdKHR) \
VK_FUNC_DECL(vkGetPhysicalDeviceExternalSemaphorePropertiesKHR)
VK_FUNC_DECL(vkGetPhysicalDeviceExternalSemaphorePropertiesKHR) \
VK_FUNC_DECL(vkGetImageSubresourceLayout)
#define VK_WINDOWS_FUNC_LIST \
VK_FUNC_DECL(vkGetMemoryWin32HandleKHR) \
VK_FUNC_DECL(vkGetSemaphoreWin32HandleKHR) \
Expand Down Expand Up @@ -200,5 +201,6 @@
#define vkGetMemoryWin32HandleKHR _vkGetMemoryWin32HandleKHR
#define vkGetSemaphoreWin32HandleKHR _vkGetSemaphoreWin32HandleKHR
#define vkImportSemaphoreWin32HandleKHR _vkImportSemaphoreWin32HandleKHR
#define vkGetImageSubresourceLayout _vkGetImageSubresourceLayout

#endif //_vulkan_api_list_hpp_
10 changes: 10 additions & 0 deletions test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,16 @@ VulkanExtent3D VulkanImage2D::getExtent3D(uint32_t mipLevel) const
return VulkanExtent3D(width, height, depth);
}

VkSubresourceLayout VulkanImage2D::getSubresourceLayout() const
{
VkImageSubresource subresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 };

VkSubresourceLayout subresourceLayout = { 0 };
vkGetImageSubresourceLayout(m_device, m_vkImage, &subresource,
&subresourceLayout);
return subresourceLayout;
}

//////////////////////////////////
// VulkanImage3D implementation //
//////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ class VulkanImage2D : public VulkanImage {
VulkanSharingMode sharingMode = VULKAN_SHARING_MODE_EXCLUSIVE);
virtual ~VulkanImage2D();
virtual VulkanExtent3D getExtent3D(uint32_t mipLevel = 0) const;
virtual VkSubresourceLayout getSubresourceLayout() const;

VulkanImage2D(const VulkanImage2D &image2D);
};
Expand Down

0 comments on commit edc2dc4

Please sign in to comment.