Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ы
- #include "_headers.h"
- //INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
- //INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd)
- //int main(int argc, char* argv[])
- void Run()
- {
- VkResult res;
- const char* func;
- //
- uint32_t inst_layer_count{};
- res = vkEnumerateInstanceLayerProperties(&inst_layer_count, nullptr);
- func = "vk Enumerate Instance Layer Properties";
- Print_Result_Num(func, res, inst_layer_count);
- if (!inst_layer_count)
- throw std::runtime_error("No Instance Layers !!");
- vector<VkLayerProperties> inst_available_layers(inst_layer_count);
- vkEnumerateInstanceLayerProperties(&inst_layer_count, inst_available_layers.data());
- Debug_Available_Arr_Vk_Struct(inst_available_layers, &VkLayerProperties::layerName);
- const char* validation_layers[]{ "VK_LAYER_KHRONOS_validation" };
- Debug_Need_Available_Arr_Vk_Struct("Layer", validation_layers, inst_available_layers, &VkLayerProperties::layerName);
- const int validation_layers_count = (int)size(validation_layers);
- //
- uint32_t inst_ext_count{};
- res = vkEnumerateInstanceExtensionProperties(nullptr, &inst_ext_count, nullptr);
- func = "vk Enumerate Instance Extension Properties";
- Print_Result_Num(func, res, inst_ext_count);
- if (!inst_ext_count)
- throw std::runtime_error("No Instance Extensions !!");
- vector<VkExtensionProperties> available_extensions(inst_ext_count);
- vkEnumerateInstanceExtensionProperties(nullptr, &inst_ext_count, available_extensions.data());
- Debug_Available_Arr_Vk_Struct(available_extensions, &VkExtensionProperties::extensionName);
- const char* extensions[]{
- VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
- "VK_KHR_surface", "VK_KHR_win32_surface" };
- Debug_Need_Available_Arr_Vk_Struct("Extension", extensions, available_extensions, &VkExtensionProperties::extensionName);
- //
- VkApplicationInfo app_inf{};
- app_inf.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
- app_inf.apiVersion = VK_MAKE_VERSION(1, 2, 131);
- VkInstanceCreateInfo inf{};
- inf.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
- inf.pApplicationInfo = &app_inf;
- inf.enabledLayerCount = (uint32_t)size(validation_layers);
- inf.ppEnabledLayerNames = validation_layers;
- inf.enabledExtensionCount = (uint32_t)size(extensions);
- inf.ppEnabledExtensionNames = extensions;
- VkDebugUtilsMessengerCreateInfoEXT debug_create_info = {};
- debug_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
- debug_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
- VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
- debug_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
- VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
- debug_create_info.pfnUserCallback = Debug_Callback;
- inf.pNext = (VkDebugUtilsMessengerCreateInfoEXT*)&debug_create_info;
- VkInstance inst;
- res = vkCreateInstance(&inf, 0, &inst);
- func = "vk Create Instance";
- Print_Result(func, res);
- VkDebugUtilsMessengerEXT debug_messenger;
- res = Create_Debug_Utils_Messenger_EXT(inst, &debug_create_info, nullptr, &debug_messenger);
- func = "Create Debug Utils Messenger EXT";
- Print_Result(func, res);
- uint32_t dev_count{};
- vkEnumeratePhysicalDevices(inst, &dev_count, nullptr);
- func = "vk Enumerate Physical Devices";
- Print_Result_Num(func, res, dev_count);
- if (!dev_count)
- throw std::runtime_error("Failed to find GPUs with Vulkan support !!");
- vector<VkPhysicalDevice> devs(dev_count);
- vector<VkPhysicalDeviceProperties> devs_prop(dev_count);
- vector<VkPhysicalDeviceFeatures> devs_feat(dev_count);
- vector<int> devs_rate(dev_count, 0);
- vkEnumeratePhysicalDevices(inst, &dev_count, devs.data());
- Get_Properties_Features_FromPhysDevs(devs, devs_prop, devs_feat);
- Rate_Physical_Devices(devs, devs_prop, devs_feat, devs_rate);
- int best_gpu_index = Best_Vulkan_GPU(devs_prop, devs_rate);
- VkPhysicalDevice dev = devs[best_gpu_index];
- VkPhysicalDeviceProperties dev_prop = devs_prop[best_gpu_index];
- VkPhysicalDeviceFeatures dev_feat = devs_feat[best_gpu_index];
- uint32_t queue_families_count{};
- vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_families_count, nullptr);
- if (!queue_families_count)
- throw std::runtime_error("No Families Count !!");
- if (MY_DEBUG)
- std::cout << "Queue Families: " << queue_families_count << std::endl;
- vector<VkQueueFamilyProperties> queue_families(queue_families_count);
- vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_families_count, queue_families.data());
- //
- int q_num{};
- bool q_num_found{};
- for (const auto& queue_family : queue_families)
- {
- if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
- {
- q_num_found = 1;
- break;
- }
- q_num++;
- }
- if (!q_num_found)
- throw std::runtime_error("No found in queue_families - VK_QUEUE_GRAPHICS_BIT");
- if (MY_DEBUG)
- std::cout << "Needed - Queue Family - found !! And index = "
- << q_num << std::endl << std::endl;
- //
- float queue_priority = 1.0f;
- VkDeviceQueueCreateInfo queue_create_info =
- Get_VkDeviceQueueCreateInfo(q_num, queue_families_count, &queue_priority);
- //
- uint32_t dev_ext_count{};
- res = vkEnumerateDeviceExtensionProperties(dev, nullptr, &dev_ext_count, nullptr);
- func = "vk Enumerate Device Extension Properties";
- Print_Result_Num(func, res, dev_ext_count);
- if (!dev_ext_count)
- throw std::runtime_error("No Device Extensions !!");
- vector<VkExtensionProperties> available_dev_extensions(dev_ext_count);
- vkEnumerateDeviceExtensionProperties(dev, nullptr, &dev_ext_count, available_dev_extensions.data());
- Debug_Available_Arr_Vk_Struct(available_dev_extensions, &VkExtensionProperties::extensionName);
- const char* dev_extensions[]{ VK_KHR_SWAPCHAIN_EXTENSION_NAME };
- Debug_Need_Available_Arr_Vk_Struct("Extension", dev_extensions,
- available_dev_extensions, &VkExtensionProperties::extensionName);
- //
- VkDeviceCreateInfo create_info{};
- create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
- create_info.pQueueCreateInfos = &queue_create_info;
- create_info.queueCreateInfoCount = 1;
- create_info.pEnabledFeatures = &dev_feat;
- create_info.enabledExtensionCount = (int)size(dev_extensions);
- create_info.ppEnabledExtensionNames = dev_extensions;
- if (MY_DEBUG)
- {
- create_info.enabledLayerCount = validation_layers_count;
- create_info.ppEnabledLayerNames = validation_layers;
- }
- VkDevice device{};
- res = vkCreateDevice(dev, &create_info, nullptr, &device);
- func = "vk Create Device";
- Print_Result(func, res);
- //
- std::cout << std::setprecision(50);
- Input inp;
- Input_Exit ie;
- ie.keys = inp.keys;
- ie.Exit_Combo_Add(Input_Combo(VK_CONTROL, 'D'));
- ie.Exit_Combo_Add(Input_Combo(VK_ESCAPE));
- Controller_Screen cs;
- cs.Detect();
- Time tme;
- __int64 fr_tme_microsec{};
- int fr_tme_millisec{};
- float fr_tme_sec{};
- Controller_Windows cw;
- pt_Contr_Wnds = &cw;
- pt_Contr_Scr = &cs;
- cw.p_m_Wheel = &inp.m_Wheel;
- Window& win = cw.Create("My11", 800, 600, WS_OVERLAPPEDWINDOW, GetModuleHandle(0), false, true);
- //
- VkSurfaceKHR surface{};
- VkWin32SurfaceCreateInfoKHR s_create_info{};
- s_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
- s_create_info.hwnd = win.hWnd;
- s_create_info.hinstance = win.hInst;
- res = vkCreateWin32SurfaceKHR(inst, &s_create_info, nullptr, &surface);
- func = "vk Create Win32 Surface KHR";
- Print_Result(func, res);
- VkBool32 surface_support{};
- res = vkGetPhysicalDeviceSurfaceSupportKHR(dev, q_num, surface, &surface_support);
- func = "vk Get Physical Device Surface Support KHR";
- Print_Result(func, res);
- if (!surface_support)
- throw std::runtime_error("No surface support !!");
- VkQueue presentQueue{};
- vkGetDeviceQueue(device, q_num, 0, &presentQueue);
- //
- VkSurfaceCapabilitiesKHR capabilities;
- res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(dev, surface, &capabilities);
- func = "vk Get Physical Device Surface Capabilities KHR";
- Print_Result(func, res);
- if (MY_DEBUG)
- Print_VkSurfaceCapabilitiesKHR(capabilities);
- VkExtent2D swapChainExtent = capabilities.currentExtent;
- uint32_t image_count = capabilities.minImageCount + 1;
- //
- uint32_t format_count{};
- res = vkGetPhysicalDeviceSurfaceFormatsKHR(dev, surface, &format_count, nullptr);
- func = "vk Get Physical Device Surface Formats KHR";
- Print_Result_Num(func, res, format_count);
- if (!format_count)
- throw std::runtime_error("No Surface Formats !!");
- vector<VkSurfaceFormatKHR> formats(format_count);
- vkGetPhysicalDeviceSurfaceFormatsKHR(dev, surface, &format_count, formats.data());
- if (MY_DEBUG)
- Print_VkSurfaceFormatsKHR(formats);
- int fr{};
- for (auto& f : formats)
- {
- if (f.format == VK_FORMAT_B8G8R8A8_SRGB)
- break;
- fr++;
- }
- VkSurfaceFormatKHR surf_format = formats[fr];
- VkFormat format = surf_format.format;
- VkColorSpaceKHR col_space = surf_format.colorSpace;
- //
- uint32_t present_mode_count;
- res = vkGetPhysicalDeviceSurfacePresentModesKHR(dev, surface, &present_mode_count, nullptr);
- func = "vk Get Physical Device Surface Present Modes KHR";
- Print_Result_Num(func, res, present_mode_count);
- if (!present_mode_count)
- throw std::runtime_error("No Surface Present Modes !!");
- vector<VkPresentModeKHR> present_modes(present_mode_count);
- vkGetPhysicalDeviceSurfacePresentModesKHR(dev, surface, &present_mode_count, present_modes.data());
- if (MY_DEBUG)
- Print_VkPresentModesKHR(present_modes);
- int pr{};
- for (auto& p : present_modes)
- {
- if (p == VK_PRESENT_MODE_FIFO_KHR)
- break;
- pr++;
- }
- VkPresentModeKHR present_mode = present_modes[pr];
- //
- VkSwapchainCreateInfoKHR sw_create_info{};
- sw_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
- sw_create_info.surface = surface;
- sw_create_info.minImageCount = image_count;
- sw_create_info.imageFormat = format;
- sw_create_info.imageColorSpace = col_space;
- sw_create_info.imageExtent = swapChainExtent;
- sw_create_info.imageArrayLayers = 1;
- sw_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
- sw_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
- sw_create_info.preTransform = capabilities.currentTransform;
- sw_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
- sw_create_info.presentMode = present_mode;
- sw_create_info.clipped = VK_TRUE;
- VkSwapchainKHR swapChain;
- res = vkCreateSwapchainKHR(device, &sw_create_info, nullptr, &swapChain);
- func = "vk Create Swapchain KHR";
- Print_Result(func, res);
- //
- res = vkGetSwapchainImagesKHR(device, swapChain, &image_count, nullptr);
- func = "vk Get Swapchain Images KHR";
- Print_Result(func, res);
- vector<VkImage> swap_сhain_images(image_count, nullptr);
- vkGetSwapchainImagesKHR(device, swapChain, &image_count, swap_сhain_images.data());
- //
- // далее понадобится
- // swapChain, swap_chain_images, format, swapChainExtent
- //
- vector<VkImageView> swap_chain_image_views(image_count, nullptr);
- for (auto& swch_imv : swap_chain_image_views)
- {
- static int n{};
- VkImageViewCreateInfo create_info{};
- create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
- create_info.image = swap_сhain_images[n];
- create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
- create_info.format = format;
- create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
- create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
- create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
- create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
- create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- create_info.subresourceRange.baseMipLevel = 0;
- create_info.subresourceRange.levelCount = 1;
- create_info.subresourceRange.baseArrayLayer = 0;
- create_info.subresourceRange.layerCount = 1;
- res = vkCreateImageView(device, &create_info, nullptr, &swch_imv);
- func = "vk Create Image View";
- Print_Result(func, res);
- n++;
- }
- //
- vector<char> vert_shader_code = readFile("../../src/sh_v.spv");
- vector<char> frag_shader_code = readFile("../../src/sh_f.spv");
- VkShaderModule vert_shader_module = createShaderModule(device, vert_shader_code);
- VkShaderModule frag_shader_module = createShaderModule(device, frag_shader_code);
- VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
- vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
- vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
- vertShaderStageInfo.module = vert_shader_module;
- vertShaderStageInfo.pName = "main";
- VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
- fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
- fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
- fragShaderStageInfo.module = frag_shader_module;
- fragShaderStageInfo.pName = "main";
- VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
- //
- VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
- vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
- vertexInputInfo.vertexBindingDescriptionCount = 0;
- vertexInputInfo.pVertexBindingDescriptions = nullptr; // Optional
- vertexInputInfo.vertexAttributeDescriptionCount = 0;
- vertexInputInfo.pVertexAttributeDescriptions = nullptr; // Optional
- VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
- inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
- inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
- inputAssembly.primitiveRestartEnable = VK_FALSE;
- VkViewport viewport{};
- viewport.x = 0.0f;
- viewport.y = 0.0f;
- viewport.width = (float)swapChainExtent.width;
- viewport.height = (float)swapChainExtent.height;
- viewport.minDepth = 0.0f;
- viewport.maxDepth = 1.0f;
- VkRect2D scissor{};
- scissor.offset = { 0, 0 };
- scissor.extent = swapChainExtent;
- VkPipelineViewportStateCreateInfo viewportState{};
- viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
- viewportState.viewportCount = 1;
- viewportState.pViewports = &viewport;
- viewportState.scissorCount = 1;
- viewportState.pScissors = &scissor;
- VkPipelineRasterizationStateCreateInfo rasterizer{};
- rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
- rasterizer.depthClampEnable = VK_FALSE;
- rasterizer.rasterizerDiscardEnable = VK_FALSE;
- rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
- rasterizer.lineWidth = 1.0f;
- rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
- rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
- rasterizer.depthBiasEnable = VK_FALSE;
- rasterizer.depthBiasConstantFactor = 0.0f; // Optional
- rasterizer.depthBiasClamp = 0.0f; // Optional
- rasterizer.depthBiasSlopeFactor = 0.0f; // Optional
- VkPipelineMultisampleStateCreateInfo multisampling{};
- multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
- multisampling.sampleShadingEnable = VK_FALSE;
- multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
- multisampling.minSampleShading = 1.0f; // Optional
- multisampling.pSampleMask = nullptr; // Optional
- multisampling.alphaToCoverageEnable = VK_FALSE; // Optional
- multisampling.alphaToOneEnable = VK_FALSE; // Optional
- VkPipelineColorBlendAttachmentState colorBlendAttachment{};
- colorBlendAttachment.colorWriteMask =
- VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
- colorBlendAttachment.blendEnable = VK_FALSE;
- colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
- colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
- colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; // Optional
- colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
- colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
- colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; // Optional
- VkPipelineColorBlendStateCreateInfo colorBlending{};
- colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
- colorBlending.logicOpEnable = VK_FALSE;
- colorBlending.logicOp = VK_LOGIC_OP_COPY; // Optional
- colorBlending.attachmentCount = 1;
- colorBlending.pAttachments = &colorBlendAttachment;
- colorBlending.blendConstants[0] = 0.0f; // Optional
- colorBlending.blendConstants[1] = 0.0f; // Optional
- colorBlending.blendConstants[2] = 0.0f; // Optional
- colorBlending.blendConstants[3] = 0.0f; // Optional
- VkDynamicState dynamicStates[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_LINE_WIDTH };
- VkPipelineDynamicStateCreateInfo dynamicState{};
- dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
- dynamicState.dynamicStateCount = 2;
- dynamicState.pDynamicStates = dynamicStates;
- VkPipelineLayout pipelineLayout;
- VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
- pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
- pipelineLayoutInfo.setLayoutCount = 0; // Optional
- pipelineLayoutInfo.pSetLayouts = nullptr; // Optional
- pipelineLayoutInfo.pushConstantRangeCount = 0; // Optional
- pipelineLayoutInfo.pPushConstantRanges = nullptr; // Optional
- res = vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout);
- func = "vk Create Pipeline Layout";
- Print_Result(func, res);
- //
- VkAttachmentDescription colorAttachment{};
- colorAttachment.format = format;
- colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
- colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
- colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
- colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
- colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
- colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
- colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
- VkAttachmentReference colorAttachmentRef{};
- colorAttachmentRef.attachment = 0;
- colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
- VkSubpassDescription subpass{};
- subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
- subpass.colorAttachmentCount = 1;
- subpass.pColorAttachments = &colorAttachmentRef;
- VkRenderPass renderPass;
- VkRenderPassCreateInfo renderPassInfo{};
- renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
- renderPassInfo.attachmentCount = 1;
- renderPassInfo.pAttachments = &colorAttachment;
- renderPassInfo.subpassCount = 1;
- renderPassInfo.pSubpasses = &subpass;
- res = vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass);
- func = "vk Create Render Pass";
- Print_Result(func, res);
- //
- VkGraphicsPipelineCreateInfo pipelineInfo{};
- pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
- pipelineInfo.stageCount = 2;
- pipelineInfo.pStages = shaderStages;
- pipelineInfo.pVertexInputState = &vertexInputInfo;
- pipelineInfo.pInputAssemblyState = &inputAssembly;
- pipelineInfo.pViewportState = &viewportState;
- pipelineInfo.pRasterizationState = &rasterizer;
- pipelineInfo.pMultisampleState = &multisampling;
- pipelineInfo.pDepthStencilState = nullptr; // Optional
- pipelineInfo.pColorBlendState = &colorBlending;
- pipelineInfo.pDynamicState = nullptr; // Optional
- pipelineInfo.layout = pipelineLayout;
- pipelineInfo.renderPass = renderPass;
- pipelineInfo.subpass = 0;
- pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // Optional
- pipelineInfo.basePipelineIndex = -1; // Optional
- VkPipeline graphicsPipeline;
- res = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline);
- func = "vk Create Graphics Pipelines";
- Print_Result(func, res);
- //
- vector<VkFramebuffer> swapChainFramebuffers;
- swapChainFramebuffers.resize(swap_chain_image_views.size());
- for (auto& swch_frb : swapChainFramebuffers)
- {
- static int n{};
- VkImageView attachments[] = {
- swap_chain_image_views[n]
- };
- VkFramebufferCreateInfo framebufferInfo{};
- framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
- framebufferInfo.renderPass = renderPass;
- framebufferInfo.attachmentCount = 1;
- framebufferInfo.pAttachments = attachments;
- framebufferInfo.width = swapChainExtent.width;
- framebufferInfo.height = swapChainExtent.height;
- framebufferInfo.layers = 1;
- res = vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swch_frb);
- func = "vk Create Framebuffer";
- Print_Result(func, res);
- n++;
- }
- //
- VkCommandPool commandPool;
- VkCommandPoolCreateInfo poolInfo{};
- poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
- poolInfo.queueFamilyIndex = q_num;
- poolInfo.flags = 0; // Optional
- res = vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool);
- func = "vk Create Command Pool";
- Print_Result(func, res);
- vector<VkCommandBuffer> commandBuffers;
- commandBuffers.resize(swapChainFramebuffers.size());
- VkCommandBufferAllocateInfo allocInfo{};
- allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
- allocInfo.commandPool = commandPool;
- allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
- allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
- res = vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data());
- func = "vk Allocate Command Buffers";
- Print_Result(func, res);
- for (auto& cbuf : commandBuffers)
- {
- VkCommandBufferBeginInfo beginInfo{};
- beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
- beginInfo.flags = 0; // Optional
- beginInfo.pInheritanceInfo = nullptr; // Optional
- res = vkBeginCommandBuffer(cbuf, &beginInfo);
- func = "vk Begin Command Buffer";
- Print_Result(func, res);
- }
- VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
- vector<VkRenderPassBeginInfo> renderPassBeginInfos(commandBuffers.size(), VkRenderPassBeginInfo{});
- for (auto& rp_bi : renderPassBeginInfos)
- {
- static int n{};
- rp_bi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
- rp_bi.renderPass = renderPass;
- rp_bi.framebuffer = swapChainFramebuffers[n];
- rp_bi.renderArea.offset = { 0, 0 };
- rp_bi.renderArea.extent = swapChainExtent;
- rp_bi.clearValueCount = 1;
- rp_bi.pClearValues = &clearColor;
- vkCmdBeginRenderPass(commandBuffers[n], &rp_bi, VK_SUBPASS_CONTENTS_INLINE);
- vkCmdBindPipeline(commandBuffers[n], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
- vkCmdDraw(commandBuffers[n], 3, 1, 0, 0);
- vkCmdEndRenderPass(commandBuffers[n]);
- res = vkEndCommandBuffer(commandBuffers[n]);
- func = "vk End Command Buffer";
- Print_Result(func, res);
- n++;
- }
- //
- VkSemaphoreCreateInfo semaphoreInfo{};
- semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
- VkSemaphore imageAvailableSemaphore, renderFinishedSemaphore;
- func = "vk Create Semaphore";
- res = vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphore);
- Print_Result(func, res);
- res = vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphore);
- Print_Result(func, res);
- //
- VkQueue graphicsQueue;
- vkGetDeviceQueue(device, 0, 0, &graphicsQueue);
- VkSubpassDependency dependency{};
- dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
- dependency.dstSubpass = 0;
- dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- dependency.srcAccessMask = 0;
- dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
- renderPassInfo.dependencyCount = 1;
- renderPassInfo.pDependencies = &dependency;
- //
- tme.Refresh();
- while (1)
- {
- tme.Measure_Refresh();
- Time_Tools::Set_Frame(tme, fr_tme_microsec, fr_tme_millisec, fr_tme_sec);
- inp.Parse();
- if (ie.Exit_Combo_Parse())
- cw.Destroy_Active_Window();
- cw.Parse_Msgs();
- if (cw.exit) break;
- //
- // draw frame
- uint32_t imageIndex;
- func = "vk Acquire Next Image KHR";
- res = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
- if (res)
- Print_Result(func, res);
- VkSubmitInfo submitInfo{};
- submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
- VkSemaphore waitSemaphores[] = { imageAvailableSemaphore };
- VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
- submitInfo.waitSemaphoreCount = 1;
- submitInfo.pWaitSemaphores = waitSemaphores;
- submitInfo.pWaitDstStageMask = waitStages;
- submitInfo.commandBufferCount = 1;
- submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
- VkSemaphore signalSemaphores[] = { renderFinishedSemaphore };
- submitInfo.signalSemaphoreCount = 1;
- submitInfo.pSignalSemaphores = signalSemaphores;
- func = "vk Queue Submit";
- res = vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
- if (res)
- Print_Result(func, res);
- VkPresentInfoKHR presentInfo{};
- presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
- presentInfo.waitSemaphoreCount = 1;
- presentInfo.pWaitSemaphores = signalSemaphores;
- VkSwapchainKHR swapChains[] = { swapChain };
- presentInfo.swapchainCount = 1;
- presentInfo.pSwapchains = swapChains;
- presentInfo.pImageIndices = &imageIndex;
- presentInfo.pResults = nullptr; // Optional
- vkQueuePresentKHR(presentQueue, &presentInfo);
- //
- //render.Display();
- }
- //
- vkDestroySemaphore(device, renderFinishedSemaphore, nullptr);
- vkDestroySemaphore(device, imageAvailableSemaphore, nullptr);
- vkDestroyCommandPool(device, commandPool, nullptr);
- for (auto& framebuffer : swapChainFramebuffers)
- vkDestroyFramebuffer(device, framebuffer, nullptr);
- vkDestroyPipeline(device, graphicsPipeline, nullptr);
- vkDestroyRenderPass(device, renderPass, nullptr);
- vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
- vkDestroyShaderModule(device, frag_shader_module, nullptr);
- vkDestroyShaderModule(device, vert_shader_module, nullptr);
- for (auto& swch_imv : swap_chain_image_views)
- vkDestroyImageView(device, swch_imv, nullptr);
- vkDestroySwapchainKHR(device, swapChain, nullptr);
- vkDestroySurfaceKHR(inst, surface, nullptr);
- vkDestroyDevice(device, nullptr);
- Destroy_Debug_Utils_Messenger_EXT(inst, debug_messenger, nullptr);
- vkDestroyInstance(inst, nullptr);
- }
- int main()
- {
- try { Run(); }
- catch (const std::exception & e)
- {
- std::cout << e.what() << std::endl;
- return 1;
- }
- //
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement