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
	VkBufferCreateInfo bufferInfo = {};
	bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
	bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
	bufferInfo.size = sizeof(float) * 6;
	bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

	VkBuffer buffer;
	res = vkCreateBuffer(_VkSystem.device, &bufferInfo, 0, &buffer);
	if (res != VK_SUCCESS)
		return 1;

	VkMemoryRequirements memoryReq;
	vkGetBufferMemoryRequirements(_VkSystem.device, buffer, &memoryReq);

	VkMemoryAllocateInfo allocInfo = {};
	allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
	allocInfo.allocationSize = memoryReq.size;
	allocInfo.memoryTypeIndex = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;

	VkDeviceMemory devMemory = {};
	res = vkAllocateMemory(_VkSystem.device, &allocInfo, 0, &devMemory);
	if (res != VK_SUCCESS)
		return 1;

	res = vkBindBufferMemory(_VkSystem.device, buffer, devMemory, 0);
	if (res != VK_SUCCESS)
		return 1;

	void* mappedMemory = 0;
	res = vkMapMemory(_VkSystem.device, devMemory, 0, allocInfo.allocationSize, 0, &mappedMemory);
	if (res != VK_SUCCESS)
		return 1;

	// Vertex Data
	float vertData[] = { -1.f,1.f , 0.f,-1.f, 1.f,1.f };
	// Copy to Device memory and Unmap
	memcpy(mappedMemory, vertData, sizeof(vertData));
	vkUnmapMemory(_VkSystem.device, devMemory);

	VkMappedMemoryRange memoryRange = {};
	memoryRange.memory = devMemory;
	memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
	memoryRange.size = VK_WHOLE_SIZE;

	// Flush if memory isn't coherent?
	if (!(memoryReq.memoryTypeBits & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
	{
		res = vkFlushMappedMemoryRanges(_VkSystem.device, 1, &memoryRange);
		if (res != VK_SUCCESS)
			return 1;
	}

	_VkSystem.graphicsPipeline = graphicPipeline;
	_VkSystem.buffer = buffer;