无法使用图片提交cmdbuffer ...

时间:2017-04-10 21:32:36

标签: c++ vulkan

我正试图用一种颜色清除屏幕,但我总是收到错误 “首次使用VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL时,无法使用带有布局VK_IMAGE_LAYOUT_UNDEFINED的图像(...)提交cmd缓冲区。

而Actully我试图修改一些undefine值到depth_stenci_attachment最优,但唯一的问题是我得到的更多是这些错误。那么是否有一个字段,填写错误,或者我忘了填写?

所以这是我的完整main.cpp因为我不知道错误可能在哪里。

#include<stdio.h>
#include<string.h>
#include<vector>

#define DEBUG

#ifdef _WIN32
#define VK_USE_PLATFORM_WIN32_KHR 
#endif
#define KNOCH_JULIA 42
#include"window.h"

using namespace std;

#ifdef DEBUG
#include<iostream>

using namespace std;
VkDebugReportCallbackEXT report;
void init_debug(vulkan *vulk);

PFN_vkCreateDebugReportCallbackEXT fvkCreateDebugReportCallbackEXT = VK_NULL_HANDLE;
PFN_vkDestroyDebugReportCallbackEXT fvkDestroyDebugReportCallbackEXT = VK_NULL_HANDLE;


VKAPI_ATTR VkBool32 VKAPI_CALL callback(VkDebugReportFlagsEXT flag, VkDebugReportObjectTypeEXT obj_t, uint64_t src_obj, size_t loc, int32_t msg_code, const char* layer_pref, const char* msg, void* user_data) {
    switch (flag) {
    case VK_DEBUG_REPORT_ERROR_BIT_EXT:
        cout<<"error!"<<" "<< flag<<" source:"<<src_obj<<"location: "<<loc<<": "<< msg<<endl;
        break;
    case VK_DEBUG_REPORT_WARNING_BIT_EXT:
        cout << "warning!" << obj_t << ": " << msg << endl;
        break;
    }
    return false;
}
void init_debug(vulkan *vulk) {
    fvkCreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(vulk->inst, "vkCreateDebugReportCallbackEXT");
    fvkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(vulk->inst, "vkDestroyDebugReportCallbackEXT");
    if (nullptr == fvkCreateDebugReportCallbackEXT || nullptr == fvkDestroyDebugReportCallbackEXT) {
        exit(-5);
    }
    VkDebugReportCallbackCreateInfoEXT info = {};
    info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
    info.pfnCallback = callback;
    info.pNext = nullptr;
    info.flags = VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_ERROR_BIT_EXT;
    fvkCreateDebugReportCallbackEXT(vulk->inst, &info, nullptr, &report);
}
#endif

FILE *fileptr;
void initInstance(vulkan *vulk){
    vector<char*> ext;
    ext.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
    ext.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);

    VkApplicationInfo app_info = {};
    app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    app_info.apiVersion = VK_MAKE_VERSION(1, 0, 39);
    app_info.engineVersion = VK_MAKE_VERSION(0, 0, 1);
    app_info.pApplicationName = "szar";
    app_info.pEngineName = "yayitstarts";
    app_info.pNext = nullptr;

    VkInstanceCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
#ifdef DEBUG
    vector<char*>layers;
    layers.push_back("VK_LAYER_LUNARG_object_tracker");
    layers.push_back("VK_LAYER_LUNARG_core_validation");
    layers.push_back("VK_LAYER_LUNARG_parameter_validation");
    //layers.push_back("VK_LAYER_LUNARG_vktrace");
    layers.push_back("VK_LAYER_LUNARG_swapchain");
    layers.push_back("VK_LAYER_LUNARG_image");
    ext.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
#endif

#ifdef DEBUG
    info.enabledLayerCount = layers.size();
    info.ppEnabledLayerNames = layers.data();
#else
    info.enabledLayerCount = 0;
    info.ppEnabledLayerNames = nullptr;
