summaryrefslogtreecommitdiff
path: root/src/static-ui.cpp
blob: 8c8f920b78326c56f3889649313ef72d23cfaf76 (plain)
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

#include "static-ui.hpp"

#include "dynamic-array.hpp"
#include "game-settings.hpp"
#include "pk.h"
#include "static-plane.hpp"
#include "vendor-glm-include.hpp"
#include "window.hpp"

#include <cassert>
#include <cstdio>
#include <cstring>
#include <vulkan/vulkan_core.h>

struct pke_ui_box_instance_buffer_item {
	glm::mat4 pos_scale;
	glm::vec2 px_scale;
	float depth;
	float padding[1];
};

struct pke_ui_master {
	pk_membucket *bkt;
	pke_ui_box **root_boxes;
	pke_ui_box_count_T h_root_boxes;
	pke_ui_box_count_T r_root_boxes;
	pke_ui_graphics_bindings bindings;
	bool should_recalc_ui = false;
	glm::vec2 px_scale;
} pke_ui_master;

void pke_ui_init() {
	pke_ui_master.bkt = pk_bucket_create("pke ui", PK_DEFAULT_BUCKET_SIZE, false);
	pke_ui_master.root_boxes = pk_new<pke_ui_box*>(1, pke_ui_master.bkt);
	pke_ui_master.h_root_boxes = 0;
	pke_ui_master.r_root_boxes = 1;
	pke_ui_master.bindings = {};
}

void pke_ui_init_bindings() {
	pkvk_buffer_create_data create_data{};
	create_data.buffer_byte_length[0] = sizeof(pkeIntrinsicsPlane.vert[0]) * 4;
	create_data.buffer_byte_length[1] = sizeof(pkeIntrinsicsPlane.uv[0]) * 4;
	create_data.buffer_byte_length[2] = sizeof(pkeIntrinsicsPlane.index[0]) * 6;
	create_data.buffer_byte_length[3] = sizeof(pke_ui_box_instance_buffer_item);
	create_data.src_data[0] = &pkeIntrinsicsPlane.vert;
	create_data.src_data[1] = &pkeIntrinsicsPlane.uv;
	create_data.src_data[2] = &pkeIntrinsicsPlane.index;
	create_data.src_data[3] = nullptr;
	create_data.n_buffers = 4;
	create_data.index_index = 2;
	create_data.index_instance = 3;

	pkvk_buffer_create_data_out out{};
	pkvk_buffer_create(&create_data, &out);

	pke_ui_master.bindings.bd_vertex.buffer = out.buffers[0];
	pke_ui_master.bindings.bd_vertex.firstBinding = 0;
	pke_ui_master.bindings.bd_vertex.bindingCount = 1;
	pke_ui_master.bindings.bd_uv.buffer = out.buffers[1];
	pke_ui_master.bindings.bd_uv.firstBinding = 1;
	pke_ui_master.bindings.bd_uv.bindingCount = 1;
	pke_ui_master.bindings.bd_index.buffer = out.buffers[2];
	pke_ui_master.bindings.bd_index.firstBinding = 0;
	pke_ui_master.bindings.bd_index.bindingCount = 1;
	pke_ui_master.bindings.index_count = 6;
	pke_ui_master.bindings.bd_instance.buffer = out.buffers[3];
	pke_ui_master.bindings.bd_instance.firstBinding = 2;
	pke_ui_master.bindings.bd_instance.bindingCount = 1;
	pke_ui_master.bindings.deviceMemoryVert = out.device_memory_vertex;
	pke_ui_master.bindings.deviceMemoryInst = out.device_memory_instance;

	pke_ui_master.bindings.instance_buffer_max_count = out.memory_requirements_instance.size / sizeof(pke_ui_box_instance_buffer_item);
}

/* UI layout notes
Some restrictions:
- children cannot change the size of a parent, parents must be sized manually
	- e.g.: exact font width
	- consider writing a method to calculate text
*/

