32 #include "../ref_vk/vk_local.h"
34 #include "../win32/vk_win.h"
37 #include "../linux/vk_linux.h"
40 #include "../macos/vk_macos.h"
53 .logical = VK_NULL_HANDLE,
54 .gfxQueue = VK_NULL_HANDLE,
55 .presentQueue = VK_NULL_HANDLE,
56 .transferQueue = VK_NULL_HANDLE,
58 .presentFamilyIndex = -1,
59 .transferFamilyIndex = -1
65 .format = VK_FORMAT_UNDEFINED,
66 .presentMode = VK_PRESENT_MODE_MAILBOX_KHR,
77 .colorLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
78 .sampleCount = VK_SAMPLE_COUNT_1_BIT
83 .colorLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
84 .sampleCount = VK_SAMPLE_COUNT_1_BIT
89 .colorLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
90 .sampleCount = VK_SAMPLE_COUNT_1_BIT
117 #define NUM_CMDBUFFERS 2
174 #define VK_INPUTBIND_DESC(s) { \
177 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX \
180 #define VK_INPUTATTR_DESC(l, f, o) { \
187 #define VK_VERTEXINPUT_CINF(b, a) { \
188 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, \
191 .vertexBindingDescriptionCount = 1, \
192 .pVertexBindingDescriptions = &b, \
193 .vertexAttributeDescriptionCount = sizeof(a) / sizeof(a[0]), \
194 .pVertexAttributeDescriptions = a \
197 #define VK_VERTINFO(name, bindSize, ...) \
198 VkVertexInputAttributeDescription attrDesc##name[] = { __VA_ARGS__ }; \
199 VkVertexInputBindingDescription name##bindingDesc = VK_INPUTBIND_DESC(bindSize); \
200 VkPipelineVertexInputStateCreateInfo vertInfo##name = VK_VERTEXINPUT_CINF(name##bindingDesc, attrDesc##name);
202 #define VK_NULL_VERTEXINPUT_CINF { \
203 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, \
206 .vertexBindingDescriptionCount = 0, \
207 .pVertexBindingDescriptions = NULL, \
208 .vertexAttributeDescriptionCount = 0, \
209 .pVertexAttributeDescriptions = NULL \
212 #define VK_LOAD_VERTFRAG_SHADERS(shaders, namevert, namefrag) \
213 vkDestroyShaderModule(vk_device.logical, shaders[0].module, NULL); \
214 vkDestroyShaderModule(vk_device.logical, shaders[1].module, NULL); \
215 shaders[0] = QVk_CreateShader(namevert##_vert_spv, namevert##_vert_size, VK_SHADER_STAGE_VERTEX_BIT); \
216 shaders[1] = QVk_CreateShader(namefrag##_frag_spv, namefrag##_frag_size, VK_SHADER_STAGE_FRAGMENT_BIT); \
217 QVk_DebugSetObjectName((uint64_t)shaders[0].module, VK_OBJECT_TYPE_SHADER_MODULE, "Shader Module: "#namevert".vert"); \
218 QVk_DebugSetObjectName((uint64_t)shaders[1].module, VK_OBJECT_TYPE_SHADER_MODULE, "Shader Module: "#namefrag".frag");
226 #define NUM_DYNBUFFERS 2
240 #define NUM_SWAPBUFFER_SLOTS 4
247 #define BUFFER_RESIZE_FACTOR 2.f
249 #define UNIFORM_ALLOC_SIZE 1024
251 #define VERTEX_BUFFER_SIZE (1024 * 1024)
252 #define INDEX_BUFFER_SIZE (2 * 1024)
253 #define UNIFORM_BUFFER_SIZE (2048 * 1024)
255 #define STAGING_BUFFER_MAXSIZE (8192 * 1024)
257 #define TRIANGLE_FAN_INDEX_CNT 200
269 VkFormat depthFormats[] = {
270 VK_FORMAT_D32_SFLOAT_S8_UINT,
271 VK_FORMAT_D32_SFLOAT,
272 VK_FORMAT_D24_UNORM_S8_UINT,
273 VK_FORMAT_D16_UNORM_S8_UINT,
277 for (
int i = 0;
i < 5; ++
i)
279 VkFormatProperties formatProps;
280 vkGetPhysicalDeviceFormatProperties(
vk_device.
physical, depthFormats[
i], &formatProps);
282 if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
283 return depthFormats[
i];
286 return VK_FORMAT_D16_UNORM;
292 static VkSampleCountFlagBits msaaModes[] = {
293 VK_SAMPLE_COUNT_1_BIT,
294 VK_SAMPLE_COUNT_2_BIT,
295 VK_SAMPLE_COUNT_4_BIT,
296 VK_SAMPLE_COUNT_8_BIT,
297 VK_SAMPLE_COUNT_16_BIT
320 VkResult res = VK_SUCCESS;
329 if (res != VK_SUCCESS)
363 VkFramebufferCreateInfo fbCreateInfos[] = {
366 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
377 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
381 .attachmentCount = 3,
388 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
392 .attachmentCount = 2,
402 fbCreateInfos[
RP_WORLD].pAttachments = worldAttachments;
408 fbCreateInfos[
RP_UI].pAttachments = uiAttachments;
415 if (res != VK_SUCCESS)
435 VkAttachmentDescription worldAttachments[] = {
440 .samples = VK_SAMPLE_COUNT_1_BIT,
443 .storeOp = msaaEnabled ? VK_ATTACHMENT_STORE_OP_DONT_CARE : VK_ATTACHMENT_STORE_OP_STORE,
444 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
445 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
446 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
447 .finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
454 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
455 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
456 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
457 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
458 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
459 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
467 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
468 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
469 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
470 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
471 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
475 VkAttachmentReference worldAttachmentRefs[] = {
478 .attachment = msaaEnabled ? 2 : 0,
479 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
484 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
489 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
494 VkSubpassDescription worldSubpassDesc = {
496 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
497 .inputAttachmentCount = 0,
498 .pInputAttachments =
NULL,
499 .colorAttachmentCount = 1,
500 .pColorAttachments = &worldAttachmentRefs[0],
501 .pResolveAttachments = msaaEnabled ? &worldAttachmentRefs[2] :
NULL,
502 .pDepthStencilAttachment = &worldAttachmentRefs[1],
503 .preserveAttachmentCount = 0,
504 .pPreserveAttachments =
NULL
510 VkAttachmentDescription warpAttachments[] = {
515 .samples = VK_SAMPLE_COUNT_1_BIT,
516 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
517 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
518 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
519 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
520 .initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
521 .finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
527 .samples = VK_SAMPLE_COUNT_1_BIT,
528 .loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
529 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
530 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
531 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
532 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
533 .finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
537 VkAttachmentReference warpAttachmentRef = {
540 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
544 VkSubpassDescription warpSubpassDesc = {
546 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
547 .inputAttachmentCount = 0,
548 .pInputAttachments =
NULL,
549 .colorAttachmentCount = 1,
550 .pColorAttachments = &warpAttachmentRef,
551 .pResolveAttachments =
NULL,
552 .pDepthStencilAttachment =
NULL,
553 .preserveAttachmentCount = 0,
554 .pPreserveAttachments =
NULL
560 VkAttachmentDescription uiAttachments[] = {
565 .samples = VK_SAMPLE_COUNT_1_BIT,
566 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
567 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
568 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
569 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
570 .initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
571 .finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
577 .samples = VK_SAMPLE_COUNT_1_BIT,
578 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
579 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
580 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
581 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
582 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
583 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
589 .samples = VK_SAMPLE_COUNT_1_BIT,
590 .loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
591 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
592 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
593 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
594 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
595 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
600 VkAttachmentReference uiAttachmentRefs[] = {
604 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
609 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
613 VkSubpassDescription uiSubpassDesc = {
615 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
616 .inputAttachmentCount = 0,
617 .pInputAttachments =
NULL,
618 .colorAttachmentCount = 1,
619 .pColorAttachments = &uiAttachmentRefs[1],
620 .pResolveAttachments =
NULL,
621 .pDepthStencilAttachment = &uiAttachmentRefs[0],
622 .preserveAttachmentCount = 0,
623 .pPreserveAttachments =
NULL
630 VkSubpassDependency subpassDeps[2] = {
632 .srcSubpass = VK_SUBPASS_EXTERNAL,
634 .srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
635 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
636 .srcAccessMask = VK_ACCESS_SHADER_READ_BIT,
637 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
638 .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT
642 .dstSubpass = VK_SUBPASS_EXTERNAL,
643 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
644 .dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
645 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
646 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
647 .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT
651 VkRenderPassCreateInfo rpCreateInfos[] = {
654 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
657 .attachmentCount = msaaEnabled ? 3 : 2,
658 .pAttachments = worldAttachments,
660 .pSubpasses = &worldSubpassDesc,
661 .dependencyCount = 2,
662 .pDependencies = subpassDeps
666 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
669 .attachmentCount = 3,
670 .pAttachments = uiAttachments,
672 .pSubpasses = &uiSubpassDesc,
673 .dependencyCount = 2,
674 .pDependencies = subpassDeps
678 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
681 .attachmentCount = 2,
682 .pAttachments = warpAttachments,
684 .pSubpasses = &warpSubpassDesc,
685 .dependencyCount = 2,
686 .pDependencies = subpassDeps
693 if (res != VK_SUCCESS)
741 if (drawBuffer->
image != VK_NULL_HANDLE)
745 drawBuffer->
image = VK_NULL_HANDLE;
763 VkDescriptorSetLayoutBinding layoutBinding = {
765 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
766 .descriptorCount = 1,
767 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
768 .pImmutableSamplers =
NULL
771 VkDescriptorSetLayoutCreateInfo layoutInfo = {
772 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
776 .pBindings = &layoutBinding
782 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
783 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
796 VkSamplerCreateInfo samplerInfo = {
797 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
800 .magFilter = VK_FILTER_NEAREST,
801 .minFilter = VK_FILTER_NEAREST,
802 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
803 .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
804 .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
805 .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
807 .anisotropyEnable = VK_FALSE,
808 .maxAnisotropy = 1.f,
809 .compareEnable = VK_FALSE,
810 .compareOp = VK_COMPARE_OP_ALWAYS,
813 .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK,
814 .unnormalizedCoordinates = VK_FALSE
820 samplerInfo.maxLod = FLT_MAX;
824 samplerInfo.magFilter = VK_FILTER_LINEAR;
825 samplerInfo.minFilter = VK_FILTER_LINEAR;
826 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
830 samplerInfo.maxLod = 1.f;
835 assert((
vk_device.
properties.limits.maxSamplerAnisotropy > 1.f) &&
"maxSamplerAnisotropy is 1");
837 samplerInfo.magFilter = VK_FILTER_NEAREST;
838 samplerInfo.minFilter = VK_FILTER_NEAREST;
839 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
840 samplerInfo.anisotropyEnable = VK_TRUE;
842 samplerInfo.maxLod = 1.f;
847 samplerInfo.maxLod = FLT_MAX;
851 samplerInfo.magFilter = VK_FILTER_LINEAR;
852 samplerInfo.minFilter = VK_FILTER_LINEAR;
853 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
857 samplerInfo.maxLod = 1.f;
878 VkDescriptorPoolSize poolSizes[] = {
881 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
882 .descriptorCount = 16
886 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
891 VkDescriptorPoolCreateInfo poolInfo = {
892 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
894 .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
896 .poolSizeCount =
sizeof(poolSizes) /
sizeof(poolSizes[0]),
897 .pPoolSizes = poolSizes,
907 VkDescriptorSetAllocateInfo dsAllocInfo = {
908 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
911 .descriptorSetCount = 1,
917 VkDescriptorBufferInfo bufferInfo = {
923 VkWriteDescriptorSet descriptorWrite = {
924 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
928 .dstArrayElement = 0,
929 .descriptorCount = 1,
930 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
932 .pBufferInfo = &bufferInfo,
933 .pTexelBufferView =
NULL,
1007 VkDeviceSize dstOffset = 0;
1009 uint16_t *iboData =
NULL;
1010 uint16_t *fanData = malloc(bufferSize);
1016 fanData[idx++] =
i + 1;
1017 fanData[idx++] =
i + 2;
1026 memcpy(iboData, fanData, bufferSize);
1042 VkFenceCreateInfo fCreateInfo = {
1043 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1068 VkMemoryBarrier memBarrier = {
1069 .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
1071 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
1072 .dstAccessMask = VK_ACCESS_INDEX_READ_BIT | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
1075 vkCmdPipelineBarrier(
vk_stagingBuffers[index].cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, 0, 1, &memBarrier, 0,
NULL, 0,
NULL);
1078 VkSubmitInfo submitInfo = {
1079 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
1080 .waitSemaphoreCount = 0,
1081 .pWaitSemaphores =
NULL,
1082 .signalSemaphoreCount = 0,
1083 .pSignalSemaphores =
NULL,
1084 .pWaitDstStageMask =
NULL,
1085 .commandBufferCount = 1,
1098 const float texVerts[] = { -1., -1., 0., 0.,
1103 const float colorVerts[] = { -1., -1.,
1108 const uint32_t
indices[] = { 0, 1, 2, 0, 3, 1 };
1157 qvkshader_t shaders[2] = { VK_NULL_HANDLE, VK_NULL_HANDLE };
1160 VkPushConstantRange pushConstantRangeVert = {
1161 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1163 .size = 32 *
sizeof(float)
1165 VkPushConstantRange pushConstantRangeFrag = {
1166 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1168 .size = 4 *
sizeof(float)
1197 for (
int i = 0;
i < 2; ++
i)
1218 for (
int i = 0;
i < 2; ++
i)
1382 for (
int i = 0;
i < 2; ++
i)
1502 # pragma warning (disable : 4113 4133 4047 )
1512 PFN_vkEnumerateInstanceVersion vkEnumerateInstanceVersion = (PFN_vkEnumerateInstanceVersion)vkGetInstanceProcAddr(
NULL,
"vkEnumerateInstanceVersion");
1513 uint32_t instanceVersion = VK_API_VERSION_1_0;
1515 if (vkEnumerateInstanceVersion)
1517 VK_VERIFY(vkEnumerateInstanceVersion(&instanceVersion));
1520 VkApplicationInfo appInfo = {
1521 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
1523 .pApplicationName =
"Quake 2",
1524 .applicationVersion = VK_MAKE_VERSION(3, 21, 0),
1525 .pEngineName =
"id Tech 2",
1526 .engineVersion = VK_MAKE_VERSION(2, 0, 0),
1527 .apiVersion = instanceVersion
1531 char **wantedExtensions;
1553 #if defined(_DEBUG) || defined(ENABLE_DEBUG_LABELS)
1558 wantedExtensions = (
char **)malloc(extCount *
sizeof(
const char *));
1562 wantedExtensions[extCount - 1] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
1563 #if defined(_DEBUG) || defined(ENABLE_DEBUG_LABELS)
1565 wantedExtensions[extCount - 1] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
1569 for (
int i = 0;
i < extCount;
i++)
1576 VkInstanceCreateInfo createInfo = {
1577 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
1579 .pApplicationInfo = &appInfo,
1580 .enabledLayerCount = 0,
1581 .ppEnabledLayerNames =
NULL,
1582 .enabledExtensionCount = extCount,
1583 .ppEnabledExtensionNames = (
const char*
const*)wantedExtensions
1586 #if VK_HEADER_VERSION > 101
1587 const char *validationLayers[] = {
"VK_LAYER_KHRONOS_validation" };
1589 const char *validationLayers[] = {
"VK_LAYER_LUNARG_standard_validation" };
1594 createInfo.enabledLayerCount =
sizeof(validationLayers) /
sizeof(validationLayers[0]);
1595 createInfo.ppEnabledLayerNames = validationLayers;
1596 for (
int i = 0;
i < createInfo.enabledLayerCount;
i++)
1603 free(wantedExtensions);
1605 if (res != VK_SUCCESS)
1625 if (res != VK_SUCCESS)
1641 .preferredLargeHeapBlockSize = 0,
1642 .pAllocationCallbacks =
NULL,
1643 .pDeviceMemoryCallbacks =
NULL,
1644 .frameInUseCount = 0,
1645 .pHeapSizeLimit =
NULL,
1646 .pVulkanFunctions =
NULL,
1647 .pRecordSettings =
NULL
1651 if (res != VK_SUCCESS)
1660 if (res != VK_SUCCESS)
1679 VkFenceCreateInfo fCreateInfo = {
1680 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1682 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1684 VkSemaphoreCreateInfo sCreateInfo = {
1685 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
1708 VkSampleCountFlagBits supportedMsaa =
vk_device.
properties.limits.framebufferColorSampleCounts;
1709 if (!(supportedMsaa & msaaMode))
1713 msaaMode = VK_SAMPLE_COUNT_1_BIT;
1722 if (res != VK_SUCCESS)
1731 if (res != VK_SUCCESS)
1737 if (res != VK_SUCCESS)
1752 if (res != VK_SUCCESS)
1761 if (res != VK_SUCCESS)
1771 VkCommandBufferAllocateInfo cbInfo = {
1772 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1775 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1780 if (res != VK_SUCCESS)
1806 VkDescriptorSetAllocateInfo dsAllocInfo = {
1807 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1810 .descriptorSetCount = 1,
1850 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_SURFACE_LOST_KHR)
1855 else if (result != VK_SUCCESS)
1857 Sys_Error(
"QVk_BeginFrame(): unexpected error after vkAcquireNextImageKHR: %s",
QVk_GetError(result));
1864 VkCommandBufferBeginInfo beginInfo = {
1865 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1867 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
1868 .pInheritanceInfo =
NULL
1884 return VK_NOT_READY;
1902 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1903 VkSubmitInfo submitInfo = {
1904 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
1905 .waitSemaphoreCount = 1,
1907 .signalSemaphoreCount = 1,
1909 .pWaitDstStageMask = &waitStages,
1910 .commandBufferCount = 1,
1917 VkPresentInfoKHR presentInfo = {
1918 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1919 .waitSemaphoreCount = 1,
1921 .swapchainCount = 1,
1930 if (renderResult == VK_ERROR_OUT_OF_DATE_KHR || renderResult == VK_SUBOPTIMAL_KHR || renderResult == VK_ERROR_SURFACE_LOST_KHR)
1935 else if (renderResult != VK_SUCCESS)
1937 Sys_Error(
"QVk_EndFrame(): unexpected error after vkQueuePresentKHR: %s",
QVk_GetError(renderResult));
1943 return renderResult;
1948 VkClearValue clearColors[3] = {
1949 {.color = { 1.f, .0f, .5f, 1.f } },
1950 {.depthStencil = { 1.f, 0 } },
1951 {.color = { 1.f, .0f, .5f, 1.f } },
1954 VkRenderPassBeginInfo renderBeginInfo[] = {
1957 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1960 .renderArea.offset = { 0, 0 },
1963 .pClearValues = clearColors
1967 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1970 .renderArea.offset = { 0, 0 },
1972 .clearValueCount = 2,
1973 .pClearValues = clearColors
1977 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1980 .renderArea.offset = { 0, 0 },
1982 .clearValueCount = 1,
1983 .pClearValues = clearColors
1987 #if defined(_DEBUG) || defined(ENABLE_DEBUG_LABELS)
1991 if (rpType ==
RP_UI) {
2061 const int align_mod = size % 4;
2062 const uint32_t aligned_size = ((size % 4) == 0) ? size : (size + 4 - align_mod);
2103 const int align_mod = size % 256;
2104 const uint32_t aligned_size = ((size % 256) == 0) ? size : (size + 256 - align_mod);
2161 Sys_Error(
"QVk_GetStagingBuffer(): Cannot allocate staging buffer space!");
2175 VkCommandBufferBeginInfo beginInfo = {
2176 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2178 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
2179 .pInheritanceInfo =
NULL
2227 assert((
vk_samplers[samplerType] != VK_NULL_HANDLE) &&
"Sampler is VK_NULL_HANDLE!");
2229 VkDescriptorImageInfo dImgInfo = {
2231 .imageView =
texture->imageView,
2232 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
2235 VkWriteDescriptorSet writeSet = {
2236 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2238 .dstSet =
texture->descriptorSet,
2240 .dstArrayElement = 0,
2241 .descriptorCount = 1,
2242 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2243 .pImageInfo = &dImgInfo,
2244 .pBufferInfo =
NULL,
2245 .pTexelBufferView =
NULL
2256 VkDescriptorSet uboDescriptorSet;
2258 memcpy(vertData, ubo, uboSize);
2261 VkDeviceSize offsets = 0;
2271 VkDescriptorSet uboDescriptorSet;
2273 memcpy(uboData, ubo, uboSize);
2276 VkDeviceSize offsets = 0;
2277 VkDescriptorSet descriptorSets[] = {
texture->descriptorSet, uboDescriptorSet };
2295 #define ERRSTR(r) case VK_ ##r: return "VK_"#r
2304 ERRSTR(ERROR_OUT_OF_HOST_MEMORY);
2305 ERRSTR(ERROR_OUT_OF_DEVICE_MEMORY);
2306 ERRSTR(ERROR_INITIALIZATION_FAILED);
2307 ERRSTR(ERROR_DEVICE_LOST);
2308 ERRSTR(ERROR_MEMORY_MAP_FAILED);
2309 ERRSTR(ERROR_LAYER_NOT_PRESENT);
2310 ERRSTR(ERROR_EXTENSION_NOT_PRESENT);
2311 ERRSTR(ERROR_FEATURE_NOT_PRESENT);
2312 ERRSTR(ERROR_INCOMPATIBLE_DRIVER);
2313 ERRSTR(ERROR_TOO_MANY_OBJECTS);
2314 ERRSTR(ERROR_FORMAT_NOT_SUPPORTED);
2315 ERRSTR(ERROR_SURFACE_LOST_KHR);
2316 ERRSTR(ERROR_NATIVE_WINDOW_IN_USE_KHR);
2318 ERRSTR(ERROR_OUT_OF_DATE_KHR);
2319 ERRSTR(ERROR_INCOMPATIBLE_DISPLAY_KHR);
2320 ERRSTR(ERROR_VALIDATION_FAILED_EXT);
2321 ERRSTR(ERROR_INVALID_SHADER_NV);
2322 default:
return "<unknown>";
2325 return "UNKNOWN ERROR";
2339 newtime = localtime(&aclock);