• No results found

Texture Minification

In document glspec31.20090324.pdf (Page 157-166)

3.8 Texturing

3.8.8 Texture Minification

Applying a texture to a primitive implies a mapping from texture image space to framebuffer image space. In general, this mapping involves a reconstruction of the sampled texture image, followed by a homogeneous warping implied by the mapping to framebuffer space, then a filtering, followed finally by a resampling of the filtered, warped, reconstructed image before applying it to a fragment. In the GL this mapping is approximated by one of two simple filtering schemes. One

3.8. TEXTURING 145

of these schemes is selected based on whether the mapping from texture space to framebuffer space is deemed tomagnifyorminifythe texture image.

Scale Factor and Level of Detail

The choice is governed by a scale factorρ(x, y)and thelevel-of-detailparameter

λ(x, y), defined as

λbase(x, y) = log2[ρ(x, y)] (3.17)

λ0(x, y) =λbase(x, y) +clamp(biastexobj+biasshader) (3.18)

λ=       

lodmax, λ0 > lodmax

λ0, lodmin ≤λ0≤lodmax

lodmin, λ0 < lodmin

undef ined, lodmin > lodmax

(3.19)

biastexobj is the value ofTEXTURE LOD BIASfor the bound texture object (as de-

scribed in section3.8.5). biasshader is the value of the optional bias parameter

in the texture lookup functions available to fragment shaders. If the texture access is performed in a fragment shader without a provided bias, or outside a fragment shader, thenbiasshader is zero. The sum of these values is clamped to the range [−biasmax, biasmax]where biasmax is the value of the implementation defined

constantMAX TEXTURE LOD BIAS.

Ifλ(x, y)is less than or equal to the constantc(see section3.8.9) the texture is said to be magnified; if it is greater, the texture is minified. Sampling of minified textures is described in the remainder of this section, while sampling of magnified textures is described in section3.8.9.

The initial values oflodmin andlodmax are chosen so as to never clamp the

normal range ofλ. They may be respecified for a specific texture by callingTex- Parameter[if] with pname set toTEXTURE MIN LOD orTEXTURE MAX LOD re- spectively.

Lets(x, y)be the function that associates anstexture coordinate with each set of window coordinates(x, y)that lie within a primitive; definet(x, y)andr(x, y)

3.8. TEXTURING 146 analogously. Let u(x, y) = ( wt+δu, rectangular texture wt×s(x, y) +δu, otherwise v(x, y) = ( ht+δu, rectangular texture ht×t(x, y) +δv, otherwise w(x, y) =dt×r(x, y) +δw (3.20)

wherewt,ht, anddtare as defined by equation3.16withws,hs, anddsequal to

the width, height, and depth of the image array whose level islevelbase. For a one-

dimensional or one-dimensional array texture, definev(x, y) = 0andw(x, y) = 0; for a two-dimensional, two-dimensional array, rectangular, or cube map texture, definew(x, y) = 0.

(δu, δv, δw) are the texel offsets specified in the OpenGL Shading Language

texture lookup functions that support offsets. If the texture function used does not support offsets, all three shader offsets are taken to be zero. If any of the offset values are outside the range of the implementation-defined values

MIN PROGRAM TEXEL OFFSETandMAX PROGRAM TEXEL OFFSET, results of the texture lookup are undefined.

For a polygon or point, ρis given at a fragment with window coordinates(x, y)

by ρ= max    s ∂u ∂x 2 + ∂v ∂x 2 + ∂w ∂x 2 , s ∂u ∂y 2 + ∂v ∂y 2 + ∂w ∂y 2    (3.21) where∂u/∂xindicates the derivative ofuwith respect to windowx, and similarly for the other derivatives.

For a line, the formula is

ρ= s ∂u ∂x∆x+ ∂u ∂y∆y 2 + ∂v ∂x∆x+ ∂v ∂y∆y 2 + ∂w ∂x∆x+ ∂w ∂y∆y 2 l, (3.22) where∆x = x2 −x1 and∆y = y2 −y1 with (x1, y1) and(x2, y2) being the segment’s window coordinate endpoints andl=p∆x2+ ∆y2.

While it is generally agreed that equations3.21and3.22give the best results when texturing, they are often impractical to implement. Therefore, an imple-

3.8. TEXTURING 147

mentation may approximate the idealρ with a functionf(x, y) subject to these conditions:

1. f(x, y) is continuous and monotonically increasing in each of |∂u/∂x|, |∂u/∂y|,|∂v/∂x|,|∂v/∂y|,|∂w/∂x|, and|∂w/∂y|

2. Let mu = max ∂u ∂x , ∂u ∂y mv = max ∂v ∂x , ∂v ∂y mw= max ∂w ∂x , ∂w ∂y . Thenmax{mu, mv, mw} ≤f(x, y)≤mu+mv+mw.

Coordinate Wrapping and Texel Selection

After generatingu(x, y),v(x, y), andw(x, y), they may be clamped and wrapped before sampling the texture, depending on the corresponding texture wrap modes. Let u0(x, y) = ( u(x, y), otherwise v0(x, y) = ( v(x, y), otherwise w0(x, y) = ( w(x, y), otherwise

whereclamp(a, b, c)returnsbifa < b,cifa > c, andaotherwise.

The value assigned to TEXTURE MIN FILTER is used to determine how the texture value for a fragment is selected.

When the value ofTEXTURE MIN FILTERisNEAREST, the texel in the image array of level levelbase that is nearest (in Manhattan distance) to (u0, v0, w0) is

3.8. TEXTURING 148

i=wrap(bu0(x, y)c)

j =wrap(bv0(x, y)c)

k=wrap(bw0(x, y)c)

and the value returned bywrap()is defined in table3.18. For a three-dimensional texture, the texel at location (i, j, k) becomes the texture value. For two- dimensional, two-dimensional array, rectangular, or cube map textures,kis irrele- vant, and the texel at location(i, j)becomes the texture value. For one-dimensional texture or one-dimensional array textures,j andkare irrelevant, and the texel at locationibecomes the texture value.

For one- and two-dimensional array textures, the texel is obtained from image layerl, where

l=

(

clamp(bt+ 0.5c,0, ht−1), for one-dimensional array textures

clamp(br+ 0.5c,0, dt−1), for two-dimensional array textures

Wrap mode Result ofwrap(coord) CLAMP TO EDGE clamp(coord,0, size−1) CLAMP TO BORDER clamp(coord,−1, size)

REPEAT f mod(coord, size)

MIRRORED REPEAT (size−1)−mirror(f mod(coord,2×size)−size)

Table 3.18: Texel location wrap mode application. f mod(a, b) returns a−b× ba

bc. mirror(a) returns a if a ≥ 0, and −(1 + a) otherwise. The values

ofmode andsize are TEXTURE WRAP S and wt, TEXTURE WRAP T and ht, and TEXTURE WRAP Randdtwhen wrappingi,j, orkcoordinates, respectively.

If the selected(i, j, k),(i, j), orilocation refers to a border texel that satisfies any of the conditions

i <−bs i≥wt+bs

j <−bs j ≥ht+bs

k <−bs k≥dt+bs

then the border values defined byTEXTURE BORDER COLOR are used in place of the non-existent texel. If the texture contains color components, the values of

3.8. TEXTURING 149

TEXTURE BORDER COLORare interpreted as an RGBA color to match the texture’s internal format in a manner consistent with table3.11. The internal data type of the border values must be consistent with the type returned by the texture as described in section3.8, or the result is undefined. Border values are clamped before they are used, according to the format in which texture components are stored. For signed and unsigned normalized fixed-point formats, border values are clamped to[−1,1]

and[0,1], respectively. For floating-point and integer formats, border values are clamped to the representable range of the format. If the texture contains depth components, the first component ofTEXTURE BORDER COLORis interpreted as a depth value.

When the value ofTEXTURE MIN FILTERisLINEAR, a2×2×2cube of texels in the image array of levellevelbaseis selected. Let

i0 =wrap(bu0−0.5c) j0 =wrap(bv0−0.5c) k0 =wrap(bw0−0.5c) i1 =wrap(bu0−0.5c+ 1) j1 =wrap(bv0−0.5c+ 1) k1 =wrap(bw0−0.5c+ 1) alpha=f rac(u0−0.5) beta=f rac(v0−0.5) gamma=f rac(w0−0.5)

wheref rac(x)denotes the fractional part ofx.

For a three-dimensional texture, the texture valueτ is found as

τ = (1−α)(1−β)(1−γ)τi0j0k0+α(1−β)(1−γ)τi1j0k0

+ (1−α)β(1−γ)τi0j1k0 +αβ(1−γ)τi1j1k0

+ (1−α)(1−β)γτi0j0k1 +α(1−β)γτi1j0k1

+ (1−α)βγτi0j1k1 +αβγτi1j1k1

(3.23)

whereτijkis the texel at location(i, j, k)in the three-dimensional texture image.

For a two-dimensional, two-dimensional array, rectangular, or cube map tex- ture,

τ =(1−α)(1−β)τi0j0 +α(1−β)τi1j0

3.8. TEXTURING 150

whereτij is the texel at location(i, j) in the two-dimensional texture image. For

two-dimensional array textures, all texels are obtained from layerl, where

l=clamp(br+ 0.5c,0, dt−1).

And for a one-dimensional or one-dimensional array texture,

τ = (1−α)τi0+ατi1

where τi is the texel at location i in the one-dimensional texture. For one-

dimensional array textures, both texels are obtained from layerl, where

l=clamp(bt+ 0.5c,0, ht−1).

For any texel in the equation above that refers to a border texel outside the defined range of the image, the texel value is taken from the texture border color as withNEARESTfiltering.

Rendering Feedback Loops

If all of the following conditions are satisfied, then the value of the selectedτijk,

τij, orτiin the above equations is undefined instead of referring to the value of the

texel at location(i, j, k),(i, j), or(i)respectively. This situation is discussed in more detail in the description of feedback loops in section4.4.2.

• The currentDRAW FRAMEBUFFER BINDINGnames a framebuffer objectF. • The texture is attached to one of the attachment points, A, of framebuffer

objectF.

• The value ofTEXTURE MIN FILTERisNEARESTorLINEAR, and the value ofFRAMEBUFFER ATTACHMENT TEXTURE LEVELfor attachment pointAis equal to the value ofTEXTURE BASE LEVEL

-or-

The value of TEXTURE MIN FILTER is NEAREST MIPMAP NEAREST,

NEAREST MIPMAP LINEAR, LINEAR MIPMAP NEAREST,

or LINEAR MIPMAP LINEAR, and the value of

FRAMEBUFFER ATTACHMENT TEXTURE LEVEL for attachment point A

3.8. TEXTURING 151

Mipmapping

TEXTURE MIN FILTER values NEAREST MIPMAP NEAREST,

NEAREST MIPMAP LINEAR, LINEAR MIPMAP NEAREST, and

LINEAR MIPMAP LINEAR each require the use of a mipmap. Rectangular textures do not support mipmapping (it is an error to specify a minification filter that requires mipmapping). A mipmap is an ordered set of arrays representing the same image; each array has a resolution lower than the previous one. If the image array of level levelbase has dimensions wt×ht ×dt, then there are

blog2(maxsize)c+ 1levels in the mipmap. where

maxsize=     

wt, for 1D and 1D array textures

max(wt, ht), for 2D, 2D array, and cube map textures

max(wt, ht, dt), for 3D textures

Numbering the levels such that levellevelbaseis the 0th level, theith array has

dimensions max(1,bwt wd c)×max(1,bht hd c)×max(1,bdt dd c) where wd= 2i hd= (

1, for 1D and 1D array textures

2i, otherwise

dd=

(

2i, for 3D textures 1, otherwise

until the last array is reached with dimension1×1×1.

Each array in a mipmap is defined usingTexImage3D,TexImage2D,Copy- TexImage2D,TexImage1D, orCopyTexImage1D; the array being set is indicated with the level-of-detail argument level. Level-of-detail numbers proceed from

levelbase for the original texel array through p = blog2(maxsize)c+levelbase

with each unit increase indicating an array of half the dimensions of the previous one (rounded down to the next integer if fractional) as already described. All ar- rays fromlevelbasethroughq = min{p, levelmax}must be defined, as discussed

3.8. TEXTURING 152

The values of levelbase and levelmax may be respecified for a specific tex-

ture by callingTexParameter[if] with pname set to TEXTURE BASE LEVEL or

TEXTURE MAX LEVELrespectively.

The errorINVALID VALUEis generated if either value is negative.

The mipmap is used in conjunction with the level of detail to approximate the application of an appropriately filtered texture to a fragment. Letcbe the value ofλat which the transition from minification to magnification occurs (since this discussion pertains to minification, we are concerned only with values ofλwhere

λ > c).

For mipmap filters NEAREST MIPMAP NEAREST and

LINEAR MIPMAP NEAREST, thedth mipmap array is selected, where

d=      levelbase, λ≤ 12

dlevelbase+λ+ 12e −1, λ > 12, levelbase+λ≤q+12

q, λ > 12, levelbase+λ > q+12

(3.24)

The rules for NEARESTor LINEAR filtering are then applied to the selected array. Specifically, the coordinate(u, v, w)is computed as in equation3.20, with

ws,hs, anddsequal to the width, height, and depth of the image array whose level

isd.

For mipmap filtersNEAREST MIPMAP LINEARandLINEAR MIPMAP LINEAR, the leveld1andd2mipmap arrays are selected, where

d1 =

(

q, levelbase+λ≥q

blevelbase+λc, otherwise

(3.25) d2 = ( q, levelbase+λ≥q d1+ 1, otherwise (3.26)

The rules for NEAREST or LINEAR filtering are then applied to each of the selected arrays, yielding two corresponding texture valuesτ1 andτ2. Specifically, for leveld1, the coordinate(u, v, w)is computed as in equation3.20, withws,hs,

andds equal to the width, height, and depth of the image array whose level isd1. For leveld2 the coordinate (u0, v0, w0) is computed as in equation3.20, withws,

hs, anddsequal to the width, height, and depth of the image array whose level is

d2.

The final texture value is then found as

3.8. TEXTURING 153

Manual Mipmap Generation

Mipmaps can be generated manually with the command

void GenerateMipmap(enumtarget);

where target is one of TEXTURE 1D, TEXTURE 2D, TEXTURE 3D,

TEXTURE 1D ARRAY, TEXTURE 2D ARRAY, or TEXTURE CUBE MAP. Mipmap generation affects the texture image attached totarget. For cube map textures, an

INVALID OPERATIONerror is generated if the texture bound totargetis not cube complete, as defined in section3.8.11.

Mipmap generation replaces texel array levels levelbase + 1 through q with

arrays derived from thelevelbasearray, regardless of their previous contents. All

other mipmap arrays, including thelevelbasearray, are left unchanged by this com-

putation.

The internal formats of the derived mipmap arrays all match those of the

levelbasearray, and the dimensions of the derived arrays follow the requirements

described in section3.8.11.

The contents of the derived arrays are computed by repeated, filtered reduction of thelevelbasearray. For one- and two-dimensional array textures, each layer is

filtered independently. No particular filter algorithm is required, though a box filter is recommended as the default filter.

In document glspec31.20090324.pdf (Page 157-166)