blob: 2dd6116cbe08def693a9fa1d5ef22eedd9fb07ee [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
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000748void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
749 int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000750{
751 bool depthStencilStateChanged = mForceSetDepthStencilState ||
752 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000753 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
754 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000755 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
756 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
757
758 if (depthStencilStateChanged)
759 {
760 if (depthStencilState.depthTest)
761 {
762 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
763 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
764 }
765 else
766 {
767 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
768 }
769
770 mCurDepthStencilState = depthStencilState;
771 }
772
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000773 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000774 {
775 if (depthStencilState.stencilTest && stencilSize > 0)
776 {
777 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
778 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
779
780 // FIXME: Unsupported by D3D9
781 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
782 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
783 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
784 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000785 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000786 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
787 {
788 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
789 return error(GL_INVALID_OPERATION);
790 }
791
792 // get the maximum size of the stencil ref
793 GLuint maxStencil = (1 << stencilSize) - 1;
794
795 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
796 depthStencilState.stencilWritemask);
797 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
798 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
799
800 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000801 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
803 depthStencilState.stencilMask);
804
805 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
806 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
807 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
808 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
809 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
810 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
811
812 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
813 depthStencilState.stencilBackWritemask);
814 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
815 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
816
817 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000818 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000819 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
820 depthStencilState.stencilBackMask);
821
822 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
823 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
824 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
825 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
826 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
827 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
828 }
829 else
830 {
831 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
832 }
833
834 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
835
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000836 mCurStencilRef = stencilRef;
837 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000838 mCurFrontFaceCCW = frontFaceCCW;
839 mCurStencilSize = stencilSize;
840 }
841
842 mForceSetDepthStencilState = false;
843}
844
845void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
846 unsigned int renderTargetHeight)
847{
848 bool renderTargetSizedChanged = mForceSetScissor ||
849 renderTargetWidth != mCurRenderTargetWidth ||
850 renderTargetHeight != mCurRenderTargetHeight;
851 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
852
853 if (renderTargetSizedChanged || scissorChanged)
854 {
855 RECT rect;
856 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
857 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth));
858 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
859 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth));
860 mDevice->SetScissorRect(&rect);
861
862 mCurScissor = scissor;
863 mCurRenderTargetWidth = renderTargetWidth;
864 mCurRenderTargetHeight = renderTargetHeight;
865 }
866
867 mForceSetScissor = false;
868}
869
870void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer)
871{
872 mForceSetScissor = true;
873
874 // TODO
875}
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000876
daniel@transgaming.comd084c622012-11-28 19:36:05 +0000877void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear,
878 gl::Framebuffer *frameBuffer)
879{
880 mForceSetDepthStencilState = true;
881
882 // TODO
883}
884
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000885void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000886{
887 while (!mEventQueryPool.empty())
888 {
889 mEventQueryPool.back()->Release();
890 mEventQueryPool.pop_back();
891 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000892
893 mVertexShaderCache.clear();
894 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000895}
896
897
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000898void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000899{
900 mDeviceLost = true;
901}
902
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000903bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000904{
905 return mDeviceLost;
906}
907
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000908// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000909bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000910{
911 bool isLost = false;
912
913 if (mDeviceEx)
914 {
915 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
916 }
917 else if (mDevice)
918 {
919 isLost = FAILED(mDevice->TestCooperativeLevel());
920 }
921 else
922 {
923 // No device yet, so no reset required
924 }
925
926 if (isLost)
927 {
928 // ensure we note the device loss --
929 // we'll probably get this done again by markDeviceLost
930 // but best to remember it!
931 // Note that we don't want to clear the device loss status here
932 // -- this needs to be done by resetDevice
933 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000934 if (notify)
935 {
936 mDisplay->notifyDeviceLost();
937 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000938 }
939
940 return isLost;
941}
942
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000943bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000944{
945 HRESULT status = D3D_OK;
946
947 if (mDeviceEx)
948 {
949 status = mDeviceEx->CheckDeviceState(NULL);
950 }
951 else if (mDevice)
952 {
953 status = mDevice->TestCooperativeLevel();
954 }
955
956 switch (status)
957 {
958 case D3DERR_DEVICENOTRESET:
959 case D3DERR_DEVICEHUNG:
960 return true;
961 default:
962 return false;
963 }
964}
965
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000966bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000967{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000968 releaseDeviceResources();
969
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000970 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
971
972 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000973 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000974 int attempts = 3;
975
976 while (lost && attempts > 0)
977 {
978 if (mDeviceEx)
979 {
980 Sleep(500); // Give the graphics driver some CPU time
981 result = mDeviceEx->ResetEx(&presentParameters, NULL);
982 }
983 else
984 {
985 result = mDevice->TestCooperativeLevel();
986 while (result == D3DERR_DEVICELOST)
987 {
988 Sleep(100); // Give the graphics driver some CPU time
989 result = mDevice->TestCooperativeLevel();
990 }
991
992 if (result == D3DERR_DEVICENOTRESET)
993 {
994 result = mDevice->Reset(&presentParameters);
995 }
996 }
997
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000998 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000999 attempts --;
1000 }
1001
1002 if (FAILED(result))
1003 {
1004 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1005 return false;
1006 }
1007
1008 // reset device defaults
1009 initializeDevice();
1010 mDeviceLost = false;
1011
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001012 mForceSetDepthStencilState = true;
1013 mForceSetRasterState = true;
1014 mForceSetBlendState = true;
1015 mForceSetScissor = true;
1016
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001017 return true;
1018}
1019
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001020DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001021{
1022 return mAdapterIdentifier.VendorId;
1023}
1024
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001025const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001026{
1027 return mAdapterIdentifier.Description;
1028}
1029
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001030GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001031{
1032 return mAdapterIdentifier.DeviceIdentifier;
1033}
1034
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001035void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001036{
1037 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1038 {
1039 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1040 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1041
1042 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1043 }
1044}
1045
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001046bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001047{
1048 D3DDISPLAYMODE currentDisplayMode;
1049 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1050
1051 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1052}
1053
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001054bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001055{
1056 D3DDISPLAYMODE currentDisplayMode;
1057 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1058
1059 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1060}
1061
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001062bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001063{
1064 D3DDISPLAYMODE currentDisplayMode;
1065 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1066
1067 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1068}
1069
1070// we use INTZ for depth textures in Direct3D9
1071// we also want NULL texture support to ensure the we can make depth-only FBOs
1072// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001073bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001074{
1075 D3DDISPLAYMODE currentDisplayMode;
1076 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1077
1078 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1079 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1080 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1081 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1082
1083 return intz && null;
1084}
1085
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001086bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001087{
1088 D3DDISPLAYMODE currentDisplayMode;
1089 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1090
1091 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1092 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1093 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1094 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1095
1096 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1097 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1098 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1099 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1100
1101 if (!*filtering && !*renderable)
1102 {
1103 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1104 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1105 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1106 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1107 }
1108 else
1109 {
1110 return true;
1111 }
1112}
1113
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001114bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001115{
1116 D3DDISPLAYMODE currentDisplayMode;
1117 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1118
1119 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1120 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1121 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1122 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1123
1124 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1125 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1126 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1127 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1128
1129 if (!*filtering && !*renderable)
1130 {
1131 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1132 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1133 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1134 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1135 }
1136 else
1137 {
1138 return true;
1139 }
1140}
1141
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001142bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001143{
1144 D3DDISPLAYMODE currentDisplayMode;
1145 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1146
1147 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1148}
1149
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001150bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001151{
1152 D3DDISPLAYMODE currentDisplayMode;
1153 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1154
1155 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1156}
1157
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001158bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001159{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001160 return mSupportsTextureFilterAnisotropy;
1161}
1162
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001163float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001164{
1165 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001166 {
1167 return mDeviceCaps.MaxAnisotropy;
1168 }
1169 return 1.0f;
1170}
1171
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001172bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001173{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001174 IDirect3DQuery9 *query = allocateEventQuery();
1175 if (query)
1176 {
1177 freeEventQuery(query);
1178 return true;
1179 }
1180 else
1181 {
1182 return false;
1183 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001184 return true;
1185}
1186
1187// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1188// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001189bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001190{
1191 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1192 {
1193 return false;
1194 }
1195
1196 D3DDISPLAYMODE currentDisplayMode;
1197 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1198
1199 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1200
1201 return SUCCEEDED(result);
1202}
1203
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001204bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001205{
1206 return mSupportsNonPower2Textures;
1207}
1208
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001209bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001210{
1211 if (!mDevice)
1212 {
1213 return false;
1214 }
1215
1216 IDirect3DQuery9 *query = NULL;
1217 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1218 if (SUCCEEDED(result) && query)
1219 {
1220 query->Release();
1221 return true;
1222 }
1223 else
1224 {
1225 return false;
1226 }
1227}
1228
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001229bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001230{
1231 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1232}
1233
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001234bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001235{
1236 // PIX doesn't seem to support using share handles, so disable them.
1237 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001238 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001239}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001240
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001241bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001242{
1243 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1244}
1245
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001246float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001247{
1248 return mDeviceCaps.MaxPointSize;
1249}
1250
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001251int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001252{
1253 return (int)mDeviceCaps.MaxTextureWidth;
1254}
1255
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001256int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001257{
1258 return (int)mDeviceCaps.MaxTextureHeight;
1259}
1260
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001261bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001262{
1263 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1264}
1265
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001266DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001267{
1268 return mDeviceCaps.DeclTypes;
1269}
1270
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001271int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001272{
1273 return mMinSwapInterval;
1274}
1275
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001276int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001277{
1278 return mMaxSwapInterval;
1279}
1280
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001281int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001282{
1283 return mMaxSupportedSamples;
1284}
1285
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001286int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001287{
1288 if (requested == 0)
1289 {
1290 return requested;
1291 }
1292
1293 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
1294 if (itr == mMultiSampleSupport.end())
1295 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00001296 if (format == D3DFMT_UNKNOWN)
1297 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001298 return -1;
1299 }
1300
1301 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
1302 {
1303 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
1304 {
1305 return i;
1306 }
1307 }
1308
1309 return -1;
1310}
1311
daniel@transgaming.coma9571682012-11-28 19:33:08 +00001312D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
1313{
1314 switch (internalformat)
1315 {
1316 case GL_DEPTH_COMPONENT16:
1317 case GL_DEPTH_COMPONENT32_OES:
1318 case GL_DEPTH24_STENCIL8_OES:
1319 return D3DFMT_INTZ;
1320 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1321 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1322 return D3DFMT_DXT1;
1323 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1324 return D3DFMT_DXT3;
1325 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1326 return D3DFMT_DXT5;
1327 case GL_RGBA32F_EXT:
1328 case GL_RGB32F_EXT:
1329 case GL_ALPHA32F_EXT:
1330 case GL_LUMINANCE32F_EXT:
1331 case GL_LUMINANCE_ALPHA32F_EXT:
1332 return D3DFMT_A32B32G32R32F;
1333 case GL_RGBA16F_EXT:
1334 case GL_RGB16F_EXT:
1335 case GL_ALPHA16F_EXT:
1336 case GL_LUMINANCE16F_EXT:
1337 case GL_LUMINANCE_ALPHA16F_EXT:
1338 return D3DFMT_A16B16G16R16F;
1339 case GL_LUMINANCE8_EXT:
1340 if (getLuminanceTextureSupport())
1341 {
1342 return D3DFMT_L8;
1343 }
1344 break;
1345 case GL_LUMINANCE8_ALPHA8_EXT:
1346 if (getLuminanceAlphaTextureSupport())
1347 {
1348 return D3DFMT_A8L8;
1349 }
1350 break;
1351 case GL_RGB8_OES:
1352 case GL_RGB565:
1353 return D3DFMT_X8R8G8B8;
1354 }
1355
1356 return D3DFMT_A8R8G8B8;
1357}
1358
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001359bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001360{
1361 bool result = false;
1362
1363 if (source && dest)
1364 {
1365 int levels = source->levelCount();
1366 for (int i = 0; i < levels; ++i)
1367 {
1368 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
1369 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
1370
1371 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1372
1373 if (srcSurf) srcSurf->Release();
1374 if (dstSurf) dstSurf->Release();
1375
1376 if (!result)
1377 return false;
1378 }
1379 }
1380
1381 return result;
1382}
1383
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001384bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001385{
1386 bool result = false;
1387
1388 if (source && dest)
1389 {
1390 int levels = source->levelCount();
1391 for (int f = 0; f < 6; f++)
1392 {
1393 for (int i = 0; i < levels; i++)
1394 {
1395 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
1396 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
1397
1398 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1399
1400 if (srcSurf) srcSurf->Release();
1401 if (dstSurf) dstSurf->Release();
1402
1403 if (!result)
1404 return false;
1405 }
1406 }
1407 }
1408
1409 return result;
1410}
1411
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001412D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001413{
1414 if (mD3d9Ex != NULL)
1415 {
1416 return D3DPOOL_DEFAULT;
1417 }
1418 else
1419 {
1420 if (!(usage & D3DUSAGE_DYNAMIC))
1421 {
1422 return D3DPOOL_MANAGED;
1423 }
1424 }
1425
1426 return D3DPOOL_DEFAULT;
1427}
1428
daniel@transgaming.com38380882012-11-28 19:36:39 +00001429bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1430 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001431{
1432 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
1433}
1434
daniel@transgaming.com38380882012-11-28 19:36:39 +00001435bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1436 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001437{
1438 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
1439}
1440
1441bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
1442{
1443 return mBlit->boxFilter(source, dest);
1444}
1445
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001446D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001447{
1448 if (mD3d9Ex != NULL)
1449 {
1450 return D3DPOOL_DEFAULT;
1451 }
1452 else
1453 {
1454 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
1455 {
1456 return D3DPOOL_MANAGED;
1457 }
1458 }
1459
1460 return D3DPOOL_DEFAULT;
1461}
1462
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001463bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
1464{
1465 if (source && dest)
1466 {
1467 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
1468 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
1469
1470 if (fromManaged)
1471 {
1472 D3DSURFACE_DESC desc;
1473 source->GetDesc(&desc);
1474
1475 IDirect3DSurface9 *surf = 0;
1476 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
1477
1478 if (SUCCEEDED(result))
1479 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001480 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001481 result = device->UpdateSurface(surf, NULL, dest, NULL);
1482 surf->Release();
1483 }
1484 }
1485 else
1486 {
1487 endScene();
1488 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
1489 }
1490
1491 if (FAILED(result))
1492 {
1493 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
1494 return false;
1495 }
1496 }
1497
1498 return true;
1499}
1500
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001501}