Results 1 to 2 of 2

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

  1. #1

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

    Code:
    void TKScene::updateDrawCommand(){
    //TKLog("update command image indice %d\n", 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!\n");
    }

    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!\n");
    }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!\n");
    }
    //TKLog("end update command buffer\n");
    }

    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\n", result);
    }while(result != VK_SUCCESS);
    //TKLog("frame idx: %d, current idx = %d\n", 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!\n");
    }

    //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!\n");
    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. #2
    Senior Member
    Join Date
    Mar 2016
    Location
    vulkan.hys.cz
    Posts
    358
    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 [CODE]blah[/CODE] (or use the button in advanced mode), so the code is readable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean