-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_vk_pipelines.hpp
211 lines (174 loc) · 7.93 KB
/
r_vk_pipelines.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once
#define VK_USE_PLATFORM_WIN32_KHR
#define VK_NO_PROTOTYPES
#define __VK
#include "DEFS_WIN32_NO_BS.h"
// TODO: autogen custom vulkan ?
#include <vulkan.h>
// TODO: header + .cpp ?
// TODO: revisit this
#include "vk_procs.h"
#include "sys_os_api.h"
#include "vk_utils.hpp"
#include <vector>
static bool colorBlending = 0;
// TODO: variable entry point
constexpr char SHADER_ENTRY_POINT[] = "main";
// TODO: store more stuff ?
struct vk_gfx_pipeline_state
{
VkPolygonMode polyMode = VK_POLYGON_MODE_FILL;
VkCullModeFlags cullFlags = VK_CULL_MODE_BACK_BIT;
VkFrontFace frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
VkPrimitiveTopology primTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkBlendFactor srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
VkBlendFactor dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
VkBlendFactor srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
VkBlendFactor dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
float extraPrimitiveOverestimationSize = 0.0f;
bool conservativeRasterEnable = false;
bool depthWrite = true;
bool depthTestEnable = true;
bool blendCol = colorBlending;
};
using vk_shader_stage_list = std::initializer_list<VkPipelineShaderStageCreateInfo>;
using vk_specializations = std::initializer_list<u32>;
inline static VkSpecializationInfo
VkMakeSpecializationInfo(
std::vector<VkSpecializationMapEntry>& specializations,
const vk_specializations& consts
) {
specializations.resize( std::size( consts ) );
u64 sizeOfASpecConst = sizeof( *std::cbegin( consts ) );
for( u64 i = 0; i < std::size( consts ); ++i )
specializations[ i ] = { u32( i ), u32( i * sizeOfASpecConst ), u32( sizeOfASpecConst ) };
VkSpecializationInfo specInfo = {};
specInfo.mapEntryCount = std::size( specializations );
specInfo.pMapEntries = std::data( specializations );
specInfo.dataSize = std::size( consts ) * sizeOfASpecConst;
specInfo.pData = std::cbegin( consts );
return specInfo;
}
struct vk_render_attachment
{
VkFormat format;
};
// TODO: rework
// TODO: specialization for gfx ?
// TODO: depth clamp ?
// TODO: entry point name
VkPipeline VkMakeGfxPipeline(
VkDevice vkDevice,
VkPipelineLayout vkPipelineLayout,
VkShaderModule vs,
VkShaderModule fs,
const VkFormat* desiredColorFormat,
VkFormat desiredDepthFormat,
const vk_gfx_pipeline_state& pipelineState
) {
VkPipelineShaderStageCreateInfo shaderStagesInfo[ 2 ] = {};
shaderStagesInfo[ 0 ].sType = shaderStagesInfo[ 1 ].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStagesInfo[ 0 ].stage = VK_SHADER_STAGE_VERTEX_BIT;
shaderStagesInfo[ 0 ].module = vs;
shaderStagesInfo[ 0 ].pName = SHADER_ENTRY_POINT;
shaderStagesInfo[ 1 ].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shaderStagesInfo[ 1 ].module = fs;
shaderStagesInfo[ 1 ].pName = SHADER_ENTRY_POINT;
u32 shaderStagesCount = bool( vs ) + bool( fs );
VkPipelineInputAssemblyStateCreateInfo inAsmStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO };
inAsmStateInfo.topology = pipelineState.primTopology;
VkPipelineViewportStateCreateInfo viewportInfo = { VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO };
viewportInfo.viewportCount = 1;
viewportInfo.scissorCount = 1;
VkDynamicState dynamicStates[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VkPipelineDynamicStateCreateInfo dynamicStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO };
dynamicStateInfo.dynamicStateCount = std::size( dynamicStates );
dynamicStateInfo.pDynamicStates = dynamicStates;
// TODO: place inside if ?
VkPipelineRasterizationConservativeStateCreateInfoEXT conservativeRasterState =
{ VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT };
conservativeRasterState.conservativeRasterizationMode = VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT;
conservativeRasterState.extraPrimitiveOverestimationSize = pipelineState.extraPrimitiveOverestimationSize;
VkPipelineRasterizationStateCreateInfo rasterInfo = { VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO };
rasterInfo.pNext = pipelineState.conservativeRasterEnable ? &conservativeRasterState : 0;
rasterInfo.depthClampEnable = 0;
rasterInfo.rasterizerDiscardEnable = 0;
rasterInfo.polygonMode = pipelineState.polyMode;
rasterInfo.cullMode = pipelineState.cullFlags;
rasterInfo.frontFace = pipelineState.frontFace;
rasterInfo.lineWidth = 1.0f;
VkPipelineDepthStencilStateCreateInfo depthStencilState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
.depthTestEnable = pipelineState.depthTestEnable,
.depthWriteEnable = pipelineState.depthWrite,
.depthCompareOp = VK_COMPARE_OP_GREATER,
.depthBoundsTestEnable = VK_TRUE,
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f
};
constexpr VkColorComponentFlags colWriteMask =
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendAttachmentState blendConfig = {
.blendEnable = pipelineState.blendCol,
.srcColorBlendFactor = pipelineState.srcColorBlendFactor,
.dstColorBlendFactor = pipelineState.dstColorBlendFactor,
.colorBlendOp = VK_BLEND_OP_ADD,
.srcAlphaBlendFactor = pipelineState.srcAlphaBlendFactor,
.dstAlphaBlendFactor = pipelineState.dstAlphaBlendFactor,
.alphaBlendOp = VK_BLEND_OP_ADD,
.colorWriteMask = colWriteMask
};
VkPipelineColorBlendStateCreateInfo colorBlendStateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &blendConfig
};
// TODO: only if we use frag
VkPipelineMultisampleStateCreateInfo multisamplingInfo = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
multisamplingInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineRenderingCreateInfo renderingInfo = { VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO };
renderingInfo.colorAttachmentCount = desiredColorFormat ? 1 : 0;
renderingInfo.pColorAttachmentFormats = desiredColorFormat;
renderingInfo.depthAttachmentFormat = desiredDepthFormat;
VkGraphicsPipelineCreateInfo pipelineInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
pipelineInfo.pNext = &renderingInfo;
pipelineInfo.stageCount = shaderStagesCount;
pipelineInfo.pStages = shaderStagesInfo;
VkPipelineVertexInputStateCreateInfo vtxInCreateInfo = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
pipelineInfo.pVertexInputState = &vtxInCreateInfo;
pipelineInfo.pInputAssemblyState = &inAsmStateInfo;
pipelineInfo.pViewportState = &viewportInfo;
pipelineInfo.pRasterizationState = &rasterInfo;
pipelineInfo.pMultisampleState = &multisamplingInfo;
pipelineInfo.pDepthStencilState = &depthStencilState;
pipelineInfo.pColorBlendState = &colorBlendStateInfo;
pipelineInfo.pDynamicState = &dynamicStateInfo;
pipelineInfo.layout = vkPipelineLayout;
pipelineInfo.basePipelineIndex = -1;
VkPipeline vkGfxPipeline;
VK_CHECK( vkCreateGraphicsPipelines( vkDevice, 0, 1, &pipelineInfo, 0, &vkGfxPipeline ) );
return vkGfxPipeline;
}
// TODO: pipeline caputre representations blah blah ?
VkPipeline VkMakeComputePipeline(
VkDevice vkDevice,
VkPipelineCache vkPipelineCache,
VkPipelineLayout vkPipelineLayout,
VkShaderModule cs,
vk_specializations consts,
const char* pEntryPointName = SHADER_ENTRY_POINT
) {
std::vector<VkSpecializationMapEntry> specializations;
VkSpecializationInfo specInfo = VkMakeSpecializationInfo( specializations, consts );
VkPipelineShaderStageCreateInfo stage = { VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO };
stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
stage.module = cs;
stage.pName = pEntryPointName;
stage.pSpecializationInfo = &specInfo;
VkComputePipelineCreateInfo compPipelineInfo = { VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO };
compPipelineInfo.stage = stage;
compPipelineInfo.layout = vkPipelineLayout;
VkPipeline pipeline = 0;
VK_CHECK( vkCreateComputePipelines( vkDevice, vkPipelineCache, 1, &compPipelineInfo, 0, &pipeline ) );
return pipeline;
}