In DirectX 2, DirectDraw supports mipmapped texture surfaces. A mipmap is a sequence of textures, each of which is a progressively lower resolution, prefiltered representation of the same image. Mipmapping is a computationally low-cost way of improving the quality of rendered textures. Each pre-filtered image, or level, in the mipmap is a power of two smaller than the previous level. 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 which 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 surface description passed to the IDirectDraw::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 with the IDirectDrawSurface::AddAttachedSurface method. However, the IDirectDraw::CreateSurface method can be used to build an entire mipmap chain in a single operation.
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;
LPDIRECTDRAWSURFACE 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 mipmaps levels, in which case the IDirectDraw::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 IDirectDraw::CreateSurface will create the number of levels you specify with a minimum level size of 1´1.
A chain of mipmap surfaces is traversed using the IDirectDrawSurface::GetAttachedSurface method, specifying the DDSCAPS_MIPMAP and DDSCAPS_TEXTURE capability flags. 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 flippable chains of mipmaps. In this scenario, each mipmap level has an associated chain of back buffer texture surfaces. Each back buffer texture 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 (DDSCAPS_TEXTURE). 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 flippable chain. All the surfaces in each flippable chain must be of the same size.
It is not possible to build such a surface arrangement with a single call to the IDirectDraw::CreateSurface method. To construct a flippable mipmap, either build a complex mipmap chain and manually attach back buffers with the IDirectDrawSurface::AddAttachedSurface method or create a sequence of flippable chains and build the mipmap with IDirectDrawSurface::AddAttachedSurface.
Note Blit operations apply to only a single level in the mipmap chain. To blit an entire chain of mipmaps, each level must be blitted separately.
The IDirectDrawSurface::Flip method will flip all the levels of a mipmap from the level supplied to the lowest level in the map. A destination surface can also be provided, in which case all levels in the mipmap will flip to the back buffer in their flippable chain. This back buffer matches the supplied override. For example, if the third back buffer in the top-level flippable chain is supplied, all levels in the mipmap will flip to their third back buffer.
The number of levels in a mipmap chain is stored explicitly. When the surface description of a mipmap is obtained (using IDirectDrawSurface::Lock or IDirectDrawSurface::GetSurfaceDesc), the dwMipMapCount member will contain the number of levels in a mipmap, including the top level. For levels other than the top level in the map, dwMipMapCount will specify the number of levels from that map to the smallest map in the chain.