blob: fa4724dac90f8b8390e054e0e7f66b74a27d21ec [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
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00008
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009#include "common/debug.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000010#include "libGLESv2/main.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/mathutil.h"
13#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000014#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000015#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000016#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +000017#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000018#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000019
daniel@transgaming.com3281f972012-10-31 18:38:51 +000020#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000021#include "libEGL/Display.h"
22
daniel@transgaming.com621ce052012-10-31 17:52:29 +000023// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
24#define REF_RAST 0
25
26// The "Debug This Pixel..." feature in PIX often fails when using the
27// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
28// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
29#if !defined(ANGLE_ENABLE_D3D9EX)
30// Enables use of the IDirect3D9Ex interface, when available
31#define ANGLE_ENABLE_D3D9EX 1
32#endif // !defined(ANGLE_ENABLE_D3D9EX)
33
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000034namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000035{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000036static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000037 {
38 D3DFMT_A1R5G5B5,
39 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
40 D3DFMT_A8R8G8B8,
41 D3DFMT_R5G6B5,
42 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
43 D3DFMT_X8R8G8B8
44 };
45
daniel@transgaming.com222ee082012-11-28 19:31:49 +000046static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000047 {
48 D3DFMT_UNKNOWN,
49 // D3DFMT_D16_LOCKABLE,
50 D3DFMT_D32,
51 // D3DFMT_D15S1,
52 D3DFMT_D24S8,
53 D3DFMT_D24X8,
54 // D3DFMT_D24X4S4,
55 D3DFMT_D16,
56 // D3DFMT_D32F_LOCKABLE,
57 // D3DFMT_D24FS8
58 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000059
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000060Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000061{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000062 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000063
64 mD3d9 = NULL;
65 mD3d9Ex = NULL;
66 mDevice = NULL;
67 mDeviceEx = NULL;
68 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000069 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000070
71 mAdapter = D3DADAPTER_DEFAULT;
72
73 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
74 mDeviceType = D3DDEVTYPE_REF;
75 #else
76 mDeviceType = D3DDEVTYPE_HAL;
77 #endif
78
79 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000080
81 mMaxSupportedSamples = 0;
daniel@transgaming.com8a8b24c2012-11-28 19:36:26 +000082
83 mForceSetDepthStencilState = true;
84 mForceSetRasterState = true;
85 mForceSetBlendState = true;
86 mForceSetScissor = true;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000087}
88
daniel@transgaming.com2507f412012-10-31 18:46:48 +000089Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +000090{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000091 releaseDeviceResources();
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000092
93 delete mBlit;
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000094
daniel@transgaming.com621ce052012-10-31 17:52:29 +000095 if (mDevice)
96 {
97 // 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 +000098 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +000099 {
100 resetDevice();
101 }
102
103 mDevice->Release();
104 mDevice = NULL;
105 }
106
107 if (mDeviceEx)
108 {
109 mDeviceEx->Release();
110 mDeviceEx = NULL;
111 }
112
113 if (mD3d9)
114 {
115 mD3d9->Release();
116 mD3d9 = NULL;
117 }
118
119 if (mDeviceWindow)
120 {
121 DestroyWindow(mDeviceWindow);
122 mDeviceWindow = NULL;
123 }
124
125 if (mD3d9Ex)
126 {
127 mD3d9Ex->Release();
128 mD3d9Ex = NULL;
129 }
130
131 if (mD3d9Module)
132 {
133 mD3d9Module = NULL;
134 }
135
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000136 while (!mMultiSampleSupport.empty())
137 {
138 delete [] mMultiSampleSupport.begin()->second;
139 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
140 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000141}
142
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000143EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000144{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000145 if (mSoftwareDevice)
146 {
147 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
148 }
149 else
150 {
151 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
152 }
153
154 if (mD3d9Module == NULL)
155 {
156 ERR("No D3D9 module found - aborting!\n");
157 return EGL_NOT_INITIALIZED;
158 }
159
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000160 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
161 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
162
163 // Use Direct3D9Ex if available. Among other things, this version is less
164 // inclined to report a lost context, for example when the user switches
165 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
166 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
167 {
168 ASSERT(mD3d9Ex);
169 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
170 ASSERT(mD3d9);
171 }
172 else
173 {
174 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
175 }
176
177 if (!mD3d9)
178 {
179 ERR("Could not create D3D9 device - aborting!\n");
180 return EGL_NOT_INITIALIZED;
181 }
182 if (mDc != NULL)
183 {
184 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
185 }
186
187 HRESULT result;
188
189 // Give up on getting device caps after about one second.
190 for (int i = 0; i < 10; ++i)
191 {
192 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
193 if (SUCCEEDED(result))
194 {
195 break;
196 }
197 else if (result == D3DERR_NOTAVAILABLE)
198 {
199 Sleep(100); // Give the driver some time to initialize/recover
200 }
201 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
202 {
203 ERR("failed to get device caps (0x%x)\n", result);
204 return EGL_NOT_INITIALIZED;
205 }
206 }
207
208 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
209 {
210 ERR("Renderer does not support PS 2.0. aborting!\n");
211 return EGL_NOT_INITIALIZED;
212 }
213
214 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
215 // This is required by Texture2D::convertToRenderTarget.
216 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
217 {
218 ERR("Renderer does not support stretctrect from textures!\n");
219 return EGL_NOT_INITIALIZED;
220 }
221
222 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
223
224 // ATI cards on XP have problems with non-power-of-two textures.
225 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
226 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
227 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
228 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
229
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000230 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
231 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
232
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000233 mMinSwapInterval = 4;
234 mMaxSwapInterval = 0;
235
236 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
237 {
238 mMinSwapInterval = std::min(mMinSwapInterval, 0);
239 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
240 }
241 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
242 {
243 mMinSwapInterval = std::min(mMinSwapInterval, 1);
244 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
245 }
246 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
247 {
248 mMinSwapInterval = std::min(mMinSwapInterval, 2);
249 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
250 }
251 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
252 {
253 mMinSwapInterval = std::min(mMinSwapInterval, 3);
254 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
255 }
256 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
257 {
258 mMinSwapInterval = std::min(mMinSwapInterval, 4);
259 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
260 }
261
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000262 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000263 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000264 {
265 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000266 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
267 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000268
269 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
270 {
271 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
272 {
273 max = j;
274 }
275 }
276 }
277
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000278 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000279 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000280 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000281 continue;
282
283 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000284 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
285 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000286
287 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
288 {
289 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
290 {
291 max = j;
292 }
293 }
294 }
295
296 mMaxSupportedSamples = max;
297
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000298 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
299 static const TCHAR className[] = TEXT("STATIC");
300
301 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
302
303 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
304 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
305
306 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
307 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
308 {
309 return EGL_BAD_ALLOC;
310 }
311
312 if (FAILED(result))
313 {
314 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
315
316 if (FAILED(result))
317 {
318 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
319 return EGL_BAD_ALLOC;
320 }
321 }
322
323 if (mD3d9Ex)
324 {
325 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
326 ASSERT(SUCCEEDED(result));
327 }
328
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000329 mVertexShaderCache.initialize(mDevice);
330 mPixelShaderCache.initialize(mDevice);
331
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000332 initializeDevice();
333
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000334 mBlit = new Blit(this);
335
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000336 return EGL_SUCCESS;
337}
338
339// do any one-time device initialization
340// NOTE: this is also needed after a device lost/reset
341// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000342void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000343{
344 // Permanent non-default states
345 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
346 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
347
348 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
349 {
350 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
351 }
352 else
353 {
354 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
355 }
356
357 mSceneStarted = false;
358}
359
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000360D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000361{
362 D3DPRESENT_PARAMETERS presentParameters = {0};
363
364 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
365 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
366 presentParameters.BackBufferCount = 1;
367 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
368 presentParameters.BackBufferWidth = 1;
369 presentParameters.BackBufferHeight = 1;
370 presentParameters.EnableAutoDepthStencil = FALSE;
371 presentParameters.Flags = 0;
372 presentParameters.hDeviceWindow = mDeviceWindow;
373 presentParameters.MultiSampleQuality = 0;
374 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
375 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
376 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
377 presentParameters.Windowed = TRUE;
378
379 return presentParameters;
380}
381
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000382int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000383{
384 D3DDISPLAYMODE currentDisplayMode;
385 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
386
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000387 int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
388 int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000389 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
390 int numConfigs = 0;
391
392 for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
393 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000394 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000395
396 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
397
398 if (SUCCEEDED(result))
399 {
400 for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
401 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000402 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000403 HRESULT result = D3D_OK;
404
405 if(depthStencilFormat != D3DFMT_UNKNOWN)
406 {
407 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
408 }
409
410 if (SUCCEEDED(result))
411 {
412 if(depthStencilFormat != D3DFMT_UNKNOWN)
413 {
414 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
415 }
416
417 if (SUCCEEDED(result))
418 {
419 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000420 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
421 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000422 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
423 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
424
425 (*configDescList)[numConfigs++] = newConfig;
426 }
427 }
428 }
429 }
430 }
431
432 return numConfigs;
433}
434
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000435void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000436{
437 delete [] (configDescList);
438}
439
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000440void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000441{
442 if (!mSceneStarted)
443 {
444 long result = mDevice->BeginScene();
445 if (SUCCEEDED(result)) {
446 // This is defensive checking against the device being
447 // lost at unexpected times.
448 mSceneStarted = true;
449 }
450 }
451}
452
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000453void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000454{
455 if (mSceneStarted)
456 {
457 // EndScene can fail if the device was lost, for example due
458 // to a TDR during a draw call.
459 mDevice->EndScene();
460 mSceneStarted = false;
461 }
462}
463
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000464// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000465void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000466{
467 HRESULT result;
468
469 IDirect3DQuery9* query = allocateEventQuery();
470 if (!query)
471 {
472 return;
473 }
474
475 result = query->Issue(D3DISSUE_END);
476 ASSERT(SUCCEEDED(result));
477
478 do
479 {
480 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
481
482 if(block && result == S_FALSE)
483 {
484 // Keep polling, but allow other threads to do something useful first
485 Sleep(0);
486 // explicitly check for device loss
487 // some drivers seem to return S_FALSE even if the device is lost
488 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000489 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000490 {
491 result = D3DERR_DEVICELOST;
492 }
493 }
494 }
495 while(block && result == S_FALSE);
496
497 freeEventQuery(query);
498
499 if (isDeviceLostError(result))
500 {
501 mDisplay->notifyDeviceLost();
502 }
503}
504
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000505SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
506{
507 return new rx::SwapChain(this, window, shareHandle, backBufferFormat, depthBufferFormat);
508}
509
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000510// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000511IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000512{
513 IDirect3DQuery9 *query = NULL;
514
515 if (mEventQueryPool.empty())
516 {
517 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
518 ASSERT(SUCCEEDED(result));
519 }
520 else
521 {
522 query = mEventQueryPool.back();
523 mEventQueryPool.pop_back();
524 }
525
526 return query;
527}
528
529// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000530void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000531{
532 if (mEventQueryPool.size() > 1000)
533 {
534 query->Release();
535 }
536 else
537 {
538 mEventQueryPool.push_back(query);
539 }
540}
541
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000542IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000543{
544 return mVertexShaderCache.create(function, length);
545}
546
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000547IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000548{
549 return mPixelShaderCache.create(function, length);
550}
551
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000552HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
553{
554 D3DPOOL Pool = getBufferPool(Usage);
555 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
556}
557
558HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
559{
560 D3DPOOL Pool = getBufferPool(Usage);
561 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
562}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000563
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000564void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000565{
566 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
567 int d3dSampler = index + d3dSamplerOffset;
568
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000569 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
570 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000571
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000572 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000573 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000574 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000575 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
576 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
577 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
578 if (mSupportsTextureFilterAnisotropy)
579 {
580 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
581 }
582}
583
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000584void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000585{
586 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
587 int d3dSampler = index + d3dSamplerOffset;
588 IDirect3DBaseTexture9 *d3dTexture = NULL;
589
590 if (texture)
591 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000592 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000593 if (texStorage)
594 {
595 d3dTexture = texStorage->getBaseTexture();
596 }
597 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000598 // in the texture class and we're unexpectedly missing the d3d texture
599 ASSERT(d3dTexture != NULL);
600 }
601
602 mDevice->SetTexture(d3dSampler, d3dTexture);
603}
604
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000605void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize)
606{
607 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
608 bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize;
609
610 if (rasterStateChanged)
611 {
612 // Set the cull mode
613 if (rasterState.cullFace)
614 {
615 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
616 }
617 else
618 {
619 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
620 }
621
622 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
623
624 mCurRasterState = rasterState;
625 }
626
627 if (rasterStateChanged || depthSizeChanged)
628 {
629 if (rasterState.polygonOffsetFill)
630 {
631 if (depthSize > 0)
632 {
633 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
634
635 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
636 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
637 }
638 }
639 else
640 {
641 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
642 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
643 }
644
645 mCurDepthSize = depthSize;
646 }
647
648 mForceSetRasterState = false;
649}
650
651void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
652{
653 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
654 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
655 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
656
657 if (blendStateChanged || blendColorChanged)
658 {
659 if (blendState.blend)
660 {
661 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
662
663 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
664 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
665 {
666 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
667 }
668 else
669 {
670 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
671 gl::unorm<8>(blendColor.alpha),
672 gl::unorm<8>(blendColor.alpha),
673 gl::unorm<8>(blendColor.alpha)));
674 }
675
676 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
677 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
678 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
679
680 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
681 blendState.destBlendRGB != blendState.destBlendAlpha ||
682 blendState.blendEquationRGB != blendState.blendEquationAlpha)
683 {
684 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
685
686 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
687 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
688 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
689 }
690 else
691 {
692 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
693 }
694 }
695 else
696 {
697 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
698 }
699
700 if (blendState.sampleAlphaToCoverage)
701 {
702 FIXME("Sample alpha to coverage is unimplemented.");
703 }
704
705 // Set the color mask
706 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
707 // Apparently some ATI cards have a bug where a draw with a zero color
708 // write mask can cause later draws to have incorrect results. Instead,
709 // set a nonzero color write mask but modify the blend state so that no
710 // drawing is done.
711 // http://code.google.com/p/angleproject/issues/detail?id=169
712
713 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
714 blendState.colorMaskBlue, blendState.colorMaskAlpha);
715 if (colorMask == 0 && !zeroColorMaskAllowed)
716 {
717 // Enable green channel, but set blending so nothing will be drawn.
718 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
719 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
720
721 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
722 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
723 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
724 }
725 else
726 {
727 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
728 }
729
730 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
731
732 mCurBlendState = blendState;
733 mCurBlendColor = blendColor;
734 }
735
736 if (sampleMaskChanged)
737 {
738 // Set the multisample mask
739 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
740 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
741
742 mCurSampleMask = sampleMask;
743 }
744
745 mForceSetBlendState = false;
746}
747
748void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, bool frontFaceCCW,
749 unsigned int stencilSize)
750{
751 bool depthStencilStateChanged = mForceSetDepthStencilState ||
752 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
753 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
754 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
755
756 if (depthStencilStateChanged)
757 {
758 if (depthStencilState.depthTest)
759 {
760 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
761 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
762 }
763 else
764 {
765 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
766 }
767
768 mCurDepthStencilState = depthStencilState;
769 }
770
771 if (depthStencilStateChanged || frontFaceCCWChanged || stencilSizeChanged)
772 {
773 if (depthStencilState.stencilTest && stencilSize > 0)
774 {
775 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
776 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
777
778 // FIXME: Unsupported by D3D9
779 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
780 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
781 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
782 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
783 depthStencilState.stencilRef != depthStencilState.stencilBackRef ||
784 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
785 {
786 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
787 return error(GL_INVALID_OPERATION);
788 }
789
790 // get the maximum size of the stencil ref
791 GLuint maxStencil = (1 << stencilSize) - 1;
792
793 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
794 depthStencilState.stencilWritemask);
795 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
796 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
797
798 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
799 (depthStencilState.stencilRef < (GLint)maxStencil) ? depthStencilState.stencilRef : maxStencil);
800 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
801 depthStencilState.stencilMask);
802
803 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
804 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
805 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
806 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
807 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
808 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
809
810 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
811 depthStencilState.stencilBackWritemask);
812 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
813 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
814
815 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
816 (depthStencilState.stencilBackRef < (GLint)maxStencil) ? depthStencilState.stencilBackRef : maxStencil);
817 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
818 depthStencilState.stencilBackMask);
819
820 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
821 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
822 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
823 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
824 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
825 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
826 }
827 else
828 {
829 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
830 }
831
832 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
833
834 mCurFrontFaceCCW = frontFaceCCW;
835 mCurStencilSize = stencilSize;
836 }
837
838 mForceSetDepthStencilState = false;
839}
840
841void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
842 unsigned int renderTargetHeight)
843{
844 bool renderTargetSizedChanged = mForceSetScissor ||
845 renderTargetWidth != mCurRenderTargetWidth ||
846 renderTargetHeight != mCurRenderTargetHeight;
847 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
848
849 if (renderTargetSizedChanged || scissorChanged)
850 {
851 RECT rect;
852 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
853 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth));
854 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
855 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth));
856 mDevice->SetScissorRect(&rect);
857
858 mCurScissor = scissor;
859 mCurRenderTargetWidth = renderTargetWidth;
860 mCurRenderTargetHeight = renderTargetHeight;
861 }
862
863 mForceSetScissor = false;
864}
865
866void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer)
867{
868 mForceSetScissor = true;
869
870 // TODO
871}
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000872
daniel@transgaming.comd084c622012-11-28 19:36:05 +0000873void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear,
874 gl::Framebuffer *frameBuffer)
875{
876 mForceSetDepthStencilState = true;
877
878 // TODO
879}
880
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000881void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000882{
883 while (!mEventQueryPool.empty())
884 {
885 mEventQueryPool.back()->Release();
886 mEventQueryPool.pop_back();
887 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000888
889 mVertexShaderCache.clear();
890 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000891}
892
893
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000894void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000895{
896 mDeviceLost = true;
897}
898
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000899bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000900{
901 return mDeviceLost;
902}
903
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000904// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000905bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000906{
907 bool isLost = false;
908
909 if (mDeviceEx)
910 {
911 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
912 }
913 else if (mDevice)
914 {
915 isLost = FAILED(mDevice->TestCooperativeLevel());
916 }
917 else
918 {
919 // No device yet, so no reset required
920 }
921
922 if (isLost)
923 {
924 // ensure we note the device loss --
925 // we'll probably get this done again by markDeviceLost
926 // but best to remember it!
927 // Note that we don't want to clear the device loss status here
928 // -- this needs to be done by resetDevice
929 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000930 if (notify)
931 {
932 mDisplay->notifyDeviceLost();
933 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000934 }
935
936 return isLost;
937}
938
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000939bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000940{
941 HRESULT status = D3D_OK;
942
943 if (mDeviceEx)
944 {
945 status = mDeviceEx->CheckDeviceState(NULL);
946 }
947 else if (mDevice)
948 {
949 status = mDevice->TestCooperativeLevel();
950 }
951
952 switch (status)
953 {
954 case D3DERR_DEVICENOTRESET:
955 case D3DERR_DEVICEHUNG:
956 return true;
957 default:
958 return false;
959 }
960}
961
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000962bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000963{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000964 releaseDeviceResources();
965
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000966 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
967
968 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000969 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000970 int attempts = 3;
971
972 while (lost && attempts > 0)
973 {
974 if (mDeviceEx)
975 {
976 Sleep(500); // Give the graphics driver some CPU time
977 result = mDeviceEx->ResetEx(&presentParameters, NULL);
978 }
979 else
980 {
981 result = mDevice->TestCooperativeLevel();
982 while (result == D3DERR_DEVICELOST)
983 {
984 Sleep(100); // Give the graphics driver some CPU time
985 result = mDevice->TestCooperativeLevel();
986 }
987
988 if (result == D3DERR_DEVICENOTRESET)
989 {
990 result = mDevice->Reset(&presentParameters);
991 }
992 }
993
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000994 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000995 attempts --;
996 }
997
998 if (FAILED(result))
999 {
1000 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1001 return false;
1002 }
1003
1004 // reset device defaults
1005 initializeDevice();
1006 mDeviceLost = false;
1007
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001008 mForceSetDepthStencilState = true;
1009 mForceSetRasterState = true;
1010 mForceSetBlendState = true;
1011 mForceSetScissor = true;
1012
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001013 return true;
1014}
1015
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001016DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001017{
1018 return mAdapterIdentifier.VendorId;
1019}
1020
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001021const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001022{
1023 return mAdapterIdentifier.Description;
1024}
1025
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001026GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001027{
1028 return mAdapterIdentifier.DeviceIdentifier;
1029}
1030
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001031void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001032{
1033 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1034 {
1035 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1036 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1037
1038 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1039 }
1040}
1041
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001042bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001043{
1044 D3DDISPLAYMODE currentDisplayMode;
1045 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1046
1047 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1048}
1049
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001050bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001051{
1052 D3DDISPLAYMODE currentDisplayMode;
1053 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1054
1055 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1056}
1057
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001058bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001059{
1060 D3DDISPLAYMODE currentDisplayMode;
1061 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1062
1063 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1064}
1065
1066// we use INTZ for depth textures in Direct3D9
1067// we also want NULL texture support to ensure the we can make depth-only FBOs
1068// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001069bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001070{
1071 D3DDISPLAYMODE currentDisplayMode;
1072 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1073
1074 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1075 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1076 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1077 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1078
1079 return intz && null;
1080}
1081
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001082bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001083{
1084 D3DDISPLAYMODE currentDisplayMode;
1085 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1086
1087 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1088 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1089 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1090 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1091
1092 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1093 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1094 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1095 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1096
1097 if (!*filtering && !*renderable)
1098 {
1099 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1100 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1101 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1102 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1103 }
1104 else
1105 {
1106 return true;
1107 }
1108}
1109
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001110bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001111{
1112 D3DDISPLAYMODE currentDisplayMode;
1113 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1114
1115 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1116 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1117 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1118 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1119
1120 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1121 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1122 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1123 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1124
1125 if (!*filtering && !*renderable)
1126 {
1127 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1128 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1129 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1130 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1131 }
1132 else
1133 {
1134 return true;
1135 }
1136}
1137
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001138bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001139{
1140 D3DDISPLAYMODE currentDisplayMode;
1141 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1142
1143 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1144}
1145
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001146bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001147{
1148 D3DDISPLAYMODE currentDisplayMode;
1149 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1150
1151 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1152}
1153
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001154bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001155{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001156 return mSupportsTextureFilterAnisotropy;
1157}
1158
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001159float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001160{
1161 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001162 {
1163 return mDeviceCaps.MaxAnisotropy;
1164 }
1165 return 1.0f;
1166}
1167
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001168bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001169{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001170 IDirect3DQuery9 *query = allocateEventQuery();
1171 if (query)
1172 {
1173 freeEventQuery(query);
1174 return true;
1175 }
1176 else
1177 {
1178 return false;
1179 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001180 return true;
1181}
1182
1183// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1184// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001185bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001186{
1187 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1188 {
1189 return false;
1190 }
1191
1192 D3DDISPLAYMODE currentDisplayMode;
1193 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1194
1195 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1196
1197 return SUCCEEDED(result);
1198}
1199
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001200bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001201{
1202 return mSupportsNonPower2Textures;
1203}
1204
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001205bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001206{
1207 if (!mDevice)
1208 {
1209 return false;
1210 }
1211
1212 IDirect3DQuery9 *query = NULL;
1213 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1214 if (SUCCEEDED(result) && query)
1215 {
1216 query->Release();
1217 return true;
1218 }
1219 else
1220 {
1221 return false;
1222 }
1223}
1224
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001225bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001226{
1227 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1228}
1229
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001230bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001231{
1232 // PIX doesn't seem to support using share handles, so disable them.
1233 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001234 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001235}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001236
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001237bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001238{
1239 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1240}
1241
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001242float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001243{
1244 return mDeviceCaps.MaxPointSize;
1245}
1246
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001247int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001248{
1249 return (int)mDeviceCaps.MaxTextureWidth;
1250}
1251
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001252int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001253{
1254 return (int)mDeviceCaps.MaxTextureHeight;
1255}
1256
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001257bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001258{
1259 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1260}
1261
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001262DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001263{
1264 return mDeviceCaps.DeclTypes;
1265}
1266
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001267int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001268{
1269 return mMinSwapInterval;
1270}
1271
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001272int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001273{
1274 return mMaxSwapInterval;
1275}
1276
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001277int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001278{
1279 return mMaxSupportedSamples;
1280}
1281
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001282int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001283{
1284 if (requested == 0)
1285 {
1286 return requested;
1287 }
1288
1289 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
1290 if (itr == mMultiSampleSupport.end())
1291 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00001292 if (format == D3DFMT_UNKNOWN)
1293 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001294 return -1;
1295 }
1296
1297 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
1298 {
1299 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
1300 {
1301 return i;
1302 }
1303 }
1304
1305 return -1;
1306}
1307
daniel@transgaming.coma9571682012-11-28 19:33:08 +00001308D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
1309{
1310 switch (internalformat)
1311 {
1312 case GL_DEPTH_COMPONENT16:
1313 case GL_DEPTH_COMPONENT32_OES:
1314 case GL_DEPTH24_STENCIL8_OES:
1315 return D3DFMT_INTZ;
1316 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1317 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1318 return D3DFMT_DXT1;
1319 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1320 return D3DFMT_DXT3;
1321 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1322 return D3DFMT_DXT5;
1323 case GL_RGBA32F_EXT:
1324 case GL_RGB32F_EXT:
1325 case GL_ALPHA32F_EXT:
1326 case GL_LUMINANCE32F_EXT:
1327 case GL_LUMINANCE_ALPHA32F_EXT:
1328 return D3DFMT_A32B32G32R32F;
1329 case GL_RGBA16F_EXT:
1330 case GL_RGB16F_EXT:
1331 case GL_ALPHA16F_EXT:
1332 case GL_LUMINANCE16F_EXT:
1333 case GL_LUMINANCE_ALPHA16F_EXT:
1334 return D3DFMT_A16B16G16R16F;
1335 case GL_LUMINANCE8_EXT:
1336 if (getLuminanceTextureSupport())
1337 {
1338 return D3DFMT_L8;
1339 }
1340 break;
1341 case GL_LUMINANCE8_ALPHA8_EXT:
1342 if (getLuminanceAlphaTextureSupport())
1343 {
1344 return D3DFMT_A8L8;
1345 }
1346 break;
1347 case GL_RGB8_OES:
1348 case GL_RGB565:
1349 return D3DFMT_X8R8G8B8;
1350 }
1351
1352 return D3DFMT_A8R8G8B8;
1353}
1354
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001355bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001356{
1357 bool result = false;
1358
1359 if (source && dest)
1360 {
1361 int levels = source->levelCount();
1362 for (int i = 0; i < levels; ++i)
1363 {
1364 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
1365 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
1366
1367 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1368
1369 if (srcSurf) srcSurf->Release();
1370 if (dstSurf) dstSurf->Release();
1371
1372 if (!result)
1373 return false;
1374 }
1375 }
1376
1377 return result;
1378}
1379
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001380bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001381{
1382 bool result = false;
1383
1384 if (source && dest)
1385 {
1386 int levels = source->levelCount();
1387 for (int f = 0; f < 6; f++)
1388 {
1389 for (int i = 0; i < levels; i++)
1390 {
1391 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
1392 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
1393
1394 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1395
1396 if (srcSurf) srcSurf->Release();
1397 if (dstSurf) dstSurf->Release();
1398
1399 if (!result)
1400 return false;
1401 }
1402 }
1403 }
1404
1405 return result;
1406}
1407
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001408D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001409{
1410 if (mD3d9Ex != NULL)
1411 {
1412 return D3DPOOL_DEFAULT;
1413 }
1414 else
1415 {
1416 if (!(usage & D3DUSAGE_DYNAMIC))
1417 {
1418 return D3DPOOL_MANAGED;
1419 }
1420 }
1421
1422 return D3DPOOL_DEFAULT;
1423}
1424
daniel@transgaming.com38380882012-11-28 19:36:39 +00001425bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1426 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001427{
1428 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
1429}
1430
daniel@transgaming.com38380882012-11-28 19:36:39 +00001431bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1432 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001433{
1434 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
1435}
1436
1437bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
1438{
1439 return mBlit->boxFilter(source, dest);
1440}
1441
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001442D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001443{
1444 if (mD3d9Ex != NULL)
1445 {
1446 return D3DPOOL_DEFAULT;
1447 }
1448 else
1449 {
1450 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
1451 {
1452 return D3DPOOL_MANAGED;
1453 }
1454 }
1455
1456 return D3DPOOL_DEFAULT;
1457}
1458
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001459bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
1460{
1461 if (source && dest)
1462 {
1463 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
1464 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
1465
1466 if (fromManaged)
1467 {
1468 D3DSURFACE_DESC desc;
1469 source->GetDesc(&desc);
1470
1471 IDirect3DSurface9 *surf = 0;
1472 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
1473
1474 if (SUCCEEDED(result))
1475 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001476 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001477 result = device->UpdateSurface(surf, NULL, dest, NULL);
1478 surf->Release();
1479 }
1480 }
1481 else
1482 {
1483 endScene();
1484 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1485 }
1486
1487 if (FAILED(result))
1488 {
1489 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1490 return false;
1491 }
1492 }
1493
1494 return true;
1495}
1496
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001497}