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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include "UnityPrefix.h"
#include "D3D9Utils.h"
#include "Runtime/Utilities/ArrayUtility.h"
#include "Runtime/Shaders/GraphicsCaps.h"
#ifdef DUMMY_D3D9_CALLS
HRESULT CallDummyD3D9Function()
{
return S_OK;
}
#endif
struct D3D9Error {
HRESULT hr;
const char* message;
};
static D3D9Error s_D3DErrors[] = {
{ D3DOK_NOAUTOGEN, "no mipmap autogen" },
{ D3DERR_WRONGTEXTUREFORMAT, "wrong texture format" },
{ D3DERR_UNSUPPORTEDCOLOROPERATION, "unsupported color op" },
{ D3DERR_UNSUPPORTEDCOLORARG, "unsupported color arg" },
{ D3DERR_UNSUPPORTEDALPHAOPERATION, "unsupported alpha op" },
{ D3DERR_UNSUPPORTEDALPHAARG, "unsupported alpha arg" },
{ D3DERR_TOOMANYOPERATIONS, "too many texture operations" },
{ D3DERR_CONFLICTINGTEXTUREFILTER, "conflicting texture filters" },
{ D3DERR_UNSUPPORTEDFACTORVALUE, "unsupported factor value" },
{ D3DERR_CONFLICTINGRENDERSTATE, "conflicting render states" },
{ D3DERR_UNSUPPORTEDTEXTUREFILTER, "unsupported texture filter" },
{ D3DERR_CONFLICTINGTEXTUREPALETTE, "conflicting texture palettes" },
{ D3DERR_DRIVERINTERNALERROR, "internal driver error" },
{ D3DERR_NOTFOUND, "requested item not found" },
{ D3DERR_MOREDATA, "more data than fits into buffer" },
{ D3DERR_DEVICELOST, "device lost" },
{ D3DERR_DEVICENOTRESET, "device not reset" },
{ D3DERR_NOTAVAILABLE, "queried technique not available" },
{ D3DERR_OUTOFVIDEOMEMORY, "out of VRAM" },
{ D3DERR_INVALIDDEVICE, "invalid device" },
{ D3DERR_INVALIDCALL, "invalid call" },
{ D3DERR_DRIVERINVALIDCALL, "driver invalid call" },
{ D3DERR_WASSTILLDRAWING, "was still drawing" },
{ S_OK, "S_OK" },
{ E_FAIL, "E_FAIL" },
{ E_INVALIDARG, "E_INVALIDARG" },
{ E_OUTOFMEMORY, "out of memory" },
};
const char* GetD3D9Error( HRESULT hr )
{
for( int i = 0; i < ARRAY_SIZE(s_D3DErrors); ++i )
{
if( hr == s_D3DErrors[i].hr )
return s_D3DErrors[i].message;
}
static char buffer[1000];
sprintf( buffer, "unknown error, code 0x%X", hr );
return buffer;
}
int GetBPPFromD3DFormat( D3DFORMAT format )
{
switch( format ) {
case D3DFMT_UNKNOWN:
case kD3D9FormatNULL:
return 0;
case D3DFMT_X8R8G8B8:
case D3DFMT_A8R8G8B8:
case D3DFMT_A2R10G10B10:
case D3DFMT_A2B10G10R10:
case D3DFMT_R8G8B8:
case D3DFMT_A8B8G8R8:
case D3DFMT_R32F:
case D3DFMT_D24X8:
case D3DFMT_D24S8:
case D3DFMT_D24X4S4:
case kD3D9FormatINTZ:
case kD3D9FormatRAWZ:
return 32;
case D3DFMT_X1R5G5B5:
case D3DFMT_A1R5G5B5:
case D3DFMT_A4R4G4B4:
case D3DFMT_X4R4G4B4:
case D3DFMT_R5G6B5:
case D3DFMT_R16F:
case D3DFMT_D16:
case D3DFMT_D15S1:
case D3DFMT_D16_LOCKABLE:
case D3DFMT_L16:
case D3DFMT_A8L8:
case kD3D9FormatDF16:
return 16;
case D3DFMT_A16B16G16R16F:
return 64;
case D3DFMT_A32B32G32R32F:
return 128;
case D3DFMT_DXT1:
return 4;
case D3DFMT_A8:
case D3DFMT_L8:
case D3DFMT_DXT3:
case D3DFMT_DXT5:
return 8;
default:
ErrorString( Format("Unknown D3D format %x", format) );
return 32;
}
}
int GetStencilBitsFromD3DFormat (D3DFORMAT fmt)
{
switch( fmt ) {
case D3DFMT_D15S1: return 1;
case D3DFMT_D24S8: return 8;
case D3DFMT_D24X4S4: return 4;
default: return 0;
}
}
D3DMULTISAMPLE_TYPE GetD3DMultiSampleType (int samples)
{
// Optimizer should take care of this, since value of D3DMULTISAMPLE_N_SAMPLES is N
switch( samples ) {
case 0:
case 1: return D3DMULTISAMPLE_NONE;
case 2: return D3DMULTISAMPLE_2_SAMPLES;
case 3: return D3DMULTISAMPLE_3_SAMPLES;
case 4: return D3DMULTISAMPLE_4_SAMPLES;
case 5: return D3DMULTISAMPLE_5_SAMPLES;
case 6: return D3DMULTISAMPLE_6_SAMPLES;
case 7: return D3DMULTISAMPLE_7_SAMPLES;
case 8: return D3DMULTISAMPLE_8_SAMPLES;
case 9: return D3DMULTISAMPLE_9_SAMPLES;
case 10: return D3DMULTISAMPLE_10_SAMPLES;
case 11: return D3DMULTISAMPLE_11_SAMPLES;
case 12: return D3DMULTISAMPLE_12_SAMPLES;
case 13: return D3DMULTISAMPLE_13_SAMPLES;
case 14: return D3DMULTISAMPLE_14_SAMPLES;
case 15: return D3DMULTISAMPLE_15_SAMPLES;
case 16: return D3DMULTISAMPLE_16_SAMPLES;
default:
ErrorString("Unknown sample count");
return D3DMULTISAMPLE_NONE;
}
}
bool CheckD3D9DebugRuntime (IDirect3DDevice9* dev)
{
IDirect3DQuery9* query = NULL;
HRESULT hr = dev->CreateQuery (D3DQUERYTYPE_VERTEXSTATS, &query);
if( SUCCEEDED(hr) )
{
query->Release ();
return true;
}
return false;
}
D3D9DepthStencilTexture CreateDepthStencilTextureD3D9 (IDirect3DDevice9* dev, int width, int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE msType, DWORD msQuality, BOOL discardable)
{
D3D9DepthStencilTexture tex;
HRESULT hr = dev->CreateDepthStencilSurface (width, height, format, msType, msQuality, discardable, &tex.m_Surface, NULL);
REGISTER_EXTERNAL_GFX_ALLOCATION_REF(tex.m_Surface, width * height * GetBPPFromD3DFormat(format), NULL);
return tex;
}
|