Weird artifacts with multi layered terrain

Started by
12 comments, last by Ardan 10 years, 1 month ago
Maybe this has to do with mipmapping. The hardware selects the mipmap level based on the texture coordinates used in 2x2 pixel groups. This means that it only works, if all the pixels of a 2x2 group sample the same textures. This is exactly not the case for the regions where the artifacts occure.

Maybe, just for testing this hypothesis, you can always sample and mix all textures and set the scaling factor to zero for those that you don't need.


Edit: Like so (Note that this could also be done without branches):

void main(void)
{
    const vec3 light = vec3(0.5, 1, 0.5);
	shade = dot(light, normalize(normal));

    const float fRange1 = 0.01f;
    const float fRange2 = 0.1f;
    const float fRange3 = 0.5f;
    const float fRange4 = 0.9f;

    float fScale = position.y/TerrainHeight;
 
    float ScaleSand = 0.0f;
    float ScaleGrass = 0.0f;
    float ScaleRock = 0.0f;


    if(fScale >= 0.0 && fScale <= fRange1)
        ScaleSand = 1.0f;
    else if(fScale <= fRange2)
    {
        fScale -= fRange1;
        fScale /= (fRange2-fRange1);

        float fScale2 = fScale;
        fScale = 1.0-fScale;

        ScaleSand = fScale;
        ScaleGrass = fScale2;

    }
    else if(fScale <= fRange3)
        ScaleGrass = 1.0f;
    else if(fScale <= fRange4)
    {
        fScale -= fRange3;
        fScale /= (fRange4-fRange3);

        float fScale2 = fScale;
        fScale = 1.0-fScale;

        ScaleGrass = fScale;
        ScaleRock = fScale2;
    }
    else
        ScaleRock = 1.0f;
 
    texColor = texture(sandTexture, texCoord) * ScaleSand +
			   texture(grassTexture, texCoord) * ScaleGrass +
			   texture(rockTexture, texCoord) * ScaleRock;

    out_Color = texColor * shade;

}
Advertisement

I was thinking numerical issue too, but not seeing where one could exist based off his code. The values for the ranges are constants on top and seem to be well defined.

I also looked for division by 0 but it seems impossible as well.

Still, a clamp would not hurt.


Also, provide a screenshot of a close-up of one of the artifacts. I need to see one of them up close to get an idea of what type of numerical issue it could be.


L. Spiro

If you zoom in on the image you can see that the errors are not contiguous. It also looks like they might be in 2x2 blocks of pixels - most noticeable at the start of the grass to rock transition. There also seems to be two separate lines in the sand.
Thinking more on this, I'm beginning to suspect you might have found a driver bug.

Fragments are rendered as screen aligned quads (2x2 pixels at a time) because it helps optimize texture access, derivatives calculation, z-culling and other things besides. If you zoom in on the errors in the image, you can see that they are in 2x2 pixel quads - the quads also appear aligned on even boundaries which also fits.

I'm wondering if the errors only appear when the four pixels in a quad do not all take the same execution path through the shader. When comfortably within the same range all the quad's pixels will pass/fail the same tests and so take the same path. If some pixels in a quad fall within different ranges they will take different execution paths - which might be the cause of the problem. This would explain why it works most of the time but only fails at the transition between ranges.

The fact that the errors don't always appear might also fit this explanation. Think of a situation where the transition between the layers occurs as a horizontal line. When the line and quads are aligned such that this line passes through a single quad, the pixels will fall within different ranges and follow different execution paths. If the horizontal line falls exactly between two quads, both quads will have all their pixels in the same range/execution path even though the two quads are in different ranges.

Try to rewrite the shader so all pixels follow the same execution paths regardless of the ranges they fall in. (Maybe try Ohforf sake's suggestion of always blend all the textures as a first quick test as this does seem to be texture related).

Thanks for your suggestions, I will look into all you've said and I'll post back with my results.

This topic is closed to new replies.

Advertisement