#endif
    info.pApplicationInfo = &app_info;
    info.enabledExtensionCount = ext.size();
    info.ppEnabledExtensionNames = ext.data();

    info.flags = 0;
    info.pNext = nullptr;

    vkCreateInstance(&info, nullptr, &(vulk->inst));
}
void getGPU(vulkan *vulk) {
    uint32_t dev_c=0;

    vkEnumeratePhysicalDevices(vulk->inst,&dev_c,nullptr);
    VkPhysicalDevice *gpus=(VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice)*dev_c);
    vkEnumeratePhysicalDevices(vulk->inst, &dev_c, gpus);
    vulk->gpu = gpus[0];
}
void createDevice(vulkan *vulk) {
    VkPhysicalDeviceFeatures features;
    vkGetPhysicalDeviceFeatures(vulk->gpu, &features);

    float prior[] = { 1.0f };

    uint32_t prop_c;
    vkGetPhysicalDeviceQueueFamilyProperties(vulk->gpu, &prop_c, nullptr);
    VkQueueFamilyProperties *props = (VkQueueFamilyProperties*)malloc(sizeof(VkQueueFamilyProperties)*prop_c);
    vkGetPhysicalDeviceQueueFamilyProperties(vulk->gpu, &prop_c, props);

    uint32_t index = -1;

    for (int i = 0; i < prop_c; i++) {
        VkBool32 supported;
        vkGetPhysicalDeviceSurfaceSupportKHR(vulk->gpu, i, vulk->surface_struct.surface, &supported);
        if (props[i].queueFlags&VK_QUEUE_GRAPHICS_BIT&&supported) {
            index = i;
        }
    }
    if (index == -1) {
        printf("no graphic queue family found");
        exit(-1);
    }

#ifdef DEBUG
    uint32_t count;

    vkEnumerateInstanceLayerProperties(&count, nullptr);
    vector<VkLayerProperties>layers_access(count);
    vkEnumerateInstanceLayerProperties(&count, layers_access.data());
    for (int i = 0; i < count; i++) {
        printf("%s\n", layers_access[i].layerName);
    }
    uint32_t dev_count;
    vkEnumerateDeviceLayerProperties(vulk->gpu, &dev_count, nullptr);
    vector<VkLayerProperties>layers_access_dev(count);
    vkEnumerateDeviceLayerProperties(vulk->gpu, &dev_count, layers_access.data());
    for (int i = 0; i < dev_count; i++) {
        printf("dev: %s\n", layers_access_dev[i].layerName);
    }
#endif
    vulk->queue_fam_ind = index;
    int queue_count=1;

    vector<char*> ext;
    ext.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);

#ifdef DEBUG
    vector<char*> layers;
    layers.push_back("VK_LAYER_LUNARG_object_tracker");
    layers.push_back("VK_LAYER_LUNARG_core_validation");
#endif

    VkDeviceQueueCreateInfo queue_info = {};
    queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    queue_info.pQueuePriorities = prior;
    queue_info.queueCount = queue_count;
    queue_info.queueFamilyIndex = index;
    queue_info.flags = 0;
    queue_info.pNext = nullptr;

    VkDeviceCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
#ifdef DEBUG
    info.enabledLayerCount = layers.size();
    info.ppEnabledLayerNames = layers.data();
#else
    info.enabledLayerCount = 0;
    info.ppEnabledLayerNames = nullptr;
#endif
    info.pEnabledFeatures = &features;
    info.enabledExtensionCount = ext.size();
    info.ppEnabledExtensionNames = ext.data();
    info.pQueueCreateInfos = &queue_info;
    info.queueCreateInfoCount = 1;
    info.pNext = nullptr;

    if (VK_SUCCESS != vkCreateDevice(vulk->gpu, &info, nullptr, &(vulk->device))) {
        exit(-1);
    }

    vulk->queue = (VkQueue*)malloc(sizeof(VkQueue)*queue_count);
    vkGetDeviceQueue(vulk->device, index, 0, &(vulk->queue[0]));
}
void createSwapchain(vulkan *vulk) {
    VkSurfaceCapabilitiesKHR capabilities;

    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulk->gpu, vulk->surface_struct.surface,&capabilities);

    uint32_t format_c;
    vkGetPhysicalDeviceSurfaceFormatsKHR(vulk->gpu, vulk->surface_struct.surface, &format_c, nullptr);
    VkSurfaceFormatKHR *formats = (VkSurfaceFormatKHR*)malloc(sizeof(VkSurfaceFormatKHR)*format_c);
    vkGetPhysicalDeviceSurfaceFormatsKHR(vulk->gpu, vulk->surface_struct.surface, &format_c, formats);

    uint32_t pres_mode_c;
    vkGetPhysicalDeviceSurfacePresentModesKHR(vulk->gpu, vulk->surface_struct.surface, &pres_mode_c, nullptr);
    VkPresentModeKHR *pres_modes = (VkPresentModeKHR*)malloc(sizeof(VkPresentModeKHR)*pres_mode_c);
    vkGetPhysicalDeviceSurfacePresentModesKHR(vulk->gpu, vulk->surface_struct.surface, &pres_mode_c, pres_modes);

    int pres_mode_i = 0;
    for (int i = 0; i < pres_mode_c; i++) {
        if (pres_modes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
            pres_mode_i = i;
        }
    }

    vulk->surface_struct.extent = capabilities.currentExtent;
    vulk->image.color_format= formats[0].format;

    VkSwapchainCreateInfoKHR info = {};
    info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    info.clipped = VK_TRUE;
    info.compositeAlpha = (VkCompositeAlphaFlagBitsKHR)capabilities.supportedCompositeAlpha;
    info.flags = 0;
    info.imageArrayLayers=1;
    info.imageColorSpace = formats[0].colorSpace;
    info.imageExtent = capabilities.currentExtent;
    info.imageFormat = formats[0].format;
    info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    info.minImageCount = capabilities.minImageCount;
    info.oldSwapchain =VK_NULL_HANDLE;
    info.pNext = nullptr;
    info.pQueueFamilyIndices =&(vulk->queue_fam_ind);
    info.presentMode = pres_modes[pres_mode_i];
    info.preTransform = capabilities.currentTransform;
    info.queueFamilyIndexCount = 1;
    info.surface = vulk->surface_struct.surface;

    VkResult not_VK_SUCCESS = vkCreateSwapchainKHR(vulk->device, &info, nullptr, &(vulk->swapchain_struct.swapchain));
    if (not_VK_SUCCESS != VK_SUCCESS) {
        exit(-1);
    }
    vulk->swapchain_struct.format = formats[0].format;
}
void createImages(vulkan *vulk,Memory *depth_img_memory) {
    uint32_t img_c;
    vkGetSwapchainImagesKHR(vulk->device, vulk->swapchain_struct.swapchain, &img_c, nullptr);
    vulk->image.color_images = (VkImage*)malloc(sizeof(VkImage)*img_c);
    vkGetSwapchainImagesKHR(vulk->device, vulk->swapchain_struct.swapchain, &img_c, vulk->image.color_images);

    vulk->image_c = img_c;

    vulk->image.depth_images = (VkImage*)malloc(sizeof(VkImage));

    vulk->image.color_image_views=(VkImageView*)malloc(sizeof(VkImageView)*img_c);
    vulk->image.depth_image_views=(VkImageView*)malloc(sizeof(VkImageView));

    VkComponentMapping mapping = {};
    mapping.r = VK_COMPONENT_SWIZZLE_R;
    mapping.g = VK_COMPONENT_SWIZZLE_G;
    mapping.b = VK_COMPONENT_SWIZZLE_B;
    mapping.a = VK_COMPONENT_SWIZZLE_A;

    vulk->image.color_range = (VkImageSubresourceRange*)malloc(sizeof(VkImageSubresourceRange)*img_c);

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

    for (int i = 0; i < img_c; i++) {
        VkImageViewCreateInfo info = {};
        info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
        info.components = mapping;
        info.flags = 0;
        info.format = vulk->swapchain_struct.format;
        info.image = (vulk->image.color_images)[i];
        info.pNext = nullptr;
        info.subresourceRange = range;
        info.viewType = VK_IMAGE_VIEW_TYPE_2D;

        vulk->image.color_range[i] = range;
        vkCreateImageView(vulk->device, &info, nullptr, &(vulk->image.color_image_views)[i]);
    }
    vulk->image.depth_range = (VkImageSubresourceRange*)malloc(sizeof(VkImageSubresourceRange));
    vulk->image.depth_range[0] = range;

    vector<VkFormat> depth_formats{ 
        VK_FORMAT_D32_SFLOAT_S8_UINT,
        VK_FORMAT_D32_SFLOAT,
        VK_FORMAT_D24_UNORM_S8_UINT,
        VK_FORMAT_D16_UNORM_S8_UINT,
        VK_FORMAT_D16_UNORM
    };

    VkFormat depth_format;
    for (int i = 0; i < depth_formats.size(); i++) {
        VkFormatProperties props;
        vkGetPhysicalDeviceFormatProperties(vulk->gpu, depth_formats[i], &props);
        if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
            depth_format = depth_formats[i];
            break;
        }
    }

    vulk->image.depth_format = depth_format;
    VkImageCreateInfo img_info = {};
    img_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
    img_info.arrayLayers = 1;
    img_info.extent.width = vulk->surface_struct.extent.width;
    img_info.extent.height = vulk->surface_struct.extent.height;
    img_info.extent.depth = 1;
    img_info.flags = 0;
    img_info.format = depth_format;
    img_info.imageType = VK_IMAGE_TYPE_2D;
    img_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    img_info.mipLevels = 1;
    img_info.pNext = nullptr;
    img_info.pQueueFamilyIndices = &(vulk->queue_fam_ind);
    img_info.queueFamilyIndexCount = 1;
    img_info.samples = VK_SAMPLE_COUNT_1_BIT;
    img_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    img_info.tiling = VK_IMAGE_TILING_OPTIMAL;
    img_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;

    if (VK_SUCCESS!=vkCreateImage(vulk->device, &img_info, nullptr, &(vulk->image.depth_images)[0])) {
        printf("It not works");
    }
    VkMemoryRequirements req;
    vkGetImageMemoryRequirements(vulk->device, (vulk->image.depth_images)[0], &req);

    vkGetPhysicalDeviceMemoryProperties(vulk->gpu, &(depth_img_memory->props));
    uint32_t mem_index=-2;
    for (int i = 0; i < depth_img_memory->props.memoryTypeCount; i++) {
        if (req.memoryTypeBits & (1 << i)) {
            if ((depth_img_memory->props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
                mem_index = i;
                break;
            }
        }
    }

    if (mem_index == -2) {
        printf("no supported memorytype");
        exit(-2);
    }

    VkMemoryAllocateInfo mem_info = {};
    mem_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    mem_info.pNext = nullptr;
    mem_info.allocationSize = req.size;
    mem_info.memoryTypeIndex = mem_index;

    vkAllocateMemory(vulk->device, &mem_info, nullptr, &(depth_img_memory->dev_mem));
    vkBindImageMemory(vulk->device, (vulk->image.depth_images)[0], depth_img_memory->dev_mem, 0);

    VkComponentMapping mapping_d = {
        VK_COMPONENT_SWIZZLE_IDENTITY,
        VK_COMPONENT_SWIZZLE_IDENTITY,
        VK_COMPONENT_SWIZZLE_IDENTITY,
        VK_COMPONENT_SWIZZLE_IDENTITY,
    };

    range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;


    VkImageViewCreateInfo img_view_info_d = {};
    img_view_info_d.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    img_view_info_d.components = mapping_d;
    img_view_info_d.flags = 0;
    img_view_info_d.format = depth_format;
    img_view_info_d.image = (vulk->image.depth_images)[0];
    img_view_info_d.pNext = nullptr;
    img_view_info_d.subresourceRange = range;
    img_view_info_d.viewType = VK_IMAGE_VIEW_TYPE_2D;

    if (VK_SUCCESS != vkCreateImageView(vulk->device, &img_view_info_d, nullptr, &(vulk->image.depth_image_views)[0])) {
        printf("huge pile of shit!!!");
        exit(-1);
    }

}
void createCommandPool(vulkan vulk,cmd_pool *pool, uint32_t cmd_buff_c) {
    VkCommandPoolCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT|VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
    info.pNext = nullptr;
    info.queueFamilyIndex = vulk.queue_fam_ind;

    vkCreateCommandPool(vulk.device, &info, nullptr, &(pool->pool));

    pool->cmd_buff_c = cmd_buff_c;
    pool->cmd_buffs = (VkCommandBuffer*)malloc(sizeof(VkCommandBuffer) * cmd_buff_c);

    VkCommandBufferAllocateInfo cmd_info = {};
    cmd_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
    cmd_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    cmd_info.commandPool = pool->pool;
    cmd_info.commandBufferCount = cmd_buff_c ;
    cmd_info.pNext = nullptr;

    vkAllocateCommandBuffers(vulk.device, &cmd_info, pool->cmd_buffs);
}
VkClearValue *clear;
void createFramebuffer(vulkan *vulk,VkExtent2D extent) {
    vulk->fbo = (VkFramebuffer*)malloc(sizeof(VkFramebuffer)*vulk->image_c);
    for (int i = 0; i < vulk->image_c; i++) {
        VkImageView *img_views = (VkImageView*)malloc(sizeof(VkImageView) * 2);
        img_views[0] = vulk->image.color_image_views[i];
        img_views[1] = vulk->image.depth_image_views[0];

        VkFramebufferCreateInfo info = {};
        info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
        info.attachmentCount = 2;
        info.pAttachments = img_views;
        info.width = extent.height;
        info.height = extent.width;
        info.layers = 1;
        info.renderPass = vulk->render_pass;
        info.flags = 0;
        info.pNext = nullptr;

        if (VK_SUCCESS != vkCreateFramebuffer(vulk->device, &info, nullptr, &(vulk->fbo[i]))) {
            printf("could not create framebuffer");
        }
    }
}