void pke_ui_calc_px(pke_ui_box *box, DynArray<pke_ui_box_instance_buffer_item> &buffer) {
	assert(box != nullptr);
	// val = desired_padding + border_width
	static const float built_in_offset = 2.0;
	glm::vec2 size;
	glm::vec2 parent_size_padded;
	glm::vec2 parent_pos_and_offset;

	assert(box->pos_top_left_x >= 0.0);
	assert(box->pos_top_left_y >= 0.0);
	assert(box->max_width >= 0.0);
	assert(box->max_height >= 0.0);

	if (box->internal.parent != nullptr) {
		parent_pos_and_offset.x = box->internal.parent->internal.px_corner_x + box->internal.parent->internal.px_offset_x;
		parent_pos_and_offset.y = box->internal.parent->internal.px_corner_y + box->internal.parent->internal.px_offset_y;
		parent_size_padded.x = box->internal.parent->internal.px_width;
		parent_size_padded.y = box->internal.parent->internal.px_height;
		// built-in padding
		parent_size_padded -= glm::vec2(built_in_offset * 2);
		size = parent_size_padded;
	} else {
		parent_pos_and_offset = glm::vec2(0);
		parent_size_padded = glm::vec2(Extent.width, Extent.height);
		size = glm::vec2(Extent.width, Extent.height);
	}

	if (PK_HAS_FLAG(box->flags, PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC)) {
		size.x -= box->pos_top_left_x;
		size.y -= box->pos_top_left_y;
		box->internal.px_corner_x = 0
			+ parent_pos_and_offset.x
			+ box->pos_top_left_x;
		box->internal.px_corner_y = 0
			+ parent_pos_and_offset.y
			+ box->pos_top_left_y;
		size.x = PK_MIN(size.x, box->max_width);
		size.y = PK_MIN(size.y, box->max_height);
	} else {
		assert(box->pos_top_left_x < 1.0);
		assert(box->pos_top_left_y < 1.0);
		box->internal.px_corner_x = parent_pos_and_offset.x;
		box->internal.px_corner_y = parent_pos_and_offset.y;
		float px_left = size.x * box->pos_top_left_x;
		float px_top = size.y * box->pos_top_left_y;
		box->internal.px_corner_x += px_left;
		box->internal.px_corner_y += px_top;
		size -= px_left;
		size -= px_top;
		size.x = PK_MIN(size.x, box->max_width * parent_size_padded.x);
		size.y = PK_MIN(size.y, box->max_height * parent_size_padded.y);
	}
	if (box->internal.parent != nullptr) {
		box->internal.parent->internal.px_offset_y += size.y;
	}
	// built-in padding
	box->internal.px_offset_x = built_in_offset;
	box->internal.px_offset_y = built_in_offset;
	box->internal.px_width = size.x;
	box->internal.px_height = size.y;

	// update buffer
	glm::vec3 scale = glm::vec3(box->internal.px_width / (float)Extent.width, box->internal.px_height / (float)Extent.height, 1.0);
	glm::vec3 translate = glm::vec3(1);
	// ((val) - (width/2)) / (width/2)
	// place left line of box at edge of screen
	translate.x = (box->internal.px_width / 2.0);
	// box position
	translate.x += box->internal.px_corner_x;
	translate.x -= ((float)Extent.width / 2.0);
	translate.x /= ((float)Extent.width / 2.0);
	// ((val) - (height/2)) / (height/2)
	// place top line of box at edge of screen
	translate.y = (box->internal.px_height / 2.0);
	// box position
	translate.y += box->internal.px_corner_y;
	translate.y -= ((float)Extent.height / 2.0);
	translate.y /= ((float)Extent.height / 2.0);
	translate.z = 0;

	pke_ui_box_instance_buffer_item *buffer_item = &buffer.Push();
	buffer_item->pos_scale = glm::translate(glm::mat4(1), translate);
	buffer_item->pos_scale = glm::scale(buffer_item->pos_scale, scale);
	buffer_item->px_scale.x = (2.0 / (float)Extent.width);
	buffer_item->px_scale.y = (2.0 / (float)Extent.height);
	buffer_item->depth = (float)box->layer;
}

void pke_ui_recalc_sizes_recursive(DynArray<pke_ui_box_instance_buffer_item> &arr, pke_ui_box *box, uint8_t depth = 0) {
	uint64_t flags_masked;

	for (pke_ui_box_count_T i = 0; i < box->internal.h_children; ++i) {
		flags_masked = box->flags & (PKE_UI_BOX_FLAG_POSITION_TYPE_BOTH);
		if (flags_masked == 0 || flags_masked == PKE_UI_BOX_FLAG_POSITION_TYPE_BOTH) {
			fprintf(stderr, "[%s] ui box invalid flags: position", __FILE__);
			return;
		}
	}

	for (pke_ui_box_count_T i = 0; i < box->internal.h_children; ++i) {
		pke_ui_calc_px(box->internal.children[i], arr);
		pke_ui_recalc_sizes_recursive(arr, box->internal.children[i], depth + 1);
	}
}

