vk_initializers.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "vk_initializers.h"
  2. VkCommandPoolCreateInfo vkinit::command_pool_ci(uint32_t queueFamilyIndex, VkCommandPoolCreateFlags flags)
  3. {
  4. // Create a command pool for commands submitted to the graphics queue
  5. VkCommandPoolCreateInfo info{};
  6. info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  7. info.pNext = nullptr;
  8. // The command pool will be one that can submit graphics commands
  9. info.queueFamilyIndex = queueFamilyIndex;
  10. // We also want the pool to allow for resetting individual command buffers
  11. info.flags = flags;
  12. return info;
  13. }
  14. VkCommandBufferAllocateInfo vkinit::command_buffer_ai(VkCommandPool pool, uint32_t count, VkCommandBufferLevel level)
  15. {
  16. // Allocate the default command buffer that we will use for rendering
  17. VkCommandBufferAllocateInfo info{};
  18. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  19. info.pNext = nullptr;
  20. // Commands will be made from our m_CommandPool
  21. info.commandPool = pool;
  22. // Allocate just one command buffer
  23. info.commandBufferCount = count;
  24. // The command buffer is a primary buffer (that can submit to the queue)
  25. info.level = level;
  26. return info;
  27. }
  28. VkCommandBufferBeginInfo vkinit::command_buffer_bi(VkCommandBufferResetFlags flags)
  29. {
  30. VkCommandBufferBeginInfo info{};
  31. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  32. info.pNext = nullptr;
  33. info.pInheritanceInfo = nullptr;
  34. info.flags = flags;
  35. return info;
  36. }
  37. VkSubmitInfo vkinit::submit_info(VkCommandBuffer* cmd)
  38. {
  39. VkSubmitInfo info{};
  40. info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  41. info.pNext = nullptr;
  42. info.waitSemaphoreCount = 0;
  43. info.pWaitSemaphores = nullptr;
  44. info.pWaitDstStageMask = nullptr;
  45. info.commandBufferCount = 1;
  46. info.pCommandBuffers = cmd;
  47. info.signalSemaphoreCount = 0;
  48. info.pSignalSemaphores = nullptr;
  49. return info;
  50. }
  51. VkFenceCreateInfo vkinit::fence_ci(VkFenceCreateFlags flags)
  52. {
  53. VkFenceCreateInfo info = {};
  54. info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  55. info.pNext = nullptr;
  56. info.flags = flags;
  57. return info;
  58. }
  59. VkSemaphoreCreateInfo vkinit::semaphore_ci(VkSemaphoreCreateFlags flags)
  60. {
  61. VkSemaphoreCreateInfo info = {};
  62. info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  63. info.pNext = nullptr;
  64. info.flags = flags;
  65. return info;
  66. }
  67. VkPipelineShaderStageCreateInfo vkinit::pipeline_shader_stage_ci(VkShaderStageFlagBits stage, VkShaderModule shaderModule)
  68. {
  69. VkPipelineShaderStageCreateInfo info{};
  70. info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  71. info.pNext = nullptr;
  72. // Shader stage
  73. info.stage = stage;
  74. info.module = shaderModule;
  75. // Entry point in the shader
  76. info.pName = "main";
  77. return info;
  78. }
  79. VkPipelineVertexInputStateCreateInfo vkinit::vertex_input_state_ci()
  80. {
  81. VkPipelineVertexInputStateCreateInfo info{};
  82. info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  83. info.pNext = nullptr;
  84. // No vertex bindings or attributes
  85. info.vertexBindingDescriptionCount = 0;
  86. info.vertexAttributeDescriptionCount = 0;
  87. info.pVertexAttributeDescriptions = nullptr;
  88. info.pVertexBindingDescriptions = nullptr;
  89. return info;
  90. }
  91. VkPipelineInputAssemblyStateCreateInfo vkinit::input_assembly_ci(VkPrimitiveTopology topology)
  92. {
  93. VkPipelineInputAssemblyStateCreateInfo info{};
  94. info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  95. info.pNext = nullptr;
  96. // Primitive topology
  97. info.topology = topology;
  98. info.primitiveRestartEnable = VK_FALSE;
  99. return info;
  100. }
  101. VkPipelineRasterizationStateCreateInfo vkinit::rasterization_state_ci(VkPolygonMode polygonMode)
  102. {
  103. VkPipelineRasterizationStateCreateInfo info{};
  104. info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  105. info.pNext = nullptr;
  106. // How to handle polygons
  107. info.depthClampEnable = VK_FALSE;
  108. info.rasterizerDiscardEnable = VK_FALSE;
  109. // How to handle filling points between vertices
  110. info.polygonMode = polygonMode; // Solid / Wireframe
  111. info.lineWidth = 1.0f;
  112. // No backface culling
  113. info.cullMode = VK_CULL_MODE_NONE;
  114. info.frontFace = VK_FRONT_FACE_CLOCKWISE;
  115. // No depth bias
  116. info.depthBiasEnable = VK_FALSE;
  117. info.depthBiasConstantFactor = 0.0f;
  118. info.depthBiasClamp = 0.0f;
  119. info.depthBiasSlopeFactor = 0.0f;
  120. return info;
  121. }
  122. VkPipelineMultisampleStateCreateInfo vkinit::multisample_state_ci()
  123. {
  124. VkPipelineMultisampleStateCreateInfo info{};
  125. info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  126. info.pNext = nullptr;
  127. // No multisampling
  128. info.sampleShadingEnable = VK_FALSE;
  129. // 1 sample per pixel
  130. info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  131. info.minSampleShading = 1.0f;
  132. info.pSampleMask = nullptr;
  133. info.alphaToCoverageEnable = VK_FALSE;
  134. info.alphaToOneEnable = VK_FALSE;
  135. return info;
  136. }
  137. VkPipelineColorBlendAttachmentState vkinit::color_blend_attachment_state()
  138. {
  139. VkPipelineColorBlendAttachmentState info{};
  140. info.colorWriteMask =
  141. VK_COLOR_COMPONENT_R_BIT |
  142. VK_COLOR_COMPONENT_G_BIT |
  143. VK_COLOR_COMPONENT_B_BIT |
  144. VK_COLOR_COMPONENT_A_BIT;
  145. info.blendEnable = VK_FALSE;
  146. return info;
  147. }
  148. VkPipelineLayoutCreateInfo vkinit::pipeline_layout_ci()
  149. {
  150. VkPipelineLayoutCreateInfo info{};
  151. info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  152. info.pNext = nullptr;
  153. // Empty defaults
  154. info.flags = 0;
  155. info.setLayoutCount = 0;
  156. info.pSetLayouts = nullptr;
  157. info.pushConstantRangeCount = 0;
  158. info.pPushConstantRanges = nullptr;
  159. return info;
  160. }
  161. VkImageCreateInfo vkinit::image_ci(VkFormat format, VkImageUsageFlags usageFlags, VkExtent3D extent)
  162. {
  163. VkImageCreateInfo info = { };
  164. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  165. info.pNext = nullptr;
  166. info.imageType = VK_IMAGE_TYPE_2D;
  167. info.format = format;
  168. info.extent = extent;
  169. info.mipLevels = 1;
  170. info.arrayLayers = 1;
  171. info.samples = VK_SAMPLE_COUNT_1_BIT;
  172. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  173. info.usage = usageFlags;
  174. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  175. return info;
  176. }
  177. VkImageViewCreateInfo vkinit::image_view_ci(VkFormat format, VkImage image, VkImageAspectFlags aspectFlags)
  178. {
  179. //build a image-view for the depth image to use for rendering
  180. VkImageViewCreateInfo info = {};
  181. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  182. info.pNext = nullptr;
  183. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  184. info.image = image;
  185. info.format = format;
  186. info.subresourceRange.baseMipLevel = 0;
  187. info.subresourceRange.levelCount = 1;
  188. info.subresourceRange.baseArrayLayer = 0;
  189. info.subresourceRange.layerCount = 1;
  190. info.subresourceRange.aspectMask = aspectFlags;
  191. return info;
  192. }
  193. VkPipelineDepthStencilStateCreateInfo vkinit::depth_stencil_ci(bool bDepthTest, bool bDepthWrite, VkCompareOp compareOp)
  194. {
  195. VkPipelineDepthStencilStateCreateInfo info{};
  196. info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  197. info.pNext = nullptr;
  198. info.depthTestEnable = bDepthTest ? VK_TRUE : VK_FALSE;
  199. info.depthWriteEnable = bDepthWrite ? VK_TRUE : VK_FALSE;
  200. info.depthCompareOp = bDepthTest ? compareOp : VK_COMPARE_OP_ALWAYS;
  201. info.depthBoundsTestEnable = VK_FALSE;
  202. info.minDepthBounds = 0.0f;
  203. info.maxDepthBounds = 1.0f;
  204. info.stencilTestEnable = VK_FALSE;
  205. return info;
  206. }
  207. VkSamplerCreateInfo vkinit::sampler_ci(VkFilter filters, float max_anisotropy, VkSamplerAddressMode samplerAddressMode)
  208. {
  209. VkSamplerCreateInfo info{};
  210. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  211. info.pNext = nullptr;
  212. info.magFilter = filters;
  213. info.minFilter = filters;
  214. info.addressModeU = samplerAddressMode;
  215. info.addressModeV = samplerAddressMode;
  216. info.addressModeW = samplerAddressMode;
  217. info.anisotropyEnable = max_anisotropy > 0.01f ? VK_TRUE : VK_FALSE;
  218. info.maxAnisotropy = max_anisotropy;
  219. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  220. info.mipLodBias = 0.0f;
  221. info.minLod = 0.0f;
  222. info.maxLod = 0.0f;
  223. return info;
  224. }
  225. VkDescriptorSetLayoutBinding vkinit::descriptor_set_layout_binding(VkDescriptorType type, VkShaderStageFlags stageFlags, uint32_t binding)
  226. {
  227. VkDescriptorSetLayoutBinding setBind{};
  228. setBind.binding = binding;
  229. setBind.descriptorCount = 1;
  230. setBind.descriptorType = type;
  231. setBind.pImmutableSamplers = nullptr;
  232. setBind.stageFlags = stageFlags;
  233. return setBind;
  234. }
  235. VkWriteDescriptorSet vkinit::write_descriptor_buffer(VkDescriptorType type, VkDescriptorSet dstSet, VkDescriptorBufferInfo* bufferInfo, uint32_t binding)
  236. {
  237. VkWriteDescriptorSet write{};
  238. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  239. write.pNext = nullptr;
  240. write.dstSet = dstSet;
  241. write.dstBinding = binding;
  242. write.descriptorCount = 1;
  243. write.descriptorType = type;
  244. write.pBufferInfo = bufferInfo;
  245. return write;
  246. }
  247. VkWriteDescriptorSet vkinit::write_descriptor_image(VkDescriptorType type, VkDescriptorSet dstSet, VkDescriptorImageInfo* imageInfo, uint32_t binding)
  248. {
  249. VkWriteDescriptorSet write{};
  250. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  251. write.pNext = nullptr;
  252. write.dstBinding = binding;
  253. write.dstSet = dstSet;
  254. write.descriptorCount = 1;
  255. write.descriptorType = type;
  256. write.pImageInfo = imageInfo;
  257. return write;
  258. }