blob: c42548281a39bb42803b64aeeebfa27f8eef7fca [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
daniel@transgaming.com7d738a22012-11-28 19:43:08 +000068 mD3dCompilerModule = NULL;
69
daniel@transgaming.com621ce052012-10-31 17:52:29 +000070 mD3d9 = NULL;
71 mD3d9Ex = NULL;
72 mDevice = NULL;
73 mDeviceEx = NULL;
74 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000075 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000076
77 mAdapter = D3DADAPTER_DEFAULT;
78
79 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
80 mDeviceType = D3DDEVTYPE_REF;
81 #else
82 mDeviceType = D3DDEVTYPE_HAL;
83 #endif
84
85 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000086
87 mMaxSupportedSamples = 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000088}
89
daniel@transgaming.com2507f412012-10-31 18:46:48 +000090Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +000091{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000092 releaseDeviceResources();
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000093
94 delete mBlit;
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000095
daniel@transgaming.com621ce052012-10-31 17:52:29 +000096 if (mDevice)
97 {
98 // 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 +000099 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000100 {
101 resetDevice();
102 }
103
104 mDevice->Release();
105 mDevice = NULL;
106 }
107
108 if (mDeviceEx)
109 {
110 mDeviceEx->Release();
111 mDeviceEx = NULL;
112 }
113
114 if (mD3d9)
115 {
116 mD3d9->Release();
117 mD3d9 = NULL;
118 }
119
120 if (mDeviceWindow)
121 {
122 DestroyWindow(mDeviceWindow);
123 mDeviceWindow = NULL;
124 }
125
126 if (mD3d9Ex)
127 {
128 mD3d9Ex->Release();
129 mD3d9Ex = NULL;
130 }
131
132 if (mD3d9Module)
133 {
134 mD3d9Module = NULL;
135 }
136
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000137 if (mD3dCompilerModule)
138 {
139 FreeLibrary(mD3dCompilerModule);
140 mD3dCompilerModule = NULL;
141 }
142
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000143 while (!mMultiSampleSupport.empty())
144 {
145 delete [] mMultiSampleSupport.begin()->second;
146 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
147 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000148}
149
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000150EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000151{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000152 if (mSoftwareDevice)
153 {
154 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
155 }
156 else
157 {
158 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
159 }
160
161 if (mD3d9Module == NULL)
162 {
163 ERR("No D3D9 module found - aborting!\n");
164 return EGL_NOT_INITIALIZED;
165 }
166
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000167 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
168 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
169
170 // Use Direct3D9Ex if available. Among other things, this version is less
171 // inclined to report a lost context, for example when the user switches
172 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
173 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
174 {
175 ASSERT(mD3d9Ex);
176 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
177 ASSERT(mD3d9);
178 }
179 else
180 {
181 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
182 }
183
184 if (!mD3d9)
185 {
186 ERR("Could not create D3D9 device - aborting!\n");
187 return EGL_NOT_INITIALIZED;
188 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000189
190#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
191 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
192 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
193
194 for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i)
195 {
196 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
197 {
198 break;
199 }
200 }
201#else
202 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
203 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
204#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
205
206 if (!mD3dCompilerModule)
207 {
208 terminate();
209 return false;
210 }
211
212 mD3DCompileFunc = reinterpret_cast<D3DCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
213 ASSERT(mD3DCompileFunc);
214
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000215 if (mDc != NULL)
216 {
217 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
218 }
219
220 HRESULT result;
221
222 // Give up on getting device caps after about one second.
223 for (int i = 0; i < 10; ++i)
224 {
225 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
226 if (SUCCEEDED(result))
227 {
228 break;
229 }
230 else if (result == D3DERR_NOTAVAILABLE)
231 {
232 Sleep(100); // Give the driver some time to initialize/recover
233 }
234 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
235 {
236 ERR("failed to get device caps (0x%x)\n", result);
237 return EGL_NOT_INITIALIZED;
238 }
239 }
240
241 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
242 {
243 ERR("Renderer does not support PS 2.0. aborting!\n");
244 return EGL_NOT_INITIALIZED;
245 }
246
247 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
248 // This is required by Texture2D::convertToRenderTarget.
249 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
250 {
251 ERR("Renderer does not support stretctrect from textures!\n");
252 return EGL_NOT_INITIALIZED;
253 }
254
255 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
256
257 // ATI cards on XP have problems with non-power-of-two textures.
258 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
259 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
260 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
261 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
262
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000263 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
264 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
265
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000266 mMinSwapInterval = 4;
267 mMaxSwapInterval = 0;
268
269 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
270 {
271 mMinSwapInterval = std::min(mMinSwapInterval, 0);
272 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
273 }
274 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
275 {
276 mMinSwapInterval = std::min(mMinSwapInterval, 1);
277 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
278 }
279 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
280 {
281 mMinSwapInterval = std::min(mMinSwapInterval, 2);
282 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
283 }
284 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
285 {
286 mMinSwapInterval = std::min(mMinSwapInterval, 3);
287 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
288 }
289 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
290 {
291 mMinSwapInterval = std::min(mMinSwapInterval, 4);
292 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
293 }
294
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000295 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000296 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000297 {
298 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000299 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
300 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000301
302 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
303 {
304 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
305 {
306 max = j;
307 }
308 }
309 }
310
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000311 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000312 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000313 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000314 continue;
315
316 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000317 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
318 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000319
320 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
321 {
322 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
323 {
324 max = j;
325 }
326 }
327 }
328
329 mMaxSupportedSamples = max;
330
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000331 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
332 static const TCHAR className[] = TEXT("STATIC");
333
334 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
335
336 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
337 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
338
339 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
340 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
341 {
342 return EGL_BAD_ALLOC;
343 }
344
345 if (FAILED(result))
346 {
347 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
348
349 if (FAILED(result))
350 {
351 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
352 return EGL_BAD_ALLOC;
353 }
354 }
355
356 if (mD3d9Ex)
357 {
358 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
359 ASSERT(SUCCEEDED(result));
360 }
361
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000362 mVertexShaderCache.initialize(mDevice);
363 mPixelShaderCache.initialize(mDevice);
364
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000365 initializeDevice();
366
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000367 mBlit = new Blit(this);
368
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000369 return EGL_SUCCESS;
370}
371
372// do any one-time device initialization
373// NOTE: this is also needed after a device lost/reset
374// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000375void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000376{
377 // Permanent non-default states
378 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
379 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
380
381 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
382 {
383 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
384 }
385 else
386 {
387 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
388 }
389
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000390 markAllStateDirty();
391
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000392 mSceneStarted = false;
393}
394
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000395D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000396{
397 D3DPRESENT_PARAMETERS presentParameters = {0};
398
399 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
400 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
401 presentParameters.BackBufferCount = 1;
402 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
403 presentParameters.BackBufferWidth = 1;
404 presentParameters.BackBufferHeight = 1;
405 presentParameters.EnableAutoDepthStencil = FALSE;
406 presentParameters.Flags = 0;
407 presentParameters.hDeviceWindow = mDeviceWindow;
408 presentParameters.MultiSampleQuality = 0;
409 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
410 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
411 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
412 presentParameters.Windowed = TRUE;
413
414 return presentParameters;
415}
416
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000417int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000418{
419 D3DDISPLAYMODE currentDisplayMode;
420 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
421
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000422 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
423 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000424 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
425 int numConfigs = 0;
426
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000427 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000428 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000429 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000430
431 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
432
433 if (SUCCEEDED(result))
434 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000435 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000436 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000437 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000438 HRESULT result = D3D_OK;
439
440 if(depthStencilFormat != D3DFMT_UNKNOWN)
441 {
442 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
443 }
444
445 if (SUCCEEDED(result))
446 {
447 if(depthStencilFormat != D3DFMT_UNKNOWN)
448 {
449 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
450 }
451
452 if (SUCCEEDED(result))
453 {
454 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000455 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
456 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000457 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
458 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
459
460 (*configDescList)[numConfigs++] = newConfig;
461 }
462 }
463 }
464 }
465 }
466
467 return numConfigs;
468}
469
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000470void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000471{
472 delete [] (configDescList);
473}
474
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000475void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000476{
477 if (!mSceneStarted)
478 {
479 long result = mDevice->BeginScene();
480 if (SUCCEEDED(result)) {
481 // This is defensive checking against the device being
482 // lost at unexpected times.
483 mSceneStarted = true;
484 }
485 }
486}
487
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000488void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000489{
490 if (mSceneStarted)
491 {
492 // EndScene can fail if the device was lost, for example due
493 // to a TDR during a draw call.
494 mDevice->EndScene();
495 mSceneStarted = false;
496 }
497}
498
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000499// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000500void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000501{
502 HRESULT result;
503
504 IDirect3DQuery9* query = allocateEventQuery();
505 if (!query)
506 {
507 return;
508 }
509
510 result = query->Issue(D3DISSUE_END);
511 ASSERT(SUCCEEDED(result));
512
513 do
514 {
515 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
516
517 if(block && result == S_FALSE)
518 {
519 // Keep polling, but allow other threads to do something useful first
520 Sleep(0);
521 // explicitly check for device loss
522 // some drivers seem to return S_FALSE even if the device is lost
523 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000524 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000525 {
526 result = D3DERR_DEVICELOST;
527 }
528 }
529 }
530 while(block && result == S_FALSE);
531
532 freeEventQuery(query);
533
534 if (isDeviceLostError(result))
535 {
536 mDisplay->notifyDeviceLost();
537 }
538}
539
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000540SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
541{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000542 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000543}
544
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000545// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000546IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000547{
548 IDirect3DQuery9 *query = NULL;
549
550 if (mEventQueryPool.empty())
551 {
552 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
553 ASSERT(SUCCEEDED(result));
554 }
555 else
556 {
557 query = mEventQueryPool.back();
558 mEventQueryPool.pop_back();
559 }
560
561 return query;
562}
563
564// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000565void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000566{
567 if (mEventQueryPool.size() > 1000)
568 {
569 query->Release();
570 }
571 else
572 {
573 mEventQueryPool.push_back(query);
574 }
575}
576
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000577
578HRESULT Renderer9::compileShaderSource(const char* hlsl, const char* sourceName, const char* profile, DWORD flags, ID3DBlob** binary, ID3DBlob** errorMessage)
579{
580 return mD3DCompileFunc(hlsl, strlen(hlsl), sourceName, NULL, NULL, "main", profile, flags, 0, binary, errorMessage);
581}
582
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000583IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000584{
585 return mVertexShaderCache.create(function, length);
586}
587
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000588IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000589{
590 return mPixelShaderCache.create(function, length);
591}
592
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000593HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
594{
595 D3DPOOL Pool = getBufferPool(Usage);
596 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
597}
598
599HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
600{
601 D3DPOOL Pool = getBufferPool(Usage);
602 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
603}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000604
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000605void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000606{
607 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
608 int d3dSampler = index + d3dSamplerOffset;
609
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000610 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
611 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000612
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000613 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000614 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000615 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000616 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
617 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
618 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
619 if (mSupportsTextureFilterAnisotropy)
620 {
621 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
622 }
623}
624
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000625void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000626{
627 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
628 int d3dSampler = index + d3dSamplerOffset;
629 IDirect3DBaseTexture9 *d3dTexture = NULL;
630
631 if (texture)
632 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000633 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000634 if (texStorage)
635 {
636 d3dTexture = texStorage->getBaseTexture();
637 }
638 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000639 // in the texture class and we're unexpectedly missing the d3d texture
640 ASSERT(d3dTexture != NULL);
641 }
642
643 mDevice->SetTexture(d3dSampler, d3dTexture);
644}
645
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000646void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize)
647{
648 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
649 bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize;
650
651 if (rasterStateChanged)
652 {
653 // Set the cull mode
654 if (rasterState.cullFace)
655 {
656 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
657 }
658 else
659 {
660 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
661 }
662
663 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
664
665 mCurRasterState = rasterState;
666 }
667
668 if (rasterStateChanged || depthSizeChanged)
669 {
670 if (rasterState.polygonOffsetFill)
671 {
672 if (depthSize > 0)
673 {
674 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
675
676 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
677 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
678 }
679 }
680 else
681 {
682 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
683 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
684 }
685
686 mCurDepthSize = depthSize;
687 }
688
689 mForceSetRasterState = false;
690}
691
692void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
693{
694 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
695 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
696 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
697
698 if (blendStateChanged || blendColorChanged)
699 {
700 if (blendState.blend)
701 {
702 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
703
704 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
705 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
706 {
707 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
708 }
709 else
710 {
711 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
712 gl::unorm<8>(blendColor.alpha),
713 gl::unorm<8>(blendColor.alpha),
714 gl::unorm<8>(blendColor.alpha)));
715 }
716
717 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
718 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
719 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
720
721 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
722 blendState.destBlendRGB != blendState.destBlendAlpha ||
723 blendState.blendEquationRGB != blendState.blendEquationAlpha)
724 {
725 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
726
727 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
728 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
729 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
730 }
731 else
732 {
733 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
734 }
735 }
736 else
737 {
738 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
739 }
740
741 if (blendState.sampleAlphaToCoverage)
742 {
743 FIXME("Sample alpha to coverage is unimplemented.");
744 }
745
746 // Set the color mask
747 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
748 // Apparently some ATI cards have a bug where a draw with a zero color
749 // write mask can cause later draws to have incorrect results. Instead,
750 // set a nonzero color write mask but modify the blend state so that no
751 // drawing is done.
752 // http://code.google.com/p/angleproject/issues/detail?id=169
753
754 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
755 blendState.colorMaskBlue, blendState.colorMaskAlpha);
756 if (colorMask == 0 && !zeroColorMaskAllowed)
757 {
758 // Enable green channel, but set blending so nothing will be drawn.
759 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
760 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
761
762 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
763 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
764 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
765 }
766 else
767 {
768 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
769 }
770
771 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
772
773 mCurBlendState = blendState;
774 mCurBlendColor = blendColor;
775 }
776
777 if (sampleMaskChanged)
778 {
779 // Set the multisample mask
780 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
781 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
782
783 mCurSampleMask = sampleMask;
784 }
785
786 mForceSetBlendState = false;
787}
788
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000789void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
790 int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000791{
792 bool depthStencilStateChanged = mForceSetDepthStencilState ||
793 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000794 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
795 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000796 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
797 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
798
799 if (depthStencilStateChanged)
800 {
801 if (depthStencilState.depthTest)
802 {
803 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
804 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
805 }
806 else
807 {
808 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
809 }
810
811 mCurDepthStencilState = depthStencilState;
812 }
813
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000814 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000815 {
816 if (depthStencilState.stencilTest && stencilSize > 0)
817 {
818 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
819 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
820
821 // FIXME: Unsupported by D3D9
822 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
824 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
825 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000826 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000827 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
828 {
829 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
830 return error(GL_INVALID_OPERATION);
831 }
832
833 // get the maximum size of the stencil ref
834 GLuint maxStencil = (1 << stencilSize) - 1;
835
836 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
837 depthStencilState.stencilWritemask);
838 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
839 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
840
841 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000842 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000843 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
844 depthStencilState.stencilMask);
845
846 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
847 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
848 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
849 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
850 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
851 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
852
853 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
854 depthStencilState.stencilBackWritemask);
855 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
856 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
857
858 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000859 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000860 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
861 depthStencilState.stencilBackMask);
862
863 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
864 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
865 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
866 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
867 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
868 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
869 }
870 else
871 {
872 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
873 }
874
875 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
876
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000877 mCurStencilRef = stencilRef;
878 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000879 mCurFrontFaceCCW = frontFaceCCW;
880 mCurStencilSize = stencilSize;
881 }
882
883 mForceSetDepthStencilState = false;
884}
885
886void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
887 unsigned int renderTargetHeight)
888{
889 bool renderTargetSizedChanged = mForceSetScissor ||
890 renderTargetWidth != mCurRenderTargetWidth ||
891 renderTargetHeight != mCurRenderTargetHeight;
892 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
893
894 if (renderTargetSizedChanged || scissorChanged)
895 {
896 RECT rect;
897 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
898 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetWidth));
899 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
900 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetWidth));
901 mDevice->SetScissorRect(&rect);
902
903 mCurScissor = scissor;
904 mCurRenderTargetWidth = renderTargetWidth;
905 mCurRenderTargetHeight = renderTargetHeight;
906 }
907
908 mForceSetScissor = false;
909}
910
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000911bool Renderer9::setViewport(const gl::Rectangle& viewport, float zNear, float zFar,
912 unsigned int renderTargetWidth, unsigned int renderTargetHeight,
913 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
914{
915 bool viewportChanged = mForceSetViewport || memcmp(&viewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
916 zNear != mCurNear || zFar != mCurFar;
917
918 D3DVIEWPORT9 dxViewport;
919 dxViewport.X = gl::clamp(viewport.x, 0, static_cast<int>(renderTargetWidth));
920 dxViewport.Y = gl::clamp(viewport.y, 0, static_cast<int>(renderTargetHeight));
921 dxViewport.Width = gl::clamp(viewport.width, 0, static_cast<int>(renderTargetWidth) - static_cast<int>(dxViewport.X));
922 dxViewport.Height = gl::clamp(viewport.height, 0, static_cast<int>(renderTargetHeight) - static_cast<int>(dxViewport.Y));
923 dxViewport.MinZ = zNear;
924 dxViewport.MaxZ = zFar;
925
926 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
927 {
928 return false; // Nothing to render
929 }
930
931 if (viewportChanged)
932 {
933 mDevice->SetViewport(&dxViewport);
934
935 mCurViewport = viewport;
936 mCurNear = zNear;
937 mCurFar = zFar;
938 }
939
940 if (currentProgram && (viewportChanged || forceSetUniforms))
941 {
942 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
943 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
944 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
945
946 // These values are used for computing gl_FragCoord in Program::linkVaryings().
947 GLint coord = currentProgram->getDxCoordLocation();
948 GLfloat whxy[4] = { viewport.width * 0.5f,
949 viewport.height * 0.5f,
950 viewport.x + (viewport.width * 0.5f),
951 viewport.y + (viewport.height * 0.5f) };
952 currentProgram->setUniform4fv(coord, 1, whxy);
953
954 GLint depth = currentProgram->getDxDepthLocation();
955 GLfloat dz[2] = { (zFar - zNear) * 0.5f, (zNear + zFar) * 0.5f };
956 currentProgram->setUniform2fv(depth, 1, dz);
957
958 GLint depthRange = currentProgram->getDxDepthRangeLocation();
959 GLfloat nearFarDiff[3] = { zNear, zFar, zFar - zNear };
960 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
961 }
962
963 mForceSetViewport = false;
964 return true;
965}
966
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000967bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000968{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +0000969 // if there is no color attachment we must synthesize a NULL colorattachment
970 // to keep the D3D runtime happy. This should only be possible if depth texturing.
971 gl::Renderbuffer *renderbufferObject = NULL;
972 if (framebuffer->getColorbufferType() != GL_NONE)
973 {
974 renderbufferObject = framebuffer->getColorbuffer();
975 }
976 else
977 {
978 renderbufferObject = framebuffer->getNullColorbuffer();
979 }
980 if (!renderbufferObject)
981 {
982 ERR("unable to locate renderbuffer for FBO.");
983 return false;
984 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000985
daniel@transgaming.com220e79a2012-11-28 19:42:11 +0000986 bool renderTargetChanged = false;
987 unsigned int renderTargetSerial = renderbufferObject->getSerial();
988 if (renderTargetSerial != mAppliedRenderTargetSerial)
989 {
990 // Apply the render target on the device
991 IDirect3DSurface9 *renderTargetSurface = NULL;
992
993 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
994 if (renderTarget)
995 {
996 renderTargetSurface = renderTarget->getSurface();
997 }
998
999 if (!renderTargetSurface)
1000 {
1001 ERR("render target pointer unexpectedly null.");
1002 return false; // Context must be lost
1003 }
1004
1005 mDevice->SetRenderTarget(0, renderTargetSurface);
1006 renderTargetSurface->Release();
1007
1008 mAppliedRenderTargetSerial = renderTargetSerial;
1009 renderTargetChanged = true;
1010 }
1011
1012 gl::Renderbuffer *depthStencil = NULL;
1013 unsigned int depthbufferSerial = 0;
1014 unsigned int stencilbufferSerial = 0;
1015 if (framebuffer->getDepthbufferType() != GL_NONE)
1016 {
1017 depthStencil = framebuffer->getDepthbuffer();
1018 if (!depthStencil)
1019 {
1020 ERR("Depth stencil pointer unexpectedly null.");
1021 return false;
1022 }
1023
1024 depthbufferSerial = depthStencil->getSerial();
1025 }
1026 else if (framebuffer->getStencilbufferType() != GL_NONE)
1027 {
1028 depthStencil = framebuffer->getStencilbuffer();
1029 if (!depthStencil)
1030 {
1031 ERR("Depth stencil pointer unexpectedly null.");
1032 return false;
1033 }
1034
1035 stencilbufferSerial = depthStencil->getSerial();
1036 }
1037
1038 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1039 stencilbufferSerial != mAppliedStencilbufferSerial ||
1040 !mDepthStencilInitialized)
1041 {
1042 // Apply the depth stencil on the device
1043 if (depthStencil)
1044 {
1045 IDirect3DSurface9 *depthStencilSurface = NULL;
1046 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1047
1048 if (depthStencilRenderTarget)
1049 {
1050 depthStencilSurface = depthStencilRenderTarget->getSurface();
1051 }
1052
1053 if (!depthStencilSurface)
1054 {
1055 ERR("depth stencil pointer unexpectedly null.");
1056 return false; // Context must be lost
1057 }
1058
1059 mDevice->SetDepthStencilSurface(depthStencilSurface);
1060 depthStencilSurface->Release();
1061 }
1062 else
1063 {
1064 mDevice->SetDepthStencilSurface(NULL);
1065 }
1066
1067 mAppliedDepthbufferSerial = depthbufferSerial;
1068 mAppliedStencilbufferSerial = stencilbufferSerial;
1069 mDepthStencilInitialized = true;
1070 }
1071
1072 if (renderTargetChanged || !mRenderTargetDescInitialized)
1073 {
1074 mForceSetScissor = true;
1075 mForceSetViewport = true;
1076
1077 mRenderTargetDesc.width = renderbufferObject->getWidth();
1078 mRenderTargetDesc.height = renderbufferObject->getHeight();
1079 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1080 mRenderTargetDescInitialized = true;
1081 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001082
1083 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001084}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001085
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001086void Renderer9::clear(GLbitfield mask, const gl::Color &colorClear, float depthClear, int stencilClear,
1087 gl::Framebuffer *frameBuffer)
1088{
1089 mForceSetDepthStencilState = true;
1090
1091 // TODO
1092}
1093
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001094void Renderer9::markAllStateDirty()
1095{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001096 mAppliedRenderTargetSerial = 0;
1097 mAppliedDepthbufferSerial = 0;
1098 mAppliedStencilbufferSerial = 0;
1099 mDepthStencilInitialized = false;
1100 mRenderTargetDescInitialized = false;
1101
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001102 mForceSetDepthStencilState = true;
1103 mForceSetRasterState = true;
1104 mForceSetBlendState = true;
1105 mForceSetScissor = true;
1106 mForceSetViewport = true;
1107}
1108
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001109void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001110{
1111 while (!mEventQueryPool.empty())
1112 {
1113 mEventQueryPool.back()->Release();
1114 mEventQueryPool.pop_back();
1115 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001116
1117 mVertexShaderCache.clear();
1118 mPixelShaderCache.clear();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001119}
1120
1121
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001122void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001123{
1124 mDeviceLost = true;
1125}
1126
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001127bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001128{
1129 return mDeviceLost;
1130}
1131
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001132// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001133bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001134{
1135 bool isLost = false;
1136
1137 if (mDeviceEx)
1138 {
1139 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1140 }
1141 else if (mDevice)
1142 {
1143 isLost = FAILED(mDevice->TestCooperativeLevel());
1144 }
1145 else
1146 {
1147 // No device yet, so no reset required
1148 }
1149
1150 if (isLost)
1151 {
1152 // ensure we note the device loss --
1153 // we'll probably get this done again by markDeviceLost
1154 // but best to remember it!
1155 // Note that we don't want to clear the device loss status here
1156 // -- this needs to be done by resetDevice
1157 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001158 if (notify)
1159 {
1160 mDisplay->notifyDeviceLost();
1161 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001162 }
1163
1164 return isLost;
1165}
1166
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001167bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001168{
1169 HRESULT status = D3D_OK;
1170
1171 if (mDeviceEx)
1172 {
1173 status = mDeviceEx->CheckDeviceState(NULL);
1174 }
1175 else if (mDevice)
1176 {
1177 status = mDevice->TestCooperativeLevel();
1178 }
1179
1180 switch (status)
1181 {
1182 case D3DERR_DEVICENOTRESET:
1183 case D3DERR_DEVICEHUNG:
1184 return true;
1185 default:
1186 return false;
1187 }
1188}
1189
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001190bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001191{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001192 releaseDeviceResources();
1193
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001194 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1195
1196 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001197 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001198 int attempts = 3;
1199
1200 while (lost && attempts > 0)
1201 {
1202 if (mDeviceEx)
1203 {
1204 Sleep(500); // Give the graphics driver some CPU time
1205 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1206 }
1207 else
1208 {
1209 result = mDevice->TestCooperativeLevel();
1210 while (result == D3DERR_DEVICELOST)
1211 {
1212 Sleep(100); // Give the graphics driver some CPU time
1213 result = mDevice->TestCooperativeLevel();
1214 }
1215
1216 if (result == D3DERR_DEVICENOTRESET)
1217 {
1218 result = mDevice->Reset(&presentParameters);
1219 }
1220 }
1221
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001222 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001223 attempts --;
1224 }
1225
1226 if (FAILED(result))
1227 {
1228 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1229 return false;
1230 }
1231
1232 // reset device defaults
1233 initializeDevice();
1234 mDeviceLost = false;
1235
1236 return true;
1237}
1238
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001239DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001240{
1241 return mAdapterIdentifier.VendorId;
1242}
1243
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001244const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001245{
1246 return mAdapterIdentifier.Description;
1247}
1248
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001249GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001250{
1251 return mAdapterIdentifier.DeviceIdentifier;
1252}
1253
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001254void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001255{
1256 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1257 {
1258 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1259 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1260
1261 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1262 }
1263}
1264
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001265bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001266{
1267 D3DDISPLAYMODE currentDisplayMode;
1268 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1269
1270 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1271}
1272
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001273bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001274{
1275 D3DDISPLAYMODE currentDisplayMode;
1276 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1277
1278 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1279}
1280
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001281bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001282{
1283 D3DDISPLAYMODE currentDisplayMode;
1284 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1285
1286 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1287}
1288
1289// we use INTZ for depth textures in Direct3D9
1290// we also want NULL texture support to ensure the we can make depth-only FBOs
1291// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001292bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001293{
1294 D3DDISPLAYMODE currentDisplayMode;
1295 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1296
1297 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1298 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1299 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1300 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1301
1302 return intz && null;
1303}
1304
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001305bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001306{
1307 D3DDISPLAYMODE currentDisplayMode;
1308 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1309
1310 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1311 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1312 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1313 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1314
1315 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1316 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1317 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1318 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1319
1320 if (!*filtering && !*renderable)
1321 {
1322 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1323 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1324 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1325 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1326 }
1327 else
1328 {
1329 return true;
1330 }
1331}
1332
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001333bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001334{
1335 D3DDISPLAYMODE currentDisplayMode;
1336 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1337
1338 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1339 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1340 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1341 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1342
1343 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1344 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1345 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1346 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1347
1348 if (!*filtering && !*renderable)
1349 {
1350 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1351 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1352 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1353 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1354 }
1355 else
1356 {
1357 return true;
1358 }
1359}
1360
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001361bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001362{
1363 D3DDISPLAYMODE currentDisplayMode;
1364 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1365
1366 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1367}
1368
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001369bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001370{
1371 D3DDISPLAYMODE currentDisplayMode;
1372 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1373
1374 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1375}
1376
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001377bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001378{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001379 return mSupportsTextureFilterAnisotropy;
1380}
1381
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001382float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001383{
1384 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001385 {
1386 return mDeviceCaps.MaxAnisotropy;
1387 }
1388 return 1.0f;
1389}
1390
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001391bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001392{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001393 IDirect3DQuery9 *query = allocateEventQuery();
1394 if (query)
1395 {
1396 freeEventQuery(query);
1397 return true;
1398 }
1399 else
1400 {
1401 return false;
1402 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001403 return true;
1404}
1405
1406// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1407// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001408bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001409{
1410 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1411 {
1412 return false;
1413 }
1414
1415 D3DDISPLAYMODE currentDisplayMode;
1416 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1417
1418 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1419
1420 return SUCCEEDED(result);
1421}
1422
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001423bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001424{
1425 return mSupportsNonPower2Textures;
1426}
1427
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001428bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001429{
1430 if (!mDevice)
1431 {
1432 return false;
1433 }
1434
1435 IDirect3DQuery9 *query = NULL;
1436 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1437 if (SUCCEEDED(result) && query)
1438 {
1439 query->Release();
1440 return true;
1441 }
1442 else
1443 {
1444 return false;
1445 }
1446}
1447
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001448bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001449{
1450 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1451}
1452
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001453bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001454{
1455 // PIX doesn't seem to support using share handles, so disable them.
1456 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001457 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001458}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001459
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001460bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001461{
1462 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1463}
1464
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001465float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001466{
1467 return mDeviceCaps.MaxPointSize;
1468}
1469
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001470int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001471{
1472 return (int)mDeviceCaps.MaxTextureWidth;
1473}
1474
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001475int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001476{
1477 return (int)mDeviceCaps.MaxTextureHeight;
1478}
1479
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001480bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001481{
1482 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1483}
1484
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001485DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001486{
1487 return mDeviceCaps.DeclTypes;
1488}
1489
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001490int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001491{
1492 return mMinSwapInterval;
1493}
1494
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001495int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001496{
1497 return mMaxSwapInterval;
1498}
1499
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001500int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001501{
1502 return mMaxSupportedSamples;
1503}
1504
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001505int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001506{
1507 if (requested == 0)
1508 {
1509 return requested;
1510 }
1511
1512 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
1513 if (itr == mMultiSampleSupport.end())
1514 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00001515 if (format == D3DFMT_UNKNOWN)
1516 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001517 return -1;
1518 }
1519
1520 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
1521 {
1522 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
1523 {
1524 return i;
1525 }
1526 }
1527
1528 return -1;
1529}
1530
daniel@transgaming.coma9571682012-11-28 19:33:08 +00001531D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
1532{
1533 switch (internalformat)
1534 {
1535 case GL_DEPTH_COMPONENT16:
1536 case GL_DEPTH_COMPONENT32_OES:
1537 case GL_DEPTH24_STENCIL8_OES:
1538 return D3DFMT_INTZ;
1539 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1540 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1541 return D3DFMT_DXT1;
1542 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1543 return D3DFMT_DXT3;
1544 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1545 return D3DFMT_DXT5;
1546 case GL_RGBA32F_EXT:
1547 case GL_RGB32F_EXT:
1548 case GL_ALPHA32F_EXT:
1549 case GL_LUMINANCE32F_EXT:
1550 case GL_LUMINANCE_ALPHA32F_EXT:
1551 return D3DFMT_A32B32G32R32F;
1552 case GL_RGBA16F_EXT:
1553 case GL_RGB16F_EXT:
1554 case GL_ALPHA16F_EXT:
1555 case GL_LUMINANCE16F_EXT:
1556 case GL_LUMINANCE_ALPHA16F_EXT:
1557 return D3DFMT_A16B16G16R16F;
1558 case GL_LUMINANCE8_EXT:
1559 if (getLuminanceTextureSupport())
1560 {
1561 return D3DFMT_L8;
1562 }
1563 break;
1564 case GL_LUMINANCE8_ALPHA8_EXT:
1565 if (getLuminanceAlphaTextureSupport())
1566 {
1567 return D3DFMT_A8L8;
1568 }
1569 break;
1570 case GL_RGB8_OES:
1571 case GL_RGB565:
1572 return D3DFMT_X8R8G8B8;
1573 }
1574
1575 return D3DFMT_A8R8G8B8;
1576}
1577
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001578bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001579{
1580 bool result = false;
1581
1582 if (source && dest)
1583 {
1584 int levels = source->levelCount();
1585 for (int i = 0; i < levels; ++i)
1586 {
1587 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
1588 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
1589
1590 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1591
1592 if (srcSurf) srcSurf->Release();
1593 if (dstSurf) dstSurf->Release();
1594
1595 if (!result)
1596 return false;
1597 }
1598 }
1599
1600 return result;
1601}
1602
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001603bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001604{
1605 bool result = false;
1606
1607 if (source && dest)
1608 {
1609 int levels = source->levelCount();
1610 for (int f = 0; f < 6; f++)
1611 {
1612 for (int i = 0; i < levels; i++)
1613 {
1614 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
1615 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
1616
1617 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1618
1619 if (srcSurf) srcSurf->Release();
1620 if (dstSurf) dstSurf->Release();
1621
1622 if (!result)
1623 return false;
1624 }
1625 }
1626 }
1627
1628 return result;
1629}
1630
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001631D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001632{
1633 if (mD3d9Ex != NULL)
1634 {
1635 return D3DPOOL_DEFAULT;
1636 }
1637 else
1638 {
1639 if (!(usage & D3DUSAGE_DYNAMIC))
1640 {
1641 return D3DPOOL_MANAGED;
1642 }
1643 }
1644
1645 return D3DPOOL_DEFAULT;
1646}
1647
daniel@transgaming.com38380882012-11-28 19:36:39 +00001648bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1649 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001650{
1651 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
1652}
1653
daniel@transgaming.com38380882012-11-28 19:36:39 +00001654bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1655 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001656{
1657 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
1658}
1659
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001660bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
1661 bool blitRenderTarget, bool blitDepthStencil)
1662{
1663 endScene();
1664
1665 if (blitRenderTarget)
1666 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001667 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
1668 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
1669 RenderTarget9 *readRenderTarget = NULL;
1670 RenderTarget9 *drawRenderTarget = NULL;
1671 IDirect3DSurface9* readSurface = NULL;
1672 IDirect3DSurface9* drawSurface = NULL;
1673
1674 if (readBuffer)
1675 {
1676 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
1677 }
1678 if (drawBuffer)
1679 {
1680 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
1681 }
1682
1683 if (readRenderTarget)
1684 {
1685 readSurface = readRenderTarget->getSurface();
1686 }
1687 if (drawRenderTarget)
1688 {
1689 drawSurface = drawRenderTarget->getSurface();
1690 }
1691
1692 if (!readSurface || !drawSurface)
1693 {
1694 ERR("Failed to retrieve the render target.");
1695 return error(GL_OUT_OF_MEMORY, false);
1696 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001697
1698 RECT srcRect, dstRect;
1699 RECT *srcRectPtr = NULL;
1700 RECT *dstRectPtr = NULL;
1701
1702 if (readRect)
1703 {
1704 srcRect.left = readRect->x;
1705 srcRect.right = readRect->x + readRect->width;
1706 srcRect.top = readRect->y;
1707 srcRect.bottom = readRect->y + readRect->height;
1708 srcRectPtr = &srcRect;
1709 }
1710
1711 if (drawRect)
1712 {
1713 dstRect.left = drawRect->x;
1714 dstRect.right = drawRect->x + drawRect->width;
1715 dstRect.top = drawRect->y;
1716 dstRect.bottom = drawRect->y + drawRect->height;
1717 dstRectPtr = &dstRect;
1718 }
1719
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001720 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001721
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001722 readSurface->Release();
1723 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001724
1725 if (FAILED(result))
1726 {
1727 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
1728 return false;
1729 }
1730 }
1731
1732 if (blitDepthStencil)
1733 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001734 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
1735 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
1736 RenderTarget9 *readDepthStencil = NULL;
1737 RenderTarget9 *drawDepthStencil = NULL;
1738 IDirect3DSurface9* readSurface = NULL;
1739 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001740
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001741 if (readBuffer)
1742 {
1743 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
1744 }
1745 if (drawBuffer)
1746 {
1747 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
1748 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001749
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001750 if (readDepthStencil)
1751 {
1752 readSurface = readDepthStencil->getSurface();
1753 }
1754 if (drawDepthStencil)
1755 {
1756 drawSurface = drawDepthStencil->getSurface();
1757 }
1758
1759 if (!readSurface || !drawSurface)
1760 {
1761 ERR("Failed to retrieve the render target.");
1762 return error(GL_OUT_OF_MEMORY, false);
1763 }
1764
1765 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
1766
1767 readSurface->Release();
1768 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001769
1770 if (FAILED(result))
1771 {
1772 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
1773 return false;
1774 }
1775 }
1776
1777 return true;
1778}
1779
1780void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
1781 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
1782{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001783 RenderTarget9 *renderTarget = NULL;
1784 IDirect3DSurface9 *surface = NULL;
1785 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1786
1787 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001788 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001789 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
1790 }
1791
1792 if (renderTarget)
1793 {
1794 surface = renderTarget->getSurface();
1795 }
1796
1797 if (!surface)
1798 {
1799 // context must be lost
1800 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001801 }
1802
1803 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001804 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001805
1806 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1807 {
1808 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001809 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001810 return error(GL_OUT_OF_MEMORY);
1811 }
1812
1813 HRESULT result;
1814 IDirect3DSurface9 *systemSurface = NULL;
1815 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
1816 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
1817 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
1818 if (directToPixels)
1819 {
1820 // Use the pixels ptr as a shared handle to write directly into client's memory
1821 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
1822 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
1823 if (FAILED(result))
1824 {
1825 // Try again without the shared handle
1826 directToPixels = false;
1827 }
1828 }
1829
1830 if (!directToPixels)
1831 {
1832 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
1833 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1834 if (FAILED(result))
1835 {
1836 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001837 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001838 return error(GL_OUT_OF_MEMORY);
1839 }
1840 }
1841
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001842 result = mDevice->GetRenderTargetData(surface, systemSurface);
1843 surface->Release();
1844 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001845
1846 if (FAILED(result))
1847 {
1848 systemSurface->Release();
1849
1850 // It turns out that D3D will sometimes produce more error
1851 // codes than those documented.
1852 if (gl::checkDeviceLost(result))
1853 return error(GL_OUT_OF_MEMORY);
1854 else
1855 {
1856 UNREACHABLE();
1857 return;
1858 }
1859
1860 }
1861
1862 if (directToPixels)
1863 {
1864 systemSurface->Release();
1865 return;
1866 }
1867
1868 RECT rect;
1869 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
1870 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
1871 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
1872 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
1873
1874 D3DLOCKED_RECT lock;
1875 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1876
1877 if (FAILED(result))
1878 {
1879 UNREACHABLE();
1880 systemSurface->Release();
1881
1882 return; // No sensible error to generate
1883 }
1884
1885 unsigned char *dest = (unsigned char*)pixels;
1886 unsigned short *dest16 = (unsigned short*)pixels;
1887
1888 unsigned char *source;
1889 int inputPitch;
1890 if (packReverseRowOrder)
1891 {
1892 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
1893 inputPitch = -lock.Pitch;
1894 }
1895 else
1896 {
1897 source = (unsigned char*)lock.pBits;
1898 inputPitch = lock.Pitch;
1899 }
1900
1901 unsigned int fastPixelSize = 0;
1902
1903 if (desc.Format == D3DFMT_A8R8G8B8 &&
1904 format == GL_BGRA_EXT &&
1905 type == GL_UNSIGNED_BYTE)
1906 {
1907 fastPixelSize = 4;
1908 }
1909 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
1910 format == GL_BGRA_EXT &&
1911 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
1912 (desc.Format == D3DFMT_A1R5G5B5 &&
1913 format == GL_BGRA_EXT &&
1914 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
1915 {
1916 fastPixelSize = 2;
1917 }
1918 else if (desc.Format == D3DFMT_A16B16G16R16F &&
1919 format == GL_RGBA &&
1920 type == GL_HALF_FLOAT_OES)
1921 {
1922 fastPixelSize = 8;
1923 }
1924 else if (desc.Format == D3DFMT_A32B32G32R32F &&
1925 format == GL_RGBA &&
1926 type == GL_FLOAT)
1927 {
1928 fastPixelSize = 16;
1929 }
1930
1931 for (int j = 0; j < rect.bottom - rect.top; j++)
1932 {
1933 if (fastPixelSize != 0)
1934 {
1935 // Fast path for formats which require no translation:
1936 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
1937 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
1938 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
1939 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
1940 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
1941 //
1942 // Note that buffers with no alpha go through the slow path below.
1943 memcpy(dest + j * outputPitch,
1944 source + j * inputPitch,
1945 (rect.right - rect.left) * fastPixelSize);
1946 continue;
1947 }
1948
1949 for (int i = 0; i < rect.right - rect.left; i++)
1950 {
1951 float r;
1952 float g;
1953 float b;
1954 float a;
1955
1956 switch (desc.Format)
1957 {
1958 case D3DFMT_R5G6B5:
1959 {
1960 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
1961
1962 a = 1.0f;
1963 b = (rgb & 0x001F) * (1.0f / 0x001F);
1964 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1965 r = (rgb & 0xF800) * (1.0f / 0xF800);
1966 }
1967 break;
1968 case D3DFMT_A1R5G5B5:
1969 {
1970 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
1971
1972 a = (argb & 0x8000) ? 1.0f : 0.0f;
1973 b = (argb & 0x001F) * (1.0f / 0x001F);
1974 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1975 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1976 }
1977 break;
1978 case D3DFMT_A8R8G8B8:
1979 {
1980 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
1981
1982 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1983 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1984 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1985 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1986 }
1987 break;
1988 case D3DFMT_X8R8G8B8:
1989 {
1990 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
1991
1992 a = 1.0f;
1993 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1994 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1995 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1996 }
1997 break;
1998 case D3DFMT_A2R10G10B10:
1999 {
2000 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2001
2002 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2003 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2004 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2005 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2006 }
2007 break;
2008 case D3DFMT_A32B32G32R32F:
2009 {
2010 // float formats in D3D are stored rgba, rather than the other way round
2011 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2012 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2013 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2014 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2015 }
2016 break;
2017 case D3DFMT_A16B16G16R16F:
2018 {
2019 // float formats in D3D are stored rgba, rather than the other way round
2020 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2021 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2022 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2023 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2024 }
2025 break;
2026 default:
2027 UNIMPLEMENTED(); // FIXME
2028 UNREACHABLE();
2029 return;
2030 }
2031
2032 switch (format)
2033 {
2034 case GL_RGBA:
2035 switch (type)
2036 {
2037 case GL_UNSIGNED_BYTE:
2038 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2039 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2040 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2041 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2042 break;
2043 default: UNREACHABLE();
2044 }
2045 break;
2046 case GL_BGRA_EXT:
2047 switch (type)
2048 {
2049 case GL_UNSIGNED_BYTE:
2050 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2051 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2052 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2053 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2054 break;
2055 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2056 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2057 // this type is packed as follows:
2058 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2059 // --------------------------------------------------------------------------------
2060 // | 4th | 3rd | 2nd | 1st component |
2061 // --------------------------------------------------------------------------------
2062 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2063 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2064 ((unsigned short)(15 * a + 0.5f) << 12)|
2065 ((unsigned short)(15 * r + 0.5f) << 8) |
2066 ((unsigned short)(15 * g + 0.5f) << 4) |
2067 ((unsigned short)(15 * b + 0.5f) << 0);
2068 break;
2069 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2070 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2071 // this type is packed as follows:
2072 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2073 // --------------------------------------------------------------------------------
2074 // | 4th | 3rd | 2nd | 1st component |
2075 // --------------------------------------------------------------------------------
2076 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2077 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2078 ((unsigned short)( a + 0.5f) << 15) |
2079 ((unsigned short)(31 * r + 0.5f) << 10) |
2080 ((unsigned short)(31 * g + 0.5f) << 5) |
2081 ((unsigned short)(31 * b + 0.5f) << 0);
2082 break;
2083 default: UNREACHABLE();
2084 }
2085 break;
2086 case GL_RGB:
2087 switch (type)
2088 {
2089 case GL_UNSIGNED_SHORT_5_6_5:
2090 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2091 ((unsigned short)(31 * b + 0.5f) << 0) |
2092 ((unsigned short)(63 * g + 0.5f) << 5) |
2093 ((unsigned short)(31 * r + 0.5f) << 11);
2094 break;
2095 case GL_UNSIGNED_BYTE:
2096 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2097 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2098 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2099 break;
2100 default: UNREACHABLE();
2101 }
2102 break;
2103 default: UNREACHABLE();
2104 }
2105 }
2106 }
2107
2108 systemSurface->UnlockRect();
2109
2110 systemSurface->Release();
2111}
2112
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002113bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2114{
2115 return mBlit->boxFilter(source, dest);
2116}
2117
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002118D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002119{
2120 if (mD3d9Ex != NULL)
2121 {
2122 return D3DPOOL_DEFAULT;
2123 }
2124 else
2125 {
2126 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2127 {
2128 return D3DPOOL_MANAGED;
2129 }
2130 }
2131
2132 return D3DPOOL_DEFAULT;
2133}
2134
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002135bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2136{
2137 if (source && dest)
2138 {
2139 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2140 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2141
2142 if (fromManaged)
2143 {
2144 D3DSURFACE_DESC desc;
2145 source->GetDesc(&desc);
2146
2147 IDirect3DSurface9 *surf = 0;
2148 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2149
2150 if (SUCCEEDED(result))
2151 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002152 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002153 result = device->UpdateSurface(surf, NULL, dest, NULL);
2154 surf->Release();
2155 }
2156 }
2157 else
2158 {
2159 endScene();
2160 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2161 }
2162
2163 if (FAILED(result))
2164 {
2165 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2166 return false;
2167 }
2168 }
2169
2170 return true;
2171}
2172
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002173}