Advertisement
Redee

Untitled

Oct 3rd, 2020
1,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.19 KB | None | 0 0
  1. //ы
  2. #include "_headers.h"
  3.  
  4. //INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
  5. //INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd)
  6. //int main(int argc, char* argv[])
  7.  
  8.  
  9.  
  10. void Run()
  11. {
  12.     VkResult res;
  13.     const char* func;
  14.  
  15.     //
  16.  
  17.     uint32_t inst_layer_count{};
  18.     res = vkEnumerateInstanceLayerProperties(&inst_layer_count, nullptr);
  19.     func = "vk Enumerate Instance Layer Properties";
  20.     Print_Result_Num(func, res, inst_layer_count);
  21.  
  22.     if (!inst_layer_count)
  23.         throw std::runtime_error("No Instance Layers !!");
  24.  
  25.     vector<VkLayerProperties> inst_available_layers(inst_layer_count);
  26.     vkEnumerateInstanceLayerProperties(&inst_layer_count, inst_available_layers.data());
  27.     Debug_Available_Arr_Vk_Struct(inst_available_layers, &VkLayerProperties::layerName);
  28.  
  29.     const char* validation_layers[]{ "VK_LAYER_KHRONOS_validation" };
  30.     Debug_Need_Available_Arr_Vk_Struct("Layer", validation_layers, inst_available_layers, &VkLayerProperties::layerName);
  31.     const int validation_layers_count = (int)size(validation_layers);
  32.  
  33.     //
  34.  
  35.     uint32_t inst_ext_count{};
  36.     res = vkEnumerateInstanceExtensionProperties(nullptr, &inst_ext_count, nullptr);
  37.     func = "vk Enumerate Instance Extension Properties";
  38.     Print_Result_Num(func, res, inst_ext_count);
  39.  
  40.     if (!inst_ext_count)
  41.         throw std::runtime_error("No Instance Extensions !!");
  42.  
  43.     vector<VkExtensionProperties> available_extensions(inst_ext_count);
  44.     vkEnumerateInstanceExtensionProperties(nullptr, &inst_ext_count, available_extensions.data());
  45.     Debug_Available_Arr_Vk_Struct(available_extensions, &VkExtensionProperties::extensionName);
  46.  
  47.     const char* extensions[]{
  48.         VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
  49.         "VK_KHR_surface", "VK_KHR_win32_surface" };
  50.     Debug_Need_Available_Arr_Vk_Struct("Extension", extensions, available_extensions, &VkExtensionProperties::extensionName);
  51.  
  52.     //
  53.  
  54.     VkApplicationInfo app_inf{};
  55.     app_inf.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  56.     app_inf.apiVersion = VK_MAKE_VERSION(1, 2, 131);
  57.  
  58.     VkInstanceCreateInfo inf{};
  59.     inf.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  60.     inf.pApplicationInfo = &app_inf;
  61.     inf.enabledLayerCount = (uint32_t)size(validation_layers);
  62.     inf.ppEnabledLayerNames = validation_layers;
  63.     inf.enabledExtensionCount = (uint32_t)size(extensions);
  64.     inf.ppEnabledExtensionNames = extensions;
  65.  
  66.     VkDebugUtilsMessengerCreateInfoEXT debug_create_info = {};
  67.     debug_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  68.     debug_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
  69.         VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
  70.     debug_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
  71.         VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  72.     debug_create_info.pfnUserCallback = Debug_Callback;
  73.  
  74.     inf.pNext = (VkDebugUtilsMessengerCreateInfoEXT*)&debug_create_info;
  75.  
  76.     VkInstance inst;
  77.     res = vkCreateInstance(&inf, 0, &inst);
  78.     func = "vk Create Instance";
  79.     Print_Result(func, res);
  80.  
  81.     VkDebugUtilsMessengerEXT debug_messenger;
  82.     res = Create_Debug_Utils_Messenger_EXT(inst, &debug_create_info, nullptr, &debug_messenger);
  83.     func = "Create Debug Utils Messenger EXT";
  84.     Print_Result(func, res);
  85.  
  86.     uint32_t dev_count{};
  87.     vkEnumeratePhysicalDevices(inst, &dev_count, nullptr);
  88.     func = "vk Enumerate Physical Devices";
  89.     Print_Result_Num(func, res, dev_count);
  90.  
  91.     if (!dev_count)
  92.         throw std::runtime_error("Failed to find GPUs with Vulkan support !!");
  93.  
  94.     vector<VkPhysicalDevice> devs(dev_count);
  95.     vector<VkPhysicalDeviceProperties> devs_prop(dev_count);
  96.     vector<VkPhysicalDeviceFeatures> devs_feat(dev_count);
  97.     vector<int> devs_rate(dev_count, 0);
  98.  
  99.     vkEnumeratePhysicalDevices(inst, &dev_count, devs.data());
  100.     Get_Properties_Features_FromPhysDevs(devs, devs_prop, devs_feat);
  101.  
  102.     Rate_Physical_Devices(devs, devs_prop, devs_feat, devs_rate);
  103.     int best_gpu_index = Best_Vulkan_GPU(devs_prop, devs_rate);
  104.  
  105.     VkPhysicalDevice dev = devs[best_gpu_index];
  106.     VkPhysicalDeviceProperties dev_prop = devs_prop[best_gpu_index];
  107.     VkPhysicalDeviceFeatures dev_feat = devs_feat[best_gpu_index];
  108.  
  109.     uint32_t queue_families_count{};
  110.     vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_families_count, nullptr);
  111.  
  112.     if (!queue_families_count)
  113.         throw std::runtime_error("No Families Count !!");
  114.  
  115.     if (MY_DEBUG)
  116.         std::cout << "Queue Families: " << queue_families_count << std::endl;
  117.  
  118.     vector<VkQueueFamilyProperties> queue_families(queue_families_count);
  119.     vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_families_count, queue_families.data());
  120.  
  121.     //
  122.  
  123.     int q_num{};
  124.     bool q_num_found{};
  125.     for (const auto& queue_family : queue_families)
  126.     {
  127.         if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
  128.         {
  129.             q_num_found = 1;
  130.             break;
  131.         }
  132.         q_num++;
  133.     }
  134.  
  135.     if (!q_num_found)
  136.         throw std::runtime_error("No found in queue_families - VK_QUEUE_GRAPHICS_BIT");
  137.  
  138.     if (MY_DEBUG)
  139.         std::cout << "Needed - Queue Family - found !! And index = "
  140.         << q_num << std::endl << std::endl;
  141.  
  142.  
  143.     //
  144.  
  145.     float queue_priority = 1.0f;
  146.  
  147.     VkDeviceQueueCreateInfo queue_create_info =
  148.         Get_VkDeviceQueueCreateInfo(q_num, queue_families_count, &queue_priority);
  149.  
  150.     //
  151.  
  152.     uint32_t dev_ext_count{};
  153.     res = vkEnumerateDeviceExtensionProperties(dev, nullptr, &dev_ext_count, nullptr);
  154.     func = "vk Enumerate Device Extension Properties";
  155.     Print_Result_Num(func, res, dev_ext_count);
  156.  
  157.     if (!dev_ext_count)
  158.         throw std::runtime_error("No Device Extensions !!");
  159.  
  160.     vector<VkExtensionProperties> available_dev_extensions(dev_ext_count);
  161.     vkEnumerateDeviceExtensionProperties(dev, nullptr, &dev_ext_count, available_dev_extensions.data());
  162.     Debug_Available_Arr_Vk_Struct(available_dev_extensions, &VkExtensionProperties::extensionName);
  163.  
  164.     const char* dev_extensions[]{ VK_KHR_SWAPCHAIN_EXTENSION_NAME };
  165.     Debug_Need_Available_Arr_Vk_Struct("Extension", dev_extensions,
  166.         available_dev_extensions, &VkExtensionProperties::extensionName);
  167.  
  168.     //
  169.  
  170.     VkDeviceCreateInfo create_info{};
  171.     create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  172.     create_info.pQueueCreateInfos = &queue_create_info;
  173.     create_info.queueCreateInfoCount = 1;
  174.     create_info.pEnabledFeatures = &dev_feat;
  175.  
  176.     create_info.enabledExtensionCount = (int)size(dev_extensions);
  177.     create_info.ppEnabledExtensionNames = dev_extensions;
  178.  
  179.     if (MY_DEBUG)
  180.     {
  181.         create_info.enabledLayerCount = validation_layers_count;
  182.         create_info.ppEnabledLayerNames = validation_layers;
  183.     }
  184.  
  185.     VkDevice device{};
  186.     res = vkCreateDevice(dev, &create_info, nullptr, &device);
  187.     func = "vk Create Device";
  188.     Print_Result(func, res);
  189.  
  190.     //
  191.  
  192.     std::cout << std::setprecision(50);
  193.  
  194.     Input inp;
  195.     Input_Exit ie;
  196.     ie.keys = inp.keys;
  197.     ie.Exit_Combo_Add(Input_Combo(VK_CONTROL, 'D'));
  198.     ie.Exit_Combo_Add(Input_Combo(VK_ESCAPE));
  199.  
  200.     Controller_Screen cs;
  201.     cs.Detect();
  202.  
  203.     Time tme;
  204.     __int64 fr_tme_microsec{};
  205.     int fr_tme_millisec{};
  206.     float fr_tme_sec{};
  207.  
  208.     Controller_Windows cw;
  209.     pt_Contr_Wnds = &cw;
  210.     pt_Contr_Scr = &cs;
  211.     cw.p_m_Wheel = &inp.m_Wheel;
  212.  
  213.     Window& win = cw.Create("My11", 800, 600, WS_OVERLAPPEDWINDOW, GetModuleHandle(0), false, true);
  214.  
  215.     //
  216.  
  217.     VkSurfaceKHR surface{};
  218.     VkWin32SurfaceCreateInfoKHR s_create_info{};
  219.     s_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
  220.     s_create_info.hwnd = win.hWnd;
  221.     s_create_info.hinstance = win.hInst;
  222.  
  223.     res = vkCreateWin32SurfaceKHR(inst, &s_create_info, nullptr, &surface);
  224.     func = "vk Create Win32 Surface KHR";
  225.     Print_Result(func, res);
  226.  
  227.     VkBool32 surface_support{};
  228.     res = vkGetPhysicalDeviceSurfaceSupportKHR(dev, q_num, surface, &surface_support);
  229.     func = "vk Get Physical Device Surface Support KHR";
  230.     Print_Result(func, res);
  231.  
  232.     if (!surface_support)
  233.         throw std::runtime_error("No surface support !!");
  234.  
  235.     VkQueue presentQueue{};
  236.     vkGetDeviceQueue(device, q_num, 0, &presentQueue);
  237.  
  238.     //
  239.  
  240.     VkSurfaceCapabilitiesKHR capabilities;
  241.     res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(dev, surface, &capabilities);
  242.     func = "vk Get Physical Device Surface Capabilities KHR";
  243.     Print_Result(func, res);
  244.  
  245.     if (MY_DEBUG)
  246.         Print_VkSurfaceCapabilitiesKHR(capabilities);
  247.  
  248.     VkExtent2D swapChainExtent = capabilities.currentExtent;
  249.     uint32_t image_count = capabilities.minImageCount + 1;
  250.  
  251.     //
  252.  
  253.     uint32_t format_count{};
  254.     res = vkGetPhysicalDeviceSurfaceFormatsKHR(dev, surface, &format_count, nullptr);
  255.     func = "vk Get Physical Device Surface Formats KHR";
  256.     Print_Result_Num(func, res, format_count);
  257.  
  258.     if (!format_count)
  259.         throw std::runtime_error("No Surface Formats !!");
  260.  
  261.     vector<VkSurfaceFormatKHR> formats(format_count);
  262.     vkGetPhysicalDeviceSurfaceFormatsKHR(dev, surface, &format_count, formats.data());
  263.  
  264.     if (MY_DEBUG)
  265.         Print_VkSurfaceFormatsKHR(formats);
  266.  
  267.     int fr{};
  268.     for (auto& f : formats)
  269.     {
  270.         if (f.format == VK_FORMAT_B8G8R8A8_SRGB)
  271.             break;
  272.         fr++;
  273.     }
  274.     VkSurfaceFormatKHR surf_format = formats[fr];
  275.     VkFormat format = surf_format.format;
  276.     VkColorSpaceKHR col_space = surf_format.colorSpace;
  277.  
  278.     //
  279.  
  280.     uint32_t present_mode_count;
  281.     res = vkGetPhysicalDeviceSurfacePresentModesKHR(dev, surface, &present_mode_count, nullptr);
  282.     func = "vk Get Physical Device Surface Present Modes KHR";
  283.     Print_Result_Num(func, res, present_mode_count);
  284.  
  285.     if (!present_mode_count)
  286.         throw std::runtime_error("No Surface Present Modes !!");
  287.  
  288.     vector<VkPresentModeKHR> present_modes(present_mode_count);
  289.     vkGetPhysicalDeviceSurfacePresentModesKHR(dev, surface, &present_mode_count, present_modes.data());
  290.  
  291.     if (MY_DEBUG)
  292.         Print_VkPresentModesKHR(present_modes);
  293.  
  294.     int pr{};
  295.     for (auto& p : present_modes)
  296.     {
  297.         if (p == VK_PRESENT_MODE_FIFO_KHR)
  298.             break;
  299.         pr++;
  300.     }
  301.     VkPresentModeKHR present_mode = present_modes[pr];
  302.  
  303.     //
  304.  
  305.     VkSwapchainCreateInfoKHR sw_create_info{};
  306.     sw_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  307.     sw_create_info.surface = surface;
  308.     sw_create_info.minImageCount = image_count;
  309.     sw_create_info.imageFormat = format;
  310.     sw_create_info.imageColorSpace = col_space;
  311.     sw_create_info.imageExtent = swapChainExtent;
  312.     sw_create_info.imageArrayLayers = 1;
  313.     sw_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  314.  
  315.     sw_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  316.  
  317.     sw_create_info.preTransform = capabilities.currentTransform;
  318.     sw_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  319.  
  320.     sw_create_info.presentMode = present_mode;
  321.     sw_create_info.clipped = VK_TRUE;
  322.  
  323.     VkSwapchainKHR swapChain;
  324.     res = vkCreateSwapchainKHR(device, &sw_create_info, nullptr, &swapChain);
  325.     func = "vk Create Swapchain KHR";
  326.     Print_Result(func, res);
  327.  
  328.     //
  329.  
  330.     res = vkGetSwapchainImagesKHR(device, swapChain, &image_count, nullptr);
  331.     func = "vk Get Swapchain Images KHR";
  332.     Print_Result(func, res);
  333.     vector<VkImage> swap_сhain_images(image_count, nullptr);
  334.     vkGetSwapchainImagesKHR(device, swapChain, &image_count, swap_сhain_images.data());
  335.  
  336.     //
  337.  
  338.     // далее понадобится
  339.     // swapChain, swap_chain_images, format, swapChainExtent
  340.  
  341.     //
  342.  
  343.     vector<VkImageView> swap_chain_image_views(image_count, nullptr);
  344.  
  345.     for (auto& swch_imv : swap_chain_image_views)
  346.     {
  347.         static int n{};
  348.  
  349.         VkImageViewCreateInfo create_info{};
  350.         create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  351.         create_info.image = swap_сhain_images[n];
  352.  
  353.         create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  354.         create_info.format = format;
  355.  
  356.         create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
  357.         create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
  358.         create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
  359.         create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
  360.  
  361.         create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  362.         create_info.subresourceRange.baseMipLevel = 0;
  363.         create_info.subresourceRange.levelCount = 1;
  364.         create_info.subresourceRange.baseArrayLayer = 0;
  365.         create_info.subresourceRange.layerCount = 1;
  366.  
  367.         res = vkCreateImageView(device, &create_info, nullptr, &swch_imv);
  368.         func = "vk Create Image View";
  369.         Print_Result(func, res);
  370.  
  371.         n++;
  372.     }
  373.  
  374.     //
  375.  
  376.     vector<char> vert_shader_code = readFile("../../src/sh_v.spv");
  377.     vector<char> frag_shader_code = readFile("../../src/sh_f.spv");
  378.  
  379.     VkShaderModule vert_shader_module = createShaderModule(device, vert_shader_code);
  380.     VkShaderModule frag_shader_module = createShaderModule(device, frag_shader_code);
  381.  
  382.     VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
  383.     vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  384.     vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
  385.     vertShaderStageInfo.module = vert_shader_module;
  386.     vertShaderStageInfo.pName = "main";
  387.  
  388.     VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
  389.     fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  390.     fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  391.     fragShaderStageInfo.module = frag_shader_module;
  392.     fragShaderStageInfo.pName = "main";
  393.  
  394.     VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
  395.  
  396.     //
  397.  
  398.     VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
  399.     vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  400.     vertexInputInfo.vertexBindingDescriptionCount = 0;
  401.     vertexInputInfo.pVertexBindingDescriptions = nullptr; // Optional
  402.     vertexInputInfo.vertexAttributeDescriptionCount = 0;
  403.     vertexInputInfo.pVertexAttributeDescriptions = nullptr; // Optional
  404.  
  405.     VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
  406.     inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  407.     inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  408.     inputAssembly.primitiveRestartEnable = VK_FALSE;
  409.  
  410.     VkViewport viewport{};
  411.     viewport.x = 0.0f;
  412.     viewport.y = 0.0f;
  413.     viewport.width = (float)swapChainExtent.width;
  414.     viewport.height = (float)swapChainExtent.height;
  415.     viewport.minDepth = 0.0f;
  416.     viewport.maxDepth = 1.0f;
  417.  
  418.     VkRect2D scissor{};
  419.     scissor.offset = { 0, 0 };
  420.     scissor.extent = swapChainExtent;
  421.  
  422.     VkPipelineViewportStateCreateInfo viewportState{};
  423.     viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  424.     viewportState.viewportCount = 1;
  425.     viewportState.pViewports = &viewport;
  426.     viewportState.scissorCount = 1;
  427.     viewportState.pScissors = &scissor;
  428.  
  429.     VkPipelineRasterizationStateCreateInfo rasterizer{};
  430.     rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  431.     rasterizer.depthClampEnable = VK_FALSE;
  432.     rasterizer.rasterizerDiscardEnable = VK_FALSE;
  433.     rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
  434.     rasterizer.lineWidth = 1.0f;
  435.     rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
  436.     rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
  437.     rasterizer.depthBiasEnable = VK_FALSE;
  438.     rasterizer.depthBiasConstantFactor = 0.0f; // Optional
  439.     rasterizer.depthBiasClamp = 0.0f; // Optional
  440.     rasterizer.depthBiasSlopeFactor = 0.0f; // Optional
  441.  
  442.     VkPipelineMultisampleStateCreateInfo multisampling{};
  443.     multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  444.     multisampling.sampleShadingEnable = VK_FALSE;
  445.     multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  446.     multisampling.minSampleShading = 1.0f; // Optional
  447.     multisampling.pSampleMask = nullptr; // Optional
  448.     multisampling.alphaToCoverageEnable = VK_FALSE; // Optional
  449.     multisampling.alphaToOneEnable = VK_FALSE; // Optional
  450.  
  451.     VkPipelineColorBlendAttachmentState colorBlendAttachment{};
  452.     colorBlendAttachment.colorWriteMask =
  453.         VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  454.     colorBlendAttachment.blendEnable = VK_FALSE;
  455.     colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
  456.     colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
  457.     colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; // Optional
  458.     colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
  459.     colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
  460.     colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; // Optional
  461.  
  462.     VkPipelineColorBlendStateCreateInfo colorBlending{};
  463.     colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  464.     colorBlending.logicOpEnable = VK_FALSE;
  465.     colorBlending.logicOp = VK_LOGIC_OP_COPY; // Optional
  466.     colorBlending.attachmentCount = 1;
  467.     colorBlending.pAttachments = &colorBlendAttachment;
  468.     colorBlending.blendConstants[0] = 0.0f; // Optional
  469.     colorBlending.blendConstants[1] = 0.0f; // Optional
  470.     colorBlending.blendConstants[2] = 0.0f; // Optional
  471.     colorBlending.blendConstants[3] = 0.0f; // Optional
  472.  
  473.     VkDynamicState dynamicStates[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_LINE_WIDTH };
  474.  
  475.     VkPipelineDynamicStateCreateInfo dynamicState{};
  476.     dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  477.     dynamicState.dynamicStateCount = 2;
  478.     dynamicState.pDynamicStates = dynamicStates;
  479.  
  480.     VkPipelineLayout pipelineLayout;
  481.  
  482.     VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
  483.     pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  484.     pipelineLayoutInfo.setLayoutCount = 0; // Optional
  485.     pipelineLayoutInfo.pSetLayouts = nullptr; // Optional
  486.     pipelineLayoutInfo.pushConstantRangeCount = 0; // Optional
  487.     pipelineLayoutInfo.pPushConstantRanges = nullptr; // Optional
  488.  
  489.     res = vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout);
  490.     func = "vk Create Pipeline Layout";
  491.     Print_Result(func, res);
  492.  
  493.     //
  494.  
  495.     VkAttachmentDescription colorAttachment{};
  496.     colorAttachment.format = format;
  497.     colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
  498.     colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
  499.     colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  500.     colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  501.     colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  502.     colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  503.     colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
  504.  
  505.     VkAttachmentReference colorAttachmentRef{};
  506.     colorAttachmentRef.attachment = 0;
  507.     colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  508.  
  509.     VkSubpassDescription subpass{};
  510.     subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  511.     subpass.colorAttachmentCount = 1;
  512.     subpass.pColorAttachments = &colorAttachmentRef;
  513.  
  514.     VkRenderPass renderPass;
  515.  
  516.     VkRenderPassCreateInfo renderPassInfo{};
  517.     renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  518.     renderPassInfo.attachmentCount = 1;
  519.     renderPassInfo.pAttachments = &colorAttachment;
  520.     renderPassInfo.subpassCount = 1;
  521.     renderPassInfo.pSubpasses = &subpass;
  522.  
  523.     res = vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass);
  524.     func = "vk Create Render Pass";
  525.     Print_Result(func, res);
  526.  
  527.     //
  528.  
  529.     VkGraphicsPipelineCreateInfo pipelineInfo{};
  530.     pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  531.     pipelineInfo.stageCount = 2;
  532.     pipelineInfo.pStages = shaderStages;
  533.  
  534.     pipelineInfo.pVertexInputState = &vertexInputInfo;
  535.     pipelineInfo.pInputAssemblyState = &inputAssembly;
  536.     pipelineInfo.pViewportState = &viewportState;
  537.     pipelineInfo.pRasterizationState = &rasterizer;
  538.     pipelineInfo.pMultisampleState = &multisampling;
  539.     pipelineInfo.pDepthStencilState = nullptr; // Optional
  540.     pipelineInfo.pColorBlendState = &colorBlending;
  541.     pipelineInfo.pDynamicState = nullptr; // Optional
  542.  
  543.     pipelineInfo.layout = pipelineLayout;
  544.     pipelineInfo.renderPass = renderPass;
  545.     pipelineInfo.subpass = 0;
  546.     pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // Optional
  547.     pipelineInfo.basePipelineIndex = -1; // Optional
  548.  
  549.     VkPipeline graphicsPipeline;
  550.     res = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline);
  551.     func = "vk Create Graphics Pipelines";
  552.     Print_Result(func, res);
  553.  
  554.     //
  555.  
  556.     vector<VkFramebuffer> swapChainFramebuffers;
  557.     swapChainFramebuffers.resize(swap_chain_image_views.size());
  558.  
  559.     for (auto& swch_frb : swapChainFramebuffers)
  560.     {
  561.         static int n{};
  562.  
  563.         VkImageView attachments[] = {
  564.             swap_chain_image_views[n]
  565.         };
  566.  
  567.         VkFramebufferCreateInfo framebufferInfo{};
  568.         framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  569.         framebufferInfo.renderPass = renderPass;
  570.         framebufferInfo.attachmentCount = 1;
  571.         framebufferInfo.pAttachments = attachments;
  572.         framebufferInfo.width = swapChainExtent.width;
  573.         framebufferInfo.height = swapChainExtent.height;
  574.         framebufferInfo.layers = 1;
  575.  
  576.         res = vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swch_frb);
  577.         func = "vk Create Framebuffer";
  578.         Print_Result(func, res);
  579.  
  580.         n++;
  581.     }
  582.  
  583.     //
  584.  
  585.     VkCommandPool commandPool;
  586.  
  587.     VkCommandPoolCreateInfo poolInfo{};
  588.     poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  589.     poolInfo.queueFamilyIndex = q_num;
  590.     poolInfo.flags = 0; // Optional
  591.  
  592.     res = vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool);
  593.     func = "vk Create Command Pool";
  594.     Print_Result(func, res);
  595.  
  596.     vector<VkCommandBuffer> commandBuffers;
  597.     commandBuffers.resize(swapChainFramebuffers.size());
  598.  
  599.     VkCommandBufferAllocateInfo allocInfo{};
  600.     allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  601.     allocInfo.commandPool = commandPool;
  602.     allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  603.     allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
  604.  
  605.     res = vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data());
  606.     func = "vk Allocate Command Buffers";
  607.     Print_Result(func, res);
  608.  
  609.     for (auto& cbuf : commandBuffers)
  610.     {
  611.         VkCommandBufferBeginInfo beginInfo{};
  612.         beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  613.         beginInfo.flags = 0; // Optional
  614.         beginInfo.pInheritanceInfo = nullptr; // Optional
  615.  
  616.         res = vkBeginCommandBuffer(cbuf, &beginInfo);
  617.         func = "vk Begin Command Buffer";
  618.         Print_Result(func, res);
  619.     }
  620.  
  621.     VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
  622.     vector<VkRenderPassBeginInfo> renderPassBeginInfos(commandBuffers.size(), VkRenderPassBeginInfo{});
  623.     for (auto& rp_bi : renderPassBeginInfos)
  624.     {
  625.         static int n{};
  626.  
  627.         rp_bi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  628.         rp_bi.renderPass = renderPass;
  629.         rp_bi.framebuffer = swapChainFramebuffers[n];
  630.  
  631.         rp_bi.renderArea.offset = { 0, 0 };
  632.         rp_bi.renderArea.extent = swapChainExtent;
  633.  
  634.         rp_bi.clearValueCount = 1;
  635.         rp_bi.pClearValues = &clearColor;
  636.  
  637.         vkCmdBeginRenderPass(commandBuffers[n], &rp_bi, VK_SUBPASS_CONTENTS_INLINE);
  638.  
  639.         vkCmdBindPipeline(commandBuffers[n], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
  640.  
  641.         vkCmdDraw(commandBuffers[n], 3, 1, 0, 0);
  642.  
  643.         vkCmdEndRenderPass(commandBuffers[n]);
  644.  
  645.         res = vkEndCommandBuffer(commandBuffers[n]);
  646.         func = "vk End Command Buffer";
  647.         Print_Result(func, res);
  648.  
  649.         n++;
  650.     }
  651.  
  652.     //
  653.  
  654.     VkSemaphoreCreateInfo semaphoreInfo{};
  655.     semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  656.     VkSemaphore imageAvailableSemaphore, renderFinishedSemaphore;
  657.  
  658.     func = "vk Create Semaphore";
  659.     res = vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphore);
  660.     Print_Result(func, res);
  661.     res = vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphore);
  662.     Print_Result(func, res);
  663.  
  664.     //
  665.  
  666.     VkQueue graphicsQueue;
  667.     vkGetDeviceQueue(device, 0, 0, &graphicsQueue);
  668.  
  669.     VkSubpassDependency dependency{};
  670.     dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
  671.     dependency.dstSubpass = 0;
  672.     dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  673.     dependency.srcAccessMask = 0;
  674.     dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  675.     dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  676.     renderPassInfo.dependencyCount = 1;
  677.     renderPassInfo.pDependencies = &dependency;
  678.  
  679.     //
  680.  
  681.     tme.Refresh();
  682.     while (1)
  683.     {
  684.         tme.Measure_Refresh();
  685.         Time_Tools::Set_Frame(tme, fr_tme_microsec, fr_tme_millisec, fr_tme_sec);
  686.  
  687.         inp.Parse();
  688.         if (ie.Exit_Combo_Parse())
  689.             cw.Destroy_Active_Window();
  690.  
  691.         cw.Parse_Msgs();
  692.         if (cw.exit) break;
  693.  
  694.         //
  695.         // draw frame
  696.  
  697.         uint32_t imageIndex;
  698.         func = "vk Acquire Next Image KHR";
  699.         res = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
  700.         if (res)
  701.             Print_Result(func, res);
  702.  
  703.         VkSubmitInfo submitInfo{};
  704.         submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  705.  
  706.         VkSemaphore waitSemaphores[] = { imageAvailableSemaphore };
  707.         VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
  708.         submitInfo.waitSemaphoreCount = 1;
  709.         submitInfo.pWaitSemaphores = waitSemaphores;
  710.         submitInfo.pWaitDstStageMask = waitStages;
  711.  
  712.         submitInfo.commandBufferCount = 1;
  713.         submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
  714.  
  715.         VkSemaphore signalSemaphores[] = { renderFinishedSemaphore };
  716.         submitInfo.signalSemaphoreCount = 1;
  717.         submitInfo.pSignalSemaphores = signalSemaphores;
  718.  
  719.         func = "vk Queue Submit";
  720.         res = vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
  721.         if (res)
  722.             Print_Result(func, res);
  723.  
  724.         VkPresentInfoKHR presentInfo{};
  725.         presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  726.  
  727.         presentInfo.waitSemaphoreCount = 1;
  728.         presentInfo.pWaitSemaphores = signalSemaphores;
  729.  
  730.         VkSwapchainKHR swapChains[] = { swapChain };
  731.         presentInfo.swapchainCount = 1;
  732.         presentInfo.pSwapchains = swapChains;
  733.         presentInfo.pImageIndices = &imageIndex;
  734.         presentInfo.pResults = nullptr; // Optional
  735.         vkQueuePresentKHR(presentQueue, &presentInfo);
  736.  
  737.         //
  738.  
  739.         //render.Display();
  740.     }
  741.  
  742.     //
  743.  
  744.     vkDestroySemaphore(device, renderFinishedSemaphore, nullptr);
  745.     vkDestroySemaphore(device, imageAvailableSemaphore, nullptr);
  746.  
  747.     vkDestroyCommandPool(device, commandPool, nullptr);
  748.  
  749.     for (auto& framebuffer : swapChainFramebuffers)
  750.         vkDestroyFramebuffer(device, framebuffer, nullptr);
  751.  
  752.     vkDestroyPipeline(device, graphicsPipeline, nullptr);
  753.  
  754.     vkDestroyRenderPass(device, renderPass, nullptr);
  755.     vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
  756.  
  757.     vkDestroyShaderModule(device, frag_shader_module, nullptr);
  758.     vkDestroyShaderModule(device, vert_shader_module, nullptr);
  759.  
  760.     for (auto& swch_imv : swap_chain_image_views)
  761.         vkDestroyImageView(device, swch_imv, nullptr);
  762.  
  763.     vkDestroySwapchainKHR(device, swapChain, nullptr);
  764.     vkDestroySurfaceKHR(inst, surface, nullptr);
  765.     vkDestroyDevice(device, nullptr);
  766.     Destroy_Debug_Utils_Messenger_EXT(inst, debug_messenger, nullptr);
  767.     vkDestroyInstance(inst, nullptr);
  768. }
  769.  
  770. int main()
  771. {
  772.     try { Run(); }
  773.     catch (const std::exception & e)
  774.     {
  775.         std::cout << e.what() << std::endl;
  776.         return 1;
  777.     }
  778.  
  779.     //
  780.  
  781.     return 0;
  782. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement