-
Notifications
You must be signed in to change notification settings - Fork 0
/
0003index-buffer.diff
159 lines (150 loc) · 5.7 KB
/
0003index-buffer.diff
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
diff -bNur 0002lighting/data_bulk.c 0003index-buffer/data_bulk.c
--- 0002lighting/data_bulk.c 2019-03-03 15:08:52.160918059 -0600
+++ 0003index-buffer/data_bulk.c 2019-03-03 15:08:52.160918059 -0600
@@ -14,22 +14,12 @@
{0.0f, 2.0f / 3.0f},
{1.0f, 0.0f, 0.0f, 0.0f},
{0, 0, 0, 0}},
- {{-0.5f, -0.5f, -0.5f},
- {0.0f, 0.0f, -1.0f},
- {0.25f, 1.0f / 3.0f},
- {1.0f, 0.0f, 0.0f, 0.0f},
- {0, 0, 0, 0}},
{{0.5f, -0.5f, -0.5f},
{0.0f, 0.0f, -1.0f},
{0.0f, 1.0f / 3.0f},
{1.0f, 0.0f, 0.0f, 0.0f},
{0, 0, 0, 0}},
- {{-0.5f, 0.5f, -0.5f},
- {0.0f, 0.0f, -1.0f},
- {0.0f, 2.0f / 3.0f},
- {1.0f, 0.0f, 0.0f, 0.0f},
- {0, 0, 0, 0}},
- {{0.5f, -0.5f, -0.5f},
+ {{-0.5f, -0.5f, -0.5f},
{0.0f, 0.0f, -1.0f},
{0.25f, 1.0f / 3.0f},
{1.0f, 0.0f, 0.0f, 0.0f},
@@ -72,11 +62,23 @@
sizeof(data_attribute_descriptions) /
sizeof(data_attribute_descriptions[0]);
+const struct data_model_index
+{
+ struct
+ {
+ uint32_t first, second, third;
+ } square[2];
+} *data_model_index = &(struct data_model_index){{
+ {0, 2, 1},
+ {0, 1, 3},
+}};
+const VkDeviceSize data_model_index_size = sizeof(*data_model_index);
+
data_model_t data_models[] = {
{
"Square",
- 0,
- sizeof(model_vertex) / sizeof(uint32_t),
+ offsetof(struct data_model_index, square) / sizeof(uint32_t),
+ sizeof(data_model_index->square) / sizeof(uint32_t),
0,
},
};
diff -bNur 0002lighting/data.h 0003index-buffer/data.h
--- 0002lighting/data.h 2019-03-03 15:08:52.160918059 -0600
+++ 0003index-buffer/data.h 2019-03-03 15:08:52.160918059 -0600
@@ -28,6 +28,9 @@
extern const VkVertexInputAttributeDescription data_attribute_descriptions[];
extern const uint32_t data_attribute_descriptions_count;
+extern const struct data_model_index *data_model_index;
+extern const VkDeviceSize data_model_index_size;
+
typedef struct
{
char *name;
diff -bNur 0002lighting/vulkan.c 0003index-buffer/vulkan.c
--- 0002lighting/vulkan.c 2019-03-07 12:27:08.482438455 -0600
+++ 0003index-buffer/vulkan.c 2019-03-07 12:28:22.728545402 -0600
@@ -29,6 +29,8 @@
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
VkBuffer vertex_buffer = VK_NULL_HANDLE;
VmaAllocation vertex_buffer_allocation = NULL;
+VkBuffer index_buffer = VK_NULL_HANDLE;
+VmaAllocation index_buffer_allocation = NULL;
VkCommandPool command_pool = VK_NULL_HANDLE;
VkSemaphore wait_semaphore = VK_NULL_HANDLE;
VkFence queue_submit_fence = VK_NULL_HANDLE;
@@ -686,6 +688,42 @@
vmaFlushAllocation(allocator, vertex_buffer_allocation, 0, data_model_vertex_size);
}
+inline static void create_index_buffer()
+{
+ VkBufferCreateInfo buffer_info_create;
+ static const VkBufferCreateInfo EmptyVkBufferCreateInfo;
+ buffer_info_create = EmptyVkBufferCreateInfo;
+ buffer_info_create.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+ buffer_info_create.size = data_model_index_size;
+ buffer_info_create.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
+ buffer_info_create.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
+
+ VmaAllocationCreateInfo alloc_create_info;
+ static const VmaAllocationCreateInfo EmptyVmaAllocationCreateInfo;
+ alloc_create_info = EmptyVmaAllocationCreateInfo;
+ alloc_create_info.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
+
+ VkResult err;
+ err = vmaCreateBuffer(allocator, &buffer_info_create, &alloc_create_info, &index_buffer, &index_buffer_allocation, NULL);
+ assert((err == VK_SUCCESS) && "vmaCreateBuffer: Failed index buffer.");
+}
+
+inline static void
+setup_index_buffer()
+{
+ create_index_buffer();
+
+ void *data;
+ VkResult err;
+ err = vmaMapMemory(allocator, index_buffer_allocation, &data);
+ assert((err == VK_SUCCESS) && "vmaMapMemory: Failed index buffer.");
+
+ memcpy(data, data_model_index, data_model_index_size);
+
+ vmaUnmapMemory(allocator, index_buffer_allocation);
+ vmaFlushAllocation(allocator, index_buffer_allocation, 0, data_model_index_size);
+}
+
inline static void create_shaders(VkShaderModule *vert_modules,
VkShaderModule *frag_modules)
{
@@ -1033,6 +1071,8 @@
vkCmdBindVertexBuffers(command_buffer, 0, 1, &vertex_buffer,
&ZeroVkDeviceSize);
+ vkCmdBindIndexBuffer(command_buffer, index_buffer, 0, VK_INDEX_TYPE_UINT32);
+
data_model_instance_t *ptr =
list_entry(data_model_instances.next, data_model_instance_t, list);
vkCmdBindPipeline(
@@ -1045,8 +1085,8 @@
&ptr->uniform_map->descriptor_set,
0, NULL);
- vkCmdDraw(command_buffer, data_models[ptr->model].count, 1,
- data_models[ptr->model].first, 0);
+ vkCmdDrawIndexed(command_buffer, data_models[ptr->model].count, 1,
+ data_models[ptr->model].first, 0, 0);
vkCmdEndRenderPass(command_buffer);
@@ -1161,6 +1201,9 @@
vert_modules[0] = VK_NULL_HANDLE;
vkDestroyPipelineLayout(device, pipeline_layout, NULL);
pipeline_layout = VK_NULL_HANDLE;
+ vmaDestroyBuffer(allocator, index_buffer, index_buffer_allocation);
+ index_buffer_allocation = NULL;
+ index_buffer = VK_NULL_HANDLE;
vmaDestroyBuffer(allocator, vertex_buffer, vertex_buffer_allocation);
vertex_buffer_allocation = NULL;
vertex_buffer = VK_NULL_HANDLE;
@@ -1236,6 +1279,7 @@
setup_descriptor_set();
setup_vertex_buffer();
+ setup_index_buffer();
VkShaderModule vert_modules[] = {VK_NULL_HANDLE};
VkShaderModule frag_modules[] = {VK_NULL_HANDLE};