📄 d3denumeration.cpp
字号:
//-----------------------------------------------------------------------------
// File: D3DEnumeration.cpp
//
// Desc: Enumerates D3D adapters, devices, modes, etc.
//-----------------------------------------------------------------------------
#define STRICT
#include "stdafx.h"
#include <windows.h>
#include "D3DEnumeration.h"
//-----------------------------------------------------------------------------
// Name: ColorChannelBits
// Desc: Returns the number of color channel bits in the specified D3DFORMAT
//-----------------------------------------------------------------------------
static UINT ColorChannelBits( D3DFORMAT fmt )
{
switch( fmt )
{
case D3DFMT_R8G8B8:
return 8;
case D3DFMT_A8R8G8B8:
return 8;
case D3DFMT_X8R8G8B8:
return 8;
case D3DFMT_R5G6B5:
return 5;
case D3DFMT_X1R5G5B5:
return 5;
case D3DFMT_A1R5G5B5:
return 5;
case D3DFMT_A4R4G4B4:
return 4;
case D3DFMT_R3G3B2:
return 2;
case D3DFMT_A8R3G3B2:
return 2;
case D3DFMT_X4R4G4B4:
return 4;
case D3DFMT_A2B10G10R10:
return 10;
case D3DFMT_A2R10G10B10:
return 10;
default:
return 0;
}
}
//-----------------------------------------------------------------------------
// Name: AlphaChannelBits
// Desc: Returns the number of alpha channel bits in the specified D3DFORMAT
//-----------------------------------------------------------------------------
static UINT AlphaChannelBits( D3DFORMAT fmt )
{
switch( fmt )
{
case D3DFMT_R8G8B8:
return 0;
case D3DFMT_A8R8G8B8:
return 8;
case D3DFMT_X8R8G8B8:
return 0;
case D3DFMT_R5G6B5:
return 0;
case D3DFMT_X1R5G5B5:
return 0;
case D3DFMT_A1R5G5B5:
return 1;
case D3DFMT_A4R4G4B4:
return 4;
case D3DFMT_R3G3B2:
return 0;
case D3DFMT_A8R3G3B2:
return 8;
case D3DFMT_X4R4G4B4:
return 0;
case D3DFMT_A2B10G10R10:
return 2;
case D3DFMT_A2R10G10B10:
return 2;
default:
return 0;
}
}
//-----------------------------------------------------------------------------
// Name: DepthBits
// Desc: Returns the number of depth bits in the specified D3DFORMAT
//-----------------------------------------------------------------------------
static UINT DepthBits( D3DFORMAT fmt )
{
switch( fmt )
{
case D3DFMT_D16:
return 16;
case D3DFMT_D15S1:
return 15;
case D3DFMT_D24X8:
return 24;
case D3DFMT_D24S8:
return 24;
case D3DFMT_D24X4S4:
return 24;
case D3DFMT_D32:
return 32;
default:
return 0;
}
}
//-----------------------------------------------------------------------------
// Name: StencilBits
// Desc: Returns the number of stencil bits in the specified D3DFORMAT
//-----------------------------------------------------------------------------
static UINT StencilBits( D3DFORMAT fmt )
{
switch( fmt )
{
case D3DFMT_D16:
return 0;
case D3DFMT_D15S1:
return 1;
case D3DFMT_D24X8:
return 0;
case D3DFMT_D24S8:
return 8;
case D3DFMT_D24X4S4:
return 4;
case D3DFMT_D32:
return 0;
default:
return 0;
}
}
//-----------------------------------------------------------------------------
// Name: D3DAdapterInfo destructor
// Desc:
//-----------------------------------------------------------------------------
D3DAdapterInfo::~D3DAdapterInfo( void )
{
delete pDisplayModeList;
if (pDeviceInfoList != NULL)
{
for(int n=0; n<(int)pDeviceInfoList->GetArrayCount(); n++)
{
D3DDeviceInfo * pDeviceInfo = pDeviceInfoList->Get(n);
delete pDeviceInfo;
}
delete pDeviceInfoList;
}
}
//-----------------------------------------------------------------------------
// Name: D3DDeviceInfo destructor
// Desc:
//-----------------------------------------------------------------------------
D3DDeviceInfo::~D3DDeviceInfo( void )
{
if( pDeviceComboList != NULL )
{
for(int n=0; n<(int)pDeviceComboList->GetArrayCount(); n++)
{
D3DDeviceCombo * pDeviceCombo = pDeviceComboList->Get(n);
delete pDeviceCombo;
}
delete pDeviceComboList;
}
}
//-----------------------------------------------------------------------------
// Name: D3DDeviceCombo destructor
// Desc:
//-----------------------------------------------------------------------------
D3DDeviceCombo::~D3DDeviceCombo( void )
{
if( pDepthStencilFormatList != NULL )
delete pDepthStencilFormatList;
if( pMultiSampleTypeList != NULL )
delete pMultiSampleTypeList;
if( pMultiSampleQualityList != NULL )
delete pMultiSampleQualityList;
if( pDSMSConflictList != NULL )
delete pDSMSConflictList;
if( pVertexProcessingTypeList != NULL )
delete pVertexProcessingTypeList;
if( pPresentIntervalList != NULL )
delete pPresentIntervalList;
}
//-----------------------------------------------------------------------------
// Name: CD3DEnumeration constructor
// Desc:
//-----------------------------------------------------------------------------
CD3DEnumeration::CD3DEnumeration()
{
m_pAdapterInfoList = NULL;
m_pAllowedAdapterFormatList = NULL;
AppMinFullscreenWidth = 640;
AppMinFullscreenHeight = 480;
AppMinColorChannelBits = 5;
AppMinAlphaChannelBits = 0;
AppMinDepthBits = 15;
AppMinStencilBits = 0;
AppUsesDepthBuffer = false;
AppUsesMixedVP = false;
AppRequiresWindowed = false;
AppRequiresFullscreen = false;
}
//-----------------------------------------------------------------------------
// Name: CD3DEnumeration destructor
// Desc:
//-----------------------------------------------------------------------------
CD3DEnumeration::~CD3DEnumeration()
{
if( m_pAdapterInfoList != NULL )
{
for( UINT iai = 0; iai < m_pAdapterInfoList->GetArrayCount(); iai++ )
{
D3DAdapterInfo * pAdapterInfo = m_pAdapterInfoList->Get(iai);
delete pAdapterInfo;
}
delete m_pAdapterInfoList;
}
// SAFE_DELETE( m_pAllowedAdapterFormatList );
delete m_pAllowedAdapterFormatList;
}
//-----------------------------------------------------------------------------
// Name: SortModesCallback
// Desc: Used to sort D3DDISPLAYMODEs
//-----------------------------------------------------------------------------
static bool __cdecl SortModesCallback(const D3DDISPLAYMODE & rDMode1, const D3DDISPLAYMODE & rDMode2)
{
if (rDMode1.Width > rDMode2.Width)
return true;
if (rDMode1.Width < rDMode2.Width)
return false;
if (rDMode1.Height > rDMode2.Height)
return true;
if (rDMode1.Height < rDMode2.Height)
return false;
if (rDMode1.Format > rDMode2.Format)
return true;
if (rDMode1.Format < rDMode2.Format)
return false;
if (rDMode1.RefreshRate > rDMode2.RefreshRate)
return true;
if (rDMode1.RefreshRate < rDMode2.RefreshRate)
return false;
return false;
}
//-----------------------------------------------------------------------------
// Name: Enumerate
// Desc: Enumerates available D3D adapters, devices, modes, etc.
//-----------------------------------------------------------------------------
bool IsFormatEqual(const D3DFORMAT & format1, const D3DFORMAT & format2)
{
return (format1 == format2);
}
HRESULT CD3DEnumeration::Enumerate()
{
HRESULT hr;
Array<D3DFORMAT> adapterFormatList(10);
if( m_pD3D == NULL )
return E_FAIL;
m_pAdapterInfoList = new Array<D3DAdapterInfo *>(10);
if( m_pAdapterInfoList == NULL )
return E_OUTOFMEMORY;
m_pAllowedAdapterFormatList = new Array<D3DFORMAT>(10);
if( m_pAllowedAdapterFormatList == NULL )
return E_OUTOFMEMORY;
D3DFORMAT fmt;
m_pAllowedAdapterFormatList->Add( ( fmt = D3DFMT_A8R8G8B8 ) );
m_pAllowedAdapterFormatList->Add( ( fmt = D3DFMT_X1R5G5B5 ) );
m_pAllowedAdapterFormatList->Add( ( fmt = D3DFMT_R5G6B5 ) );
m_pAllowedAdapterFormatList->Add( ( fmt = D3DFMT_A2R10G10B10 ) );
D3DAdapterInfo* pAdapterInfo = NULL;
UINT numAdapters = m_pD3D->GetAdapterCount();
for (UINT adapterOrdinal = 0; adapterOrdinal < numAdapters; adapterOrdinal++)
{
pAdapterInfo = new D3DAdapterInfo;
if( pAdapterInfo == NULL )
return E_OUTOFMEMORY;
pAdapterInfo->pDisplayModeList = new Array<D3DDISPLAYMODE>(30);
pAdapterInfo->pDeviceInfoList = new Array<D3DDeviceInfo *>(10);
if( pAdapterInfo->pDisplayModeList == NULL ||
pAdapterInfo->pDeviceInfoList == NULL )
{
delete pAdapterInfo;
return E_OUTOFMEMORY;
}
pAdapterInfo->AdapterOrdinal = adapterOrdinal;
m_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.Clear();
for( UINT iaaf = 0; iaaf < m_pAllowedAdapterFormatList->GetArrayCount(); iaaf++ )
{
D3DFORMAT allowedAdapterFormat = m_pAllowedAdapterFormatList->Get( iaaf );
UINT numAdapterModes = m_pD3D->GetAdapterModeCount( adapterOrdinal, allowedAdapterFormat );
for (UINT mode = 0; mode < numAdapterModes; mode++)
{
D3DDISPLAYMODE displayMode;
m_pD3D->EnumAdapterModes( adapterOrdinal, allowedAdapterFormat, mode, &displayMode );
if( displayMode.Width < AppMinFullscreenWidth ||
displayMode.Height < AppMinFullscreenHeight ||
ColorChannelBits(displayMode.Format) < AppMinColorChannelBits )
{
continue;
}
pAdapterInfo->pDisplayModeList->Add(displayMode);
// Add format if it doesn't already exist
if (!adapterFormatList.Contains(displayMode.Format, IsFormatEqual))
adapterFormatList.Add(displayMode.Format);
/*
for (size_t t=0; t<adapterFormatList.GetArrayCount(); t++)
{
D3DFORMAT testFormat = adapterFormatList[t];
if (testFormat == displayMode.Format)
break;
}
if(t == adapterFormatList.GetArrayCount())
adapterFormatList.Add(displayMode.Format);
*/
}
}
// Sort displaymode list
pAdapterInfo->pDisplayModeList->Sort(SortModesCallback);
// Get info for each device on this adapter
if( FAILED( hr = EnumerateDevices( pAdapterInfo, &adapterFormatList ) ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -