HRESULT WINAPI BaseDirect3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9** ppReturnedDeviceInterface) { // load configuration file ProxyHelper helper = ProxyHelper(); ProxyHelper::ProxyConfig cfg; bool stereoificatorCfgLoaded = helper.LoadConfig(cfg); if (cfg.forceAdapterNumber >= (int)GetAdapterCount()) { OutputDebugString("[ERR] forceAdapterNumber outside of range of valid adapters. Using original Adapter instead.\n"); } // Create real interface HRESULT hResult = m_pD3D->CreateDevice( (cfg.forceAdapterNumber >= (int)GetAdapterCount() || (cfg.forceAdapterNumber < 0)) ? Adapter : cfg.forceAdapterNumber, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface); if(FAILED(hResult)) return hResult; OutputDebugString("[OK] Normal D3D device created\n"); char buf[64]; sprintf_s(buf, "Number of back buffers = %d\n", pPresentationParameters->BackBufferCount); OutputDebugString(buf); if(!stereoificatorCfgLoaded) { OutputDebugString("[ERR] Config loading failed, config could not be loaded. Returning normal D3DDevice. Stereoificator will not be active.\n"); return hResult; } OutputDebugString("[OK] Config loading - OK\n"); char buf1[32]; LPCSTR psz = NULL; wsprintf(buf1,"Config type: %d", cfg.game_type); psz = buf1; OutputDebugString(psz); OutputDebugString("\n"); // Create and return proxy D3DProxyDevice* newDev = new D3DProxyDevice(*ppReturnedDeviceInterface, this, cfg); *ppReturnedDeviceInterface = newDev; OutputDebugString("[OK] Stereoificator D3D device created.\n"); return hResult; }
//----------------------------------------------------------------------------- // Name : FindBestFullscreenMode () // Desc : Find the best fullscreen mode, and fill out our D3DSettings structure. //----------------------------------------------------------------------------- bool CD3DInitialize::FindBestFullscreenMode( CD3DSettings & D3DSettings, D3DDISPLAYMODE * pMatchMode, bool bRequireHAL, bool bRequireREF ) { // For fullscreen, default to first HAL option that supports the current desktop // display mode, or any display mode if HAL is not compatible with the desktop mode, or // non-HAL if no HAL is available ULONG i, j, k; D3DDISPLAYMODE AdapterDisplayMode; D3DDISPLAYMODE BestAdapterDisplayMode; D3DDISPLAYMODE BestDisplayMode; CD3DEnumAdapter *pBestAdapter = NULL; CD3DEnumDevice *pBestDevice = NULL; CD3DEnumDeviceOptions *pBestOptions = NULL; CD3DSettings::Settings *pSettings = NULL; BestAdapterDisplayMode.Width = 0; BestAdapterDisplayMode.Height = 0; BestAdapterDisplayMode.Format = D3DFMT_UNKNOWN; BestAdapterDisplayMode.RefreshRate = 0; // Loop through each adapter for( i = 0; i < GetAdapterCount(); i++ ) { CD3DEnumAdapter * pAdapter = m_vpAdapters[ i ]; // Retrieve the desktop display mode m_pD3D->GetAdapterDisplayMode( pAdapter->Ordinal, &AdapterDisplayMode ); // If any settings were passed, overwrite to test for matches if ( pMatchMode ) { if ( pMatchMode->Width != 0 ) AdapterDisplayMode.Width = pMatchMode->Width; if ( pMatchMode->Height != 0 ) AdapterDisplayMode.Height = pMatchMode->Height; if ( pMatchMode->Format != D3DFMT_UNKNOWN ) AdapterDisplayMode.Format = pMatchMode->Format; if ( pMatchMode->RefreshRate != 0 ) AdapterDisplayMode.RefreshRate = pMatchMode->RefreshRate; } // End if match mode passed // Loop through each device for( j = 0; j < pAdapter->Devices.size(); j++ ) { CD3DEnumDevice * pDevice = pAdapter->Devices[ j ]; // Skip if this is not of the required type if ( bRequireHAL && pDevice->DeviceType != D3DDEVTYPE_HAL ) continue; if ( bRequireREF && pDevice->DeviceType != D3DDEVTYPE_REF ) continue; // Loop through each option set for ( k = 0; k < pDevice->Options.size(); k++ ) { CD3DEnumDeviceOptions * pOptions = pDevice->Options[ k ]; // Determine if back buffer format matches adapter bool MatchedBB = (pOptions->BackBufferFormat == pOptions->AdapterFormat ); bool MatchedDesktop = (pOptions->AdapterFormat == AdapterDisplayMode.Format); // Skip if this is not fullscreen if ( pOptions->Windowed ) continue; // If we haven't found a compatible option set yet, or if this set // is better (because it's HAL / formats match better) then save it. if ( pBestOptions == NULL || (pBestOptions->DeviceType != D3DDEVTYPE_HAL && pDevice->DeviceType == D3DDEVTYPE_HAL ) || (pOptions->DeviceType == D3DDEVTYPE_HAL && pBestOptions->AdapterFormat != AdapterDisplayMode.Format && MatchedDesktop ) || (pOptions->DeviceType == D3DDEVTYPE_HAL && MatchedDesktop && MatchedBB) ) { // Store best so far BestAdapterDisplayMode = AdapterDisplayMode; pBestAdapter = pAdapter; pBestDevice = pDevice; pBestOptions = pOptions; if ( pOptions->DeviceType == D3DDEVTYPE_HAL && MatchedDesktop && MatchedBB ) { // This fullscreen device option looks great -- take it goto EndFullscreenDeviceOptionSearch; } } // End if not a better match } // Next Option Set } // Next Device Type } // Next Adapter EndFullscreenDeviceOptionSearch: if ( pBestOptions == NULL) return false; // Need to find a display mode on the best adapter that uses pBestOptions->AdapterFormat // and is as close to BestAdapterDisplayMode's res as possible BestDisplayMode.Width = 0; BestDisplayMode.Height = 0; BestDisplayMode.Format = D3DFMT_UNKNOWN; BestDisplayMode.RefreshRate = 0; // Loop through valid display modes for( i = 0; i < pBestAdapter->Modes.size(); i++ ) { D3DDISPLAYMODE Mode = pBestAdapter->Modes[ i ]; // Skip if it doesn't match our best format if( Mode.Format != pBestOptions->AdapterFormat ) continue; // Determine how good a match this is if( Mode.Width == BestAdapterDisplayMode.Width && Mode.Height == BestAdapterDisplayMode.Height && Mode.RefreshRate == BestAdapterDisplayMode.RefreshRate ) { // found a perfect match, so stop BestDisplayMode = Mode; break; } // End if Perfect Match else if( Mode.Width == BestAdapterDisplayMode.Width && Mode.Height == BestAdapterDisplayMode.Height && Mode.RefreshRate > BestDisplayMode.RefreshRate ) { // refresh rate doesn't match, but width/height match, so keep this // and keep looking BestDisplayMode = Mode; } else if( Mode.Width == BestAdapterDisplayMode.Width ) { // width matches, so keep this and keep looking BestDisplayMode = Mode; } else if( BestDisplayMode.Width == 0 ) { // we don't have anything better yet, so keep this and keep looking BestDisplayMode = Mode; } // End if } // Next Mode // Fill out passed settings details D3DSettings.Windowed = false; pSettings = D3DSettings.GetSettings(); pSettings->AdapterOrdinal = pBestOptions->AdapterOrdinal; pSettings->DisplayMode = BestDisplayMode; pSettings->DeviceType = pBestOptions->DeviceType; pSettings->BackBufferFormat = pBestOptions->BackBufferFormat; pSettings->DepthStencilFormat = pBestOptions->DepthFormats[ 0 ]; pSettings->MultisampleType = pBestOptions->MultiSampleTypes[ 0 ]; pSettings->MultisampleQuality = 0; pSettings->VertexProcessingType = pBestOptions->VertexProcessingTypes[ 0 ]; pSettings->PresentInterval = pBestOptions->PresentIntervals[ 0 ]; // Success! return true; }
//----------------------------------------------------------------------------- // Name : FindBestWindowedMode () // Desc : Find the best windowed mode, and fill out our D3DSettings structure. //----------------------------------------------------------------------------- bool CD3DInitialize::FindBestWindowedMode( CD3DSettings & D3DSettings, bool bRequireHAL, bool bRequireREF ) { ULONG i, j, k; D3DDISPLAYMODE DisplayMode; CD3DEnumAdapter *pBestAdapter = NULL; CD3DEnumDevice *pBestDevice = NULL; CD3DEnumDeviceOptions *pBestOptions = NULL; CD3DSettings::Settings *pSettings = NULL; // Retrieve the primary adapters display mode. m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &DisplayMode); // Loop through each adapter for( i = 0; i < GetAdapterCount(); i++ ) { CD3DEnumAdapter * pAdapter = m_vpAdapters[ i ]; // Loop through each device for( j = 0; j < pAdapter->Devices.size(); j++ ) { CD3DEnumDevice * pDevice = pAdapter->Devices[ j ]; // Skip if this is not of the required type if ( bRequireHAL && pDevice->DeviceType != D3DDEVTYPE_HAL ) continue; if ( bRequireREF && pDevice->DeviceType != D3DDEVTYPE_REF ) continue; // Loop through each option set for ( k = 0; k < pDevice->Options.size(); k++ ) { CD3DEnumDeviceOptions * pOptions = pDevice->Options[ k ]; // Determine if back buffer format matches adapter bool MatchedBB = (pOptions->BackBufferFormat == pOptions->AdapterFormat ); // Skip if this is not windowed, and formats don't match if (!pOptions->Windowed) continue; if ( pOptions->AdapterFormat != DisplayMode.Format) continue; // If we haven't found a compatible option set yet, or if this set // is better (because it's HAL / formats match better) then save it. if( pBestOptions == NULL || (pOptions->DeviceType == D3DDEVTYPE_HAL && MatchedBB ) || (pBestOptions->DeviceType != D3DDEVTYPE_HAL && pOptions->DeviceType == D3DDEVTYPE_HAL) ) { // Store best so far pBestAdapter = pAdapter; pBestDevice = pDevice; pBestOptions = pOptions; if ( pOptions->DeviceType == D3DDEVTYPE_HAL && MatchedBB ) { // This windowed device option looks great -- take it goto EndWindowedDeviceOptionSearch; } } // End if not a better match } // Next Option Set } // Next Device Type } // Next Adapter EndWindowedDeviceOptionSearch: if ( pBestOptions == NULL ) return false; // Fill out passed settings details D3DSettings.Windowed = true; pSettings = D3DSettings.GetSettings(); pSettings->AdapterOrdinal = pBestOptions->AdapterOrdinal; pSettings->DisplayMode = DisplayMode; pSettings->DeviceType = pBestOptions->DeviceType; pSettings->BackBufferFormat = pBestOptions->BackBufferFormat; pSettings->DepthStencilFormat = pBestOptions->DepthFormats[ 0 ]; pSettings->MultisampleType = pBestOptions->MultiSampleTypes[ 0 ]; pSettings->MultisampleQuality = 0; pSettings->VertexProcessingType = pBestOptions->VertexProcessingTypes[ 0 ]; pSettings->PresentInterval = pBestOptions->PresentIntervals[ 0 ]; // We found a mode return true; }