为什么我必须在代码中包含“ vkClearValue”才能使其正常工作?

时间:2019-03-31 13:23:15

标签: vulkan

我有一些基本的c ++ / vulkan代码(Windows 10、64):

#define LOG(x) std::cout << "CONSOLE_LOG: " << ##x << std::endl

#include <iostream>
#include <vector>
#include "xgk-vulkan-wrappers.h"
#define GLFW_EXPOSE_NATIVE_WIN32
#include "glfw3.h"
#include <glfw3native.h>

#define WIDTH 800
#define HEIGHT 800

const char* instance_layers[1] = { "VK_LAYER_LUNARG_standard_validation" };
const char* instance_exts[3] = { "VK_KHR_surface", "VK_KHR_win32_surface", VK_EXT_DEBUG_REPORT_EXTENSION_NAME };
const char* device_exts[1] = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
float queuePriorities[] = { 1.0f };

int main() {
  XGK_VULKAN_WRAPPERS::Library library;

  VkApplicationInfo app_info = {};
    app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    app_info.apiVersion = VK_API_VERSION_1_0;

  VkInstanceCreateInfo inst_info = {};
    inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    inst_info.pApplicationInfo = &app_info;
    inst_info.enabledLayerCount = 1;
    inst_info.ppEnabledLayerNames = instance_layers;
    inst_info.enabledExtensionCount = 3;
    inst_info.ppEnabledExtensionNames = instance_exts;

  XGK_VULKAN_WRAPPERS::Instance instance(&library, &inst_info);

  std::vector<XGK_VULKAN_WRAPPERS::PhysicalDevice> devs = instance.enumDevs();

  XGK_VULKAN_WRAPPERS::PhysicalDevice phys_dev = devs[0];
    phys_dev.getQueueFamilyProps();

  VkDeviceQueueCreateInfo queue_info = {};
    queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    queue_info.queueFamilyIndex = 0;
    queue_info.queueCount = 1;
    queue_info.pQueuePriorities = queuePriorities;

  VkDeviceCreateInfo dev_info = {};
    dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    dev_info.queueCreateInfoCount = 1;
    dev_info.pQueueCreateInfos = &queue_info;
    dev_info.enabledExtensionCount = 1;
    dev_info.ppEnabledExtensionNames = device_exts;

  XGK_VULKAN_WRAPPERS::Device dev(&instance, &phys_dev, &dev_info);

  XGK_VULKAN_WRAPPERS::Queue present_queue(&dev, 0, 0);

  XGK_VULKAN_WRAPPERS::Queue graphics_queue(&dev, 0, 0);

  glfwInit();

  glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
  GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "test_vulkan", nullptr, nullptr);
  glfwSetWindowUserPointer(window, instance.handle);

  VkWin32SurfaceCreateInfoKHR win32_surface_info = {};
    win32_surface_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
    win32_surface_info.hinstance = GetModuleHandle(nullptr);
    win32_surface_info.hwnd = glfwGetWin32Window(window);

  XGK_VULKAN_WRAPPERS::SurfaceKHR surfaceKHR(&instance, &win32_surface_info);

  phys_dev.getSurfaceSupportKHR(&surfaceKHR, 0);
  phys_dev.getSurfaceCapabilitiesKHR(&surfaceKHR);
  phys_dev.getSurfaceFormatsKHR(&surfaceKHR);

  const uint32_t queueFamilyIndices[1] = { 0 };

  VkSwapchainCreateInfoKHR swapchain_info = {};
    swapchain_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    swapchain_info.surface = surfaceKHR.handle;
    swapchain_info.minImageCount = 3;
    swapchain_info.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
    swapchain_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
    swapchain_info.imageExtent = { WIDTH, HEIGHT };
    swapchain_info.imageArrayLayers = 1;
    swapchain_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
    swapchain_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    swapchain_info.queueFamilyIndexCount = 1;
    swapchain_info.pQueueFamilyIndices = queueFamilyIndices;
    swapchain_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    swapchain_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
    swapchain_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    swapchain_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
    swapchain_info.clipped = VK_TRUE;

  XGK_VULKAN_WRAPPERS::SwapchainKHR swapchainKHR(&dev, &swapchain_info);

  std::vector<VkImage> images = swapchainKHR.getImages();

  std::vector<XGK_VULKAN_WRAPPERS::ImageView> image_views;
  for (size_t i = 0; i < images.size(); i++) {
    VkImageViewCreateInfo image_view_info = {};
      image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
      image_view_info.image = images[i];
      image_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
      image_view_info.format = VK_FORMAT_B8G8R8A8_UNORM;
      image_view_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
      image_view_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
      image_view_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
      image_view_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
      image_view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
      image_view_info.subresourceRange.baseMipLevel = 0;
      image_view_info.subresourceRange.levelCount = 1;
      image_view_info.subresourceRange.baseArrayLayer = 0;
      image_view_info.subresourceRange.layerCount = 1;

    XGK_VULKAN_WRAPPERS::ImageView image_view(&dev, &image_view_info);

    image_views.push_back(image_view);
  }

  VkAttachmentReference color_attachment_ref = {};
    color_attachment_ref.attachment = 0;
    color_attachment_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

  VkSubpassDescription subpass = {};
    subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    subpass.colorAttachmentCount = 1;
    subpass.pColorAttachments = &color_attachment_ref;

  VkAttachmentDescription color_attachment = {};
    color_attachment.samples = VK_SAMPLE_COUNT_1_BIT;
    color_attachment.format = VK_FORMAT_B8G8R8A8_UNORM;
    color_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    color_attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    color_attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    color_attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    color_attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    color_attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;

  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_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;

  VkRenderPassCreateInfo render_pass_info = {};
    render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    render_pass_info.attachmentCount = 1;
    render_pass_info.pAttachments = &color_attachment;
    render_pass_info.subpassCount = 1;
    render_pass_info.pSubpasses = &subpass;
    render_pass_info.dependencyCount = 1;
    render_pass_info.pDependencies = &dependency;

  XGK_VULKAN_WRAPPERS::RenderPass render_pass(&dev, &render_pass_info);

  std::vector<XGK_VULKAN_WRAPPERS::Framebuffer> framebuffers;
  std::vector<VkCommandBuffer> vk_command_buffers;

  for (size_t i = 0; i < image_views.size(); i++) {
    VkFramebufferCreateInfo framebuffer_info = {};
      framebuffer_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
      framebuffer_info.renderPass = render_pass.handle;
      framebuffer_info.attachmentCount = 1;
      framebuffer_info.pAttachments = &image_views[i].handle;
      framebuffer_info.width = WIDTH;
      framebuffer_info.height = HEIGHT;
      framebuffer_info.layers = 1;

    XGK_VULKAN_WRAPPERS::Framebuffer framebuffer(&dev, &framebuffer_info);

    framebuffers.push_back(framebuffer);

    VkCommandBuffer vk_command_buffer;

    vk_command_buffers.push_back(vk_command_buffer);
  }

  VkCommandPoolCreateInfo command_pool_info = {};
    command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    command_pool_info.queueFamilyIndex = 0;

  XGK_VULKAN_WRAPPERS::CommandPool command_pool(&dev, &command_pool_info);

  VkCommandBufferAllocateInfo alloc_info = {};
    alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
    alloc_info.commandPool = command_pool.handle;
    alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    alloc_info.commandBufferCount = 3;

  command_pool.alloc(&alloc_info, vk_command_buffers.data());

  VkClearColorValue clear_color = { 0, 1, 0, 1 };

  VkCommandBufferBeginInfo command_buffer_begin_info = {};
    command_buffer_begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    command_buffer_begin_info.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;

  VkImageSubresourceRange image_range = {};
    image_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    image_range.baseMipLevel = 0;
    image_range.levelCount = 1;
    image_range.baseArrayLayer = 0;
    image_range.layerCount = 1;

  VkClearValue test_clear_color = { 1, 0, 0, 1 };

  for (size_t i = 0; i < image_views.size(); i++) {
    VkImageMemoryBarrier clear_barier = {};
      clear_barier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
      clear_barier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
      clear_barier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
      clear_barier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
      clear_barier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
      clear_barier.srcQueueFamilyIndex = 0;
      clear_barier.dstQueueFamilyIndex = 0;
      clear_barier.image = images[i];
      clear_barier.subresourceRange = image_range;

    VkImageMemoryBarrier present_barier = {};
      present_barier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
      present_barier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
      present_barier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
      present_barier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
      present_barier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
      present_barier.srcQueueFamilyIndex = 0;
      present_barier.dstQueueFamilyIndex = 0;
      present_barier.image = images[i];
      present_barier.subresourceRange = image_range;

    dev.vkBeginCommandBuffer(vk_command_buffers[i], &command_buffer_begin_info);
    dev.vkCmdPipelineBarrier(vk_command_buffers[i], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &clear_barier);
    dev.vkCmdClearColorImage(vk_command_buffers[i], images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear_color, 1, &image_range);
    dev.vkCmdPipelineBarrier(vk_command_buffers[i], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &present_barier);
    dev.vkEndCommandBuffer(vk_command_buffers[i]);
  }

  VkSemaphore image_available_semaphore;
  VkSemaphore render_finished_semaphore;

  VkSemaphoreCreateInfo
    semaphore_info = {};
    semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;

  vkCreateSemaphore(dev.handle, &semaphore_info, nullptr, &image_available_semaphore);
  vkCreateSemaphore(dev.handle, &semaphore_info, nullptr, &render_finished_semaphore);

  VkPipelineStageFlags wait_stages[1] = { VK_PIPELINE_STAGE_TRANSFER_BIT };

  VkSubmitInfo
    submit_info = {};
    submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    submit_info.waitSemaphoreCount = 1;
    submit_info.pWaitSemaphores = &image_available_semaphore;
    submit_info.pWaitDstStageMask = wait_stages;
    submit_info.commandBufferCount = 1;
    submit_info.signalSemaphoreCount = 1;
    submit_info.pSignalSemaphores = &render_finished_semaphore;

  VkPresentInfoKHR
    present_info_KHR = {};
    present_info_KHR.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    present_info_KHR.waitSemaphoreCount = 1;
    present_info_KHR.pWaitSemaphores = &render_finished_semaphore;
    present_info_KHR.swapchainCount = 1;
    present_info_KHR.pSwapchains = &swapchainKHR.handle;

  uint32_t image_index;

  while (true) {
    swapchainKHR.acquireNextImage(0xffffffff, image_available_semaphore, VK_NULL_HANDLE, image_index);

    submit_info.pCommandBuffers = &vk_command_buffers[image_index];

    graphics_queue.submit(1, &submit_info);

    present_info_KHR.pImageIndices = &image_index;

    present_queue.presentKHR(&present_info_KHR);

    dev.waitIdle();
  }

  vkDestroySemaphore(dev.handle, image_available_semaphore, nullptr);
  vkDestroySemaphore(dev.handle, render_finished_semaphore, nullptr);

  command_pool.destroy();

  for (size_t i = 0; i < framebuffers.size(); i++) {
    framebuffers[i].destroy();
  }

  render_pass.destroy();

  for (size_t i = 0; i < image_views.size(); i++) {
    image_views[i].destroy();
  }

  swapchainKHR.destroy();
  surfaceKHR.destroy();

  glfwDestroyWindow(window);
  glfwTerminate();

  dev.destroy(); 
  instance.destroy(); 
  library.destroy();

  return 0;
};

这是代码中的多余一行:

“ VkClearValue test_clear_color = {1,0,0,1};”

此对象从未在代码中使用,仅被声明。但是,如果我删除或注释这一行,则代码执行将在图形队列子任务上失败。

有人面对过这种奇怪的行为吗?有什么问题吗?

0 个答案:

没有答案