-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_pipeline.c
356 lines (296 loc) · 12.8 KB
/
r_pipeline.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "r_pipeline.h"
#include "v_video.h"
#include "r_render.h"
#include "m_math.h"
#include "def.h"
#include <stdio.h>
#include <assert.h>
#include <vulkan/vulkan_core.h>
VkPipeline pipelines[MAX_PIPELINES];
VkDescriptorSet descriptorSets[MAX_DESCRIPTOR_SETS];
static VkPipelineLayout pipelineLayoutGeometry;
VkPipelineLayout pipelineLayoutPostProcess;
static VkDescriptorSetLayout descriptorSetLayoutEmpty;
static VkDescriptorSetLayout descriptorSetLayoutPostProcess;
static VkDescriptorPool descriptorPool;
enum shaderStageType { VERT, FRAG };
#define PIPELINE_COUNT 3
#define DESCRIPTOR_SET_COUNT 1
static void initShaderModule(const char* filepath, VkShaderModule* module)
{
VkResult r;
int fr;
FILE* fp;
fp = fopen(filepath, "rb");
fr = fseek(fp, 0, SEEK_END);
assert( fr == 0 ); // success
size_t codeSize = ftell(fp);
rewind(fp);
unsigned char code[codeSize];
fread(code, 1, codeSize, fp);
fclose(fp);
const VkShaderModuleCreateInfo shaderInfo = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = codeSize,
.pCode = (uint32_t*)code,
};
r = vkCreateShaderModule(device, &shaderInfo, NULL, module);
assert( VK_SUCCESS == r );
}
void initDescriptorSets(void)
{
VkResult r;
VkDescriptorSetLayoutCreateInfo layoutInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = 0,
.pBindings = NULL
};
r = vkCreateDescriptorSetLayout(device, &layoutInfo, NULL, &descriptorSetLayoutEmpty);
assert( VK_SUCCESS == r );
VkDescriptorSetLayoutBinding textureBinding = {
.binding = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.pImmutableSamplers = NULL, // no one really seems to use these
};
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = &textureBinding;
r = vkCreateDescriptorSetLayout(device, &layoutInfo, NULL, &descriptorSetLayoutPostProcess);
assert( VK_SUCCESS == r );
VkDescriptorPoolSize poolSize = {
.descriptorCount = 1,
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
};
VkDescriptorPoolCreateInfo poolInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.maxSets = 1,
.poolSizeCount = 1,
// odd name. not the sizes of pools, but the numbers of descriptors
// of a certain type that can be created from the pool
.pPoolSizes = &poolSize,
};
vkCreateDescriptorPool(device, &poolInfo, NULL, &descriptorPool);
VkDescriptorSetAllocateInfo allocInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = descriptorPool,
.descriptorSetCount = DESCRIPTOR_SET_COUNT,
.pSetLayouts = &descriptorSetLayoutPostProcess,
};
vkAllocateDescriptorSets(device, &allocInfo, descriptorSets);
}
void initDescriptors(void)
{
VkDescriptorImageInfo imageInfo = {
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.imageView = offscreenFrameBuffer.image.view,
.sampler = offscreenFrameBuffer.image.sampler
};
VkWriteDescriptorSet writeImages = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstArrayElement = 0,
.dstSet = descriptorSets[R_POST_PROC_DESCRIPTOR_SET],
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = &imageInfo
};
vkUpdateDescriptorSets(device, 1, &writeImages, 0, NULL);
}
static void initPipelineLayouts(void)
{
VkResult r;
VkPipelineLayoutCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.setLayoutCount = 1, // ?
.pSetLayouts = &descriptorSetLayoutEmpty,
.pushConstantRangeCount = 0,
.pPushConstantRanges = NULL
};
r = vkCreatePipelineLayout(device, &info, NULL, &pipelineLayoutGeometry);
assert( VK_SUCCESS == r );
info.pSetLayouts = &descriptorSetLayoutPostProcess;
r = vkCreatePipelineLayout(device, &info, NULL, &pipelineLayoutPostProcess);
assert( VK_SUCCESS == r );
}
void initPipelines(void)
{
initPipelineLayouts();
VkShaderModule vertModule;
VkShaderModule fragModule;
initShaderModule("shaders/spv/simple-vert.spv", &vertModule);
initShaderModule("shaders/spv/simple-frag.spv", &fragModule);
const VkSpecializationInfo shaderSpecialInfo = {
// TODO
};
const VkPipelineShaderStageCreateInfo shaderStages[2] = {
[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
[0].stage = VK_SHADER_STAGE_VERTEX_BIT,
[0].module = vertModule,
[0].pName = "main",
[0].pSpecializationInfo = NULL,
[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT,
[1].module = fragModule,
[1].pName = "main",
[1].pSpecializationInfo = NULL,
}; // vert and frag
const VkVertexInputBindingDescription bindingDescription = {
.binding = 0,
.stride = sizeof(Vec2), // all our verts will be 2D
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX
};
const VkVertexInputAttributeDescription positionAttributeDescription = {
.binding = 0,
.location = 0,
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = 0,
};
const VkPipelineVertexInputStateCreateInfo vertexInput = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &bindingDescription,
.vertexAttributeDescriptionCount = 1,
.pVertexAttributeDescriptions = &positionAttributeDescription
};
const VkPipelineInputAssemblyStateCreateInfo inputAssembly = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
.primitiveRestartEnable = VK_FALSE // applies only to index calls
};
const VkViewport viewport = {
.height = WINDOW_HEIGHT,
.width = WINDOW_WIDTH,
.x = 0,
.y = 0,
.minDepth = 0.0,
.maxDepth = 1.0
};
const VkRect2D scissor = {
.extent = {WINDOW_WIDTH, WINDOW_HEIGHT},
.offset = {0, 0}
};
const VkPipelineViewportStateCreateInfo viewportState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.scissorCount = 1,
.pScissors = &scissor,
.viewportCount = 1,
.pViewports = &viewport,
};
const VkPipelineRasterizationStateCreateInfo rasterizationState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.depthClampEnable = VK_FALSE, // dunno
.rasterizerDiscardEnable = VK_FALSE, // actually discards everything
.polygonMode = VK_POLYGON_MODE_LINE,
.cullMode = VK_CULL_MODE_BACK_BIT,
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
.depthBiasEnable = VK_FALSE,
.lineWidth = 4.0
};
const VkPipelineMultisampleStateCreateInfo multisampleState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.sampleShadingEnable = VK_FALSE,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT
// TODO: alot more settings here. more to look into
};
const VkPipelineColorBlendAttachmentState attachmentState = {
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT, /* need this to actually
write anything to the
framebuffer */
.blendEnable = VK_FALSE, // no blending for now
.srcColorBlendFactor = 0,
.dstColorBlendFactor = 0,
.colorBlendOp = 0,
.srcAlphaBlendFactor = 0,
.dstAlphaBlendFactor = 0,
.alphaBlendOp = 0,
};
const VkPipelineColorBlendStateCreateInfo colorBlendState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.logicOpEnable = VK_FALSE, // only for integer framebuffer formats
.logicOp = 0,
.attachmentCount = 1,
.pAttachments = &attachmentState /* must have independentBlending device
feature enabled for these to be different. each entry would correspond
to the blending for a different framebuffer. */
};
const VkGraphicsPipelineCreateInfo pipelineInfo = {
.basePipelineIndex = 0, // not used
.basePipelineHandle = 0,
.subpass = 0, // which subpass in the renderpass do we use this pipeline with
.renderPass = offscreenRenderPass,
.layout = pipelineLayoutGeometry,
.pDynamicState = NULL,
.pColorBlendState = &colorBlendState,
.pDepthStencilState = NULL,
.pMultisampleState = &multisampleState,
.pRasterizationState = &rasterizationState,
.pViewportState = &viewportState,
.pTessellationState = NULL, // may be able to do splines with this
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.flags = 0,
.stageCount = sizeof(shaderStages) / sizeof(shaderStages[0]),
.pStages = shaderStages,
.pVertexInputState = &vertexInput,
.pInputAssemblyState = &inputAssembly,
};
// ----------------------------------------------------------
// creating the second pipeline info for the emitables
// ----------------------------------------------------------
VkPipelineInputAssemblyStateCreateInfo inputAssemblyEmit = inputAssembly;
inputAssemblyEmit.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
VkPipelineRasterizationStateCreateInfo rasterizationStateEmit = rasterizationState;
rasterizationStateEmit.polygonMode = VK_POLYGON_MODE_POINT;
VkGraphicsPipelineCreateInfo emitablePipelineInfo = pipelineInfo;
emitablePipelineInfo.pRasterizationState = &rasterizationStateEmit;
emitablePipelineInfo.pInputAssemblyState = &inputAssemblyEmit;
// ----------------------------------------------------------
// creating the third pipeline info for post processing
// ----------------------------------------------------------
VkPipelineInputAssemblyStateCreateInfo inputAssemblyPostProc = inputAssembly;
inputAssemblyPostProc.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineRasterizationStateCreateInfo rasterizationStatePostProc = rasterizationState;
rasterizationStatePostProc.polygonMode = VK_POLYGON_MODE_FILL;
const VkPipelineVertexInputStateCreateInfo postProcInputState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexAttributeDescriptionCount = 0,
.vertexBindingDescriptionCount = 0,
};
VkShaderModule postProcVertModule;
VkShaderModule postProcFragModule;
initShaderModule("shaders/spv/postproc-vert.spv", &postProcVertModule);
initShaderModule("shaders/spv/glow-only-frag.spv", &postProcFragModule);
VkPipelineShaderStageCreateInfo postProcShaderStages[2] = {shaderStages[0], shaderStages[1]};
postProcShaderStages[0].module = postProcVertModule;
postProcShaderStages[1].module = postProcFragModule;
// ----------------------------------------------------------
// ----------------------------------------------------------
VkGraphicsPipelineCreateInfo postProcPipelineInfo = pipelineInfo;
postProcPipelineInfo.pRasterizationState = &rasterizationStatePostProc;
postProcPipelineInfo.pInputAssemblyState = &inputAssemblyPostProc;
postProcPipelineInfo.pVertexInputState = &postProcInputState;
postProcPipelineInfo.pStages = postProcShaderStages;
postProcPipelineInfo.layout = pipelineLayoutPostProcess;
postProcPipelineInfo.renderPass = swapchainRenderPass;
VkGraphicsPipelineCreateInfo infos[PIPELINE_COUNT] = {pipelineInfo, emitablePipelineInfo, postProcPipelineInfo};
vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, PIPELINE_COUNT, infos, NULL, pipelines);
vkDestroyShaderModule(device, vertModule, NULL);
vkDestroyShaderModule(device, fragModule, NULL);
vkDestroyShaderModule(device, postProcVertModule, NULL);
vkDestroyShaderModule(device, postProcFragModule, NULL);
vkDestroyDescriptorSetLayout(device, descriptorSetLayoutEmpty, NULL);
vkDestroyDescriptorSetLayout(device, descriptorSetLayoutPostProcess, NULL);
}
void cleanUpPipelines()
{
vkDestroyPipelineLayout(device, pipelineLayoutGeometry, NULL);
vkDestroyPipelineLayout(device, pipelineLayoutPostProcess, NULL);
for (int i = 0; i < PIPELINE_COUNT; i++)
{
vkDestroyPipeline(device, pipelines[i], NULL);
}
vkDestroyDescriptorPool(device, descriptorPool, NULL);
}