VkSemaphore *semaphores;

void createSemaphore(vulkan *vulk ,VkSemaphore *semaphore) {
    VkSemaphoreCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
    info.pNext = nullptr;
    info.flags = 0;

    vkCreateSemaphore(vulk->device, &info, nullptr, semaphore);
}
void createRenderPass(vulkan *vulk) {
    VkAttachmentDescription *descr = (VkAttachmentDescription*)malloc(sizeof(VkAttachmentDescription) * 2);

    VkAttachmentDescription color_descr = {};
    color_descr.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    color_descr.format = vulk->image.color_format;
    color_descr.samples = VK_SAMPLE_COUNT_1_BIT;
    color_descr.initialLayout= VK_IMAGE_LAYOUT_UNDEFINED;
    color_descr.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    color_descr.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    color_descr.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    color_descr.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    color_descr.flags = 0;

    VkAttachmentDescription depth_descr = {};
    depth_descr.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
    depth_descr.format = vulk->image.depth_format;
    depth_descr.samples = VK_SAMPLE_COUNT_1_BIT;
    depth_descr.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    depth_descr.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    depth_descr.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    depth_descr.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    depth_descr.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
    depth_descr.flags = 0;

    *descr = color_descr;
    *(descr + 1) = depth_descr;

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

    VkAttachmentReference depth_ref = {};
    depth_ref.attachment = 1;
    depth_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

    VkSubpassDescription subp_descr = {};
    subp_descr.colorAttachmentCount = 1;
    subp_descr.pColorAttachments=&color_ref;
    subp_descr.pDepthStencilAttachment = &depth_ref;
    subp_descr.inputAttachmentCount = 0;
    subp_descr.pInputAttachments = nullptr;
    subp_descr.preserveAttachmentCount = 0;
    subp_descr.pPreserveAttachments = nullptr;
    subp_descr.pResolveAttachments = nullptr;
    subp_descr.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    subp_descr.flags = 0;

    VkRenderPassCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    info.attachmentCount = 2;
    info.pAttachments = descr;
    info.dependencyCount = 0;
    info.pDependencies = VK_NULL_HANDLE;
    info.subpassCount = 1;
    info.pSubpasses = &subp_descr;
    info.flags = 0;
    info.pNext = nullptr;

    if (VK_SUCCESS != vkCreateRenderPass(vulk->device, &info, nullptr, &(vulk->render_pass))) {
        printf("Could not create render pass.");
    }
}

