blob: 10a858ec1f8e845a3825c5900655c22acc355228 [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.com621ce052012-10-31 17:52:29 +000086}
87
daniel@transgaming.com2507f412012-10-31 18:46:48 +000088Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +000089{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000090 releaseDeviceResources();
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000091
92 delete mBlit;
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000093
daniel@transgaming.com621ce052012-10-31 17:52:29 +000094 if (mDevice)
95 {
96 // 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 +000097 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +000098 {
99 resetDevice();
100 }
101
102 mDevice->Release();
103 mDevice = NULL;
104 }
105
106 if (mDeviceEx)
107 {
108 mDeviceEx->Release();
109 mDeviceEx = NULL;
110 }
111
112 if (mD3d9)
113 {
114 mD3d9->Release();
115 mD3d9 = NULL;
116 }
117
118 if (mDeviceWindow)
119 {
120 DestroyWindow(mDeviceWindow);
121 mDeviceWindow = NULL;
122 }
123
124 if (mD3d9Ex)
125 {
126 mD3d9Ex->Release();
127 mD3d9Ex = NULL;
128 }
129
130 if (mD3d9Module)
131 {
132 mD3d9Module = NULL;
133 }
134
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000135 while (!mMultiSampleSupport.empty())
136 {
137 delete [] mMultiSampleSupport.begin()->second;
138 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
139 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000140}
141
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000142EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000143{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000144 if (mSoftwareDevice)
145 {
146 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
147 }
148 else
149 {
150 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
151 }
152
153 if (mD3d9Module == NULL)
154 {
155 ERR("No D3D9 module found - aborting!\n");
156 return EGL_NOT_INITIALIZED;
157 }
158
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000159 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
160 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
161
162 // Use Direct3D9Ex if available. Among other things, this version is less
163 // inclined to report a lost context, for example when the user switches
164 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
165 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
166 {
167 ASSERT(mD3d9Ex);
168 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
169 ASSERT(mD3d9);
170 }
171 else
172 {
173 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
174 }
175
176 if (!mD3d9)
177 {
178 ERR("Could not create D3D9 device - aborting!\n");
179 return EGL_NOT_INITIALIZED;
180 }
181 if (mDc != NULL)
182 {
183 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
184 }
185
186 HRESULT result;
187
188 // Give up on getting device caps after about one second.
189 for (int i = 0; i < 10; ++i)
190 {
191 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
192 if (SUCCEEDED(result))
193 {
194 break;
195 }
196 else if (result == D3DERR_NOTAVAILABLE)
197 {
198 Sleep(100); // Give the driver some time to initialize/recover
199 }
200 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
201 {
202 ERR("failed to get device caps (0x%x)\n", result);
203 return EGL_NOT_INITIALIZED;
204 }
205 }
206
207 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
208 {
209 ERR("Renderer does not support PS 2.0. aborting!\n");
210 return EGL_NOT_INITIALIZED;
211 }
212
213 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
214 // This is required by Texture2D::convertToRenderTarget.
215 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
216 {
217 ERR("Renderer does not support stretctrect from textures!\n");
218 return EGL_NOT_INITIALIZED;
219 }
220
221 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
222
223 // ATI cards on XP have problems with non-power-of-two textures.
224 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
225 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
226 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
227 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
228
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000229 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
230 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
231
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000232 mMinSwapInterval = 4;
233 mMaxSwapInterval = 0;
234
235 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
236 {
237 mMinSwapInterval = std::min(mMinSwapInterval, 0);
238 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
239 }
240 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
241 {
242 mMinSwapInterval = std::min(mMinSwapInterval, 1);
243 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
244 }
245 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
246 {
247 mMinSwapInterval = std::min(mMinSwapInterval, 2);
248 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
249 }
250 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
251 {
252 mMinSwapInterval = std::min(mMinSwapInterval, 3);
253 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
254 }
255 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
256 {
257 mMinSwapInterval = std::min(mMinSwapInterval, 4);
258 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
259 }
260
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000261 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000262 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000263 {
264 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000265 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
266 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000267
268 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
269 {
270 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
271 {
272 max = j;
273 }
274 }
275 }
276
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000277 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000278 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000279 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000280 continue;
281
282 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000283 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
284 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000285
286 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
287 {
288 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
289 {
290 max = j;
291 }
292 }
293 }
294
295 mMaxSupportedSamples = max;
296
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000297 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
298 static const TCHAR className[] = TEXT("STATIC");
299
300 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
301
302 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
303 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
304
305 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
306 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
307 {
308 return EGL_BAD_ALLOC;
309 }
310
311 if (FAILED(result))
312 {
313 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
314
315 if (FAILED(result))
316 {
317 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
318 return EGL_BAD_ALLOC;
319 }
320 }
321
322 if (mD3d9Ex)
323 {
324 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
325 ASSERT(SUCCEEDED(result));
326 }
327
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000328 mVertexShaderCache.initialize(mDevice);
329 mPixelShaderCache.initialize(mDevice);
330
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000331 initializeDevice();
332
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000333 mBlit = new Blit(this);
334
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000335 return EGL_SUCCESS;
336}
337
338// do any one-time device initialization
339// NOTE: this is also needed after a device lost/reset
340// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000341void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000342{
343 // Permanent non-default states
344 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
345 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
346
347 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
348 {
349 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
350 }
351 else
352 {
353 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
354 }
355
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000356 markAllStateDirty();
357
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000358 mSceneStarted = false;
359}
360
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000361D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000362{
363 D3DPRESENT_PARAMETERS presentParameters = {0};
364
365 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
366 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
367 presentParameters.BackBufferCount = 1;
368 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
369 presentParameters.BackBufferWidth = 1;
370 presentParameters.BackBufferHeight = 1;
371 presentParameters.EnableAutoDepthStencil = FALSE;
372 presentParameters.Flags = 0;
373 presentParameters.hDeviceWindow = mDeviceWindow;
374 presentParameters.MultiSampleQuality = 0;
375 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
376 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
377 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
378 presentParameters.Windowed = TRUE;
379
380 return presentParameters;
381}
382
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000383int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000384{
385 D3DDISPLAYMODE currentDisplayMode;
386 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
387
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000388 int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
389 int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000390 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
391 int numConfigs = 0;
392
393 for (int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
394 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000395 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000396
397 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
398
399 if (SUCCEEDED(result))
400 {
401 for (int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
402 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000403 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000404 HRESULT result = D3D_OK;
405
406 if(depthStencilFormat != D3DFMT_UNKNOWN)
407 {
408 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
409 }
410
411 if (SUCCEEDED(result))
412 {
413 if(depthStencilFormat != D3DFMT_UNKNOWN)
414 {
415 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
416 }
417
418 if (SUCCEEDED(result))
419 {
420 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000421 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
422 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000423 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
424 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
425
426 (*configDescList)[numConfigs++] = newConfig;
427 }
428 }
429 }
430 }
431 }
432
433 return numConfigs;
434}
435
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000436void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000437{
438 delete [] (configDescList);
439}
440
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000441void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000442{
443 if (!mSceneStarted)
444 {
445 long result = mDevice->BeginScene();
446 if (SUCCEEDED(result)) {
447 // This is defensive checking against the device being
448 // lost at unexpected times.
449 mSceneStarted = true;
450 }
451 }
452}
453
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000454void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000455{
456 if (mSceneStarted)
457 {
458 // EndScene can fail if the device was lost, for example due
459 // to a TDR during a draw call.
460 mDevice->EndScene();
461 mSceneStarted = false;
462 }
463}
464
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000465// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000466void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000467{
468 HRESULT result;
469
470 IDirect3DQuery9* query = allocateEventQuery();
471 if (!query)
472 {
473 return;
474 }
475
476 result = query->Issue(D3DISSUE_END);
477 ASSERT(SUCCEEDED(result));
478
479 do
480 {
481 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
482
483 if(block && result == S_FALSE)
484 {
485 // Keep polling, but allow other threads to do something useful first
486 Sleep(0);
487 // explicitly check for device loss
488 // some drivers seem to return S_FALSE even if the device is lost
489 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000490 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000491 {
492 result = D3DERR_DEVICELOST;
493 }
494 }
495 }
496 while(block && result == S_FALSE);
497
498 freeEventQuery(query);
499
500 if (isDeviceLostError(result))
501 {
502 mDisplay->notifyDeviceLost();
503 }
504}
505
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000506SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
507{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000508 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000509}
510
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000511// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000512IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000513{
514 IDirect3DQuery9 *query = NULL;
515
516 if (mEventQueryPool.empty())
517 {
518 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
519 ASSERT(SUCCEEDED(result));
520 }
521 else
522 {
523 query = mEventQueryPool.back();
524 mEventQueryPool.pop_back();
525 }
526
527 return query;
528}
529
530// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000531void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000532{
533 if (mEventQueryPool.size() > 1000)
534 {
535 query->Release();
536 }
537 else
538 {
539 mEventQueryPool.push_back(query);
540 }
541}
542
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000543IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000544{
545 return mVertexShaderCache.create(function, length);
546}
547
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000548IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000549{
550 return mPixelShaderCache.create(function, length);
551}
552
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000553HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
554{
555 D3DPOOL Pool = getBufferPool(Usage);
556 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
557}
558
559HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
560{
561 D3DPOOL Pool = getBufferPool(Usage);
562 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
563}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000564
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000565void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000566{
567 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
568 int d3dSampler = index + d3dSamplerOffset;
569
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000570 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
571 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000572
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000573 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000574 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000575 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000576 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
577 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
578 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
579 if (mSupportsTextureFilterAnisotropy)
580 {
581 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
582 }
583}
584
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000585void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000586{
587 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
588 int d3dSampler = index + d3dSamplerOffset;
589 IDirect3DBaseTexture9 *d3dTexture = NULL;
590
591 if (texture)
592 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000593 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000594 if (texStorage)
595 {
596 d3dTexture = texStorage->getBaseTexture();
597 }
598 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000599 // in the texture class and we're unexpectedly missing the d3d texture
600 ASSERT(d3dTexture != NULL);
601 }
602
603 mDevice->SetTexture(d3dSampler, d3dTexture);
604}
605
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000606void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize)
607{
608 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
609 bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize;
610
611 if (rasterStateChanged)
612 {
613 // Set the cull mode
614 if (rasterState.cullFace)
615 {
616 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
617 }
618 else
619 {
620 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
621 }
622
623 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
624
625 mCurRasterState = rasterState;
626 }
627
628 if (rasterStateChanged || depthSizeChanged)
629 {
630 if (rasterState.polygonOffsetFill)
631 {
632 if (depthSize > 0)
633 {
634 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
635
636 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
637 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
638 }
639 }
640 else
641 {
642 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
643 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
644 }
645
646 mCurDepthSize = depthSize;
647 }
648
649 mForceSetRasterState = false;
650}
651
652void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
653{
654 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
655 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
656 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
657
658 if (blendStateChanged || blendColorChanged)
659 {
660 if (blendState.blend)
661 {
662 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
663
664 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
665 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
666 {
667 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
668 }
669 else
670 {
671 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
672 gl::unorm<8>(blendColor.alpha),
673 gl::unorm<8>(blendColor.alpha),
674 gl::unorm<8>(blendColor.alpha)));
675 }
676
677 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
678 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
679 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
680
681 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
682 blendState.destBlendRGB != blendState.destBlendAlpha ||
683 blendState.blendEquationRGB != blendState.blendEquationAlpha)
684 {
685 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
686
687 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
688 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
689 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
690 }
691 else
692 {
693 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
694 }
695 }
696 else
697 {
698 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
699 }
700
701 if (blendState.sampleAlphaToCoverage)
702 {
703 FIXME("Sample alpha to coverage is unimplemented.");
704 }
705
706 // Set the color mask
707 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
708 // Apparently some ATI cards have a bug where a draw with a zero color
709 // write mask can cause later draws to have incorrect results. Instead,
710 // set a nonzero color write mask but modify the blend state so that no
711 // drawing is done.
712 // http://code.google.com/p/angleproject/issues/detail?id=169
713
714 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
715 blendState.colorMaskBlue, blendState.colorMaskAlpha);
716 if (colorMask == 0 && !zeroColorMaskAllowed)
717 {
718 // Enable green channel, but set blending so nothing will be drawn.
719 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
720 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
721
722 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
723 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
724 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
725 }
726 else
727 {
728 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
729 }
730
731 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
732
733 mCurBlendState = blendState;
734 mCurBlendColor = blendColor;
735 }
736
737 if (sampleMaskChanged)
738 {
739 // Set the multisample mask
740 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
741 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
742
743 mCurSampleMask = sampleMask;
744 }
745
746 mForceSetBlendState = false;
747}
748
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000749void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
750 int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000751{
752 bool depthStencilStateChanged = mForceSetDepthStencilState ||
753 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000754 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
755 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000756 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
757 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
758
759 if (depthStencilStateChanged)
760 {
761 if (depthStencilState.depthTest)
762 {
763 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
764 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
765 }
766 else
767 {
768 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
769 }
770
771 mCurDepthStencilState = depthStencilState;
772 }
773
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000774 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000775 {
776 if (depthStencilState.stencilTest && stencilSize > 0)
777 {
778 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
779 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
780
781 // FIXME: Unsupported by D3D9
782 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
783 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
784 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
785 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000786 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000787 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
788 {
789 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
790 return error(GL_INVALID_OPERATION);
791 }
792
793 // get the maximum size of the stencil ref
794 GLuint maxStencil = (1 << stencilSize) - 1;
795
796 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
797 depthStencilState.stencilWritemask);
798 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
799 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
800
801 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000802 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000803 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
804 depthStencilState.stencilMask);
805
806 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
807 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
808 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
809 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
810 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
811 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
812
813 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
814 depthStencilState.stencilBackWritemask);
815 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
816 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
817
818 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000819 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000820 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
821 depthStencilState.stencilBackMask);
822
823 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
824 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
825 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
826 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
827 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
828 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
829 }
830 else
831 {
832 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
833 }
834
835 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
836
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000837 mCurStencilRef = stencilRef;
838 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000839 mCurFrontFaceCCW = frontFaceCCW;
840 mCurStencilSize = stencilSize;
841 }
842
843 mForceSetDepthStencilState = false;
844}
845
846void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
847 unsigned int renderTargetHeight)
848{
849 bool renderTargetSizedChanged = mForceSetScissor ||
850 renderTargetWidth != mCurRenderTargetWidth ||
851 renderTargetHeight != mCurRenderTargetHeight;
852 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
853
854 if (renderTargetSizedChanged || scissorChanged)
855 {
856 RECT rect;
857 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
858 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth));
859 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
860 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth));
861 mDevice->SetScissorRect(&rect);
862
863 mCurScissor = scissor;
864 mCurRenderTargetWidth = renderTargetWidth;
865 mCurRenderTargetHeight = renderTargetHeight;
866 }
867
868 mForceSetScissor = false;
869}
870
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000871bool Renderer9::setViewport(const gl::Rectangle& viewport, float zNear, float zFar,
872 unsigned int renderTargetWidth, unsigned int renderTargetHeight,
873 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
874{
875 bool viewportChanged = mForceSetViewport || memcmp(&viewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
876 zNear != mCurNear || zFar != mCurFar;
877
878 D3DVIEWPORT9 dxViewport;
879 dxViewport.X = gl::clamp(viewport.x, 0, static_cast<int>(renderTargetWidth));
880 dxViewport.Y = gl::clamp(viewport.y, 0, static_cast<int>(renderTargetHeight));
881 dxViewport.Width = gl::clamp(viewport.width, 0, static_cast<int>(renderTargetWidth) - static_cast<int>(dxViewport.X));
882 dxViewport.Height = gl::clamp(viewport.height, 0, static_cast<int>(renderTargetHeight) - static_cast<int>(dxViewport.Y));
883 dxViewport.MinZ = zNear;
884 dxViewport.MaxZ = zFar;
885
886 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
887 {
888 return false; // Nothing to render
889 }
890
891 if (viewportChanged)
892 {
893 mDevice->SetViewport(&dxViewport);
894
895 mCurViewport = viewport;
896 mCurNear = zNear;
897 mCurFar = zFar;
898 }
899
900 if (currentProgram && (viewportChanged || forceSetUniforms))
901 {
902 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
903 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
904 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
905
906 // These values are used for computing gl_FragCoord in Program::linkVaryings().
907 GLint coord = currentProgram->getDxCoordLocation();
908 GLfloat whxy[4] = { viewport.width * 0.5f,
909 viewport.height * 0.5f,
910 viewport.x + (viewport.width * 0.5f),
911 viewport.y + (viewport.height * 0.5f) };
912 currentProgram->setUniform4fv(coord, 1, whxy);
913
914 GLint depth = currentProgram->getDxDepthLocation();
915 GLfloat dz[2] = { (zFar - zNear) * 0.5f, (zNear + zFar) * 0.5f };
916 currentProgram->setUniform2fv(depth, 1, dz);
917
918 GLint depthRange = currentProgram->getDxDepthRangeLocation();
919 GLfloat nearFarDiff[3] = { zNear, zFar, zFar - zNear };
920 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
921 }
922
923 mForceSetViewport = false;
924 return true;
925}
926
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000927void Renderer9::applyRenderTarget(gl::Framebuffer *frameBuffer)
928{
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000929 // TODO: only set these when the rendertarget actually changes
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000930 mForceSetScissor = true;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000931 mForceSetViewport = true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000932
933 // TODO
934}
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000935
daniel@transgaming.comd084c622012-11-28 19:36:05 +0000936void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear,
937 gl::Framebuffer *frameBuffer)
938{
939 mForceSetDepthStencilState = true;
940
941 // TODO
942}
943
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000944void Renderer9::markAllStateDirty()
945{
946 mForceSetDepthStencilState = true;
947 mForceSetRasterState = true;
948 mForceSetBlendState = true;
949 mForceSetScissor = true;
950 mForceSetViewport = true;
951}
952
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000953void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000954{
955 while (!mEventQueryPool.empty())
956 {
957 mEventQueryPool.back()->Release();
958 mEventQueryPool.pop_back();
959 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000960
961 mVertexShaderCache.clear();
962 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000963}
964
965
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000966void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000967{
968 mDeviceLost = true;
969}
970
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000971bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000972{
973 return mDeviceLost;
974}
975
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000976// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000977bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000978{
979 bool isLost = false;
980
981 if (mDeviceEx)
982 {
983 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
984 }
985 else if (mDevice)
986 {
987 isLost = FAILED(mDevice->TestCooperativeLevel());
988 }
989 else
990 {
991 // No device yet, so no reset required
992 }
993
994 if (isLost)
995 {
996 // ensure we note the device loss --
997 // we'll probably get this done again by markDeviceLost
998 // but best to remember it!
999 // Note that we don't want to clear the device loss status here
1000 // -- this needs to be done by resetDevice
1001 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001002 if (notify)
1003 {
1004 mDisplay->notifyDeviceLost();
1005 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001006 }
1007
1008 return isLost;
1009}
1010
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001011bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001012{
1013 HRESULT status = D3D_OK;
1014
1015 if (mDeviceEx)
1016 {
1017 status = mDeviceEx->CheckDeviceState(NULL);
1018 }
1019 else if (mDevice)
1020 {
1021 status = mDevice->TestCooperativeLevel();
1022 }
1023
1024 switch (status)
1025 {
1026 case D3DERR_DEVICENOTRESET:
1027 case D3DERR_DEVICEHUNG:
1028 return true;
1029 default:
1030 return false;
1031 }
1032}
1033
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001034bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001035{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001036 releaseDeviceResources();
1037
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001038 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1039
1040 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001041 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001042 int attempts = 3;
1043
1044 while (lost && attempts > 0)
1045 {
1046 if (mDeviceEx)
1047 {
1048 Sleep(500); // Give the graphics driver some CPU time
1049 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1050 }
1051 else
1052 {
1053 result = mDevice->TestCooperativeLevel();
1054 while (result == D3DERR_DEVICELOST)
1055 {
1056 Sleep(100); // Give the graphics driver some CPU time
1057 result = mDevice->TestCooperativeLevel();
1058 }
1059
1060 if (result == D3DERR_DEVICENOTRESET)
1061 {
1062 result = mDevice->Reset(&presentParameters);
1063 }
1064 }
1065
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001066 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001067 attempts --;
1068 }
1069
1070 if (FAILED(result))
1071 {
1072 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1073 return false;
1074 }
1075
1076 // reset device defaults
1077 initializeDevice();
1078 mDeviceLost = false;
1079
1080 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}