Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
GraphicsHelpers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "FlingVulkan.h"
4 #include "FlingTypes.h"
5 #include "File.h"
6 #include "Texture.h"
7 #include "Buffer.h"
8 
9 #define VK_CHECK_RESULT(f) \
10 { \
11  VkResult res = (f); \
12  if (res != VK_SUCCESS) \
13  { \
14  F_LOG_ERROR("VkResult is {} in {} at line {}", res, __FILE__, __LINE__); \
15  assert(res == VK_SUCCESS); \
16  } \
17 }
18 
19 namespace Fling
20 {
21  namespace GraphicsHelpers
22  {
31  UINT32 FindMemoryType(VkPhysicalDevice t_PhysicalDevice, UINT32 t_Filter, VkMemoryPropertyFlags t_Props);
32 
33  void CreateBuffer(VkDevice t_Device, VkPhysicalDevice t_PhysicalDevice, VkDeviceSize t_Size, VkBufferUsageFlags t_Usage, VkMemoryPropertyFlags t_Properties, VkBuffer& t_Buffer, VkDeviceMemory& t_BuffMemory);
34 
35  VkCommandBuffer BeginSingleTimeCommands();
36 
37  void EndSingleTimeCommands(VkCommandBuffer t_CommandBuffer);
38 
39  void CreateVkImage(
40  VkDevice t_Dev,
41  UINT32 t_Width,
42  UINT32 t_Height,
43  VkFormat t_Format,
44  VkImageTiling t_Tiling,
45  VkImageUsageFlags t_Useage,
46  VkMemoryPropertyFlags t_Props,
47  VkImage& t_Image,
48  VkDeviceMemory& t_Memory,
49  VkSampleCountFlagBits t_NumSamples = VK_SAMPLE_COUNT_1_BIT
50  );
51 
52  VkSemaphore CreateSemaphore(VkDevice t_Dev);
53 
54  void CreateVkImage(
55  VkDevice t_Dev,
56  UINT32 t_Width,
57  UINT32 t_Height,
58  UINT32 t_MipLevels,
59  UINT32 t_Depth,
60  UINT32 t_ArrayLayers,
61  VkFormat t_Format,
62  VkImageTiling t_Tiling,
63  VkImageUsageFlags t_Useage,
64  VkMemoryPropertyFlags t_Props,
65  VkImageCreateFlags t_flags,
66  VkImage& t_Image,
67  VkDeviceMemory& t_Memory,
68  VkSampleCountFlagBits t_NumSamples = VK_SAMPLE_COUNT_1_BIT
69  );
70 
71  void CreateVkSampler(
72  VkFilter t_magFilter,
73  VkFilter t_minFilter,
74  VkSamplerMipmapMode t_mipmapMode,
75  VkSamplerAddressMode t_addressModeU,
76  VkSamplerAddressMode t_addressModeV,
77  VkSamplerAddressMode t_addressModeM,
78  VkBorderColor t_borderColor,
79  VkSampler& t_sampler
80  );
81 
82  void SetImageLayout(
83  VkCommandBuffer t_cmdbuffer,
84  VkImage t_image,
85  VkImageAspectFlags t_aspectMask,
86  VkImageLayout t_oldImageLayout,
87  VkImageLayout t_newImageLayout,
88  VkPipelineStageFlags t_srcStageMask,
89  VkPipelineStageFlags t_dstStageMask
90  );
91 
92  void SetImageLayout(
93  VkCommandBuffer t_cmdbuffer,
94  VkImage t_image,
95  VkImageLayout t_oldImageLayout,
96  VkImageLayout t_newImageLayout,
97  VkImageSubresourceRange t_subresourceRange,
98  VkPipelineStageFlags t_srcStageMask,
99  VkPipelineStageFlags t_dstStageMask
100  );
101 
102  void CreateCommandPool(
103  VkCommandPool* t_commandPool,
104  VkCommandPoolCreateFlags t_flags
105  );
106 
108  VkCommandBuffer* t_commandBuffer,
109  UINT32 t_commandBufferCount,
110  VkCommandPool& t_commandPool
111  );
112 
113  void CreatePipelineCache(VkPipelineCache& t_PipelineCache);
114 
115  VkShaderModule CreateShaderModule(std::shared_ptr<File> t_ShaderCode);
116 
120  VkImageView CreateVkImageView(VkImage t_Image, VkFormat t_Format, VkImageAspectFlags t_AspectFalgs, UINT32 t_MipLevels = 1);
121 
122  VkFormat FindSupportedFormat(const std::vector<VkFormat>& t_Candidates, VkImageTiling t_Tiling, VkFormatFeatureFlags t_Features);
123 
125  VkImage t_Image,
126  VkFormat t_Format,
127  VkImageLayout t_oldLayout,
128  VkImageLayout t_NewLayout,
129  UINT32 t_MipLevels = 1
130  );
131 
135  bool HasStencilComponent(VkFormat t_format);
136 
137  } // namespace GraphicsHelpers
138 
139 
140  // Some helpers for Vulkan initialization
141  // Grabbed these from https://github.com/SaschaWillems/Vulkan/tree/master/examples/dynamicuniformbuffer
142  namespace Initializers
143  {
144  VkMappedMemoryRange MappedMemoryRange();
145 
146  VkDescriptorPoolSize DescriptorPoolSize(VkDescriptorType t_type, UINT32 t_descriptorCount);
147 
148  VkPipelineVertexInputStateCreateInfo PiplineVertexInptStateCreateInfo();
149 
150  VkDescriptorPoolCreateInfo DescriptorPoolCreateInfo(
151  const std::vector<VkDescriptorPoolSize>& t_poolSizes,
152  UINT32 t_maxSets
153  );
154 
155  VkMemoryAllocateInfo MemoryAllocateInfo();
156 
157  VkDescriptorSetLayoutBinding DescriptorSetLayoutBindings(
158  VkDescriptorType t_type,
159  VkShaderStageFlags t_stageFlags,
160  UINT32 t_binding,
161  UINT32 t_descriptorCount = 1
162  );
163 
164  VkDescriptorSetLayoutCreateInfo DescriptorSetLayoutCreateInfo(
165  const std::vector<VkDescriptorSetLayoutBinding>& t_bindings
166  );
167 
168  inline VkRenderPassBeginInfo RenderPassBeginInfo()
169  {
170  VkRenderPassBeginInfo renderPassBeginInfo{};
171  renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
172  return renderPassBeginInfo;
173  }
174 
175  VkWriteDescriptorSet WriteDescriptorSetUniform(
176  Buffer* t_Buffer,
177  VkDescriptorSet t_DstSet,
178  UINT32 t_Binding,
179  UINT32 t_Set = 0,
180  VkDeviceSize t_Offset = 0);
181 
182  VkWriteDescriptorSet WriteDescriptorSetImage(
183  Texture* t_Image,
184  VkDescriptorSet t_DstSet,
185  UINT32 t_Binding,
186  UINT32 t_Set = 0,
187  VkDeviceSize t_Offset = 0);
188 
189  VkSamplerCreateInfo SamplerCreateInfo();
190 
191  VkImageViewCreateInfo ImageViewCreateInfo();
192 
193  VkDescriptorSetAllocateInfo DescriptorSetAllocateInfo(
194  VkDescriptorPool t_descriptorPool,
195  const VkDescriptorSetLayout* t_pSetLayouts,
196  UINT32 t_descriptorSetCount
197  );
198 
199  VkDescriptorImageInfo DescriptorImageInfo(
200  VkSampler t_sampler,
201  VkImageView t_imageView,
202  VkImageLayout t_imageLayout
203  );
204 
205  VkWriteDescriptorSet WriteDescriptorSet(
206  VkDescriptorSet t_dstSet,
207  VkDescriptorType t_type,
208  UINT32 t_binding,
209  VkDescriptorImageInfo* imageInfo,
210  UINT32 descriptorCount = 1
211  );
212 
213  VkPushConstantRange PushConstantRange(
214  VkShaderStageFlags t_stageFlags,
215  UINT32 t_size,
216  UINT32 t_offset
217  );
218 
219  VkPipelineLayoutCreateInfo PiplineLayoutCreateInfo(
220  const VkDescriptorSetLayout* t_pSetLayouts,
221  UINT32 t_setLayoutCount = 1
222  );
223 
224  VkPipelineInputAssemblyStateCreateInfo PipelineInputAssemblyStateCreateInfo(
225  VkPrimitiveTopology t_topology,
226  VkPipelineInputAssemblyStateCreateFlags t_flags,
227  VkBool32 t_primitiveRestartEnable
228  );
229 
230  VkPipelineRasterizationStateCreateInfo PipelineRasterizationStateCreateInfo(
231  VkPolygonMode t_polygonMode,
232  VkCullModeFlags t_cullMode,
233  VkFrontFace t_frontFace,
234  VkPipelineRasterizationStateCreateFlags t_flags = 0
235  );
236 
237  VkPipelineColorBlendStateCreateInfo PipelineColorBlendStateCreateInfo(
238  UINT32 t_attachmentCount,
239  const VkPipelineColorBlendAttachmentState* t_pAttachments
240  );
241 
242  VkPipelineColorBlendAttachmentState PipelineColorBlendAttachmentState(
243  VkColorComponentFlags t_colorWriteMask,
244  VkBool32 t_blendEnable
245  );
246 
247  inline VkPipelineDepthStencilStateCreateInfo PipelineDepthStencilStateCreateInfo(
248  VkBool32 depthTestEnable,
249  VkBool32 depthWriteEnable,
250  VkCompareOp depthCompareOp)
251  {
252  VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo{};
253  pipelineDepthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
254  pipelineDepthStencilStateCreateInfo.depthTestEnable = depthTestEnable;
255  pipelineDepthStencilStateCreateInfo.depthWriteEnable = depthWriteEnable;
256  pipelineDepthStencilStateCreateInfo.depthCompareOp = depthCompareOp;
257  pipelineDepthStencilStateCreateInfo.front = pipelineDepthStencilStateCreateInfo.back;
258  pipelineDepthStencilStateCreateInfo.back.compareOp = VK_COMPARE_OP_ALWAYS;
259  return pipelineDepthStencilStateCreateInfo;
260  }
261 
262  inline VkPipelineDynamicStateCreateInfo PipelineDynamicStateCreateInfo(
263  const VkDynamicState* pDynamicStates,
264  uint32_t dynamicStateCount,
265  VkPipelineDynamicStateCreateFlags flags = 0)
266  {
267  VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo{};
268  pipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
269  pipelineDynamicStateCreateInfo.pDynamicStates = pDynamicStates;
270  pipelineDynamicStateCreateInfo.dynamicStateCount = dynamicStateCount;
271  pipelineDynamicStateCreateInfo.flags = flags;
272  return pipelineDynamicStateCreateInfo;
273  }
274 
275  VkPipelineDepthStencilStateCreateInfo DepthStencilState(
276  VkBool32 t_depthTestEnable,
277  VkBool32 t_depthWriteEnable,
278  VkCompareOp t_depthCompareOp
279  );
280 
281  VkPipelineViewportStateCreateInfo PipelineViewportStateCreateInfo(
282  UINT32 t_viewportCount,
283  UINT32 t_scissorCount,
284  VkPipelineViewportStateCreateFlags t_flags = 0
285  );
286 
287  VkPipelineMultisampleStateCreateInfo PipelineMultiSampleStateCreateInfo(
288  VkSampleCountFlagBits t_rasterizationSamples,
289  VkPipelineMultisampleStateCreateFlags t_flags = 0
290  );
291 
292  VkPipelineDynamicStateCreateInfo PipelineDynamicStateCreateInfo(
293  const std::vector<VkDynamicState>& t_pDynamicStates,
294  VkPipelineDynamicStateCreateFlags t_flags = 0
295  );
296 
297  VkGraphicsPipelineCreateInfo PipelineCreateInfo(
298  VkPipelineLayout t_layout,
299  VkRenderPass t_renderPass,
300  VkPipelineCreateFlags t_flags = 0
301  );
302 
303  VkVertexInputBindingDescription VertexInputBindingDescription(
304  UINT32 t_binding,
305  UINT32 t_stride,
306  VkVertexInputRate t_inputRate
307  );
308 
309  VkVertexInputAttributeDescription VertexInputAttributeDescription(
310  UINT32 t_binding,
311  UINT32 t_location,
312  VkFormat t_format,
313  UINT32 t_offset
314  );
315 
316  VkViewport Viewport(
317  float t_width,
318  float t_height,
319  float t_minDepth,
320  float t_maxDepth
321  );
322 
323  VkDescriptorSetLayoutBinding DescriptorSetLayoutBinding(
324  VkDescriptorType type,
325  VkShaderStageFlags stageFlags,
326  uint32_t binding,
327  uint32_t descriptorCount = 1
328  );
329 
330  VkWriteDescriptorSet WriteDescriptorSet(
331  VkDescriptorSet dstSet,
332  VkDescriptorType type,
333  uint32_t binding,
334  VkDescriptorBufferInfo* bufferInfo,
335  uint32_t descriptorCount = 1
336  );
337 
338  VkRect2D Rect2D(
339  int32_t width,
340  int32_t height,
341  int32_t offsetX,
342  int32_t offsetY
343  );
344  }
345 
346 } // namespace Fling
VkFormat FindSupportedFormat(const std::vector< VkFormat > &t_Candidates, VkImageTiling t_Tiling, VkFormatFeatureFlags t_Features)
Definition: GraphicsHelpers.cpp:566
VkDescriptorSetLayoutBinding DescriptorSetLayoutBinding(VkDescriptorType type, VkShaderStageFlags stageFlags, uint32_t binding, uint32_t descriptorCount=1)
Definition: GraphicsHelpers.cpp:629
VkWriteDescriptorSet WriteDescriptorSetImage(Texture *t_Image, VkDescriptorSet t_DstSet, UINT32 t_Binding, UINT32 t_Set=0, VkDeviceSize t_Offset=0)
Definition: GraphicsHelpers.cpp:731
bool HasStencilComponent(VkFormat t_format)
Returns true if the given format has a stencil component.
Definition: GraphicsHelpers.cpp:613
VkRenderPassBeginInfo RenderPassBeginInfo()
Definition: GraphicsHelpers.h:168
VkPipelineDynamicStateCreateInfo PipelineDynamicStateCreateInfo(const std::vector< VkDynamicState > &t_pDynamicStates, VkPipelineDynamicStateCreateFlags t_flags=0)
Definition: GraphicsHelpers.cpp:901
void CreateCommandBuffers(VkCommandBuffer *t_commandBuffer, UINT32 t_commandBufferCount, VkCommandPool &t_commandPool)
Definition: GraphicsHelpers.cpp:408
VkPushConstantRange PushConstantRange(VkShaderStageFlags t_stageFlags, UINT32 t_size, UINT32 t_offset)
Definition: GraphicsHelpers.cpp:793
void SetImageLayout(VkCommandBuffer t_cmdbuffer, VkImage t_image, VkImageAspectFlags t_aspectMask, VkImageLayout t_oldImageLayout, VkImageLayout t_newImageLayout, VkPipelineStageFlags t_srcStageMask, VkPipelineStageFlags t_dstStageMask)
Definition: GraphicsHelpers.cpp:245
VkDescriptorSetLayoutCreateInfo DescriptorSetLayoutCreateInfo(const std::vector< VkDescriptorSetLayoutBinding > &t_bindings)
Definition: GraphicsHelpers.cpp:708
VkShaderModule CreateShaderModule(std::shared_ptr< File > t_ShaderCode)
Definition: GraphicsHelpers.cpp:592
void CreateVkSampler(VkFilter t_magFilter, VkFilter t_minFilter, VkSamplerMipmapMode t_mipmapMode, VkSamplerAddressMode t_addressModeU, VkSamplerAddressMode t_addressModeV, VkSamplerAddressMode t_addressModeM, VkBorderColor t_borderColor, VkSampler &t_sampler)
Definition: GraphicsHelpers.cpp:214
void CreateBuffer(VkDevice t_Device, VkPhysicalDevice t_PhysicalDevice, VkDeviceSize t_Size, VkBufferUsageFlags t_Usage, VkMemoryPropertyFlags t_Properties, VkBuffer &t_Buffer, VkDeviceMemory &t_BuffMemory)
Definition: GraphicsHelpers.cpp:30
VkDescriptorImageInfo DescriptorImageInfo(VkSampler t_sampler, VkImageView t_imageView, VkImageLayout t_imageLayout)
Definition: GraphicsHelpers.cpp:772
void CreateCommandPool(VkCommandPool *t_commandPool, VkCommandPoolCreateFlags t_flags)
Definition: GraphicsHelpers.cpp:392
An image represents a 2D file that has data about each pixel in the image.
Definition: Texture.h:11
VkVertexInputBindingDescription VertexInputBindingDescription(UINT32 t_binding, UINT32 t_stride, VkVertexInputRate t_inputRate)
Definition: GraphicsHelpers.cpp:928
VkDescriptorSetLayoutBinding DescriptorSetLayoutBindings(VkDescriptorType t_type, VkShaderStageFlags t_stageFlags, UINT32 t_binding, UINT32 t_descriptorCount=1)
Definition: GraphicsHelpers.cpp:695
VkWriteDescriptorSet WriteDescriptorSet(VkDescriptorSet dstSet, VkDescriptorType type, uint32_t binding, VkDescriptorBufferInfo *bufferInfo, uint32_t descriptorCount=1)
Definition: GraphicsHelpers.cpp:639
VkPipelineViewportStateCreateInfo PipelineViewportStateCreateInfo(UINT32 t_viewportCount, UINT32 t_scissorCount, VkPipelineViewportStateCreateFlags t_flags=0)
Definition: GraphicsHelpers.cpp:877
VkPipelineInputAssemblyStateCreateInfo PipelineInputAssemblyStateCreateInfo(VkPrimitiveTopology t_topology, VkPipelineInputAssemblyStateCreateFlags t_flags, VkBool32 t_primitiveRestartEnable)
Definition: GraphicsHelpers.cpp:813
VkPipelineDepthStencilStateCreateInfo DepthStencilState(VkBool32 t_depthTestEnable, VkBool32 t_depthWriteEnable, VkCompareOp t_depthCompareOp)
Definition: GraphicsHelpers.cpp:862
VkMappedMemoryRange MappedMemoryRange()
Definition: GraphicsHelpers.cpp:622
VkSamplerCreateInfo SamplerCreateInfo()
Definition: GraphicsHelpers.cpp:747
VkPipelineVertexInputStateCreateInfo PiplineVertexInptStateCreateInfo()
Definition: GraphicsHelpers.cpp:661
VkPipelineColorBlendAttachmentState PipelineColorBlendAttachmentState(VkColorComponentFlags t_colorWriteMask, VkBool32 t_blendEnable)
Definition: GraphicsHelpers.cpp:854
VkWriteDescriptorSet WriteDescriptorSetUniform(Buffer *t_Buffer, VkDescriptorSet t_DstSet, UINT32 t_Binding, UINT32 t_Set=0, VkDeviceSize t_Offset=0)
Definition: GraphicsHelpers.cpp:717
void CreatePipelineCache(VkPipelineCache &t_PipelineCache)
Definition: GraphicsHelpers.cpp:427
VkGraphicsPipelineCreateInfo PipelineCreateInfo(VkPipelineLayout t_layout, VkRenderPass t_renderPass, VkPipelineCreateFlags t_flags=0)
Definition: GraphicsHelpers.cpp:913
void TransitionImageLayout(VkImage t_Image, VkFormat t_Format, VkImageLayout t_oldLayout, VkImageLayout t_NewLayout, UINT32 t_MipLevels=1)
Definition: GraphicsHelpers.cpp:441
VkViewport Viewport(float t_width, float t_height, float t_minDepth, float t_maxDepth)
Definition: GraphicsHelpers.cpp:954
VkPipelineLayoutCreateInfo PiplineLayoutCreateInfo(const VkDescriptorSetLayout *t_pSetLayouts, UINT32 t_setLayoutCount=1)
Definition: GraphicsHelpers.cpp:802
VkSemaphore CreateSemaphore(VkDevice t_Dev)
Definition: GraphicsHelpers.cpp:203
VkPipelineColorBlendStateCreateInfo PipelineColorBlendStateCreateInfo(UINT32 t_attachmentCount, const VkPipelineColorBlendAttachmentState *t_pAttachments)
Definition: GraphicsHelpers.cpp:843
VkPipelineRasterizationStateCreateInfo PipelineRasterizationStateCreateInfo(VkPolygonMode t_polygonMode, VkCullModeFlags t_cullMode, VkFrontFace t_frontFace, VkPipelineRasterizationStateCreateFlags t_flags=0)
Definition: GraphicsHelpers.cpp:826
VkDescriptorPoolSize DescriptorPoolSize(VkDescriptorType t_type, UINT32 t_descriptorCount)
Definition: GraphicsHelpers.cpp:668
VkVertexInputAttributeDescription VertexInputAttributeDescription(UINT32 t_binding, UINT32 t_location, VkFormat t_format, UINT32 t_offset)
Definition: GraphicsHelpers.cpp:940
UINT32 FindMemoryType(VkPhysicalDevice t_PhysicalDevice, UINT32 t_Filter, VkMemoryPropertyFlags t_Props)
Find a suitable memory type for use on the current device.
Definition: GraphicsHelpers.cpp:11
void CreateVkImage(VkDevice t_Dev, UINT32 t_Width, UINT32 t_Height, VkFormat t_Format, VkImageTiling t_Tiling, VkImageUsageFlags t_Useage, VkMemoryPropertyFlags t_Props, VkImage &t_Image, VkDeviceMemory &t_Memory, VkSampleCountFlagBits t_NumSamples=VK_SAMPLE_COUNT_1_BIT)
Definition: GraphicsHelpers.cpp:110
A Buffer represents a Vulkan buffer with a size, buffer pointer, and buffer memory.
Definition: Buffer.h:11
VkMemoryAllocateInfo MemoryAllocateInfo()
Definition: GraphicsHelpers.cpp:688
VkRect2D Rect2D(int32_t width, int32_t height, int32_t offsetX, int32_t offsetY)
Definition: GraphicsHelpers.cpp:651
uint32_t UINT32
Definition: FlingTypes.h:13
Definition: Engine.h:29
VkImageView CreateVkImageView(VkImage t_Image, VkFormat t_Format, VkImageAspectFlags t_AspectFalgs, UINT32 t_MipLevels=1)
Create a an image view for Vulkan with the given format.
Definition: GraphicsHelpers.cpp:531
VkPipelineMultisampleStateCreateInfo PipelineMultiSampleStateCreateInfo(VkSampleCountFlagBits t_rasterizationSamples, VkPipelineMultisampleStateCreateFlags t_flags=0)
Definition: GraphicsHelpers.cpp:890
VkPipelineDepthStencilStateCreateInfo PipelineDepthStencilStateCreateInfo(VkBool32 depthTestEnable, VkBool32 depthWriteEnable, VkCompareOp depthCompareOp)
Definition: GraphicsHelpers.h:247
VkDescriptorSetAllocateInfo DescriptorSetAllocateInfo(VkDescriptorPool t_descriptorPool, const VkDescriptorSetLayout *t_pSetLayouts, UINT32 t_descriptorSetCount)
Definition: GraphicsHelpers.cpp:762
VkDescriptorPoolCreateInfo DescriptorPoolCreateInfo(const std::vector< VkDescriptorPoolSize > &t_poolSizes, UINT32 t_maxSets)
Definition: GraphicsHelpers.cpp:676
void EndSingleTimeCommands(VkCommandBuffer t_CommandBuffer)
Definition: GraphicsHelpers.cpp:89
VkImageViewCreateInfo ImageViewCreateInfo()
Definition: GraphicsHelpers.cpp:755
VkCommandBuffer BeginSingleTimeCommands()
Definition: GraphicsHelpers.cpp:65