blob: c89959fb4008b90f30faf3bf4b15150b1b43d6f7 [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"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000014#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000015#include "libGLESv2/Program.h"
16#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000017#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000018#include "libGLESv2/VertexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000019#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000020#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000021#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000022#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000023#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +000024#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000025#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000026#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000027
daniel@transgaming.com3281f972012-10-31 18:38:51 +000028#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000029#include "libEGL/Display.h"
30
daniel@transgaming.com621ce052012-10-31 17:52:29 +000031// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
32#define REF_RAST 0
33
34// The "Debug This Pixel..." feature in PIX often fails when using the
35// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
36// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
37#if !defined(ANGLE_ENABLE_D3D9EX)
38// Enables use of the IDirect3D9Ex interface, when available
39#define ANGLE_ENABLE_D3D9EX 1
40#endif // !defined(ANGLE_ENABLE_D3D9EX)
41
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000042#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
43#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
44#endif
45
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000046namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000047{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000048static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000049 {
50 D3DFMT_A1R5G5B5,
51 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
52 D3DFMT_A8R8G8B8,
53 D3DFMT_R5G6B5,
54 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
55 D3DFMT_X8R8G8B8
56 };
57
daniel@transgaming.com222ee082012-11-28 19:31:49 +000058static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000059 {
60 D3DFMT_UNKNOWN,
61 // D3DFMT_D16_LOCKABLE,
62 D3DFMT_D32,
63 // D3DFMT_D15S1,
64 D3DFMT_D24S8,
65 D3DFMT_D24X8,
66 // D3DFMT_D24X4S4,
67 D3DFMT_D16,
68 // D3DFMT_D32F_LOCKABLE,
69 // D3DFMT_D24FS8
70 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000071
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000072Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000073{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000074 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000075
daniel@transgaming.com7d738a22012-11-28 19:43:08 +000076 mD3dCompilerModule = NULL;
77
daniel@transgaming.com621ce052012-10-31 17:52:29 +000078 mD3d9 = NULL;
79 mD3d9Ex = NULL;
80 mDevice = NULL;
81 mDeviceEx = NULL;
82 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000083 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000084
85 mAdapter = D3DADAPTER_DEFAULT;
86
87 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
88 mDeviceType = D3DDEVTYPE_REF;
89 #else
90 mDeviceType = D3DDEVTYPE_HAL;
91 #endif
92
93 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000094
95 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000096
daniel@transgaming.com91207b72012-11-28 20:56:43 +000097 mAppliedIBSerial = 0;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000098
99 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000100
101 mVertexDataManager = NULL;
102 mIndexDataManager = NULL;
103 mLineLoopIB = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000104}
105
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000106Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000107{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000108 releaseDeviceResources();
109
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000110 if (mDevice)
111 {
112 // 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 +0000113 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000114 {
115 resetDevice();
116 }
117
118 mDevice->Release();
119 mDevice = NULL;
120 }
121
122 if (mDeviceEx)
123 {
124 mDeviceEx->Release();
125 mDeviceEx = NULL;
126 }
127
128 if (mD3d9)
129 {
130 mD3d9->Release();
131 mD3d9 = NULL;
132 }
133
134 if (mDeviceWindow)
135 {
136 DestroyWindow(mDeviceWindow);
137 mDeviceWindow = NULL;
138 }
139
140 if (mD3d9Ex)
141 {
142 mD3d9Ex->Release();
143 mD3d9Ex = NULL;
144 }
145
146 if (mD3d9Module)
147 {
148 mD3d9Module = NULL;
149 }
150
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000151 if (mD3dCompilerModule)
152 {
153 FreeLibrary(mD3dCompilerModule);
154 mD3dCompilerModule = NULL;
155 }
156
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000157 while (!mMultiSampleSupport.empty())
158 {
159 delete [] mMultiSampleSupport.begin()->second;
160 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
161 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000162}
163
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000164Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
165{
166 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
167 return static_cast<rx::Renderer9*>(renderer);
168}
169
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000170EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000171{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000172 if (mSoftwareDevice)
173 {
174 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
175 }
176 else
177 {
178 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
179 }
180
181 if (mD3d9Module == NULL)
182 {
183 ERR("No D3D9 module found - aborting!\n");
184 return EGL_NOT_INITIALIZED;
185 }
186
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000187 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
188 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
189
190 // Use Direct3D9Ex if available. Among other things, this version is less
191 // inclined to report a lost context, for example when the user switches
192 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
193 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
194 {
195 ASSERT(mD3d9Ex);
196 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
197 ASSERT(mD3d9);
198 }
199 else
200 {
201 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
202 }
203
204 if (!mD3d9)
205 {
206 ERR("Could not create D3D9 device - aborting!\n");
207 return EGL_NOT_INITIALIZED;
208 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000209
210#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
211 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
212 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
213
214 for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i)
215 {
216 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
217 {
218 break;
219 }
220 }
221#else
222 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
223 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
224#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
225
226 if (!mD3dCompilerModule)
227 {
228 terminate();
229 return false;
230 }
231
232 mD3DCompileFunc = reinterpret_cast<D3DCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
233 ASSERT(mD3DCompileFunc);
234
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000235 if (mDc != NULL)
236 {
237 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
238 }
239
240 HRESULT result;
241
242 // Give up on getting device caps after about one second.
243 for (int i = 0; i < 10; ++i)
244 {
245 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
246 if (SUCCEEDED(result))
247 {
248 break;
249 }
250 else if (result == D3DERR_NOTAVAILABLE)
251 {
252 Sleep(100); // Give the driver some time to initialize/recover
253 }
254 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
255 {
256 ERR("failed to get device caps (0x%x)\n", result);
257 return EGL_NOT_INITIALIZED;
258 }
259 }
260
261 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
262 {
263 ERR("Renderer does not support PS 2.0. aborting!\n");
264 return EGL_NOT_INITIALIZED;
265 }
266
267 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
268 // This is required by Texture2D::convertToRenderTarget.
269 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
270 {
271 ERR("Renderer does not support stretctrect from textures!\n");
272 return EGL_NOT_INITIALIZED;
273 }
274
275 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
276
277 // ATI cards on XP have problems with non-power-of-two textures.
278 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
279 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
280 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
281 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
282
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000283 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
284 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
285
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000286 mMinSwapInterval = 4;
287 mMaxSwapInterval = 0;
288
289 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
290 {
291 mMinSwapInterval = std::min(mMinSwapInterval, 0);
292 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
293 }
294 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
295 {
296 mMinSwapInterval = std::min(mMinSwapInterval, 1);
297 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
298 }
299 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
300 {
301 mMinSwapInterval = std::min(mMinSwapInterval, 2);
302 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
303 }
304 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
305 {
306 mMinSwapInterval = std::min(mMinSwapInterval, 3);
307 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
308 }
309 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
310 {
311 mMinSwapInterval = std::min(mMinSwapInterval, 4);
312 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
313 }
314
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000315 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000316 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000317 {
318 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000319 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
320 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000321
322 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
323 {
324 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
325 {
326 max = j;
327 }
328 }
329 }
330
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000331 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000332 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000333 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000334 continue;
335
336 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000337 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
338 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000339
340 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
341 {
342 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
343 {
344 max = j;
345 }
346 }
347 }
348
349 mMaxSupportedSamples = max;
350
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000351 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
352 static const TCHAR className[] = TEXT("STATIC");
353
354 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
355
356 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
357 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
358
359 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
360 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
361 {
362 return EGL_BAD_ALLOC;
363 }
364
365 if (FAILED(result))
366 {
367 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
368
369 if (FAILED(result))
370 {
371 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
372 return EGL_BAD_ALLOC;
373 }
374 }
375
376 if (mD3d9Ex)
377 {
378 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
379 ASSERT(SUCCEEDED(result));
380 }
381
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000382 mVertexShaderCache.initialize(mDevice);
383 mPixelShaderCache.initialize(mDevice);
384
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000385 initializeDevice();
386
387 return EGL_SUCCESS;
388}
389
390// do any one-time device initialization
391// NOTE: this is also needed after a device lost/reset
392// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000393void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000394{
395 // Permanent non-default states
396 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
397 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
398
399 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
400 {
401 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
402 }
403 else
404 {
405 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
406 }
407
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000408 markAllStateDirty();
409
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000410 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000411
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000412 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000413 mBlit = new Blit(this);
414 mVertexDataManager = new gl::VertexDataManager(this);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000415 mIndexDataManager = new gl::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000416}
417
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000418D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000419{
420 D3DPRESENT_PARAMETERS presentParameters = {0};
421
422 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
423 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
424 presentParameters.BackBufferCount = 1;
425 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
426 presentParameters.BackBufferWidth = 1;
427 presentParameters.BackBufferHeight = 1;
428 presentParameters.EnableAutoDepthStencil = FALSE;
429 presentParameters.Flags = 0;
430 presentParameters.hDeviceWindow = mDeviceWindow;
431 presentParameters.MultiSampleQuality = 0;
432 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
433 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
434 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
435 presentParameters.Windowed = TRUE;
436
437 return presentParameters;
438}
439
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000440int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000441{
442 D3DDISPLAYMODE currentDisplayMode;
443 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
444
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000445 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
446 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000447 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
448 int numConfigs = 0;
449
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000450 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000451 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000452 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000453
454 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
455
456 if (SUCCEEDED(result))
457 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000458 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000459 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000460 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000461 HRESULT result = D3D_OK;
462
463 if(depthStencilFormat != D3DFMT_UNKNOWN)
464 {
465 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
466 }
467
468 if (SUCCEEDED(result))
469 {
470 if(depthStencilFormat != D3DFMT_UNKNOWN)
471 {
472 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
473 }
474
475 if (SUCCEEDED(result))
476 {
477 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000478 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
479 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000480 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
481 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
482
483 (*configDescList)[numConfigs++] = newConfig;
484 }
485 }
486 }
487 }
488 }
489
490 return numConfigs;
491}
492
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000493void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000494{
495 delete [] (configDescList);
496}
497
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000498void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000499{
500 if (!mSceneStarted)
501 {
502 long result = mDevice->BeginScene();
503 if (SUCCEEDED(result)) {
504 // This is defensive checking against the device being
505 // lost at unexpected times.
506 mSceneStarted = true;
507 }
508 }
509}
510
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000511void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000512{
513 if (mSceneStarted)
514 {
515 // EndScene can fail if the device was lost, for example due
516 // to a TDR during a draw call.
517 mDevice->EndScene();
518 mSceneStarted = false;
519 }
520}
521
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000522// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000523void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000524{
525 HRESULT result;
526
527 IDirect3DQuery9* query = allocateEventQuery();
528 if (!query)
529 {
530 return;
531 }
532
533 result = query->Issue(D3DISSUE_END);
534 ASSERT(SUCCEEDED(result));
535
536 do
537 {
538 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
539
540 if(block && result == S_FALSE)
541 {
542 // Keep polling, but allow other threads to do something useful first
543 Sleep(0);
544 // explicitly check for device loss
545 // some drivers seem to return S_FALSE even if the device is lost
546 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000547 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000548 {
549 result = D3DERR_DEVICELOST;
550 }
551 }
552 }
553 while(block && result == S_FALSE);
554
555 freeEventQuery(query);
556
557 if (isDeviceLostError(result))
558 {
559 mDisplay->notifyDeviceLost();
560 }
561}
562
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000563SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
564{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000565 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000566}
567
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000568// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000569IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000570{
571 IDirect3DQuery9 *query = NULL;
572
573 if (mEventQueryPool.empty())
574 {
575 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
576 ASSERT(SUCCEEDED(result));
577 }
578 else
579 {
580 query = mEventQueryPool.back();
581 mEventQueryPool.pop_back();
582 }
583
584 return query;
585}
586
587// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000588void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000589{
590 if (mEventQueryPool.size() > 1000)
591 {
592 query->Release();
593 }
594 else
595 {
596 mEventQueryPool.push_back(query);
597 }
598}
599
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000600IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000601{
602 return mVertexShaderCache.create(function, length);
603}
604
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000605IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000606{
607 return mPixelShaderCache.create(function, length);
608}
609
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000610HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
611{
612 D3DPOOL Pool = getBufferPool(Usage);
613 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
614}
615
616HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
617{
618 D3DPOOL Pool = getBufferPool(Usage);
619 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
620}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000621
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000622void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000623{
624 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
625 int d3dSampler = index + d3dSamplerOffset;
626
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000627 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
628 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000629
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000630 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000631 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000632 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000633 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
634 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
635 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
636 if (mSupportsTextureFilterAnisotropy)
637 {
638 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
639 }
640}
641
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000642void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000643{
644 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
645 int d3dSampler = index + d3dSamplerOffset;
646 IDirect3DBaseTexture9 *d3dTexture = NULL;
647
648 if (texture)
649 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000650 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000651 if (texStorage)
652 {
653 d3dTexture = texStorage->getBaseTexture();
654 }
655 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000656 // in the texture class and we're unexpectedly missing the d3d texture
657 ASSERT(d3dTexture != NULL);
658 }
659
660 mDevice->SetTexture(d3dSampler, d3dTexture);
661}
662
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000663void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000664{
665 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000666
667 if (rasterStateChanged)
668 {
669 // Set the cull mode
670 if (rasterState.cullFace)
671 {
672 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
673 }
674 else
675 {
676 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
677 }
678
679 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
680
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000681 if (rasterState.polygonOffsetFill)
682 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000683 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000684 {
685 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
686
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000687 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000688 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
689 }
690 }
691 else
692 {
693 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
694 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
695 }
696
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000697 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000698 }
699
700 mForceSetRasterState = false;
701}
702
703void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
704{
705 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
706 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
707 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
708
709 if (blendStateChanged || blendColorChanged)
710 {
711 if (blendState.blend)
712 {
713 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
714
715 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
716 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
717 {
718 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
719 }
720 else
721 {
722 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
723 gl::unorm<8>(blendColor.alpha),
724 gl::unorm<8>(blendColor.alpha),
725 gl::unorm<8>(blendColor.alpha)));
726 }
727
728 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
729 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
730 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
731
732 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
733 blendState.destBlendRGB != blendState.destBlendAlpha ||
734 blendState.blendEquationRGB != blendState.blendEquationAlpha)
735 {
736 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
737
738 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
739 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
740 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
741 }
742 else
743 {
744 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
745 }
746 }
747 else
748 {
749 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
750 }
751
752 if (blendState.sampleAlphaToCoverage)
753 {
754 FIXME("Sample alpha to coverage is unimplemented.");
755 }
756
757 // Set the color mask
758 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
759 // Apparently some ATI cards have a bug where a draw with a zero color
760 // write mask can cause later draws to have incorrect results. Instead,
761 // set a nonzero color write mask but modify the blend state so that no
762 // drawing is done.
763 // http://code.google.com/p/angleproject/issues/detail?id=169
764
765 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
766 blendState.colorMaskBlue, blendState.colorMaskAlpha);
767 if (colorMask == 0 && !zeroColorMaskAllowed)
768 {
769 // Enable green channel, but set blending so nothing will be drawn.
770 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
771 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
772
773 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
774 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
775 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
776 }
777 else
778 {
779 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
780 }
781
782 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
783
784 mCurBlendState = blendState;
785 mCurBlendColor = blendColor;
786 }
787
788 if (sampleMaskChanged)
789 {
790 // Set the multisample mask
791 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
792 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
793
794 mCurSampleMask = sampleMask;
795 }
796
797 mForceSetBlendState = false;
798}
799
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000800void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000801 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802{
803 bool depthStencilStateChanged = mForceSetDepthStencilState ||
804 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000805 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
806 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000807 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000808
809 if (depthStencilStateChanged)
810 {
811 if (depthStencilState.depthTest)
812 {
813 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
814 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
815 }
816 else
817 {
818 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
819 }
820
821 mCurDepthStencilState = depthStencilState;
822 }
823
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000824 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000825 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000826 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000827 {
828 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
829 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
830
831 // FIXME: Unsupported by D3D9
832 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
833 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
834 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
835 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000836 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000837 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
838 {
839 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
840 return error(GL_INVALID_OPERATION);
841 }
842
843 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000844 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000845
846 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
847 depthStencilState.stencilWritemask);
848 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
849 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
850
851 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000852 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000853 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
854 depthStencilState.stencilMask);
855
856 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
857 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
858 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
859 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
860 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
861 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
862
863 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
864 depthStencilState.stencilBackWritemask);
865 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
866 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
867
868 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000869 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000870 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
871 depthStencilState.stencilBackMask);
872
873 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
874 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
875 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
876 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
877 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
878 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
879 }
880 else
881 {
882 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
883 }
884
885 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
886
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000887 mCurStencilRef = stencilRef;
888 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000889 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000890 }
891
892 mForceSetDepthStencilState = false;
893}
894
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000895void Renderer9::setScissorRectangle(const gl::Rectangle &scissor)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000896{
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000897 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
898
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000899 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000900 {
901 RECT rect;
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000902 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
903 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
904 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
905 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000906 mDevice->SetScissorRect(&rect);
907
908 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000909 }
910
911 mForceSetScissor = false;
912}
913
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000914bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000915 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
916{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000917 gl::Rectangle actualViewport = viewport;
918 float actualZNear = gl::clamp01(zNear);
919 float actualZFar = gl::clamp01(zFar);
920 if (ignoreViewport)
921 {
922 actualViewport.x = 0;
923 actualViewport.y = 0;
924 actualViewport.width = mRenderTargetDesc.width;
925 actualViewport.height = mRenderTargetDesc.height;
926 actualZNear = 0.0f;
927 actualZFar = 1.0f;
928 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000929
930 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000931 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
932 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
933 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
934 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
935 dxViewport.MinZ = actualZNear;
936 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000937
938 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
939 {
940 return false; // Nothing to render
941 }
942
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000943 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
944 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000945 if (viewportChanged)
946 {
947 mDevice->SetViewport(&dxViewport);
948
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000949 mCurViewport = actualViewport;
950 mCurNear = actualZNear;
951 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000952 }
953
954 if (currentProgram && (viewportChanged || forceSetUniforms))
955 {
956 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
957 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
958 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
959
960 // These values are used for computing gl_FragCoord in Program::linkVaryings().
961 GLint coord = currentProgram->getDxCoordLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000962 GLfloat whxy[4] = { actualViewport.width * 0.5f,
963 actualViewport.height * 0.5f,
964 actualViewport.x + (actualViewport.width * 0.5f),
965 actualViewport.y + (actualViewport.height * 0.5f) };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000966 currentProgram->setUniform4fv(coord, 1, whxy);
967
968 GLint depth = currentProgram->getDxDepthLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000969 GLfloat dz[2] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000970 currentProgram->setUniform2fv(depth, 1, dz);
971
972 GLint depthRange = currentProgram->getDxDepthRangeLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000973 GLfloat nearFarDiff[3] = { actualZNear, actualZFar, actualZFar - actualZNear };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000974 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
975 }
976
977 mForceSetViewport = false;
978 return true;
979}
980
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000981bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
982{
983 switch (mode)
984 {
985 case GL_POINTS:
986 mPrimitiveType = D3DPT_POINTLIST;
987 mPrimitiveCount = count;
988 break;
989 case GL_LINES:
990 mPrimitiveType = D3DPT_LINELIST;
991 mPrimitiveCount = count / 2;
992 break;
993 case GL_LINE_LOOP:
994 mPrimitiveType = D3DPT_LINESTRIP;
995 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
996 break;
997 case GL_LINE_STRIP:
998 mPrimitiveType = D3DPT_LINESTRIP;
999 mPrimitiveCount = count - 1;
1000 break;
1001 case GL_TRIANGLES:
1002 mPrimitiveType = D3DPT_TRIANGLELIST;
1003 mPrimitiveCount = count / 3;
1004 break;
1005 case GL_TRIANGLE_STRIP:
1006 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1007 mPrimitiveCount = count - 2;
1008 break;
1009 case GL_TRIANGLE_FAN:
1010 mPrimitiveType = D3DPT_TRIANGLEFAN;
1011 mPrimitiveCount = count - 2;
1012 break;
1013 default:
1014 return error(GL_INVALID_ENUM, false);
1015 }
1016
1017 return mPrimitiveCount > 0;
1018}
1019
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001020bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001021{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001022 // if there is no color attachment we must synthesize a NULL colorattachment
1023 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1024 gl::Renderbuffer *renderbufferObject = NULL;
1025 if (framebuffer->getColorbufferType() != GL_NONE)
1026 {
1027 renderbufferObject = framebuffer->getColorbuffer();
1028 }
1029 else
1030 {
1031 renderbufferObject = framebuffer->getNullColorbuffer();
1032 }
1033 if (!renderbufferObject)
1034 {
1035 ERR("unable to locate renderbuffer for FBO.");
1036 return false;
1037 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001038
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001039 bool renderTargetChanged = false;
1040 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1041 if (renderTargetSerial != mAppliedRenderTargetSerial)
1042 {
1043 // Apply the render target on the device
1044 IDirect3DSurface9 *renderTargetSurface = NULL;
1045
1046 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1047 if (renderTarget)
1048 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001049 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001050 }
1051
1052 if (!renderTargetSurface)
1053 {
1054 ERR("render target pointer unexpectedly null.");
1055 return false; // Context must be lost
1056 }
1057
1058 mDevice->SetRenderTarget(0, renderTargetSurface);
1059 renderTargetSurface->Release();
1060
1061 mAppliedRenderTargetSerial = renderTargetSerial;
1062 renderTargetChanged = true;
1063 }
1064
1065 gl::Renderbuffer *depthStencil = NULL;
1066 unsigned int depthbufferSerial = 0;
1067 unsigned int stencilbufferSerial = 0;
1068 if (framebuffer->getDepthbufferType() != GL_NONE)
1069 {
1070 depthStencil = framebuffer->getDepthbuffer();
1071 if (!depthStencil)
1072 {
1073 ERR("Depth stencil pointer unexpectedly null.");
1074 return false;
1075 }
1076
1077 depthbufferSerial = depthStencil->getSerial();
1078 }
1079 else if (framebuffer->getStencilbufferType() != GL_NONE)
1080 {
1081 depthStencil = framebuffer->getStencilbuffer();
1082 if (!depthStencil)
1083 {
1084 ERR("Depth stencil pointer unexpectedly null.");
1085 return false;
1086 }
1087
1088 stencilbufferSerial = depthStencil->getSerial();
1089 }
1090
1091 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1092 stencilbufferSerial != mAppliedStencilbufferSerial ||
1093 !mDepthStencilInitialized)
1094 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001095 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001096 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001097
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001098 // Apply the depth stencil on the device
1099 if (depthStencil)
1100 {
1101 IDirect3DSurface9 *depthStencilSurface = NULL;
1102 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1103
1104 if (depthStencilRenderTarget)
1105 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001106 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001107 }
1108
1109 if (!depthStencilSurface)
1110 {
1111 ERR("depth stencil pointer unexpectedly null.");
1112 return false; // Context must be lost
1113 }
1114
1115 mDevice->SetDepthStencilSurface(depthStencilSurface);
1116 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001117
1118 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001119 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001120 }
1121 else
1122 {
1123 mDevice->SetDepthStencilSurface(NULL);
1124 }
1125
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001126 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1127 {
1128 mCurDepthSize = depthSize;
1129 mForceSetRasterState = true;
1130 }
1131
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001132 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1133 {
1134 mCurStencilSize = stencilSize;
1135 mForceSetDepthStencilState = true;
1136 }
1137
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001138 mAppliedDepthbufferSerial = depthbufferSerial;
1139 mAppliedStencilbufferSerial = stencilbufferSerial;
1140 mDepthStencilInitialized = true;
1141 }
1142
1143 if (renderTargetChanged || !mRenderTargetDescInitialized)
1144 {
1145 mForceSetScissor = true;
1146 mForceSetViewport = true;
1147
1148 mRenderTargetDesc.width = renderbufferObject->getWidth();
1149 mRenderTargetDesc.height = renderbufferObject->getHeight();
1150 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1151 mRenderTargetDescInitialized = true;
1152 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001153
1154 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001155}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001156
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001157GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001158{
1159 gl::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
1160 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1161 if (err != GL_NO_ERROR)
1162 {
1163 return err;
1164 }
1165
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001166 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1167}
1168
1169// Applies the indices and element array bindings to the Direct3D 9 device
1170GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, gl::TranslatedIndexData *indexInfo)
1171{
1172 IDirect3DIndexBuffer9 *indexBuffer;
1173 unsigned int serial;
1174 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial);
1175
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001176 if (err == GL_NO_ERROR)
1177 {
1178 if (serial != mAppliedIBSerial)
1179 {
1180 mDevice->SetIndices(indexBuffer);
1181 mAppliedIBSerial = serial;
1182 }
1183 }
1184
1185 return err;
1186}
1187
1188void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1189{
1190 startScene();
1191
1192 if (mode == GL_LINE_LOOP)
1193 {
1194 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1195 }
1196 else if (instances > 0)
1197 {
1198 gl::StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
1199 if (countingIB)
1200 {
1201 if (mAppliedIBSerial != countingIB->getSerial())
1202 {
1203 mDevice->SetIndices(countingIB->getBuffer());
1204 mAppliedIBSerial = countingIB->getSerial();
1205 }
1206
1207 for (int i = 0; i < mRepeatDraw; i++)
1208 {
1209 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1210 }
1211 }
1212 else
1213 {
1214 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1215 return error(GL_OUT_OF_MEMORY);
1216 }
1217 }
1218 else // Regular case
1219 {
1220 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1221 }
1222}
1223
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001224void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const gl::TranslatedIndexData &indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001225{
1226 startScene();
1227
1228 if (mode == GL_LINE_LOOP)
1229 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001230 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001231 }
1232 else
1233 {
1234 for (int i = 0; i < mRepeatDraw; i++)
1235 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001236 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1237 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001238 }
1239 }
1240}
1241
1242void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1243{
1244 // Get the raw indices for an indexed draw
1245 if (type != GL_NONE && elementArrayBuffer)
1246 {
1247 gl::Buffer *indexBuffer = elementArrayBuffer;
1248 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1249 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1250 }
1251
1252 UINT startIndex = 0;
1253 bool succeeded = false;
1254
1255 if (get32BitIndexSupport())
1256 {
1257 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1258
1259 if (!mLineLoopIB)
1260 {
1261 mLineLoopIB = new gl::StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
1262 }
1263
1264 if (mLineLoopIB)
1265 {
1266 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
1267
1268 UINT offset = 0;
1269 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
1270 startIndex = offset / 4;
1271
1272 if (data)
1273 {
1274 switch (type)
1275 {
1276 case GL_NONE: // Non-indexed draw
1277 for (int i = 0; i < count; i++)
1278 {
1279 data[i] = i;
1280 }
1281 data[count] = 0;
1282 break;
1283 case GL_UNSIGNED_BYTE:
1284 for (int i = 0; i < count; i++)
1285 {
1286 data[i] = static_cast<const GLubyte*>(indices)[i];
1287 }
1288 data[count] = static_cast<const GLubyte*>(indices)[0];
1289 break;
1290 case GL_UNSIGNED_SHORT:
1291 for (int i = 0; i < count; i++)
1292 {
1293 data[i] = static_cast<const GLushort*>(indices)[i];
1294 }
1295 data[count] = static_cast<const GLushort*>(indices)[0];
1296 break;
1297 case GL_UNSIGNED_INT:
1298 for (int i = 0; i < count; i++)
1299 {
1300 data[i] = static_cast<const GLuint*>(indices)[i];
1301 }
1302 data[count] = static_cast<const GLuint*>(indices)[0];
1303 break;
1304 default: UNREACHABLE();
1305 }
1306
1307 mLineLoopIB->unmap();
1308 succeeded = true;
1309 }
1310 }
1311 }
1312 else
1313 {
1314 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
1315
1316 if (!mLineLoopIB)
1317 {
1318 mLineLoopIB = new gl::StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
1319 }
1320
1321 if (mLineLoopIB)
1322 {
1323 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
1324
1325 UINT offset = 0;
1326 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
1327 startIndex = offset / 2;
1328
1329 if (data)
1330 {
1331 switch (type)
1332 {
1333 case GL_NONE: // Non-indexed draw
1334 for (int i = 0; i < count; i++)
1335 {
1336 data[i] = i;
1337 }
1338 data[count] = 0;
1339 break;
1340 case GL_UNSIGNED_BYTE:
1341 for (int i = 0; i < count; i++)
1342 {
1343 data[i] = static_cast<const GLubyte*>(indices)[i];
1344 }
1345 data[count] = static_cast<const GLubyte*>(indices)[0];
1346 break;
1347 case GL_UNSIGNED_SHORT:
1348 for (int i = 0; i < count; i++)
1349 {
1350 data[i] = static_cast<const GLushort*>(indices)[i];
1351 }
1352 data[count] = static_cast<const GLushort*>(indices)[0];
1353 break;
1354 case GL_UNSIGNED_INT:
1355 for (int i = 0; i < count; i++)
1356 {
1357 data[i] = static_cast<const GLuint*>(indices)[i];
1358 }
1359 data[count] = static_cast<const GLuint*>(indices)[0];
1360 break;
1361 default: UNREACHABLE();
1362 }
1363
1364 mLineLoopIB->unmap();
1365 succeeded = true;
1366 }
1367 }
1368 }
1369
1370 if (succeeded)
1371 {
1372 if (mAppliedIBSerial != mLineLoopIB->getSerial())
1373 {
1374 mDevice->SetIndices(mLineLoopIB->getBuffer());
1375 mAppliedIBSerial = mLineLoopIB->getSerial();
1376 }
1377
1378 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1379 }
1380 else
1381 {
1382 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
1383 return error(GL_OUT_OF_MEMORY);
1384 }
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001385}
1386
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001387void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1388{
daniel@transgaming.com95892412012-11-28 20:59:09 +00001389 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1390 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1391
1392 IDirect3DVertexShader9 *vertexShader = NULL;
1393 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
1394
1395 IDirect3DPixelShader9 *pixelShader = NULL;
1396 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001397
1398 mDevice->SetPixelShader(pixelShader);
1399 mDevice->SetVertexShader(vertexShader);
1400 programBinary->dirtyAllUniforms();
1401}
1402
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001403void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001404{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001405 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1406 gl::unorm<8>(clearParams.colorClearValue.red),
1407 gl::unorm<8>(clearParams.colorClearValue.green),
1408 gl::unorm<8>(clearParams.colorClearValue.blue));
1409 float depth = gl::clamp01(clearParams.depthClearValue);
1410 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001411
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001412 unsigned int stencilUnmasked = 0x0;
1413 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1414 {
1415 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1416 stencilUnmasked = (0x1 << stencilSize) - 1;
1417 }
1418
1419 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1420
1421 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1422 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1423 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1424 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1425 clearParams.colorMaskBlue && alphaUnmasked);
1426
1427 if (needMaskedColorClear || needMaskedStencilClear)
1428 {
1429 // State which is altered in all paths from this point to the clear call is saved.
1430 // State which is altered in only some paths will be flagged dirty in the case that
1431 // that path is taken.
1432 HRESULT hr;
1433 if (mMaskedClearSavedState == NULL)
1434 {
1435 hr = mDevice->BeginStateBlock();
1436 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1437
1438 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1439 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1440 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1441 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1442 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1443 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1444 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1445 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1446 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1447 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1448 mDevice->SetPixelShader(NULL);
1449 mDevice->SetVertexShader(NULL);
1450 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1451 mDevice->SetStreamSource(0, NULL, 0, 0);
1452 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1453 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1454 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1455 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1456 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1457 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1458 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1459
1460 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1461 {
1462 mDevice->SetStreamSourceFreq(i, 1);
1463 }
1464
1465 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1466 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1467 }
1468
1469 ASSERT(mMaskedClearSavedState != NULL);
1470
1471 if (mMaskedClearSavedState != NULL)
1472 {
1473 hr = mMaskedClearSavedState->Capture();
1474 ASSERT(SUCCEEDED(hr));
1475 }
1476
1477 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1478 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1479 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1480 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1481 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1482 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1483 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1484 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1485
1486 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1487 {
1488 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1489 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1490 clearParams.colorMaskGreen,
1491 clearParams.colorMaskBlue,
1492 clearParams.colorMaskAlpha));
1493 }
1494 else
1495 {
1496 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1497 }
1498
1499 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1500 {
1501 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1502 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1503 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1504 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1505 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1506 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1507 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1508 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1509 }
1510 else
1511 {
1512 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1513 }
1514
1515 mDevice->SetPixelShader(NULL);
1516 mDevice->SetVertexShader(NULL);
1517 mDevice->SetFVF(D3DFVF_XYZRHW);
1518 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1519 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1520 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1521 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1522 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1523 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1524 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1525
1526 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1527 {
1528 mDevice->SetStreamSourceFreq(i, 1);
1529 }
1530
1531 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1532 quad[0][0] = -0.5f;
1533 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1534 quad[0][2] = 0.0f;
1535 quad[0][3] = 1.0f;
1536
1537 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1538 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1539 quad[1][2] = 0.0f;
1540 quad[1][3] = 1.0f;
1541
1542 quad[2][0] = -0.5f;
1543 quad[2][1] = -0.5f;
1544 quad[2][2] = 0.0f;
1545 quad[2][3] = 1.0f;
1546
1547 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1548 quad[3][1] = -0.5f;
1549 quad[3][2] = 0.0f;
1550 quad[3][3] = 1.0f;
1551
1552 startScene();
1553 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1554
1555 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1556 {
1557 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1558 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1559 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1560 }
1561
1562 if (mMaskedClearSavedState != NULL)
1563 {
1564 mMaskedClearSavedState->Apply();
1565 }
1566 }
1567 else if (clearParams.mask)
1568 {
1569 DWORD dxClearFlags = 0;
1570 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1571 {
1572 dxClearFlags |= D3DCLEAR_TARGET;
1573 }
1574 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1575 {
1576 dxClearFlags |= D3DCLEAR_ZBUFFER;
1577 }
1578 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1579 {
1580 dxClearFlags |= D3DCLEAR_STENCIL;
1581 }
1582
1583 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1584 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001585}
1586
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001587void Renderer9::markAllStateDirty()
1588{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001589 mAppliedRenderTargetSerial = 0;
1590 mAppliedDepthbufferSerial = 0;
1591 mAppliedStencilbufferSerial = 0;
1592 mDepthStencilInitialized = false;
1593 mRenderTargetDescInitialized = false;
1594
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001595 mForceSetDepthStencilState = true;
1596 mForceSetRasterState = true;
1597 mForceSetBlendState = true;
1598 mForceSetScissor = true;
1599 mForceSetViewport = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001600
1601 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001602}
1603
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001604void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001605{
1606 while (!mEventQueryPool.empty())
1607 {
1608 mEventQueryPool.back()->Release();
1609 mEventQueryPool.pop_back();
1610 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001611
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001612 if (mMaskedClearSavedState)
1613 {
1614 mMaskedClearSavedState->Release();
1615 mMaskedClearSavedState = NULL;
1616 }
1617
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001618 mVertexShaderCache.clear();
1619 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001620
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001621 delete mBlit;
1622 mBlit = NULL;
1623
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001624 delete mVertexDataManager;
1625 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001626
1627 delete mIndexDataManager;
1628 mIndexDataManager = NULL;
1629
1630 delete mLineLoopIB;
1631 mLineLoopIB = NULL;
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001632}
1633
1634
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001635void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001636{
1637 mDeviceLost = true;
1638}
1639
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001640bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001641{
1642 return mDeviceLost;
1643}
1644
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001645// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001646bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001647{
1648 bool isLost = false;
1649
1650 if (mDeviceEx)
1651 {
1652 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1653 }
1654 else if (mDevice)
1655 {
1656 isLost = FAILED(mDevice->TestCooperativeLevel());
1657 }
1658 else
1659 {
1660 // No device yet, so no reset required
1661 }
1662
1663 if (isLost)
1664 {
1665 // ensure we note the device loss --
1666 // we'll probably get this done again by markDeviceLost
1667 // but best to remember it!
1668 // Note that we don't want to clear the device loss status here
1669 // -- this needs to be done by resetDevice
1670 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001671 if (notify)
1672 {
1673 mDisplay->notifyDeviceLost();
1674 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001675 }
1676
1677 return isLost;
1678}
1679
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001680bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001681{
1682 HRESULT status = D3D_OK;
1683
1684 if (mDeviceEx)
1685 {
1686 status = mDeviceEx->CheckDeviceState(NULL);
1687 }
1688 else if (mDevice)
1689 {
1690 status = mDevice->TestCooperativeLevel();
1691 }
1692
1693 switch (status)
1694 {
1695 case D3DERR_DEVICENOTRESET:
1696 case D3DERR_DEVICEHUNG:
1697 return true;
1698 default:
1699 return false;
1700 }
1701}
1702
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001703bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001704{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001705 releaseDeviceResources();
1706
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001707 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1708
1709 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001710 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001711 int attempts = 3;
1712
1713 while (lost && attempts > 0)
1714 {
1715 if (mDeviceEx)
1716 {
1717 Sleep(500); // Give the graphics driver some CPU time
1718 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1719 }
1720 else
1721 {
1722 result = mDevice->TestCooperativeLevel();
1723 while (result == D3DERR_DEVICELOST)
1724 {
1725 Sleep(100); // Give the graphics driver some CPU time
1726 result = mDevice->TestCooperativeLevel();
1727 }
1728
1729 if (result == D3DERR_DEVICENOTRESET)
1730 {
1731 result = mDevice->Reset(&presentParameters);
1732 }
1733 }
1734
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001735 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001736 attempts --;
1737 }
1738
1739 if (FAILED(result))
1740 {
1741 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1742 return false;
1743 }
1744
1745 // reset device defaults
1746 initializeDevice();
1747 mDeviceLost = false;
1748
1749 return true;
1750}
1751
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001752DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001753{
1754 return mAdapterIdentifier.VendorId;
1755}
1756
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001757const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001758{
1759 return mAdapterIdentifier.Description;
1760}
1761
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001762GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001763{
1764 return mAdapterIdentifier.DeviceIdentifier;
1765}
1766
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001767void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001768{
1769 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1770 {
1771 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1772 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1773
1774 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1775 }
1776}
1777
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001778bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001779{
1780 D3DDISPLAYMODE currentDisplayMode;
1781 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1782
1783 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1784}
1785
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001786bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001787{
1788 D3DDISPLAYMODE currentDisplayMode;
1789 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1790
1791 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1792}
1793
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001794bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001795{
1796 D3DDISPLAYMODE currentDisplayMode;
1797 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1798
1799 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1800}
1801
1802// we use INTZ for depth textures in Direct3D9
1803// we also want NULL texture support to ensure the we can make depth-only FBOs
1804// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001805bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001806{
1807 D3DDISPLAYMODE currentDisplayMode;
1808 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1809
1810 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1811 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1812 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1813 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1814
1815 return intz && null;
1816}
1817
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001818bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001819{
1820 D3DDISPLAYMODE currentDisplayMode;
1821 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1822
1823 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1824 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1825 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1826 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1827
1828 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1829 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1830 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1831 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1832
1833 if (!*filtering && !*renderable)
1834 {
1835 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1836 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1837 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1838 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1839 }
1840 else
1841 {
1842 return true;
1843 }
1844}
1845
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001846bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001847{
1848 D3DDISPLAYMODE currentDisplayMode;
1849 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1850
1851 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1852 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1853 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1854 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1855
1856 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1857 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1858 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1859 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1860
1861 if (!*filtering && !*renderable)
1862 {
1863 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1864 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1865 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1866 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1867 }
1868 else
1869 {
1870 return true;
1871 }
1872}
1873
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001874bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001875{
1876 D3DDISPLAYMODE currentDisplayMode;
1877 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1878
1879 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1880}
1881
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001882bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001883{
1884 D3DDISPLAYMODE currentDisplayMode;
1885 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1886
1887 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1888}
1889
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001890bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001891{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001892 return mSupportsTextureFilterAnisotropy;
1893}
1894
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001895float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001896{
1897 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001898 {
1899 return mDeviceCaps.MaxAnisotropy;
1900 }
1901 return 1.0f;
1902}
1903
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001904bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001905{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001906 IDirect3DQuery9 *query = allocateEventQuery();
1907 if (query)
1908 {
1909 freeEventQuery(query);
1910 return true;
1911 }
1912 else
1913 {
1914 return false;
1915 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001916 return true;
1917}
1918
1919// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1920// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001921bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001922{
1923 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1924 {
1925 return false;
1926 }
1927
1928 D3DDISPLAYMODE currentDisplayMode;
1929 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1930
1931 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1932
1933 return SUCCEEDED(result);
1934}
1935
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001936bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001937{
1938 return mSupportsNonPower2Textures;
1939}
1940
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001941bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001942{
1943 if (!mDevice)
1944 {
1945 return false;
1946 }
1947
1948 IDirect3DQuery9 *query = NULL;
1949 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1950 if (SUCCEEDED(result) && query)
1951 {
1952 query->Release();
1953 return true;
1954 }
1955 else
1956 {
1957 return false;
1958 }
1959}
1960
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001961bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001962{
1963 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1964}
1965
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001966bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001967{
1968 // PIX doesn't seem to support using share handles, so disable them.
1969 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001970 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001971}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001972
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001973int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001974{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001975 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001976}
1977
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001978float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001979{
1980 return mDeviceCaps.MaxPointSize;
1981}
1982
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001983int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001984{
1985 return (int)mDeviceCaps.MaxTextureWidth;
1986}
1987
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001988int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001989{
1990 return (int)mDeviceCaps.MaxTextureHeight;
1991}
1992
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001993bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001994{
1995 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1996}
1997
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001998DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001999{
2000 return mDeviceCaps.DeclTypes;
2001}
2002
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002003int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002004{
2005 return mMinSwapInterval;
2006}
2007
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002008int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002009{
2010 return mMaxSwapInterval;
2011}
2012
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002013int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002014{
2015 return mMaxSupportedSamples;
2016}
2017
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002018int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002019{
2020 if (requested == 0)
2021 {
2022 return requested;
2023 }
2024
2025 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2026 if (itr == mMultiSampleSupport.end())
2027 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002028 if (format == D3DFMT_UNKNOWN)
2029 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002030 return -1;
2031 }
2032
2033 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2034 {
2035 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2036 {
2037 return i;
2038 }
2039 }
2040
2041 return -1;
2042}
2043
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002044D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2045{
2046 switch (internalformat)
2047 {
2048 case GL_DEPTH_COMPONENT16:
2049 case GL_DEPTH_COMPONENT32_OES:
2050 case GL_DEPTH24_STENCIL8_OES:
2051 return D3DFMT_INTZ;
2052 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2053 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2054 return D3DFMT_DXT1;
2055 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2056 return D3DFMT_DXT3;
2057 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2058 return D3DFMT_DXT5;
2059 case GL_RGBA32F_EXT:
2060 case GL_RGB32F_EXT:
2061 case GL_ALPHA32F_EXT:
2062 case GL_LUMINANCE32F_EXT:
2063 case GL_LUMINANCE_ALPHA32F_EXT:
2064 return D3DFMT_A32B32G32R32F;
2065 case GL_RGBA16F_EXT:
2066 case GL_RGB16F_EXT:
2067 case GL_ALPHA16F_EXT:
2068 case GL_LUMINANCE16F_EXT:
2069 case GL_LUMINANCE_ALPHA16F_EXT:
2070 return D3DFMT_A16B16G16R16F;
2071 case GL_LUMINANCE8_EXT:
2072 if (getLuminanceTextureSupport())
2073 {
2074 return D3DFMT_L8;
2075 }
2076 break;
2077 case GL_LUMINANCE8_ALPHA8_EXT:
2078 if (getLuminanceAlphaTextureSupport())
2079 {
2080 return D3DFMT_A8L8;
2081 }
2082 break;
2083 case GL_RGB8_OES:
2084 case GL_RGB565:
2085 return D3DFMT_X8R8G8B8;
2086 }
2087
2088 return D3DFMT_A8R8G8B8;
2089}
2090
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002091bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002092{
2093 bool result = false;
2094
2095 if (source && dest)
2096 {
2097 int levels = source->levelCount();
2098 for (int i = 0; i < levels; ++i)
2099 {
2100 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
2101 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
2102
2103 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2104
2105 if (srcSurf) srcSurf->Release();
2106 if (dstSurf) dstSurf->Release();
2107
2108 if (!result)
2109 return false;
2110 }
2111 }
2112
2113 return result;
2114}
2115
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002116bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002117{
2118 bool result = false;
2119
2120 if (source && dest)
2121 {
2122 int levels = source->levelCount();
2123 for (int f = 0; f < 6; f++)
2124 {
2125 for (int i = 0; i < levels; i++)
2126 {
2127 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2128 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
2129
2130 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2131
2132 if (srcSurf) srcSurf->Release();
2133 if (dstSurf) dstSurf->Release();
2134
2135 if (!result)
2136 return false;
2137 }
2138 }
2139 }
2140
2141 return result;
2142}
2143
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002144D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002145{
2146 if (mD3d9Ex != NULL)
2147 {
2148 return D3DPOOL_DEFAULT;
2149 }
2150 else
2151 {
2152 if (!(usage & D3DUSAGE_DYNAMIC))
2153 {
2154 return D3DPOOL_MANAGED;
2155 }
2156 }
2157
2158 return D3DPOOL_DEFAULT;
2159}
2160
daniel@transgaming.com38380882012-11-28 19:36:39 +00002161bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2162 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002163{
2164 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
2165}
2166
daniel@transgaming.com38380882012-11-28 19:36:39 +00002167bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2168 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002169{
2170 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
2171}
2172
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002173bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2174 bool blitRenderTarget, bool blitDepthStencil)
2175{
2176 endScene();
2177
2178 if (blitRenderTarget)
2179 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002180 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2181 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2182 RenderTarget9 *readRenderTarget = NULL;
2183 RenderTarget9 *drawRenderTarget = NULL;
2184 IDirect3DSurface9* readSurface = NULL;
2185 IDirect3DSurface9* drawSurface = NULL;
2186
2187 if (readBuffer)
2188 {
2189 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2190 }
2191 if (drawBuffer)
2192 {
2193 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2194 }
2195
2196 if (readRenderTarget)
2197 {
2198 readSurface = readRenderTarget->getSurface();
2199 }
2200 if (drawRenderTarget)
2201 {
2202 drawSurface = drawRenderTarget->getSurface();
2203 }
2204
2205 if (!readSurface || !drawSurface)
2206 {
2207 ERR("Failed to retrieve the render target.");
2208 return error(GL_OUT_OF_MEMORY, false);
2209 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002210
2211 RECT srcRect, dstRect;
2212 RECT *srcRectPtr = NULL;
2213 RECT *dstRectPtr = NULL;
2214
2215 if (readRect)
2216 {
2217 srcRect.left = readRect->x;
2218 srcRect.right = readRect->x + readRect->width;
2219 srcRect.top = readRect->y;
2220 srcRect.bottom = readRect->y + readRect->height;
2221 srcRectPtr = &srcRect;
2222 }
2223
2224 if (drawRect)
2225 {
2226 dstRect.left = drawRect->x;
2227 dstRect.right = drawRect->x + drawRect->width;
2228 dstRect.top = drawRect->y;
2229 dstRect.bottom = drawRect->y + drawRect->height;
2230 dstRectPtr = &dstRect;
2231 }
2232
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002233 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002234
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002235 readSurface->Release();
2236 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002237
2238 if (FAILED(result))
2239 {
2240 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2241 return false;
2242 }
2243 }
2244
2245 if (blitDepthStencil)
2246 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002247 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2248 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2249 RenderTarget9 *readDepthStencil = NULL;
2250 RenderTarget9 *drawDepthStencil = NULL;
2251 IDirect3DSurface9* readSurface = NULL;
2252 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002253
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002254 if (readBuffer)
2255 {
2256 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2257 }
2258 if (drawBuffer)
2259 {
2260 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2261 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002262
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002263 if (readDepthStencil)
2264 {
2265 readSurface = readDepthStencil->getSurface();
2266 }
2267 if (drawDepthStencil)
2268 {
2269 drawSurface = drawDepthStencil->getSurface();
2270 }
2271
2272 if (!readSurface || !drawSurface)
2273 {
2274 ERR("Failed to retrieve the render target.");
2275 return error(GL_OUT_OF_MEMORY, false);
2276 }
2277
2278 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2279
2280 readSurface->Release();
2281 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002282
2283 if (FAILED(result))
2284 {
2285 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2286 return false;
2287 }
2288 }
2289
2290 return true;
2291}
2292
2293void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2294 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2295{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002296 RenderTarget9 *renderTarget = NULL;
2297 IDirect3DSurface9 *surface = NULL;
2298 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2299
2300 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002301 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002302 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2303 }
2304
2305 if (renderTarget)
2306 {
2307 surface = renderTarget->getSurface();
2308 }
2309
2310 if (!surface)
2311 {
2312 // context must be lost
2313 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002314 }
2315
2316 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002317 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002318
2319 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2320 {
2321 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002322 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002323 return error(GL_OUT_OF_MEMORY);
2324 }
2325
2326 HRESULT result;
2327 IDirect3DSurface9 *systemSurface = NULL;
2328 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2329 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2330 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2331 if (directToPixels)
2332 {
2333 // Use the pixels ptr as a shared handle to write directly into client's memory
2334 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2335 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2336 if (FAILED(result))
2337 {
2338 // Try again without the shared handle
2339 directToPixels = false;
2340 }
2341 }
2342
2343 if (!directToPixels)
2344 {
2345 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2346 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2347 if (FAILED(result))
2348 {
2349 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002350 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002351 return error(GL_OUT_OF_MEMORY);
2352 }
2353 }
2354
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002355 result = mDevice->GetRenderTargetData(surface, systemSurface);
2356 surface->Release();
2357 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002358
2359 if (FAILED(result))
2360 {
2361 systemSurface->Release();
2362
2363 // It turns out that D3D will sometimes produce more error
2364 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002365 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002366 return error(GL_OUT_OF_MEMORY);
2367 else
2368 {
2369 UNREACHABLE();
2370 return;
2371 }
2372
2373 }
2374
2375 if (directToPixels)
2376 {
2377 systemSurface->Release();
2378 return;
2379 }
2380
2381 RECT rect;
2382 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2383 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2384 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2385 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2386
2387 D3DLOCKED_RECT lock;
2388 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2389
2390 if (FAILED(result))
2391 {
2392 UNREACHABLE();
2393 systemSurface->Release();
2394
2395 return; // No sensible error to generate
2396 }
2397
2398 unsigned char *dest = (unsigned char*)pixels;
2399 unsigned short *dest16 = (unsigned short*)pixels;
2400
2401 unsigned char *source;
2402 int inputPitch;
2403 if (packReverseRowOrder)
2404 {
2405 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2406 inputPitch = -lock.Pitch;
2407 }
2408 else
2409 {
2410 source = (unsigned char*)lock.pBits;
2411 inputPitch = lock.Pitch;
2412 }
2413
2414 unsigned int fastPixelSize = 0;
2415
2416 if (desc.Format == D3DFMT_A8R8G8B8 &&
2417 format == GL_BGRA_EXT &&
2418 type == GL_UNSIGNED_BYTE)
2419 {
2420 fastPixelSize = 4;
2421 }
2422 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2423 format == GL_BGRA_EXT &&
2424 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2425 (desc.Format == D3DFMT_A1R5G5B5 &&
2426 format == GL_BGRA_EXT &&
2427 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2428 {
2429 fastPixelSize = 2;
2430 }
2431 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2432 format == GL_RGBA &&
2433 type == GL_HALF_FLOAT_OES)
2434 {
2435 fastPixelSize = 8;
2436 }
2437 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2438 format == GL_RGBA &&
2439 type == GL_FLOAT)
2440 {
2441 fastPixelSize = 16;
2442 }
2443
2444 for (int j = 0; j < rect.bottom - rect.top; j++)
2445 {
2446 if (fastPixelSize != 0)
2447 {
2448 // Fast path for formats which require no translation:
2449 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2450 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2451 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2452 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2453 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2454 //
2455 // Note that buffers with no alpha go through the slow path below.
2456 memcpy(dest + j * outputPitch,
2457 source + j * inputPitch,
2458 (rect.right - rect.left) * fastPixelSize);
2459 continue;
2460 }
2461
2462 for (int i = 0; i < rect.right - rect.left; i++)
2463 {
2464 float r;
2465 float g;
2466 float b;
2467 float a;
2468
2469 switch (desc.Format)
2470 {
2471 case D3DFMT_R5G6B5:
2472 {
2473 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2474
2475 a = 1.0f;
2476 b = (rgb & 0x001F) * (1.0f / 0x001F);
2477 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2478 r = (rgb & 0xF800) * (1.0f / 0xF800);
2479 }
2480 break;
2481 case D3DFMT_A1R5G5B5:
2482 {
2483 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2484
2485 a = (argb & 0x8000) ? 1.0f : 0.0f;
2486 b = (argb & 0x001F) * (1.0f / 0x001F);
2487 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2488 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2489 }
2490 break;
2491 case D3DFMT_A8R8G8B8:
2492 {
2493 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2494
2495 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2496 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2497 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2498 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2499 }
2500 break;
2501 case D3DFMT_X8R8G8B8:
2502 {
2503 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2504
2505 a = 1.0f;
2506 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2507 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2508 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2509 }
2510 break;
2511 case D3DFMT_A2R10G10B10:
2512 {
2513 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2514
2515 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2516 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2517 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2518 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2519 }
2520 break;
2521 case D3DFMT_A32B32G32R32F:
2522 {
2523 // float formats in D3D are stored rgba, rather than the other way round
2524 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2525 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2526 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2527 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2528 }
2529 break;
2530 case D3DFMT_A16B16G16R16F:
2531 {
2532 // float formats in D3D are stored rgba, rather than the other way round
2533 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2534 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2535 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2536 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2537 }
2538 break;
2539 default:
2540 UNIMPLEMENTED(); // FIXME
2541 UNREACHABLE();
2542 return;
2543 }
2544
2545 switch (format)
2546 {
2547 case GL_RGBA:
2548 switch (type)
2549 {
2550 case GL_UNSIGNED_BYTE:
2551 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2552 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2553 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2554 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2555 break;
2556 default: UNREACHABLE();
2557 }
2558 break;
2559 case GL_BGRA_EXT:
2560 switch (type)
2561 {
2562 case GL_UNSIGNED_BYTE:
2563 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2564 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2565 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2566 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2567 break;
2568 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2569 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2570 // this type is packed as follows:
2571 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2572 // --------------------------------------------------------------------------------
2573 // | 4th | 3rd | 2nd | 1st component |
2574 // --------------------------------------------------------------------------------
2575 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2576 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2577 ((unsigned short)(15 * a + 0.5f) << 12)|
2578 ((unsigned short)(15 * r + 0.5f) << 8) |
2579 ((unsigned short)(15 * g + 0.5f) << 4) |
2580 ((unsigned short)(15 * b + 0.5f) << 0);
2581 break;
2582 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2583 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2584 // this type is packed as follows:
2585 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2586 // --------------------------------------------------------------------------------
2587 // | 4th | 3rd | 2nd | 1st component |
2588 // --------------------------------------------------------------------------------
2589 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2590 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2591 ((unsigned short)( a + 0.5f) << 15) |
2592 ((unsigned short)(31 * r + 0.5f) << 10) |
2593 ((unsigned short)(31 * g + 0.5f) << 5) |
2594 ((unsigned short)(31 * b + 0.5f) << 0);
2595 break;
2596 default: UNREACHABLE();
2597 }
2598 break;
2599 case GL_RGB:
2600 switch (type)
2601 {
2602 case GL_UNSIGNED_SHORT_5_6_5:
2603 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2604 ((unsigned short)(31 * b + 0.5f) << 0) |
2605 ((unsigned short)(63 * g + 0.5f) << 5) |
2606 ((unsigned short)(31 * r + 0.5f) << 11);
2607 break;
2608 case GL_UNSIGNED_BYTE:
2609 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2610 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2611 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2612 break;
2613 default: UNREACHABLE();
2614 }
2615 break;
2616 default: UNREACHABLE();
2617 }
2618 }
2619 }
2620
2621 systemSurface->UnlockRect();
2622
2623 systemSurface->Release();
2624}
2625
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002626RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2627{
2628 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2629 IDirect3DSurface9 *surface = NULL;
2630 if (depth)
2631 {
2632 surface = swapChain9->getDepthStencil();
2633 }
2634 else
2635 {
2636 surface = swapChain9->getRenderTarget();
2637 }
2638
2639 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2640
2641 return renderTarget;
2642}
2643
2644RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2645{
2646 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2647 return renderTarget;
2648}
2649
daniel@transgaming.com55318902012-11-28 20:58:58 +00002650ShaderExecutable *Renderer9::loadExecutable(const DWORD *function, size_t length, GLenum type, void *data)
2651{
2652 ShaderExecutable9 *executable = NULL;
2653 gl::D3DConstantTable *table = reinterpret_cast<gl::D3DConstantTable *>(data);
2654
2655 switch (type)
2656 {
2657 case GL_VERTEX_SHADER:
2658 {
2659 IDirect3DVertexShader9 *vshader = createVertexShader(function, length);
2660 if (vshader)
2661 {
2662 executable = new ShaderExecutable9(vshader, table);
2663 }
2664 }
2665 break;
2666 case GL_FRAGMENT_SHADER:
2667 {
2668 IDirect3DPixelShader9 *pshader = createPixelShader(function, length);
2669 if (pshader)
2670 {
2671 executable = new ShaderExecutable9(pshader, table);
2672 }
2673 }
2674 break;
2675 default:
2676 UNREACHABLE();
2677 break;
2678 }
2679
2680 return executable;
2681}
2682
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002683ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2684{
2685 const char *profile = NULL;
2686
2687 switch (type)
2688 {
2689 case GL_VERTEX_SHADER:
2690 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
2691 break;
2692 case GL_FRAGMENT_SHADER:
2693 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
2694 break;
2695 default:
2696 UNREACHABLE();
2697 return NULL;
2698 }
2699
2700 gl::D3DConstantTable *constantTable = NULL;
2701 ID3D10Blob *binary = compileToBinary(infoLog, shaderHLSL, profile, &constantTable);
2702 if (!binary)
2703 return NULL;
2704
daniel@transgaming.com55318902012-11-28 20:58:58 +00002705 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type, constantTable);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00002706 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002707
2708 return executable;
2709}
2710
2711// Compiles the HLSL code of the attached shaders into executable binaries
2712ID3D10Blob *Renderer9::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, gl::D3DConstantTable **constantTable)
2713{
2714 if (!hlsl)
2715 {
2716 return NULL;
2717 }
2718
2719 HRESULT result = S_OK;
2720 UINT flags = 0;
2721 std::string sourceText;
2722 if (gl::perfActive())
2723 {
2724 flags |= D3DCOMPILE_DEBUG;
2725#ifdef NDEBUG
2726 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
2727#else
2728 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
2729#endif
2730
2731 std::string sourcePath = getTempPath();
2732 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
2733 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2734 }
2735 else
2736 {
2737 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
2738 sourceText = hlsl;
2739 }
2740
2741 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2742 // Try the default flags first and if compilation fails, try some alternatives.
2743 const static UINT extraFlags[] =
2744 {
2745 0,
2746 D3DCOMPILE_AVOID_FLOW_CONTROL,
2747 D3DCOMPILE_PREFER_FLOW_CONTROL
2748 };
2749
2750 const static char * const extraFlagNames[] =
2751 {
2752 "default",
2753 "avoid flow control",
2754 "prefer flow control"
2755 };
2756
2757 for (int i = 0; i < sizeof(extraFlags) / sizeof(UINT); ++i)
2758 {
2759 ID3D10Blob *errorMessage = NULL;
2760 ID3D10Blob *binary = NULL;
daniel@transgaming.comf0516cf2012-11-28 20:59:18 +00002761 result = mD3DCompileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL,
2762 "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002763 if (errorMessage)
2764 {
2765 const char *message = (const char*)errorMessage->GetBufferPointer();
2766
2767 infoLog.appendSanitized(message);
2768 TRACE("\n%s", hlsl);
2769 TRACE("\n%s", message);
2770
2771 errorMessage->Release();
2772 errorMessage = NULL;
2773 }
2774
2775 if (SUCCEEDED(result))
2776 {
2777 gl::D3DConstantTable *table = new gl::D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
2778 if (table->error())
2779 {
2780 delete table;
2781 binary->Release();
2782 return NULL;
2783 }
2784
2785 *constantTable = table;
2786 return binary;
2787 }
2788 else
2789 {
2790 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2791 {
2792 return error(GL_OUT_OF_MEMORY, (ID3D10Blob*) NULL);
2793 }
2794
2795 infoLog.append("Warning: D3D shader compilation failed with ");
2796 infoLog.append(extraFlagNames[i]);
2797 infoLog.append(" flags.");
2798 if (i + 1 < sizeof(extraFlagNames) / sizeof(char*))
2799 {
2800 infoLog.append(" Retrying with ");
2801 infoLog.append(extraFlagNames[i + 1]);
2802 infoLog.append(".\n");
2803 }
2804 }
2805 }
2806
2807 return NULL;
2808}
2809
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002810bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2811{
2812 return mBlit->boxFilter(source, dest);
2813}
2814
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002815D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002816{
2817 if (mD3d9Ex != NULL)
2818 {
2819 return D3DPOOL_DEFAULT;
2820 }
2821 else
2822 {
2823 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2824 {
2825 return D3DPOOL_MANAGED;
2826 }
2827 }
2828
2829 return D3DPOOL_DEFAULT;
2830}
2831
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002832bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2833{
2834 if (source && dest)
2835 {
2836 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2837 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2838
2839 if (fromManaged)
2840 {
2841 D3DSURFACE_DESC desc;
2842 source->GetDesc(&desc);
2843
2844 IDirect3DSurface9 *surf = 0;
2845 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2846
2847 if (SUCCEEDED(result))
2848 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002849 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002850 result = device->UpdateSurface(surf, NULL, dest, NULL);
2851 surf->Release();
2852 }
2853 }
2854 else
2855 {
2856 endScene();
2857 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2858 }
2859
2860 if (FAILED(result))
2861 {
2862 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2863 return false;
2864 }
2865 }
2866
2867 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002868}
2869
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002870}