Let's say I have some shader program in DirectX or OpenGL rendering a full screen quad. And in a pixel/fragment shader I sample some huge textures at random texture coordinates. That is one same texture coordinate for all texture samplings in one shader invocation, but it is various among different shader invocations. These fetch operations produce performance drop, I even think that due to the size of the textures the GPU texture cache is not big enough and is used not efficiently.

Now I have a theoretical question: can I optimize the performance by using some low-resolution like 32x32 mask textures, which are built by mipmapping the large textures, and if a value in a mask texture at given texture coordinate at some higher mip level is not appropriate, then I don't need to perform texture fetches at full-size level 0? Something like this in HLSL (GLSL code is pretty similar, but there is no [branch] attribute):

float2 tc = calculateTexCoordinates();
bool performHeavyComputations = testValue(largeMipmappedTexture.SampleLevel(sampler, tc, 5));

float result = 0;

if (performHeavyComputations)
    result += largeMipmappedTexture.SampleLevel(sampler, tc, 0);

About 50% of texels at mip level 5 will not pass the test. And so a lot of shader invocations should not sample the full-size textures.

But I am introducing branching in the code. May this branching hurt the performance even worse than sampling the full-size texture even if that is not needed? Different GPUs may behave differently, some may not even support branching, will they perform two fetches instead of one?

I can test this code on some machines later, but my question is theoretical.

And can you suggest another optimizations, if this won't work properly ?

  • $\begingroup$ I think this is a viable optimization that is not uncommon. For example look at a similar technique that was used in Killzone 4. Check the "Killzone Shadow Fall Demo Postmortem" starting from page 77. Slideshare link: slideshare.net/guerrillagames/… In the same vein, people use an early out test in their shaders to optimize things. For example a common example is skipping sky pixels for SSAO, if the pixel's depth exceeds a certain value. $\endgroup$
    – Tara
    Oct 3, 2023 at 7:12


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.