DirectX Utility Functions

If you are writing shaders for the DirectX view mode, then you must use the DirectX utility functions. To do so, include the DX9View_API.h file, and link with the DX9View_API.lib library.

This section contains the following topics:

• DX9View_GetContext

• Resources

• Drawing

• Texture Target and Texture Coordinate Management

• Renderstate Functions

DX9View_GetContext

This function gets the current DirectX context. You can use this context to get:

• the DirectX device with the m_pD3DDevice member

• the Texture coordinate maps with the m_TexCoordMap member

• whether the device is invalid and resources need to be recreated with the m_RecreateResources member

• a pointer to the D3DXFont so you can draw text on the viewport using the m_D3DXFont member

For example, if you want to set a render state in DirectX, you can use the following code:

    DX9View_GetContext()->m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

Resources

In DirectX, all resources such as shader programs, texture and vertex buffers must be deallocated properly in order to destroy a Direct3D device. In SOFTIMAGE|XSI, the Direct3D device is destroyed when the user switches the viewport from DirectX9 mode to another mode. Since the shader might not necessarily get destroyed, it doesn’t know that it has to delete its DirectX resource when the viewport mode changes.

In order to properly cleanup resources in DirectX, every resource must be registered in every DirectX context. Use the DX9View_AddResource function. For example:

    LPDIRECT3DTEXTURE9 texture = NULL;

    // texture initialization

    DX9View_AddResource(texture, pInstanceData->m_XGSTextureHandleName);

When the resource needs to be freed for one context (for example, a texture had changed and needs to be recreated), then use the DX9View_FreeResource function. For example:

    if((texture != NULL) && (!DX9View_GetContext()->m_RecreateResources))
    {
    DX9View_FreeResource(pInstanceData->m_XGSTextureHandleName);
    }

When the resource is no longer needed, because the shader is being destroyed, then you should remove it for all the contexts. Use the DX9View_FreeResourceFromAllContexts function. For example:

    DX9View_FreeResourceFromAllContexts(pInstanceData->m_XGSTextureHandleName);

Drawing

• DX9View_DrawGrid
Draws the grid in the DirectX viewport.

• DX9View_DrawGeometry
Draws the geometry using DirectX in the DirectX viewport.

Texture Target and Texture Coordinate Management

The drawing function needs to know which texture coordinate set is going to be used for a particular target. In order to do this, there is a structure called DX9View_TexCoordMap that is used. If your shader needs a texture coordinate set to be bound to a texture target, then it needs to fill in this structure. You should never overwrite existing data; instead check for the current active UVs, using the nbActiveUVs member and fill in starting from there.

For example, if the shader needs texture coordinate set 1 to be bound to texture target 2, then the following code should be used.

    DX9View_TexCoordMap *pTexCoordMap = DX9View_GetContext()->m_TexCoordMap;

    pTexCoordMap->target[pTexCoordMap->nbActiveUVs] = 2;
    pTexCoordMap->UVSet[pTexCoordMap->nbActiveUVs] = 1;
    pTexCoordMap->nbActiveUVs ++;

If your shader has a texture projection parameter, then this parameter is converted to a LONG value. This value is the texture coordinate set to use for this shader

Renderstate Functions

• DX9View_SetDefaultMaterial
Sets the default material in the DirectX display.

• DX9View_TrackWorldMatrix
Tracks the world matrix from the OpenGL context of the viewport and sets it in the DirectX context.

• DX9View_TrackLights
Tracks light information from the OpenGL context of the viewport and sets it in the DirectX context.

• DX9View_SaveInitialStates
Save all DirectX states in an initial state group.

• DX9View_RestoreInitialStates
Restores the initial DirectX states saved with DX9View_SaveInitialStates.