Exemple #1
0
//--------------------------------------------------------------------------------------
// Rejects any devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
                                  D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
    // Typically want to skip backbuffer formats that don't support alpha blending
    IDirect3D9* pD3D = DXUTGetD3DObject(); 
    if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
                    AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
                    D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
        return false;

    return true;
}
//--------------------------------------------------------------------------------------
// Enumerates available D3D adapters, devices, modes, etc.
//--------------------------------------------------------------------------------------
HRESULT CD3DEnumeration::Enumerate( IDirect3D9* pD3D,
                                    LPDXUTCALLBACKISDEVICEACCEPTABLE IsDeviceAcceptableFunc,
                                    void* pIsDeviceAcceptableFuncUserContext )
{
    if( pD3D == NULL )
    {
        pD3D = DXUTGetD3DObject();
        if( pD3D == NULL )
            return DXUTERR_NODIRECT3D;
    }

    m_pD3D = pD3D;
    m_IsDeviceAcceptableFunc = IsDeviceAcceptableFunc;
    m_pIsDeviceAcceptableFuncUserContext = pIsDeviceAcceptableFuncUserContext;

    HRESULT hr;
    ClearAdapterInfoList();
    CGrowableArray<D3DFORMAT> adapterFormatList;

    const D3DFORMAT allowedAdapterFormatArray[] = 
    {   
        D3DFMT_X8R8G8B8, 
        D3DFMT_X1R5G5B5, 
        D3DFMT_R5G6B5, 
        D3DFMT_A2R10G10B10
    };
    const UINT allowedAdapterFormatArrayCount  = sizeof(allowedAdapterFormatArray) / sizeof(allowedAdapterFormatArray[0]);

    UINT numAdapters = pD3D->GetAdapterCount();
    for (UINT adapterOrdinal = 0; adapterOrdinal < numAdapters; adapterOrdinal++)
    {
        CD3DEnumAdapterInfo* pAdapterInfo = MEMALLOC_NEW(CD3DEnumAdapterInfo);
        if( pAdapterInfo == NULL )
            return E_OUTOFMEMORY;

        pAdapterInfo->AdapterOrdinal = adapterOrdinal;
        pD3D->GetAdapterIdentifier(adapterOrdinal, 0, &pAdapterInfo->AdapterIdentifier);

        // Get list of all display modes on this adapter.  
        // Also build a temporary list of all display adapter formats.
        adapterFormatList.RemoveAll();

        for( UINT iFormatList = 0; iFormatList < allowedAdapterFormatArrayCount; iFormatList++ )
        {
            D3DFORMAT allowedAdapterFormat = allowedAdapterFormatArray[iFormatList];
            UINT numAdapterModes = pD3D->GetAdapterModeCount( adapterOrdinal, allowedAdapterFormat );
            for (UINT mode = 0; mode < numAdapterModes; mode++)
            {
                D3DDISPLAYMODE displayMode;
                pD3D->EnumAdapterModes( adapterOrdinal, allowedAdapterFormat, mode, &displayMode );

                if( displayMode.Width < m_nMinWidth ||
                    displayMode.Height < m_nMinHeight || 
                    displayMode.Width > m_nMaxWidth ||
                    displayMode.Height > m_nMaxHeight || 
                    displayMode.RefreshRate < m_nRefreshMin ||
                    displayMode.RefreshRate > m_nRefreshMax )
                {
                    continue;
                }

                pAdapterInfo->displayModeList.Add( displayMode );
                
                if( !adapterFormatList.Contains(displayMode.Format) )
                    adapterFormatList.Add( displayMode.Format );
            }

        }

        D3DDISPLAYMODE displayMode;
        pD3D->GetAdapterDisplayMode( adapterOrdinal, &displayMode );
        if( !adapterFormatList.Contains(displayMode.Format) )
            adapterFormatList.Add( displayMode.Format );

        // Sort displaymode list
        ::qsort( pAdapterInfo->displayModeList.GetData(), 
               pAdapterInfo->displayModeList.GetSize(), sizeof( D3DDISPLAYMODE ),
               SortModesCallback );

        // Get info for each device on this adapter
        if( FAILED( EnumerateDevices( pAdapterInfo, &adapterFormatList ) ) )
        {
            delete pAdapterInfo;
            continue;
        }

        // If at least one device on this adapter is available and compatible
        // with the app, add the adapterInfo to the list
        if( pAdapterInfo->deviceInfoList.GetSize() > 0 )
        {
            hr = m_AdapterInfoList.Add( pAdapterInfo );
            if( FAILED(hr) )
                return hr;
        } else
            delete pAdapterInfo;
    }

    bool bUniqueDesc = true;
    CD3DEnumAdapterInfo* pAdapterInfo;
    for( NxI32 i=0; i<m_AdapterInfoList.GetSize(); i++ )
    {
        CD3DEnumAdapterInfo* pAdapterInfo1 = m_AdapterInfoList.GetAt(i);

        for( NxI32 j=i+1; j<m_AdapterInfoList.GetSize(); j++ )
        {
            CD3DEnumAdapterInfo* pAdapterInfo2 = m_AdapterInfoList.GetAt(j);
            if( _stricmp( pAdapterInfo1->AdapterIdentifier.Description, 
                          pAdapterInfo2->AdapterIdentifier.Description ) == 0 )
            {
                bUniqueDesc = false;
                break;
            }
        }

        if( !bUniqueDesc )
            break;
    }

    for( NxI32 i=0; i<m_AdapterInfoList.GetSize(); i++ )
    {
        pAdapterInfo = m_AdapterInfoList.GetAt(i);

        MultiByteToWideChar( CP_ACP, 0, 
                             pAdapterInfo->AdapterIdentifier.Description, -1, 
                             pAdapterInfo->szUniqueDescription, 100 );
        pAdapterInfo->szUniqueDescription[100] = 0;

        if( !bUniqueDesc )
        {
            WCHAR sz[100];
            StringCchPrintf( sz, 100, L" (#%d)", pAdapterInfo->AdapterOrdinal );
            StringCchCat( pAdapterInfo->szUniqueDescription, 256, sz );

        }
    }

    return S_OK;
}
HRESULT CMayaManager::Create(MObject obj)
{
    HRESULT hr= S_OK;
    MStatus stat= MS::kSuccess;
    //MItDag FindRoot;
    D3DPRESENT_PARAMETERS pp = {0};
    HWND hShell;
    HMENU hMenu;
    int iMenu;
    MArgList args;
    const MString DirectXShader_UserClassify( "shader/surface" );



    MFnPlugin plugin(obj, "Microsoft", "6.0", "Any");

    MGlobal::displayInfo("DirectX Extensions for Maya - Initialization Beginning");


    MGlobal::displayInfo("DirectX Extensions for Maya - DXCC Initialization Beginning");
    CStringA pathChangeErr;
    if(!AddDxccDllPath(pathChangeErr))
    {
        MString errStr= MString("DirectX Extensions for Maya - Error loading DXCC: ") + pathChangeErr.GetString();
        MGlobal::displayInfo(errStr);
        MessageBoxA((HWND)M3dView::applicationShell(), errStr.asChar(), "DirectX Extensions for Maya - Critical Error", MB_ICONEXCLAMATION);
        DXCC_GOTO_EXIT(e_Exit, false);
    }

#if defined(DEBUG) | defined(_DEBUG)
    DXCCSetDebugPrintfACallback( MayaDebugPrintfACallback ) ;
    DXCCSetDebugPrintfWCallback( MayaDebugPrintfWCallback ) ;
    DXCCSetBreakPointCallback( DXCCBreakPointDefaultCallback ) ;
    //g_DebugBasic= true;
    //g_DebugExtreme= true;
#endif

    MGlobal::displayInfo("DirectX Extensions for Maya - DXCC Initialization Complete");


    MGlobal::displayInfo("DirectX Extensions for Maya - DXUT Initialization Beginning");
    if( NULL == DXUTGetD3DObject() )
    {
        if(DXCC_FAILED( DXUTInit( false, false, false ) ))
        {
            MString errStr= MString("DirectX Extensions for Maya - Error loading DXUT: Most likely caused by D3D or D3DX DLL miss-match.");
            MGlobal::displayInfo(errStr);
            MessageBoxA((HWND)M3dView::applicationShell(), errStr.asChar(), "DirectX Extensions for Maya - Critical Error", MB_ICONEXCLAMATION);
            DXCC_GOTO_EXIT(e_Exit, false);
        }


        DXCC_ASSERT( DXUTGetD3DObject() != NULL );
    }
    MGlobal::displayInfo("DirectX Extensions for Maya - DXUT Initialization Complete");


    //-----------------------INITIALIZATION ------------------------//

    hr= CManager::Create();
    if(DXCC_FAILED(hr))
    {
        MGlobal::displayError("DirectX Extensions for Maya - Error");
        MGlobal::displayError("Could not initialize base plugin manager");
        DXCC_GOTO_EXIT(e_Exit, false);
    }

    DeviceCreatedEvent= CreateEventA(NULL, FALSE, FALSE, "DXCCManager_DeviceCreated");
    InitializeCriticalSection(&DeviceAndViewerSection);


    MGlobal::displayInfo("DirectX Extensions for Maya - Engine/Device Initialization Beginning");
    g_Engine.Create(this);
    MGlobal::displayInfo("DirectX Extensions for Maya - Engine/Device Initialization Complete");

    SetEngine(&g_Engine);

    pp.BackBufferWidth = 640;
    pp.BackBufferHeight = 480;
    pp.BackBufferCount = 1;
    pp.MultiSampleType = D3DMULTISAMPLE_NONE;
    pp.MultiSampleQuality = 0;
    pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    pp.hDeviceWindow = NULL;
    pp.Windowed = true;
    pp.EnableAutoDepthStencil = true;
    pp.AutoDepthStencilFormat = D3DFMT_D16;
    pp.FullScreen_RefreshRateInHz = 0;
    pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

    MGlobal::displayInfo("DirectX Extensions for Maya - Viewer Initialization Beginning");
    g_Viewer.Create( &g_PreviewPipeline, &pp );
    MGlobal::displayInfo("DirectX Extensions for Maya - Viewer Initialization Complete");


    MGlobal::displayInfo("DirectX Extensions for Maya - SyncGraph Initialization Beginning");
    TagGraph.Initialize("DirectXTag", "DxTag");
    TagGraph.AddAdapter(&TagGraphLayerAdapter);
    TagGraph.AddAdapter(&TagGraphDagAdapter);
    TagGraph.AddAdapter(&TagGraphMeshAdapter);
    TagGraph.AddAdapter(&TagGraphShaderAdapter);
    TagGraph.AddAdapter(&TagGraphStdMtlAdapter);
    TagGraph.SetCallbackState(true);
    TagGraph.RegisterNodes();
    MGlobal::displayInfo("DirectX Extensions for Maya - SyncGraph Initialization Complete");


    MGlobal::displayInfo("DirectX Extensions for Maya - DirectXShader Initialization Beginning");
    DirectXShader::StaticInitialize();
    stat= plugin.registerNode(
              "DirectXShader",
              DirectXShader::id,
              DirectXShader::creator,
              DirectXShader::initialize,
              MPxNode::kDependNode,
              &DirectXShader_UserClassify );
    DXCHECK_MSTATUS(stat);
    MGlobal::displayInfo("DirectX Extensions for Maya - DirectXShader Initialization Complete");

    MGlobal::displayInfo("DirectX Extensions for Maya - XFileTranslator Initialization Beginning");
    // register the translator
    stat = plugin.registerFileTranslator(DXCC_EXPORTER,	// name
                                         NULL,			// icon
                                         XFileTranslator::creator,
                                         NULL,			// script
                                         NULL,
                                         false);
    DXCHECK_MSTATUS(stat);
    MGlobal::displayInfo("DirectX Extensions for Maya - XFileTranslator Initialization Complete");


    /*
    	//DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_EXPORT_SELECTED,  DXCCExportSelectedCommand::creator ));
    */
    CallbackId_Exiting= MSceneMessage::addCallback( MSceneMessage::kMayaExiting, CMayaManager::Callback_MayaExiting, NULL, NULL);
    CallbackId_TimeChanged= MEventMessage::addEventCallback ( "timeChanged", CMayaManager::DispatchTimeChanged, NULL, NULL);

    MGlobal::displayInfo("DirectX Extensions for Maya - MenuCommands Initialization Beginning");
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXM_UIHELP ,  DXMUiHelpCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_PREVIEW_CHANGED ,  DXCCPreviewChangedCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_EXPORT_SCENE,  DXCCExportSceneCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_EXPORT_OPTIONS,  DXCCExportOptionsCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_REBUILD_SCENE,  DXCCRebuildSceneCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_REBUILD_DIRTY,  DCCRebuildDirtyCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_VIEW_FLOATING,  DXCCFloatingViewCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_VIEW_CLOSE,  DXCCCloseViewCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_VIEW_FRONT,  DXCCFrontViewCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_VIEW_SIDE,  DXCCSideViewCommand::creator ));
    DXCHECK_MSTATUS(stat= plugin.registerCommand( DXCC_VIEW_TOP,  DXCCTopViewCommand::creator ));

    MGlobal::displayInfo("DirectX Extensions for Maya - MenuCommands Initialization Complete");

    //Menu Creation
    MGlobal::displayInfo("DirectX Extensions for Maya - Menu Initialization Beginning");
    MGlobal::executeCommand("eval( \"source DirectX\" )", true, true);
    MGlobal::displayInfo("DirectX Extensions for Maya - Menu Initialization Complete");


    //Menu Capture IDs
    MGlobal::displayInfo("DirectX Extensions for Maya - UpdateTimer Initialization Beginning");
    hShell= (HWND)M3dView::applicationShell();
    hMenu= GetMenu(hShell);
    for(iMenu= 0; iMenu < GetMenuItemCount(hMenu); iMenu++)
    {
        char szMenu[MAX_PATH];
        MENUITEMINFOA info;
        ZeroMemory(&info, sizeof(MENUITEMINFO));
        info.cbSize= sizeof(MENUITEMINFO);
        info.fMask= MIIM_STRING|MIIM_SUBMENU;
        info.dwTypeData= szMenu;
        info.cch= MAX_PATH;

        GetMenuItemInfoA(hMenu, iMenu, TRUE, &info);
        szMenu[MAX_PATH-1]= '\0';

        if(0 == lstrcmpiA(szMenu, "DirectX"))
        {
            HMENU hDirectXMenu= info.hSubMenu ;

            for(iMenu= 0; iMenu < GetMenuItemCount(hDirectXMenu); iMenu++)
            {
                ZeroMemory(&info, sizeof(MENUITEMINFO));
                info.cbSize= sizeof(MENUITEMINFO);
                info.fMask= MIIM_STRING|MIIM_SUBMENU;
                info.dwTypeData= szMenu;
                info.cch= MAX_PATH;

                GetMenuItemInfoA(hDirectXMenu, iMenu, TRUE, &info);
                szMenu[MAX_PATH-1]= '\0';

                if(0 == lstrcmpiA(szMenu, "Rebuild Dirty"))
                {
                    m_RebuildDirty_MenuId= (WPARAM)MAKELONG(GetMenuItemID(hDirectXMenu, iMenu), 0);
                    m_RebuildDirtyTimer= SetTimer(NULL, NULL, 200, CMayaManager::TimerProc);
                    break;
                }
            }
            break;
        }
    }
    MGlobal::displayInfo("DirectX Extensions for Maya - UpdateTimer Initialization Complete");

    MGlobal::displayInfo("DirectX Extensions for Maya - UI Options Loading Beginning");
    UI_LoadOptions();
    MGlobal::displayInfo("DirectX Extensions for Maya - UI Options Loading Complete");


    MGlobal::displayInfo("DirectX Extensions for Maya - State Query Command Registration Beginning");
    DXMUnitTests::RegisterCommands(plugin);
    MGlobal::displayInfo("DirectX Extensions for Maya - State Query Command Registration Complete");


    MGlobal::displayInfo("DirectX Extensions for Maya - Initialization Complete");



e_Exit:
    return hr;
}