DirectX Utility Functions

If you are writing shaders for the Direct3D 10 view mode, then you must use the DirectX utility functions. The DirectX utility is comprised of several callback functions which center around one structure: tagDX10ViewGlobals. This structure stores the information about the following:

• Windows

• Core and Display Buffers

• Grid Information

• Text Elements (Font and Sprite)

• States and Buffers

• Binding Texture Coordinate Sets

• SAS (Standard Annotations and Semantics)

In addition, there are a number of members that are used internally by the DX10View API, and are of limited use to custom display callbacks or realtime shaders. These store information about the following:

• Current States

Accessing the DirectX Utility

Include File

You need to include the DX10View_API.h file.

Library File

You need to link with the DX10View_API.lib library.

Getting the Context

Use the DX10View_GetContext function to get the current DirectX context in the form of the tagDX10ViewGlobals structure.

Managing DirectX10 Views

The following callback functions are provided to manage rendering contexts for the DirectX10 viewport:

• DX10View_IsSupported

• DX10View_Initialize

• DX10View_InitializeWindow

• DX10View_InitViewport

• DX10View_Cleanup

Rendering

• DX10View_BeginRender

• DX10View_SetInitialState

• DX10View_EndRender

Drawing Objects

To draw geometry coming from Softimage, use the IRTSExecutionState::DrawGeometry call. For example:

    XSI_RTS_Attribute gDX10DrawAttribs[] =
    {
       XSI_RTS_ATTRIBUTE_POSITION, ePOSITION,
       XSI_RTS_ATTRIBUTE_NORMAL, eNORMAL,
       XSI_RTS_ATTRIBUTE_COLOR , eCOLOR0,
       XSI_RTS_ATTRIBUTE_COLOR , eCOLOR1,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD0,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD1,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD2,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD3,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD4,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD5,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD6,
       XSI_RTS_ATTRIBUTE_TEXUVW , eTEXCOORD7,
       NULL
    };

    //****************************************************************************************
    // Implementation
    //****************************************************************************************
    RTSHADER_API bool DX10Draw_Execute
    (
       IRTSExecutionState *in_pExecState, // The execute state
       void                  *in_pParams,      // The property page parameters
       XSI_RTS_Primitive *in_pPrimitive, // The geometry information
       void                  *in_pAttributes, // The attribute data
       XSI_RTS_Attribute_Size *in_pAttributesSize, // The sizes
       void                  **io_pShaderInstanceData // The shader instance data
    )
    {
       DX10Draw_PPG* l_pPPG = (DX10Draw_PPG*) in_pParams;

       //****************************************************************************************
       // Now we do our stuff with DirectX
       //****************************************************************************************
       if(DX10View_GetContext()->m_pSwapChain != NULL)
       {
           // texture coords are now completely in order because they
           // are set in the DX10Draw note

           int loop;
           float l_Matrix[16];

           for(loop = 0; loop < 8; loop++)
           {
              if(l_pPPG->texcoord[loop] == 0xffffffff)
              {
                  break;
              }
              DX10View_GetContext()->m_TexCoordMap.UVSet[loop] = loop;
              DX10View_GetContext()->m_TexCoordMap.target[loop] = loop;
           }

           DX10View_GetContext()->m_TexCoordMap.nbActiveUVs = loop;

           // use the default vertex shader if we have no vertex shader
           if(DX10View_GetContext()->m_pCurrentVS == NULL)
           {
              DX10View_GetContext()->m_pModelMatrix->GetMatrix(l_Matrix);
              DX10View_GetContext()->m_pModelMatrix->SetMatrixTranspose(in_pExecState->GetSystemParams()->m_m4x4ObjectPose);
              DX10View_GetContext()->m_pTechnique->GetPassByIndex( 0 )->Apply(0);
           }

           in_pExecState->DrawGeometry ( DirectX_10,
                                       in_pPrimitive,
                                       gDX10DrawAttribs,
                                       in_pAttributes,
                                       in_pAttributesSize);

           // reset the previous model matrix if we were using the default vertex shader
           if(DX10View_GetContext()->m_pCurrentVS == NULL)
           {
              DX10View_GetContext()->m_pModelMatrix->SetMatrix(l_Matrix);
           }
       }

       return TRUE;
    }

Uploading Textures

Use the following functions to upload textures to the DirectX 10 device:

• DX10View_UploadTexture1D

• DX10View_UploadTexture2D

• DX10View_UploadTexture3D

• DX10View_UploadTextureCube

Windows

These fields are used to access window information, including:

• The current window (via the m_hwnd field). The current window is defined as being the window in which the realtime shader or the display callback is being called.

• The current window’s parent (via the m_ParentHwnd field).

• The Rect (width, height, and location) of the parent window (via the m_ParentRect field).

• The window class (via the m_wc field).

