2
\$\begingroup\$

I am trying to render a .obj model with Vulkan 1.4. The object is rotated with a quaternion over time. The "front" geometry of the model is rendered as expected, but when the model rotates and shows the back, some parts of the geometry from the front is peeking through the model. I have tried a model with a texture, and the back of the model seems transparent.

The VkPipeline i have setup follow the one in the depth buffering code from the Khronos Vulkan-tutorial 1

VkPipelineShaderStageCreateInfo shader_stages[info->shader_count];
for (size_t i = 0; i < info->shader_count; ++i) {
    shader_stages[i] = (VkPipelineShaderStageCreateInfo) {
        .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
        .stage = (VkShaderStageFlagBits)info->shaders[i]->module.shader_stage,
        .module = info->shaders[i]->handle,
        .pName = "main",
    };
}

const VkPipelineVertexInputStateCreateInfo vertex_input = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
};

const VkPipelineInputAssemblyStateCreateInfo input_assembly = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
    .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
    .primitiveRestartEnable = VK_FALSE,
};

const VkPipelineViewportStateCreateInfo viewport = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
    .viewportCount = 1,
    .scissorCount = 1,
};

const VkPipelineRasterizationStateCreateInfo rasterization = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
    .depthClampEnable = VK_FALSE,
    .rasterizerDiscardEnable = VK_FALSE,
    .polygonMode = VK_POLYGON_MODE_FILL,
    .cullMode = VK_CULL_MODE_BACK_BIT,
    .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
    .lineWidth = 1.f,
    .depthBiasEnable = VK_FALSE,
};

const VkPipelineMultisampleStateCreateInfo multisampling = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
    .sampleShadingEnable = VK_FALSE,
    .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
    .minSampleShading = 0.0f,
    .pSampleMask = NULL,
    .alphaToCoverageEnable = VK_FALSE,
    .alphaToOneEnable = VK_FALSE,
};

const VkPipelineDepthStencilStateCreateInfo depth_stencil = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
    .depthTestEnable = VK_TRUE,
    .depthWriteEnable = VK_TRUE,
    .depthCompareOp = VK_COMPARE_OP_GREATER_OR_EQUAL,
    .depthBoundsTestEnable = VK_FALSE,
    .stencilTestEnable = VK_FALSE,
};

const VkPipelineColorBlendAttachmentState color_blend = {
    .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT
        | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
    .blendEnable = VK_FALSE,
};

const VkPipelineColorBlendStateCreateInfo color_blending = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
    .logicOpEnable = VK_FALSE,
    .logicOp = VK_LOGIC_OP_COPY,
    .attachmentCount = 1,
    .pAttachments = &color_blend,
    .blendConstants[0] = 0.f,
    .blendConstants[1] = 0.f,
    .blendConstants[2] = 0.f,
    .blendConstants[3] = 0.f,
};

const VkDynamicState dynamic_states[] = {
    VK_DYNAMIC_STATE_VIEWPORT,
    VK_DYNAMIC_STATE_SCISSOR,
};

const VkPipelineDynamicStateCreateInfo dynamic_state = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
    .dynamicStateCount = ARRAY_SIZE(dynamic_states),
    .pDynamicStates = dynamic_states,
};

const VkPipelineRenderingCreateInfo rendering = {
    .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
    .colorAttachmentCount = 1,
    .pColorAttachmentFormats = &info->color,
    .depthAttachmentFormat = VK_FORMAT_D32_SFLOAT,
};

const VkGraphicsPipelineCreateInfo pipeline_info = {
    .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
    .pNext = &rendering,
    .stageCount = ARRAY_SIZE(shader_stages),
    .pStages = shader_stages,
    .pVertexInputState = &vertex_input,
    .pInputAssemblyState = &input_assembly,
    .pViewportState = &viewport,
    .pRasterizationState = &rasterization,
    .pMultisampleState = &multisampling,
    .pDepthStencilState = &depth_stencil,
    .pColorBlendState = &color_blending,
    .pDynamicState = &dynamic_state,
    .layout = pipeline->layout,
    .renderPass = NULL,
    .basePipelineHandle = VK_NULL_HANDLE,
};

The colour attachment is using the format VK_FORMAT_R8G8B8A8_SRGB and the depth VK_FORMAT_D32_SFLOAT when rendering with VkRenderingInfo.

Here is the front of a coloured Suzanne, you can see the ear appearing through the face on the right

enter image description here

And the back, with the geometry from the face peeking through.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Your culling and depth testing code looks reasonable at a glance, but it looks like somehow you're getting z-fighting between the far ear and the face. That could happen if your projection matrix / vertex shader is flattening the depth during projection, or if your depth buffer is lacking enough bits to adequately distinguish between these surfaces. I see where you set the colour attachment format, but I don't see where you set your depth attachment? \$\endgroup\$ Commented Jun 10 at 13:18
  • \$\begingroup\$ Do I need a depth attachment if I'm not doing anything with the depth image? I'm using cglm for the projection matrix, computed with glm_perspective(glm_rad(60.f), aspect, 0.1f, 100.f, proj); and proj[1][1] *= -1; \$\endgroup\$ Commented Jun 10 at 13:27
  • \$\begingroup\$ I added .depthAttachmentFormat = VK_FORMAT_D32_SFLOAT to the VkPipelineRenderingCreateInfo struct in the pipeline creation, but the issue is still there. \$\endgroup\$ Commented Jun 10 at 13:35
  • \$\begingroup\$ And now I added a depth attachment to VkRenderingInfo::pDepthAttachment with the same format, I get a slightly different result. The issue around the ear in the front coloured suzanne screenshot is not present anymore, but I still see the details from the face peeking through the back of the model \$\endgroup\$ Commented Jun 10 at 17:21

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.