Using the Stencil Test

Hello guys,
i am implementing deferred lighting with light-volumes and try to mark only pixels within the light-range using the stencil-test.
I failed so far :slight_smile:
Here is my code:


// Framebuffer with a color + depth/stencil attachment. Color + Depth will be loaded. Stencil will be cleared
loadRenderpass->begin(cmd, frameResources[frameDataIndex].framebuffer);
        {
            // Bind descriptor-set referencing the G-Buffer
            frameResources[frameDataIndex].gBuffer->bind(cmd, pointLightShader.getPipelineLayout());

            // Bind Camera-Descriptor-Set
            camera->bind(cmd, pointLightShader.getPipelineLayout());

            // Bind Index- & Vertex-Buffer
            sphereMesh.bind(cmd);

            for (const auto& pointLight : SceneGraph::getVisiblePointLights())
            {
                // Bind pipeline which writes into the stencil-buffer (THIS DONT WORK)
                lightStencilShader.bind(cmd);

                // Update model-matrix through push-constant
                const Mat4f& worldMat = pointLight->getWorldMatrix();
                vkCmdPushConstants(cmd, lightStencilShader.getPipelineLayout()->get(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(Mat4f), &worldMat);

                // Draw sphere scaled with range of the light. Only Updates the Stencil-Buffer. FS is disabled. Depth-Test is enabled.
                sphereMesh.draw(cmd);

                // Bind point-light shader. Uses the stencil buffer. If value in it is NOT 0 it will execute the FS. (THIS WORKS FINE)
                pointLightShader.bind(cmd);

                // Bind Light-Descriptor-Set
                pointLight->bind(cmd, pointLightShader.getPipelineLayout());

                // Draw sphere scaled with range of the light
                sphereMesh.draw(cmd);
            }

So just the writes from the lightStencilShader with drawing the sphere does not occur. Clearing the Stencil buffer with arbritary-values works fine. I have no plan what kind of information i am missing.
I have found sth interesting in the spec:

The depth bounds test, stencil test, depth test, and occlusion query sample counting are performed before fragment shading if and only if early fragment tests are enabled by the fragment shader (see Early Fragment Tests).

How do i enable early fragment tests?

Thanks in Advance :slight_smile:

AFAIK simply by not forcing it off (e.g. by writing depth in fragment shader).

Or explicitly by layout(early_fragment_tests) in;.

[QUOTE=krOoze;41316]AFAIK simply by not forcing it off (e.g. by writing depth in fragment shader).

Or explicitly by layout(early_fragment_tests) in;.[/QUOTE]

Thanks for the information, but i guess the driver did it anyways.

So to my Stencil-Problem. I recognized that i have to clear the stencil buffer after every light of course. But this means i have to use one render-pass instance for every light. Isnt that to expensive?

So to my Stencil-Problem. I recognized that i have to clear the stencil buffer after every light of course. But this means i have to use one render-pass instance for every light. Isnt that to expensive?

This is why people don’t use stencil shadows. Well, this is part of why they don’t. As annoying as depth shadows may be to implement, they’re easier to work with overall.

I am not talking about shadows. I want to limit the amount of pixels on where i have to do the lighting calculations. In other words: I try to implement this: OpenGL Step by Step - OpenGL Development

I figured out why the stencil writes didnt work. I had the rasterizerDiscardEnable set to TRUE in the stencil-pipeline. I didnt know that this disables the stencil-test.
But yea using a render-pass for just one light is more inefficient than just one render-pass for all lights. I will remove this technique then.

In other words: I try to implement this: OpenGL Step by Step - OpenGL Development

It would be so much faster to just do tile-deferred rendering.

Yes i know :slight_smile: And i will implement this in the future, but i wanted to try out the stencil-test and this was a good opportunity.