More efficient use of Texture.bind() when rendering terrain patches?

Hey peeps!

I’m currently using LWJGL to write a terrain renderer for an upcoming project, and wanted to ask a bit of a newbie question about texture binding and performance.

The Set-Up:
Here’s a quick schematic of how my terrain works at present:

I load in a greyscale height map of 64 x 64 pixels, where each pixel represents a terrain quad of a set height. The entire data set is split into 16 patches each containing 16 x 16 (256) individual quads.

The Problem:

At runtime, I scan the entire set of terrain patches (16 in total), and work out which ones are in the view frustum (shown in green in the left part of the diagram, from the perspective of the yellow player marker).

For each of these patches in view, I then cycle through each of its individual quads (i.e. the right portion of the diagram) and render them at the required position. No frustum culling is done on these to minimise the amount of view-calculations I’m doing at run time. All of these objects are rendered as VBOs.

My problem is with texture binding. You’ll see that in any single patch of terrain, I might have two (or potentially more) different textures to apply. If it was one texture, I’d just do a Texture.bind() once for each patch (or maybe just once per render cycle, until it needs changing for something else), but when there’s more than one I’m starting to wonder about the best way to optimise it.

Would a solution be to map out a list of indices for each unique texture to be rendered in any given patch, then cycle through each one - rendering as I go?

So, each patch could have the following code:


// Index arrays for each unique type of texture
boolean[] texIndex1 = new boolean[16*16];
boolean[] texIndex2 = new boolean[16*16];

protected void resetIndices()
{
    for (int i = 0; i < (16*16); i++) { texIndex1[i] = false; texIndex2[i] = false; }
}

protected void setupIndices()
{
    // N.B. These values will be set during the initial terrain load
    // Obviously there would be 'true' values for all 256 quads, but for now
    // I'm just using 8 as an example!
    texIndex1[14] = true;
    texIndex1[18] = true;
    texIndex1[25] = true;
    texIndex1[29] = true;

    texIndex2[6] = true;
    texIndex2[8] = true;
    texIndex2[11] = true;
    texIndex2[21] = true;
}

protected void renderPatch()
{
    // bind the first texture
    texture1.bind();
    for (int i = 0; i < (16*16); i++)
    {
        if (texIndex1[i])
        {
            // render the quad VBO object
        }
    }

    texture2.bind();
    for (int i = 0; i < (16*16); i++)
    {
        if (texIndex2[i])
        {
            // render the quad VBO object
        }
    }
}


Am I thinking along the right lines here? Or should I simply switch texture bindings when needed?


for (int i = 0; i < (16*16); i++)
{
    // do we need to use texture 1, but are currently using texture 2?
    if (texIndex1[i] && texActive2)
    {
        texActive2 = false;
        texActive1 = true;
        texture1.bind();
    }

    // do we need to use texture 2, but are currently using texture 1?
    if (texIndex2[i] && texActive1)
    {
        texActive1 = false;
        texActive2 = true;
        texture2.bind();
    }

    // render the quad VBO object
}

If someone could provide some insight into whether my ideas above are along the right lines, I’d be really grateful! I might be doing something dreadfully wrong here!

Thanks!

You could create a list of geometry for each texture in each patch. Instead of looping sequentially through the patch and drawing everything in order, you’d draw everything that uses texture 0 first, then everything that uses texture 1, etc. You’ll need some code to assign each object/tile/whatever to its texture when you start the game, but that’s about it. Doing that should give you a maximum number of texture binds per patch equal to the number of unique textures the patch uses. You could even go one step further and loop over all visible patches and draw everything that passed the frustum test that uses texture 0, then all geometry that uses texture 1, etc which would keep the number of texture binds constant regardless of how much you draw or how many patches are visible.

This was really useful, thanks!

I ended up creating three render loops - one for each renderable texture type (one for the sides of the terrain, two for the tops).

Thanks for the tip!