VkFence fence;

void createFences(vulkan *vulk) {
    VkFenceCreateInfo info = {};
    info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
    info.pNext = nullptr;
    info.flags = 0;

    vkCreateFence(vulk->device, &info, nullptr, &fence);
}
int main(int argc,char** argv) {
    vulkan vulk;
    Memory depth_memory;
    cmd_pool pool;

    initInstance(&vulk);
    getGPU(&vulk);
    Window window = Window();
    window.open(&vulk);
    createDevice(&vulk);

    VkViewport viewport = {};
    viewport.width = window.extent.width;
    viewport.height = window.extent.height;
    viewport.x = 0;
    viewport.y = 0;
    viewport.minDepth = 0.0f;
    viewport.maxDepth = 1.0f;
    init_debug(&vulk);
    createSwapchain(&vulk);
    createImages(&vulk, &depth_memory);
    createRenderPass(&vulk);
    createFramebuffer(&vulk,window.extent);

    semaphores= (VkSemaphore*) malloc(sizeof(VkSemaphore)*2);

    createSemaphore(&vulk, &semaphores[0]);
    createSemaphore(&vulk, &semaphores[1]);
    createFences(&vulk);

    createCommandPool(vulk,&pool,2);

    uint32_t img_pres;
    VkResult result;

    VkPresentInfoKHR info = {};
    info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    info.pImageIndices = &img_pres;
    info.pResults = &result;
    info.swapchainCount = 1;
    info.pSwapchains = &vulk.swapchain_struct.swapchain;
    info.waitSemaphoreCount =0;
    info.pWaitSemaphores = nullptr;
    info.pNext = nullptr;

    VkCommandBufferBeginInfo beg = {};
    beg.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    beg.pInheritanceInfo = nullptr;
    beg.pNext = nullptr;
    beg.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;

    VkClearValue val[2];
    val[1] = { 0.0f,1.0f,1.0f,1.0f };
    val[0] = { 0.0f,0 };

    VkRenderPassBeginInfo render = {};
    render.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
    render.clearValueCount = 2;
    render.framebuffer = vulk.fbo[0];
    render.pClearValues = val;
    render.pNext = nullptr;
    render.renderArea.offset = { 0,0 };
    render.renderArea.extent = { window.extent.height, window.extent.width };
    render.renderPass = vulk.render_pass;

    vkBeginCommandBuffer(pool.cmd_buffs[0], &beg);
    vkCmdBeginRenderPass(pool.cmd_buffs[0], &render, VK_SUBPASS_CONTENTS_INLINE);
    vkCmdEndRenderPass(pool.cmd_buffs[0]);
    vkEndCommandBuffer(pool.cmd_buffs[0]);

    VkRenderPassBeginInfo render_2 = {};
    render_2.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
    render_2.clearValueCount = 2;
    render_2.framebuffer = vulk.fbo[1];
    render_2.pClearValues = val;
    render_2.pNext = nullptr;
    render_2.renderArea = { 0,0,window.extent.height,window.extent.width };
    render_2.renderPass = vulk.render_pass;

    vkBeginCommandBuffer(pool.cmd_buffs[1], &beg);
    vkCmdBeginRenderPass(pool.cmd_buffs[1], &render_2, VK_SUBPASS_CONTENTS_INLINE);
    vkCmdEndRenderPass(pool.cmd_buffs[1]);
    vkEndCommandBuffer(pool.cmd_buffs[1]);

    VkSubmitInfo sub = {};
    sub.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    sub.commandBufferCount = 1;
    sub.pNext = nullptr;
    sub.pSignalSemaphores = nullptr;
    sub.pWaitDstStageMask = nullptr;
    sub.pWaitSemaphores = nullptr;
    sub.signalSemaphoreCount = 0;
    sub.waitSemaphoreCount = 0;
    VkResult res=VK_ERROR_DEVICE_LOST;

    sub.pCommandBuffers = &pool.cmd_buffs[0];
    vkQueueSubmit(vulk.queue[0], 1, &sub, VK_NULL_HANDLE);
    sub.pCommandBuffers = &pool.cmd_buffs[1];
    vkQueueSubmit(vulk.queue[0], 1, &sub, VK_NULL_HANDLE);

    while (window.running) {
        if (VK_SUCCESS != vkAcquireNextImageKHR(vulk.device, vulk.swapchain_struct.swapchain, UINT64_MAX, VK_NULL_HANDLE, fence, &img_pres)) {
            return -2;
        }
        vkWaitForFences(vulk.device, 1, &fence, VK_TRUE, UINT64_MAX);
        vkResetFences(vulk.device, 1, &fence);vkQueueWaitIdle(vulk.queue[0]);

        sub.pCommandBuffers = &pool.cmd_buffs[img_pres];
        if (res == vkQueueSubmit(vulk.queue[0], 1, &sub, VK_NULL_HANDLE)) {
            printf("img: %d\n",res);
        }
        cout << hex << vulk.image.depth_images[0] << endl;
        vkQueuePresentKHR(vulk.queue[0], &info);
        window.run();
    }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

我还没有彻底检查你的代码,但是renderpass说两个附件最初都处于UNDEFINED布局,转换到COLOR_ATTACHMENT_OPTIMAL / DEPTH_STENCIL_ATTACHMENT_OPTIMAL,然后转到PRESENT_SRC / DEPTH_STENCIL_ATTACHMENT_OPTIMAL。这似乎是正确的,验证层似乎忽略了renderpass initialLayout设置。如果您在最新版本的SDK上看到此问题,请在https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers提交错误。

答案 1 :(得分:0)

1)更新SDK。自1.0.42.0起,甚至没有VK_LAYER_LUNARG_image层。而且你知道,可能有大量的错误修正。

2)您正在以错误的顺序启用图层。使用VK_LAYER_LUNARG_standard_validation元层而不是手动执行(也避免了1尝试使用过时图层时的问题)。

3)我在你的代码中看到很多错误。图层不一定具有完整的覆盖范围(另一个未经修复的错误可能会导致在没有上下文的情况下显示另一个没有意义的错误。)

E.g。没有同步(你的信号量在那里没用),很多内存泄漏(由于C风格编程),假设至少有两个交换链图像,而不是检查VkResult s ...

4)我无法用你的代码重现它。首先,我遇到的问题是只有一个交换链图像和代码不期望它(在3中提到)。修复之后,我收到的错误是vkAcquireNextImageKHR获得的图像多于允许的图像(如果使用VkPresentInfoKHR::pResults,则驱动程序图层错误)。解决方法是我没有收到任何错误消息。