void pke_ui_update_instance_buffer(DynArray<pke_ui_box_instance_buffer_item> &arr) {
	VkResult vkResult;
	if (arr.Count() > pke_ui_master.bindings.instance_buffer_max_count) {
		VkBuffer newBuffer;
		VkBufferCreateInfo bufferCI;
		bufferCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
		bufferCI.pNext = nullptr;
		bufferCI.flags = {};
		bufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
		bufferCI.queueFamilyIndexCount = 1;
		bufferCI.pQueueFamilyIndices = &graphicsFamilyIndex;
		bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
		bufferCI.size = sizeof(pke_ui_box_instance_buffer_item) * arr.Count();
		vkResult = vkCreateBuffer(vkDevice, &bufferCI, vkAllocator, &newBuffer);
		assert(vkResult == VK_SUCCESS);
		VkMemoryRequirements vkMemReqs;
		vkGetBufferMemoryRequirements(vkDevice, newBuffer, &vkMemReqs);
		assert(sizeof(pke_ui_box_instance_buffer_item) % vkMemReqs.alignment == 0);

		vkDestroyBuffer(vkDevice, newBuffer, vkAllocator);
		newBuffer = VK_NULL_HANDLE;

		bufferCI.size = vkMemReqs.size + (vkMemReqs.alignment - (vkMemReqs.size & vkMemReqs.alignment));
		vkResult = vkCreateBuffer(vkDevice, &bufferCI, vkAllocator, &newBuffer);
		assert(vkResult == VK_SUCCESS);

		VkDeviceMemory new_memory;
		VkMemoryAllocateInfo vkMemoryAllocateInfo;
		vkMemoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
		vkMemoryAllocateInfo.pNext = nullptr;
		vkMemoryAllocateInfo.allocationSize = bufferCI.size;
		vkMemoryAllocateInfo.memoryTypeIndex = FindMemoryTypeIndex(vkMemReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);

		vkResult = vkAllocateMemory(vkDevice, &vkMemoryAllocateInfo, vkAllocator, &new_memory);
		assert(vkResult == VK_SUCCESS);

		vkResult = vkBindBufferMemory(vkDevice, newBuffer, new_memory, 0);
		assert(vkResult == VK_SUCCESS);
		if (pke_ui_master.bindings.bd_instance.buffer != VK_NULL_HANDLE) {
			vkDestroyBuffer(vkDevice, pke_ui_master.bindings.bd_instance.buffer, vkAllocator);
		}
		if (pke_ui_master.bindings.deviceMemoryInst != VK_NULL_HANDLE) {
			vkFreeMemory(vkDevice, pke_ui_master.bindings.deviceMemoryInst, vkAllocator);
		}
		pke_ui_master.bindings.bd_instance.buffer = newBuffer;
		pke_ui_master.bindings.deviceMemoryInst = new_memory;
		pke_ui_master.bindings.instance_buffer_max_count = arr.Count();
	}
	PKVK_TmpBufferDetails tmpBufferDetails{};
	PKVK_BeginBuffer(graphicsFamilyIndex, sizeof(pke_ui_box_instance_buffer_item) * arr.Count(), tmpBufferDetails);
	{
		VkCommandBufferBeginInfo vkCommandBufferBeginInfo;
		vkCommandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
		vkCommandBufferBeginInfo.pNext = nullptr;
		// TODO consider single-use?
		vkCommandBufferBeginInfo.flags = 0;
		vkCommandBufferBeginInfo.pInheritanceInfo = nullptr;
		vkResult = vkBeginCommandBuffer(tmpBufferDetails.cmdBuffer, &vkCommandBufferBeginInfo);
		assert(vkResult == VK_SUCCESS);

		memcpy(tmpBufferDetails.deviceData, arr.GetPtr(), sizeof(pke_ui_box_instance_buffer_item) * arr.Count());

		VkBufferCopy vk_buffer_copy{};
		vk_buffer_copy.srcOffset = 0;
		vk_buffer_copy.dstOffset = 0;
		vk_buffer_copy.size = sizeof(pke_ui_box_instance_buffer_item) * arr.Count();
		vkCmdCopyBuffer(tmpBufferDetails.cmdBuffer, tmpBufferDetails.buffer, pke_ui_master.bindings.bd_instance.buffer, 1, &vk_buffer_copy);

		vkResult = vkEndCommandBuffer(tmpBufferDetails.cmdBuffer);
		assert(vkResult == VK_SUCCESS);

		VkSubmitInfo submitInfo;
		submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
		submitInfo.pNext = nullptr;
		submitInfo.waitSemaphoreCount = 0;
		submitInfo.pWaitSemaphores = nullptr;
		submitInfo.pWaitDstStageMask = nullptr;
		submitInfo.commandBufferCount = 1;
		submitInfo.pCommandBuffers = &tmpBufferDetails.cmdBuffer;
		submitInfo.signalSemaphoreCount = 0;
		submitInfo.pSignalSemaphores = nullptr;
		vkResult = vkQueueSubmit(tmpBufferDetails.queue, 1, &submitInfo, nullptr);
		assert(vkResult == VK_SUCCESS);
		vkResult = vkQueueWaitIdle(tmpBufferDetails.queue);
		assert(vkResult == VK_SUCCESS);
	}
	PKVK_EndBuffer(tmpBufferDetails);
	pke_ui_master.bindings.instance_counter = arr.Count();
}

