333 lines
8.0 KiB
C++
333 lines
8.0 KiB
C++
#ifndef INITIALIZERS_H
|
|
#define INITIALIZERS_H
|
|
// Normal header
|
|
#include "Common/Types.h"
|
|
#include <vulkan/vulkan_core.h>
|
|
|
|
namespace vkinit {
|
|
VkCommandPoolCreateInfo commandPoolCreateInfo(
|
|
uint32_t queueFamilyIndex,
|
|
VkCommandPoolCreateFlags flags = 0
|
|
);
|
|
|
|
VkCommandBufferAllocateInfo commandBufferAllocateInfo(
|
|
VkCommandPool pool,
|
|
uint32_t count
|
|
);
|
|
|
|
VkFenceCreateInfo fenceCreateInfo(
|
|
VkFenceCreateFlags flags = 0
|
|
);
|
|
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo (
|
|
VkSemaphoreCreateFlags flags = 0
|
|
);
|
|
|
|
VkCommandBufferBeginInfo commandBufferBeginInfo (
|
|
VkCommandBufferUsageFlags flags = 0
|
|
);
|
|
|
|
VkImageSubresourceRange imageSubresourceRange(
|
|
VkImageAspectFlags aspectMask
|
|
);
|
|
|
|
VkSemaphoreSubmitInfo semaphoreSubmitInfo(
|
|
VkPipelineStageFlags2 stageMask,
|
|
VkSemaphore semaphore
|
|
);
|
|
|
|
VkCommandBufferSubmitInfo commandBufferSubmitInfo (
|
|
VkCommandBuffer cmd
|
|
);
|
|
|
|
VkSubmitInfo2 submitInfo(
|
|
VkCommandBufferSubmitInfo* cmd,
|
|
VkSemaphoreSubmitInfo* signalSemaphoreInfo,
|
|
VkSemaphoreSubmitInfo* waitSemaphoreInfo
|
|
);
|
|
|
|
VkImageCreateInfo imageCreateInfo(
|
|
VkFormat format,
|
|
VkImageUsageFlags usageFlags,
|
|
VkExtent3D extent
|
|
);
|
|
|
|
VkImageViewCreateInfo imageViewCreateInfo(
|
|
VkFormat format,
|
|
VkImage image,
|
|
VkImageAspectFlags aspectFlags
|
|
);
|
|
|
|
VkRenderingAttachmentInfo attachmentInfo(
|
|
VkImageView view,
|
|
VkClearValue* clear,
|
|
VkImageLayout layout /*= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL*/
|
|
);
|
|
|
|
VkRenderingInfo renderingInfo(
|
|
VkExtent2D renderExtent,
|
|
VkRenderingAttachmentInfo* colorAttachment,
|
|
VkRenderingAttachmentInfo* depthAttachment
|
|
);
|
|
|
|
VkPipelineShaderStageCreateInfo pipelineShaderStageCreateInfo(
|
|
VkShaderStageFlagBits stage,
|
|
VkShaderModule shaderModule,
|
|
const char * entry = "main"
|
|
);
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
#ifdef INITIALIZERS_IMPL
|
|
#ifndef INITIALIZERS_IMPL_H
|
|
#define INITIALIZERS_IMPL_H
|
|
// Implementation
|
|
namespace vkinit {
|
|
VkCommandPoolCreateInfo commandPoolCreateInfo(
|
|
uint32_t queueFamilyIndex,
|
|
VkCommandPoolCreateFlags flags
|
|
) {
|
|
VkCommandPoolCreateInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
|
|
.pNext = nullptr,
|
|
.flags = flags,
|
|
.queueFamilyIndex = queueFamilyIndex
|
|
};
|
|
return info;
|
|
}
|
|
|
|
VkCommandBufferAllocateInfo commandBufferAllocateInfo(
|
|
VkCommandPool pool,
|
|
uint32_t count
|
|
) {
|
|
VkCommandBufferAllocateInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
|
.pNext = nullptr,
|
|
.commandPool = pool,
|
|
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
.commandBufferCount = count
|
|
};
|
|
return info;
|
|
}
|
|
|
|
VkFenceCreateInfo fenceCreateInfo(
|
|
VkFenceCreateFlags flags /*= 0 */
|
|
) {
|
|
VkFenceCreateInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
|
|
.pNext = nullptr,
|
|
.flags = flags
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo (
|
|
VkSemaphoreCreateFlags flags /*= 0*/
|
|
) {
|
|
VkSemaphoreCreateInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
|
|
.pNext = nullptr,
|
|
.flags = flags,
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkCommandBufferBeginInfo commandBufferBeginInfo (
|
|
VkCommandBufferUsageFlags flags /*= 0*/
|
|
) {
|
|
VkCommandBufferBeginInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
.pNext = nullptr,
|
|
|
|
.flags = flags,
|
|
.pInheritanceInfo = nullptr
|
|
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkImageSubresourceRange imageSubresourceRange(
|
|
VkImageAspectFlags aspectMask
|
|
) {
|
|
VkImageSubresourceRange subImage {
|
|
.aspectMask = aspectMask,
|
|
.baseMipLevel = 0,
|
|
.levelCount = VK_REMAINING_MIP_LEVELS,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = VK_REMAINING_ARRAY_LAYERS,
|
|
};
|
|
|
|
return subImage;
|
|
}
|
|
|
|
VkSemaphoreSubmitInfo semaphoreSubmitInfo(
|
|
VkPipelineStageFlags2 stageMask,
|
|
VkSemaphore semaphore
|
|
) {
|
|
VkSemaphoreSubmitInfo submitInfo {
|
|
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
|
|
.pNext = nullptr,
|
|
.semaphore = semaphore,
|
|
.value = 1,
|
|
.stageMask = stageMask,
|
|
.deviceIndex = 0
|
|
};
|
|
|
|
return submitInfo;
|
|
}
|
|
|
|
VkCommandBufferSubmitInfo commandBufferSubmitInfo (
|
|
VkCommandBuffer cmd
|
|
) {
|
|
VkCommandBufferSubmitInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO,
|
|
.pNext = nullptr,
|
|
.commandBuffer = cmd,
|
|
.deviceMask = 0
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkSubmitInfo2 submitInfo(
|
|
VkCommandBufferSubmitInfo* cmd,
|
|
VkSemaphoreSubmitInfo* signalSemaphoreInfo,
|
|
VkSemaphoreSubmitInfo* waitSemaphoreInfo
|
|
) {
|
|
VkSubmitInfo2 info {
|
|
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2,
|
|
.pNext = nullptr,
|
|
|
|
.waitSemaphoreInfoCount = static_cast<uint32_t>(waitSemaphoreInfo == nullptr ? 0 : 1),
|
|
.pWaitSemaphoreInfos = waitSemaphoreInfo,
|
|
|
|
.commandBufferInfoCount = 1,
|
|
.pCommandBufferInfos = cmd,
|
|
|
|
.signalSemaphoreInfoCount = static_cast<uint32_t>(signalSemaphoreInfo == nullptr ? 0 : 1),
|
|
.pSignalSemaphoreInfos = signalSemaphoreInfo
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkImageCreateInfo imageCreateInfo(
|
|
VkFormat format,
|
|
VkImageUsageFlags usageFlags,
|
|
VkExtent3D extent
|
|
) {
|
|
VkImageCreateInfo info = {
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
|
.pNext = nullptr,
|
|
|
|
.imageType = VK_IMAGE_TYPE_2D,
|
|
|
|
.format = format,
|
|
.extent = extent,
|
|
|
|
.mipLevels = 1,
|
|
.arrayLayers = 1,
|
|
|
|
//for MSAA. I won't be using it, so only 1 sample per pixel
|
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
.tiling = VK_IMAGE_TILING_OPTIMAL,
|
|
.usage = usageFlags
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkImageViewCreateInfo imageViewCreateInfo(
|
|
VkFormat format,
|
|
VkImage image,
|
|
VkImageAspectFlags aspectFlags
|
|
) {
|
|
VkImageViewCreateInfo info = {
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
|
.pNext = nullptr,
|
|
.image = image,
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
|
.format = format,
|
|
.subresourceRange = {
|
|
.aspectMask = aspectFlags,
|
|
.baseMipLevel = 0,
|
|
.levelCount = 1,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = 1,
|
|
}
|
|
};
|
|
|
|
return info;
|
|
}
|
|
|
|
VkRenderingAttachmentInfo attachmentInfo(
|
|
VkImageView view,
|
|
VkClearValue* clear,
|
|
VkImageLayout layout /*= VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL*/
|
|
) {
|
|
VkRenderingAttachmentInfo colorAttachment {
|
|
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
|
|
.pNext = nullptr,
|
|
.imageView = view,
|
|
.imageLayout = layout,
|
|
.loadOp = clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD,
|
|
.storeOp = VK_ATTACHMENT_STORE_OP_STORE
|
|
};
|
|
|
|
if (clear) {
|
|
colorAttachment.clearValue = *clear;
|
|
}
|
|
|
|
return colorAttachment;
|
|
}
|
|
|
|
VkRenderingInfo renderingInfo(
|
|
VkExtent2D renderExtent,
|
|
VkRenderingAttachmentInfo* colorAttachment,
|
|
VkRenderingAttachmentInfo* depthAttachment
|
|
) {
|
|
VkRenderingInfo renderInfo {
|
|
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
|
|
.pNext = nullptr,
|
|
.renderArea = VkRect2D {
|
|
VkOffset2D {
|
|
0,
|
|
0
|
|
}, renderExtent
|
|
},
|
|
.layerCount = 1,
|
|
.colorAttachmentCount = 1,
|
|
.pColorAttachments = colorAttachment,
|
|
.pDepthAttachment = depthAttachment,
|
|
.pStencilAttachment = nullptr
|
|
};
|
|
return renderInfo;
|
|
}
|
|
|
|
VkPipelineShaderStageCreateInfo pipelineShaderStageCreateInfo(
|
|
VkShaderStageFlagBits stage,
|
|
VkShaderModule shaderModule,
|
|
const char * entry
|
|
) {
|
|
VkPipelineShaderStageCreateInfo info {
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
.pNext = nullptr,
|
|
|
|
.stage = stage,
|
|
.module = shaderModule,
|
|
|
|
.pName = entry
|
|
};
|
|
|
|
return info;
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|