0
votes

I have my Vertex data defined like this:

struct Vertex{
    glm::vec4 pos;
    glm::vec2 texcoord;
    glm::vec2 texcoordex;
    float alpha;
    float idx;
};

my problem is: vertex buffer has read all data, but vertex shader only gets the first one pos as input, others are 0.0f

vertex shader input

layout (location = 0) in vec4 pos;
layout (location = 1) in vec2 attr;
layout (location = 2) in vec2 attrex;
layout (location = 3) in float alph;
layout (location = 4) in float id;

some code snippets

VkVertexInputBindingDescription vertex_input_bindings{
.binding = 0,
.stride = sizeof(Vertex),
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
};

VkVertexInputAttributeDescription vertex_input_attributes[5]{
{
.binding = 0,
.location = 0,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.offset = offsetof(Vertex, pos),
},
{
.binding = 0,
.location = 1,
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = offsetof(Vertex, texcoord),
},
{
.binding = 0,
.location = 2,
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = offsetof(Vertex, texcoordex),
},
{
.binding = 0,
.location = 3,
.format = VK_FORMAT_R32_SFLOAT,
.offset = offsetof(Vertex, alpha),
},
{
.binding = 0,
.location = 4,
.format = VK_FORMAT_R32_SFLOAT,
.offset = offsetof(Vertex, idx),
}};

VkPipelineVertexInputStateCreateInfo vertexInputInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = nullptr,
.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &vertex_input_bindings,
.vertexAttributeDescriptionCount = 5,
.pVertexAttributeDescriptions = vertex_input_attributes,
};

VkDeviceSize offset = 0;
vkCmdBindVertexBuffers(render.cmdBuffer_[bufferIndex], 0, 1, &buffers.vertexBuf_, &offset);

How can I solve the problem? Thanks for any help.


update


I get some information from Renderdoc by capturing frame. The result shows that vertex shader doesn't output correct data.(The vertex shader is a pass.) But Renderdoc shows that vertex buffer has correct data. Meanwhile, the vertex shader output varies each run, and never gets right. Is it a sync issue? Where might the mistake be.

vertex shader data from Renderdoc

// Draw one frame
bool VulkanDrawFrame(void) {
    uint32_t nextIndex;
    // Get the framebuffer index we should draw in
    CALL_VK(vkAcquireNextImageKHR(device.device_, swapchain.swapchain_,
                                  UINT64_MAX, render.semaphore_, VK_NULL_HANDLE,
                                  &nextIndex));
    CALL_VK(vkResetFences(device.device_, 1, &render.fence_));

    VkPipelineStageFlags waitStageMask =
        VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
                                .pNext = nullptr,
                                .waitSemaphoreCount = 1,
                                .pWaitSemaphores = &render.semaphore_,
                                .pWaitDstStageMask = &waitStageMask,
                                .commandBufferCount = 1,
                                .pCommandBuffers = &render.cmdBuffer_[nextIndex],
                                .signalSemaphoreCount = 0,
                                .pSignalSemaphores = nullptr};
    CALL_VK(vkQueueSubmit(device.queue_, 1, &submit_info, render.fence_));
    CALL_VK(
        vkWaitForFences(device.device_, 1, &render.fence_, VK_TRUE, 100000000));

    LOGI("Drawing frames......");

    VkResult result;
    VkPresentInfoKHR presentInfo{
        .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
        .pNext = nullptr,
        .swapchainCount = 1,
        .pSwapchains = &swapchain.swapchain_,
        .pImageIndices = &nextIndex,
        .waitSemaphoreCount = 0,
        .pWaitSemaphores = nullptr,
        .pResults = &result,
        };
    vkQueuePresentKHR(device.queue_, &presentInfo);
    return true;
}


    // Create the pipeline
    VkGraphicsPipelineCreateInfo pipelineCreateInfo{
        .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
        .pNext = nullptr,
        .flags = 0,
        .stageCount = 2,
        .pStages = shaderStages,
        .pVertexInputState = &vertexInputInfo,
        .pInputAssemblyState = &inputAssemblyInfo,
        .pTessellationState = nullptr,
        .pViewportState = &viewportInfo,
        .pRasterizationState = &rasterInfo,
        .pMultisampleState = &multisampleInfo,
        .pDepthStencilState = nullptr,
        .pColorBlendState = &colorBlendInfo,
        .pDynamicState = &dynamicStateInfo,
        .layout = gfxPipeline.layout_,
        .renderPass = render.renderPass_,
        .subpass = 0,
        .basePipelineHandle = VK_NULL_HANDLE,
        .basePipelineIndex = 0,
    };

  VkResult pipelineResult = vkCreateGraphicsPipelines(
      device.device_, gfxPipeline.cache_, 1, &pipelineCreateInfo, nullptr,
      &gfxPipeline.pipeline_);
1
Verify Vulkan is seeing what you think it is seeing with VK_LAYER_LUNARG_api_dump layer. Since your code is some kind of exerpt, I do not see e.g. that you use vertexInputInfo correctly; the lifetime of the arrays it references must extend all the way to the point the vkCreateGPipelines is called. Otherwise code seems OK to me; might always be a sync issue. - krOoze
Thank you for answer. I update the problem and give more detail. I get some information from Renderdoc by capturing frame. The result shows that vertex shader doesn't output correct data.(The vertex shader is a pass.) But Renderdoc shows that vertex buffer has correct data. Meanwhile, the vertex shader output varies each run, and never gets right. Is it a sync issue? Where might the mistake be. - Arti
From the updated code I still don't see if this could be a memory issue or not. Are you sure the VkVertexInputAttributeDescriptions were not destroyed already at the point the vkCreateGraphicsPipelines is called? - krOoze
Thank you for your help, and I have sovled this. The driver optimizes useless inputs. I delete operations concerning to them to simplify the debug, so the driver finds they are useless. - Arti
Ah, makes sense. Please make a formal answer here then so it is useful for others. - krOoze

1 Answers

0
votes

I have sovled this. The driver optimizes useless inputs. I delete operations concerning to them to simplify the debug, so the driver finds they are useless.