blob: c7397365830552684815324a1691f1ebc13d73cb [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Renderer.cpp: Implements a back-end specific class that hides the details of the
8// implementation-specific renderer.
9
daniel@transgaming.com621ce052012-10-31 17:52:29 +000010#include "common/debug.h"
11#include "libGLESv2/utilities.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000012#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000013#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000014
daniel@transgaming.com3281f972012-10-31 18:38:51 +000015#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000016#include "libEGL/Display.h"
17
daniel@transgaming.com621ce052012-10-31 17:52:29 +000018// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
19#define REF_RAST 0
20
21// The "Debug This Pixel..." feature in PIX often fails when using the
22// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
23// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
24#if !defined(ANGLE_ENABLE_D3D9EX)
25// Enables use of the IDirect3D9Ex interface, when available
26#define ANGLE_ENABLE_D3D9EX 1
27#endif // !defined(ANGLE_ENABLE_D3D9EX)
28
29namespace renderer
30{
daniel@transgaming.com2507f412012-10-31 18:46:48 +000031const D3DFORMAT Renderer9::mRenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000032 {
33 D3DFMT_A1R5G5B5,
34 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
35 D3DFMT_A8R8G8B8,
36 D3DFMT_R5G6B5,
37 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
38 D3DFMT_X8R8G8B8
39 };
40
daniel@transgaming.com2507f412012-10-31 18:46:48 +000041const D3DFORMAT Renderer9::mDepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000042 {
43 D3DFMT_UNKNOWN,
44 // D3DFMT_D16_LOCKABLE,
45 D3DFMT_D32,
46 // D3DFMT_D15S1,
47 D3DFMT_D24S8,
48 D3DFMT_D24X8,
49 // D3DFMT_D24X4S4,
50 D3DFMT_D16,
51 // D3DFMT_D32F_LOCKABLE,
52 // D3DFMT_D24FS8
53 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000054
daniel@transgaming.com2507f412012-10-31 18:46:48 +000055Renderer9::Renderer9(egl::Display *display, HMODULE hModule, HDC hDc) : Renderer(display), mDc(hDc)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000056{
57 mD3d9Module = hModule;
58
59 mD3d9 = NULL;
60 mD3d9Ex = NULL;
61 mDevice = NULL;
62 mDeviceEx = NULL;
63 mDeviceWindow = NULL;
64
65 mAdapter = D3DADAPTER_DEFAULT;
66
67 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
68 mDeviceType = D3DDEVTYPE_REF;
69 #else
70 mDeviceType = D3DDEVTYPE_HAL;
71 #endif
72
73 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000074
75 mMaxSupportedSamples = 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000076}
77
daniel@transgaming.com2507f412012-10-31 18:46:48 +000078Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +000079{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000080 releaseDeviceResources();
81
daniel@transgaming.com621ce052012-10-31 17:52:29 +000082 if (mDevice)
83 {
84 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +000085 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +000086 {
87 resetDevice();
88 }
89
90 mDevice->Release();
91 mDevice = NULL;
92 }
93
94 if (mDeviceEx)
95 {
96 mDeviceEx->Release();
97 mDeviceEx = NULL;
98 }
99
100 if (mD3d9)
101 {
102 mD3d9->Release();
103 mD3d9 = NULL;
104 }
105
106 if (mDeviceWindow)
107 {
108 DestroyWindow(mDeviceWindow);
109 mDeviceWindow = NULL;
110 }
111
112 if (mD3d9Ex)
113 {
114 mD3d9Ex->Release();
115 mD3d9Ex = NULL;
116 }
117
118 if (mD3d9Module)
119 {
120 mD3d9Module = NULL;
121 }
122
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000123 while (!mMultiSampleSupport.empty())
124 {
125 delete [] mMultiSampleSupport.begin()->second;
126 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
127 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000128}
129
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000130EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000131{
132 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
133 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
134
135 // Use Direct3D9Ex if available. Among other things, this version is less
136 // inclined to report a lost context, for example when the user switches
137 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
138 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
139 {
140 ASSERT(mD3d9Ex);
141 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
142 ASSERT(mD3d9);
143 }
144 else
145 {
146 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
147 }
148
149 if (!mD3d9)
150 {
151 ERR("Could not create D3D9 device - aborting!\n");
152 return EGL_NOT_INITIALIZED;
153 }
154 if (mDc != NULL)
155 {
156 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
157 }
158
159 HRESULT result;
160
161 // Give up on getting device caps after about one second.
162 for (int i = 0; i < 10; ++i)
163 {
164 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
165 if (SUCCEEDED(result))
166 {
167 break;
168 }
169 else if (result == D3DERR_NOTAVAILABLE)
170 {
171 Sleep(100); // Give the driver some time to initialize/recover
172 }
173 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
174 {
175 ERR("failed to get device caps (0x%x)\n", result);
176 return EGL_NOT_INITIALIZED;
177 }
178 }
179
180 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
181 {
182 ERR("Renderer does not support PS 2.0. aborting!\n");
183 return EGL_NOT_INITIALIZED;
184 }
185
186 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
187 // This is required by Texture2D::convertToRenderTarget.
188 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
189 {
190 ERR("Renderer does not support stretctrect from textures!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
194 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
195
196 // ATI cards on XP have problems with non-power-of-two textures.
197 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
198 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
199 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
200 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
201
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000202 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
203 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
204
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000205 mMinSwapInterval = 4;
206 mMaxSwapInterval = 0;
207
208 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
209 {
210 mMinSwapInterval = std::min(mMinSwapInterval, 0);
211 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
212 }
213 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
214 {
215 mMinSwapInterval = std::min(mMinSwapInterval, 1);
216 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
217 }
218 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
219 {
220 mMinSwapInterval = std::min(mMinSwapInterval, 2);
221 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
222 }
223 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
224 {
225 mMinSwapInterval = std::min(mMinSwapInterval, 3);
226 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
227 }
228 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
229 {
230 mMinSwapInterval = std::min(mMinSwapInterval, 4);
231 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
232 }
233
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000234 int max = 0;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000235 for (int i = 0; i < sizeof(mRenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000236 {
237 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com92955622012-10-31 18:38:41 +0000238 getMultiSampleSupport(mRenderTargetFormats[i], multisampleArray);
239 mMultiSampleSupport[mRenderTargetFormats[i]] = multisampleArray;
240
241 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
242 {
243 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
244 {
245 max = j;
246 }
247 }
248 }
249
250 for (int i = 0; i < sizeof(mDepthStencilFormats) / sizeof(D3DFORMAT); ++i)
251 {
252 if (mDepthStencilFormats[i] == D3DFMT_UNKNOWN)
253 continue;
254
255 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
256 getMultiSampleSupport(mDepthStencilFormats[i], multisampleArray);
257 mMultiSampleSupport[mDepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000258
259 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
260 {
261 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
262 {
263 max = j;
264 }
265 }
266 }
267
268 mMaxSupportedSamples = max;
269
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000270 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
271 static const TCHAR className[] = TEXT("STATIC");
272
273 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
274
275 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
276 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
277
278 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
279 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
280 {
281 return EGL_BAD_ALLOC;
282 }
283
284 if (FAILED(result))
285 {
286 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
287
288 if (FAILED(result))
289 {
290 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
291 return EGL_BAD_ALLOC;
292 }
293 }
294
295 if (mD3d9Ex)
296 {
297 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
298 ASSERT(SUCCEEDED(result));
299 }
300
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000301 mVertexShaderCache.initialize(mDevice);
302 mPixelShaderCache.initialize(mDevice);
303
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000304 initializeDevice();
305
306 return EGL_SUCCESS;
307}
308
309// do any one-time device initialization
310// NOTE: this is also needed after a device lost/reset
311// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000312void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000313{
314 // Permanent non-default states
315 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
316 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
317
318 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
319 {
320 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
321 }
322 else
323 {
324 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
325 }
326
327 mSceneStarted = false;
328}
329
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000330D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000331{
332 D3DPRESENT_PARAMETERS presentParameters = {0};
333
334 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
335 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
336 presentParameters.BackBufferCount = 1;
337 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
338 presentParameters.BackBufferWidth = 1;
339 presentParameters.BackBufferHeight = 1;
340 presentParameters.EnableAutoDepthStencil = FALSE;
341 presentParameters.Flags = 0;
342 presentParameters.hDeviceWindow = mDeviceWindow;
343 presentParameters.MultiSampleQuality = 0;
344 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
345 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
346 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
347 presentParameters.Windowed = TRUE;
348
349 return presentParameters;
350}
351
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000352int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000353{
354 D3DDISPLAYMODE currentDisplayMode;
355 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
356
357 int numRenderFormats = sizeof(mRenderTargetFormats) / sizeof(mRenderTargetFormats[0]);
358 int numDepthFormats = sizeof(mDepthStencilFormats) / sizeof(mDepthStencilFormats[0]);
359 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
360 int numConfigs = 0;
361
362 for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
363 {
364 D3DFORMAT renderTargetFormat = mRenderTargetFormats[formatIndex];
365
366 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
367
368 if (SUCCEEDED(result))
369 {
370 for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
371 {
372 D3DFORMAT depthStencilFormat = mDepthStencilFormats[depthStencilIndex];
373 HRESULT result = D3D_OK;
374
375 if(depthStencilFormat != D3DFMT_UNKNOWN)
376 {
377 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
378 }
379
380 if (SUCCEEDED(result))
381 {
382 if(depthStencilFormat != D3DFMT_UNKNOWN)
383 {
384 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
385 }
386
387 if (SUCCEEDED(result))
388 {
389 ConfigDesc newConfig;
390 newConfig.renderTargetFormat = dx2es::ConvertBackBufferFormat(renderTargetFormat);
391 newConfig.depthStencilFormat = dx2es::ConvertDepthStencilFormat(depthStencilFormat);
392 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
393 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
394
395 (*configDescList)[numConfigs++] = newConfig;
396 }
397 }
398 }
399 }
400 }
401
402 return numConfigs;
403}
404
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000405void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000406{
407 delete [] (configDescList);
408}
409
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000410void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000411{
412 if (!mSceneStarted)
413 {
414 long result = mDevice->BeginScene();
415 if (SUCCEEDED(result)) {
416 // This is defensive checking against the device being
417 // lost at unexpected times.
418 mSceneStarted = true;
419 }
420 }
421}
422
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000423void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000424{
425 if (mSceneStarted)
426 {
427 // EndScene can fail if the device was lost, for example due
428 // to a TDR during a draw call.
429 mDevice->EndScene();
430 mSceneStarted = false;
431 }
432}
433
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000434// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000435void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000436{
437 HRESULT result;
438
439 IDirect3DQuery9* query = allocateEventQuery();
440 if (!query)
441 {
442 return;
443 }
444
445 result = query->Issue(D3DISSUE_END);
446 ASSERT(SUCCEEDED(result));
447
448 do
449 {
450 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
451
452 if(block && result == S_FALSE)
453 {
454 // Keep polling, but allow other threads to do something useful first
455 Sleep(0);
456 // explicitly check for device loss
457 // some drivers seem to return S_FALSE even if the device is lost
458 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000459 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000460 {
461 result = D3DERR_DEVICELOST;
462 }
463 }
464 }
465 while(block && result == S_FALSE);
466
467 freeEventQuery(query);
468
469 if (isDeviceLostError(result))
470 {
471 mDisplay->notifyDeviceLost();
472 }
473}
474
475// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000476IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000477{
478 IDirect3DQuery9 *query = NULL;
479
480 if (mEventQueryPool.empty())
481 {
482 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
483 ASSERT(SUCCEEDED(result));
484 }
485 else
486 {
487 query = mEventQueryPool.back();
488 mEventQueryPool.pop_back();
489 }
490
491 return query;
492}
493
494// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000495void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000496{
497 if (mEventQueryPool.size() > 1000)
498 {
499 query->Release();
500 }
501 else
502 {
503 mEventQueryPool.push_back(query);
504 }
505}
506
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000507IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000508{
509 return mVertexShaderCache.create(function, length);
510}
511
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000512IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000513{
514 return mPixelShaderCache.create(function, length);
515}
516
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000517HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
518{
519 D3DPOOL Pool = getBufferPool(Usage);
520 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
521}
522
523HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
524{
525 D3DPOOL Pool = getBufferPool(Usage);
526 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
527}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000528
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000529void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000530{
531 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
532 int d3dSampler = index + d3dSamplerOffset;
533
534 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(samplerState.wrapS));
535 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(samplerState.wrapT));
536
537 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
538 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
539 es2dx::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
540 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
541 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
542 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
543 if (mSupportsTextureFilterAnisotropy)
544 {
545 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
546 }
547}
548
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000549void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000550{
551 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
552 int d3dSampler = index + d3dSamplerOffset;
553 IDirect3DBaseTexture9 *d3dTexture = NULL;
554
555 if (texture)
556 {
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000557 gl::TextureStorage *texStorage = texture->getNativeTexture();
558 if (texStorage)
559 {
560 d3dTexture = texStorage->getBaseTexture();
561 }
562 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000563 // in the texture class and we're unexpectedly missing the d3d texture
564 ASSERT(d3dTexture != NULL);
565 }
566
567 mDevice->SetTexture(d3dSampler, d3dTexture);
568}
569
570
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000571void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000572{
573 while (!mEventQueryPool.empty())
574 {
575 mEventQueryPool.back()->Release();
576 mEventQueryPool.pop_back();
577 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000578
579 mVertexShaderCache.clear();
580 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000581}
582
583
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000584void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000585{
586 mDeviceLost = true;
587}
588
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000589bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000590{
591 return mDeviceLost;
592}
593
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000594// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000595bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000596{
597 bool isLost = false;
598
599 if (mDeviceEx)
600 {
601 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
602 }
603 else if (mDevice)
604 {
605 isLost = FAILED(mDevice->TestCooperativeLevel());
606 }
607 else
608 {
609 // No device yet, so no reset required
610 }
611
612 if (isLost)
613 {
614 // ensure we note the device loss --
615 // we'll probably get this done again by markDeviceLost
616 // but best to remember it!
617 // Note that we don't want to clear the device loss status here
618 // -- this needs to be done by resetDevice
619 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000620 if (notify)
621 {
622 mDisplay->notifyDeviceLost();
623 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000624 }
625
626 return isLost;
627}
628
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000629bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000630{
631 HRESULT status = D3D_OK;
632
633 if (mDeviceEx)
634 {
635 status = mDeviceEx->CheckDeviceState(NULL);
636 }
637 else if (mDevice)
638 {
639 status = mDevice->TestCooperativeLevel();
640 }
641
642 switch (status)
643 {
644 case D3DERR_DEVICENOTRESET:
645 case D3DERR_DEVICEHUNG:
646 return true;
647 default:
648 return false;
649 }
650}
651
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000652bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000653{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000654 releaseDeviceResources();
655
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000656 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
657
658 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000659 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000660 int attempts = 3;
661
662 while (lost && attempts > 0)
663 {
664 if (mDeviceEx)
665 {
666 Sleep(500); // Give the graphics driver some CPU time
667 result = mDeviceEx->ResetEx(&presentParameters, NULL);
668 }
669 else
670 {
671 result = mDevice->TestCooperativeLevel();
672 while (result == D3DERR_DEVICELOST)
673 {
674 Sleep(100); // Give the graphics driver some CPU time
675 result = mDevice->TestCooperativeLevel();
676 }
677
678 if (result == D3DERR_DEVICENOTRESET)
679 {
680 result = mDevice->Reset(&presentParameters);
681 }
682 }
683
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000684 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000685 attempts --;
686 }
687
688 if (FAILED(result))
689 {
690 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
691 return false;
692 }
693
694 // reset device defaults
695 initializeDevice();
696 mDeviceLost = false;
697
698 return true;
699}
700
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000701DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +0000702{
703 return mAdapterIdentifier.VendorId;
704}
705
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000706const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +0000707{
708 return mAdapterIdentifier.Description;
709}
710
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000711GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +0000712{
713 return mAdapterIdentifier.DeviceIdentifier;
714}
715
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000716void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000717{
718 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
719 {
720 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
721 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
722
723 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
724 }
725}
726
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000727bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000728{
729 D3DDISPLAYMODE currentDisplayMode;
730 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
731
732 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
733}
734
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000735bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000736{
737 D3DDISPLAYMODE currentDisplayMode;
738 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
739
740 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
741}
742
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000743bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000744{
745 D3DDISPLAYMODE currentDisplayMode;
746 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
747
748 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
749}
750
751// we use INTZ for depth textures in Direct3D9
752// we also want NULL texture support to ensure the we can make depth-only FBOs
753// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000754bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000755{
756 D3DDISPLAYMODE currentDisplayMode;
757 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
758
759 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
760 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
761 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
762 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
763
764 return intz && null;
765}
766
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000767bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000768{
769 D3DDISPLAYMODE currentDisplayMode;
770 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
771
772 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
773 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
774 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
775 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
776
777 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
778 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
779 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
780 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
781
782 if (!*filtering && !*renderable)
783 {
784 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
785 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
786 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
787 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
788 }
789 else
790 {
791 return true;
792 }
793}
794
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000795bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000796{
797 D3DDISPLAYMODE currentDisplayMode;
798 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
799
800 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
801 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
802 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
803 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
804
805 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
806 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
807 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
808 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
809
810 if (!*filtering && !*renderable)
811 {
812 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
813 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
814 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
815 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
816 }
817 else
818 {
819 return true;
820 }
821}
822
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000823bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000824{
825 D3DDISPLAYMODE currentDisplayMode;
826 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
827
828 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
829}
830
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000831bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000832{
833 D3DDISPLAYMODE currentDisplayMode;
834 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
835
836 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
837}
838
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000839bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000840{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000841 return mSupportsTextureFilterAnisotropy;
842}
843
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000844float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000845{
846 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000847 {
848 return mDeviceCaps.MaxAnisotropy;
849 }
850 return 1.0f;
851}
852
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000853bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000854{
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000855 IDirect3DQuery9 *query = allocateEventQuery();
856 if (query)
857 {
858 freeEventQuery(query);
859 return true;
860 }
861 else
862 {
863 return false;
864 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000865 return true;
866}
867
868// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
869// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000870bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000871{
872 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
873 {
874 return false;
875 }
876
877 D3DDISPLAYMODE currentDisplayMode;
878 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
879
880 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
881
882 return SUCCEEDED(result);
883}
884
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000885bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000886{
887 return mSupportsNonPower2Textures;
888}
889
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000890bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000891{
892 if (!mDevice)
893 {
894 return false;
895 }
896
897 IDirect3DQuery9 *query = NULL;
898 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
899 if (SUCCEEDED(result) && query)
900 {
901 query->Release();
902 return true;
903 }
904 else
905 {
906 return false;
907 }
908}
909
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000910bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000911{
912 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
913}
914
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000915bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +0000916{
917 // PIX doesn't seem to support using share handles, so disable them.
918 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +0000919 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +0000920}
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000921
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000922bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000923{
924 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
925}
926
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000927float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000928{
929 return mDeviceCaps.MaxPointSize;
930}
931
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000932int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000933{
934 return (int)mDeviceCaps.MaxTextureWidth;
935}
936
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000937int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000938{
939 return (int)mDeviceCaps.MaxTextureHeight;
940}
941
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000942bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000943{
944 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
945}
946
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000947DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000948{
949 return mDeviceCaps.DeclTypes;
950}
951
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000952int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000953{
954 return mMinSwapInterval;
955}
956
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000957int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000958{
959 return mMaxSwapInterval;
960}
961
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000962int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000963{
964 return mMaxSupportedSamples;
965}
966
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000967int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000968{
969 if (requested == 0)
970 {
971 return requested;
972 }
973
974 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
975 if (itr == mMultiSampleSupport.end())
976 {
daniel@transgaming.com92955622012-10-31 18:38:41 +0000977 if (format == D3DFMT_UNKNOWN)
978 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000979 return -1;
980 }
981
982 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
983 {
984 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
985 {
986 return i;
987 }
988 }
989
990 return -1;
991}
992
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000993D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000994{
995 if (mD3d9Ex != NULL)
996 {
997 return D3DPOOL_DEFAULT;
998 }
999 else
1000 {
1001 if (!(usage & D3DUSAGE_DYNAMIC))
1002 {
1003 return D3DPOOL_MANAGED;
1004 }
1005 }
1006
1007 return D3DPOOL_DEFAULT;
1008}
1009
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001010D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001011{
1012 if (mD3d9Ex != NULL)
1013 {
1014 return D3DPOOL_DEFAULT;
1015 }
1016 else
1017 {
1018 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
1019 {
1020 return D3DPOOL_MANAGED;
1021 }
1022 }
1023
1024 return D3DPOOL_DEFAULT;
1025}
1026
1027}