Skip to content

Instantly share code, notes, and snippets.

@fromasmtodisasm
Forked from onatto/gfxpipe.h
Created November 21, 2020 19:14
Show Gist options
  • Save fromasmtodisasm/6fa2c56da3b670a4425620799a435b2c to your computer and use it in GitHub Desktop.
Save fromasmtodisasm/6fa2c56da3b670a4425620799a435b2c to your computer and use it in GitHub Desktop.
C99 DSL for filling in VkGraphicsPipelineCreateInfo
//--- usage ------------------------------------------------//
void usage() {
VertexBindings(vbindings) = {
{ .binding = 0, .stride = 12, PerVertex },
{ .binding = 1, .stride = 12, PerVertex },
{ .binding = 2, .stride = 8 , PerVertex },
{ .binding = 3, .stride = 16, PerInstance },
};
VertexAttrs(vattrs) = {
// loc , binding , format , offset ,
{ 0 , 0 , VertexFormat_F3 , 0 , },
{ 1 , 1 , VertexFormat_F3 , 0 , },
{ 2 , 2 , VertexFormat_F2 , 0 , },
{ 3 , 3 , VertexFormat_F4 , 0 , },
};
BlendAttachments(attachments) = {
BlendAttachment(
SrcColorBlend(ONE),
DstColorBlend(ONE),
ColorBlendOp(ADD),
AlphaBlendOp(ADD),
),
BlendAttachment(
SrcColorBlend(ONE),
DstColorBlend(ONE),
ColorBlendOp(ADD),
AlphaBlendOp(ADD),
),
{ }, // disabled
BlendAttachment(
SrcColorBlend(ONE),
DstColorBlend(ONE),
ColorBlendOp(ADD),
AlphaBlendOp(ADD),
),
};
VkGraphicsPipelineCreateInfo defaults[1] = {
GfxPipeline(
VertexInput(vbindings, vattrs),
Rasterization(
CullBack, // CullFront, CullNone
PolyFill, // PolyLine, PolyPoint
FrontCCW, // FrontCW
),
Viewport(),
InputAssembly(TriangleList), // PointList, LineList, TriangleStrip, TriangleFan
DepthStencil(
DepthTest = true,
DepthWrite = true,
DepthCompare(LESS_OR_EQUAL),
DepthBoundsEnable(0.0, 1.0),
),
Blend(attachments, .blendConstants = {0,1,0,1}),
// .layout =
// .renderPass =
// .subpass =
),
};
}
//--- dynamic state ------------------------------------------------//
#define DynamicStateDefaults() \
.pDynamicState = &(VkPipelineDynamicStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, \
.dynamicStateCount = 2, \
.pDynamicStates = (VkDynamicState[]) { \
VK_DYNAMIC_STATE_VIEWPORT, \
VK_DYNAMIC_STATE_SCISSOR, \
},\
}
//--- depth-stencil ------------------------------------------------//
#define DepthTest .depthTestEnable
#define DepthWrite .depthWriteEnable
#define DepthCompare(op) .depthCompareOp = VK_COMPARE_OP_##op
#define DepthBoundsEnable(minn, maxx) \
.depthBoundsTestEnable = true, \
.minDepthBounds = minn, \
.maxDepthBounds = maxx,
#define DepthStencil(...) \
.pDepthStencilState = &(VkPipelineDepthStencilStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, \
DepthTest = true, \
DepthWrite = true, \
DepthCompare(LESS_OR_EQUAL), \
} \
//--- blend ------------------------------------------------//
#define BlendFactor(fac) VK_BLEND_FACTOR_##fac
#define SrcColorBlend(fac) .srcColorBlendFactor = BlendFactor(fac)
#define DstColorBlend(fac) .dstColorBlendFactor = BlendFactor(fac)
#define SrcAlphaBlend(fac) .srcAlphaBlendFactor = BlendFactor(fac)
#define DstAlphaBlend(fac) .dstAlphaBlendFactor = BlendFactor(fac)
#define ColorBlendOp(op) .colorBlendOp = VK_BLEND_OP_##op
#define AlphaBlendOp(op) .alphaBlendOp = VK_BLEND_OP_##op
#define BlendAttachments(var) VkPipelineColorBlendAttachmentState var[]
#define BlendAttachment(...) \
(VkPipelineColorBlendAttachmentState) { \
.blendEnable = true, \
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT \
| VK_COLOR_COMPONENT_G_BIT \
| VK_COLOR_COMPONENT_B_BIT \
| VK_COLOR_COMPONENT_A_BIT, \
__VA_ARGS__ \
}
#define Blend(attach, ...) \
.pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, \
.attachmentCount = ARRAYCOUNT(attach), \
.pAttachments = attach, \
__VA_ARGS__ \
}
//--- input assembly ------------------------------------------------//
#define PointList .topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST
#define LineList .topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST
#define TriangleList .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
#define TriangleStrip .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
#define TriangleFan .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
#define InputAssembly(...) \
.pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, \
TriangleList, \
__VA_ARGS__ \
}
//--- viewport ------------------------------------------------//
#define Viewport(...) \
.pViewportState = &(VkPipelineViewportStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, \
.viewportCount = 1, \
}
//--- rasterizer ------------------------------------------------//
#define PolyFill .polygonMode = VK_POLYGON_MODE_FILL
#define PolyLine .polygonMode = VK_POLYGON_MODE_LINE
#define PolyPoint .polygonMode = VK_POLYGON_MODE_POINT
#define FrontCW .frontFace = VK_FRONT_FACE_CLOCKWISE
#define FrontCCW .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
#define CullFront .cullMode = VK_CULL_MODE_FRONT_BIT
#define CullBack .cullMode = VK_CULL_MODE_BACK_BIT
#define CullNone .cullMode = VK_CULL_MODE_NONE
#define Rasterization(...) \
.pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, \
CullBack , PolyFill , FrontCCW, \
__VA_ARGS__ \
}
//--- vertex input ------------------------------------------------//
#define VertexBindings(var) VkVertexInputBindingDescription var[]
#define VertexAttrs(var) VkVertexInputAttributeDescription var[]
#define PerVertex .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
#define PerInstance .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
#define VertexFormat_U4 VK_FORMAT_R32_UINT
#define VertexFormat_F1 VK_FORMAT_R32_SFLOAT
#define VertexFormat_F2 VK_FORMAT_R32G32_SFLOAT
#define VertexFormat_F3 VK_FORMAT_R32G32B32_SFLOAT
#define VertexFormat_F4 VK_FORMAT_R32G32B32A32_SFLOAT
#define VertexInput(bindings, attrs) \
.pVertexInputState = &(VkPipelineVertexInputStateCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, \
.pVertexBindingDescriptions = bindings, \
.vertexBindingDescriptionCount = ARRAYCOUNT(bindings), \
.pVertexAttributeDescriptions = attrs, \
.vertexAttributeDescriptionCount = ARRAYCOUNT(attrs), \
}
//--- pipeline ------------------------------------------------//
#define GfxPipeline(...) (VkGraphicsPipelineCreateInfo) { \
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, \
Rasterization(), \
Viewport(), \
InputAssembly(), \
DepthStencil(), \
DynamicStateDefaults(), \
__VA_ARGS__ \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment