vk_initializers.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. return info;
  88. }
  89. VkPipelineInputAssemblyStateCreateInfo vkinit::input_assembly_ci(VkPrimitiveTopology topology)
  90. {
  91. VkPipelineInputAssemblyStateCreateInfo info{};
  92. info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  93. info.pNext = nullptr;
  94. // Primitive topology
  95. info.topology = topology;
  96. info.primitiveRestartEnable = VK_FALSE;
  97. return info;
  98. }
  99. VkPipelineRasterizationStateCreateInfo vkinit::rasterization_state_ci(VkPolygonMode polygonMode)
  100. {
  101. VkPipelineRasterizationStateCreateInfo info{};
  102. info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  103. info.pNext = nullptr;
  104. // How to handle polygons
  105. info.depthClampEnable = VK_FALSE;
  106. info.rasterizerDiscardEnable = VK_FALSE;
  107. // How to handle filling points between vertices
  108. info.polygonMode = polygonMode; // Solid / Wireframe
  109. info.lineWidth = 1.0f;
  110. // No backface culling
  111. info.cullMode = VK_CULL_MODE_NONE;
  112. info.frontFace = VK_FRONT_FACE_CLOCKWISE;
  113. // No depth bias
  114. info.depthBiasEnable = VK_FALSE;
  115. info.depthBiasConstantFactor = 0.0f;
  116. info.depthBiasClamp = 0.0f;
  117. info.depthBiasSlopeFactor = 0.0f;
  118. return info;
  119. }
  120. VkPipelineMultisampleStateCreateInfo vkinit::multisample_state_ci()
  121. {
  122. VkPipelineMultisampleStateCreateInfo info{};
  123. info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  124. info.pNext = nullptr;
  125. // No multisampling
  126. info.sampleShadingEnable = VK_FALSE;
  127. // 1 sample per pixel
  128. info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  129. info.minSampleShading = 1.0f;
  130. info.pSampleMask = nullptr;
  131. info.alphaToCoverageEnable = VK_FALSE;
  132. info.alphaToOneEnable = VK_FALSE;
  133. return info;
  134. }
  135. VkPipelineColorBlendAttachmentState vkinit::color_blend_attachment_state()
  136. {
  137. VkPipelineColorBlendAttachmentState info{};
  138. info.colorWriteMask =
  139. VK_COLOR_COMPONENT_R_BIT |
  140. VK_COLOR_COMPONENT_G_BIT |
  141. VK_COLOR_COMPONENT_B_BIT |
  142. VK_COLOR_COMPONENT_A_BIT;
  143. info.blendEnable = VK_FALSE;
  144. return info;
  145. }
  146. VkPipelineLayoutCreateInfo vkinit::pipeline_layout_ci()
  147. {
  148. VkPipelineLayoutCreateInfo info{};
  149. info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  150. info.pNext = nullptr;
  151. // Empty defaults
  152. info.flags = 0;
  153. info.setLayoutCount = 0;
  154. info.pSetLayouts = nullptr;
  155. info.pushConstantRangeCount = 0;
  156. info.pPushConstantRanges = nullptr;
  157. return info;
  158. }
  159. VkImageCreateInfo vkinit::image_ci(VkFormat format, VkImageUsageFlags usageFlags, VkExtent3D extent)
  160. {
  161. VkImageCreateInfo info = { };
  162. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  163. info.pNext = nullptr;
  164. info.imageType = VK_IMAGE_TYPE_2D;
  165. info.format = format;
  166. info.extent = extent;
  167. info.mipLevels = 1;
  168. info.arrayLayers = 1;
  169. info.samples = VK_SAMPLE_COUNT_1_BIT;
  170. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  171. info.usage = usageFlags;
  172. return info;
  173. }
  174. VkImageViewCreateInfo vkinit::image_view_ci(VkFormat format, VkImage image, VkImageAspectFlags aspectFlags)
  175. {
  176. //build a image-view for the depth image to use for rendering
  177. VkImageViewCreateInfo info = {};
  178. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  179. info.pNext = nullptr;
  180. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  181. info.image = image;
  182. info.format = format;
  183. info.subresourceRange.baseMipLevel = 0;
  184. info.subresourceRange.levelCount = 1;
  185. info.subresourceRange.baseArrayLayer = 0;
  186. info.subresourceRange.layerCount = 1;
  187. info.subresourceRange.aspectMask = aspectFlags;
  188. return info;
  189. }
  190. VkPipelineDepthStencilStateCreateInfo vkinit::depth_stencil_ci(bool bDepthTest, bool bDepthWrite, VkCompareOp compareOp)
  191. {
  192. VkPipelineDepthStencilStateCreateInfo info{};
  193. info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  194. info.pNext = nullptr;
  195. info.depthTestEnable = bDepthTest ? VK_TRUE : VK_FALSE;
  196. info.depthWriteEnable = bDepthWrite ? VK_TRUE : VK_FALSE;
  197. info.depthCompareOp = bDepthTest ? compareOp : VK_COMPARE_OP_ALWAYS;
  198. info.depthBoundsTestEnable = VK_FALSE;
  199. info.minDepthBounds = 0.0f;
  200. info.maxDepthBounds = 1.0f;
  201. info.stencilTestEnable = VK_FALSE;
  202. return info;
  203. }
  204. VkSamplerCreateInfo vkinit::sampler_ci(VkFilter filters, VkSamplerAddressMode samplerAddressMode)
  205. {
  206. VkSamplerCreateInfo info{};
  207. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  208. info.pNext = nullptr;
  209. info.magFilter = filters;
  210. info.minFilter = filters;
  211. info.addressModeU = samplerAddressMode;
  212. info.addressModeV = samplerAddressMode;
  213. info.addressModeW = samplerAddressMode;
  214. return info;
  215. }
  216. VkDescriptorSetLayoutBinding vkinit::descriptor_set_layout_binding(VkDescriptorType type, VkShaderStageFlags stageFlags, uint32_t binding)
  217. {
  218. VkDescriptorSetLayoutBinding setBind{};
  219. setBind.binding = binding;
  220. setBind.descriptorCount = 1;
  221. setBind.descriptorType = type;
  222. setBind.pImmutableSamplers = nullptr;
  223. setBind.stageFlags = stageFlags;
  224. return setBind;
  225. }
  226. VkWriteDescriptorSet vkinit::write_descriptor_buffer(VkDescriptorType type, VkDescriptorSet dstSet, VkDescriptorBufferInfo* bufferInfo, uint32_t binding)
  227. {
  228. VkWriteDescriptorSet write{};
  229. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  230. write.pNext = nullptr;
  231. write.dstSet = dstSet;
  232. write.dstBinding = binding;
  233. write.descriptorCount = 1;
  234. write.descriptorType = type;
  235. write.pBufferInfo = bufferInfo;
  236. return write;
  237. }
  238. VkWriteDescriptorSet vkinit::write_descriptor_image(VkDescriptorType type, VkDescriptorSet dstSet, VkDescriptorImageInfo* imageInfo, uint32_t binding)
  239. {
  240. VkWriteDescriptorSet write{};
  241. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  242. write.pNext = nullptr;
  243. write.dstBinding = binding;
  244. write.dstSet = dstSet;
  245. write.descriptorCount = 1;
  246. write.descriptorType = type;
  247. write.pImageInfo = imageInfo;
  248. return write;
  249. }