Variable Rate Shading in G3D

48 views
Skip to first unread message
Message has been deleted

Krzysztof Wolski

unread,
Jun 22, 2020, 11:37:43 AM6/22/20
to G3D Users
Hey guys,

Does anyone know how to add Variable Rate Shading (VRS) functionality to G3D?
I am rather new to this engine and I would really appreciate some help or instructions how to do it.

Morgan McGuire

unread,
Jun 22, 2020, 5:18:36 PM6/22/20
to g3d-...@googlegroups.com
You can use any OpenGL feature or extension in G3D. Just glEnable the appropriate call.

G3D does not currently abstract MSAA or VRS in a special way. to use them effectively, just enable those features, and access the underlying OpenGL texture handles from G3D::Texture and G3D::RenderDevice.

Note that if you wish to use the G3D::Renderer interface with VRS then you will have to switch to forward shading mode, as VRS does not work effectively with deferred shading.

-m


--
You received this message because you are subscribed to the Google Groups "G3D Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to g3d-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/g3d-users/8d6f7d8b-42c7-4821-8751-1f85b0e8f5f4o%40googlegroups.com.

Krzysztof Wolski

unread,
Jun 24, 2020, 10:11:56 AM6/24/20
to G3D Users
Thanks for a quick response.
I tried to implement VRS in the starter project. Unfortunately results are weird. VRS works in terms of shading, but rasterization is not decoupled from shading. This results in not sharp edges of obejcts.

Notsharp.png


The right-hand side is rendered with one sample per pixel, and the left-hand side uses 1 sample per 4x4 pixels.


I tried also to do the same in minimalOpenGL project, which does not use G3D lib at all and results are as expected:


Sharp.jpg


Is there any chance you might have an idea what might cause such weird behavior?


Here's my code for VRS:


    GLint srTexelW, srTexelH;

    glGetIntegerv(GL_SHADING_RATE_IMAGE_TEXEL_WIDTH_NV, &srTexelW);

    glGetIntegerv(GL_SHADING_RATE_IMAGE_TEXEL_HEIGHT_NV, &srTexelH);


    GLuint sri;

    glGenTextures(1, &sri);


    GLsizei sri_w = m_framebuffer->width() / srTexelW;

    GLsizei sri_h = m_framebuffer->height() / srTexelH;


    unsigned char* data = (unsigned char*)malloc(sri_w * sri_h * sizeof(unsigned char));


    for (size_t y = 0; y < sri_h; ++y)

    {

        for (size_t x = 0; x < sri_w; ++x)

        {

            if ((float)x / sri_w < 0.5)

                data[x + y * sri_w] = 0;

            else

                data[x + y * sri_w] = 2;

        }

    }


    glBindTexture(GL_TEXTURE_2D, sri);

    glTexStorage2D(GL_TEXTURE_2D, 1, GL_R8UI, sri_w, sri_h);

    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sri_w, sri_h, GL_RED_INTEGER, GL_UNSIGNED_BYTE, data);


    GLint palSize;

    glGetIntegerv(GL_SHADING_RATE_IMAGE_PALETTE_SIZE_NV, &palSize);


    GLenum* palette = new GLenum[palSize];

    for (size_t i = 0; i < palSize; ++i)

    {

        palette[i] = GL_SHADING_RATE_NO_INVOCATIONS_NV;

    }


    palette[0] = GL_SHADING_RATE_1_INVOCATION_PER_4X4_PIXELS_NV;

    palette[1] = GL_SHADING_RATE_1_INVOCATION_PER_2X2_PIXELS_NV;

    palette[2] = GL_SHADING_RATE_1_INVOCATION_PER_PIXEL_NV;

    palette[3] = GL_SHADING_RATE_NO_INVOCATIONS_NV;


    glBindShadingRateImageNV(sri);

    glShadingRateImagePaletteNV(0, 0, palSize, palette);


    delete[] palette;


    glEnable(GL_SHADING_RATE_IMAGE_NV);



Morgan McGuire

unread,
Jun 24, 2020, 5:42:29 PM6/24/20
to g3d-...@googlegroups.com
Did you put DefaultRenderer in forward shading mode?

You will need to enable VRS only around the primary view pass. It must be disabled for the shadow map, AO, post-processing, and other passes.

-m


--
You received this message because you are subscribed to the Google Groups "G3D Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to g3d-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages