简体   繁体   中英

Vulkan Instanced Rendering Weird Depth Buffer Behaviour

I am rendering a single mesh test object that contains 8000 cubes, 1 monkey, and 1 plane. I checked normals and winding orders. all seems correct for mesh.

if I render a single instance, the depth buffer works correctly. if I render using instanced when instant number increase depth buffer doesn't work correctly.

//Depth Attachment
        //Create Depth Image
        depthImage = new Image(
            physicalDevice_,
            logicalDevice_,
            VK_IMAGE_TYPE_2D,
            depthFormat,
            VK_IMAGE_TILING_OPTIMAL,
            swapChainExtent.width,
            swapChainExtent.height,
            1,
            sampleCount_, //do we really need to sample depth????
            VK_IMAGE_LAYOUT_UNDEFINED,
            VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
            VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);

        depthImageView = new ImageView(
            logicalDevice_,
            VK_IMAGE_VIEW_TYPE_2D,
            depthImage->handle,
            depthFormat,
            1,
            VK_IMAGE_ASPECT_DEPTH_BIT);

//Render pass Depth Attachment
        VkAttachmentDescription depthAttachment{};
        depthAttachment.format = depthFormat;
        depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
        depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
        depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
        depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
        depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
        depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

//Graphics Pipeline 
        VkPipelineDepthStencilStateCreateInfo depthStencil{};
        depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
        depthStencil.depthTestEnable = VK_TRUE;
        depthStencil.depthWriteEnable = VK_TRUE;
        depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
        depthStencil.depthBoundsTestEnable = VK_FALSE;
        depthStencil.minDepthBounds = 0.0f; // Optional
        depthStencil.maxDepthBounds = 1.0f; // Optional
        depthStencil.stencilTestEnable = VK_FALSE;
        depthStencil.front = {}; // Optional
        depthStencil.back = {};  // Optional


Below the screen is the view from the 100th instance of that mesh. The higher the instance number, the more the depth buffer deteriorates. The result is still the same if I use a different model.

What am I missing?

Result: 结果 Depth Buffer: 深度缓冲区

For those who may struggle with similar issue. I found a solution.

Actually, this issue is not related to instancing directly but it's a bit confusing issue becomes visible as the instance index increases.

the cause is projection matrix z-near value was so small.

...
fieldOfView = 65.0f;
znear = 0.001f;       // ! this much small value cause this problem
zfar = 5000.0f;
...

changed z-near to an acceptable value. Problem solved.

...
fieldOfView = 65.0f;
znear = 0.1f;       
zfar = 5000.0f;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM