ktxvulkan.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* -*- tab-width: 4; -*- */
  2. /* vi: set sw=2 ts=4 expandtab: */
  3. #ifndef KTX_H_C54B42AEE39611E68E1E4FF8C51D1C66
  4. #define KTX_H_C54B42AEE39611E68E1E4FF8C51D1C66
  5. /*
  6. * Copyright 2017-2020 The Khronos Group, Inc.
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. /**
  10. * @internal
  11. * @file
  12. * @~English
  13. *
  14. * @brief Declares the public functions and structures of the
  15. * KTX Vulkan texture loading API.
  16. *
  17. * A separate header file is used to avoid extra dependencies for those not
  18. * using Vulkan. The nature of the Vulkan API, rampant structures and enums,
  19. * means that vulkan.h must be included @e before including this file. The
  20. * alternative is duplicating unattractively large parts of it.
  21. *
  22. * @author Mark Callow, Edgewise Consulting
  23. *
  24. * $Date$
  25. */
  26. #include <ktx.h>
  27. #if 0
  28. /* Avoid Vulkan include file */
  29. #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
  30. #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  31. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
  32. #else
  33. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
  34. #endif
  35. VK_DEFINE_HANDLE(VkPhysicalDevice)
  36. VK_DEFINE_HANDLE(VkDevice)
  37. VK_DEFINE_HANDLE(VkQueue)
  38. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkCommandPool)
  39. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkDeviceMemory)
  40. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkImage)
  41. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkImageView)
  42. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSampler)
  43. #endif
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @struct ktxVulkanFunctions
  49. * @~English
  50. * @brief Struct for applications to pass Vulkan function pointers to the
  51. * ktxTexture_VkUpload functions via a ktxVulkanDeviceInfo struct.
  52. *
  53. * @c vkGetInstanceProcAddr and @c vkGetDeviceProcAddr should be set, others
  54. * are optional.
  55. */
  56. typedef struct ktxVulkanFunctions {
  57. // These are functions pointers we need to perform our vulkan duties.
  58. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
  59. PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;
  60. // These we optionally specify
  61. PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers;
  62. PFN_vkAllocateMemory vkAllocateMemory;
  63. PFN_vkBeginCommandBuffer vkBeginCommandBuffer;
  64. PFN_vkBindBufferMemory vkBindBufferMemory;
  65. PFN_vkBindImageMemory vkBindImageMemory;
  66. PFN_vkCmdBlitImage vkCmdBlitImage;
  67. PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage;
  68. PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier;
  69. PFN_vkCreateImage vkCreateImage;
  70. PFN_vkDestroyImage vkDestroyImage;
  71. PFN_vkCreateBuffer vkCreateBuffer;
  72. PFN_vkDestroyBuffer vkDestroyBuffer;
  73. PFN_vkCreateFence vkCreateFence;
  74. PFN_vkDestroyFence vkDestroyFence;
  75. PFN_vkEndCommandBuffer vkEndCommandBuffer;
  76. PFN_vkFreeCommandBuffers vkFreeCommandBuffers;
  77. PFN_vkFreeMemory vkFreeMemory;
  78. PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements;
  79. PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements;
  80. PFN_vkGetImageSubresourceLayout vkGetImageSubresourceLayout;
  81. PFN_vkGetPhysicalDeviceImageFormatProperties vkGetPhysicalDeviceImageFormatProperties;
  82. PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties;
  83. PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties;
  84. PFN_vkMapMemory vkMapMemory;
  85. PFN_vkQueueSubmit vkQueueSubmit;
  86. PFN_vkQueueWaitIdle vkQueueWaitIdle;
  87. PFN_vkUnmapMemory vkUnmapMemory;
  88. PFN_vkWaitForFences vkWaitForFences;
  89. } ktxVulkanFunctions;
  90. /**
  91. * @class ktxVulkanTexture
  92. * @~English
  93. * @brief Struct for returning information about the Vulkan texture image
  94. * created by the ktxTexture_VkUpload* functions.
  95. *
  96. * Creation of these objects is internal to the upload functions.
  97. */
  98. typedef struct ktxVulkanTexture
  99. {
  100. PFN_vkDestroyImage vkDestroyImage; /*!< Pointer to vkDestroyImage function */
  101. PFN_vkFreeMemory vkFreeMemory; /*!< Pointer to vkFreeMemory function */
  102. VkImage image; /*!< Handle to the Vulkan image created by the loader. */
  103. VkFormat imageFormat; /*!< Format of the image data. */
  104. VkImageLayout imageLayout; /*!< Layout of the created image. Has the same
  105. value as @p layout parameter passed to the
  106. loader. */
  107. VkDeviceMemory deviceMemory; /*!< The memory allocated for the image on
  108. the Vulkan device. */
  109. VkImageViewType viewType; /*!< ViewType corresponding to @p image. Reflects
  110. the dimensionality, cubeness and arrayness
  111. of the image. */
  112. uint32_t width; /*!< The width of the image. */
  113. uint32_t height; /*!< The height of the image. */
  114. uint32_t depth; /*!< The depth of the image. */
  115. uint32_t levelCount; /*!< The number of MIP levels in the image. */
  116. uint32_t layerCount; /*!< The number of array layers in the image. */
  117. } ktxVulkanTexture;
  118. KTX_API void KTX_APIENTRY
  119. ktxVulkanTexture_Destruct(ktxVulkanTexture* This, VkDevice device,
  120. const VkAllocationCallbacks* pAllocator);
  121. /**
  122. * @class ktxVulkanDeviceInfo
  123. * @~English
  124. * @brief Struct for passing information about the Vulkan device on which
  125. * to create images to the texture image loading functions.
  126. *
  127. * Avoids passing a large number of parameters to each loading function.
  128. * Use of ktxVulkanDeviceInfo_create() or ktxVulkanDeviceInfo_construct() to
  129. * populate this structure is highly recommended.
  130. *
  131. * @code
  132. ktxVulkanDeviceInfo vdi;
  133. ktxVulkanTexture texture;
  134. vdi = ktxVulkanDeviceInfo_create(physicalDevice,
  135. device,
  136. queue,
  137. cmdPool,
  138. &allocator);
  139. ktxLoadVkTextureN("texture_1.ktx", vdi, &texture, NULL, NULL);
  140. // ...
  141. ktxLoadVkTextureN("texture_n.ktx", vdi, &texture, NULL, NULL);
  142. ktxVulkanDeviceInfo_destroy(vdi);
  143. * @endcode
  144. */
  145. typedef struct ktxVulkanDeviceInfo {
  146. VkInstance instance; /*!< Instance used to communicate with vulkan. */
  147. VkPhysicalDevice physicalDevice; /*!< Handle of the physical device. */
  148. VkDevice device; /*!< Handle of the logical device. */
  149. VkQueue queue; /*!< Handle to the queue to which to submit commands. */
  150. VkCommandBuffer cmdBuffer; /*!< Handle of the cmdBuffer to use. */
  151. /** Handle of the command pool from which to allocate the command buffer. */
  152. VkCommandPool cmdPool;
  153. /** Pointer to the allocator to use for the command buffer and created
  154. * images.
  155. */
  156. const VkAllocationCallbacks* pAllocator;
  157. /** Memory properties of the Vulkan physical device. */
  158. VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
  159. /** The functions needed to operate functions */
  160. ktxVulkanFunctions vkFuncs;
  161. } ktxVulkanDeviceInfo;
  162. KTX_API ktxVulkanDeviceInfo* KTX_APIENTRY
  163. ktxVulkanDeviceInfo_CreateEx(VkInstance instance, VkPhysicalDevice physicalDevice, VkDevice device,
  164. VkQueue queue, VkCommandPool cmdPool,
  165. const VkAllocationCallbacks* pAllocator,
  166. const ktxVulkanFunctions* pFunctions);
  167. KTX_API ktxVulkanDeviceInfo* KTX_APIENTRY
  168. ktxVulkanDeviceInfo_Create(VkPhysicalDevice physicalDevice, VkDevice device,
  169. VkQueue queue, VkCommandPool cmdPool,
  170. const VkAllocationCallbacks* pAllocator);
  171. KTX_API KTX_error_code KTX_APIENTRY
  172. ktxVulkanDeviceInfo_Construct(ktxVulkanDeviceInfo* This,
  173. VkPhysicalDevice physicalDevice, VkDevice device,
  174. VkQueue queue, VkCommandPool cmdPool,
  175. const VkAllocationCallbacks* pAllocator);
  176. KTX_API KTX_error_code KTX_APIENTRY
  177. ktxVulkanDeviceInfo_ConstructEx(ktxVulkanDeviceInfo* This,
  178. VkInstance instance,
  179. VkPhysicalDevice physicalDevice, VkDevice device,
  180. VkQueue queue, VkCommandPool cmdPool,
  181. const VkAllocationCallbacks* pAllocator,
  182. const ktxVulkanFunctions* pFunctions);
  183. KTX_API void KTX_APIENTRY
  184. ktxVulkanDeviceInfo_Destruct(ktxVulkanDeviceInfo* This);
  185. KTX_API void KTX_APIENTRY
  186. ktxVulkanDeviceInfo_Destroy(ktxVulkanDeviceInfo* This);
  187. KTX_API KTX_error_code KTX_APIENTRY
  188. ktxTexture_VkUploadEx(ktxTexture* This, ktxVulkanDeviceInfo* vdi,
  189. ktxVulkanTexture* vkTexture,
  190. VkImageTiling tiling,
  191. VkImageUsageFlags usageFlags,
  192. VkImageLayout finalLayout);
  193. KTX_API KTX_error_code KTX_APIENTRY
  194. ktxTexture_VkUpload(ktxTexture* texture, ktxVulkanDeviceInfo* vdi,
  195. ktxVulkanTexture *vkTexture);
  196. KTX_API KTX_error_code KTX_APIENTRY
  197. ktxTexture1_VkUploadEx(ktxTexture1* This, ktxVulkanDeviceInfo* vdi,
  198. ktxVulkanTexture* vkTexture,
  199. VkImageTiling tiling,
  200. VkImageUsageFlags usageFlags,
  201. VkImageLayout finalLayout);
  202. KTX_API KTX_error_code KTX_APIENTRY
  203. ktxTexture1_VkUpload(ktxTexture1* texture, ktxVulkanDeviceInfo* vdi,
  204. ktxVulkanTexture *vkTexture);
  205. KTX_API KTX_error_code KTX_APIENTRY
  206. ktxTexture2_VkUploadEx(ktxTexture2* This, ktxVulkanDeviceInfo* vdi,
  207. ktxVulkanTexture* vkTexture,
  208. VkImageTiling tiling,
  209. VkImageUsageFlags usageFlags,
  210. VkImageLayout finalLayout);
  211. KTX_API KTX_error_code KTX_APIENTRY
  212. ktxTexture2_VkUpload(ktxTexture2* texture, ktxVulkanDeviceInfo* vdi,
  213. ktxVulkanTexture *vkTexture);
  214. KTX_API VkFormat KTX_APIENTRY
  215. ktxTexture_GetVkFormat(ktxTexture* This);
  216. KTX_API VkFormat KTX_APIENTRY
  217. ktxTexture1_GetVkFormat(ktxTexture1* This);
  218. KTX_API VkFormat KTX_APIENTRY
  219. ktxTexture2_GetVkFormat(ktxTexture2* This);
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif /* KTX_H_A55A6F00956F42F3A137C11929827FE1 */