void pke_ui_tick(double delta) {
	(void)delta;
	if (pke_ui_master.should_recalc_ui == true || pkeSettings.rt.was_framebuffer_resized == true) {
		DynArray<pke_ui_box_instance_buffer_item> arr;
		pke_ui_master.should_recalc_ui = false;
		pke_ui_master.px_scale = glm::vec2(
			2.0 / (float)Extent.width,
			2.0 / (float)Extent.height
		);
		for (pke_ui_box_count_T i = 0; i < pke_ui_master.h_root_boxes; ++i) {
			pke_ui_box *box = pke_ui_master.root_boxes[i];
			pke_ui_calc_px(box, arr);
			pke_ui_recalc_sizes_recursive(arr, box, 0);
		}
		pke_ui_update_instance_buffer(arr);
	}
}

void pke_ui_teardown_box_recursive(pke_ui_box *box) {
	for (pke_ui_box_count_T i = 0; i < box->internal.h_children; ++i) {
		pke_ui_teardown_box_recursive(box->internal.children[i]);
	}
	if (box->internal.children != nullptr) {
		pk_delete<pke_ui_box *>(box->internal.children, box->internal.r_children);
	}
}

void pke_ui_teardown() {
	for (pke_ui_box_count_T i = 0; i < pke_ui_master.h_root_boxes; ++i) {
		pke_ui_teardown_box_recursive(pke_ui_master.root_boxes[i]);
	}
	pk_delete<pke_ui_box *>(pke_ui_master.root_boxes, pke_ui_master.r_root_boxes);
	pk_bucket_destroy(pke_ui_master.bkt);
	pke_ui_master.bkt = nullptr;
	pke_ui_master.root_boxes = nullptr;
	pke_ui_master.h_root_boxes = 0;
	pke_ui_master.r_root_boxes = 0;

	if (pke_ui_master.bindings.bd_vertex.buffer != VK_NULL_HANDLE)
		vkDestroyBuffer(vkDevice, pke_ui_master.bindings.bd_vertex.buffer, vkAllocator);
	pke_ui_master.bindings.bd_vertex.buffer = VK_NULL_HANDLE;
	pke_ui_master.bindings.bd_vertex.firstBinding = 0;
	pke_ui_master.bindings.bd_vertex.bindingCount = 0;
	pke_ui_master.bindings.bd_vertex.offsets[0] = 0;

	if (pke_ui_master.bindings.bd_uv.buffer != VK_NULL_HANDLE)
		vkDestroyBuffer(vkDevice, pke_ui_master.bindings.bd_uv.buffer, vkAllocator);
	pke_ui_master.bindings.bd_uv.buffer = VK_NULL_HANDLE;
	pke_ui_master.bindings.bd_uv.firstBinding = 0;
	pke_ui_master.bindings.bd_uv.bindingCount = 0;
	pke_ui_master.bindings.bd_uv.offsets[0] = 0;

	if (pke_ui_master.bindings.bd_index.buffer != VK_NULL_HANDLE)
		vkDestroyBuffer(vkDevice, pke_ui_master.bindings.bd_index.buffer, vkAllocator);
	pke_ui_master.bindings.bd_index.buffer = VK_NULL_HANDLE;
	pke_ui_master.bindings.bd_index.bindingCount = 0;
	pke_ui_master.bindings.bd_index.offsets[0] = 0;
	pke_ui_master.bindings.index_count = 0;

	if (pke_ui_master.bindings.bd_instance.buffer != VK_NULL_HANDLE)
		vkDestroyBuffer(vkDevice, pke_ui_master.bindings.bd_instance.buffer, vkAllocator);
	pke_ui_master.bindings.bd_instance.buffer = VK_NULL_HANDLE;
	pke_ui_master.bindings.bd_instance.firstBinding = 0;
	pke_ui_master.bindings.bd_instance.bindingCount = 0;
	pke_ui_master.bindings.instance_counter = 0;
	pke_ui_master.bindings.instance_buffer_max_count = 0;
	pke_ui_master.bindings.bd_instance.offsets[0] = 0;

	if (pke_ui_master.bindings.deviceMemoryInst != VK_NULL_HANDLE)
		vkFreeMemory(vkDevice, pke_ui_master.bindings.deviceMemoryInst, vkAllocator);
	pke_ui_master.bindings.deviceMemoryInst = VK_NULL_HANDLE;

	if (pke_ui_master.bindings.deviceMemoryVert != VK_NULL_HANDLE)
		vkFreeMemory(vkDevice, pke_ui_master.bindings.deviceMemoryVert, vkAllocator);
	pke_ui_master.bindings.deviceMemoryVert = VK_NULL_HANDLE;
}

