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
356
357
358
359
360
361
362
363
364
365
366
367 | #include "Global_Header.h"
#include "Vulkan_Header.h"
#include "Windows_Misc.h"
#include <math.h>
extern VulkanSystem _VkSystem;
static void BeginRenderPass(VkRenderPass renderPass, VkFramebuffer frameBuffer, VkCommandBuffer commandBuffer,
float r, float g, float b)
{
VkClearValue clearValue = {};
clearValue.color = { r,g,b,0 };
VkRenderPassBeginInfo passInfo = {};
passInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
passInfo.renderPass = renderPass;
passInfo.framebuffer = frameBuffer;
passInfo.pClearValues = &clearValue;
passInfo.clearValueCount = 1;
passInfo.renderArea.extent.width = SCREEN_WIDTH;
passInfo.renderArea.extent.height = SCREEN_HEIGHT;
vkCmdBeginRenderPass(commandBuffer, &passInfo, VK_SUBPASS_CONTENTS_BEGIN_RANGE);
}
static int SubmitToQueue(VkQueue queue, VkCommandBuffer commandBuffer, VkFence fence, VkSemaphore waitSemap, VkSemaphore signalSemap)
{
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
submitInfo.pWaitSemaphores = &waitSemap;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &signalSemap;
submitInfo.signalSemaphoreCount = 1;
VkPipelineStageFlags pipeFlags = VK_PIPELINE_STAGE_TRANSFER_BIT;
submitInfo.pWaitDstStageMask = &pipeFlags;
VkResult res = vkQueueSubmit(queue, 1, &submitInfo, fence);
return res;
}
static int FindMemoryType(uint32_t memoryTypeBits, uint32_t properties)
{
// Get Physical Device Properties
VkPhysicalDeviceMemoryProperties devMemProp = {};
vkGetPhysicalDeviceMemoryProperties(_VkSystem.physicalDevice, &devMemProp);
// Check which index of the memory properties supports HOST_VISIBLE?
int typeIndex = -1;
for (uint32_t i = 0; i < devMemProp.memoryTypeCount; i++)
{
if ((memoryTypeBits & (1 << i)) && ((devMemProp.memoryTypes[i].propertyFlags & properties) == properties))
{
typeIndex = i;
break;
}
}
return typeIndex;
}
// Not too horrible
static int SetupBuffers()
{
// Vertex Data
float vertData[] = {
-1.0f,1.0f , -1.0f,-1.0f, 1.0f,-1.0f,
-1.0f,1.0f , 1.0f,-1.0f, 1.0f,1.0f
};
// Setup The Vertex Data Buffer information
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufferInfo.size = sizeof(vertData);
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
// Create Vertex Data Buffer
VkBuffer buffer;
VkResult res = vkCreateBuffer(_VkSystem.device, &bufferInfo, 0, &buffer);
if (res != VK_SUCCESS)
return 1;
// Get the memory requirements for the buffer
VkMemoryRequirements memoryReq;
vkGetBufferMemoryRequirements(_VkSystem.device, buffer, &memoryReq);
uint32_t typeIndex = FindMemoryType(memoryReq.memoryTypeBits, (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT));
if (typeIndex == -1)
DebugBreak();
// Setup Memory Allocation Information
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memoryReq.size;
allocInfo.memoryTypeIndex = typeIndex;
// Allocate device memory
VkDeviceMemory devMemory = {};
res = vkAllocateMemory(_VkSystem.device, &allocInfo, 0, &devMemory);
if (res != VK_SUCCESS)
return 1;
// Bind the Vertex Data Buffer to our Device Memory
res = vkBindBufferMemory(_VkSystem.device, buffer, devMemory, 0);
if (res != VK_SUCCESS)
return 1;
// Map the Device Memory so we can write to it
void* mappedMemory = 0;
res = vkMapMemory(_VkSystem.device, devMemory, 0, bufferInfo.size, 0, &mappedMemory);
if (res != VK_SUCCESS)
return 1;
// Copy to Device Memory
memcpy(mappedMemory, vertData, bufferInfo.size);
//// Setup Memory Range needed to Flush the data
//VkMappedMemoryRange memoryRange = {};
//memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
//memoryRange.memory = devMemory;
//memoryRange.size = VK_WHOLE_SIZE;
//// Flush Mapped Memory
//res = vkFlushMappedMemoryRanges(_VkSystem.device, 1, &memoryRange);
//if (res != VK_SUCCESS)
// return 1;
// Unmap Device Memory
vkUnmapMemory(_VkSystem.device, devMemory);
_VkSystem.buffer = buffer;
return res;
}
// Create a Shader Module from a SPIR-V Shader
static int CreateShaderModule(const wchar_t* file, VkShaderModule* pShaderModule)
{
// Load Shader (SPIR-V) to Memory temporarily
unsigned long size;
void* shaderFile = Win32_FileLoader(file, &size);
VkShaderModuleCreateInfo moduleInfo = {};
moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleInfo.codeSize = size;
moduleInfo.pCode = (uint32_t*)shaderFile;
// Create Shader Module
VkShaderModule shaderModule;
VkResult res = vkCreateShaderModule(_VkSystem.device, &moduleInfo, 0, &shaderModule);
if (res != VK_SUCCESS)
return 1;
// Free Memory used for the Shader (SPIR-V)
Win32_Free(shaderFile);
*pShaderModule = shaderModule;
return res;
}
// Setup Shader Stage Info (For convinience)
static void SetupShaderStageInfo(VkPipelineShaderStageCreateInfo* stageInfo, VkShaderModule shaderModule, const char* entryPoint, VkShaderStageFlagBits flagBits)
{
stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stageInfo->stage = flagBits;
stageInfo->pName = "main";
stageInfo->module = shaderModule;
}
// Horrible
static int SetupGraphicsPipeline()
{
VkResult res;
VkShaderModule vertexModule;
if (CreateShaderModule(L"vert.spv", &vertexModule) != VK_SUCCESS)
return 1;
// Create Fragment Shader Module
VkShaderModule fragModule;
if (CreateShaderModule(L"frag.spv", &fragModule) != VK_SUCCESS)
return 1;
// Create 2 Shader Stage Create Info for Vertex and Fragment shaders
VkPipelineShaderStageCreateInfo stageInfo[2] = {};
// Setup Stage info for the Vertex Shader
SetupShaderStageInfo(&stageInfo[0], vertexModule, "main", VK_SHADER_STAGE_VERTEX_BIT);
// Setup Stage info for the Fragment Shader
SetupShaderStageInfo(&stageInfo[1], fragModule, "main", VK_SHADER_STAGE_FRAGMENT_BIT);
VkPipelineLayoutCreateInfo layoutInfo = {};
layoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
// Create the Pipeline Layout
VkPipelineLayout pipeLayout;
res = vkCreatePipelineLayout(_VkSystem.device, &layoutInfo, 0, &pipeLayout);
if (res != VK_SUCCESS)
return 1;
// Viewport and Scissors should be the Window Width/Height
VkViewport viewPort = {};
viewPort.width = SCREEN_WIDTH;
viewPort.height = SCREEN_HEIGHT;
VkRect2D scissors = {};
scissors.extent.width = SCREEN_WIDTH;
scissors.extent.height = SCREEN_HEIGHT;
// Setup Viewport state information
VkPipelineViewportStateCreateInfo viewportInfo = {};
viewportInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportInfo.viewportCount = viewportInfo.scissorCount = 1;
viewportInfo.pViewports = (const VkViewport*)&viewportInfo;
viewportInfo.pScissors = &scissors;
// Setup Rasterization State Information
VkPipelineRasterizationStateCreateInfo rasterInfo = {};
rasterInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterInfo.polygonMode = VK_POLYGON_MODE_FILL;
rasterInfo.cullMode = VK_CULL_MODE_NONE;
rasterInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterInfo.lineWidth = 1.0f;
// Setup Input Assembly State Information
VkPipelineInputAssemblyStateCreateInfo assemblyInfo = {};
assemblyInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
assemblyInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
// Setup Vertex Input Attribute Description (2 32bit floats at location=0)
VkVertexInputAttributeDescription vertAttribDesc = {};
vertAttribDesc.binding = 0;
vertAttribDesc.location = 0;
vertAttribDesc.offset = 0;
vertAttribDesc.format = VK_FORMAT_R32G32_SFLOAT;
// Setup Vertex Input Binding Description
VkVertexInputBindingDescription vertBindDesc = {};
vertBindDesc.binding = 0;
vertBindDesc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
vertBindDesc.stride = sizeof(float) * 2;
// Setup Vertex Input State Information
VkPipelineVertexInputStateCreateInfo vertInfo = {};
vertInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertInfo.pVertexAttributeDescriptions = &vertAttribDesc;
vertInfo.vertexAttributeDescriptionCount = 1;
vertInfo.pVertexBindingDescriptions = &vertBindDesc;
vertInfo.vertexBindingDescriptionCount = 1;
VkPipelineMultisampleStateCreateInfo multiInfo = {};
multiInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multiInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState blendAttachment = {};
VkPipelineColorBlendStateCreateInfo blendInfo = {};
blendInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
blendInfo.attachmentCount = 1;
blendInfo.pAttachments = &blendAttachment;
// Setup Graphics Pipeline Information
VkGraphicsPipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.renderPass = _VkSystem.renderPass;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = stageInfo;
pipelineInfo.pVertexInputState = &vertInfo;
pipelineInfo.pInputAssemblyState = &assemblyInfo;
pipelineInfo.pRasterizationState = &rasterInfo;
pipelineInfo.layout = pipeLayout;
pipelineInfo.pViewportState = &viewportInfo;
pipelineInfo.pColorBlendState = &blendInfo;
pipelineInfo.pMultisampleState = &multiInfo;
// Create Graphics Pipeline
VkPipeline graphicPipeline;
res = vkCreateGraphicsPipelines(_VkSystem.device, 0, 1, &pipelineInfo, 0, &graphicPipeline);
if (res != VK_SUCCESS)
return 1;
_VkSystem.graphicsPipeline = graphicPipeline;
return res;
}
int QueuePresent(VkQueue queue, VkSwapchainKHR swapchain, VkSemaphore semaphoreSubmit, uint32_t imageIndex)
{
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pSwapchains = &swapchain;
presentInfo.swapchainCount = 1;
presentInfo.pWaitSemaphores = &semaphoreSubmit;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pImageIndices = &_VkSystem.imageIndex;
VkResult res = vkQueuePresentKHR(queue, &presentInfo);
return res;
}
int Vulkan_Render(float color)
{
VkResult res;
// Wait for Fence
vkWaitForFences(_VkSystem.device, 1, &_VkSystem.fence, 1, UINT64_MAX);
// Acquire the next image to work at (this is technically allowed to fail, but we need to restart the loop then)
res = vkAcquireNextImageKHR(_VkSystem.device, _VkSystem.swapchainKHR, UINT64_MAX, _VkSystem.semaphoreNextImage, VK_NULL_HANDLE, &_VkSystem.imageIndex);
if (res != VK_SUCCESS)
return 1;
// Reset Fence
vkResetFences(_VkSystem.device, 1, &_VkSystem.fence);
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
// Begin CommandBuffer Recording
vkBeginCommandBuffer(_VkSystem.commandBuffer, &beginInfo);
static int temp;
if (!temp)
{
SetupGraphicsPipeline();
SetupBuffers();
temp = 1;
}
VkDeviceSize devSize = 0;
// Bind Graphics Pipeline
vkCmdBindPipeline(_VkSystem.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _VkSystem.graphicsPipeline);
// Bind Vertex Buffers
vkCmdBindVertexBuffers(_VkSystem.commandBuffer, 0, 1, &_VkSystem.buffer, &devSize);
// Begin RenderPass
BeginRenderPass(_VkSystem.renderPass, _VkSystem.frameBuffers[_VkSystem.imageIndex].frameBuffer,
_VkSystem.commandBuffer, 0.5f, 0.1f, 0.8f);
// Draw Vertex Buffer
vkCmdDraw(_VkSystem.commandBuffer, 6, 1, 0, 0);
// End RenderPass
vkCmdEndRenderPass(_VkSystem.commandBuffer);
// End CommandBuffer Recording
res = vkEndCommandBuffer(_VkSystem.commandBuffer);
if (res != VK_SUCCESS)
return 1;
if (SubmitToQueue(_VkSystem.queue.queue, _VkSystem.commandBuffer, _VkSystem.fence,
_VkSystem.semaphoreNextImage, _VkSystem.queue.semaphoreSubmit) != VK_SUCCESS)
return 1;
QueuePresent(_VkSystem.queue.queue, _VkSystem.swapchainKHR, _VkSystem.queue.semaphoreSubmit, _VkSystem.imageIndex);
}
|