Model 3 texturing
This one is for Harry, since it was his suggestion quite some time ago that we add this code. But I never added it previously because it didn't make sense to add it so early on in the renderer development.
To start off
What is this ugly line in the sky in daytona?

To understand this problem we first need to understand how textures are repeated over a polygon. In OpenGL texture coordinates go from 0 -> 1
0 is the start of a texture, and 1 is the end. If values exceed 1, they can be wrapped, mirrored, or clamped. Something like this

Most of the time people use GL_REPEAT or GL_CLAMP_TO_EDGE. GL_CLAMP_TO_EDGE stops the textures wrapping around and 'bleeding' on the edges.
Here is an early version of supermodel, where you can see this edge bleeding all over the place.

If textures start at 0, and end at 1, and the values are always in between these, why do we get this edge bleeding at all? To understand this problem, let's have a closer look at how textures are sampled with gl_repeat.

A bilinear filter always samples 2 adjacent pixels, and does a linear interpolation between them. At the edge, or texture coordinate 0, the first pixel will actually be sampled from the other side of the texture.

Okay, so what is the issue here? The problem is the model3 has a unique texture mode, which is a combination of GL_CLAMP_TO_EDGE and GL_REPEAT. This texture mode doesn't exist on modern hardware. So this means textures repeat over a polygon, but on the edges there is no texture bleeding.
Previously I solved the issue by stretching the texture coordinates half a pixel inwards, to prevent it sampling the edges. Okay then why did the seem still exist in Daytona? The answer is because half a pixel shift, is only half a pixel in the base texture. Each mipmap is the same texture but half the resolution. Half a texel shift of the base texture, is not half a texel shift in the mipmap, so the seem appears again, and gets worse with each mipmap level.
In order to solve this problem I had to re-implement bilinear and trilinear filtering from scratch inside the shader, with our custom wrap modes
This is the result, perfect texturing.

Nice, no more seem!
Harry also noticed some quite fundamental differences in how alpha (or contour textures as they are known on the model3) work. Alpha textures are textures with 1 bit alpha. So they are either fully opaque, or 100% transparent.

To sample a texture with bilinear filtering, it's actually an average of 4 pixels. And the weight of each pixel depends how far into the pixel it is. This means at the edge of a opaque section, the edge pixels will actually average together with say the background colour in the texture. Quite a common issue with alpha texturing. An example here

Suddenly white edges appear. If the rest of the transparent texture was black, black edges would appear. The real3d guys actually had a solution in hardware, that modified how bilinear filtering worked.
The theory behind it is here
https://blog.ostermiller.org/dilate-and-erode
This was Harry's description
So what is happening is they are copying the colour values to the adjacent pixels, to stop the colour bleed from the non transparent sections. Quite a clever solution. I've never actually seen this before. Again modern hardware doesn't do this, or at least not in the fixed function hardware. We can emulate this in our shaders though.
The algorithm modifies the bilinear filter to do something like this

Anyway with this algorithm textures look perfect as they do on the model 3. There is no colour bleed from the adjacent pixels.

To start off
What is this ugly line in the sky in daytona?

To understand this problem we first need to understand how textures are repeated over a polygon. In OpenGL texture coordinates go from 0 -> 1
0 is the start of a texture, and 1 is the end. If values exceed 1, they can be wrapped, mirrored, or clamped. Something like this

Most of the time people use GL_REPEAT or GL_CLAMP_TO_EDGE. GL_CLAMP_TO_EDGE stops the textures wrapping around and 'bleeding' on the edges.
Here is an early version of supermodel, where you can see this edge bleeding all over the place.

If textures start at 0, and end at 1, and the values are always in between these, why do we get this edge bleeding at all? To understand this problem, let's have a closer look at how textures are sampled with gl_repeat.

A bilinear filter always samples 2 adjacent pixels, and does a linear interpolation between them. At the edge, or texture coordinate 0, the first pixel will actually be sampled from the other side of the texture.

Okay, so what is the issue here? The problem is the model3 has a unique texture mode, which is a combination of GL_CLAMP_TO_EDGE and GL_REPEAT. This texture mode doesn't exist on modern hardware. So this means textures repeat over a polygon, but on the edges there is no texture bleeding.
Previously I solved the issue by stretching the texture coordinates half a pixel inwards, to prevent it sampling the edges. Okay then why did the seem still exist in Daytona? The answer is because half a pixel shift, is only half a pixel in the base texture. Each mipmap is the same texture but half the resolution. Half a texel shift of the base texture, is not half a texel shift in the mipmap, so the seem appears again, and gets worse with each mipmap level.
In order to solve this problem I had to re-implement bilinear and trilinear filtering from scratch inside the shader, with our custom wrap modes

Nice, no more seem!
Harry also noticed some quite fundamental differences in how alpha (or contour textures as they are known on the model3) work. Alpha textures are textures with 1 bit alpha. So they are either fully opaque, or 100% transparent.

To sample a texture with bilinear filtering, it's actually an average of 4 pixels. And the weight of each pixel depends how far into the pixel it is. This means at the edge of a opaque section, the edge pixels will actually average together with say the background colour in the texture. Quite a common issue with alpha texturing. An example here

Suddenly white edges appear. If the rest of the transparent texture was black, black edges would appear. The real3d guys actually had a solution in hardware, that modified how bilinear filtering worked.
The theory behind it is here
https://blog.ostermiller.org/dilate-and-erode
This was Harry's description
What is happening here, instead, is that the edge texels "bleed" toward the "outer", transparent, texture area copying their RGB values there, while leaving the alpha channel intact. Infact the texture edge is cut in respect of the original alpha value, while its color is replicated from the "inner" texels.
This fixes, with the appropriate contour threshold, all the ugly white/black borders (but even some colored ones) in contour textures.
So what is happening is they are copying the colour values to the adjacent pixels, to stop the colour bleed from the non transparent sections. Quite a clever solution. I've never actually seen this before. Again modern hardware doesn't do this, or at least not in the fixed function hardware. We can emulate this in our shaders though.
The algorithm modifies the bilinear filter to do something like this

Anyway with this algorithm textures look perfect as they do on the model 3. There is no colour bleed from the adjacent pixels.
