blob: 908c3e8c0dab31d916a5f52fa1e66dbf6ffa2a18 [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00008
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009#include "common/debug.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000010#include "libGLESv2/main.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/mathutil.h"
13#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000014#include "libGLESv2/Program.h"
15#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000016#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000017#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000018#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000019#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +000020#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000021#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000022#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000023
daniel@transgaming.com3281f972012-10-31 18:38:51 +000024#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000025#include "libEGL/Display.h"
26
daniel@transgaming.com621ce052012-10-31 17:52:29 +000027// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
28#define REF_RAST 0
29
30// The "Debug This Pixel..." feature in PIX often fails when using the
31// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
32// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
33#if !defined(ANGLE_ENABLE_D3D9EX)
34// Enables use of the IDirect3D9Ex interface, when available
35#define ANGLE_ENABLE_D3D9EX 1
36#endif // !defined(ANGLE_ENABLE_D3D9EX)
37
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000038namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000039{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000040static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000041 {
42 D3DFMT_A1R5G5B5,
43 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
44 D3DFMT_A8R8G8B8,
45 D3DFMT_R5G6B5,
46 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
47 D3DFMT_X8R8G8B8
48 };
49
daniel@transgaming.com222ee082012-11-28 19:31:49 +000050static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000051 {
52 D3DFMT_UNKNOWN,
53 // D3DFMT_D16_LOCKABLE,
54 D3DFMT_D32,
55 // D3DFMT_D15S1,
56 D3DFMT_D24S8,
57 D3DFMT_D24X8,
58 // D3DFMT_D24X4S4,
59 D3DFMT_D16,
60 // D3DFMT_D32F_LOCKABLE,
61 // D3DFMT_D24FS8
62 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000063
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000064Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000065{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000066 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000067
68 mD3d9 = NULL;
69 mD3d9Ex = NULL;
70 mDevice = NULL;
71 mDeviceEx = NULL;
72 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000073 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000074
75 mAdapter = D3DADAPTER_DEFAULT;
76
77 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
78 mDeviceType = D3DDEVTYPE_REF;
79 #else
80 mDeviceType = D3DDEVTYPE_HAL;
81 #endif
82
83 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000084
85 mMaxSupportedSamples = 0;
daniel@transgaming.com8a8b24c2012-11-28 19:36:26 +000086
87 mForceSetDepthStencilState = true;
88 mForceSetRasterState = true;
89 mForceSetBlendState = true;
90 mForceSetScissor = true;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000091 mForceSetViewport = true;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000092}
93
daniel@transgaming.com2507f412012-10-31 18:46:48 +000094Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +000095{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000096 releaseDeviceResources();
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000097
98 delete mBlit;
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000099
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000100 if (mDevice)
101 {
102 // 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 +0000103 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000104 {
105 resetDevice();
106 }
107
108 mDevice->Release();
109 mDevice = NULL;
110 }
111
112 if (mDeviceEx)
113 {
114 mDeviceEx->Release();
115 mDeviceEx = NULL;
116 }
117
118 if (mD3d9)
119 {
120 mD3d9->Release();
121 mD3d9 = NULL;
122 }
123
124 if (mDeviceWindow)
125 {
126 DestroyWindow(mDeviceWindow);
127 mDeviceWindow = NULL;
128 }
129
130 if (mD3d9Ex)
131 {
132 mD3d9Ex->Release();
133 mD3d9Ex = NULL;
134 }
135
136 if (mD3d9Module)
137 {
138 mD3d9Module = NULL;
139 }
140
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000141 while (!mMultiSampleSupport.empty())
142 {
143 delete [] mMultiSampleSupport.begin()->second;
144 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
145 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000146}
147
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000148EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000149{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000150 if (mSoftwareDevice)
151 {
152 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
153 }
154 else
155 {
156 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
157 }
158
159 if (mD3d9Module == NULL)
160 {
161 ERR("No D3D9 module found - aborting!\n");
162 return EGL_NOT_INITIALIZED;
163 }
164
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000165 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
166 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
167
168 // Use Direct3D9Ex if available. Among other things, this version is less
169 // inclined to report a lost context, for example when the user switches
170 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
171 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
172 {
173 ASSERT(mD3d9Ex);
174 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
175 ASSERT(mD3d9);
176 }
177 else
178 {
179 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
180 }
181
182 if (!mD3d9)
183 {
184 ERR("Could not create D3D9 device - aborting!\n");
185 return EGL_NOT_INITIALIZED;
186 }
187 if (mDc != NULL)
188 {
189 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
190 }
191
192 HRESULT result;
193
194 // Give up on getting device caps after about one second.
195 for (int i = 0; i < 10; ++i)
196 {
197 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
198 if (SUCCEEDED(result))
199 {
200 break;
201 }
202 else if (result == D3DERR_NOTAVAILABLE)
203 {
204 Sleep(100); // Give the driver some time to initialize/recover
205 }
206 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
207 {
208 ERR("failed to get device caps (0x%x)\n", result);
209 return EGL_NOT_INITIALIZED;
210 }
211 }
212
213 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
214 {
215 ERR("Renderer does not support PS 2.0. aborting!\n");
216 return EGL_NOT_INITIALIZED;
217 }
218
219 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
220 // This is required by Texture2D::convertToRenderTarget.
221 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
222 {
223 ERR("Renderer does not support stretctrect from textures!\n");
224 return EGL_NOT_INITIALIZED;
225 }
226
227 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
228
229 // ATI cards on XP have problems with non-power-of-two textures.
230 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
231 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
232 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
233 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
234
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000235 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
236 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
237
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000238 mMinSwapInterval = 4;
239 mMaxSwapInterval = 0;
240
241 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
242 {
243 mMinSwapInterval = std::min(mMinSwapInterval, 0);
244 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
245 }
246 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
247 {
248 mMinSwapInterval = std::min(mMinSwapInterval, 1);
249 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
250 }
251 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
252 {
253 mMinSwapInterval = std::min(mMinSwapInterval, 2);
254 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
255 }
256 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
257 {
258 mMinSwapInterval = std::min(mMinSwapInterval, 3);
259 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
260 }
261 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
262 {
263 mMinSwapInterval = std::min(mMinSwapInterval, 4);
264 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
265 }
266
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000267 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000268 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000269 {
270 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000271 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
272 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000273
274 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
275 {
276 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
277 {
278 max = j;
279 }
280 }
281 }
282
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000283 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000284 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000285 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000286 continue;
287
288 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000289 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
290 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000291
292 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
293 {
294 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
295 {
296 max = j;
297 }
298 }
299 }
300
301 mMaxSupportedSamples = max;
302
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000303 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
304 static const TCHAR className[] = TEXT("STATIC");
305
306 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
307
308 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
309 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
310
311 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
312 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
313 {
314 return EGL_BAD_ALLOC;
315 }
316
317 if (FAILED(result))
318 {
319 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
320
321 if (FAILED(result))
322 {
323 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
324 return EGL_BAD_ALLOC;
325 }
326 }
327
328 if (mD3d9Ex)
329 {
330 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
331 ASSERT(SUCCEEDED(result));
332 }
333
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000334 mVertexShaderCache.initialize(mDevice);
335 mPixelShaderCache.initialize(mDevice);
336
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000337 initializeDevice();
338
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000339 mBlit = new Blit(this);
340
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000341 return EGL_SUCCESS;
342}
343
344// do any one-time device initialization
345// NOTE: this is also needed after a device lost/reset
346// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000347void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000348{
349 // Permanent non-default states
350 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
351 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
352
353 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
354 {
355 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
356 }
357 else
358 {
359 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
360 }
361
362 mSceneStarted = false;
363}
364
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000365D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000366{
367 D3DPRESENT_PARAMETERS presentParameters = {0};
368
369 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
370 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
371 presentParameters.BackBufferCount = 1;
372 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
373 presentParameters.BackBufferWidth = 1;
374 presentParameters.BackBufferHeight = 1;
375 presentParameters.EnableAutoDepthStencil = FALSE;
376 presentParameters.Flags = 0;
377 presentParameters.hDeviceWindow = mDeviceWindow;
378 presentParameters.MultiSampleQuality = 0;
379 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
380 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
381 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
382 presentParameters.Windowed = TRUE;
383
384 return presentParameters;
385}
386
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000387int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000388{
389 D3DDISPLAYMODE currentDisplayMode;
390 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
391
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000392 int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
393 int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000394 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
395 int numConfigs = 0;
396
397 for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
398 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000399 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000400
401 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
402
403 if (SUCCEEDED(result))
404 {
405 for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
406 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000407 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000408 HRESULT result = D3D_OK;
409
410 if(depthStencilFormat != D3DFMT_UNKNOWN)
411 {
412 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
413 }
414
415 if (SUCCEEDED(result))
416 {
417 if(depthStencilFormat != D3DFMT_UNKNOWN)
418 {
419 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
420 }
421
422 if (SUCCEEDED(result))
423 {
424 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000425 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
426 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000427 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
428 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
429
430 (*configDescList)[numConfigs++] = newConfig;
431 }
432 }
433 }
434 }
435 }
436
437 return numConfigs;
438}
439
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000440void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000441{
442 delete [] (configDescList);
443}
444
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000445void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000446{
447 if (!mSceneStarted)
448 {
449 long result = mDevice->BeginScene();
450 if (SUCCEEDED(result)) {
451 // This is defensive checking against the device being
452 // lost at unexpected times.
453 mSceneStarted = true;
454 }
455 }
456}
457
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000458void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000459{
460 if (mSceneStarted)
461 {
462 // EndScene can fail if the device was lost, for example due
463 // to a TDR during a draw call.
464 mDevice->EndScene();
465 mSceneStarted = false;
466 }
467}
468
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000469// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000470void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000471{
472 HRESULT result;
473
474 IDirect3DQuery9* query = allocateEventQuery();
475 if (!query)
476 {
477 return;
478 }
479
480 result = query->Issue(D3DISSUE_END);
481 ASSERT(SUCCEEDED(result));
482
483 do
484 {
485 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
486
487 if(block && result == S_FALSE)
488 {
489 // Keep polling, but allow other threads to do something useful first
490 Sleep(0);
491 // explicitly check for device loss
492 // some drivers seem to return S_FALSE even if the device is lost
493 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000494 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000495 {
496 result = D3DERR_DEVICELOST;
497 }
498 }
499 }
500 while(block && result == S_FALSE);
501
502 freeEventQuery(query);
503
504 if (isDeviceLostError(result))
505 {
506 mDisplay->notifyDeviceLost();
507 }
508}
509
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000510SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
511{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000512 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000513}
514
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000515// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000516IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000517{
518 IDirect3DQuery9 *query = NULL;
519
520 if (mEventQueryPool.empty())
521 {
522 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
523 ASSERT(SUCCEEDED(result));
524 }
525 else
526 {
527 query = mEventQueryPool.back();
528 mEventQueryPool.pop_back();
529 }
530
531 return query;
532}
533
534// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000535void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000536{
537 if (mEventQueryPool.size() > 1000)
538 {
539 query->Release();
540 }
541 else
542 {
543 mEventQueryPool.push_back(query);
544 }
545}
546
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000547IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000548{
549 return mVertexShaderCache.create(function, length);
550}
551
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000552IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000553{
554 return mPixelShaderCache.create(function, length);
555}
556
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000557HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
558{
559 D3DPOOL Pool = getBufferPool(Usage);
560 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
561}
562
563HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
564{
565 D3DPOOL Pool = getBufferPool(Usage);
566 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
567}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000568
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000569void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000570{
571 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
572 int d3dSampler = index + d3dSamplerOffset;
573
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000574 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
575 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000576
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000577 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000578 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000579 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000580 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
581 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
582 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
583 if (mSupportsTextureFilterAnisotropy)
584 {
585 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
586 }
587}
588
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000589void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000590{
591 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
592 int d3dSampler = index + d3dSamplerOffset;
593 IDirect3DBaseTexture9 *d3dTexture = NULL;
594
595 if (texture)
596 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000597 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000598 if (texStorage)
599 {
600 d3dTexture = texStorage->getBaseTexture();
601 }
602 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000603 // in the texture class and we're unexpectedly missing the d3d texture
604 ASSERT(d3dTexture != NULL);
605 }
606
607 mDevice->SetTexture(d3dSampler, d3dTexture);
608}
609
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000610void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize)
611{
612 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
613 bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize;
614
615 if (rasterStateChanged)
616 {
617 // Set the cull mode
618 if (rasterState.cullFace)
619 {
620 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
621 }
622 else
623 {
624 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
625 }
626
627 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
628
629 mCurRasterState = rasterState;
630 }
631
632 if (rasterStateChanged || depthSizeChanged)
633 {
634 if (rasterState.polygonOffsetFill)
635 {
636 if (depthSize > 0)
637 {
638 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
639
640 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
641 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
642 }
643 }
644 else
645 {
646 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
647 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
648 }
649
650 mCurDepthSize = depthSize;
651 }
652
653 mForceSetRasterState = false;
654}
655
656void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
657{
658 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
659 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
660 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
661
662 if (blendStateChanged || blendColorChanged)
663 {
664 if (blendState.blend)
665 {
666 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
667
668 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
669 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
670 {
671 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
672 }
673 else
674 {
675 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
676 gl::unorm<8>(blendColor.alpha),
677 gl::unorm<8>(blendColor.alpha),
678 gl::unorm<8>(blendColor.alpha)));
679 }
680
681 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
682 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
683 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
684
685 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
686 blendState.destBlendRGB != blendState.destBlendAlpha ||
687 blendState.blendEquationRGB != blendState.blendEquationAlpha)
688 {
689 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
690
691 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
692 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
693 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
694 }
695 else
696 {
697 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
698 }
699 }
700 else
701 {
702 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
703 }
704
705 if (blendState.sampleAlphaToCoverage)
706 {
707 FIXME("Sample alpha to coverage is unimplemented.");
708 }
709
710 // Set the color mask
711 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
712 // Apparently some ATI cards have a bug where a draw with a zero color
713 // write mask can cause later draws to have incorrect results. Instead,
714 // set a nonzero color write mask but modify the blend state so that no
715 // drawing is done.
716 // http://code.google.com/p/angleproject/issues/detail?id=169
717
718 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
719 blendState.colorMaskBlue, blendState.colorMaskAlpha);
720 if (colorMask == 0 && !zeroColorMaskAllowed)
721 {
722 // Enable green channel, but set blending so nothing will be drawn.
723 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
724 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
725
726 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
727 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
728 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
729 }
730 else
731 {
732 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
733 }
734
735 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
736
737 mCurBlendState = blendState;
738 mCurBlendColor = blendColor;
739 }
740
741 if (sampleMaskChanged)
742 {
743 // Set the multisample mask
744 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
745 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
746
747 mCurSampleMask = sampleMask;
748 }
749
750 mForceSetBlendState = false;
751}
752
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000753void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
754 int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000755{
756 bool depthStencilStateChanged = mForceSetDepthStencilState ||
757 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000758 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
759 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000760 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
761 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
762
763 if (depthStencilStateChanged)
764 {
765 if (depthStencilState.depthTest)
766 {
767 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
768 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
769 }
770 else
771 {
772 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
773 }
774
775 mCurDepthStencilState = depthStencilState;
776 }
777
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000778 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000779 {
780 if (depthStencilState.stencilTest && stencilSize > 0)
781 {
782 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
783 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
784
785 // FIXME: Unsupported by D3D9
786 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
787 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
788 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
789 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000790 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000791 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
792 {
793 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
794 return error(GL_INVALID_OPERATION);
795 }
796
797 // get the maximum size of the stencil ref
798 GLuint maxStencil = (1 << stencilSize) - 1;
799
800 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
801 depthStencilState.stencilWritemask);
802 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
803 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
804
805 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000806 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000807 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
808 depthStencilState.stencilMask);
809
810 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
811 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
812 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
813 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
814 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
815 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
816
817 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
818 depthStencilState.stencilBackWritemask);
819 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
820 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
821
822 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000823 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000824 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
825 depthStencilState.stencilBackMask);
826
827 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
828 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
829 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
830 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
831 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
832 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
833 }
834 else
835 {
836 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
837 }
838
839 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
840
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000841 mCurStencilRef = stencilRef;
842 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000843 mCurFrontFaceCCW = frontFaceCCW;
844 mCurStencilSize = stencilSize;
845 }
846
847 mForceSetDepthStencilState = false;
848}
849
850void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
851 unsigned int renderTargetHeight)
852{
853 bool renderTargetSizedChanged = mForceSetScissor ||
854 renderTargetWidth != mCurRenderTargetWidth ||
855 renderTargetHeight != mCurRenderTargetHeight;
856 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
857
858 if (renderTargetSizedChanged || scissorChanged)
859 {
860 RECT rect;
861 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
862 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth));
863 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
864 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth));
865 mDevice->SetScissorRect(&rect);
866
867 mCurScissor = scissor;
868 mCurRenderTargetWidth = renderTargetWidth;
869 mCurRenderTargetHeight = renderTargetHeight;
870 }
871
872 mForceSetScissor = false;
873}
874
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000875bool Renderer9::setViewport(const gl::Rectangle& viewport, float zNear, float zFar,
876 unsigned int renderTargetWidth, unsigned int renderTargetHeight,
877 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
878{
879 bool viewportChanged = mForceSetViewport || memcmp(&viewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
880 zNear != mCurNear || zFar != mCurFar;
881
882 D3DVIEWPORT9 dxViewport;
883 dxViewport.X = gl::clamp(viewport.x, 0, static_cast<int>(renderTargetWidth));
884 dxViewport.Y = gl::clamp(viewport.y, 0, static_cast<int>(renderTargetHeight));
885 dxViewport.Width = gl::clamp(viewport.width, 0, static_cast<int>(renderTargetWidth) - static_cast<int>(dxViewport.X));
886 dxViewport.Height = gl::clamp(viewport.height, 0, static_cast<int>(renderTargetHeight) - static_cast<int>(dxViewport.Y));
887 dxViewport.MinZ = zNear;
888 dxViewport.MaxZ = zFar;
889
890 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
891 {
892 return false; // Nothing to render
893 }
894
895 if (viewportChanged)
896 {
897 mDevice->SetViewport(&dxViewport);
898
899 mCurViewport = viewport;
900 mCurNear = zNear;
901 mCurFar = zFar;
902 }
903
904 if (currentProgram && (viewportChanged || forceSetUniforms))
905 {
906 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
907 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
908 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
909
910 // These values are used for computing gl_FragCoord in Program::linkVaryings().
911 GLint coord = currentProgram->getDxCoordLocation();
912 GLfloat whxy[4] = { viewport.width * 0.5f,
913 viewport.height * 0.5f,
914 viewport.x + (viewport.width * 0.5f),
915 viewport.y + (viewport.height * 0.5f) };
916 currentProgram->setUniform4fv(coord, 1, whxy);
917
918 GLint depth = currentProgram->getDxDepthLocation();
919 GLfloat dz[2] = { (zFar - zNear) * 0.5f, (zNear + zFar) * 0.5f };
920 currentProgram->setUniform2fv(depth, 1, dz);
921
922 GLint depthRange = currentProgram->getDxDepthRangeLocation();
923 GLfloat nearFarDiff[3] = { zNear, zFar, zFar - zNear };
924 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
925 }
926
927 mForceSetViewport = false;
928 return true;
929}
930
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000931void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer)
932{
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000933 // TODO: only set these when the rendertarget actually changes
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000934 mForceSetScissor = true;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000935 mForceSetViewport = true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000936
937 // TODO
938}
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000939
daniel@transgaming.comd084c622012-11-28 19:36:05 +0000940void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear,
941 gl::Framebuffer *frameBuffer)
942{
943 mForceSetDepthStencilState = true;
944
945 // TODO
946}
947
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000948void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000949{
950 while (!mEventQueryPool.empty())
951 {
952 mEventQueryPool.back()->Release();
953 mEventQueryPool.pop_back();
954 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000955
956 mVertexShaderCache.clear();
957 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000958}
959
960
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000961void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000962{
963 mDeviceLost = true;
964}
965
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000966bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000967{
968 return mDeviceLost;
969}
970
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000971// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000972bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000973{
974 bool isLost = false;
975
976 if (mDeviceEx)
977 {
978 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
979 }
980 else if (mDevice)
981 {
982 isLost = FAILED(mDevice->TestCooperativeLevel());
983 }
984 else
985 {
986 // No device yet, so no reset required
987 }
988
989 if (isLost)
990 {
991 // ensure we note the device loss --
992 // we'll probably get this done again by markDeviceLost
993 // but best to remember it!
994 // Note that we don't want to clear the device loss status here
995 // -- this needs to be done by resetDevice
996 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000997 if (notify)
998 {
999 mDisplay->notifyDeviceLost();
1000 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001001 }
1002
1003 return isLost;
1004}
1005
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001006bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001007{
1008 HRESULT status = D3D_OK;
1009
1010 if (mDeviceEx)
1011 {
1012 status = mDeviceEx->CheckDeviceState(NULL);
1013 }
1014 else if (mDevice)
1015 {
1016 status = mDevice->TestCooperativeLevel();
1017 }
1018
1019 switch (status)
1020 {
1021 case D3DERR_DEVICENOTRESET:
1022 case D3DERR_DEVICEHUNG:
1023 return true;
1024 default:
1025 return false;
1026 }
1027}
1028
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001029bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001030{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001031 releaseDeviceResources();
1032
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001033 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1034
1035 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001036 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001037 int attempts = 3;
1038
1039 while (lost && attempts > 0)
1040 {
1041 if (mDeviceEx)
1042 {
1043 Sleep(500); // Give the graphics driver some CPU time
1044 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1045 }
1046 else
1047 {
1048 result = mDevice->TestCooperativeLevel();
1049 while (result == D3DERR_DEVICELOST)
1050 {
1051 Sleep(100); // Give the graphics driver some CPU time
1052 result = mDevice->TestCooperativeLevel();
1053 }
1054
1055 if (result == D3DERR_DEVICENOTRESET)
1056 {
1057 result = mDevice->Reset(&presentParameters);
1058 }
1059 }
1060
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001061 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001062 attempts --;
1063 }
1064
1065 if (FAILED(result))
1066 {
1067 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1068 return false;
1069 }
1070
1071 // reset device defaults
1072 initializeDevice();
1073 mDeviceLost = false;
1074
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001075 mForceSetDepthStencilState = true;
1076 mForceSetRasterState = true;
1077 mForceSetBlendState = true;
1078 mForceSetScissor = true;
1079
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001080 return true;
1081}
1082
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001083DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001084{
1085 return mAdapterIdentifier.VendorId;
1086}
1087
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001088const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001089{
1090 return mAdapterIdentifier.Description;
1091}
1092
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001093GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001094{
1095 return mAdapterIdentifier.DeviceIdentifier;
1096}
1097
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001098void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001099{
1100 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1101 {
1102 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1103 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1104
1105 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1106 }
1107}
1108
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001109bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001110{
1111 D3DDISPLAYMODE currentDisplayMode;
1112 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1113
1114 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1115}
1116
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001117bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001118{
1119 D3DDISPLAYMODE currentDisplayMode;
1120 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1121
1122 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1123}
1124
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001125bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001126{
1127 D3DDISPLAYMODE currentDisplayMode;
1128 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1129
1130 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1131}
1132
1133// we use INTZ for depth textures in Direct3D9
1134// we also want NULL texture support to ensure the we can make depth-only FBOs
1135// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001136bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001137{
1138 D3DDISPLAYMODE currentDisplayMode;
1139 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1140
1141 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1142 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1143 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1144 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1145
1146 return intz && null;
1147}
1148
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001149bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001150{
1151 D3DDISPLAYMODE currentDisplayMode;
1152 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1153
1154 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1155 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1156 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1157 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1158
1159 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1160 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1161 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1162 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1163
1164 if (!*filtering && !*renderable)
1165 {
1166 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1167 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1168 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1169 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1170 }
1171 else
1172 {
1173 return true;
1174 }
1175}
1176
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001177bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001178{
1179 D3DDISPLAYMODE currentDisplayMode;
1180 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1181
1182 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1183 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1184 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1185 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1186
1187 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1188 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1189 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1190 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1191
1192 if (!*filtering && !*renderable)
1193 {
1194 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1195 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1196 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1197 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1198 }
1199 else
1200 {
1201 return true;
1202 }
1203}
1204
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001205bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001206{
1207 D3DDISPLAYMODE currentDisplayMode;
1208 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1209
1210 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1211}
1212
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001213bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001214{
1215 D3DDISPLAYMODE currentDisplayMode;
1216 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1217
1218 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1219}
1220
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001221bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001222{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001223 return mSupportsTextureFilterAnisotropy;
1224}
1225
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001226float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001227{
1228 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001229 {
1230 return mDeviceCaps.MaxAnisotropy;
1231 }
1232 return 1.0f;
1233}
1234
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001235bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001236{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001237 IDirect3DQuery9 *query = allocateEventQuery();
1238 if (query)
1239 {
1240 freeEventQuery(query);
1241 return true;
1242 }
1243 else
1244 {
1245 return false;
1246 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001247 return true;
1248}
1249
1250// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1251// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001252bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001253{
1254 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1255 {
1256 return false;
1257 }
1258
1259 D3DDISPLAYMODE currentDisplayMode;
1260 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1261
1262 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1263
1264 return SUCCEEDED(result);
1265}
1266
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001267bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001268{
1269 return mSupportsNonPower2Textures;
1270}
1271
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001272bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001273{
1274 if (!mDevice)
1275 {
1276 return false;
1277 }
1278
1279 IDirect3DQuery9 *query = NULL;
1280 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1281 if (SUCCEEDED(result) && query)
1282 {
1283 query->Release();
1284 return true;
1285 }
1286 else
1287 {
1288 return false;
1289 }
1290}
1291
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001292bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001293{
1294 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1295}
1296
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001297bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001298{
1299 // PIX doesn't seem to support using share handles, so disable them.
1300 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001301 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001302}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001303
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001304bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001305{
1306 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1307}
1308
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001309float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001310{
1311 return mDeviceCaps.MaxPointSize;
1312}
1313
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001314int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001315{
1316 return (int)mDeviceCaps.MaxTextureWidth;
1317}
1318
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001319int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001320{
1321 return (int)mDeviceCaps.MaxTextureHeight;
1322}
1323
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001324bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001325{
1326 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1327}
1328
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001329DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001330{
1331 return mDeviceCaps.DeclTypes;
1332}
1333
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001334int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001335{
1336 return mMinSwapInterval;
1337}
1338
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001339int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001340{
1341 return mMaxSwapInterval;
1342}
1343
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001344int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001345{
1346 return mMaxSupportedSamples;
1347}
1348
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001349int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001350{
1351 if (requested == 0)
1352 {
1353 return requested;
1354 }
1355
1356 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
1357 if (itr == mMultiSampleSupport.end())
1358 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00001359 if (format == D3DFMT_UNKNOWN)
1360 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001361 return -1;
1362 }
1363
1364 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
1365 {
1366 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
1367 {
1368 return i;
1369 }
1370 }
1371
1372 return -1;
1373}
1374
daniel@transgaming.coma9571682012-11-28 19:33:08 +00001375D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
1376{
1377 switch (internalformat)
1378 {
1379 case GL_DEPTH_COMPONENT16:
1380 case GL_DEPTH_COMPONENT32_OES:
1381 case GL_DEPTH24_STENCIL8_OES:
1382 return D3DFMT_INTZ;
1383 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1384 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1385 return D3DFMT_DXT1;
1386 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1387 return D3DFMT_DXT3;
1388 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1389 return D3DFMT_DXT5;
1390 case GL_RGBA32F_EXT:
1391 case GL_RGB32F_EXT:
1392 case GL_ALPHA32F_EXT:
1393 case GL_LUMINANCE32F_EXT:
1394 case GL_LUMINANCE_ALPHA32F_EXT:
1395 return D3DFMT_A32B32G32R32F;
1396 case GL_RGBA16F_EXT:
1397 case GL_RGB16F_EXT:
1398 case GL_ALPHA16F_EXT:
1399 case GL_LUMINANCE16F_EXT:
1400 case GL_LUMINANCE_ALPHA16F_EXT:
1401 return D3DFMT_A16B16G16R16F;
1402 case GL_LUMINANCE8_EXT:
1403 if (getLuminanceTextureSupport())
1404 {
1405 return D3DFMT_L8;
1406 }
1407 break;
1408 case GL_LUMINANCE8_ALPHA8_EXT:
1409 if (getLuminanceAlphaTextureSupport())
1410 {
1411 return D3DFMT_A8L8;
1412 }
1413 break;
1414 case GL_RGB8_OES:
1415 case GL_RGB565:
1416 return D3DFMT_X8R8G8B8;
1417 }
1418
1419 return D3DFMT_A8R8G8B8;
1420}
1421
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001422bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001423{
1424 bool result = false;
1425
1426 if (source && dest)
1427 {
1428 int levels = source->levelCount();
1429 for (int i = 0; i < levels; ++i)
1430 {
1431 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
1432 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
1433
1434 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1435
1436 if (srcSurf) srcSurf->Release();
1437 if (dstSurf) dstSurf->Release();
1438
1439 if (!result)
1440 return false;
1441 }
1442 }
1443
1444 return result;
1445}
1446
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001447bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001448{
1449 bool result = false;
1450
1451 if (source && dest)
1452 {
1453 int levels = source->levelCount();
1454 for (int f = 0; f < 6; f++)
1455 {
1456 for (int i = 0; i < levels; i++)
1457 {
1458 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
1459 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
1460
1461 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1462
1463 if (srcSurf) srcSurf->Release();
1464 if (dstSurf) dstSurf->Release();
1465
1466 if (!result)
1467 return false;
1468 }
1469 }
1470 }
1471
1472 return result;
1473}
1474
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001475D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001476{
1477 if (mD3d9Ex != NULL)
1478 {
1479 return D3DPOOL_DEFAULT;
1480 }
1481 else
1482 {
1483 if (!(usage & D3DUSAGE_DYNAMIC))
1484 {
1485 return D3DPOOL_MANAGED;
1486 }
1487 }
1488
1489 return D3DPOOL_DEFAULT;
1490}
1491
daniel@transgaming.com38380882012-11-28 19:36:39 +00001492bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1493 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001494{
1495 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
1496}
1497
daniel@transgaming.com38380882012-11-28 19:36:39 +00001498bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1499 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001500{
1501 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
1502}
1503
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001504bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
1505 bool blitRenderTarget, bool blitDepthStencil)
1506{
1507 endScene();
1508
1509 if (blitRenderTarget)
1510 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001511 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
1512 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
1513 RenderTarget9 *readRenderTarget = NULL;
1514 RenderTarget9 *drawRenderTarget = NULL;
1515 IDirect3DSurface9* readSurface = NULL;
1516 IDirect3DSurface9* drawSurface = NULL;
1517
1518 if (readBuffer)
1519 {
1520 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
1521 }
1522 if (drawBuffer)
1523 {
1524 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
1525 }
1526
1527 if (readRenderTarget)
1528 {
1529 readSurface = readRenderTarget->getSurface();
1530 }
1531 if (drawRenderTarget)
1532 {
1533 drawSurface = drawRenderTarget->getSurface();
1534 }
1535
1536 if (!readSurface || !drawSurface)
1537 {
1538 ERR("Failed to retrieve the render target.");
1539 return error(GL_OUT_OF_MEMORY, false);
1540 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001541
1542 RECT srcRect, dstRect;
1543 RECT *srcRectPtr = NULL;
1544 RECT *dstRectPtr = NULL;
1545
1546 if (readRect)
1547 {
1548 srcRect.left = readRect->x;
1549 srcRect.right = readRect->x + readRect->width;
1550 srcRect.top = readRect->y;
1551 srcRect.bottom = readRect->y + readRect->height;
1552 srcRectPtr = &srcRect;
1553 }
1554
1555 if (drawRect)
1556 {
1557 dstRect.left = drawRect->x;
1558 dstRect.right = drawRect->x + drawRect->width;
1559 dstRect.top = drawRect->y;
1560 dstRect.bottom = drawRect->y + drawRect->height;
1561 dstRectPtr = &dstRect;
1562 }
1563
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001564 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001565
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001566 readSurface->Release();
1567 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001568
1569 if (FAILED(result))
1570 {
1571 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
1572 return false;
1573 }
1574 }
1575
1576 if (blitDepthStencil)
1577 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001578 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
1579 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
1580 RenderTarget9 *readDepthStencil = NULL;
1581 RenderTarget9 *drawDepthStencil = NULL;
1582 IDirect3DSurface9* readSurface = NULL;
1583 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001584
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001585 if (readBuffer)
1586 {
1587 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
1588 }
1589 if (drawBuffer)
1590 {
1591 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
1592 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001593
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001594 if (readDepthStencil)
1595 {
1596 readSurface = readDepthStencil->getSurface();
1597 }
1598 if (drawDepthStencil)
1599 {
1600 drawSurface = drawDepthStencil->getSurface();
1601 }
1602
1603 if (!readSurface || !drawSurface)
1604 {
1605 ERR("Failed to retrieve the render target.");
1606 return error(GL_OUT_OF_MEMORY, false);
1607 }
1608
1609 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
1610
1611 readSurface->Release();
1612 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001613
1614 if (FAILED(result))
1615 {
1616 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
1617 return false;
1618 }
1619 }
1620
1621 return true;
1622}
1623
1624void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
1625 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
1626{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001627 RenderTarget9 *renderTarget = NULL;
1628 IDirect3DSurface9 *surface = NULL;
1629 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1630
1631 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001632 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001633 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
1634 }
1635
1636 if (renderTarget)
1637 {
1638 surface = renderTarget->getSurface();
1639 }
1640
1641 if (!surface)
1642 {
1643 // context must be lost
1644 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001645 }
1646
1647 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001648 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001649
1650 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1651 {
1652 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001653 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001654 return error(GL_OUT_OF_MEMORY);
1655 }
1656
1657 HRESULT result;
1658 IDirect3DSurface9 *systemSurface = NULL;
1659 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
1660 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
1661 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
1662 if (directToPixels)
1663 {
1664 // Use the pixels ptr as a shared handle to write directly into client's memory
1665 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
1666 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
1667 if (FAILED(result))
1668 {
1669 // Try again without the shared handle
1670 directToPixels = false;
1671 }
1672 }
1673
1674 if (!directToPixels)
1675 {
1676 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
1677 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1678 if (FAILED(result))
1679 {
1680 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001681 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001682 return error(GL_OUT_OF_MEMORY);
1683 }
1684 }
1685
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001686 result = mDevice->GetRenderTargetData(surface, systemSurface);
1687 surface->Release();
1688 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001689
1690 if (FAILED(result))
1691 {
1692 systemSurface->Release();
1693
1694 // It turns out that D3D will sometimes produce more error
1695 // codes than those documented.
1696 if (gl::checkDeviceLost(result))
1697 return error(GL_OUT_OF_MEMORY);
1698 else
1699 {
1700 UNREACHABLE();
1701 return;
1702 }
1703
1704 }
1705
1706 if (directToPixels)
1707 {
1708 systemSurface->Release();
1709 return;
1710 }
1711
1712 RECT rect;
1713 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
1714 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
1715 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
1716 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
1717
1718 D3DLOCKED_RECT lock;
1719 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1720
1721 if (FAILED(result))
1722 {
1723 UNREACHABLE();
1724 systemSurface->Release();
1725
1726 return; // No sensible error to generate
1727 }
1728
1729 unsigned char *dest = (unsigned char*)pixels;
1730 unsigned short *dest16 = (unsigned short*)pixels;
1731
1732 unsigned char *source;
1733 int inputPitch;
1734 if (packReverseRowOrder)
1735 {
1736 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
1737 inputPitch = -lock.Pitch;
1738 }
1739 else
1740 {
1741 source = (unsigned char*)lock.pBits;
1742 inputPitch = lock.Pitch;
1743 }
1744
1745 unsigned int fastPixelSize = 0;
1746
1747 if (desc.Format == D3DFMT_A8R8G8B8 &&
1748 format == GL_BGRA_EXT &&
1749 type == GL_UNSIGNED_BYTE)
1750 {
1751 fastPixelSize = 4;
1752 }
1753 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
1754 format == GL_BGRA_EXT &&
1755 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
1756 (desc.Format == D3DFMT_A1R5G5B5 &&
1757 format == GL_BGRA_EXT &&
1758 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
1759 {
1760 fastPixelSize = 2;
1761 }
1762 else if (desc.Format == D3DFMT_A16B16G16R16F &&
1763 format == GL_RGBA &&
1764 type == GL_HALF_FLOAT_OES)
1765 {
1766 fastPixelSize = 8;
1767 }
1768 else if (desc.Format == D3DFMT_A32B32G32R32F &&
1769 format == GL_RGBA &&
1770 type == GL_FLOAT)
1771 {
1772 fastPixelSize = 16;
1773 }
1774
1775 for (int j = 0; j < rect.bottom - rect.top; j++)
1776 {
1777 if (fastPixelSize != 0)
1778 {
1779 // Fast path for formats which require no translation:
1780 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
1781 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
1782 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
1783 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
1784 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
1785 //
1786 // Note that buffers with no alpha go through the slow path below.
1787 memcpy(dest + j * outputPitch,
1788 source + j * inputPitch,
1789 (rect.right - rect.left) * fastPixelSize);
1790 continue;
1791 }
1792
1793 for (int i = 0; i < rect.right - rect.left; i++)
1794 {
1795 float r;
1796 float g;
1797 float b;
1798 float a;
1799
1800 switch (desc.Format)
1801 {
1802 case D3DFMT_R5G6B5:
1803 {
1804 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
1805
1806 a = 1.0f;
1807 b = (rgb & 0x001F) * (1.0f / 0x001F);
1808 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1809 r = (rgb & 0xF800) * (1.0f / 0xF800);
1810 }
1811 break;
1812 case D3DFMT_A1R5G5B5:
1813 {
1814 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
1815
1816 a = (argb & 0x8000) ? 1.0f : 0.0f;
1817 b = (argb & 0x001F) * (1.0f / 0x001F);
1818 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1819 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1820 }
1821 break;
1822 case D3DFMT_A8R8G8B8:
1823 {
1824 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
1825
1826 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1827 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1828 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1829 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1830 }
1831 break;
1832 case D3DFMT_X8R8G8B8:
1833 {
1834 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
1835
1836 a = 1.0f;
1837 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1838 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1839 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1840 }
1841 break;
1842 case D3DFMT_A2R10G10B10:
1843 {
1844 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
1845
1846 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1847 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1848 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1849 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1850 }
1851 break;
1852 case D3DFMT_A32B32G32R32F:
1853 {
1854 // float formats in D3D are stored rgba, rather than the other way round
1855 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
1856 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
1857 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
1858 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
1859 }
1860 break;
1861 case D3DFMT_A16B16G16R16F:
1862 {
1863 // float formats in D3D are stored rgba, rather than the other way round
1864 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
1865 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
1866 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
1867 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
1868 }
1869 break;
1870 default:
1871 UNIMPLEMENTED(); // FIXME
1872 UNREACHABLE();
1873 return;
1874 }
1875
1876 switch (format)
1877 {
1878 case GL_RGBA:
1879 switch (type)
1880 {
1881 case GL_UNSIGNED_BYTE:
1882 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
1883 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
1884 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
1885 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
1886 break;
1887 default: UNREACHABLE();
1888 }
1889 break;
1890 case GL_BGRA_EXT:
1891 switch (type)
1892 {
1893 case GL_UNSIGNED_BYTE:
1894 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
1895 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
1896 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
1897 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
1898 break;
1899 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
1900 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
1901 // this type is packed as follows:
1902 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1903 // --------------------------------------------------------------------------------
1904 // | 4th | 3rd | 2nd | 1st component |
1905 // --------------------------------------------------------------------------------
1906 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
1907 dest16[i + j * outputPitch / sizeof(unsigned short)] =
1908 ((unsigned short)(15 * a + 0.5f) << 12)|
1909 ((unsigned short)(15 * r + 0.5f) << 8) |
1910 ((unsigned short)(15 * g + 0.5f) << 4) |
1911 ((unsigned short)(15 * b + 0.5f) << 0);
1912 break;
1913 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
1914 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
1915 // this type is packed as follows:
1916 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1917 // --------------------------------------------------------------------------------
1918 // | 4th | 3rd | 2nd | 1st component |
1919 // --------------------------------------------------------------------------------
1920 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
1921 dest16[i + j * outputPitch / sizeof(unsigned short)] =
1922 ((unsigned short)( a + 0.5f) << 15) |
1923 ((unsigned short)(31 * r + 0.5f) << 10) |
1924 ((unsigned short)(31 * g + 0.5f) << 5) |
1925 ((unsigned short)(31 * b + 0.5f) << 0);
1926 break;
1927 default: UNREACHABLE();
1928 }
1929 break;
1930 case GL_RGB:
1931 switch (type)
1932 {
1933 case GL_UNSIGNED_SHORT_5_6_5:
1934 dest16[i + j * outputPitch / sizeof(unsigned short)] =
1935 ((unsigned short)(31 * b + 0.5f) << 0) |
1936 ((unsigned short)(63 * g + 0.5f) << 5) |
1937 ((unsigned short)(31 * r + 0.5f) << 11);
1938 break;
1939 case GL_UNSIGNED_BYTE:
1940 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
1941 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
1942 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
1943 break;
1944 default: UNREACHABLE();
1945 }
1946 break;
1947 default: UNREACHABLE();
1948 }
1949 }
1950 }
1951
1952 systemSurface->UnlockRect();
1953
1954 systemSurface->Release();
1955}
1956
1957bool Renderer9::setRenderTarget(gl::Renderbuffer *renderbuffer)
1958{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001959 IDirect3DSurface9 *renderTargetSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001960
1961 if (renderbuffer)
1962 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001963 RenderTarget *renderTarget = renderbuffer->getRenderTarget();
1964 if (renderTarget)
1965 {
1966 renderTargetSurface = renderTarget->getSurface();
1967 }
1968
1969 if (!renderTargetSurface)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001970 {
1971 ERR("render target pointer unexpectedly null.");
1972 return false; // Context must be lost
1973 }
1974
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001975 mDevice->SetRenderTarget(0, renderTargetSurface);
1976 renderTargetSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001977 }
1978 else
1979 {
1980 mDevice->SetRenderTarget(0, NULL);
1981 }
1982
1983 return true;
1984}
1985
1986bool Renderer9::setDepthStencil(gl::Renderbuffer *renderbuffer)
1987{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001988 IDirect3DSurface9 *depthStencilSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001989
1990 if (renderbuffer)
1991 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001992 RenderTarget *depthStencil = renderbuffer->getDepthStencil();
1993
1994 if (depthStencil)
1995 {
1996 depthStencilSurface = depthStencil->getSurface();
1997 }
1998
1999 if (!depthStencilSurface)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002000 {
2001 ERR("depth stencil pointer unexpectedly null.");
2002 return false; // Context must be lost
2003 }
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002004
2005 mDevice->SetDepthStencilSurface(depthStencilSurface);
2006 depthStencilSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002007 }
2008 else
2009 {
2010 mDevice->SetDepthStencilSurface(NULL);
2011 }
2012 return true;
2013}
2014
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002015bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2016{
2017 return mBlit->boxFilter(source, dest);
2018}
2019
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002020D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002021{
2022 if (mD3d9Ex != NULL)
2023 {
2024 return D3DPOOL_DEFAULT;
2025 }
2026 else
2027 {
2028 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2029 {
2030 return D3DPOOL_MANAGED;
2031 }
2032 }
2033
2034 return D3DPOOL_DEFAULT;
2035}
2036
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002037bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2038{
2039 if (source && dest)
2040 {
2041 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2042 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2043
2044 if (fromManaged)
2045 {
2046 D3DSURFACE_DESC desc;
2047 source->GetDesc(&desc);
2048
2049 IDirect3DSurface9 *surf = 0;
2050 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2051
2052 if (SUCCEEDED(result))
2053 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002054 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002055 result = device->UpdateSurface(surf, NULL, dest, NULL);
2056 surf->Release();
2057 }
2058 }
2059 else
2060 {
2061 endScene();
2062 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2063 }
2064
2065 if (FAILED(result))
2066 {
2067 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2068 return false;
2069 }
2070 }
2071
2072 return true;
2073}
2074
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002075}