sub-ranged texture() in GLSL
The basic idea of this suggestion is to make texture atlasing less awful to do. Create (another!) texture function of the form:
texture(sampler2D T, vec2 p, vec4 rect)
which does the sampling restricted to rect (encoded as .xy=position withing texture and .zw=size within texture) i.e. the texture coordinate p is relative to rect. Without filtering, it is equivalent to
texture(T, p, rect) == texture(T, rect.xy + rect.zw*p)
with filtering is where it is useful, texels on the boundary of rect get filtered with the texel on the opposite size for repeat, and with itself for mirror repeat, mirror and clamp. Extending, I want this:
texture(sampler2D T, vec2 p, vec4 rect, enum RepeatMode mode);
this does fit in with mipmapping and gives what you want IF both:
- the texel-size of rect is a power of 2 AND
- the boundary of rect is a multiple of the size of rect
Naturally this above can be logically extended to textureLod, textureGrad, textureProj, textureProjGrad, texture arrays, 1D textures, 3D textures, and texture rectangles. [3D textures will needs that the rect is a cube].
As a side note, it is debatable if it is also worth adding another enumeration argument stating if the filtering is linear or nearest. [mipmap filtering is essentially handled by LOD, though one can argue the filtering between mip maps also needs to be specified].
One can naturally emulate this doing the filtering by hand in shader using texelFetch, but that seems like an unnecessary burden on a developer and a potential optimization lost on the hardware.