blob: 706e7921bbb42e2c611dfed413a32aee9f07fa72 [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00008
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009#include "common/debug.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000010#include "libGLESv2/main.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/mathutil.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000014#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000015#include "libGLESv2/Program.h"
16#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000017#include "libGLESv2/renderer/IndexDataManager.h"
18#include "libGLESv2/renderer/VertexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000019#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000020#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000021#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000022#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000023#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +000024#include "libGLESv2/renderer/Image.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000025#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000026#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000027
daniel@transgaming.com3281f972012-10-31 18:38:51 +000028#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000029#include "libEGL/Display.h"
30
daniel@transgaming.com621ce052012-10-31 17:52:29 +000031// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
32#define REF_RAST 0
33
34// The "Debug This Pixel..." feature in PIX often fails when using the
35// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
36// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
37#if !defined(ANGLE_ENABLE_D3D9EX)
38// Enables use of the IDirect3D9Ex interface, when available
39#define ANGLE_ENABLE_D3D9EX 1
40#endif // !defined(ANGLE_ENABLE_D3D9EX)
41
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000042namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000043{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000044static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000045 {
46 D3DFMT_A1R5G5B5,
47 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
48 D3DFMT_A8R8G8B8,
49 D3DFMT_R5G6B5,
50 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
51 D3DFMT_X8R8G8B8
52 };
53
daniel@transgaming.com222ee082012-11-28 19:31:49 +000054static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000055 {
56 D3DFMT_UNKNOWN,
57 // D3DFMT_D16_LOCKABLE,
58 D3DFMT_D32,
59 // D3DFMT_D15S1,
60 D3DFMT_D24S8,
61 D3DFMT_D24X8,
62 // D3DFMT_D24X4S4,
63 D3DFMT_D16,
64 // D3DFMT_D32F_LOCKABLE,
65 // D3DFMT_D24FS8
66 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000067
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000068Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000069{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000070 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000071
72 mD3d9 = NULL;
73 mD3d9Ex = NULL;
74 mDevice = NULL;
75 mDeviceEx = NULL;
76 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000077 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000078
79 mAdapter = D3DADAPTER_DEFAULT;
80
81 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
82 mDeviceType = D3DDEVTYPE_REF;
83 #else
84 mDeviceType = D3DDEVTYPE_HAL;
85 #endif
86
87 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000088
89 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000090
daniel@transgaming.com91207b72012-11-28 20:56:43 +000091 mAppliedIBSerial = 0;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000092
93 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000094
95 mVertexDataManager = NULL;
96 mIndexDataManager = NULL;
97 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +000098
99 mMaxNullColorbufferLRU = 0;
100 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
101 {
102 mNullColorbufferCache[i].lruCount = 0;
103 mNullColorbufferCache[i].width = 0;
104 mNullColorbufferCache[i].height = 0;
105 mNullColorbufferCache[i].buffer = NULL;
106 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000107}
108
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000109Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000110{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000111 releaseDeviceResources();
112
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000113 if (mDevice)
114 {
115 // 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 +0000116 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000117 {
118 resetDevice();
119 }
120
121 mDevice->Release();
122 mDevice = NULL;
123 }
124
125 if (mDeviceEx)
126 {
127 mDeviceEx->Release();
128 mDeviceEx = NULL;
129 }
130
131 if (mD3d9)
132 {
133 mD3d9->Release();
134 mD3d9 = NULL;
135 }
136
137 if (mDeviceWindow)
138 {
139 DestroyWindow(mDeviceWindow);
140 mDeviceWindow = NULL;
141 }
142
143 if (mD3d9Ex)
144 {
145 mD3d9Ex->Release();
146 mD3d9Ex = NULL;
147 }
148
149 if (mD3d9Module)
150 {
151 mD3d9Module = NULL;
152 }
153
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000154 while (!mMultiSampleSupport.empty())
155 {
156 delete [] mMultiSampleSupport.begin()->second;
157 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
158 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000159}
160
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000161Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
162{
163 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
164 return static_cast<rx::Renderer9*>(renderer);
165}
166
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000167EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000168{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000169 if (!initializeCompiler())
170 {
171 return EGL_NOT_INITIALIZED;
172 }
173
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000174 if (mSoftwareDevice)
175 {
176 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
177 }
178 else
179 {
180 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
181 }
182
183 if (mD3d9Module == NULL)
184 {
185 ERR("No D3D9 module found - aborting!\n");
186 return EGL_NOT_INITIALIZED;
187 }
188
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000189 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
190 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
191
192 // Use Direct3D9Ex if available. Among other things, this version is less
193 // inclined to report a lost context, for example when the user switches
194 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
195 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
196 {
197 ASSERT(mD3d9Ex);
198 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
199 ASSERT(mD3d9);
200 }
201 else
202 {
203 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
204 }
205
206 if (!mD3d9)
207 {
208 ERR("Could not create D3D9 device - aborting!\n");
209 return EGL_NOT_INITIALIZED;
210 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000211
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000212 if (mDc != NULL)
213 {
214 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
215 }
216
217 HRESULT result;
218
219 // Give up on getting device caps after about one second.
220 for (int i = 0; i < 10; ++i)
221 {
222 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
223 if (SUCCEEDED(result))
224 {
225 break;
226 }
227 else if (result == D3DERR_NOTAVAILABLE)
228 {
229 Sleep(100); // Give the driver some time to initialize/recover
230 }
231 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
232 {
233 ERR("failed to get device caps (0x%x)\n", result);
234 return EGL_NOT_INITIALIZED;
235 }
236 }
237
238 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
239 {
240 ERR("Renderer does not support PS 2.0. aborting!\n");
241 return EGL_NOT_INITIALIZED;
242 }
243
244 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
245 // This is required by Texture2D::convertToRenderTarget.
246 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
247 {
248 ERR("Renderer does not support stretctrect from textures!\n");
249 return EGL_NOT_INITIALIZED;
250 }
251
252 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
253
254 // ATI cards on XP have problems with non-power-of-two textures.
255 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
256 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
257 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
258 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
259
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000260 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
261 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
262
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000263 mMinSwapInterval = 4;
264 mMaxSwapInterval = 0;
265
266 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
267 {
268 mMinSwapInterval = std::min(mMinSwapInterval, 0);
269 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
270 }
271 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
272 {
273 mMinSwapInterval = std::min(mMinSwapInterval, 1);
274 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
275 }
276 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
277 {
278 mMinSwapInterval = std::min(mMinSwapInterval, 2);
279 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
280 }
281 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
282 {
283 mMinSwapInterval = std::min(mMinSwapInterval, 3);
284 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
285 }
286 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
287 {
288 mMinSwapInterval = std::min(mMinSwapInterval, 4);
289 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
290 }
291
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000292 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000293 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000294 {
295 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000296 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
297 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000298
299 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
300 {
301 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
302 {
303 max = j;
304 }
305 }
306 }
307
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000308 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000309 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000310 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000311 continue;
312
313 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000314 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
315 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000316
317 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
318 {
319 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
320 {
321 max = j;
322 }
323 }
324 }
325
326 mMaxSupportedSamples = max;
327
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000328 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
329 static const TCHAR className[] = TEXT("STATIC");
330
331 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
332
333 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
334 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
335
336 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
337 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
338 {
339 return EGL_BAD_ALLOC;
340 }
341
342 if (FAILED(result))
343 {
344 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
345
346 if (FAILED(result))
347 {
348 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
349 return EGL_BAD_ALLOC;
350 }
351 }
352
353 if (mD3d9Ex)
354 {
355 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
356 ASSERT(SUCCEEDED(result));
357 }
358
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000359 mVertexShaderCache.initialize(mDevice);
360 mPixelShaderCache.initialize(mDevice);
361
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000362 initializeDevice();
363
364 return EGL_SUCCESS;
365}
366
367// do any one-time device initialization
368// NOTE: this is also needed after a device lost/reset
369// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000370void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000371{
372 // Permanent non-default states
373 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
374 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
375
376 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
377 {
378 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
379 }
380 else
381 {
382 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
383 }
384
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000385 markAllStateDirty();
386
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000387 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000388
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000389 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000390 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000391 mVertexDataManager = new rx::VertexDataManager(this);
392 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000393}
394
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000395D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000396{
397 D3DPRESENT_PARAMETERS presentParameters = {0};
398
399 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
400 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
401 presentParameters.BackBufferCount = 1;
402 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
403 presentParameters.BackBufferWidth = 1;
404 presentParameters.BackBufferHeight = 1;
405 presentParameters.EnableAutoDepthStencil = FALSE;
406 presentParameters.Flags = 0;
407 presentParameters.hDeviceWindow = mDeviceWindow;
408 presentParameters.MultiSampleQuality = 0;
409 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
410 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
411 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
412 presentParameters.Windowed = TRUE;
413
414 return presentParameters;
415}
416
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000417int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000418{
419 D3DDISPLAYMODE currentDisplayMode;
420 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
421
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000422 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
423 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000424 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
425 int numConfigs = 0;
426
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000427 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000428 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000429 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000430
431 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
432
433 if (SUCCEEDED(result))
434 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000435 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000436 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000437 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000438 HRESULT result = D3D_OK;
439
440 if(depthStencilFormat != D3DFMT_UNKNOWN)
441 {
442 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
443 }
444
445 if (SUCCEEDED(result))
446 {
447 if(depthStencilFormat != D3DFMT_UNKNOWN)
448 {
449 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
450 }
451
452 if (SUCCEEDED(result))
453 {
454 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000455 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
456 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000457 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
458 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
459
460 (*configDescList)[numConfigs++] = newConfig;
461 }
462 }
463 }
464 }
465 }
466
467 return numConfigs;
468}
469
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000470void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000471{
472 delete [] (configDescList);
473}
474
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000475void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000476{
477 if (!mSceneStarted)
478 {
479 long result = mDevice->BeginScene();
480 if (SUCCEEDED(result)) {
481 // This is defensive checking against the device being
482 // lost at unexpected times.
483 mSceneStarted = true;
484 }
485 }
486}
487
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000488void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000489{
490 if (mSceneStarted)
491 {
492 // EndScene can fail if the device was lost, for example due
493 // to a TDR during a draw call.
494 mDevice->EndScene();
495 mSceneStarted = false;
496 }
497}
498
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000499// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000500void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000501{
502 HRESULT result;
503
504 IDirect3DQuery9* query = allocateEventQuery();
505 if (!query)
506 {
507 return;
508 }
509
510 result = query->Issue(D3DISSUE_END);
511 ASSERT(SUCCEEDED(result));
512
513 do
514 {
515 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
516
517 if(block && result == S_FALSE)
518 {
519 // Keep polling, but allow other threads to do something useful first
520 Sleep(0);
521 // explicitly check for device loss
522 // some drivers seem to return S_FALSE even if the device is lost
523 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000524 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000525 {
526 result = D3DERR_DEVICELOST;
527 }
528 }
529 }
530 while(block && result == S_FALSE);
531
532 freeEventQuery(query);
533
534 if (isDeviceLostError(result))
535 {
536 mDisplay->notifyDeviceLost();
537 }
538}
539
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000540SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
541{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000542 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000543}
544
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000545// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000546IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000547{
548 IDirect3DQuery9 *query = NULL;
549
550 if (mEventQueryPool.empty())
551 {
552 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
553 ASSERT(SUCCEEDED(result));
554 }
555 else
556 {
557 query = mEventQueryPool.back();
558 mEventQueryPool.pop_back();
559 }
560
561 return query;
562}
563
564// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000565void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000566{
567 if (mEventQueryPool.size() > 1000)
568 {
569 query->Release();
570 }
571 else
572 {
573 mEventQueryPool.push_back(query);
574 }
575}
576
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000577IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000578{
579 return mVertexShaderCache.create(function, length);
580}
581
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000582IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000583{
584 return mPixelShaderCache.create(function, length);
585}
586
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000587HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
588{
589 D3DPOOL Pool = getBufferPool(Usage);
590 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
591}
592
593HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
594{
595 D3DPOOL Pool = getBufferPool(Usage);
596 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
597}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000598
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000599void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000600{
601 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
602 int d3dSampler = index + d3dSamplerOffset;
603
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000604 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
605 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000606
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000607 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000608 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000609 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000610 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
611 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
612 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
613 if (mSupportsTextureFilterAnisotropy)
614 {
615 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
616 }
617}
618
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000619void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000620{
621 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
622 int d3dSampler = index + d3dSamplerOffset;
623 IDirect3DBaseTexture9 *d3dTexture = NULL;
624
625 if (texture)
626 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000627 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000628 if (texStorage)
629 {
630 d3dTexture = texStorage->getBaseTexture();
631 }
632 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000633 // in the texture class and we're unexpectedly missing the d3d texture
634 ASSERT(d3dTexture != NULL);
635 }
636
637 mDevice->SetTexture(d3dSampler, d3dTexture);
638}
639
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000640void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000641{
642 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000643
644 if (rasterStateChanged)
645 {
646 // Set the cull mode
647 if (rasterState.cullFace)
648 {
649 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
650 }
651 else
652 {
653 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
654 }
655
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000656 if (rasterState.polygonOffsetFill)
657 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000658 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000659 {
660 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
661
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000662 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000663 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
664 }
665 }
666 else
667 {
668 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
669 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
670 }
671
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000672 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000673 }
674
675 mForceSetRasterState = false;
676}
677
678void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
679{
680 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
681 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
682 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
683
684 if (blendStateChanged || blendColorChanged)
685 {
686 if (blendState.blend)
687 {
688 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
689
690 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
691 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
692 {
693 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
694 }
695 else
696 {
697 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
698 gl::unorm<8>(blendColor.alpha),
699 gl::unorm<8>(blendColor.alpha),
700 gl::unorm<8>(blendColor.alpha)));
701 }
702
703 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
704 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
705 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
706
707 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
708 blendState.destBlendRGB != blendState.destBlendAlpha ||
709 blendState.blendEquationRGB != blendState.blendEquationAlpha)
710 {
711 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
712
713 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
714 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
715 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
716 }
717 else
718 {
719 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
720 }
721 }
722 else
723 {
724 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
725 }
726
727 if (blendState.sampleAlphaToCoverage)
728 {
729 FIXME("Sample alpha to coverage is unimplemented.");
730 }
731
732 // Set the color mask
733 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
734 // Apparently some ATI cards have a bug where a draw with a zero color
735 // write mask can cause later draws to have incorrect results. Instead,
736 // set a nonzero color write mask but modify the blend state so that no
737 // drawing is done.
738 // http://code.google.com/p/angleproject/issues/detail?id=169
739
740 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
741 blendState.colorMaskBlue, blendState.colorMaskAlpha);
742 if (colorMask == 0 && !zeroColorMaskAllowed)
743 {
744 // Enable green channel, but set blending so nothing will be drawn.
745 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
746 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
747
748 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
749 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
750 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
751 }
752 else
753 {
754 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
755 }
756
757 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
758
759 mCurBlendState = blendState;
760 mCurBlendColor = blendColor;
761 }
762
763 if (sampleMaskChanged)
764 {
765 // Set the multisample mask
766 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
767 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
768
769 mCurSampleMask = sampleMask;
770 }
771
772 mForceSetBlendState = false;
773}
774
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000775void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000776 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000777{
778 bool depthStencilStateChanged = mForceSetDepthStencilState ||
779 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000780 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
781 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000782 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000783
784 if (depthStencilStateChanged)
785 {
786 if (depthStencilState.depthTest)
787 {
788 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
789 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
790 }
791 else
792 {
793 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
794 }
795
796 mCurDepthStencilState = depthStencilState;
797 }
798
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000799 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000800 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000801 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802 {
803 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
804 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
805
806 // FIXME: Unsupported by D3D9
807 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
808 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
809 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
810 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000811 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000812 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
813 {
814 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
815 return error(GL_INVALID_OPERATION);
816 }
817
818 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000819 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000820
821 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
822 depthStencilState.stencilWritemask);
823 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
824 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
825
826 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000827 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000828 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
829 depthStencilState.stencilMask);
830
831 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
832 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
833 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
834 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
835 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
836 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
837
838 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
839 depthStencilState.stencilBackWritemask);
840 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
841 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
842
843 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000844 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000845 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
846 depthStencilState.stencilBackMask);
847
848 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
849 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
850 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
851 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
852 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
853 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
854 }
855 else
856 {
857 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
858 }
859
860 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
861
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000862 mCurStencilRef = stencilRef;
863 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000864 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000865 }
866
867 mForceSetDepthStencilState = false;
868}
869
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000870void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000871{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000872 bool scissorChanged = mForceSetScissor ||
873 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
874 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000875
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000876 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000877 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000878 if (enabled)
879 {
880 RECT rect;
881 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
882 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
883 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
884 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
885 mDevice->SetScissorRect(&rect);
886 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000887
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000888 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
889
890 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000891 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000892 }
893
894 mForceSetScissor = false;
895}
896
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000897bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000898 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
899{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000900 gl::Rectangle actualViewport = viewport;
901 float actualZNear = gl::clamp01(zNear);
902 float actualZFar = gl::clamp01(zFar);
903 if (ignoreViewport)
904 {
905 actualViewport.x = 0;
906 actualViewport.y = 0;
907 actualViewport.width = mRenderTargetDesc.width;
908 actualViewport.height = mRenderTargetDesc.height;
909 actualZNear = 0.0f;
910 actualZFar = 1.0f;
911 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000912
913 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000914 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
915 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
916 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
917 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
918 dxViewport.MinZ = actualZNear;
919 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000920
921 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
922 {
923 return false; // Nothing to render
924 }
925
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000926 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
927 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000928 if (viewportChanged)
929 {
930 mDevice->SetViewport(&dxViewport);
931
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000932 mCurViewport = actualViewport;
933 mCurNear = actualZNear;
934 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000935 }
936
937 if (currentProgram && (viewportChanged || forceSetUniforms))
938 {
939 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
940 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
941 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
942
943 // These values are used for computing gl_FragCoord in Program::linkVaryings().
944 GLint coord = currentProgram->getDxCoordLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000945 GLfloat whxy[4] = { actualViewport.width * 0.5f,
946 actualViewport.height * 0.5f,
947 actualViewport.x + (actualViewport.width * 0.5f),
948 actualViewport.y + (actualViewport.height * 0.5f) };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000949 currentProgram->setUniform4fv(coord, 1, whxy);
950
951 GLint depth = currentProgram->getDxDepthLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000952 GLfloat dz[2] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000953 currentProgram->setUniform2fv(depth, 1, dz);
954
955 GLint depthRange = currentProgram->getDxDepthRangeLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000956 GLfloat nearFarDiff[3] = { actualZNear, actualZFar, actualZFar - actualZNear };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000957 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
958 }
959
960 mForceSetViewport = false;
961 return true;
962}
963
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000964bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
965{
966 switch (mode)
967 {
968 case GL_POINTS:
969 mPrimitiveType = D3DPT_POINTLIST;
970 mPrimitiveCount = count;
971 break;
972 case GL_LINES:
973 mPrimitiveType = D3DPT_LINELIST;
974 mPrimitiveCount = count / 2;
975 break;
976 case GL_LINE_LOOP:
977 mPrimitiveType = D3DPT_LINESTRIP;
978 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
979 break;
980 case GL_LINE_STRIP:
981 mPrimitiveType = D3DPT_LINESTRIP;
982 mPrimitiveCount = count - 1;
983 break;
984 case GL_TRIANGLES:
985 mPrimitiveType = D3DPT_TRIANGLELIST;
986 mPrimitiveCount = count / 3;
987 break;
988 case GL_TRIANGLE_STRIP:
989 mPrimitiveType = D3DPT_TRIANGLESTRIP;
990 mPrimitiveCount = count - 2;
991 break;
992 case GL_TRIANGLE_FAN:
993 mPrimitiveType = D3DPT_TRIANGLEFAN;
994 mPrimitiveCount = count - 2;
995 break;
996 default:
997 return error(GL_INVALID_ENUM, false);
998 }
999
1000 return mPrimitiveCount > 0;
1001}
1002
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001003
1004gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1005{
1006 if (!depthbuffer)
1007 {
1008 ERR("Unexpected null depthbuffer for depth-only FBO.");
1009 return NULL;
1010 }
1011
1012 GLsizei width = depthbuffer->getWidth();
1013 GLsizei height = depthbuffer->getHeight();
1014
1015 // search cached nullcolorbuffers
1016 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1017 {
1018 if (mNullColorbufferCache[i].buffer != NULL &&
1019 mNullColorbufferCache[i].width == width &&
1020 mNullColorbufferCache[i].height == height)
1021 {
1022 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1023 return mNullColorbufferCache[i].buffer;
1024 }
1025 }
1026
1027 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1028
1029 // add nullbuffer to the cache
1030 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1031 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1032 {
1033 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1034 {
1035 oldest = &mNullColorbufferCache[i];
1036 }
1037 }
1038
1039 delete oldest->buffer;
1040 oldest->buffer = nullbuffer;
1041 oldest->lruCount = ++mMaxNullColorbufferLRU;
1042 oldest->width = width;
1043 oldest->height = height;
1044
1045 return nullbuffer;
1046}
1047
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001048bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001049{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001050 // if there is no color attachment we must synthesize a NULL colorattachment
1051 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1052 gl::Renderbuffer *renderbufferObject = NULL;
1053 if (framebuffer->getColorbufferType() != GL_NONE)
1054 {
1055 renderbufferObject = framebuffer->getColorbuffer();
1056 }
1057 else
1058 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001059 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001060 }
1061 if (!renderbufferObject)
1062 {
1063 ERR("unable to locate renderbuffer for FBO.");
1064 return false;
1065 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001066
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001067 bool renderTargetChanged = false;
1068 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1069 if (renderTargetSerial != mAppliedRenderTargetSerial)
1070 {
1071 // Apply the render target on the device
1072 IDirect3DSurface9 *renderTargetSurface = NULL;
1073
1074 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1075 if (renderTarget)
1076 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001077 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001078 }
1079
1080 if (!renderTargetSurface)
1081 {
1082 ERR("render target pointer unexpectedly null.");
1083 return false; // Context must be lost
1084 }
1085
1086 mDevice->SetRenderTarget(0, renderTargetSurface);
1087 renderTargetSurface->Release();
1088
1089 mAppliedRenderTargetSerial = renderTargetSerial;
1090 renderTargetChanged = true;
1091 }
1092
1093 gl::Renderbuffer *depthStencil = NULL;
1094 unsigned int depthbufferSerial = 0;
1095 unsigned int stencilbufferSerial = 0;
1096 if (framebuffer->getDepthbufferType() != GL_NONE)
1097 {
1098 depthStencil = framebuffer->getDepthbuffer();
1099 if (!depthStencil)
1100 {
1101 ERR("Depth stencil pointer unexpectedly null.");
1102 return false;
1103 }
1104
1105 depthbufferSerial = depthStencil->getSerial();
1106 }
1107 else if (framebuffer->getStencilbufferType() != GL_NONE)
1108 {
1109 depthStencil = framebuffer->getStencilbuffer();
1110 if (!depthStencil)
1111 {
1112 ERR("Depth stencil pointer unexpectedly null.");
1113 return false;
1114 }
1115
1116 stencilbufferSerial = depthStencil->getSerial();
1117 }
1118
1119 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1120 stencilbufferSerial != mAppliedStencilbufferSerial ||
1121 !mDepthStencilInitialized)
1122 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001123 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001124 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001125
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001126 // Apply the depth stencil on the device
1127 if (depthStencil)
1128 {
1129 IDirect3DSurface9 *depthStencilSurface = NULL;
1130 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1131
1132 if (depthStencilRenderTarget)
1133 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001134 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001135 }
1136
1137 if (!depthStencilSurface)
1138 {
1139 ERR("depth stencil pointer unexpectedly null.");
1140 return false; // Context must be lost
1141 }
1142
1143 mDevice->SetDepthStencilSurface(depthStencilSurface);
1144 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001145
1146 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001147 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001148 }
1149 else
1150 {
1151 mDevice->SetDepthStencilSurface(NULL);
1152 }
1153
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001154 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1155 {
1156 mCurDepthSize = depthSize;
1157 mForceSetRasterState = true;
1158 }
1159
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001160 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1161 {
1162 mCurStencilSize = stencilSize;
1163 mForceSetDepthStencilState = true;
1164 }
1165
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001166 mAppliedDepthbufferSerial = depthbufferSerial;
1167 mAppliedStencilbufferSerial = stencilbufferSerial;
1168 mDepthStencilInitialized = true;
1169 }
1170
1171 if (renderTargetChanged || !mRenderTargetDescInitialized)
1172 {
1173 mForceSetScissor = true;
1174 mForceSetViewport = true;
1175
1176 mRenderTargetDesc.width = renderbufferObject->getWidth();
1177 mRenderTargetDesc.height = renderbufferObject->getHeight();
1178 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1179 mRenderTargetDescInitialized = true;
1180 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001181
1182 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001183}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001184
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001185GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001186{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001187 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001188 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1189 if (err != GL_NO_ERROR)
1190 {
1191 return err;
1192 }
1193
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001194 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1195}
1196
1197// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001198GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001199{
1200 IDirect3DIndexBuffer9 *indexBuffer;
1201 unsigned int serial;
1202 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial);
1203
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001204 if (err == GL_NO_ERROR)
1205 {
1206 if (serial != mAppliedIBSerial)
1207 {
1208 mDevice->SetIndices(indexBuffer);
1209 mAppliedIBSerial = serial;
1210 }
1211 }
1212
1213 return err;
1214}
1215
1216void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1217{
1218 startScene();
1219
1220 if (mode == GL_LINE_LOOP)
1221 {
1222 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1223 }
1224 else if (instances > 0)
1225 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001226 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001227 if (countingIB)
1228 {
1229 if (mAppliedIBSerial != countingIB->getSerial())
1230 {
1231 mDevice->SetIndices(countingIB->getBuffer());
1232 mAppliedIBSerial = countingIB->getSerial();
1233 }
1234
1235 for (int i = 0; i < mRepeatDraw; i++)
1236 {
1237 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1238 }
1239 }
1240 else
1241 {
1242 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1243 return error(GL_OUT_OF_MEMORY);
1244 }
1245 }
1246 else // Regular case
1247 {
1248 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1249 }
1250}
1251
daniel@transgaming.com31240482012-11-28 21:06:41 +00001252void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001253{
1254 startScene();
1255
1256 if (mode == GL_LINE_LOOP)
1257 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001258 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001259 }
1260 else
1261 {
1262 for (int i = 0; i < mRepeatDraw; i++)
1263 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001264 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1265 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001266 }
1267 }
1268}
1269
1270void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1271{
1272 // Get the raw indices for an indexed draw
1273 if (type != GL_NONE && elementArrayBuffer)
1274 {
1275 gl::Buffer *indexBuffer = elementArrayBuffer;
1276 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1277 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1278 }
1279
1280 UINT startIndex = 0;
1281 bool succeeded = false;
1282
1283 if (get32BitIndexSupport())
1284 {
1285 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1286
1287 if (!mLineLoopIB)
1288 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001289 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001290 }
1291
1292 if (mLineLoopIB)
1293 {
1294 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
1295
1296 UINT offset = 0;
1297 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
1298 startIndex = offset / 4;
1299
1300 if (data)
1301 {
1302 switch (type)
1303 {
1304 case GL_NONE: // Non-indexed draw
1305 for (int i = 0; i < count; i++)
1306 {
1307 data[i] = i;
1308 }
1309 data[count] = 0;
1310 break;
1311 case GL_UNSIGNED_BYTE:
1312 for (int i = 0; i < count; i++)
1313 {
1314 data[i] = static_cast<const GLubyte*>(indices)[i];
1315 }
1316 data[count] = static_cast<const GLubyte*>(indices)[0];
1317 break;
1318 case GL_UNSIGNED_SHORT:
1319 for (int i = 0; i < count; i++)
1320 {
1321 data[i] = static_cast<const GLushort*>(indices)[i];
1322 }
1323 data[count] = static_cast<const GLushort*>(indices)[0];
1324 break;
1325 case GL_UNSIGNED_INT:
1326 for (int i = 0; i < count; i++)
1327 {
1328 data[i] = static_cast<const GLuint*>(indices)[i];
1329 }
1330 data[count] = static_cast<const GLuint*>(indices)[0];
1331 break;
1332 default: UNREACHABLE();
1333 }
1334
1335 mLineLoopIB->unmap();
1336 succeeded = true;
1337 }
1338 }
1339 }
1340 else
1341 {
1342 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
1343
1344 if (!mLineLoopIB)
1345 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001346 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001347 }
1348
1349 if (mLineLoopIB)
1350 {
1351 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
1352
1353 UINT offset = 0;
1354 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
1355 startIndex = offset / 2;
1356
1357 if (data)
1358 {
1359 switch (type)
1360 {
1361 case GL_NONE: // Non-indexed draw
1362 for (int i = 0; i < count; i++)
1363 {
1364 data[i] = i;
1365 }
1366 data[count] = 0;
1367 break;
1368 case GL_UNSIGNED_BYTE:
1369 for (int i = 0; i < count; i++)
1370 {
1371 data[i] = static_cast<const GLubyte*>(indices)[i];
1372 }
1373 data[count] = static_cast<const GLubyte*>(indices)[0];
1374 break;
1375 case GL_UNSIGNED_SHORT:
1376 for (int i = 0; i < count; i++)
1377 {
1378 data[i] = static_cast<const GLushort*>(indices)[i];
1379 }
1380 data[count] = static_cast<const GLushort*>(indices)[0];
1381 break;
1382 case GL_UNSIGNED_INT:
1383 for (int i = 0; i < count; i++)
1384 {
1385 data[i] = static_cast<const GLuint*>(indices)[i];
1386 }
1387 data[count] = static_cast<const GLuint*>(indices)[0];
1388 break;
1389 default: UNREACHABLE();
1390 }
1391
1392 mLineLoopIB->unmap();
1393 succeeded = true;
1394 }
1395 }
1396 }
1397
1398 if (succeeded)
1399 {
1400 if (mAppliedIBSerial != mLineLoopIB->getSerial())
1401 {
1402 mDevice->SetIndices(mLineLoopIB->getBuffer());
1403 mAppliedIBSerial = mLineLoopIB->getSerial();
1404 }
1405
1406 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1407 }
1408 else
1409 {
1410 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
1411 return error(GL_OUT_OF_MEMORY);
1412 }
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001413}
1414
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001415void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1416{
daniel@transgaming.com95892412012-11-28 20:59:09 +00001417 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1418 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1419
1420 IDirect3DVertexShader9 *vertexShader = NULL;
1421 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
1422
1423 IDirect3DPixelShader9 *pixelShader = NULL;
1424 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001425
1426 mDevice->SetPixelShader(pixelShader);
1427 mDevice->SetVertexShader(vertexShader);
1428 programBinary->dirtyAllUniforms();
1429}
1430
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001431void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001432{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001433 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1434 gl::unorm<8>(clearParams.colorClearValue.red),
1435 gl::unorm<8>(clearParams.colorClearValue.green),
1436 gl::unorm<8>(clearParams.colorClearValue.blue));
1437 float depth = gl::clamp01(clearParams.depthClearValue);
1438 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001439
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001440 unsigned int stencilUnmasked = 0x0;
1441 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1442 {
1443 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1444 stencilUnmasked = (0x1 << stencilSize) - 1;
1445 }
1446
1447 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1448
1449 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1450 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1451 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1452 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1453 clearParams.colorMaskBlue && alphaUnmasked);
1454
1455 if (needMaskedColorClear || needMaskedStencilClear)
1456 {
1457 // State which is altered in all paths from this point to the clear call is saved.
1458 // State which is altered in only some paths will be flagged dirty in the case that
1459 // that path is taken.
1460 HRESULT hr;
1461 if (mMaskedClearSavedState == NULL)
1462 {
1463 hr = mDevice->BeginStateBlock();
1464 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1465
1466 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1467 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1468 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1469 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1470 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1471 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1472 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1473 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1474 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1475 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1476 mDevice->SetPixelShader(NULL);
1477 mDevice->SetVertexShader(NULL);
1478 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1479 mDevice->SetStreamSource(0, NULL, 0, 0);
1480 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1481 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1482 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1483 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1484 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1485 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1486 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1487
1488 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1489 {
1490 mDevice->SetStreamSourceFreq(i, 1);
1491 }
1492
1493 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1494 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1495 }
1496
1497 ASSERT(mMaskedClearSavedState != NULL);
1498
1499 if (mMaskedClearSavedState != NULL)
1500 {
1501 hr = mMaskedClearSavedState->Capture();
1502 ASSERT(SUCCEEDED(hr));
1503 }
1504
1505 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1506 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1507 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1508 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1509 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1510 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1511 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1512 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1513
1514 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1515 {
1516 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1517 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1518 clearParams.colorMaskGreen,
1519 clearParams.colorMaskBlue,
1520 clearParams.colorMaskAlpha));
1521 }
1522 else
1523 {
1524 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1525 }
1526
1527 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1528 {
1529 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1530 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1531 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1532 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1533 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1534 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1535 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1536 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1537 }
1538 else
1539 {
1540 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1541 }
1542
1543 mDevice->SetPixelShader(NULL);
1544 mDevice->SetVertexShader(NULL);
1545 mDevice->SetFVF(D3DFVF_XYZRHW);
1546 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1547 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1548 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1549 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1550 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1551 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1552 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1553
1554 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1555 {
1556 mDevice->SetStreamSourceFreq(i, 1);
1557 }
1558
1559 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1560 quad[0][0] = -0.5f;
1561 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1562 quad[0][2] = 0.0f;
1563 quad[0][3] = 1.0f;
1564
1565 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1566 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1567 quad[1][2] = 0.0f;
1568 quad[1][3] = 1.0f;
1569
1570 quad[2][0] = -0.5f;
1571 quad[2][1] = -0.5f;
1572 quad[2][2] = 0.0f;
1573 quad[2][3] = 1.0f;
1574
1575 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1576 quad[3][1] = -0.5f;
1577 quad[3][2] = 0.0f;
1578 quad[3][3] = 1.0f;
1579
1580 startScene();
1581 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1582
1583 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1584 {
1585 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1586 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1587 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1588 }
1589
1590 if (mMaskedClearSavedState != NULL)
1591 {
1592 mMaskedClearSavedState->Apply();
1593 }
1594 }
1595 else if (clearParams.mask)
1596 {
1597 DWORD dxClearFlags = 0;
1598 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1599 {
1600 dxClearFlags |= D3DCLEAR_TARGET;
1601 }
1602 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1603 {
1604 dxClearFlags |= D3DCLEAR_ZBUFFER;
1605 }
1606 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1607 {
1608 dxClearFlags |= D3DCLEAR_STENCIL;
1609 }
1610
1611 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1612 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001613}
1614
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001615void Renderer9::markAllStateDirty()
1616{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001617 mAppliedRenderTargetSerial = 0;
1618 mAppliedDepthbufferSerial = 0;
1619 mAppliedStencilbufferSerial = 0;
1620 mDepthStencilInitialized = false;
1621 mRenderTargetDescInitialized = false;
1622
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001623 mForceSetDepthStencilState = true;
1624 mForceSetRasterState = true;
1625 mForceSetBlendState = true;
1626 mForceSetScissor = true;
1627 mForceSetViewport = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001628
1629 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001630}
1631
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001632void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001633{
1634 while (!mEventQueryPool.empty())
1635 {
1636 mEventQueryPool.back()->Release();
1637 mEventQueryPool.pop_back();
1638 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001639
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001640 if (mMaskedClearSavedState)
1641 {
1642 mMaskedClearSavedState->Release();
1643 mMaskedClearSavedState = NULL;
1644 }
1645
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001646 mVertexShaderCache.clear();
1647 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001648
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001649 delete mBlit;
1650 mBlit = NULL;
1651
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001652 delete mVertexDataManager;
1653 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001654
1655 delete mIndexDataManager;
1656 mIndexDataManager = NULL;
1657
1658 delete mLineLoopIB;
1659 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001660
1661 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1662 {
1663 delete mNullColorbufferCache[i].buffer;
1664 mNullColorbufferCache[i].buffer = NULL;
1665 }
1666
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001667}
1668
1669
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001670void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001671{
1672 mDeviceLost = true;
1673}
1674
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001675bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001676{
1677 return mDeviceLost;
1678}
1679
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001680// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001681bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001682{
1683 bool isLost = false;
1684
1685 if (mDeviceEx)
1686 {
1687 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1688 }
1689 else if (mDevice)
1690 {
1691 isLost = FAILED(mDevice->TestCooperativeLevel());
1692 }
1693 else
1694 {
1695 // No device yet, so no reset required
1696 }
1697
1698 if (isLost)
1699 {
1700 // ensure we note the device loss --
1701 // we'll probably get this done again by markDeviceLost
1702 // but best to remember it!
1703 // Note that we don't want to clear the device loss status here
1704 // -- this needs to be done by resetDevice
1705 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001706 if (notify)
1707 {
1708 mDisplay->notifyDeviceLost();
1709 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001710 }
1711
1712 return isLost;
1713}
1714
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001715bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001716{
1717 HRESULT status = D3D_OK;
1718
1719 if (mDeviceEx)
1720 {
1721 status = mDeviceEx->CheckDeviceState(NULL);
1722 }
1723 else if (mDevice)
1724 {
1725 status = mDevice->TestCooperativeLevel();
1726 }
1727
1728 switch (status)
1729 {
1730 case D3DERR_DEVICENOTRESET:
1731 case D3DERR_DEVICEHUNG:
1732 return true;
1733 default:
1734 return false;
1735 }
1736}
1737
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001738bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001739{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001740 releaseDeviceResources();
1741
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001742 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1743
1744 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001745 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001746 int attempts = 3;
1747
1748 while (lost && attempts > 0)
1749 {
1750 if (mDeviceEx)
1751 {
1752 Sleep(500); // Give the graphics driver some CPU time
1753 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1754 }
1755 else
1756 {
1757 result = mDevice->TestCooperativeLevel();
1758 while (result == D3DERR_DEVICELOST)
1759 {
1760 Sleep(100); // Give the graphics driver some CPU time
1761 result = mDevice->TestCooperativeLevel();
1762 }
1763
1764 if (result == D3DERR_DEVICENOTRESET)
1765 {
1766 result = mDevice->Reset(&presentParameters);
1767 }
1768 }
1769
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001770 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001771 attempts --;
1772 }
1773
1774 if (FAILED(result))
1775 {
1776 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1777 return false;
1778 }
1779
1780 // reset device defaults
1781 initializeDevice();
1782 mDeviceLost = false;
1783
1784 return true;
1785}
1786
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001787DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001788{
1789 return mAdapterIdentifier.VendorId;
1790}
1791
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001792const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001793{
1794 return mAdapterIdentifier.Description;
1795}
1796
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001797GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001798{
1799 return mAdapterIdentifier.DeviceIdentifier;
1800}
1801
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001802void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001803{
1804 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1805 {
1806 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1807 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1808
1809 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1810 }
1811}
1812
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001813bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001814{
1815 D3DDISPLAYMODE currentDisplayMode;
1816 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1817
1818 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1819}
1820
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001821bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001822{
1823 D3DDISPLAYMODE currentDisplayMode;
1824 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1825
1826 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1827}
1828
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001829bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001830{
1831 D3DDISPLAYMODE currentDisplayMode;
1832 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1833
1834 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1835}
1836
1837// we use INTZ for depth textures in Direct3D9
1838// we also want NULL texture support to ensure the we can make depth-only FBOs
1839// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001840bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001841{
1842 D3DDISPLAYMODE currentDisplayMode;
1843 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1844
1845 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1846 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1847 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1848 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1849
1850 return intz && null;
1851}
1852
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001853bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001854{
1855 D3DDISPLAYMODE currentDisplayMode;
1856 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1857
1858 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1859 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1860 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1861 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1862
1863 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1864 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1865 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1866 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1867
1868 if (!*filtering && !*renderable)
1869 {
1870 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1871 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1872 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1873 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1874 }
1875 else
1876 {
1877 return true;
1878 }
1879}
1880
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001881bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001882{
1883 D3DDISPLAYMODE currentDisplayMode;
1884 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1885
1886 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1887 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1888 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1889 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1890
1891 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1892 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1893 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1894 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1895
1896 if (!*filtering && !*renderable)
1897 {
1898 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1899 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1900 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1901 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1902 }
1903 else
1904 {
1905 return true;
1906 }
1907}
1908
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001909bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001910{
1911 D3DDISPLAYMODE currentDisplayMode;
1912 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1913
1914 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1915}
1916
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001917bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001918{
1919 D3DDISPLAYMODE currentDisplayMode;
1920 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1921
1922 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1923}
1924
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001925bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001926{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001927 return mSupportsTextureFilterAnisotropy;
1928}
1929
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001930float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001931{
1932 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001933 {
1934 return mDeviceCaps.MaxAnisotropy;
1935 }
1936 return 1.0f;
1937}
1938
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001939bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001940{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001941 IDirect3DQuery9 *query = allocateEventQuery();
1942 if (query)
1943 {
1944 freeEventQuery(query);
1945 return true;
1946 }
1947 else
1948 {
1949 return false;
1950 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001951 return true;
1952}
1953
1954// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1955// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001956bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001957{
1958 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1959 {
1960 return false;
1961 }
1962
1963 D3DDISPLAYMODE currentDisplayMode;
1964 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1965
1966 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1967
1968 return SUCCEEDED(result);
1969}
1970
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001971bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001972{
1973 return mSupportsNonPower2Textures;
1974}
1975
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001976bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001977{
1978 if (!mDevice)
1979 {
1980 return false;
1981 }
1982
1983 IDirect3DQuery9 *query = NULL;
1984 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1985 if (SUCCEEDED(result) && query)
1986 {
1987 query->Release();
1988 return true;
1989 }
1990 else
1991 {
1992 return false;
1993 }
1994}
1995
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001996bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001997{
1998 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
1999}
2000
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002001bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002002{
2003 // PIX doesn't seem to support using share handles, so disable them.
2004 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002005 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002006}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002007
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002008int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002009{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002010 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002011}
2012
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002013float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002014{
2015 return mDeviceCaps.MaxPointSize;
2016}
2017
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002018int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002019{
2020 return (int)mDeviceCaps.MaxTextureWidth;
2021}
2022
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002023int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002024{
2025 return (int)mDeviceCaps.MaxTextureHeight;
2026}
2027
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002028bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002029{
2030 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2031}
2032
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002033DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002034{
2035 return mDeviceCaps.DeclTypes;
2036}
2037
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002038int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002039{
2040 return mMinSwapInterval;
2041}
2042
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002043int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002044{
2045 return mMaxSwapInterval;
2046}
2047
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002048int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002049{
2050 return mMaxSupportedSamples;
2051}
2052
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002053int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002054{
2055 if (requested == 0)
2056 {
2057 return requested;
2058 }
2059
2060 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2061 if (itr == mMultiSampleSupport.end())
2062 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002063 if (format == D3DFMT_UNKNOWN)
2064 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002065 return -1;
2066 }
2067
2068 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2069 {
2070 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2071 {
2072 return i;
2073 }
2074 }
2075
2076 return -1;
2077}
2078
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002079D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2080{
2081 switch (internalformat)
2082 {
2083 case GL_DEPTH_COMPONENT16:
2084 case GL_DEPTH_COMPONENT32_OES:
2085 case GL_DEPTH24_STENCIL8_OES:
2086 return D3DFMT_INTZ;
2087 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2088 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2089 return D3DFMT_DXT1;
2090 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2091 return D3DFMT_DXT3;
2092 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2093 return D3DFMT_DXT5;
2094 case GL_RGBA32F_EXT:
2095 case GL_RGB32F_EXT:
2096 case GL_ALPHA32F_EXT:
2097 case GL_LUMINANCE32F_EXT:
2098 case GL_LUMINANCE_ALPHA32F_EXT:
2099 return D3DFMT_A32B32G32R32F;
2100 case GL_RGBA16F_EXT:
2101 case GL_RGB16F_EXT:
2102 case GL_ALPHA16F_EXT:
2103 case GL_LUMINANCE16F_EXT:
2104 case GL_LUMINANCE_ALPHA16F_EXT:
2105 return D3DFMT_A16B16G16R16F;
2106 case GL_LUMINANCE8_EXT:
2107 if (getLuminanceTextureSupport())
2108 {
2109 return D3DFMT_L8;
2110 }
2111 break;
2112 case GL_LUMINANCE8_ALPHA8_EXT:
2113 if (getLuminanceAlphaTextureSupport())
2114 {
2115 return D3DFMT_A8L8;
2116 }
2117 break;
2118 case GL_RGB8_OES:
2119 case GL_RGB565:
2120 return D3DFMT_X8R8G8B8;
2121 }
2122
2123 return D3DFMT_A8R8G8B8;
2124}
2125
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002126bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002127{
2128 bool result = false;
2129
2130 if (source && dest)
2131 {
2132 int levels = source->levelCount();
2133 for (int i = 0; i < levels; ++i)
2134 {
2135 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
2136 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
2137
2138 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2139
2140 if (srcSurf) srcSurf->Release();
2141 if (dstSurf) dstSurf->Release();
2142
2143 if (!result)
2144 return false;
2145 }
2146 }
2147
2148 return result;
2149}
2150
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002151bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002152{
2153 bool result = false;
2154
2155 if (source && dest)
2156 {
2157 int levels = source->levelCount();
2158 for (int f = 0; f < 6; f++)
2159 {
2160 for (int i = 0; i < levels; i++)
2161 {
2162 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2163 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
2164
2165 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2166
2167 if (srcSurf) srcSurf->Release();
2168 if (dstSurf) dstSurf->Release();
2169
2170 if (!result)
2171 return false;
2172 }
2173 }
2174 }
2175
2176 return result;
2177}
2178
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002179D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002180{
2181 if (mD3d9Ex != NULL)
2182 {
2183 return D3DPOOL_DEFAULT;
2184 }
2185 else
2186 {
2187 if (!(usage & D3DUSAGE_DYNAMIC))
2188 {
2189 return D3DPOOL_MANAGED;
2190 }
2191 }
2192
2193 return D3DPOOL_DEFAULT;
2194}
2195
daniel@transgaming.com38380882012-11-28 19:36:39 +00002196bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2197 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002198{
2199 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
2200}
2201
daniel@transgaming.com38380882012-11-28 19:36:39 +00002202bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2203 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002204{
2205 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
2206}
2207
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002208bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2209 bool blitRenderTarget, bool blitDepthStencil)
2210{
2211 endScene();
2212
2213 if (blitRenderTarget)
2214 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002215 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2216 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2217 RenderTarget9 *readRenderTarget = NULL;
2218 RenderTarget9 *drawRenderTarget = NULL;
2219 IDirect3DSurface9* readSurface = NULL;
2220 IDirect3DSurface9* drawSurface = NULL;
2221
2222 if (readBuffer)
2223 {
2224 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2225 }
2226 if (drawBuffer)
2227 {
2228 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2229 }
2230
2231 if (readRenderTarget)
2232 {
2233 readSurface = readRenderTarget->getSurface();
2234 }
2235 if (drawRenderTarget)
2236 {
2237 drawSurface = drawRenderTarget->getSurface();
2238 }
2239
2240 if (!readSurface || !drawSurface)
2241 {
2242 ERR("Failed to retrieve the render target.");
2243 return error(GL_OUT_OF_MEMORY, false);
2244 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002245
2246 RECT srcRect, dstRect;
2247 RECT *srcRectPtr = NULL;
2248 RECT *dstRectPtr = NULL;
2249
2250 if (readRect)
2251 {
2252 srcRect.left = readRect->x;
2253 srcRect.right = readRect->x + readRect->width;
2254 srcRect.top = readRect->y;
2255 srcRect.bottom = readRect->y + readRect->height;
2256 srcRectPtr = &srcRect;
2257 }
2258
2259 if (drawRect)
2260 {
2261 dstRect.left = drawRect->x;
2262 dstRect.right = drawRect->x + drawRect->width;
2263 dstRect.top = drawRect->y;
2264 dstRect.bottom = drawRect->y + drawRect->height;
2265 dstRectPtr = &dstRect;
2266 }
2267
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002268 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002269
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002270 readSurface->Release();
2271 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002272
2273 if (FAILED(result))
2274 {
2275 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2276 return false;
2277 }
2278 }
2279
2280 if (blitDepthStencil)
2281 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002282 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2283 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2284 RenderTarget9 *readDepthStencil = NULL;
2285 RenderTarget9 *drawDepthStencil = NULL;
2286 IDirect3DSurface9* readSurface = NULL;
2287 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002288
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002289 if (readBuffer)
2290 {
2291 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2292 }
2293 if (drawBuffer)
2294 {
2295 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2296 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002297
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002298 if (readDepthStencil)
2299 {
2300 readSurface = readDepthStencil->getSurface();
2301 }
2302 if (drawDepthStencil)
2303 {
2304 drawSurface = drawDepthStencil->getSurface();
2305 }
2306
2307 if (!readSurface || !drawSurface)
2308 {
2309 ERR("Failed to retrieve the render target.");
2310 return error(GL_OUT_OF_MEMORY, false);
2311 }
2312
2313 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2314
2315 readSurface->Release();
2316 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002317
2318 if (FAILED(result))
2319 {
2320 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2321 return false;
2322 }
2323 }
2324
2325 return true;
2326}
2327
2328void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2329 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2330{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002331 RenderTarget9 *renderTarget = NULL;
2332 IDirect3DSurface9 *surface = NULL;
2333 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2334
2335 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002336 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002337 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2338 }
2339
2340 if (renderTarget)
2341 {
2342 surface = renderTarget->getSurface();
2343 }
2344
2345 if (!surface)
2346 {
2347 // context must be lost
2348 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002349 }
2350
2351 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002352 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002353
2354 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2355 {
2356 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002357 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002358 return error(GL_OUT_OF_MEMORY);
2359 }
2360
2361 HRESULT result;
2362 IDirect3DSurface9 *systemSurface = NULL;
2363 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2364 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2365 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2366 if (directToPixels)
2367 {
2368 // Use the pixels ptr as a shared handle to write directly into client's memory
2369 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2370 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2371 if (FAILED(result))
2372 {
2373 // Try again without the shared handle
2374 directToPixels = false;
2375 }
2376 }
2377
2378 if (!directToPixels)
2379 {
2380 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2381 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2382 if (FAILED(result))
2383 {
2384 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002385 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002386 return error(GL_OUT_OF_MEMORY);
2387 }
2388 }
2389
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002390 result = mDevice->GetRenderTargetData(surface, systemSurface);
2391 surface->Release();
2392 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002393
2394 if (FAILED(result))
2395 {
2396 systemSurface->Release();
2397
2398 // It turns out that D3D will sometimes produce more error
2399 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002400 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002401 return error(GL_OUT_OF_MEMORY);
2402 else
2403 {
2404 UNREACHABLE();
2405 return;
2406 }
2407
2408 }
2409
2410 if (directToPixels)
2411 {
2412 systemSurface->Release();
2413 return;
2414 }
2415
2416 RECT rect;
2417 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2418 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2419 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2420 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2421
2422 D3DLOCKED_RECT lock;
2423 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2424
2425 if (FAILED(result))
2426 {
2427 UNREACHABLE();
2428 systemSurface->Release();
2429
2430 return; // No sensible error to generate
2431 }
2432
2433 unsigned char *dest = (unsigned char*)pixels;
2434 unsigned short *dest16 = (unsigned short*)pixels;
2435
2436 unsigned char *source;
2437 int inputPitch;
2438 if (packReverseRowOrder)
2439 {
2440 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2441 inputPitch = -lock.Pitch;
2442 }
2443 else
2444 {
2445 source = (unsigned char*)lock.pBits;
2446 inputPitch = lock.Pitch;
2447 }
2448
2449 unsigned int fastPixelSize = 0;
2450
2451 if (desc.Format == D3DFMT_A8R8G8B8 &&
2452 format == GL_BGRA_EXT &&
2453 type == GL_UNSIGNED_BYTE)
2454 {
2455 fastPixelSize = 4;
2456 }
2457 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2458 format == GL_BGRA_EXT &&
2459 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2460 (desc.Format == D3DFMT_A1R5G5B5 &&
2461 format == GL_BGRA_EXT &&
2462 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2463 {
2464 fastPixelSize = 2;
2465 }
2466 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2467 format == GL_RGBA &&
2468 type == GL_HALF_FLOAT_OES)
2469 {
2470 fastPixelSize = 8;
2471 }
2472 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2473 format == GL_RGBA &&
2474 type == GL_FLOAT)
2475 {
2476 fastPixelSize = 16;
2477 }
2478
2479 for (int j = 0; j < rect.bottom - rect.top; j++)
2480 {
2481 if (fastPixelSize != 0)
2482 {
2483 // Fast path for formats which require no translation:
2484 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2485 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2486 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2487 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2488 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2489 //
2490 // Note that buffers with no alpha go through the slow path below.
2491 memcpy(dest + j * outputPitch,
2492 source + j * inputPitch,
2493 (rect.right - rect.left) * fastPixelSize);
2494 continue;
2495 }
2496
2497 for (int i = 0; i < rect.right - rect.left; i++)
2498 {
2499 float r;
2500 float g;
2501 float b;
2502 float a;
2503
2504 switch (desc.Format)
2505 {
2506 case D3DFMT_R5G6B5:
2507 {
2508 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2509
2510 a = 1.0f;
2511 b = (rgb & 0x001F) * (1.0f / 0x001F);
2512 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2513 r = (rgb & 0xF800) * (1.0f / 0xF800);
2514 }
2515 break;
2516 case D3DFMT_A1R5G5B5:
2517 {
2518 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2519
2520 a = (argb & 0x8000) ? 1.0f : 0.0f;
2521 b = (argb & 0x001F) * (1.0f / 0x001F);
2522 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2523 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2524 }
2525 break;
2526 case D3DFMT_A8R8G8B8:
2527 {
2528 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2529
2530 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2531 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2532 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2533 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2534 }
2535 break;
2536 case D3DFMT_X8R8G8B8:
2537 {
2538 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2539
2540 a = 1.0f;
2541 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2542 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2543 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2544 }
2545 break;
2546 case D3DFMT_A2R10G10B10:
2547 {
2548 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2549
2550 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2551 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2552 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2553 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2554 }
2555 break;
2556 case D3DFMT_A32B32G32R32F:
2557 {
2558 // float formats in D3D are stored rgba, rather than the other way round
2559 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2560 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2561 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2562 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2563 }
2564 break;
2565 case D3DFMT_A16B16G16R16F:
2566 {
2567 // float formats in D3D are stored rgba, rather than the other way round
2568 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2569 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2570 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2571 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2572 }
2573 break;
2574 default:
2575 UNIMPLEMENTED(); // FIXME
2576 UNREACHABLE();
2577 return;
2578 }
2579
2580 switch (format)
2581 {
2582 case GL_RGBA:
2583 switch (type)
2584 {
2585 case GL_UNSIGNED_BYTE:
2586 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2587 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2588 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2589 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2590 break;
2591 default: UNREACHABLE();
2592 }
2593 break;
2594 case GL_BGRA_EXT:
2595 switch (type)
2596 {
2597 case GL_UNSIGNED_BYTE:
2598 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2599 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2600 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2601 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2602 break;
2603 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2604 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2605 // this type is packed as follows:
2606 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2607 // --------------------------------------------------------------------------------
2608 // | 4th | 3rd | 2nd | 1st component |
2609 // --------------------------------------------------------------------------------
2610 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2611 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2612 ((unsigned short)(15 * a + 0.5f) << 12)|
2613 ((unsigned short)(15 * r + 0.5f) << 8) |
2614 ((unsigned short)(15 * g + 0.5f) << 4) |
2615 ((unsigned short)(15 * b + 0.5f) << 0);
2616 break;
2617 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2618 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2619 // this type is packed as follows:
2620 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2621 // --------------------------------------------------------------------------------
2622 // | 4th | 3rd | 2nd | 1st component |
2623 // --------------------------------------------------------------------------------
2624 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2625 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2626 ((unsigned short)( a + 0.5f) << 15) |
2627 ((unsigned short)(31 * r + 0.5f) << 10) |
2628 ((unsigned short)(31 * g + 0.5f) << 5) |
2629 ((unsigned short)(31 * b + 0.5f) << 0);
2630 break;
2631 default: UNREACHABLE();
2632 }
2633 break;
2634 case GL_RGB:
2635 switch (type)
2636 {
2637 case GL_UNSIGNED_SHORT_5_6_5:
2638 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2639 ((unsigned short)(31 * b + 0.5f) << 0) |
2640 ((unsigned short)(63 * g + 0.5f) << 5) |
2641 ((unsigned short)(31 * r + 0.5f) << 11);
2642 break;
2643 case GL_UNSIGNED_BYTE:
2644 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2645 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2646 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2647 break;
2648 default: UNREACHABLE();
2649 }
2650 break;
2651 default: UNREACHABLE();
2652 }
2653 }
2654 }
2655
2656 systemSurface->UnlockRect();
2657
2658 systemSurface->Release();
2659}
2660
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002661RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2662{
2663 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2664 IDirect3DSurface9 *surface = NULL;
2665 if (depth)
2666 {
2667 surface = swapChain9->getDepthStencil();
2668 }
2669 else
2670 {
2671 surface = swapChain9->getRenderTarget();
2672 }
2673
2674 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2675
2676 return renderTarget;
2677}
2678
2679RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2680{
2681 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2682 return renderTarget;
2683}
2684
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002685ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, GLenum type, void *data)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002686{
2687 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002688 D3DConstantTable *table = reinterpret_cast<D3DConstantTable *>(data);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002689
2690 switch (type)
2691 {
2692 case GL_VERTEX_SHADER:
2693 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002694 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002695 if (vshader)
2696 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002697 executable = new ShaderExecutable9(function, length, vshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002698 }
2699 }
2700 break;
2701 case GL_FRAGMENT_SHADER:
2702 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002703 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002704 if (pshader)
2705 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002706 executable = new ShaderExecutable9(function, length, pshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002707 }
2708 }
2709 break;
2710 default:
2711 UNREACHABLE();
2712 break;
2713 }
2714
2715 return executable;
2716}
2717
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002718ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2719{
2720 const char *profile = NULL;
2721
2722 switch (type)
2723 {
2724 case GL_VERTEX_SHADER:
2725 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
2726 break;
2727 case GL_FRAGMENT_SHADER:
2728 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
2729 break;
2730 default:
2731 UNREACHABLE();
2732 return NULL;
2733 }
2734
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002735 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002736 if (!binary)
2737 return NULL;
2738
daniel@transgaming.com31240482012-11-28 21:06:41 +00002739 D3DConstantTable *constantTable = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
daniel@transgaming.combe281b02012-11-28 21:05:48 +00002740 if (constantTable->error())
2741 {
2742 delete constantTable;
2743 binary->Release();
2744 return NULL;
2745 }
2746
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002747 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type, constantTable);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00002748 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002749
2750 return executable;
2751}
2752
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002753bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2754{
2755 return mBlit->boxFilter(source, dest);
2756}
2757
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002758D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002759{
2760 if (mD3d9Ex != NULL)
2761 {
2762 return D3DPOOL_DEFAULT;
2763 }
2764 else
2765 {
2766 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2767 {
2768 return D3DPOOL_MANAGED;
2769 }
2770 }
2771
2772 return D3DPOOL_DEFAULT;
2773}
2774
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002775bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2776{
2777 if (source && dest)
2778 {
2779 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2780 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2781
2782 if (fromManaged)
2783 {
2784 D3DSURFACE_DESC desc;
2785 source->GetDesc(&desc);
2786
2787 IDirect3DSurface9 *surf = 0;
2788 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2789
2790 if (SUCCEEDED(result))
2791 {
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002792 Image::copyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002793 result = device->UpdateSurface(surf, NULL, dest, NULL);
2794 surf->Release();
2795 }
2796 }
2797 else
2798 {
2799 endScene();
2800 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2801 }
2802
2803 if (FAILED(result))
2804 {
2805 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2806 return false;
2807 }
2808 }
2809
2810 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002811}
2812
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002813Image *Renderer9::createImage()
2814{
2815 return new Image();
2816}
2817
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002818void Renderer9::generateMipmap(Image *dest, Image *src)
2819{
2820 Image::generateMipmap(dest, src);
2821}
2822
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002823}