Rectangle Texture

From OpenGL Wiki
Jump to navigation Jump to search
Rectangle Texture
Core in version 4.6
Core since version 3.1
ARB extension ARB_texture_rectangle
Vendor extension NV_texture_rectangle


A Rectangle Texture is a Texture that contains a single 2D image with no mipmaps. It has no power-of-two restrictions on its size. Texture coordinates for accessing this texture must be texel values (floating-point), representing texels within the texture, rather than normalized texture coordinates.

Overview[edit]

Rectangle textures are very special texture types. They are two-dimensional, but they are not textures of the type GL_TEXTURE_2D. They have their own separate texture type: GL_TEXTURE_RECTANGLE.

Creation[edit]

Storage for rectangle textures is created in the usual way. Bind the texture to the GL_TEXTURE_RECTANGLE texture target and call an appropriate function that allocates storage. For example:

glTexImage2D(GL_TEXTURE_RECTANGLE, 0, internalformat​, width​, height​, 0, format​, type​, data​);

The named parameters all work as normal for texture image specification.

Use in shaders[edit]

Rectangle textures must use samplers of the form gsampler2DRect or gsampler2DRectShadow, where g represents the type of data the texture stores (float, signed integral, or unsigned integral).

When using rectangle samplers, all texture lookup functions automatically use non-normalized texture coordinates. This means that the values of the texture coordinates span (0..W, 0..H) across the texture, where the (W,H) refers to its dimensions in texels, rather than (0..1, 0..1). For example, the value 12.5 is the center of the 13th texel and the value 0 is the left side of the 1st texel.

Limitations[edit]

Rectangle textures contain exactly one image; they cannot have mipmaps. Therefore, any texture parameters that depend on LODs are irrelevant when used with rectangle textures; attempting to set these parameters to any value other than 0 will result in an error. Similarly, no mipmap filtering is allowed; GL_TEXTURE_MIN_FILTER must be GL_NEAREST or GL_LINEAR. Similarly, they cannot be used with any of the gradient or LOD texture accessing functions.

Also, wrapping modes for each coordinate must be either GL_CLAMP_TO_EDGE or GL_CLAMP_TO_BORDER.

Purpose[edit]

In the days before non-power-of-two textures were available, rectangle textures were the only way to have textures with irregular sizes. This was particularly useful for render targets, which often were intended to be screen-sized images.

From a modern perspective, they seem essentially useless. But even now, they do have some small purpose.

This mainly has to do with their ability to use non-normalized texture coordinates in conjunction with filtering. The texelFetch functions bypass all filtering; they take integer texture coordinates. However, with texture rectangles, you can work directly in texel coordinates, but with floating-point values. This is a useful convenience.

It could certainly be emulated by using the textureSize function to get the size and manually normalize a texture coordinate. But more often than not, rectangle textures will look more natural in the shader code.