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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#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_INLINE);
}

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;
}

// Create and Allocate a Buffer
static int CreateBuffer(uint64_t size, VkBufferUsageFlagBits usage, VkBuffer* pBuffer, VkDeviceMemory* pDevMemory)
{
	// Setup The Buffer information
	VkBufferCreateInfo bufferInfo = {};
	bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
	bufferInfo.usage = usage;
	bufferInfo.size = size;
	bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

	// Create a 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 Buffer to our Device Memory
	res = vkBindBufferMemory(_VkSystem.device, buffer, devMemory, 0);
	if (res != VK_SUCCESS)
		return 1;

	*pBuffer = buffer;
	*pDevMemory = devMemory;

	return res;
}

// Not too horrible
static int SetupVertexDataBuffer()
{
	// Vertex Data
	float vertData[] = {
		-0.5f,0.5f , -0.5f,-0.5f, 0.5f,-0.5f,
		-0.5f,0.5f , 0.5f,-0.5f, 0.5f,0.5f
	};

	uint32_t size = ALIGN_BUFFER_SIZE(sizeof(vertData));

	VkBuffer vertBuffer;
	VkDeviceMemory devMemory;
	if (CreateBuffer(size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, &vertBuffer, &devMemory) != VK_SUCCESS)
		return 1;

	// Map the Device Memory so we can write to it
	void* mappedMemory = 0;
	VkResult res = vkMapMemory(_VkSystem.device, devMemory, 0, size, 0, &mappedMemory);
	if (res != VK_SUCCESS)
		return 1;

	// Copy to Device Memory
	memcpy(mappedMemory, vertData, size);

	// Unmap Device Memory
	vkUnmapMemory(_VkSystem.device, devMemory);

	_VkSystem.vertBuffer = vertBuffer;
	_VkSystem.vertDevMemory = devMemory;
	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 convenience)
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 = entryPoint;
	stageInfo->module = shaderModule;
}

// Setup Viewport State Info (For convenience)
static void SetupViewPortStateInfo(VkPipelineViewportStateCreateInfo* pViewportInfo, VkViewport* pViewport, VkRect2D* pScissors, uint32_t width, uint32_t height)
{
	// Viewport and Scissors should be the Window Width/Height
	ZeroMemory(pViewport, sizeof(*pViewport));
	pViewport->width = (float)width;
	pViewport->height = (float)height;

	ZeroMemory(pScissors, sizeof(*pScissors));
	pScissors->extent.width = width;
	pScissors->extent.height = height;

	// Setup Viewport state information
	ZeroMemory(pViewportInfo, sizeof(*pViewportInfo));
	pViewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
	pViewportInfo->viewportCount = pViewportInfo->scissorCount = 1;
	pViewportInfo->pViewports = pViewport;
	pViewportInfo->pScissors = pScissors;
}


static int CreateLayoutBinding(VkDescriptorSetLayout* pDescLayout)
{
	// Setup Layout Binding for the Uniform buffer that contains "camera"
	VkDescriptorSetLayoutBinding layoutBinding = {};
	layoutBinding.binding = 0;
	layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
	layoutBinding.descriptorCount = 1;
	layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;

	// Setup the Description Layout Info
	VkDescriptorSetLayoutCreateInfo descLayoutInfo = {};
	descLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
	descLayoutInfo.pBindings = &layoutBinding;
	descLayoutInfo.bindingCount = 1;

	// Create Description Layout
	VkDescriptorSetLayout descLayout = {};
	VkResult res = vkCreateDescriptorSetLayout(_VkSystem.device, &descLayoutInfo, 0, &descLayout);
	if (res != VK_SUCCESS)
		return 1;

	*pDescLayout = descLayout;

	return res;
}

