basic draw triangle is not normal, sometime draws right, sometimes nothing


#1

Code:


void TKScene::updateDrawCommand(){
    //TKLog("update command image indice %d
", m_currentIdx);
    vkResetCommandBuffer(TKBaseInfo::Info()->commandBuffers[m_currentIdx],
                         VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
    VkCommandBufferBeginInfo cmdBufBeginInfo;
    cmdBufBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    cmdBufBeginInfo.pNext = nullptr;
    cmdBufBeginInfo.flags =
        VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT |
        VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
    cmdBufBeginInfo.pInheritanceInfo = nullptr;
    VkResult result = vkBeginCommandBuffer(TKBaseInfo::Info()->commandBuffers[m_currentIdx],
                                           &cmdBufBeginInfo);
    if(result != VK_SUCCESS){
        TKLog("begin command buffer error!
");
    }

    VkImageMemoryBarrier presentToDrawBarrier;
    presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    presentToDrawBarrier.pNext = nullptr;
    presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
    presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
        presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
        presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
    }else{
        presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
        presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    }
    
    presentToDrawBarrier.image = TKBaseInfo::Info()->swapchainImages[m_currentIdx];
    
    VkImageSubresourceRange subResRange;
    subResRange.aspectMask      = VK_IMAGE_ASPECT_COLOR_BIT;
    subResRange.baseMipLevel   = 0;
    subResRange.levelCount      = 1;
    subResRange.baseArrayLayer  = 0;
    subResRange.layerCount      = 1;
    presentToDrawBarrier.subresourceRange = subResRange;
    vkCmdPipelineBarrier(TKBaseInfo::Info()->commandBuffers[m_currentIdx],
                         VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
                         VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
                         0, 0, nullptr, 0, nullptr, 1, &presentToDrawBarrier);

    VkRenderPassBeginInfo beginInfo;
    beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
    beginInfo.pNext = nullptr;
    beginInfo.renderPass  = TKBaseInfo::Info()->renderPass;
    beginInfo.framebuffer = TKBaseInfo::Info()->framebuffers[m_currentIdx];
    beginInfo.renderArea  = { {0, 0}, {SCREEN_WIDTH, SCREEN_HEIGHT} };
    std::vector<VkClearValue> clearValues(2);
    clearValues[0] = {0.2f, 0.16f, 0.31f, 1.0f};
    clearValues[1].depthStencil.depth = 1.0f;
    clearValues[1].depthStencil.stencil = 0.0f;
    beginInfo.clearValueCount = clearValues.size();
    beginInfo.pClearValues    = clearValues.data();

    VkPipeline pipelineUsing = TKPipelineManager::getPipeline(std::string("basic"));
    if(pipelineUsing == VK_NULL_HANDLE){
        TKLog("pipeline error!
");
    }else{
        vkCmdBeginRenderPass(TKBaseInfo::Info()->commandBuffers[m_currentIdx],
                             &beginInfo, VK_SUBPASS_CONTENTS_INLINE);
    }
    
    VkViewport viewport = {0, float(m_height), float(m_width), -float(m_height), 0.0, 1.0};
    VkRect2D rect = {{0, 0}, {m_width, m_height}};
    vkCmdBindPipeline(TKBaseInfo::Info()->commandBuffers[m_currentIdx],
                      VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineUsing);

    vkCmdSetViewport(TKBaseInfo::Info()->commandBuffers[m_currentIdx], 0, 1, &viewport);
    vkCmdSetScissor(TKBaseInfo::Info()->commandBuffers[m_currentIdx], 0, 1, &rect);
    vkCmdSetLineWidth(TKBaseInfo::Info()->commandBuffers[m_currentIdx], 1.0);

    for(int i=0; i<m_drawNodes.size();++i){
        m_drawNodes[i]->drawInCommandBuffer(TKBaseInfo::Info()->commandBuffers[m_currentIdx]);
    }
     
    vkCmdEndRenderPass(TKBaseInfo::Info()->commandBuffers[m_currentIdx]);
    if(TKBaseInfo::Info()->presentQueueFamily != TKBaseInfo::Info()->graphicsQueueFamily){
        VkImageSubresourceRange subResourceRange;
        subResourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
        subResourceRange.baseMipLevel = 0;
        subResourceRange.levelCount = 1;
        subResourceRange.baseArrayLayer = 0;
        subResourceRange.layerCount = 1;
        VkImageMemoryBarrier drawToPresentBarrier;
        drawToPresentBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
        drawToPresentBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
        drawToPresentBarrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
        drawToPresentBarrier.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
        drawToPresentBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
        drawToPresentBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
        drawToPresentBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
        drawToPresentBarrier.image = TKBaseInfo::Info()->swapchainImages[m_currentIdx];
        drawToPresentBarrier.subresourceRange = subResourceRange;
        vkCmdPipelineBarrier(TKBaseInfo::Info()->commandBuffers[m_currentIdx],
                             VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
                             VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
                             nullptr, 0, nullptr, 1, &drawToPresentBarrier);
    }

    VkResult ret = vkEndCommandBuffer(TKBaseInfo::Info()->commandBuffers[m_currentIdx]);
    if(ret != VK_SUCCESS){
        TKLog("end command buffer error!
");
    }
    //TKLog("end update command buffer
");
}

void TKScene::renderDraw(){
    
    VkResult result;
    do{
        result = vkAcquireNextImageKHR(TKBaseInfo::Info()->device, TKBaseInfo::Info()->swapchain,
                                       UINT64_MAX, TKBaseInfo::Info()->graphicsSemaphore[m_frameIdx],
                                       VK_NULL_HANDLE, &m_currentIdx);
        // TKLog("result %x
", result);
    }while(result != VK_SUCCESS);
    //TKLog("frame idx: %d, current idx = %d
", m_frameIdx, m_currentIdx);

    // this->updateDrawCommand();

    //submit
    VkSubmitInfo submitInfo;
    submitInfo.sType                = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    submitInfo.pNext                = nullptr;
    submitInfo.waitSemaphoreCount   = 1;
    submitInfo.pWaitSemaphores      = &TKBaseInfo::Info()->graphicsSemaphore[m_frameIdx];
    VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
    submitInfo.pWaitDstStageMask    = &pipe_stage_flags;
    submitInfo.commandBufferCount   = 1;
    submitInfo.pCommandBuffers      = &(TKBaseInfo::Info()->commandBuffers[m_currentIdx]);
    submitInfo.signalSemaphoreCount = 1;
    submitInfo.pSignalSemaphores    = &TKBaseInfo::Info()->presentSemaphore[m_frameIdx];
    if (VK_SUCCESS != vkQueueSubmit(TKBaseInfo::Info()->graphicsQueue, 1, &submitInfo, TKBaseInfo::Info()->fences[m_currentIdx])){
        TKLog("queue submit failed!
");
    }

    //present 
    VkPresentInfoKHR presentInfo;
    presentInfo.sType              = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    presentInfo.pNext              = nullptr;
    presentInfo.waitSemaphoreCount = 1;
    presentInfo.pWaitSemaphores    = &TKBaseInfo::Info()->presentSemaphore[m_frameIdx];
    presentInfo.swapchainCount     = 1;
    presentInfo.pSwapchains        = &TKBaseInfo::Info()->swapchain;
    presentInfo.pImageIndices      = &m_currentIdx;
    presentInfo.pResults           = nullptr;
    
    VkResult ret = vkQueuePresentKHR(TKBaseInfo::Info()->presentQueue, &presentInfo);
    switch(ret){
    case VK_SUCCESS:
        m_frameIdx += 1;
        m_frameIdx %= TKBaseInfo::Info()->framebuffers.size();
        break;
    default:
        TKLog("Problem occured during image presentation!
");
        break;
    }
    vkWaitForFences(TKBaseInfo::Info()->device, 1, &TKBaseInfo::Info()->fences[m_currentIdx], VK_TRUE, UINT64_MAX);
    vkResetFences(TKBaseInfo::Info()->device, 1, &TKBaseInfo::Info()->fences[m_currentIdx]); 
}

Maybe the Fences and Semaphores I am using, something wrong with in it, I think, But I still can’t resovle this problem.
I need help, thanks!


#2

Your presentToDrawBarrier barrier seems nonsensical to me. The arguments do not match what I would assume from the name at all. And queues are transfered when they are equal, and ignored when differing.

Are you using the Validation Layers?

There also seems to be some sync missing (e.g. I don’t see transition to PRESENT layout). I assume there are also some external subpass dependencies you do not show?

BTW post code as [NOPARSE]

blah

[/NOPARSE] (or use the button in advanced mode), so the code is readable.


#3

I have found the reason, I missed the pipelineDescriptorSetLayout. when I setup all the descriptorSets, it gone right.
My code above is not very good.

Yes, I using the Validation Layers


vkCreateInstance(pCreateInfo, pAllocator, pInstance) returns VkResult VK_ERROR_INITIALIZATION_FAILED (-3):
    pCreateInfo:                    const VkInstanceCreateInfo* = 0x7ffd48c0fb40:
        sType:                          VkStructureType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO (1)
        pNext:                          const void* = 0x7ffd48c0fb10
        flags:                          VkInstanceCreateFlags = 0
        pApplicationInfo:               const VkApplicationInfo* = 0x7ffd48c0fdb0:
            sType:                          VkStructureType = VK_STRUCTURE_TYPE_APPLICATION_INFO (0)
            pNext:                          const void* = NULL
            pApplicationName:               const char* = "TKScene"
            applicationVersion:             uint32_t = 1
            pEngineName:                    const char* = "TKEngine"
            engineVersion:                  uint32_t = 1
            apiVersion:                     uint32_t = 4198400
        enabledLayerCount:              uint32_t = 11
        ppEnabledLayerNames:            const char* const* = 0x1e2f780
            ppEnabledLayerNames[0]:         const char* const = "VK_LAYER_LUNARG_screenshot"
            ppEnabledLayerNames[1]:         const char* const = "VK_LAYER_GOOGLE_unique_objects"
            ppEnabledLayerNames[2]:         const char* const = "VK_LAYER_LUNARG_standard_validation"
            ppEnabledLayerNames[3]:         const char* const = "VK_LAYER_LUNARG_core_validation"
            ppEnabledLayerNames[4]:         const char* const = "VK_LAYER_GOOGLE_threading"
            ppEnabledLayerNames[5]:         const char* const = "VK_LAYER_LUNARG_parameter_validation"
            ppEnabledLayerNames[6]:         const char* const = "VK_LAYER_LUNARG_api_dump"
            ppEnabledLayerNames[7]:         const char* const = "VK_LAYER_LUNARG_monitor"
            ppEnabledLayerNames[8]:         const char* const = "VK_LAYER_LUNARG_assistant_layer"
            ppEnabledLayerNames[9]:         const char* const = "VK_LAYER_LUNARG_object_tracker"
            ppEnabledLayerNames[10]:        const char* const = "VK_LAYER_LUNARG_vktrace"
        enabledExtensionCount:          uint32_t = 16
        ppEnabledExtensionNames:        const char* const* = 0x1e296a0
            ppEnabledExtensionNames[0]:     const char* const = "VK_KHR_device_group_creation"
            ppEnabledExtensionNames[1]:     const char* const = "VK_KHR_display"
            ppEnabledExtensionNames[2]:     const char* const = "VK_KHR_external_fence_capabilities"
            ppEnabledExtensionNames[3]:     const char* const = "VK_KHR_external_memory_capabilities"
            ppEnabledExtensionNames[4]:     const char* const = "VK_KHR_external_semaphore_capabilities"
            ppEnabledExtensionNames[5]:     const char* const = "VK_KHR_get_display_properties2"
            ppEnabledExtensionNames[6]:     const char* const = "VK_KHR_get_physical_device_properties2"
            ppEnabledExtensionNames[7]:     const char* const = "VK_KHR_get_surface_capabilities2"
            ppEnabledExtensionNames[8]:     const char* const = "VK_KHR_surface"
            ppEnabledExtensionNames[9]:     const char* const = "VK_KHR_xcb_surface"
            ppEnabledExtensionNames[10]:    const char* const = "VK_KHR_xlib_surface"
            ppEnabledExtensionNames[11]:    const char* const = "VK_EXT_acquire_xlib_display"
            ppEnabledExtensionNames[12]:    const char* const = "VK_EXT_debug_report"
            ppEnabledExtensionNames[13]:    const char* const = "VK_EXT_debug_utils"
            ppEnabledExtensionNames[14]:    const char* const = "VK_EXT_direct_mode_display"
            ppEnabledExtensionNames[15]:    const char* const = "VK_EXT_display_surface_counter"
    pAllocator:                     const VkAllocationCallbacks* = NULL
    pInstance:                      VkInstance* = 0x1e928c0

Something goes wrong the create the VkInstance


#4

Yes, you are enabling layers twice. VK_LAYER_LUNARG_standard_validation is alias (meta-layer) for some of the others.
Besides you should only enable layers you intend to use, no everything.


#5

rocket99ios, as krOoze suggested, please surround your posted code snippets with either [noparse]

...

or

...

[/noparse] so that readers can sift your code more easily. Observe:

[b][noparse]

...

[/noparse][/b]:


    VkImageMemoryBarrier presentToDrawBarrier;
    presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    presentToDrawBarrier.pNext = nullptr;
    presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
    presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
        presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
        presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
    }else{
        presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
        presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    }
    

vs. [b][noparse]

...

[/noparse][/b]:


    VkImageMemoryBarrier presentToDrawBarrier;
    presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    presentToDrawBarrier.pNext = nullptr;
    presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
    presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
        presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
        presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
    }else{
        presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
        presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    }
    

vs.

VkImageMemoryBarrier presentToDrawBarrier;
presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
presentToDrawBarrier.pNext = nullptr;
presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
}else{
presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
}


#6

Tkank you very much. I’ll try to correct my fault.

      • Updated - - -

Thanks for your advice. I’ll have a try.

      • Updated - - -

[QUOTE=Dark Photon;398234]rocket99ios, as krOoze suggested, please surround your posted code snippets with either [noparse]

...

or

...

[/noparse] so that readers can sift your code more easily. Observe:

[b][noparse]

...

[/noparse][/b]:


    VkImageMemoryBarrier presentToDrawBarrier;
    presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    presentToDrawBarrier.pNext = nullptr;
    presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
    presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
        presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
        presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
    }else{
        presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
        presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    }
    

vs. [b][noparse]

...

[/noparse][/b]:


    VkImageMemoryBarrier presentToDrawBarrier;
    presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    presentToDrawBarrier.pNext = nullptr;
    presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
    presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
        presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
        presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
    }else{
        presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
        presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    }
    

vs.

VkImageMemoryBarrier presentToDrawBarrier;
presentToDrawBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
presentToDrawBarrier.pNext = nullptr;
presentToDrawBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
presentToDrawBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
presentToDrawBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
presentToDrawBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
if(TKBaseInfo::Info()->graphicsQueueFamily == TKBaseInfo::Info()->presentQueueFamily){
presentToDrawBarrier.srcQueueFamilyIndex = TKBaseInfo::Info()->graphicsQueueFamily;
presentToDrawBarrier.dstQueueFamilyIndex = TKBaseInfo::Info()->presentQueueFamily;
}else{
presentToDrawBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
presentToDrawBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
}[/QUOTE]

Thanks for your advice, I’ll have a try.