forked from walbourn/directx-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVidMemViaD3D9.cpp
105 lines (90 loc) · 3.18 KB
/
VidMemViaD3D9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//----------------------------------------------------------------------------
// File: VidMemViaD3D9.cpp
//
// This method queries D3D9 for the amount of available texture memory. On
// Windows Vista, this number is typically the dedicated video memory plus
// the shared system memory minus the amount of memory in use by textures
// and render targets.
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License (MIT).
//-----------------------------------------------------------------------------
#define INITGUID
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <d3d9.h>
//-----------------------------------------------------------------------------
// Defines, and constants
//-----------------------------------------------------------------------------
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=nullptr; } }
#endif
HRESULT GetVideoMemoryViaD3D9( HMONITOR hMonitor, UINT* pdwAvailableTextureMem )
{
HRESULT hr;
bool bGotMemory = false;
*pdwAvailableTextureMem = 0;
IDirect3D9* pD3D9 = nullptr;
pD3D9 = Direct3DCreate9( D3D_SDK_VERSION );
if( pD3D9 )
{
UINT dwAdapterCount = pD3D9->GetAdapterCount();
for( UINT iAdapter = 0; iAdapter < dwAdapterCount; iAdapter++ )
{
IDirect3DDevice9* pd3dDevice = nullptr;
HMONITOR hAdapterMonitor = pD3D9->GetAdapterMonitor( iAdapter );
if( hMonitor != hAdapterMonitor )
continue;
HWND hWnd = GetDesktopWindow();
D3DPRESENT_PARAMETERS pp = {};
pp.BackBufferWidth = 800;
pp.BackBufferHeight = 600;
pp.BackBufferFormat = D3DFMT_R5G6B5;
pp.BackBufferCount = 1;
pp.MultiSampleType = D3DMULTISAMPLE_NONE;
pp.MultiSampleQuality = 0;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
pp.hDeviceWindow = hWnd;
pp.Windowed = TRUE;
pp.EnableAutoDepthStencil = FALSE;
pp.Flags = 0;
pp.FullScreen_RefreshRateInHz = 0;
pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
hr = pD3D9->CreateDevice( iAdapter, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &pd3dDevice );
if( SUCCEEDED( hr ) )
{
*pdwAvailableTextureMem = pd3dDevice->GetAvailableTextureMem();
bGotMemory = true;
SAFE_RELEASE( pd3dDevice );
}
}
SAFE_RELEASE( pD3D9 );
}
if( bGotMemory )
return S_OK;
else
return E_FAIL;
}
HRESULT GetHMonitorFromD3D9Device( IDirect3DDevice9* pd3dDevice, HMONITOR hMonitor )
{
D3DDEVICE_CREATION_PARAMETERS cp;
hMonitor = nullptr;
bool bFound = false;
if( SUCCEEDED( pd3dDevice->GetCreationParameters( &cp ) ) )
{
IDirect3D9* pD3D = nullptr;
if( SUCCEEDED( pd3dDevice->GetDirect3D( &pD3D ) ) )
{
hMonitor = pD3D->GetAdapterMonitor( cp.AdapterOrdinal );
bFound = true;
}
pD3D->Release();
}
if( bFound )
return S_OK;
else
return E_FAIL;
}