summaryrefslogtreecommitdiffstatsci
path: root/src/vkvg_device.c
blob: 6acb1c977a5103698af3f30d8e9d497f2423f673 (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
/*
 * Copyright (c) 2018-2022 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "vkvg_device_internal.h"
#include "vkh_queue.h"
#include "vkh_phyinfo.h"
#include "vk_mem_alloc.h"

#define TRY_LOAD_DEVICE_EXT(ext) {								\
if (vkh_phyinfo_try_get_extension_properties(pi, #ext, NULL))	\
	enabledExts[enabledExtsCount++] = #ext;						\
}

void _device_init (VkvgDevice dev, VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex, VkSampleCountFlags samples, bool deferredResolve) {
	dev->instance	= inst;
	dev->hdpi		= 72;
	dev->vdpi		= 72;
	dev->samples	= samples;
	dev->vkDev		= vkdev;
	dev->phy		= phy;
	dev->deferredResolve = deferredResolve;

#if VKVG_DBG_STATS
	dev->debug_stats = (vkvg_debug_stats_t) {0};
#endif

	VkFormat format = FB_COLOR_FORMAT;

	_check_best_image_tiling(dev, format);
	if (dev->status != VKVG_STATUS_SUCCESS)
		return;

	if (!_init_function_pointers (dev)){
		dev->status = VKVG_STATUS_NULL_POINTER;
		return;
	}

	VkhPhyInfo phyInfos = vkh_phyinfo_create (dev->phy, NULL);

	dev->phyMemProps = phyInfos->memProps;
	dev->gQueue = vkh_queue_create ((VkhDevice)dev, qFamIdx, qIndex);
	//mtx_init (&dev->gQMutex, mtx_plain);

	vkh_phyinfo_destroy (phyInfos);

	VmaAllocatorCreateInfo allocatorInfo = {
		.physicalDevice = phy,
		.device = vkdev
	};
	vmaCreateAllocator(&allocatorInfo, &dev->allocator);

	dev->cmdPool= vkh_cmd_pool_create		((VkhDevice)dev, dev->gQueue->familyIndex, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
	dev->cmd	= vkh_cmd_buff_create		((VkhDevice)dev, dev->cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);

	_sync_context_init			((VkhDevice)dev, &dev->syncCtx);
	_create_pipeline_cache		(dev);
	_init_fonts_cache			(dev);
	if (dev->deferredResolve || dev->samples == VK_SAMPLE_COUNT_1_BIT){
		dev->renderPass = _createRenderPassNoResolve (dev, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_LOAD_OP_LOAD);
		dev->renderPass_ClearStencil = _createRenderPassNoResolve (dev, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_LOAD_OP_CLEAR);
		dev->renderPass_ClearAll = _createRenderPassNoResolve (dev, VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_LOAD_OP_CLEAR);
	}else{
		dev->renderPass = _createRenderPassMS (dev, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_LOAD_OP_LOAD);
		dev->renderPass_ClearStencil = _createRenderPassMS (dev, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_LOAD_OP_CLEAR);
		dev->renderPass_ClearAll = _createRenderPassMS (dev, VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_LOAD_OP_CLEAR);
	}
	_createDescriptorSetLayout	(dev);
	_setupPipelines				(dev);

	_create_empty_texture		(dev, format, dev->supportedTiling);

#if defined(DEBUG) && defined (VKVG_DBG_UTILS)
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_COMMAND_POOL, (uint64_t)dev->cmdPool, "Device Cmd Pool");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)dev->cmd, "Device Cmd Buff");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_FENCE, (uint64_t)dev->syncCtx.fence, "Device Fence");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_SEMAPHORE, (uint64_t)dev->syncCtx.signals[0], "Device Semaphore A");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_SEMAPHORE, (uint64_t)dev->syncCtx.signals[1], "Device Semaphore B");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)dev->renderPass, "RP load img/stencil");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)dev->renderPass_ClearStencil, "RP clear stencil");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)dev->renderPass_ClearAll, "RP clear all");

	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, (uint64_t)dev->dslSrc, "DSLayout SOURCE");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, (uint64_t)dev->dslFont, "DSLayout FONT");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, (uint64_t)dev->dslGrad, "DSLayout GRADIENT");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_PIPELINE_LAYOUT, (uint64_t)dev->pipelineLayout, "PLLayout dev");

#ifndef __APPLE__
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_PIPELINE, (uint64_t)dev->pipelinePolyFill, "PL Poly fill");
#endif
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_PIPELINE, (uint64_t)dev->pipelineClipping, "PL Clipping");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_PIPELINE, (uint64_t)dev->pipe_OVER, "PL draw Over");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_PIPELINE, (uint64_t)dev->pipe_SUB, "PL draw Substract");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_PIPELINE, (uint64_t)dev->pipe_CLEAR, "PL draw Clear");

	vkh_image_set_name(dev->emptyImg, "empty IMG");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_IMAGE_VIEW, (uint64_t)vkh_image_get_view(dev->emptyImg), "empty IMG VIEW");
	vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_SAMPLER, (uint64_t)vkh_image_get_sampler(dev->emptyImg), "empty IMG SAMPLER");
#endif
	dev->status = VKVG_STATUS_SUCCESS;
}

VkvgDevice vkvg_device_create(VkSampleCountFlags samples, bool deferredResolve) {
	LOG(VKVG_LOG_INFO, "CREATE Device\n");
	VkvgDevice dev = (vkvg_device*)calloc(1,sizeof(vkvg_device));
	if (!dev) {
		LOG(VKVG_LOG_ERR, "CREATE Device failed, no memory\n");
		exit(-1);
	}

	dev->references = 1;

	const char* enabledExts [10];
	const char* enabledLayers[10];
	uint32_t enabledExtsCount = 0, enabledLayersCount = 0, phyCount = 0;

	vkh_layers_check_init();

#ifdef VKVG_USE_VALIDATION
	if (vkh_layer_is_present("VK_LAYER_KHRONOS_validation"))
		enabledLayers[enabledLayersCount++] = "VK_LAYER_KHRONOS_validation";
#endif

#ifdef VKVG_USE_RENDERDOC
	if (vkh_layer_is_present("VK_LAYER_RENDERDOC_Capture"))
		enabledLayers[enabledLayersCount++] = "VK_LAYER_RENDERDOC_Capture";
#endif
	vkh_layers_check_release();

	vkh_instance_extensions_check_init ();

#if defined(DEBUG) && defined (VKVG_DBG_UTILS)
	bool dbgUtilsSupported = vkh_instance_extension_supported("VK_EXT_debug_utils");
	 if (dbgUtilsSupported)
		enabledExts[enabledExtsCount++] = "VK_EXT_debug_utils";
#endif
	if (vkh_instance_extension_supported("VK_KHR_get_physical_device_properties2"))
		enabledExts[enabledExtsCount++] = "VK_KHR_get_physical_device_properties2";

	vkh_instance_extensions_check_release();

	VkhApp app =  vkh_app_create(1, 2, "vkvg", enabledLayersCount, enabledLayers, enabledExtsCount, enabledExts);

#if defined(DEBUG) && defined (VKVG_DBG_UTILS)
	if (dbgUtilsSupported)
		vkh_app_enable_debug_messenger(app
								, VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT
								, VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT
								, NULL);
#endif
	VkhPhyInfo* phys = vkh_app_get_phyinfos (app, &phyCount, VK_NULL_HANDLE);
	if (phyCount == 0) {
		dev->status = VKVG_STATUS_DEVICE_ERROR;
		vkh_app_destroy (app);
		return dev;
	}

	VkhPhyInfo pi = 0;
	if (!_try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU, &pi))
		if (!_try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU, &pi))
			pi = phys[0];

	if (!(pi->properties.limits.framebufferColorSampleCounts&samples)) {
		LOG(VKVG_LOG_ERR, "CREATE Device failed: sample count not supported: %d\n", samples);
		dev->status = VKVG_STATUS_DEVICE_ERROR;
		vkh_app_free_phyinfos (phyCount, phys);
		vkh_app_destroy (app);
		return dev;
	}

	uint32_t qCount = 0;
	float qPriorities[] = {0.0};
	VkDeviceQueueCreateInfo pQueueInfos[] = { {0},{0},{0} };

	if (vkh_phyinfo_create_queues (pi, pi->gQueue, 1, qPriorities, &pQueueInfos[qCount]))
		qCount++;

	enabledExtsCount=0;

	VkPhysicalDeviceFeatures2 phyFeat2 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
	VkPhysicalDeviceScalarBlockLayoutFeatures scalarBlockLayoutSupport = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES};
	phyFeat2.pNext = &scalarBlockLayoutSupport;

	vkGetPhysicalDeviceFeatures2(pi->phy, &phyFeat2);

	TRY_LOAD_DEVICE_EXT (VK_EXT_blend_operation_advanced)
	//https://vulkan.lunarg.com/doc/view/1.2.162.0/mac/1.2-extensions/vkspec.html#VK_KHR_portability_subset
	TRY_LOAD_DEVICE_EXT (VK_KHR_portability_subset)
	TRY_LOAD_DEVICE_EXT (VK_KHR_relaxed_block_layout)
	TRY_LOAD_DEVICE_EXT (VK_EXT_scalar_block_layout)

	VkPhysicalDeviceFeatures enabledFeatures = {
		.fillModeNonSolid = true,
		//.sampleRateShading = true
	};
	VkPhysicalDeviceVulkan12Features enabledFeatures12 = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
		.scalarBlockLayout = true
	};

	VkDeviceCreateInfo device_info = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
									   .queueCreateInfoCount = qCount,
									   .pQueueCreateInfos = (VkDeviceQueueCreateInfo*)&pQueueInfos,
									   .enabledExtensionCount = enabledExtsCount,
									   .ppEnabledExtensionNames = enabledExts,
									   .pEnabledFeatures = &enabledFeatures,
									   .pNext = &enabledFeatures12};

	VkhDevice vkhd = vkh_device_create(app, pi, &device_info);

	_device_init (dev,
				vkh_app_get_inst(app),
				vkh_device_get_phy(vkhd),
				vkh_device_get_vkdev(vkhd),
				pi->gQueue, 0,
				samples, deferredResolve);

	dev->vkhDev = vkhd;

	vkh_app_free_phyinfos (phyCount, phys);

	return dev;
}
VkvgDevice vkvg_device_create_from_vk(VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex)
{
	return vkvg_device_create_from_vk_multisample (inst,phy,vkdev,qFamIdx,qIndex, VK_SAMPLE_COUNT_1_BIT, false);
}
VkvgDevice vkvg_device_create_from_vk_multisample(VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex, VkSampleCountFlags samples, bool deferredResolve)
{
	LOG(VKVG_LOG_INFO, "CREATE Device from vk: qFam = %d; qIdx = %d\n", qFamIdx, qIndex);
	VkvgDevice dev = (vkvg_device*)calloc(1,sizeof(vkvg_device));
	if (!dev) {
		LOG(VKVG_LOG_ERR, "CREATE Device failed, no memory\n");
		exit(-1);
	}
	dev->references = 1;
	_device_init(dev, inst, phy, vkdev, qFamIdx, qIndex, samples, deferredResolve);
	return dev;
}

void vkvg_device_destroy (VkvgDevice dev)
{
	dev->references--;
	if (dev->references > 0)
		return;

	LOG(VKVG_LOG_INFO, "DESTROY Device\n");

	vkh_image_destroy				(dev->emptyImg);

	vkDestroyDescriptorSetLayout	(dev->vkDev, dev->dslGrad,NULL);
	vkDestroyDescriptorSetLayout	(dev->vkDev, dev->dslFont,NULL);
	vkDestroyDescriptorSetLayout	(dev->vkDev, dev->dslSrc, NULL);
#ifndef __APPLE__
	vkDestroyPipeline				(dev->vkDev, dev->pipelinePolyFill, NULL);
#endif
	vkDestroyPipeline				(dev->vkDev, dev->pipelineClipping, NULL);

	vkDestroyPipeline				(dev->vkDev, dev->pipe_OVER,	NULL);
	vkDestroyPipeline				(dev->vkDev, dev->pipe_SUB,		NULL);
	vkDestroyPipeline				(dev->vkDev, dev->pipe_CLEAR,	NULL);

#ifdef VKVG_WIRED_DEBUG
	vkDestroyPipeline				(dev->vkDev, dev->pipelineWired, NULL);
	vkDestroyPipeline				(dev->vkDev, dev->pipelineLineList, NULL);
#endif

	vkDestroyPipelineLayout			(dev->vkDev, dev->pipelineLayout, NULL);
	vkDestroyPipelineCache			(dev->vkDev, dev->pipelineCache, NULL);
	vkDestroyRenderPass				(dev->vkDev, dev->renderPass, NULL);
	vkDestroyRenderPass				(dev->vkDev, dev->renderPass_ClearStencil, NULL);
	vkDestroyRenderPass				(dev->vkDev, dev->renderPass_ClearAll, NULL);

	_sync_context_wait				(&dev->syncCtx, UINT64_MAX);
	_sync_context_clear				(&dev->syncCtx);

	vkFreeCommandBuffers			(dev->vkDev, dev->cmdPool, 1, &dev->cmd);
	vkDestroyCommandPool			(dev->vkDev, dev->cmdPool, NULL);

	vkh_queue_destroy(dev->gQueue);

	_destroy_font_cache(dev);

	vmaDestroyAllocator (dev->allocator);

	if (dev->vkhDev) {
		VkhApp app = vkh_device_get_app (dev->vkhDev);
		vkh_device_destroy (dev->vkhDev);
		vkh_app_destroy (app);
	}

	free(dev);
}

vkvg_status_t vkvg_device_status (VkvgDevice dev) {
	return dev->status;
}
VkvgDevice vkvg_device_reference (VkvgDevice dev) {
	dev->references++;
	return dev;
}
uint32_t vkvg_device_get_reference_count (VkvgDevice dev) {
	return dev->references;
}
void vkvg_device_set_dpy (VkvgDevice dev, int hdpy, int vdpy) {
	dev->hdpi = hdpy;
	dev->vdpi = vdpy;

	//TODO: reset font cache
}
void vkvg_device_get_dpy (VkvgDevice dev, int* hdpy, int* vdpy) {
	*hdpy = dev->hdpi;
	*vdpy = dev->vdpi;
}
void vkvg_device_set_queue_guards (VkvgDevice dev, vkvg_queue_guard before_submit, vkvg_queue_guard after_submit, void* user_data) {
	dev->gQLockGuard = before_submit;
	dev->gQUnlockGuard = after_submit;
	dev->gQGuardUserData = user_data;
}
#if VKVG_DBG_STATS
vkvg_debug_stats_t vkvg_device_get_stats (VkvgDevice dev) {
	return dev->debug_stats;
}
vkvg_debug_stats_t vkvg_device_reset_stats (VkvgDevice dev) {
	dev->debug_stats = (vkvg_debug_stats_t) {0};
}
#endif