DirectX SDK

Detecting Support For Bump Mapping

[C++]

A Direct3D device can perform bump mapping if it supports either of the D3DTOP_BUMPENVMAP or D3DTOP_BUMPENVMAPLUMINANCE texture blending operations. Additionally, applications should check the device capabilities to make sure it supports an appropriate number of blending stages (usually at least 3) and exposes at least one bump mapping pixel format.

The following code checks device capabilities to detect support for bump mapping in the current device, using the given criteria.

BOOL SupportsBumpMapping() 
{
    DDPIXELFORMAT ddpfBumpMap;
    D3DDEVICEDESC7 d3dDevDesc;
    ZeroMemory( &d3dDevDesc, sizeof(d3dDevDesc) );

    // Get the device capabilities.
    g_pd3dDevice->GetCaps( &d3dDevDesc );

    // Does this device support the two bump mapping blend operations?
    DWORD dwBumpOps = d3dDevDesc.dwTextureOpCaps & 
                      (D3DTEXOPCAPS_BUMPENVMAP | D3DTEXOPCAPS_BUMPENVMAPLUMINANCE);
    if ( 0 == dwBumpOps)
        return FALSE;

    // Does this device support up to three blending stages?
    if( d3dDevDesc.wMaxTextureBlendStages < 3)
        return FALSE;

    //
    // Check for valid bump map pixel formats.
    //

    // The g_bFoundBumpFormat global variable will be set to TRUE 
    // by the callback function if a valid format is found.
    g_bFoundBumpFormat = FALSE;  

    g_pd3dDevice->EnumTextureFormats(TextureCallback, (LPVOID) &ddpfBumpMap);
    if( FALSE == g_bFoundBumpFormat )
        return FALSE;

    // The pixel format now in ddpfBumpMap can be used to create 
    // a surface format that's guaranteed to be valid.
    return TRUE;
}

The following is the code for the callback function passes to the IDirect3DDevice7::EnumTextureFormats method.

HRESULT CALLBACK TextureCallback( DDPIXELFORMAT* pddpf, VOID* pddpfOut)
{
    // Take the first enumerated DuDv format.
    if( DDPF_BUMPDUDV == pddpf->dwFlags ){
        // Copy the format into the variable at pddpfOut 
        // for use when creating the surface later.
        memcpy( pddpfOut, (LPVOID)pddpf, sizeof(DDPIXELFORMAT) );

        // Set the global flag to signal success.
        g_bFoundBumpFormat = TRUE;
        return D3DENUMRET_CANCEL;
    }

    return D3DENUMRET_OK;
} 
[Visual Basic]

A Direct3D device can perform bump mapping if it supports either of the D3DTOP_BUMPENVMAP or D3DTOP_BUMPENVMAPLUMINANCE texture blending operations. Additionally, applications should check the device capabilities to make sure it supports an appropriate number of blending stages (usually at least 3) and exposes at least one bump mapping pixel format.

The following code checks device capabilities to detect support for bump mapping in the current device, using the given criteria.

Function SupportsBumpMapping() As Boolean
    Dim ddpfBumpMap As DDPIXELFORMAT
    Dim d3dDevDesc As D3DDEVICEDESC7
    
    SupportsBumpMapping = True
    
    ' Get the device capabilities.
    Call g_d3dDevice.GetCaps(d3dDevDesc)

    ' Does this device support the two bump mapping blend operations?
    Dim lBumpOps As Long
    lBumpOps = (d3dDevDesc.lTextureOpCaps And _
                (D3DTEXOPCAPS_BUMPENVMAP Or D3DTEXOPCAPS_BUMPENVMAPLUMINANCE))
    
    If lBumpOps = 0 Then SupportsBumpMapping = False

    ' Does this device support up to three blending stages?
    If d3dDevDesc.nMaxTextureBlendStages < 3 Then SupportsBumpMapping = False

    '
    ' Check for valid bump map pixel formats.
    '
    Dim BumpEnum As Direct3DEnumPixelFormats
    Set BumpEnum = g_d3dDevice.GetTextureFormatsEnum
    
    Dim iLoop As Integer
    For iLoop = 0 To BumpEnum.GetCount
        Call BumpEnum.GetItem(iLoop, ddpfBumpMap)
        
        ' Stop enumerating when a format is found.
        If ddpfBumpMap.lFlags And DDPF_BUMPDUDV Then Exit For
    Next iLoop
    
    If Not (ddpfBumpMap.lFlags And DDPF_BUMPDUDV) Then
        SupportsBumpMapping = False
    End If
End Function