Clearing a viewport resets the contents of the viewport rectangle on the render target surface as well as the rectangle in the depth and stencil buffer surfaces (if specified). Typically, you will clear the viewport before rendering a new frame to ensure that graphics and other data is ready to accept new rendered objects without displaying artifacts.
The IDirect3DViewport3 interface offers the IDirect3DViewport3::Clear and IDirect3DViewport3::Clear2 methods that provide various ways to clear the viewport. Both of the clearing methods accept one or more rectangles that define the area or areas on the surfaces being cleared. In cases where the scene being rendered includes motion throughout the entire viewport rectangle—in a first-person perspective game, perhaps—you might want to clear the entire viewport each frame. In this situation, you would set the dwCount parameter to 1, and the lpRects parameter to the address of a single rectangle that covers the entire viewport area.
Note DirectX 5.0 allowed background materials to have associated textures, making it possible to clear the viewport to a texture, rather than a simple color. This feature was little used, and not particularly efficient. Interfaces added for DirectX 6.0 do not accept texture handles, meaning that you can no longer clear the viewport to a texture. Rather, applications must now draw backgrounds manually. As a result, there is rarely a need to clear the viewport on the render target surface. So long as your application clears the depth buffer, all pixels on the render target surface will be overwritten anyway.
In some situations, you might only be rendering to small portions of the render target and depth buffer surfaces. The clear methods also allow you to clear multiple areas of your surfaces in a single call. Do this by setting the dwCount parameter to the number of rectangles you want cleared, and specify the address of the first rectangle in an array of rectangles in the lpRects parameter.
The clearing methods have differing features and behavior. The IDirect3DViewport3::Clear method can clear the viewport using the color of the background material if you specify the D3DCLEAR_TARGET flag in the dwFlags parameter, and it can clear the z-buffer to the "deepest" value (1.0) if you include the D3DCLEAR_ZBUFFER flag. The method requires that you set a background material by calling the IDirect3DViewport3::SetBackground method. The Clear method is incapable of clearing stencil buffers.
The IDirect3DViewport3::Clear2 method was introduced to provide more flexibility and ease-of-use than the Clear method, and to provide support for clearing stencil bits within a depth buffer. Clear2 accepts the same two flags in the dwFlags parameter as its ancestor, but with slightly different results, and it adds one new flag to support stencils. If you include the D3DCLEAR_TARGET flag, the method clears the viewport using an arbitrary RGBA color that you provide in the dwColor parameter (not the material color). If you include the D3DCLEAR_ZBUFFER flag, the method clears the depth buffer to an arbitrary depth you specify in dvZ: 0.0 is the closest distance, and 1.0 is the farthest. Including the new D3DCLEAR_STENCIL flag causes the method to reset the stencil bits to the value you provide in the dwStencil parameter. You can use integers that range from 0 to 2n-1, where n is the stencil buffer bit depth.