// Bad
static int CreateUniformDescription(VkDescriptorPool* pDescPool, VkDescriptorSetLayout* pDescLayout, VkDescriptorSet* pDescSet,
	VkDeviceMemory* pDevMemory, VkBuffer* pUniBuffer)
{

	VkDescriptorSetLayout descLayout = {};
	if (CreateLayoutBinding(&descLayout) != VK_SUCCESS)
		return 1;

	// Setup Descriptor Pool Size
	VkDescriptorPoolSize descPoolSize = {};
	descPoolSize.descriptorCount = 1;
	descPoolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;

	// Setup Descriptor Pool Info
	VkDescriptorPoolCreateInfo descPoolInfo = {};
	descPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
	descPoolInfo.maxSets = 1;
	descPoolInfo.poolSizeCount = 1;
	descPoolInfo.pPoolSizes = &descPoolSize;

	// Create Descriptor Pool
	VkDescriptorPool descPool = {};
	VkResult res = vkCreateDescriptorPool(_VkSystem.device, &descPoolInfo, 0, &descPool);
	if (res != VK_SUCCESS)
		return 1;

	// Setup Descriptor Allocate Info
	VkDescriptorSetAllocateInfo allocInfo = {};
	allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
	allocInfo.descriptorPool = descPool;
	allocInfo.pSetLayouts = &descLayout;
	allocInfo.descriptorSetCount = 1;

	// Allocate Descriptor set/s
	VkDescriptorSet descSet = {};
	res = vkAllocateDescriptorSets(_VkSystem.device, &allocInfo, &descSet);
	if (res != VK_SUCCESS)
		return 1;

	// Create some random big buffer for the Uniform block
	int uniBlockSize = ALIGN_BUFFER_SIZE(sizeof(float) * 2); // vec2
	VkDeviceMemory devMemory;
	VkBuffer uniBuffer;
	CreateBuffer(uniBlockSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, &uniBuffer, &devMemory);



	void* pBuffer;
	vkMapMemory(_VkSystem.device, devMemory, 0, uniBlockSize, 0, &pBuffer);

	((float*)pBuffer)[0] = 0.1f; // x
	((float*)pBuffer)[1] = 0.5f; // y

	VkMappedMemoryRange range = {};
	range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
	range.memory = devMemory;
	range.size = uniBlockSize;

	res = vkFlushMappedMemoryRanges(_VkSystem.device, 1, &range);
	if (res != VK_SUCCESS)
		return 1;

	vkUnmapMemory(_VkSystem.device, devMemory);

	// Setup Descriptor Buffer Info
	VkDescriptorBufferInfo bufferInfo = {};
	bufferInfo.vertBuffer = uniBuffer;
	bufferInfo.range = uniBlockSize;

	// Setup Write Descriptor Set
	VkWriteDescriptorSet writeDescSet = {};
	writeDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
	writeDescSet.descriptorCount = 1;
	writeDescSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
	writeDescSet.dstSet = descSet;
	writeDescSet.pBufferInfo = &bufferInfo;

	// Update Descriptor set/s
	vkUpdateDescriptorSets(_VkSystem.device, 1, &writeDescSet, 0, 0);

	*pDescLayout = descLayout;
	*pDescSet = descSet;
	*pDevMemory = devMemory;
	*pUniBuffer = uniBuffer;
	*pDescPool = descPool;

	return res;
}


// Simple function to setup the VkPipelineVertexInputStateCreateInfo for convenience 
static void SetupVertexInputStateInfo(VkPipelineVertexInputStateCreateInfo* pVertInfo, VkVertexInputAttributeDescription* pVertAttribDesc,
	VkVertexInputBindingDescription* pVertBindDesc, VkFormat format, VkVertexInputRate inputRate, uint32_t stride)
{
	// Setup Vertex Input Attribute Description (2 32bit floats at location=0)
	VkVertexInputAttributeDescription vertAttribDesc = {};
	vertAttribDesc.binding = 0;
	vertAttribDesc.location = 0;
	vertAttribDesc.offset = 0;
	vertAttribDesc.format = format;

	// Setup Vertex Input Binding Description
	VkVertexInputBindingDescription vertBindDesc = {};
	vertBindDesc.binding = 0;
	vertBindDesc.inputRate = inputRate;
	vertBindDesc.stride = stride;

	*pVertAttribDesc = vertAttribDesc;
	*pVertBindDesc = vertBindDesc;

	// Setup Vertex Input State Information
	VkPipelineVertexInputStateCreateInfo vertInfo = {};
	vertInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
	vertInfo.pVertexAttributeDescriptions = pVertAttribDesc;
	vertInfo.vertexAttributeDescriptionCount = 1;
	vertInfo.pVertexBindingDescriptions = pVertBindDesc;
	vertInfo.vertexBindingDescriptionCount = 1;

	*pVertInfo = vertInfo;
}

// Simple function to setup the VkPipelineRasterizationStateCreateInfo for convenience 
static void SetupRasterizationStateInfo(VkPipelineRasterizationStateCreateInfo* pRasterInfo)
{
	// 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;

	*pRasterInfo = rasterInfo;
}

// Horrible
static int SetupGraphicsPipeline()
{
	VkResult res;

	// Create Vertex Shader Module
	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);

	if (CreateUniformDescription(&_VkSystem.mainUniform.descPool, &_VkSystem.mainUniform.setLayout,
		&_VkSystem.mainUniform.descSet, &_VkSystem.mainUniform.devMemory, &_VkSystem.mainUniform.vertBuffer) != VK_SUCCESS)
		return 1;

	VkPipelineLayoutCreateInfo layoutInfo = {};
	layoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
	layoutInfo.setLayoutCount = 1;
	layoutInfo.pSetLayouts = &_VkSystem.mainUniform.setLayout;

	// Create the Pipeline Layout
	VkPipelineLayout pipeLayout;
	res = vkCreatePipelineLayout(_VkSystem.device, &layoutInfo, 0, &pipeLayout);
	if (res != VK_SUCCESS)
		return 1;


	// Setup Viewport State Information
	VkPipelineViewportStateCreateInfo viewportInfo;
	VkViewport viewPort;
	VkRect2D scissors;
	SetupViewPortStateInfo(&viewportInfo, &viewPort, &scissors, SCREEN_WIDTH, SCREEN_HEIGHT);


	// Setup Rasterization State Information
	VkPipelineRasterizationStateCreateInfo rasterInfo = {};
	SetupRasterizationStateInfo(&rasterInfo);

	// 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 State Information
	VkVertexInputAttributeDescription vertAttribDesc;
	VkVertexInputBindingDescription vertBindDesc;
	VkPipelineVertexInputStateCreateInfo vertInfo;
	SetupVertexInputStateInfo(&vertInfo, &vertAttribDesc, &vertBindDesc, VK_FORMAT_R32G32_SFLOAT,
		VK_VERTEX_INPUT_RATE_VERTEX, sizeof(float) * 2);


	VkPipelineMultisampleStateCreateInfo multiInfo = {};
	multiInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
	multiInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;

	// Set Color Blend Attachment State (we want RGBA to be enabled for writing)
	VkPipelineColorBlendAttachmentState blendAttachment = {};
	blendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;

	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 = _countof(stageInfo);
	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;
	_VkSystem.pipeLayout = pipeLayout;

	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;
}

static int runOnce = 1;
int Vulkan_Render(float color)
{
	VkResult res;

	// Setup our Pipeline (should not be done here)
	if (runOnce)
	{
		if (SetupGraphicsPipeline() != VK_SUCCESS)
			return 1;
		if (SetupVertexDataBuffer() != VK_SUCCESS)
			return 1;
	}

	// Only wait for the Fence after the first frame
	if (!runOnce)
	{
		// 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)
	{
		DebugBreak();
		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);

	// Bind Graphics Pipeline
	vkCmdBindPipeline(_VkSystem.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _VkSystem.graphicsPipeline);

	VkDeviceSize devSize = 0;
	// Bind Vertex Buffers
	vkCmdBindVertexBuffers(_VkSystem.commandBuffer, 0, 1, &_VkSystem.vertBuffer, &devSize);

	//vkCmdBindDescriptorSets(_VkSystem.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _VkSystem.pipeLayout, 0, 1, &_VkSystem.mainUniform.descSet, 0, 0);

	// 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)
	{
		DebugBreak();
		return 1;
	}

	res = (VkResult)SubmitToQueue(_VkSystem.queue.queue, _VkSystem.commandBuffer, _VkSystem.fence,
		_VkSystem.semaphoreNextImage, _VkSystem.queue.semaphoreSubmit);
	
	if(res != VK_SUCCESS)
	{
		DebugBreak();
		return 1;
	}

	QueuePresent(_VkSystem.queue.queue, _VkSystem.swapchainKHR, _VkSystem.queue.semaphoreSubmit, _VkSystem.imageIndex);

	if (runOnce)
		runOnce = 0;
}