blob: 7469c0404baadb0cb62b54ace849db6c3c6578ae [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.com67094ee2012-11-28 20:53:04 +000016#include "libGLESv2/VertexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000017#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000018#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000019#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000020#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +000021#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000022#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000023#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000024
daniel@transgaming.com3281f972012-10-31 18:38:51 +000025#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000026#include "libEGL/Display.h"
27
daniel@transgaming.com621ce052012-10-31 17:52:29 +000028// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
29#define REF_RAST 0
30
31// The "Debug This Pixel..." feature in PIX often fails when using the
32// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
33// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
34#if !defined(ANGLE_ENABLE_D3D9EX)
35// Enables use of the IDirect3D9Ex interface, when available
36#define ANGLE_ENABLE_D3D9EX 1
37#endif // !defined(ANGLE_ENABLE_D3D9EX)
38
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000039namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000040{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000041static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000042 {
43 D3DFMT_A1R5G5B5,
44 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
45 D3DFMT_A8R8G8B8,
46 D3DFMT_R5G6B5,
47 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
48 D3DFMT_X8R8G8B8
49 };
50
daniel@transgaming.com222ee082012-11-28 19:31:49 +000051static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000052 {
53 D3DFMT_UNKNOWN,
54 // D3DFMT_D16_LOCKABLE,
55 D3DFMT_D32,
56 // D3DFMT_D15S1,
57 D3DFMT_D24S8,
58 D3DFMT_D24X8,
59 // D3DFMT_D24X4S4,
60 D3DFMT_D16,
61 // D3DFMT_D32F_LOCKABLE,
62 // D3DFMT_D24FS8
63 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000064
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000065Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000066{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000067 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000068
daniel@transgaming.com7d738a22012-11-28 19:43:08 +000069 mD3dCompilerModule = NULL;
70
daniel@transgaming.com621ce052012-10-31 17:52:29 +000071 mD3d9 = NULL;
72 mD3d9Ex = NULL;
73 mDevice = NULL;
74 mDeviceEx = NULL;
75 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000076 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000077
78 mAdapter = D3DADAPTER_DEFAULT;
79
80 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
81 mDeviceType = D3DDEVTYPE_REF;
82 #else
83 mDeviceType = D3DDEVTYPE_HAL;
84 #endif
85
86 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000087
88 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000089
90 mVertexDataManager = NULL;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000091
92 mMaskedClearSavedState = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000093}
94
daniel@transgaming.com2507f412012-10-31 18:46:48 +000095Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +000096{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000097 releaseDeviceResources();
98
daniel@transgaming.com621ce052012-10-31 17:52:29 +000099 if (mDevice)
100 {
101 // 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 +0000102 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000103 {
104 resetDevice();
105 }
106
107 mDevice->Release();
108 mDevice = NULL;
109 }
110
111 if (mDeviceEx)
112 {
113 mDeviceEx->Release();
114 mDeviceEx = NULL;
115 }
116
117 if (mD3d9)
118 {
119 mD3d9->Release();
120 mD3d9 = NULL;
121 }
122
123 if (mDeviceWindow)
124 {
125 DestroyWindow(mDeviceWindow);
126 mDeviceWindow = NULL;
127 }
128
129 if (mD3d9Ex)
130 {
131 mD3d9Ex->Release();
132 mD3d9Ex = NULL;
133 }
134
135 if (mD3d9Module)
136 {
137 mD3d9Module = NULL;
138 }
139
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000140 if (mD3dCompilerModule)
141 {
142 FreeLibrary(mD3dCompilerModule);
143 mD3dCompilerModule = NULL;
144 }
145
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000146 while (!mMultiSampleSupport.empty())
147 {
148 delete [] mMultiSampleSupport.begin()->second;
149 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
150 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000151}
152
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000153Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
154{
155 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
156 return static_cast<rx::Renderer9*>(renderer);
157}
158
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000159EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000160{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000161 if (mSoftwareDevice)
162 {
163 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
164 }
165 else
166 {
167 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
168 }
169
170 if (mD3d9Module == NULL)
171 {
172 ERR("No D3D9 module found - aborting!\n");
173 return EGL_NOT_INITIALIZED;
174 }
175
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000176 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
177 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
178
179 // Use Direct3D9Ex if available. Among other things, this version is less
180 // inclined to report a lost context, for example when the user switches
181 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
182 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
183 {
184 ASSERT(mD3d9Ex);
185 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
186 ASSERT(mD3d9);
187 }
188 else
189 {
190 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
191 }
192
193 if (!mD3d9)
194 {
195 ERR("Could not create D3D9 device - aborting!\n");
196 return EGL_NOT_INITIALIZED;
197 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000198
199#if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
200 // Find a D3DCompiler module that had already been loaded based on a predefined list of versions.
201 static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
202
203 for (int i = 0; i < sizeof(d3dCompilerNames) / sizeof(*d3dCompilerNames); ++i)
204 {
205 if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule))
206 {
207 break;
208 }
209 }
210#else
211 // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with.
212 mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL);
213#endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
214
215 if (!mD3dCompilerModule)
216 {
217 terminate();
218 return false;
219 }
220
221 mD3DCompileFunc = reinterpret_cast<D3DCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile"));
222 ASSERT(mD3DCompileFunc);
223
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000224 if (mDc != NULL)
225 {
226 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
227 }
228
229 HRESULT result;
230
231 // Give up on getting device caps after about one second.
232 for (int i = 0; i < 10; ++i)
233 {
234 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
235 if (SUCCEEDED(result))
236 {
237 break;
238 }
239 else if (result == D3DERR_NOTAVAILABLE)
240 {
241 Sleep(100); // Give the driver some time to initialize/recover
242 }
243 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
244 {
245 ERR("failed to get device caps (0x%x)\n", result);
246 return EGL_NOT_INITIALIZED;
247 }
248 }
249
250 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
251 {
252 ERR("Renderer does not support PS 2.0. aborting!\n");
253 return EGL_NOT_INITIALIZED;
254 }
255
256 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
257 // This is required by Texture2D::convertToRenderTarget.
258 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
259 {
260 ERR("Renderer does not support stretctrect from textures!\n");
261 return EGL_NOT_INITIALIZED;
262 }
263
264 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
265
266 // ATI cards on XP have problems with non-power-of-two textures.
267 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
268 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
269 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
270 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
271
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000272 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
273 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
274
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000275 mMinSwapInterval = 4;
276 mMaxSwapInterval = 0;
277
278 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
279 {
280 mMinSwapInterval = std::min(mMinSwapInterval, 0);
281 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
282 }
283 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
284 {
285 mMinSwapInterval = std::min(mMinSwapInterval, 1);
286 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
287 }
288 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
289 {
290 mMinSwapInterval = std::min(mMinSwapInterval, 2);
291 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
292 }
293 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
294 {
295 mMinSwapInterval = std::min(mMinSwapInterval, 3);
296 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
297 }
298 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
299 {
300 mMinSwapInterval = std::min(mMinSwapInterval, 4);
301 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
302 }
303
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000304 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000305 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000306 {
307 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000308 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
309 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000310
311 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
312 {
313 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
314 {
315 max = j;
316 }
317 }
318 }
319
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000320 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000321 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000322 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000323 continue;
324
325 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000326 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
327 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000328
329 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
330 {
331 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
332 {
333 max = j;
334 }
335 }
336 }
337
338 mMaxSupportedSamples = max;
339
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000340 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
341 static const TCHAR className[] = TEXT("STATIC");
342
343 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
344
345 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
346 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
347
348 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
349 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
350 {
351 return EGL_BAD_ALLOC;
352 }
353
354 if (FAILED(result))
355 {
356 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
357
358 if (FAILED(result))
359 {
360 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
361 return EGL_BAD_ALLOC;
362 }
363 }
364
365 if (mD3d9Ex)
366 {
367 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
368 ASSERT(SUCCEEDED(result));
369 }
370
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000371 mVertexShaderCache.initialize(mDevice);
372 mPixelShaderCache.initialize(mDevice);
373
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000374 initializeDevice();
375
376 return EGL_SUCCESS;
377}
378
379// do any one-time device initialization
380// NOTE: this is also needed after a device lost/reset
381// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000382void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000383{
384 // Permanent non-default states
385 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
386 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
387
388 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
389 {
390 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
391 }
392 else
393 {
394 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
395 }
396
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000397 markAllStateDirty();
398
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000399 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000400
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000401 ASSERT(!mBlit && !mVertexDataManager);
402 mBlit = new Blit(this);
403 mVertexDataManager = new gl::VertexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000404}
405
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000406D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000407{
408 D3DPRESENT_PARAMETERS presentParameters = {0};
409
410 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
411 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
412 presentParameters.BackBufferCount = 1;
413 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
414 presentParameters.BackBufferWidth = 1;
415 presentParameters.BackBufferHeight = 1;
416 presentParameters.EnableAutoDepthStencil = FALSE;
417 presentParameters.Flags = 0;
418 presentParameters.hDeviceWindow = mDeviceWindow;
419 presentParameters.MultiSampleQuality = 0;
420 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
421 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
422 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
423 presentParameters.Windowed = TRUE;
424
425 return presentParameters;
426}
427
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000428int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000429{
430 D3DDISPLAYMODE currentDisplayMode;
431 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
432
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
434 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000435 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
436 int numConfigs = 0;
437
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000438 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000439 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000440 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000441
442 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
443
444 if (SUCCEEDED(result))
445 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000446 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000447 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000448 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000449 HRESULT result = D3D_OK;
450
451 if(depthStencilFormat != D3DFMT_UNKNOWN)
452 {
453 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
454 }
455
456 if (SUCCEEDED(result))
457 {
458 if(depthStencilFormat != D3DFMT_UNKNOWN)
459 {
460 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
461 }
462
463 if (SUCCEEDED(result))
464 {
465 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000466 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
467 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000468 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
469 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
470
471 (*configDescList)[numConfigs++] = newConfig;
472 }
473 }
474 }
475 }
476 }
477
478 return numConfigs;
479}
480
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000481void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000482{
483 delete [] (configDescList);
484}
485
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000486void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000487{
488 if (!mSceneStarted)
489 {
490 long result = mDevice->BeginScene();
491 if (SUCCEEDED(result)) {
492 // This is defensive checking against the device being
493 // lost at unexpected times.
494 mSceneStarted = true;
495 }
496 }
497}
498
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000499void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000500{
501 if (mSceneStarted)
502 {
503 // EndScene can fail if the device was lost, for example due
504 // to a TDR during a draw call.
505 mDevice->EndScene();
506 mSceneStarted = false;
507 }
508}
509
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000510// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000511void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000512{
513 HRESULT result;
514
515 IDirect3DQuery9* query = allocateEventQuery();
516 if (!query)
517 {
518 return;
519 }
520
521 result = query->Issue(D3DISSUE_END);
522 ASSERT(SUCCEEDED(result));
523
524 do
525 {
526 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
527
528 if(block && result == S_FALSE)
529 {
530 // Keep polling, but allow other threads to do something useful first
531 Sleep(0);
532 // explicitly check for device loss
533 // some drivers seem to return S_FALSE even if the device is lost
534 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000535 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000536 {
537 result = D3DERR_DEVICELOST;
538 }
539 }
540 }
541 while(block && result == S_FALSE);
542
543 freeEventQuery(query);
544
545 if (isDeviceLostError(result))
546 {
547 mDisplay->notifyDeviceLost();
548 }
549}
550
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000551SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
552{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000553 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000554}
555
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000556// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000557IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000558{
559 IDirect3DQuery9 *query = NULL;
560
561 if (mEventQueryPool.empty())
562 {
563 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
564 ASSERT(SUCCEEDED(result));
565 }
566 else
567 {
568 query = mEventQueryPool.back();
569 mEventQueryPool.pop_back();
570 }
571
572 return query;
573}
574
575// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000576void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000577{
578 if (mEventQueryPool.size() > 1000)
579 {
580 query->Release();
581 }
582 else
583 {
584 mEventQueryPool.push_back(query);
585 }
586}
587
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000588
589HRESULT Renderer9::compileShaderSource(const char* hlsl, const char* sourceName, const char* profile, DWORD flags, ID3DBlob** binary, ID3DBlob** errorMessage)
590{
591 return mD3DCompileFunc(hlsl, strlen(hlsl), sourceName, NULL, NULL, "main", profile, flags, 0, binary, errorMessage);
592}
593
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000594IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000595{
596 return mVertexShaderCache.create(function, length);
597}
598
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000599IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000600{
601 return mPixelShaderCache.create(function, length);
602}
603
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000604HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
605{
606 D3DPOOL Pool = getBufferPool(Usage);
607 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
608}
609
610HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
611{
612 D3DPOOL Pool = getBufferPool(Usage);
613 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
614}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000615
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000616void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000617{
618 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
619 int d3dSampler = index + d3dSamplerOffset;
620
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000621 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
622 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000623
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000624 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000625 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000626 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000627 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
628 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
629 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
630 if (mSupportsTextureFilterAnisotropy)
631 {
632 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
633 }
634}
635
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000636void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000637{
638 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
639 int d3dSampler = index + d3dSamplerOffset;
640 IDirect3DBaseTexture9 *d3dTexture = NULL;
641
642 if (texture)
643 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000644 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000645 if (texStorage)
646 {
647 d3dTexture = texStorage->getBaseTexture();
648 }
649 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000650 // in the texture class and we're unexpectedly missing the d3d texture
651 ASSERT(d3dTexture != NULL);
652 }
653
654 mDevice->SetTexture(d3dSampler, d3dTexture);
655}
656
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000657void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState, unsigned int depthSize)
658{
659 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
660 bool depthSizeChanged = mForceSetRasterState || depthSize != mCurDepthSize;
661
662 if (rasterStateChanged)
663 {
664 // Set the cull mode
665 if (rasterState.cullFace)
666 {
667 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
668 }
669 else
670 {
671 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
672 }
673
674 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, rasterState.scissorTest ? TRUE : FALSE);
675
676 mCurRasterState = rasterState;
677 }
678
679 if (rasterStateChanged || depthSizeChanged)
680 {
681 if (rasterState.polygonOffsetFill)
682 {
683 if (depthSize > 0)
684 {
685 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
686
687 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
688 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
689 }
690 }
691 else
692 {
693 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
694 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
695 }
696
697 mCurDepthSize = depthSize;
698 }
699
700 mForceSetRasterState = false;
701}
702
703void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
704{
705 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
706 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
707 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
708
709 if (blendStateChanged || blendColorChanged)
710 {
711 if (blendState.blend)
712 {
713 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
714
715 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
716 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
717 {
718 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
719 }
720 else
721 {
722 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
723 gl::unorm<8>(blendColor.alpha),
724 gl::unorm<8>(blendColor.alpha),
725 gl::unorm<8>(blendColor.alpha)));
726 }
727
728 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
729 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
730 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
731
732 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
733 blendState.destBlendRGB != blendState.destBlendAlpha ||
734 blendState.blendEquationRGB != blendState.blendEquationAlpha)
735 {
736 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
737
738 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
739 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
740 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
741 }
742 else
743 {
744 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
745 }
746 }
747 else
748 {
749 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
750 }
751
752 if (blendState.sampleAlphaToCoverage)
753 {
754 FIXME("Sample alpha to coverage is unimplemented.");
755 }
756
757 // Set the color mask
758 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
759 // Apparently some ATI cards have a bug where a draw with a zero color
760 // write mask can cause later draws to have incorrect results. Instead,
761 // set a nonzero color write mask but modify the blend state so that no
762 // drawing is done.
763 // http://code.google.com/p/angleproject/issues/detail?id=169
764
765 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
766 blendState.colorMaskBlue, blendState.colorMaskAlpha);
767 if (colorMask == 0 && !zeroColorMaskAllowed)
768 {
769 // Enable green channel, but set blending so nothing will be drawn.
770 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
771 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
772
773 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
774 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
775 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
776 }
777 else
778 {
779 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
780 }
781
782 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
783
784 mCurBlendState = blendState;
785 mCurBlendColor = blendColor;
786 }
787
788 if (sampleMaskChanged)
789 {
790 // Set the multisample mask
791 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
792 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
793
794 mCurSampleMask = sampleMask;
795 }
796
797 mForceSetBlendState = false;
798}
799
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000800void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
801 int stencilBackRef, bool frontFaceCCW, unsigned int stencilSize)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802{
803 bool depthStencilStateChanged = mForceSetDepthStencilState ||
804 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000805 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
806 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000807 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
808 bool stencilSizeChanged = mForceSetDepthStencilState || stencilSize != mCurStencilSize;
809
810 if (depthStencilStateChanged)
811 {
812 if (depthStencilState.depthTest)
813 {
814 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
815 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
816 }
817 else
818 {
819 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
820 }
821
822 mCurDepthStencilState = depthStencilState;
823 }
824
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000825 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged || stencilSizeChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000826 {
827 if (depthStencilState.stencilTest && stencilSize > 0)
828 {
829 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
830 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
831
832 // FIXME: Unsupported by D3D9
833 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
834 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
835 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
836 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000837 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000838 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
839 {
840 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
841 return error(GL_INVALID_OPERATION);
842 }
843
844 // get the maximum size of the stencil ref
845 GLuint maxStencil = (1 << stencilSize) - 1;
846
847 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
848 depthStencilState.stencilWritemask);
849 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
850 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
851
852 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000853 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000854 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
855 depthStencilState.stencilMask);
856
857 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
858 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
859 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
860 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
861 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
862 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
863
864 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
865 depthStencilState.stencilBackWritemask);
866 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
867 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
868
869 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000870 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000871 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
872 depthStencilState.stencilBackMask);
873
874 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
875 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
876 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
877 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
878 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
879 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
880 }
881 else
882 {
883 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
884 }
885
886 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
887
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000888 mCurStencilRef = stencilRef;
889 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000890 mCurFrontFaceCCW = frontFaceCCW;
891 mCurStencilSize = stencilSize;
892 }
893
894 mForceSetDepthStencilState = false;
895}
896
897void Renderer9::setScissorRectangle(const gl::Rectangle& scissor, unsigned int renderTargetWidth,
898 unsigned int renderTargetHeight)
899{
900 bool renderTargetSizedChanged = mForceSetScissor ||
901 renderTargetWidth != mCurRenderTargetWidth ||
902 renderTargetHeight != mCurRenderTargetHeight;
903 bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0;
904
905 if (renderTargetSizedChanged || scissorChanged)
906 {
907 RECT rect;
908 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(renderTargetWidth));
daniel@transgaming.com1dd557a2012-11-28 19:44:00 +0000909 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(renderTargetHeight));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000910 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(renderTargetWidth));
daniel@transgaming.com1dd557a2012-11-28 19:44:00 +0000911 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(renderTargetHeight));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000912 mDevice->SetScissorRect(&rect);
913
914 mCurScissor = scissor;
915 mCurRenderTargetWidth = renderTargetWidth;
916 mCurRenderTargetHeight = renderTargetHeight;
917 }
918
919 mForceSetScissor = false;
920}
921
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000922bool Renderer9::setViewport(const gl::Rectangle& viewport, float zNear, float zFar,
923 unsigned int renderTargetWidth, unsigned int renderTargetHeight,
924 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
925{
926 bool viewportChanged = mForceSetViewport || memcmp(&viewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
927 zNear != mCurNear || zFar != mCurFar;
928
929 D3DVIEWPORT9 dxViewport;
930 dxViewport.X = gl::clamp(viewport.x, 0, static_cast<int>(renderTargetWidth));
931 dxViewport.Y = gl::clamp(viewport.y, 0, static_cast<int>(renderTargetHeight));
932 dxViewport.Width = gl::clamp(viewport.width, 0, static_cast<int>(renderTargetWidth) - static_cast<int>(dxViewport.X));
933 dxViewport.Height = gl::clamp(viewport.height, 0, static_cast<int>(renderTargetHeight) - static_cast<int>(dxViewport.Y));
934 dxViewport.MinZ = zNear;
935 dxViewport.MaxZ = zFar;
936
937 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
938 {
939 return false; // Nothing to render
940 }
941
942 if (viewportChanged)
943 {
944 mDevice->SetViewport(&dxViewport);
945
946 mCurViewport = viewport;
947 mCurNear = zNear;
948 mCurFar = zFar;
949 }
950
951 if (currentProgram && (viewportChanged || forceSetUniforms))
952 {
953 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
954 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
955 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
956
957 // These values are used for computing gl_FragCoord in Program::linkVaryings().
958 GLint coord = currentProgram->getDxCoordLocation();
959 GLfloat whxy[4] = { viewport.width * 0.5f,
960 viewport.height * 0.5f,
961 viewport.x + (viewport.width * 0.5f),
962 viewport.y + (viewport.height * 0.5f) };
963 currentProgram->setUniform4fv(coord, 1, whxy);
964
965 GLint depth = currentProgram->getDxDepthLocation();
966 GLfloat dz[2] = { (zFar - zNear) * 0.5f, (zNear + zFar) * 0.5f };
967 currentProgram->setUniform2fv(depth, 1, dz);
968
969 GLint depthRange = currentProgram->getDxDepthRangeLocation();
970 GLfloat nearFarDiff[3] = { zNear, zFar, zFar - zNear };
971 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
972 }
973
974 mForceSetViewport = false;
975 return true;
976}
977
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000978bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000979{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +0000980 // if there is no color attachment we must synthesize a NULL colorattachment
981 // to keep the D3D runtime happy. This should only be possible if depth texturing.
982 gl::Renderbuffer *renderbufferObject = NULL;
983 if (framebuffer->getColorbufferType() != GL_NONE)
984 {
985 renderbufferObject = framebuffer->getColorbuffer();
986 }
987 else
988 {
989 renderbufferObject = framebuffer->getNullColorbuffer();
990 }
991 if (!renderbufferObject)
992 {
993 ERR("unable to locate renderbuffer for FBO.");
994 return false;
995 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000996
daniel@transgaming.com220e79a2012-11-28 19:42:11 +0000997 bool renderTargetChanged = false;
998 unsigned int renderTargetSerial = renderbufferObject->getSerial();
999 if (renderTargetSerial != mAppliedRenderTargetSerial)
1000 {
1001 // Apply the render target on the device
1002 IDirect3DSurface9 *renderTargetSurface = NULL;
1003
1004 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1005 if (renderTarget)
1006 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001007 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001008 }
1009
1010 if (!renderTargetSurface)
1011 {
1012 ERR("render target pointer unexpectedly null.");
1013 return false; // Context must be lost
1014 }
1015
1016 mDevice->SetRenderTarget(0, renderTargetSurface);
1017 renderTargetSurface->Release();
1018
1019 mAppliedRenderTargetSerial = renderTargetSerial;
1020 renderTargetChanged = true;
1021 }
1022
1023 gl::Renderbuffer *depthStencil = NULL;
1024 unsigned int depthbufferSerial = 0;
1025 unsigned int stencilbufferSerial = 0;
1026 if (framebuffer->getDepthbufferType() != GL_NONE)
1027 {
1028 depthStencil = framebuffer->getDepthbuffer();
1029 if (!depthStencil)
1030 {
1031 ERR("Depth stencil pointer unexpectedly null.");
1032 return false;
1033 }
1034
1035 depthbufferSerial = depthStencil->getSerial();
1036 }
1037 else if (framebuffer->getStencilbufferType() != GL_NONE)
1038 {
1039 depthStencil = framebuffer->getStencilbuffer();
1040 if (!depthStencil)
1041 {
1042 ERR("Depth stencil pointer unexpectedly null.");
1043 return false;
1044 }
1045
1046 stencilbufferSerial = depthStencil->getSerial();
1047 }
1048
1049 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1050 stencilbufferSerial != mAppliedStencilbufferSerial ||
1051 !mDepthStencilInitialized)
1052 {
1053 // Apply the depth stencil on the device
1054 if (depthStencil)
1055 {
1056 IDirect3DSurface9 *depthStencilSurface = NULL;
1057 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1058
1059 if (depthStencilRenderTarget)
1060 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001061 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001062 }
1063
1064 if (!depthStencilSurface)
1065 {
1066 ERR("depth stencil pointer unexpectedly null.");
1067 return false; // Context must be lost
1068 }
1069
1070 mDevice->SetDepthStencilSurface(depthStencilSurface);
1071 depthStencilSurface->Release();
1072 }
1073 else
1074 {
1075 mDevice->SetDepthStencilSurface(NULL);
1076 }
1077
1078 mAppliedDepthbufferSerial = depthbufferSerial;
1079 mAppliedStencilbufferSerial = stencilbufferSerial;
1080 mDepthStencilInitialized = true;
1081 }
1082
1083 if (renderTargetChanged || !mRenderTargetDescInitialized)
1084 {
1085 mForceSetScissor = true;
1086 mForceSetViewport = true;
1087
1088 mRenderTargetDesc.width = renderbufferObject->getWidth();
1089 mRenderTargetDesc.height = renderbufferObject->getHeight();
1090 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1091 mRenderTargetDescInitialized = true;
1092 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001093
1094 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001095}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001096
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001097GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
1098{
1099 gl::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
1100 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1101 if (err != GL_NO_ERROR)
1102 {
1103 return err;
1104 }
1105
1106 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
1107}
1108
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001109void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1110{
1111 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
1112 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
1113
1114 mDevice->SetPixelShader(pixelShader);
1115 mDevice->SetVertexShader(vertexShader);
1116 programBinary->dirtyAllUniforms();
1117}
1118
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001119void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001120{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001121 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1122 gl::unorm<8>(clearParams.colorClearValue.red),
1123 gl::unorm<8>(clearParams.colorClearValue.green),
1124 gl::unorm<8>(clearParams.colorClearValue.blue));
1125 float depth = gl::clamp01(clearParams.depthClearValue);
1126 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001127
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001128 unsigned int stencilUnmasked = 0x0;
1129 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1130 {
1131 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1132 stencilUnmasked = (0x1 << stencilSize) - 1;
1133 }
1134
1135 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1136
1137 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1138 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1139 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1140 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1141 clearParams.colorMaskBlue && alphaUnmasked);
1142
1143 if (needMaskedColorClear || needMaskedStencilClear)
1144 {
1145 // State which is altered in all paths from this point to the clear call is saved.
1146 // State which is altered in only some paths will be flagged dirty in the case that
1147 // that path is taken.
1148 HRESULT hr;
1149 if (mMaskedClearSavedState == NULL)
1150 {
1151 hr = mDevice->BeginStateBlock();
1152 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1153
1154 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1155 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1156 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1157 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1158 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1159 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1160 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1161 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1162 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1163 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1164 mDevice->SetPixelShader(NULL);
1165 mDevice->SetVertexShader(NULL);
1166 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1167 mDevice->SetStreamSource(0, NULL, 0, 0);
1168 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1169 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1170 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1171 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1172 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1173 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1174 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1175
1176 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1177 {
1178 mDevice->SetStreamSourceFreq(i, 1);
1179 }
1180
1181 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1182 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1183 }
1184
1185 ASSERT(mMaskedClearSavedState != NULL);
1186
1187 if (mMaskedClearSavedState != NULL)
1188 {
1189 hr = mMaskedClearSavedState->Capture();
1190 ASSERT(SUCCEEDED(hr));
1191 }
1192
1193 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1194 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1195 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1196 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1197 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1198 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1199 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1200 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1201
1202 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1203 {
1204 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1205 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1206 clearParams.colorMaskGreen,
1207 clearParams.colorMaskBlue,
1208 clearParams.colorMaskAlpha));
1209 }
1210 else
1211 {
1212 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1213 }
1214
1215 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1216 {
1217 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1218 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1219 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1220 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1221 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1222 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1223 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1224 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1225 }
1226 else
1227 {
1228 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1229 }
1230
1231 mDevice->SetPixelShader(NULL);
1232 mDevice->SetVertexShader(NULL);
1233 mDevice->SetFVF(D3DFVF_XYZRHW);
1234 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1235 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1236 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1237 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1238 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1239 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1240 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1241
1242 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1243 {
1244 mDevice->SetStreamSourceFreq(i, 1);
1245 }
1246
1247 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1248 quad[0][0] = -0.5f;
1249 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1250 quad[0][2] = 0.0f;
1251 quad[0][3] = 1.0f;
1252
1253 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1254 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1255 quad[1][2] = 0.0f;
1256 quad[1][3] = 1.0f;
1257
1258 quad[2][0] = -0.5f;
1259 quad[2][1] = -0.5f;
1260 quad[2][2] = 0.0f;
1261 quad[2][3] = 1.0f;
1262
1263 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1264 quad[3][1] = -0.5f;
1265 quad[3][2] = 0.0f;
1266 quad[3][3] = 1.0f;
1267
1268 startScene();
1269 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1270
1271 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1272 {
1273 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1274 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1275 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1276 }
1277
1278 if (mMaskedClearSavedState != NULL)
1279 {
1280 mMaskedClearSavedState->Apply();
1281 }
1282 }
1283 else if (clearParams.mask)
1284 {
1285 DWORD dxClearFlags = 0;
1286 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1287 {
1288 dxClearFlags |= D3DCLEAR_TARGET;
1289 }
1290 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1291 {
1292 dxClearFlags |= D3DCLEAR_ZBUFFER;
1293 }
1294 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1295 {
1296 dxClearFlags |= D3DCLEAR_STENCIL;
1297 }
1298
1299 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1300 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001301}
1302
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001303void Renderer9::markAllStateDirty()
1304{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001305 mAppliedRenderTargetSerial = 0;
1306 mAppliedDepthbufferSerial = 0;
1307 mAppliedStencilbufferSerial = 0;
1308 mDepthStencilInitialized = false;
1309 mRenderTargetDescInitialized = false;
1310
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001311 mForceSetDepthStencilState = true;
1312 mForceSetRasterState = true;
1313 mForceSetBlendState = true;
1314 mForceSetScissor = true;
1315 mForceSetViewport = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001316
1317 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001318}
1319
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001320void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001321{
1322 while (!mEventQueryPool.empty())
1323 {
1324 mEventQueryPool.back()->Release();
1325 mEventQueryPool.pop_back();
1326 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001327
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001328 if (mMaskedClearSavedState)
1329 {
1330 mMaskedClearSavedState->Release();
1331 mMaskedClearSavedState = NULL;
1332 }
1333
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001334 mVertexShaderCache.clear();
1335 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001336
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001337 delete mBlit;
1338 mBlit = NULL;
1339
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001340 delete mVertexDataManager;
1341 mVertexDataManager = NULL;
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001342}
1343
1344
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001345void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001346{
1347 mDeviceLost = true;
1348}
1349
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001350bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001351{
1352 return mDeviceLost;
1353}
1354
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001355// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001356bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001357{
1358 bool isLost = false;
1359
1360 if (mDeviceEx)
1361 {
1362 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1363 }
1364 else if (mDevice)
1365 {
1366 isLost = FAILED(mDevice->TestCooperativeLevel());
1367 }
1368 else
1369 {
1370 // No device yet, so no reset required
1371 }
1372
1373 if (isLost)
1374 {
1375 // ensure we note the device loss --
1376 // we'll probably get this done again by markDeviceLost
1377 // but best to remember it!
1378 // Note that we don't want to clear the device loss status here
1379 // -- this needs to be done by resetDevice
1380 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001381 if (notify)
1382 {
1383 mDisplay->notifyDeviceLost();
1384 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001385 }
1386
1387 return isLost;
1388}
1389
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001390bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001391{
1392 HRESULT status = D3D_OK;
1393
1394 if (mDeviceEx)
1395 {
1396 status = mDeviceEx->CheckDeviceState(NULL);
1397 }
1398 else if (mDevice)
1399 {
1400 status = mDevice->TestCooperativeLevel();
1401 }
1402
1403 switch (status)
1404 {
1405 case D3DERR_DEVICENOTRESET:
1406 case D3DERR_DEVICEHUNG:
1407 return true;
1408 default:
1409 return false;
1410 }
1411}
1412
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001413bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001414{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001415 releaseDeviceResources();
1416
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001417 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1418
1419 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001420 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001421 int attempts = 3;
1422
1423 while (lost && attempts > 0)
1424 {
1425 if (mDeviceEx)
1426 {
1427 Sleep(500); // Give the graphics driver some CPU time
1428 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1429 }
1430 else
1431 {
1432 result = mDevice->TestCooperativeLevel();
1433 while (result == D3DERR_DEVICELOST)
1434 {
1435 Sleep(100); // Give the graphics driver some CPU time
1436 result = mDevice->TestCooperativeLevel();
1437 }
1438
1439 if (result == D3DERR_DEVICENOTRESET)
1440 {
1441 result = mDevice->Reset(&presentParameters);
1442 }
1443 }
1444
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001445 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001446 attempts --;
1447 }
1448
1449 if (FAILED(result))
1450 {
1451 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1452 return false;
1453 }
1454
1455 // reset device defaults
1456 initializeDevice();
1457 mDeviceLost = false;
1458
1459 return true;
1460}
1461
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001462DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001463{
1464 return mAdapterIdentifier.VendorId;
1465}
1466
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001467const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001468{
1469 return mAdapterIdentifier.Description;
1470}
1471
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001472GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001473{
1474 return mAdapterIdentifier.DeviceIdentifier;
1475}
1476
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001477void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001478{
1479 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1480 {
1481 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1482 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1483
1484 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1485 }
1486}
1487
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001488bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001489{
1490 D3DDISPLAYMODE currentDisplayMode;
1491 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1492
1493 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1494}
1495
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001496bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001497{
1498 D3DDISPLAYMODE currentDisplayMode;
1499 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1500
1501 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1502}
1503
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001504bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001505{
1506 D3DDISPLAYMODE currentDisplayMode;
1507 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1508
1509 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1510}
1511
1512// we use INTZ for depth textures in Direct3D9
1513// we also want NULL texture support to ensure the we can make depth-only FBOs
1514// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001515bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001516{
1517 D3DDISPLAYMODE currentDisplayMode;
1518 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1519
1520 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1521 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1522 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1523 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1524
1525 return intz && null;
1526}
1527
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001528bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001529{
1530 D3DDISPLAYMODE currentDisplayMode;
1531 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1532
1533 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1534 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1535 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1536 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1537
1538 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1539 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1540 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1541 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1542
1543 if (!*filtering && !*renderable)
1544 {
1545 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1546 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1547 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1548 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1549 }
1550 else
1551 {
1552 return true;
1553 }
1554}
1555
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001556bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001557{
1558 D3DDISPLAYMODE currentDisplayMode;
1559 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1560
1561 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1562 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1563 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1564 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1565
1566 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1567 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1568 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1569 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1570
1571 if (!*filtering && !*renderable)
1572 {
1573 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1574 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1575 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1576 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1577 }
1578 else
1579 {
1580 return true;
1581 }
1582}
1583
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001584bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001585{
1586 D3DDISPLAYMODE currentDisplayMode;
1587 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1588
1589 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1590}
1591
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001592bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001593{
1594 D3DDISPLAYMODE currentDisplayMode;
1595 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1596
1597 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1598}
1599
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001600bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001601{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001602 return mSupportsTextureFilterAnisotropy;
1603}
1604
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001605float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001606{
1607 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001608 {
1609 return mDeviceCaps.MaxAnisotropy;
1610 }
1611 return 1.0f;
1612}
1613
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001614bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001615{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001616 IDirect3DQuery9 *query = allocateEventQuery();
1617 if (query)
1618 {
1619 freeEventQuery(query);
1620 return true;
1621 }
1622 else
1623 {
1624 return false;
1625 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001626 return true;
1627}
1628
1629// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1630// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001631bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001632{
1633 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1634 {
1635 return false;
1636 }
1637
1638 D3DDISPLAYMODE currentDisplayMode;
1639 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1640
1641 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1642
1643 return SUCCEEDED(result);
1644}
1645
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001646bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001647{
1648 return mSupportsNonPower2Textures;
1649}
1650
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001651bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001652{
1653 if (!mDevice)
1654 {
1655 return false;
1656 }
1657
1658 IDirect3DQuery9 *query = NULL;
1659 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1660 if (SUCCEEDED(result) && query)
1661 {
1662 query->Release();
1663 return true;
1664 }
1665 else
1666 {
1667 return false;
1668 }
1669}
1670
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001671bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001672{
1673 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1674}
1675
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001676bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001677{
1678 // PIX doesn't seem to support using share handles, so disable them.
1679 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00001680 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00001681}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001682
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001683bool Renderer9::getShaderModel3Support() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001684{
1685 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1686}
1687
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001688float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001689{
1690 return mDeviceCaps.MaxPointSize;
1691}
1692
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001693int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001694{
1695 return (int)mDeviceCaps.MaxTextureWidth;
1696}
1697
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001698int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001699{
1700 return (int)mDeviceCaps.MaxTextureHeight;
1701}
1702
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001703bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001704{
1705 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
1706}
1707
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001708DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001709{
1710 return mDeviceCaps.DeclTypes;
1711}
1712
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001713int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001714{
1715 return mMinSwapInterval;
1716}
1717
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001718int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00001719{
1720 return mMaxSwapInterval;
1721}
1722
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001723int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001724{
1725 return mMaxSupportedSamples;
1726}
1727
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001728int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001729{
1730 if (requested == 0)
1731 {
1732 return requested;
1733 }
1734
1735 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
1736 if (itr == mMultiSampleSupport.end())
1737 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00001738 if (format == D3DFMT_UNKNOWN)
1739 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00001740 return -1;
1741 }
1742
1743 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
1744 {
1745 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
1746 {
1747 return i;
1748 }
1749 }
1750
1751 return -1;
1752}
1753
daniel@transgaming.coma9571682012-11-28 19:33:08 +00001754D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
1755{
1756 switch (internalformat)
1757 {
1758 case GL_DEPTH_COMPONENT16:
1759 case GL_DEPTH_COMPONENT32_OES:
1760 case GL_DEPTH24_STENCIL8_OES:
1761 return D3DFMT_INTZ;
1762 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1763 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1764 return D3DFMT_DXT1;
1765 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1766 return D3DFMT_DXT3;
1767 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1768 return D3DFMT_DXT5;
1769 case GL_RGBA32F_EXT:
1770 case GL_RGB32F_EXT:
1771 case GL_ALPHA32F_EXT:
1772 case GL_LUMINANCE32F_EXT:
1773 case GL_LUMINANCE_ALPHA32F_EXT:
1774 return D3DFMT_A32B32G32R32F;
1775 case GL_RGBA16F_EXT:
1776 case GL_RGB16F_EXT:
1777 case GL_ALPHA16F_EXT:
1778 case GL_LUMINANCE16F_EXT:
1779 case GL_LUMINANCE_ALPHA16F_EXT:
1780 return D3DFMT_A16B16G16R16F;
1781 case GL_LUMINANCE8_EXT:
1782 if (getLuminanceTextureSupport())
1783 {
1784 return D3DFMT_L8;
1785 }
1786 break;
1787 case GL_LUMINANCE8_ALPHA8_EXT:
1788 if (getLuminanceAlphaTextureSupport())
1789 {
1790 return D3DFMT_A8L8;
1791 }
1792 break;
1793 case GL_RGB8_OES:
1794 case GL_RGB565:
1795 return D3DFMT_X8R8G8B8;
1796 }
1797
1798 return D3DFMT_A8R8G8B8;
1799}
1800
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001801bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001802{
1803 bool result = false;
1804
1805 if (source && dest)
1806 {
1807 int levels = source->levelCount();
1808 for (int i = 0; i < levels; ++i)
1809 {
1810 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
1811 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
1812
1813 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1814
1815 if (srcSurf) srcSurf->Release();
1816 if (dstSurf) dstSurf->Release();
1817
1818 if (!result)
1819 return false;
1820 }
1821 }
1822
1823 return result;
1824}
1825
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00001826bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00001827{
1828 bool result = false;
1829
1830 if (source && dest)
1831 {
1832 int levels = source->levelCount();
1833 for (int f = 0; f < 6; f++)
1834 {
1835 for (int i = 0; i < levels; i++)
1836 {
1837 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
1838 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
1839
1840 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
1841
1842 if (srcSurf) srcSurf->Release();
1843 if (dstSurf) dstSurf->Release();
1844
1845 if (!result)
1846 return false;
1847 }
1848 }
1849 }
1850
1851 return result;
1852}
1853
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001854D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001855{
1856 if (mD3d9Ex != NULL)
1857 {
1858 return D3DPOOL_DEFAULT;
1859 }
1860 else
1861 {
1862 if (!(usage & D3DUSAGE_DYNAMIC))
1863 {
1864 return D3DPOOL_MANAGED;
1865 }
1866 }
1867
1868 return D3DPOOL_DEFAULT;
1869}
1870
daniel@transgaming.com38380882012-11-28 19:36:39 +00001871bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1872 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001873{
1874 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
1875}
1876
daniel@transgaming.com38380882012-11-28 19:36:39 +00001877bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
1878 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00001879{
1880 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
1881}
1882
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001883bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
1884 bool blitRenderTarget, bool blitDepthStencil)
1885{
1886 endScene();
1887
1888 if (blitRenderTarget)
1889 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001890 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
1891 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
1892 RenderTarget9 *readRenderTarget = NULL;
1893 RenderTarget9 *drawRenderTarget = NULL;
1894 IDirect3DSurface9* readSurface = NULL;
1895 IDirect3DSurface9* drawSurface = NULL;
1896
1897 if (readBuffer)
1898 {
1899 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
1900 }
1901 if (drawBuffer)
1902 {
1903 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
1904 }
1905
1906 if (readRenderTarget)
1907 {
1908 readSurface = readRenderTarget->getSurface();
1909 }
1910 if (drawRenderTarget)
1911 {
1912 drawSurface = drawRenderTarget->getSurface();
1913 }
1914
1915 if (!readSurface || !drawSurface)
1916 {
1917 ERR("Failed to retrieve the render target.");
1918 return error(GL_OUT_OF_MEMORY, false);
1919 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001920
1921 RECT srcRect, dstRect;
1922 RECT *srcRectPtr = NULL;
1923 RECT *dstRectPtr = NULL;
1924
1925 if (readRect)
1926 {
1927 srcRect.left = readRect->x;
1928 srcRect.right = readRect->x + readRect->width;
1929 srcRect.top = readRect->y;
1930 srcRect.bottom = readRect->y + readRect->height;
1931 srcRectPtr = &srcRect;
1932 }
1933
1934 if (drawRect)
1935 {
1936 dstRect.left = drawRect->x;
1937 dstRect.right = drawRect->x + drawRect->width;
1938 dstRect.top = drawRect->y;
1939 dstRect.bottom = drawRect->y + drawRect->height;
1940 dstRectPtr = &dstRect;
1941 }
1942
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001943 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001944
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001945 readSurface->Release();
1946 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001947
1948 if (FAILED(result))
1949 {
1950 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
1951 return false;
1952 }
1953 }
1954
1955 if (blitDepthStencil)
1956 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001957 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
1958 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
1959 RenderTarget9 *readDepthStencil = NULL;
1960 RenderTarget9 *drawDepthStencil = NULL;
1961 IDirect3DSurface9* readSurface = NULL;
1962 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001963
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001964 if (readBuffer)
1965 {
1966 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
1967 }
1968 if (drawBuffer)
1969 {
1970 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
1971 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001972
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001973 if (readDepthStencil)
1974 {
1975 readSurface = readDepthStencil->getSurface();
1976 }
1977 if (drawDepthStencil)
1978 {
1979 drawSurface = drawDepthStencil->getSurface();
1980 }
1981
1982 if (!readSurface || !drawSurface)
1983 {
1984 ERR("Failed to retrieve the render target.");
1985 return error(GL_OUT_OF_MEMORY, false);
1986 }
1987
1988 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
1989
1990 readSurface->Release();
1991 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00001992
1993 if (FAILED(result))
1994 {
1995 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
1996 return false;
1997 }
1998 }
1999
2000 return true;
2001}
2002
2003void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2004 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2005{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002006 RenderTarget9 *renderTarget = NULL;
2007 IDirect3DSurface9 *surface = NULL;
2008 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2009
2010 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002011 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002012 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2013 }
2014
2015 if (renderTarget)
2016 {
2017 surface = renderTarget->getSurface();
2018 }
2019
2020 if (!surface)
2021 {
2022 // context must be lost
2023 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002024 }
2025
2026 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002027 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002028
2029 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2030 {
2031 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002032 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002033 return error(GL_OUT_OF_MEMORY);
2034 }
2035
2036 HRESULT result;
2037 IDirect3DSurface9 *systemSurface = NULL;
2038 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2039 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2040 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2041 if (directToPixels)
2042 {
2043 // Use the pixels ptr as a shared handle to write directly into client's memory
2044 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2045 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2046 if (FAILED(result))
2047 {
2048 // Try again without the shared handle
2049 directToPixels = false;
2050 }
2051 }
2052
2053 if (!directToPixels)
2054 {
2055 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2056 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2057 if (FAILED(result))
2058 {
2059 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002060 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002061 return error(GL_OUT_OF_MEMORY);
2062 }
2063 }
2064
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002065 result = mDevice->GetRenderTargetData(surface, systemSurface);
2066 surface->Release();
2067 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002068
2069 if (FAILED(result))
2070 {
2071 systemSurface->Release();
2072
2073 // It turns out that D3D will sometimes produce more error
2074 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002075 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002076 return error(GL_OUT_OF_MEMORY);
2077 else
2078 {
2079 UNREACHABLE();
2080 return;
2081 }
2082
2083 }
2084
2085 if (directToPixels)
2086 {
2087 systemSurface->Release();
2088 return;
2089 }
2090
2091 RECT rect;
2092 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2093 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2094 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2095 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2096
2097 D3DLOCKED_RECT lock;
2098 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2099
2100 if (FAILED(result))
2101 {
2102 UNREACHABLE();
2103 systemSurface->Release();
2104
2105 return; // No sensible error to generate
2106 }
2107
2108 unsigned char *dest = (unsigned char*)pixels;
2109 unsigned short *dest16 = (unsigned short*)pixels;
2110
2111 unsigned char *source;
2112 int inputPitch;
2113 if (packReverseRowOrder)
2114 {
2115 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2116 inputPitch = -lock.Pitch;
2117 }
2118 else
2119 {
2120 source = (unsigned char*)lock.pBits;
2121 inputPitch = lock.Pitch;
2122 }
2123
2124 unsigned int fastPixelSize = 0;
2125
2126 if (desc.Format == D3DFMT_A8R8G8B8 &&
2127 format == GL_BGRA_EXT &&
2128 type == GL_UNSIGNED_BYTE)
2129 {
2130 fastPixelSize = 4;
2131 }
2132 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2133 format == GL_BGRA_EXT &&
2134 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2135 (desc.Format == D3DFMT_A1R5G5B5 &&
2136 format == GL_BGRA_EXT &&
2137 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2138 {
2139 fastPixelSize = 2;
2140 }
2141 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2142 format == GL_RGBA &&
2143 type == GL_HALF_FLOAT_OES)
2144 {
2145 fastPixelSize = 8;
2146 }
2147 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2148 format == GL_RGBA &&
2149 type == GL_FLOAT)
2150 {
2151 fastPixelSize = 16;
2152 }
2153
2154 for (int j = 0; j < rect.bottom - rect.top; j++)
2155 {
2156 if (fastPixelSize != 0)
2157 {
2158 // Fast path for formats which require no translation:
2159 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2160 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2161 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2162 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2163 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2164 //
2165 // Note that buffers with no alpha go through the slow path below.
2166 memcpy(dest + j * outputPitch,
2167 source + j * inputPitch,
2168 (rect.right - rect.left) * fastPixelSize);
2169 continue;
2170 }
2171
2172 for (int i = 0; i < rect.right - rect.left; i++)
2173 {
2174 float r;
2175 float g;
2176 float b;
2177 float a;
2178
2179 switch (desc.Format)
2180 {
2181 case D3DFMT_R5G6B5:
2182 {
2183 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2184
2185 a = 1.0f;
2186 b = (rgb & 0x001F) * (1.0f / 0x001F);
2187 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2188 r = (rgb & 0xF800) * (1.0f / 0xF800);
2189 }
2190 break;
2191 case D3DFMT_A1R5G5B5:
2192 {
2193 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2194
2195 a = (argb & 0x8000) ? 1.0f : 0.0f;
2196 b = (argb & 0x001F) * (1.0f / 0x001F);
2197 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2198 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2199 }
2200 break;
2201 case D3DFMT_A8R8G8B8:
2202 {
2203 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2204
2205 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2206 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2207 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2208 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2209 }
2210 break;
2211 case D3DFMT_X8R8G8B8:
2212 {
2213 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2214
2215 a = 1.0f;
2216 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2217 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2218 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2219 }
2220 break;
2221 case D3DFMT_A2R10G10B10:
2222 {
2223 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2224
2225 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2226 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2227 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2228 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2229 }
2230 break;
2231 case D3DFMT_A32B32G32R32F:
2232 {
2233 // float formats in D3D are stored rgba, rather than the other way round
2234 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2235 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2236 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2237 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2238 }
2239 break;
2240 case D3DFMT_A16B16G16R16F:
2241 {
2242 // float formats in D3D are stored rgba, rather than the other way round
2243 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2244 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2245 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2246 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2247 }
2248 break;
2249 default:
2250 UNIMPLEMENTED(); // FIXME
2251 UNREACHABLE();
2252 return;
2253 }
2254
2255 switch (format)
2256 {
2257 case GL_RGBA:
2258 switch (type)
2259 {
2260 case GL_UNSIGNED_BYTE:
2261 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2262 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2263 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2264 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2265 break;
2266 default: UNREACHABLE();
2267 }
2268 break;
2269 case GL_BGRA_EXT:
2270 switch (type)
2271 {
2272 case GL_UNSIGNED_BYTE:
2273 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2274 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2275 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2276 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2277 break;
2278 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2279 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2280 // this type is packed as follows:
2281 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2282 // --------------------------------------------------------------------------------
2283 // | 4th | 3rd | 2nd | 1st component |
2284 // --------------------------------------------------------------------------------
2285 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2286 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2287 ((unsigned short)(15 * a + 0.5f) << 12)|
2288 ((unsigned short)(15 * r + 0.5f) << 8) |
2289 ((unsigned short)(15 * g + 0.5f) << 4) |
2290 ((unsigned short)(15 * b + 0.5f) << 0);
2291 break;
2292 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2293 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2294 // this type is packed as follows:
2295 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2296 // --------------------------------------------------------------------------------
2297 // | 4th | 3rd | 2nd | 1st component |
2298 // --------------------------------------------------------------------------------
2299 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2300 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2301 ((unsigned short)( a + 0.5f) << 15) |
2302 ((unsigned short)(31 * r + 0.5f) << 10) |
2303 ((unsigned short)(31 * g + 0.5f) << 5) |
2304 ((unsigned short)(31 * b + 0.5f) << 0);
2305 break;
2306 default: UNREACHABLE();
2307 }
2308 break;
2309 case GL_RGB:
2310 switch (type)
2311 {
2312 case GL_UNSIGNED_SHORT_5_6_5:
2313 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2314 ((unsigned short)(31 * b + 0.5f) << 0) |
2315 ((unsigned short)(63 * g + 0.5f) << 5) |
2316 ((unsigned short)(31 * r + 0.5f) << 11);
2317 break;
2318 case GL_UNSIGNED_BYTE:
2319 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2320 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2321 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2322 break;
2323 default: UNREACHABLE();
2324 }
2325 break;
2326 default: UNREACHABLE();
2327 }
2328 }
2329 }
2330
2331 systemSurface->UnlockRect();
2332
2333 systemSurface->Release();
2334}
2335
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002336RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2337{
2338 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2339 IDirect3DSurface9 *surface = NULL;
2340 if (depth)
2341 {
2342 surface = swapChain9->getDepthStencil();
2343 }
2344 else
2345 {
2346 surface = swapChain9->getRenderTarget();
2347 }
2348
2349 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2350
2351 return renderTarget;
2352}
2353
2354RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2355{
2356 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2357 return renderTarget;
2358}
2359
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002360bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2361{
2362 return mBlit->boxFilter(source, dest);
2363}
2364
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002365D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002366{
2367 if (mD3d9Ex != NULL)
2368 {
2369 return D3DPOOL_DEFAULT;
2370 }
2371 else
2372 {
2373 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2374 {
2375 return D3DPOOL_MANAGED;
2376 }
2377 }
2378
2379 return D3DPOOL_DEFAULT;
2380}
2381
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002382bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2383{
2384 if (source && dest)
2385 {
2386 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2387 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2388
2389 if (fromManaged)
2390 {
2391 D3DSURFACE_DESC desc;
2392 source->GetDesc(&desc);
2393
2394 IDirect3DSurface9 *surf = 0;
2395 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2396
2397 if (SUCCEEDED(result))
2398 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002399 Image::CopyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002400 result = device->UpdateSurface(surf, NULL, dest, NULL);
2401 surf->Release();
2402 }
2403 }
2404 else
2405 {
2406 endScene();
2407 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2408 }
2409
2410 if (FAILED(result))
2411 {
2412 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2413 return false;
2414 }
2415 }
2416
2417 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002418}
2419
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002420}