blob: 480ffa9caf8abfe3f3fa92751aaabdb409bc3d95 [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
505// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000506IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000507{
508 IDirect3DQuery9 *query = NULL;
509
510 if (mEventQueryPool.empty())
511 {
512 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
513 ASSERT(SUCCEEDED(result));
514 }
515 else
516 {
517 query = mEventQueryPool.back();
518 mEventQueryPool.pop_back();
519 }
520
521 return query;
522}
523
524// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000525void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000526{
527 if (mEventQueryPool.size() > 1000)
528 {
529 query->Release();
530 }
531 else
532 {
533 mEventQueryPool.push_back(query);
534 }
535}
536
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000537IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000538{
539 return mVertexShaderCache.create(function, length);
540}
541
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000542IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000543{
544 return mPixelShaderCache.create(function, length);
545}
546
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000547HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
548{
549 D3DPOOL Pool = getBufferPool(Usage);
550 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
551}
552
553HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
554{
555 D3DPOOL Pool = getBufferPool(Usage);
556 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
557}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000558
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000559void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000560{
561 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
562 int d3dSampler = index + d3dSamplerOffset;
563
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000564 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
565 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000566
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000567 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000568 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000569 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000570 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
571 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
572 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
573 if (mSupportsTextureFilterAnisotropy)
574 {
575 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
576 }
577}
578
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000579void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000580{
581 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
582 int d3dSampler = index + d3dSamplerOffset;
583 IDirect3DBaseTexture9 *d3dTexture = NULL;
584
585 if (texture)
586 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000587 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000588 if (texStorage)
589 {
590 d3dTexture = texStorage->getBaseTexture();
591 }
592 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000593 // in the texture class and we're unexpectedly missing the d3d texture
594 ASSERT(d3dTexture != NULL);
595 }
596
597 mDevice->SetTexture(d3dSampler, d3dTexture);
598}
599
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000600void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize)
601{
602 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
603 bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize;
604
605 if (rasterStateChanged)
606 {
607 // Set the cull mode
608 if (rasterState.cullFace)
609 {
610 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
611 }
612 else
613 {
614 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
615 }
616
617 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
618
619 mCurRasterState = rasterState;
620 }
621
622 if (rasterStateChanged || depthSizeChanged)
623 {
624 if (rasterState.polygonOffsetFill)
625 {
626 if (depthSize > 0)
627 {
628 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
629
630 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
631 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
632 }
633 }
634 else
635 {
636 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
637 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
638 }
639
640 mCurDepthSize = depthSize;
641 }
642
643 mForceSetRasterState = false;
644}
645
646void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
647{
648 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
649 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
650 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
651
652 if (blendStateChanged || blendColorChanged)
653 {
654 if (blendState.blend)
655 {
656 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
657
658 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
659 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
660 {
661 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
662 }
663 else
664 {
665 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
666 gl::unorm<8>(blendColor.alpha),
667 gl::unorm<8>(blendColor.alpha),
668 gl::unorm<8>(blendColor.alpha)));
669 }
670
671 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
672 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
673 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
674
675 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
676 blendState.destBlendRGB != blendState.destBlendAlpha ||
677 blendState.blendEquationRGB != blendState.blendEquationAlpha)
678 {
679 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
680
681 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
682 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
683 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
684 }
685 else
686 {
687 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
688 }
689 }
690 else
691 {
692 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
693 }
694
695 if (blendState.sampleAlphaToCoverage)
696 {
697 FIXME("Sample alpha to coverage is unimplemented.");
698 }
699
700 // Set the color mask
701 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
702 // Apparently some ATI cards have a bug where a draw with a zero color
703 // write mask can cause later draws to have incorrect results. Instead,
704 // set a nonzero color write mask but modify the blend state so that no
705 // drawing is done.
706 // http://code.google.com/p/angleproject/issues/detail?id=169
707
708 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
709 blendState.colorMaskBlue, blendState.colorMaskAlpha);
710 if (colorMask == 0 && !zeroColorMaskAllowed)
711 {
712 // Enable green channel, but set blending so nothing will be drawn.
713 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
714 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
715
716 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
717 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
718 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
719 }
720 else
721 {
722 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
723 }
724
725 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
726
727 mCurBlendState = blendState;
728 mCurBlendColor = blendColor;
729 }
730
731 if (sampleMaskChanged)
732 {
733 // Set the multisample mask
734 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
735 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
736
737 mCurSampleMask = sampleMask;
738 }
739
740 mForceSetBlendState = false;
741}
742
743void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, bool frontFaceCCW,
744 unsigned int stencilSize)
745{
746 bool depthStencilStateChanged = mForceSetDepthStencilState ||
747 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
748 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
749 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
750
751 if (depthStencilStateChanged)
752 {
753 if (depthStencilState.depthTest)
754 {
755 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
756 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
757 }
758 else
759 {
760 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
761 }
762
763 mCurDepthStencilState = depthStencilState;
764 }
765
766 if (depthStencilStateChanged || frontFaceCCWChanged || stencilSizeChanged)
767 {
768 if (depthStencilState.stencilTest && stencilSize > 0)
769 {
770 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
771 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
772
773 // FIXME: Unsupported by D3D9
774 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
775 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
776 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
777 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
778 depthStencilState.stencilRef != depthStencilState.stencilBackRef ||
779 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
780 {
781 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
782 return error(GL_INVALID_OPERATION);
783 }
784
785 // get the maximum size of the stencil ref
786 GLuint maxStencil = (1 << stencilSize) - 1;
787
788 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
789 depthStencilState.stencilWritemask);
790 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
791 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
792
793 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
794 (depthStencilState.stencilRef < (GLint)maxStencil) ? depthStencilState.stencilRef : maxStencil);
795 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
796 depthStencilState.stencilMask);
797
798 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
799 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
800 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
801 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
802 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
803 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
804
805 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
806 depthStencilState.stencilBackWritemask);
807 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
808 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
809
810 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
811 (depthStencilState.stencilBackRef < (GLint)maxStencil) ? depthStencilState.stencilBackRef : maxStencil);
812 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
813 depthStencilState.stencilBackMask);
814
815 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
816 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
817 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
818 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
819 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
820 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
821 }
822 else
823 {
824 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
825 }
826
827 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
828
829 mCurFrontFaceCCW = frontFaceCCW;
830 mCurStencilSize = stencilSize;
831 }
832
833 mForceSetDepthStencilState = false;
834}
835
836void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
837 unsigned int renderTargetHeight)
838{
839 bool renderTargetSizedChanged = mForceSetScissor ||
840 renderTargetWidth != mCurRenderTargetWidth ||
841 renderTargetHeight != mCurRenderTargetHeight;
842 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
843
844 if (renderTargetSizedChanged || scissorChanged)
845 {
846 RECT rect;
847 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
848 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth));
849 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
850 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth));
851 mDevice->SetScissorRect(&rect);
852
853 mCurScissor = scissor;
854 mCurRenderTargetWidth = renderTargetWidth;
855 mCurRenderTargetHeight = renderTargetHeight;
856 }
857
858 mForceSetScissor = false;
859}
860
861void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer)
862{
863 mForceSetScissor = true;
864
865 // TODO
866}
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000867
daniel@transgaming.comd084c622012-11-28 19:36:05 +0000868void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear,
869 gl::Framebuffer *frameBuffer)
870{
871 mForceSetDepthStencilState = true;
872
873 // TODO
874}
875
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000876void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000877{
878 while (!mEventQueryPool.empty())
879 {
880 mEventQueryPool.back()->Release();
881 mEventQueryPool.pop_back();
882 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000883
884 mVertexShaderCache.clear();
885 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000886}
887
888
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000889void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000890{
891 mDeviceLost = true;
892}
893
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000894bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000895{
896 return mDeviceLost;
897}
898
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000899// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000900bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000901{
902 bool isLost = false;
903
904 if (mDeviceEx)
905 {
906 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
907 }
908 else if (mDevice)
909 {
910 isLost = FAILED(mDevice->TestCooperativeLevel());
911 }
912 else
913 {
914 // No device yet, so no reset required
915 }
916
917 if (isLost)
918 {
919 // ensure we note the device loss --
920 // we'll probably get this done again by markDeviceLost
921 // but best to remember it!
922 // Note that we don't want to clear the device loss status here
923 // -- this needs to be done by resetDevice
924 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000925 if (notify)
926 {
927 mDisplay->notifyDeviceLost();
928 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000929 }
930
931 return isLost;
932}
933
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000934bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000935{
936 HRESULT status = D3D_OK;
937
938 if (mDeviceEx)
939 {
940 status = mDeviceEx->CheckDeviceState(NULL);
941 }
942 else if (mDevice)
943 {
944 status = mDevice->TestCooperativeLevel();
945 }
946
947 switch (status)
948 {
949 case D3DERR_DEVICENOTRESET:
950 case D3DERR_DEVICEHUNG:
951 return true;
952 default:
953 return false;
954 }
955}
956
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000957bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000958{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000959 releaseDeviceResources();
960
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000961 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
962
963 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000964 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000965 int attempts = 3;
966
967 while (lost && attempts > 0)
968 {
969 if (mDeviceEx)
970 {
971 Sleep(500); // Give the graphics driver some CPU time
972 result = mDeviceEx->ResetEx(&presentParameters, NULL);
973 }
974 else
975 {
976 result = mDevice->TestCooperativeLevel();
977 while (result == D3DERR_DEVICELOST)
978 {
979 Sleep(100); // Give the graphics driver some CPU time
980 result = mDevice->TestCooperativeLevel();
981 }
982
983 if (result == D3DERR_DEVICENOTRESET)
984 {
985 result = mDevice->Reset(&presentParameters);
986 }
987 }
988
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000989 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000990 attempts --;
991 }
992
993 if (FAILED(result))
994 {
995 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
996 return false;
997 }
998
999 // reset device defaults
1000 initializeDevice();
1001 mDeviceLost = false;
1002
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001003 mForceSetDepthStencilState = true;
1004 mForceSetRasterState = true;
1005 mForceSetBlendState = true;
1006 mForceSetScissor = true;
1007
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001008 return true;
1009}
1010
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001011DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001012{
1013 return mAdapterIdentifier.VendorId;
1014}
1015
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001016const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001017{
1018 return mAdapterIdentifier.Description;
1019}
1020
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001021GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001022{
1023 return mAdapterIdentifier.DeviceIdentifier;
1024}
1025
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001026void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001027{
1028 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1029 {
1030 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1031 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1032
1033 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1034 }
1035}
1036
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001037bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001038{
1039 D3DDISPLAYMODE currentDisplayMode;
1040 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1041
1042 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1043}
1044
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001045bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001046{
1047 D3DDISPLAYMODE currentDisplayMode;
1048 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1049
1050 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1051}
1052
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001053bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001054{
1055 D3DDISPLAYMODE currentDisplayMode;
1056 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1057
1058 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1059}
1060
1061// we use INTZ for depth textures in Direct3D9
1062// we also want NULL texture support to ensure the we can make depth-only FBOs
1063// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001064bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001065{
1066 D3DDISPLAYMODE currentDisplayMode;
1067 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1068
1069 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1070 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1071 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1072 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1073
1074 return intz && null;
1075}
1076
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001077bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001078{
1079 D3DDISPLAYMODE currentDisplayMode;
1080 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1081
1082 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1083 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1084 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1085 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1086
1087 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1088 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1089 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1090 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1091
1092 if (!*filtering && !*renderable)
1093 {
1094 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1095 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1096 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1097 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1098 }
1099 else
1100 {
1101 return true;
1102 }
1103}
1104
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001105bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001106{
1107 D3DDISPLAYMODE currentDisplayMode;
1108 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1109
1110 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1111 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1112 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1113 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1114
1115 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1116 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1117 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1118 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1119
1120 if (!*filtering && !*renderable)
1121 {
1122 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1123 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1124 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1125 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1126 }
1127 else
1128 {
1129 return true;
1130 }
1131}
1132
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001133bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001134{
1135 D3DDISPLAYMODE currentDisplayMode;
1136 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1137
1138 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1139}
1140
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001141bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001142{
1143 D3DDISPLAYMODE currentDisplayMode;
1144 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1145
1146 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1147}
1148
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001149bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001150{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001151 return mSupportsTextureFilterAnisotropy;
1152}
1153
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001154float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001155{
1156 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001157 {
1158 return mDeviceCaps.MaxAnisotropy;
1159 }
1160 return 1.0f;
1161}
1162
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001163bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001164{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001165 IDirect3DQuery9 *query = allocateEventQuery();
1166 if (query)
1167 {
1168 freeEventQuery(query);
1169 return true;
1170 }
1171 else
1172 {
1173 return false;
1174 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001175 return true;
1176}
1177
1178// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1179// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001180bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001181{
1182 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1183 {
1184 return false;
1185 }
1186
1187 D3DDISPLAYMODE currentDisplayMode;
1188 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1189
1190 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1191
1192 return SUCCEEDED(result);
1193}
1194
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001195bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001196{
1197 return mSupportsNonPower2Textures;
1198}
1199
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001200bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001201{
1202 if (!mDevice)
1203 {
1204 return false;
1205 }
1206
1207 IDirect3DQuery9 *query = NULL;
1208 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1209 if (SUCCEEDED(result) && query)
1210 {
1211 query->Release();
1212 return true;
1213 }
1214 else
1215 {
1216 return false;
1217 }
1218}
1219
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001220bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001221{
1222 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1223}
1224
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001225bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001226{
1227 // PIX doesn't seem to support using share handles, so disable them.
1228 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001229 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001230}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001231
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001232bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001233{
1234 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1235}
1236
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001237float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001238{
1239 return mDeviceCaps.MaxPointSize;
1240}
1241
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001242int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001243{
1244 return (int)mDeviceCaps.MaxTextureWidth;
1245}
1246
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001247int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001248{
1249 return (int)mDeviceCaps.MaxTextureHeight;
1250}
1251
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001252bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001253{
1254 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1255}
1256
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001257DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001258{
1259 return mDeviceCaps.DeclTypes;
1260}
1261
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001262int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001263{
1264 return mMinSwapInterval;
1265}
1266
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001267int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001268{
1269 return mMaxSwapInterval;
1270}
1271
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001272int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001273{
1274 return mMaxSupportedSamples;
1275}
1276
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001277int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001278{
1279 if (requested == 0)
1280 {
1281 return requested;
1282 }
1283
1284 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
1285 if (itr == mMultiSampleSupport.end())
1286 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00001287 if (format == D3DFMT_UNKNOWN)
1288 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001289 return -1;
1290 }
1291
1292 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
1293 {
1294 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
1295 {
1296 return i;
1297 }
1298 }
1299
1300 return -1;
1301}
1302
daniel@transgaming.coma9571682012-11-28 19:33:08 +00001303D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
1304{
1305 switch (internalformat)
1306 {
1307 case GL_DEPTH_COMPONENT16:
1308 case GL_DEPTH_COMPONENT32_OES:
1309 case GL_DEPTH24_STENCIL8_OES:
1310 return D3DFMT_INTZ;
1311 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1312 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1313 return D3DFMT_DXT1;
1314 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1315 return D3DFMT_DXT3;
1316 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1317 return D3DFMT_DXT5;
1318 case GL_RGBA32F_EXT:
1319 case GL_RGB32F_EXT:
1320 case GL_ALPHA32F_EXT:
1321 case GL_LUMINANCE32F_EXT:
1322 case GL_LUMINANCE_ALPHA32F_EXT:
1323 return D3DFMT_A32B32G32R32F;
1324 case GL_RGBA16F_EXT:
1325 case GL_RGB16F_EXT:
1326 case GL_ALPHA16F_EXT:
1327 case GL_LUMINANCE16F_EXT:
1328 case GL_LUMINANCE_ALPHA16F_EXT:
1329 return D3DFMT_A16B16G16R16F;
1330 case GL_LUMINANCE8_EXT:
1331 if (getLuminanceTextureSupport())
1332 {
1333 return D3DFMT_L8;
1334 }
1335 break;
1336 case GL_LUMINANCE8_ALPHA8_EXT:
1337 if (getLuminanceAlphaTextureSupport())
1338 {
1339 return D3DFMT_A8L8;
1340 }
1341 break;
1342 case GL_RGB8_OES:
1343 case GL_RGB565:
1344 return D3DFMT_X8R8G8B8;
1345 }
1346
1347 return D3DFMT_A8R8G8B8;
1348}
1349
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001350bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001351{
1352 bool result = false;
1353
1354 if (source && dest)
1355 {
1356 int levels = source->levelCount();
1357 for (int i = 0; i < levels; ++i)
1358 {
1359 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
1360 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
1361
1362 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1363
1364 if (srcSurf) srcSurf->Release();
1365 if (dstSurf) dstSurf->Release();
1366
1367 if (!result)
1368 return false;
1369 }
1370 }
1371
1372 return result;
1373}
1374
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001375bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001376{
1377 bool result = false;
1378
1379 if (source && dest)
1380 {
1381 int levels = source->levelCount();
1382 for (int f = 0; f < 6; f++)
1383 {
1384 for (int i = 0; i < levels; i++)
1385 {
1386 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
1387 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
1388
1389 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1390
1391 if (srcSurf) srcSurf->Release();
1392 if (dstSurf) dstSurf->Release();
1393
1394 if (!result)
1395 return false;
1396 }
1397 }
1398 }
1399
1400 return result;
1401}
1402
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001403D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001404{
1405 if (mD3d9Ex != NULL)
1406 {
1407 return D3DPOOL_DEFAULT;
1408 }
1409 else
1410 {
1411 if (!(usage & D3DUSAGE_DYNAMIC))
1412 {
1413 return D3DPOOL_MANAGED;
1414 }
1415 }
1416
1417 return D3DPOOL_DEFAULT;
1418}
1419
daniel@transgaming.com38380882012-11-28 19:36:39 +00001420bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1421 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001422{
1423 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
1424}
1425
daniel@transgaming.com38380882012-11-28 19:36:39 +00001426bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1427 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001428{
1429 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
1430}
1431
1432bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
1433{
1434 return mBlit->boxFilter(source, dest);
1435}
1436
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001437D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001438{
1439 if (mD3d9Ex != NULL)
1440 {
1441 return D3DPOOL_DEFAULT;
1442 }
1443 else
1444 {
1445 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
1446 {
1447 return D3DPOOL_MANAGED;
1448 }
1449 }
1450
1451 return D3DPOOL_DEFAULT;
1452}
1453
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001454bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
1455{
1456 if (source && dest)
1457 {
1458 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
1459 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
1460
1461 if (fromManaged)
1462 {
1463 D3DSURFACE_DESC desc;
1464 source->GetDesc(&desc);
1465
1466 IDirect3DSurface9 *surf = 0;
1467 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
1468
1469 if (SUCCEEDED(result))
1470 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001471 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001472 result = device->UpdateSurface(surf, NULL, dest, NULL);
1473 surf->Release();
1474 }
1475 }
1476 else
1477 {
1478 endScene();
1479 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1480 }
1481
1482 if (FAILED(result))
1483 {
1484 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1485 return false;
1486 }
1487 }
1488
1489 return true;
1490}
1491
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001492}