A mipmap is a sequence of textures, each of which is a progressively lower resolution, prefiltered representation of the same image. Each prefiltered image, or level, in the mipmap is a power of two smaller than the previous level. A high-resolution level is used for objects that are close to the viewer. Lower-resolution levels are used as the object moves farther away. Mipmapping is a computationally low-cost way of improving the quality of rendered textures.
You can use mipmaps when texture-filtering by specifying the appropriate filter mode in the D3DTEXTUREFILTER enumerated type. To find out what kinds of mipmapping support are provided by a device, use the flags specified in the dwTextureFilterCaps member of the D3DPRIMCAPS structure.
In DirectDraw, mipmaps are represented as a chain of attached surfaces. The highest resolution texture is at the head of the chain and has, as an attachment, the next level of the mipmap. That level has, in turn, an attachment that is the next level in the mipmap, and so on down to the lowest resolution level of the mipmap.
To create a surface representing a single level of a mipmap, specify the DDSCAPS_MIPMAP flag in the DDSURFACEDESC structure passed to the IDirectDraw2::CreateSurface method. Because all mipmaps are also textures, the DDSCAPS_TEXTURE flag must also be specified. It is possible to create each level manually and build the chain by using the IDirectDrawSurface3::AddAttachedSurface method. However, you can use the IDirectDraw2::CreateSurface method to build an entire mipmap chain in a single operation. In this case, the DDSCAPS_COMPLEX flag is also required.
The following example demonstrates building a chain of five mipmap levels of sizes 256´256, 128´128, 64´64, 32´32, and 16´16:
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE3 lpDDMipMap;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_MIPMAPCOUNT;
ddsd.dwMipMapCount = 5;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE |
DDSCAPS_MIPMAP | DDSCAPS_COMPLEX;
ddsd.dwWidth = 256UL;
ddsd.dwHeight = 256UL;
ddres = lpDD->CreateSurface(&ddsd, &lpDDMipMap);
if (FAILED(ddres))
.
.
.
You can omit the number of mipmap levels, in which case the IDirectDraw2::CreateSurface method will create a chain of surfaces, each a power of two smaller than the previous one, down to the smallest possible size. It is also possible to omit the width and height, in which case IDirectDraw2::CreateSurface will create the number of levels you specify, with a minimum level size of 1´1.
A chain of mipmap surfaces is traversed by using the IDirectDrawSurface3::GetAttachedSurface method and specifying the DDSCAPS_MIPMAP and DDSCAPS_TEXTURE flags in the DDSCAPS structure. The following example traverses a mipmap chain from highest to lowest resolutions:
LPDIRECTDRAWSURFACE lpDDLevel, lpDDNextLevel;
DDSCAPS ddsCaps;
lpDDLevel = lpDDMipMap;
lpDDLevel->AddRef();
ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_MIPMAP;
ddres = DD_OK;
while (ddres == DD_OK)
{
// Process this level.
.
.
.
ddres = lpDDLevel->GetAttachedSurface(
&ddsCaps, &lpDDNextLevel);
lpDDLevel->Release();
lpDDLevel = lpDDNextLevel;
}
if ((ddres != DD_OK) && (ddres != DDERR_NOTFOUND))
.
.
.
You can also build flipping chains of mipmaps. In this scenario, each mipmap level has an associated chain of back buffer texture surfaces. Each back-buffer texture surface is attached to one level of the mipmap. Only the front buffer in the chain has the DDSCAPS_MIPMAP flag set; the others are simply texture maps (created by using the DDSCAPS_TEXTURE flag). A mipmap level can have two attached texture maps, one with DDSCAPS_MIPMAP set, which is the next level in the mipmap chain, and one with the DDSCAPS_BACKBUFFER flag set, which is the back buffer of the flipping chain. All the surfaces in each flipping chain must be of the same size.
It is not possible to build such a surface arrangement with a single call to the IDirectDraw2::CreateSurface method. To construct a flipping mipmap, either build a complex mipmap chain and manually attach back buffers by using the IDirectDrawSurface3::AddAttachedSurface method, or create a sequence of flipping chains and build the mipmap by using IDirectDrawSurface3::AddAttachedSurface.
Note Blit operations apply only to a single level in the mipmap chain. To blit an entire chain of mipmaps, each level must be blitted separately.
The IDirectDrawSurface3::Flip method will flip all the levels of a mipmap from the level supplied to the lowest level in the mipmap. A destination surface can also be provided, in which case all levels in the mipmap will flip to the back buffer in their flipping chain. This back buffer matches the supplied override. For example, if the third back buffer in the top-level flipping chain is supplied as the override, all levels in the mipmap will flip to the third back buffer.
The number of levels in a mipmap chain is stored explicitly. When an application obtains the surface description of a mipmap (by calling the IDirectDrawSurface3::Lock or IDirectDrawSurface3::GetSurfaceDesc method), the dwMipMapCount member of the DDSURFACEDESC structure will contain the number of levels in the mipmap, including the top level. For levels other than the top level in the mipmap, the dwMipMapCount member specifies the number of levels from that mipmap to the smallest mipmap in the chain.