• The HINSTANCE handle (via the m_hInst field).

Core and Display Buffers

These fields are used to access the core and display buffers, including:

• The global Direct3D 10 device (via the m_pd3dDevice field). Resources should be managed through this device, since it is global for Softimage. For example, the following code creates a geometry shader using that device:

    hr = DX10View_GetContext()->m_pd3dDevice->CreateGeometryShader(
       l_pInstanceData->byte_code->GetBufferPointer(),
       l_pInstanceData->byte_code->GetBufferSize(),
       &l_pInstanceData->geometryshader
    );

• The current view's swap chain (via the m_pSwapChain field).

• The current view's render target (via the m_pRenderTargetView field).

• The current view's depth stencil buffer (via the m_pDSView field).

• A global IDXGIFactory object you can use to create DXGI objects (via the m_pFactory field).

Grid Information

These are members that are used to draw the grid on the 3D viewport:

• m_pEffect

• m_pTechnique

• m_pGridVertexLayout

• m_pGridVertexBuffer

Text Elements (Font and Sprite)

These are the members used to draw text on the Direct3D 10 render targets. You can use them to add other text elements in the viewport:

• m_pFont

• m_pSprite

• m_pFontBlendState

The following code draws some text on the viewport:

    D3DXMATRIXA16 matProjection;
    D3DXMatrixOrthoOffCenterLH(&matProjection, (FLOAT)VPs[0].TopLeftX, (FLOAT)(VPs[0].TopLeftX + VPs[0].Width), (FLOAT)VPs[0].TopLeftY, (FLOAT)(VPs[0].TopLeftY + VPs[0].Height), 0.1f, 10);
    DX10View_GetContext()->m_pSprite->SetProjectionTransform(&matProjection);
    DX10View_GetContext()->m_pSprite->Begin( D3DX10_SPRITE_SORT_TEXTURE );

    // Draw text
    RECT rc;
    SetRect( &rc, 10, 8, 0, 0 );
    DX10View_GetContext()->m_pFont->DrawText
    (
       DX10View_GetContext()->m_pSprite,
       "Hello World",
       -1,
       &rc,
       DT_NOCLIP,
       D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f )
    );

    if( DX10View_GetContext()->m_pFontBlendState )
    {
       FLOAT NewBlendFactor[4] = {0,0,0,0};
       DX10View_GetContext()->m_pd3dDevice->OMSetBlendState
       (
           DX10View_GetContext()->m_pFontBlendState,
           NewBlendFactor,
           0xffffffff
       );
    }

    if( DX10View_GetContext()->m_pDefaultRasterizerState )
    {
       DX10View_GetContext()->m_pd3dDevice->RSSetState( DX10View_GetContext()->m_pDefaultRasterizerState );
    }

    DX10View_GetContext()->m_pSprite->End();

States and Buffers

These members provide the following:

• The current projection (via the m_pProjectionMatrix field).

• The model (via the m_pModelMatrix field).

• The view matrix (via the m_pViewMatrix field).

| In addition, the m_pDefaultBlendState and m_pDefaultRasterizerState members provide the default states that some of the D10View API functions use to draw text or the grid for example. These are used internally by the DX10View API, and are of limited use to custom display callbacks or realtime shaders.

Current States

| The members in this category are used internally by the DX10View API, and are of limited use to custom display callbacks or realtime shaders.

These members provide access to the current vertex, geometry and pixel shader objects. These are set by the DX10HLSLProgram shader:

• m_pIACurrentInputSignature

• m_nIACurrentInputSignatureSize

• m_pCurrentGS

• m_pCurrentVS

• m_pCurrentPS

These members provide access to the vertex buffers used to display the geometry coming from Softimage in the DirectX 10 viewport. The DX10View API uses multiple vertex buffers to pipeline the drawing of triangles. The m_nCurrentVertexBufferId indicates which is the currently available vertex buffer to be filled with data:

• m_pCurrentVertexBuffer

• m_nCurrentVertexBufferSize

• m_nCurrentVertexBufferId

Binding Texture Coordinate Sets

If your shader needs a texture coordinate set to be bound to a texture target, then it needs to fill in the DX10View_TexCoordMap structure (available via the m_TexCoordMap member), which informs the drawing function.

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

    DX10View_TexCoordMap *pTexCoordMap = DX10View_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.

| You should never overwrite existing data in the DX10View_TexCoordMap structure. Instead, use the DX10View_TexCoordMap::nbActiveUVs member to check for the current active UVs and fill it in from there.

SAS (Standard Annotations and Semantics)

The m_Semantics member provides SAS (Standard Annotations and Semantics) data that can be fed into shaders. SAS is a specification for synchronizing data between a host application and a shader. This data can be the current camera position, lights information, model matrices and so on. See the XSI_RTS_FXSemantics struct for more information.

Autodesk Softimage v7.5