pke_ui_box *pke_ui_box_new_root() {
	if (pke_ui_master.h_root_boxes == pke_ui_master.r_root_boxes) {
		pke_ui_box_count_T prev_r_root_boxes = pke_ui_master.r_root_boxes;
		pke_ui_master.r_root_boxes *= 1.5;
		pke_ui_box **boxes = pk_new<pke_ui_box*>(pke_ui_master.r_root_boxes);
		for (pke_ui_box_count_T i = 0; i < pke_ui_master.h_root_boxes; ++i) {
			boxes[i] = pke_ui_master.root_boxes[i];
		}
		if (pke_ui_master.root_boxes != nullptr) pk_delete<pke_ui_box*>(pke_ui_master.root_boxes, prev_r_root_boxes);
		pke_ui_master.root_boxes = boxes;
	}
	pke_ui_box *box = pk_new<pke_ui_box>(pke_ui_master.bkt);
	memset(box, 0, sizeof(pke_ui_box));
	pke_ui_master.root_boxes[pke_ui_master.h_root_boxes] = box;
	pke_ui_master.h_root_boxes += 1;
	pke_ui_master.should_recalc_ui = true;
	return box;
}

pke_ui_box *pke_ui_box_new_child(pke_ui_box *parent) {
	assert(parent != nullptr);
	if (parent->internal.h_children == parent->internal.r_children) {
		pke_ui_box_count_T prev_r_children = parent->internal.r_children;
		parent->internal.r_children = PK_MAX(parent->internal.r_children * 1.5, 2);
		pke_ui_box **boxes = pk_new<pke_ui_box*>(parent->internal.r_children);
		for (pke_ui_box_count_T i = 0; i < parent->internal.h_children; ++i) {
			boxes[i] = parent->internal.children[i];
		}
		if (parent->internal.children != nullptr) {
			pk_delete<pke_ui_box*>(parent->internal.children, prev_r_children);
		}
		parent->internal.children = boxes;
	}
	pke_ui_box *box = pk_new<pke_ui_box>(pke_ui_master.bkt);
	memset(box, 0, sizeof(pke_ui_box));
	parent->internal.children[parent->internal.h_children] = box;
	parent->internal.h_children += 1;
	box->internal.parent = parent;
	pke_ui_master.should_recalc_ui = true;
	return box;
}

pke_ui_graphics_bindings *pke_ui_get_graphics_bindings() {
	return &pke_ui_master.bindings;
}