Multiple Texture Blending
A version of this page is also available for
4/8/2010
Microsoft® Direct3D® Mobile can blend as many as eight textures onto primitives in a single pass. The use of multiple texture blending can profoundly increase the frame rate of a Direct3D Mobile application. An application employs multiple texture blending to apply textures, shadows, specular lighting, diffuse lighting, and other special effects in a single pass.
Texture Stages and the Texture Blending Cascade
Direct3D Mobile supports single-pass multiple texture blending through the use of texture stages. A texture stage takes two arguments and performs a blending operation on them, passing on the result for further processing or for rasterization. You can visualize a texture stage as shown in the following illustration.
As the preceding illustration shows, texture stages blend two arguments by using a specified operator. Common operations include simple modulation or addition of the color or alpha components of the arguments, but more than two dozen operations are supported. The arguments for a stage can be an associated texture, the iterated color or alpha (iterated during Gouraud shading), arbitrary color and alpha, or the result from the previous texture stage.
Note
Direct3D Mobile distinguishes color blending from alpha blending. Applications set blending operations and arguments for color and alpha individually, and the results of those settings are independent of one another.
The combination of arguments and operations used by multiple blending stages define a simple flow-based blending language. The results from one stage flow down to another stage, from that stage to the next, and so on. The concept of results flowing from stage to stage to eventually be rasterized on a polygon is often called the texture blending cascade. The following illustration shows how individual texture stages make up the texture blending cascade.
Each stage in a device has a zero-based index. Direct3D Mobile allows up to four blending stages, although you should always check device capabilities to determine how many stages the current hardware supports. The first blending stage is at index 0, the second is at 1, and so on, up to index 3. The system blends stages in increasing index order.
Use only the number of stages you need; the unused blending stages are disabled by default. So, if your application only uses the first two stages, it need only set operations and arguments for stage 0 and 1. The system blends the two stages, and ignores the disabled stages.
Note
If your application varies the number of stages it uses for different situations — such as four stages for some objects, and only two for others — you do not need to explicitly disable all previously used stages. If you disable the color operation for the first unused stage, all stages with a higher index will not be applied. You can disable texture mapping altogether by setting the color operation for the first texture stage (stage 0).
Texture Blending Operations and Arguments
Applications associate a blending stage with each texture in the set of current textures. Direct3D Mobile evaluates each blending stage in order, beginning with the first texture in the set and ending with the fourth.
Direct3D Mobile applies the information from each texture in the set of current textures to the blending stage that is associated with it. Applications control what information from a texture stage is used by calling IDirect3DMobileDevice::SetTextureStageState. You can set separate operations for the color and alpha channels, and each operation uses two arguments. Specify color channel operations by using the D3DMTSS_COLOROP stage state; specify alpha operations by using D3DTSS_ALPHAOP. Both stage states use values from the D3DMTEXTUREOP enumerated type.
Texture blending arguments use the D3DMTSS_COLORARG1, D3DMTSS_COLORARG2, D3DMTSS_ALPHARG1, and D3DMTSS_ALPHARG2 members of the D3DMTEXTURESTAGESTATETYPE enumerated type. The corresponding argument values are identified using D3DMTA Values.
Note
You can disable a texture stage — and any subsequent texture blending stages in the cascade — by setting the color operation for that stage to D3DMTOP_DISABLE. Disabling the color operation effectively disables the alpha operation as well. Alpha operations cannot be disabled when color operations are enabled. Setting the alpha operation to D3DMTOP_DISABLE when color blending is enabled causes undefined behavior.
To determine the supported texture blending operations of a device, query the TextureCaps member of the D3DMCAPS structure.
Assigning the Current Textures
Direct3D Mobile maintains a list of up to four current textures. It blends these textures onto all the primitive it renders. Only textures created as texture interface pointers can be used in the set of current textures.
Applications call the IDirect3DMobileDevice::SetTextureStageState method to assign textures into the set of current textures. The first parameter must be from the a number in the range of 0-3, inclusive. Pass the texture interface pointer as the second parameter.
The following code shows how a texture can be assigned to the set of current textures.
// This code example assumes that the variable d3dmDevice is a
// valid pointer to an IDirect3DMobileDevice interface and pTexture
// is a valid pointer to an IDirect3DMobileBaseTexture interface.
// Set the third texture.
d3dmDevice->SetTexture(2, pTexture);
Note
Software devices do not support assigning a texture to more than one texture stage at a time.
Creating Blending Stages
A blending stage is a set of texture operations and their arguments that define how textures are blended. When making a blending stage, applications invoke the IDirect3DMobileDevice::SetTextureStageState method. The first call specifies the operation that is performed. Two additional invocations define the arguments to which the operation is applied. The following code example shows the creation of a blending stage.
// This example assumes that d3dmDevice is a valid pointer to an
// IDirect3DMobileDevice interface.
// Set the operation for the first texture.
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLOROP, D3DMTOP_ADD);
// Set argument 1 to the texture color.
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLORARG1, D3DMTA_TEXTURE);
// Set argument 2 to the iterated diffuse color.
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLORARG2, D3DMTA_DIFFUSE);
Texel data in textures contain color and alpha values. Applications can define separate operations for both color and alpha values in a single blending stage. Each operation, color and alpha, has its own arguments. For details, see D3DMTEXTURESTAGESTATETYPE.
Although not part of the Direct3D Mobile API, you can insert the following macros into your application to abbreviate the code required for creating texture blending stages.
#define SetTextureColorStage( dev, i, arg1, op, arg2 ) \
dev->SetTextureStageState( i, D3DMTSS_COLOROP, op); \
dev->SetTextureStageState( i, D3DMTSS_COLORARG1, arg1 ); \
dev->SetTextureStageState( i, D3DMTSS_COLORARG2, arg2 );
#define SetTextureAlphaStage( dev, i, arg1, op, arg2 ) \
dev->SetTextureStageState( i, D3DMTSS_ALPHAOP, op); \
dev->SetTextureStageState( i, D3DMTSS_ALPHARG1, arg1 ); \
dev->SetTextureStageState( i D3DMTSS_ALPHARG2, arg2 );