Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graphite/engine/renderer/include/vkinitializers.hpp
2024-01-03 16:05:02 -05:00

286 lines
No EOL
13 KiB
C++

#pragma once
#include <vulkan/vulkan.h>
#include <initializer_list>
namespace vk
{
namespace initializers
{
inline VkViewport Viewport(uint32_t width, uint32_t height)
{
VkViewport viewport = {};
viewport.width = static_cast<float>(width); //why does vulkan define these as floats? how do you have half a pixel?
viewport.height = static_cast<float>(height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
return viewport;
}
inline VkRect2D Rect2D(uint32_t width, uint32_t height, int32_t x, int32_t y)
{
VkRect2D rect2D = {};
rect2D.extent = { width, height };
rect2D.offset = { x, y };
return rect2D;
}
inline VkDescriptorSetLayoutBinding DescriptorBinding(uint32_t binding, VkDescriptorType type, VkShaderStageFlags stage)
{
VkDescriptorSetLayoutBinding descriptorSetLayoutBinding = {};
descriptorSetLayoutBinding.binding = binding;
descriptorSetLayoutBinding.descriptorType = type;
descriptorSetLayoutBinding.descriptorCount = 1;
descriptorSetLayoutBinding.stageFlags = stage;
return descriptorSetLayoutBinding;
}
inline VkPipelineShaderStageCreateInfo ShaderStage(VkShaderStageFlagBits stage, VkShaderModule shaderModule)
{
VkPipelineShaderStageCreateInfo shaderStageCreateInfo = {};
shaderStageCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStageCreateInfo.stage = stage;
shaderStageCreateInfo.module = shaderModule;
shaderStageCreateInfo.pName = "main";
shaderStageCreateInfo.pSpecializationInfo = nullptr;
return shaderStageCreateInfo;
}
inline VkPipelineColorBlendStateCreateInfo ColorBlendState(uint32_t count, VkPipelineColorBlendAttachmentState* attachments)
{
VkPipelineColorBlendStateCreateInfo colorBlendStateCreateInfo = {};
colorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlendStateCreateInfo.logicOpEnable = VK_FALSE;
colorBlendStateCreateInfo.logicOp = VK_LOGIC_OP_COPY;
colorBlendStateCreateInfo.attachmentCount = count;
colorBlendStateCreateInfo.pAttachments = attachments;
colorBlendStateCreateInfo.blendConstants[0] = 0.0f;
colorBlendStateCreateInfo.blendConstants[1] = 0.0f;
colorBlendStateCreateInfo.blendConstants[2] = 0.0f;
colorBlendStateCreateInfo.blendConstants[3] = 0.0f;
return colorBlendStateCreateInfo;
}
inline VkPipelineMultisampleStateCreateInfo MultisampleState()
{
VkPipelineMultisampleStateCreateInfo multisampleStateCreateInfo = {};
multisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampleStateCreateInfo.sampleShadingEnable = VK_FALSE;
multisampleStateCreateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
return multisampleStateCreateInfo;
}
inline VkPipelineViewportStateCreateInfo ViewportState()
{
VkPipelineViewportStateCreateInfo viewportStateCreateInfo = {};
viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportStateCreateInfo.viewportCount = 1;
viewportStateCreateInfo.scissorCount = 1;
return viewportStateCreateInfo;
}
inline VkPipelineInputAssemblyStateCreateInfo InputAssemblyState()
{
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo = {};
inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssemblyStateCreateInfo.pNext = nullptr;
inputAssemblyStateCreateInfo.flags = 0;
inputAssemblyStateCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE;
return inputAssemblyStateCreateInfo;
}
inline VkPipelineColorBlendAttachmentState ColorBlendAttachmentState(VkColorComponentFlags colorWriteMask, VkBool32 blendEnabled = VK_FALSE)
{
VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState = {};
pipelineColorBlendAttachmentState.colorWriteMask = colorWriteMask;
pipelineColorBlendAttachmentState.blendEnable = blendEnabled;
return pipelineColorBlendAttachmentState;
}
inline VkPipelineDynamicStateCreateInfo DynamicState(uint32_t count, const VkDynamicState* states)
{
VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo = {};
pipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
pipelineDynamicStateCreateInfo.pDynamicStates = states;
pipelineDynamicStateCreateInfo.dynamicStateCount = count;
return pipelineDynamicStateCreateInfo;
}
inline VkWriteDescriptorSet DescriptorWrite(VkDescriptorSet set, uint32_t binding, VkDescriptorType type, VkDescriptorBufferInfo* bufferInfo)
{
VkWriteDescriptorSet writeDescriptorSet = {};
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSet.dstSet = set;
writeDescriptorSet.dstBinding = binding;
writeDescriptorSet.descriptorType = type;
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.pBufferInfo = bufferInfo;
return writeDescriptorSet;
}
inline VkWriteDescriptorSet DescriptorWrite(VkDescriptorSet set, uint32_t binding, VkDescriptorType type, VkDescriptorImageInfo* imageInfo)
{
VkWriteDescriptorSet writeDescriptorSet = {};
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSet.dstSet = set;
writeDescriptorSet.dstBinding = binding;
writeDescriptorSet.descriptorType = type;
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.pImageInfo = imageInfo;
return writeDescriptorSet;
}
inline VkVertexInputAttributeDescription VertexAttribute(uint32_t binding, uint32_t location, VkFormat format, uint32_t offset = 0)
{
VkVertexInputAttributeDescription vertexInputAttributeDescription = {};
vertexInputAttributeDescription.binding = binding;
vertexInputAttributeDescription.location = location;
vertexInputAttributeDescription.format = format;
vertexInputAttributeDescription.offset = offset;
return vertexInputAttributeDescription;
}
inline VkImageCreateInfo Image2D(VkFormat format, uint32_t width, uint32_t height, VkImageUsageFlags usage, VkImageLayout layout)
{
VkImageCreateInfo imageCreateInfo = {};
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.extent.depth = 1;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.format = format;
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageCreateInfo.initialLayout = layout;
imageCreateInfo.usage = usage;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
return imageCreateInfo;
}
inline VkImageViewCreateInfo ImageView2D(VkFormat format, VkImageAspectFlags aspectMask, VkImage& image)
{
VkImageViewCreateInfo viewCreateInfo = {};
viewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewCreateInfo.format = format;
viewCreateInfo.subresourceRange = {};
viewCreateInfo.subresourceRange.aspectMask = aspectMask;
viewCreateInfo.subresourceRange.baseMipLevel = 0;
viewCreateInfo.subresourceRange.levelCount = 1;
viewCreateInfo.subresourceRange.baseArrayLayer = 0;
viewCreateInfo.subresourceRange.layerCount = 1;
viewCreateInfo.image = image;
return viewCreateInfo;
}
//used normally for framebuffers where you need to sample it later
inline VkAttachmentDescription TypicalAttachment(VkFormat format,
VkImageLayout initialLayout,
VkImageLayout finalLayout)
{
VkAttachmentDescription attachmentDescription = {};
attachmentDescription.format = format;
attachmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;
attachmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachmentDescription.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescription.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescription.initialLayout = initialLayout;
attachmentDescription.finalLayout = finalLayout;
return attachmentDescription;
}
inline VkAttachmentReference TypicalColorReference(uint32_t attachment)
{
VkAttachmentReference attachmentReference = {};
attachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentReference.attachment = attachment;
return attachmentReference;
}
inline VkPipelineRasterizationStateCreateInfo RasterizationState(
VkPolygonMode polygonMode,
VkCullModeFlags cullMode,
VkFrontFace frontFace,
VkPipelineRasterizationStateCreateFlags flags)
{
VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo = {};
pipelineRasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
pipelineRasterizationStateCreateInfo.polygonMode = polygonMode;
pipelineRasterizationStateCreateInfo.cullMode = cullMode;
pipelineRasterizationStateCreateInfo.frontFace = frontFace;
pipelineRasterizationStateCreateInfo.flags = flags;
pipelineRasterizationStateCreateInfo.depthClampEnable = VK_FALSE;
pipelineRasterizationStateCreateInfo.lineWidth = 1.0f;
return pipelineRasterizationStateCreateInfo;
}
inline VkPipelineDepthStencilStateCreateInfo DepthStencilState(VkBool32 depthTestEnable,
VkBool32 depthWriteEnable,
VkCompareOp depthCompare)
{
VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo = {};
pipelineDepthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
pipelineDepthStencilStateCreateInfo.depthTestEnable = depthTestEnable;
pipelineDepthStencilStateCreateInfo.depthWriteEnable = depthWriteEnable;
pipelineDepthStencilStateCreateInfo.depthCompareOp = depthCompare;
pipelineDepthStencilStateCreateInfo.front = pipelineDepthStencilStateCreateInfo.back;
pipelineDepthStencilStateCreateInfo.back.compareOp = VK_COMPARE_OP_ALWAYS;
return pipelineDepthStencilStateCreateInfo;
}
inline VkVertexInputBindingDescription BindingDescription(uint32_t binding, uint32_t stride, VkVertexInputRate inputRate)
{
VkVertexInputBindingDescription bindingDescription = {};
bindingDescription.binding = binding;
bindingDescription.stride = stride;
bindingDescription.inputRate = inputRate;
return bindingDescription;
}
inline VkDescriptorPoolSize PoolSize(VkDescriptorType type, uint32_t size)
{
VkDescriptorPoolSize poolSize = {};
poolSize.descriptorCount = size;
poolSize.type = type;
return poolSize;
}
inline VkDeviceQueueCreateInfo DeviceQueue(uint32_t queueIndex)
{
constexpr float queuePriority = 0.0f;
VkDeviceQueueCreateInfo queueCreateInfo = {};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = queueIndex;
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &queuePriority;
return queueCreateInfo;
}
}
}