1 /// Conversion module between vulkan and graal types 2 module gfx.vulkan.conv; 3 4 package: 5 6 import gfx.bindings.vulkan; 7 8 import gfx.graal : Severity; 9 import gfx.graal.buffer; 10 import gfx.graal.cmd; 11 import gfx.graal.format; 12 import gfx.graal.image; 13 import gfx.graal.memory; 14 import gfx.graal.pipeline; 15 import gfx.graal.presentation; 16 import gfx.graal.queue; 17 import gfx.graal.renderpass; 18 import gfx.graal.types; 19 20 import std.typecons : Flag; 21 22 // structures and enums conversion 23 24 VkFormat toVk(in Format format) { 25 return cast(VkFormat)format; 26 } 27 28 Format toGfx(in VkFormat vkFormat) { 29 return cast(Format)vkFormat; 30 } 31 32 VkFormatFeatureFlags toVk(in FormatFeatures ff) { 33 return cast(VkFormatFeatureFlags)ff; 34 } 35 36 FormatFeatures toGfx(in VkFormatFeatureFlags vkFff) { 37 return cast(FormatFeatures)vkFff; 38 } 39 40 static assert(Format.rgba8_uNorm.toVk() == VK_FORMAT_R8G8B8A8_UNORM); 41 42 VkImageType toVk(in ImageType it) { 43 final switch (it) { 44 case ImageType.d1: 45 case ImageType.d1Array: 46 return VK_IMAGE_TYPE_1D; 47 case ImageType.d2: 48 case ImageType.d2Array: 49 case ImageType.cube: 50 case ImageType.cubeArray: 51 return VK_IMAGE_TYPE_2D; 52 case ImageType.d3: 53 return VK_IMAGE_TYPE_3D; 54 } 55 } 56 57 VkImageViewType toVkView(in ImageType it) { 58 final switch (it) { 59 case ImageType.d1: 60 return VK_IMAGE_VIEW_TYPE_1D; 61 case ImageType.d1Array: 62 return VK_IMAGE_VIEW_TYPE_1D_ARRAY; 63 case ImageType.d2: 64 return VK_IMAGE_VIEW_TYPE_2D; 65 case ImageType.d2Array: 66 return VK_IMAGE_VIEW_TYPE_2D_ARRAY; 67 case ImageType.cube: 68 return VK_IMAGE_VIEW_TYPE_CUBE; 69 case ImageType.cubeArray: 70 return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY; 71 case ImageType.d3: 72 return VK_IMAGE_VIEW_TYPE_3D; 73 } 74 } 75 76 VkImageLayout toVk(in ImageLayout layout) 77 { 78 return cast(VkImageLayout)layout; 79 } 80 81 VkImageTiling toVk(in ImageTiling tiling) 82 { 83 return cast(VkImageTiling)tiling; 84 } 85 86 MemoryRequirements toGfx(in VkMemoryRequirements mr) { 87 return MemoryRequirements( 88 cast(size_t)mr.size, cast(size_t)mr.alignment, mr.memoryTypeBits 89 ); 90 } 91 92 VkComponentSwizzle toVk(in CompSwizzle cs) { 93 final switch (cs) { 94 case CompSwizzle.identity : return VK_COMPONENT_SWIZZLE_IDENTITY; 95 case CompSwizzle.zero : return VK_COMPONENT_SWIZZLE_ZERO; 96 case CompSwizzle.one : return VK_COMPONENT_SWIZZLE_ONE; 97 case CompSwizzle.r : return VK_COMPONENT_SWIZZLE_R; 98 case CompSwizzle.g : return VK_COMPONENT_SWIZZLE_G; 99 case CompSwizzle.b : return VK_COMPONENT_SWIZZLE_B; 100 case CompSwizzle.a : return VK_COMPONENT_SWIZZLE_A; 101 } 102 } 103 104 VkComponentMapping toVk(in Swizzle swizzle) { 105 return VkComponentMapping( 106 swizzle[0].toVk(), swizzle[1].toVk(), swizzle[2].toVk(), swizzle[3].toVk(), 107 ); 108 } 109 110 VkImageSubresourceRange toVk(in ImageSubresourceRange isr) { 111 return VkImageSubresourceRange( 112 isr.aspect.aspectToVk(), 113 cast(uint)isr.firstLevel, cast(uint)isr.levels, 114 cast(uint)isr.firstLayer, cast(uint)isr.layers, 115 ); 116 } 117 118 SurfaceCaps toGfx(in VkSurfaceCapabilitiesKHR vkCaps) { 119 return SurfaceCaps( 120 vkCaps.minImageCount, vkCaps.maxImageCount, 121 [ vkCaps.minImageExtent.width, vkCaps.minImageExtent.height ], 122 [ vkCaps.maxImageExtent.width, vkCaps.maxImageExtent.height ], 123 vkCaps.maxImageArrayLayers, 124 imageUsageToGfx(vkCaps.supportedUsageFlags), 125 compositeAlphaToGfx(vkCaps.supportedCompositeAlpha), 126 ); 127 } 128 129 PresentMode toGfx(in VkPresentModeKHR pm) { 130 switch (pm) { 131 case VK_PRESENT_MODE_IMMEDIATE_KHR: 132 return PresentMode.immediate; 133 case VK_PRESENT_MODE_FIFO_KHR: 134 return PresentMode.fifo; 135 case VK_PRESENT_MODE_MAILBOX_KHR: 136 return PresentMode.mailbox; 137 default: 138 assert(false); 139 } 140 } 141 142 VkPresentModeKHR toVk(in PresentMode pm) { 143 final switch (pm) { 144 case PresentMode.immediate: 145 return VK_PRESENT_MODE_IMMEDIATE_KHR; 146 case PresentMode.fifo: 147 return VK_PRESENT_MODE_FIFO_KHR; 148 case PresentMode.mailbox: 149 return VK_PRESENT_MODE_MAILBOX_KHR; 150 } 151 } 152 153 @property bool hasGfxSupport(in VkPresentModeKHR pm) { 154 switch (pm) { 155 case VK_PRESENT_MODE_IMMEDIATE_KHR: 156 case VK_PRESENT_MODE_FIFO_KHR: 157 case VK_PRESENT_MODE_MAILBOX_KHR: 158 return true; 159 default: 160 return false; 161 } 162 } 163 164 VkAttachmentLoadOp toVk(in LoadOp op) { 165 final switch (op) { 166 case LoadOp.load: return VK_ATTACHMENT_LOAD_OP_LOAD; 167 case LoadOp.clear: return VK_ATTACHMENT_LOAD_OP_CLEAR; 168 case LoadOp.dontCare: return VK_ATTACHMENT_LOAD_OP_DONT_CARE; 169 } 170 } 171 172 VkAttachmentStoreOp toVk(in StoreOp op) { 173 final switch (op) { 174 case StoreOp.store: return VK_ATTACHMENT_STORE_OP_STORE; 175 case StoreOp.dontCare: return VK_ATTACHMENT_STORE_OP_DONT_CARE; 176 } 177 } 178 179 VkPrimitiveTopology toVk(in Primitive p) { 180 return cast(VkPrimitiveTopology)p; 181 } 182 183 VkFrontFace toVk(in FrontFace ff) { 184 return cast(VkFrontFace)ff; 185 } 186 187 VkPolygonMode toVk(in PolygonMode pm) { 188 return cast(VkPolygonMode)pm; 189 } 190 191 VkBlendFactor toVk(in BlendFactor bf) { 192 return cast(VkBlendFactor)bf; 193 } 194 195 VkBlendOp toVk(in BlendOp op) { 196 return cast(VkBlendOp)op; 197 } 198 199 VkLogicOp toVk(in LogicOp op) { 200 return cast(VkLogicOp)op; 201 } 202 203 VkDynamicState toVk(in DynamicState ds) { 204 return cast(VkDynamicState)ds; 205 } 206 207 VkRect2D toVk(in Rect r) { 208 return VkRect2D(VkOffset2D(r.x, r.y), VkExtent2D(r.width, r.height)); 209 } 210 211 VkFilter toVk(in Filter f) { 212 return cast(VkFilter)f; 213 } 214 215 VkSamplerMipmapMode toVkMipmapMode(in Filter f) { 216 return cast(VkSamplerMipmapMode)f; 217 } 218 219 VkSamplerAddressMode toVk(in WrapMode wm) { 220 return cast(VkSamplerAddressMode)wm; 221 } 222 223 VkBorderColor toVk(in BorderColor bc) { 224 return cast(VkBorderColor)bc; 225 } 226 227 VkCompareOp toVk(in CompareOp op) { 228 return cast(VkCompareOp)op; 229 } 230 231 VkDescriptorType toVk(in DescriptorType dt) { 232 return cast(VkDescriptorType)dt; 233 } 234 235 VkIndexType toVk(in IndexType type) { 236 return cast(VkIndexType)type; 237 } 238 239 VkPipelineBindPoint toVk(in PipelineBindPoint bp) { 240 return cast(VkPipelineBindPoint)bp; 241 } 242 243 244 // template conversion 245 246 VkBool32 flagToVk(F)(F val) 247 { 248 return val ? VK_TRUE : VK_FALSE; 249 } 250 251 // flags conversion 252 253 Severity debugReportFlagsToGfx(in VkDebugReportFlagsEXT flags) nothrow pure { 254 return cast(Severity) flags; 255 } 256 257 MemProps memPropsToGfx(in VkMemoryPropertyFlags vkFlags) 258 { 259 MemProps props = cast(MemProps)0; 260 if (vkFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { 261 props |= MemProps.deviceLocal; 262 } 263 if (vkFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { 264 props |= MemProps.hostVisible; 265 } 266 if (vkFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) { 267 props |= MemProps.hostCoherent; 268 } 269 if (vkFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { 270 props |= MemProps.hostCached; 271 } 272 if (vkFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { 273 props |= MemProps.lazilyAllocated; 274 } 275 return props; 276 } 277 278 QueueCap queueCapToGfx(in VkQueueFlags vkFlags) 279 { 280 QueueCap caps = cast(QueueCap)0; 281 if (vkFlags & VK_QUEUE_GRAPHICS_BIT) { 282 caps |= QueueCap.graphics; 283 } 284 if (vkFlags & VK_QUEUE_COMPUTE_BIT) { 285 caps |= QueueCap.compute; 286 } 287 return caps; 288 } 289 290 ImageUsage imageUsageToGfx(in VkImageUsageFlags usage) 291 { 292 return cast(ImageUsage)usage; 293 } 294 295 CompositeAlpha compositeAlphaToGfx(in VkCompositeAlphaFlagsKHR ca) 296 { 297 return cast(CompositeAlpha)ca; 298 } 299 300 VkBufferUsageFlags bufferUsageToVk(in BufferUsage usage) { 301 return cast(VkBufferUsageFlags)usage; 302 } 303 304 VkImageUsageFlags imageUsageToVk(in ImageUsage usage) 305 { 306 return cast(VkImageUsageFlags)usage; 307 } 308 309 VkImageAspectFlags aspectToVk(in ImageAspect aspect) 310 { 311 return cast(VkImageAspectFlags)aspect; 312 } 313 314 VkAccessFlags accessToVk(in Access access) 315 { 316 return cast(VkAccessFlags)access; 317 } 318 319 VkPipelineStageFlags pipelineStageToVk(in PipelineStage stage) 320 { 321 return cast(VkPipelineStageFlags)stage; 322 } 323 324 VkCompositeAlphaFlagBitsKHR compositeAlphaToVk(in CompositeAlpha ca) 325 { 326 return cast(VkCompositeAlphaFlagBitsKHR)ca; 327 } 328 329 VkShaderStageFlagBits shaderStageToVk(in ShaderStage ss) 330 { 331 return cast(VkShaderStageFlagBits)ss; 332 } 333 334 VkCullModeFlagBits cullModeToVk(in Cull c) { 335 return cast(VkCullModeFlagBits)c; 336 }