blob: 7fe70f7349adb25fb492084ee26a7a910119c7c5 [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.com4ba24062012-12-20 20:54:24 +000024#include "libGLESv2/renderer/Image9.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.com0393e5b2012-11-28 20:55:33 +000091 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000092
93 mVertexDataManager = NULL;
94 mIndexDataManager = NULL;
95 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +000096
97 mMaxNullColorbufferLRU = 0;
98 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
99 {
100 mNullColorbufferCache[i].lruCount = 0;
101 mNullColorbufferCache[i].width = 0;
102 mNullColorbufferCache[i].height = 0;
103 mNullColorbufferCache[i].buffer = NULL;
104 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000105}
106
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000107Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000108{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000109 releaseDeviceResources();
110
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000111 if (mDevice)
112 {
113 // 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 +0000114 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000115 {
116 resetDevice();
117 }
118
119 mDevice->Release();
120 mDevice = NULL;
121 }
122
123 if (mDeviceEx)
124 {
125 mDeviceEx->Release();
126 mDeviceEx = NULL;
127 }
128
129 if (mD3d9)
130 {
131 mD3d9->Release();
132 mD3d9 = NULL;
133 }
134
135 if (mDeviceWindow)
136 {
137 DestroyWindow(mDeviceWindow);
138 mDeviceWindow = NULL;
139 }
140
141 if (mD3d9Ex)
142 {
143 mD3d9Ex->Release();
144 mD3d9Ex = NULL;
145 }
146
147 if (mD3d9Module)
148 {
149 mD3d9Module = NULL;
150 }
151
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000152 while (!mMultiSampleSupport.empty())
153 {
154 delete [] mMultiSampleSupport.begin()->second;
155 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
156 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000157}
158
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000159Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
160{
161 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
162 return static_cast<rx::Renderer9*>(renderer);
163}
164
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000165EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000166{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000167 if (!initializeCompiler())
168 {
169 return EGL_NOT_INITIALIZED;
170 }
171
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000172 if (mSoftwareDevice)
173 {
174 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
175 }
176 else
177 {
178 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
179 }
180
181 if (mD3d9Module == NULL)
182 {
183 ERR("No D3D9 module found - aborting!\n");
184 return EGL_NOT_INITIALIZED;
185 }
186
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000187 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
188 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
189
190 // Use Direct3D9Ex if available. Among other things, this version is less
191 // inclined to report a lost context, for example when the user switches
192 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
193 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
194 {
195 ASSERT(mD3d9Ex);
196 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
197 ASSERT(mD3d9);
198 }
199 else
200 {
201 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
202 }
203
204 if (!mD3d9)
205 {
206 ERR("Could not create D3D9 device - aborting!\n");
207 return EGL_NOT_INITIALIZED;
208 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000209
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000210 if (mDc != NULL)
211 {
212 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
213 }
214
215 HRESULT result;
216
217 // Give up on getting device caps after about one second.
218 for (int i = 0; i < 10; ++i)
219 {
220 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
221 if (SUCCEEDED(result))
222 {
223 break;
224 }
225 else if (result == D3DERR_NOTAVAILABLE)
226 {
227 Sleep(100); // Give the driver some time to initialize/recover
228 }
229 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
230 {
231 ERR("failed to get device caps (0x%x)\n", result);
232 return EGL_NOT_INITIALIZED;
233 }
234 }
235
236 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
237 {
238 ERR("Renderer does not support PS 2.0. aborting!\n");
239 return EGL_NOT_INITIALIZED;
240 }
241
242 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
243 // This is required by Texture2D::convertToRenderTarget.
244 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
245 {
246 ERR("Renderer does not support stretctrect from textures!\n");
247 return EGL_NOT_INITIALIZED;
248 }
249
250 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
251
252 // ATI cards on XP have problems with non-power-of-two textures.
253 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
254 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
255 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
256 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
257
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000258 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
259 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
260
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000261 mMinSwapInterval = 4;
262 mMaxSwapInterval = 0;
263
264 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
265 {
266 mMinSwapInterval = std::min(mMinSwapInterval, 0);
267 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
268 }
269 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
270 {
271 mMinSwapInterval = std::min(mMinSwapInterval, 1);
272 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
273 }
274 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
275 {
276 mMinSwapInterval = std::min(mMinSwapInterval, 2);
277 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
278 }
279 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
280 {
281 mMinSwapInterval = std::min(mMinSwapInterval, 3);
282 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
283 }
284 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
285 {
286 mMinSwapInterval = std::min(mMinSwapInterval, 4);
287 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
288 }
289
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000290 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000291 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000292 {
293 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000294 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
295 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000296
297 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
298 {
299 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
300 {
301 max = j;
302 }
303 }
304 }
305
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000306 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000307 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000308 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000309 continue;
310
311 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000312 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
313 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000314
315 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
316 {
317 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
318 {
319 max = j;
320 }
321 }
322 }
323
324 mMaxSupportedSamples = max;
325
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000326 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
327 static const TCHAR className[] = TEXT("STATIC");
328
329 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
330
331 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
332 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
333
334 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
335 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
336 {
337 return EGL_BAD_ALLOC;
338 }
339
340 if (FAILED(result))
341 {
342 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
343
344 if (FAILED(result))
345 {
346 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
347 return EGL_BAD_ALLOC;
348 }
349 }
350
351 if (mD3d9Ex)
352 {
353 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
354 ASSERT(SUCCEEDED(result));
355 }
356
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000357 mVertexShaderCache.initialize(mDevice);
358 mPixelShaderCache.initialize(mDevice);
359
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000360 initializeDevice();
361
362 return EGL_SUCCESS;
363}
364
365// do any one-time device initialization
366// NOTE: this is also needed after a device lost/reset
367// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000368void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000369{
370 // Permanent non-default states
371 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
372 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
373
374 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
375 {
376 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
377 }
378 else
379 {
380 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
381 }
382
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000383 markAllStateDirty();
384
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000385 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000386
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000387 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000388 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000389 mVertexDataManager = new rx::VertexDataManager(this);
390 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000391}
392
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000393D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000394{
395 D3DPRESENT_PARAMETERS presentParameters = {0};
396
397 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
398 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
399 presentParameters.BackBufferCount = 1;
400 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
401 presentParameters.BackBufferWidth = 1;
402 presentParameters.BackBufferHeight = 1;
403 presentParameters.EnableAutoDepthStencil = FALSE;
404 presentParameters.Flags = 0;
405 presentParameters.hDeviceWindow = mDeviceWindow;
406 presentParameters.MultiSampleQuality = 0;
407 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
408 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
409 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
410 presentParameters.Windowed = TRUE;
411
412 return presentParameters;
413}
414
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000415int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000416{
417 D3DDISPLAYMODE currentDisplayMode;
418 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
419
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000420 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
421 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000422 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
423 int numConfigs = 0;
424
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000425 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000426 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000427 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000428
429 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
430
431 if (SUCCEEDED(result))
432 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000434 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000435 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000436 HRESULT result = D3D_OK;
437
438 if(depthStencilFormat != D3DFMT_UNKNOWN)
439 {
440 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
441 }
442
443 if (SUCCEEDED(result))
444 {
445 if(depthStencilFormat != D3DFMT_UNKNOWN)
446 {
447 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
448 }
449
450 if (SUCCEEDED(result))
451 {
452 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000453 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
454 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000455 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
456 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
457
458 (*configDescList)[numConfigs++] = newConfig;
459 }
460 }
461 }
462 }
463 }
464
465 return numConfigs;
466}
467
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000468void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000469{
470 delete [] (configDescList);
471}
472
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000473void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000474{
475 if (!mSceneStarted)
476 {
477 long result = mDevice->BeginScene();
478 if (SUCCEEDED(result)) {
479 // This is defensive checking against the device being
480 // lost at unexpected times.
481 mSceneStarted = true;
482 }
483 }
484}
485
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000486void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000487{
488 if (mSceneStarted)
489 {
490 // EndScene can fail if the device was lost, for example due
491 // to a TDR during a draw call.
492 mDevice->EndScene();
493 mSceneStarted = false;
494 }
495}
496
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000497// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000498void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000499{
500 HRESULT result;
501
502 IDirect3DQuery9* query = allocateEventQuery();
503 if (!query)
504 {
505 return;
506 }
507
508 result = query->Issue(D3DISSUE_END);
509 ASSERT(SUCCEEDED(result));
510
511 do
512 {
513 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
514
515 if(block && result == S_FALSE)
516 {
517 // Keep polling, but allow other threads to do something useful first
518 Sleep(0);
519 // explicitly check for device loss
520 // some drivers seem to return S_FALSE even if the device is lost
521 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000522 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000523 {
524 result = D3DERR_DEVICELOST;
525 }
526 }
527 }
528 while(block && result == S_FALSE);
529
530 freeEventQuery(query);
531
532 if (isDeviceLostError(result))
533 {
534 mDisplay->notifyDeviceLost();
535 }
536}
537
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000538SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
539{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000540 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000541}
542
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000543// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000544IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000545{
546 IDirect3DQuery9 *query = NULL;
547
548 if (mEventQueryPool.empty())
549 {
550 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
551 ASSERT(SUCCEEDED(result));
552 }
553 else
554 {
555 query = mEventQueryPool.back();
556 mEventQueryPool.pop_back();
557 }
558
559 return query;
560}
561
562// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000563void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000564{
565 if (mEventQueryPool.size() > 1000)
566 {
567 query->Release();
568 }
569 else
570 {
571 mEventQueryPool.push_back(query);
572 }
573}
574
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000575IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000576{
577 return mVertexShaderCache.create(function, length);
578}
579
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000580IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000581{
582 return mPixelShaderCache.create(function, length);
583}
584
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000585HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
586{
587 D3DPOOL Pool = getBufferPool(Usage);
588 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
589}
590
591HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
592{
593 D3DPOOL Pool = getBufferPool(Usage);
594 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
595}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000596
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000597void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000598{
599 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
600 int d3dSampler = index + d3dSamplerOffset;
601
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000602 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
603 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000604
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000605 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000606 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000607 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000608 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
609 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
610 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
611 if (mSupportsTextureFilterAnisotropy)
612 {
613 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
614 }
615}
616
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000617void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000618{
619 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
620 int d3dSampler = index + d3dSamplerOffset;
621 IDirect3DBaseTexture9 *d3dTexture = NULL;
622
623 if (texture)
624 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000625 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000626 if (texStorage)
627 {
628 d3dTexture = texStorage->getBaseTexture();
629 }
630 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000631 // in the texture class and we're unexpectedly missing the d3d texture
632 ASSERT(d3dTexture != NULL);
633 }
634
635 mDevice->SetTexture(d3dSampler, d3dTexture);
636}
637
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000638void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000639{
640 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000641
642 if (rasterStateChanged)
643 {
644 // Set the cull mode
645 if (rasterState.cullFace)
646 {
647 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
648 }
649 else
650 {
651 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
652 }
653
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000654 if (rasterState.polygonOffsetFill)
655 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000656 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000657 {
658 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
659
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000660 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000661 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
662 }
663 }
664 else
665 {
666 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
667 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
668 }
669
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000670 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000671 }
672
673 mForceSetRasterState = false;
674}
675
676void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
677{
678 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
679 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
680 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
681
682 if (blendStateChanged || blendColorChanged)
683 {
684 if (blendState.blend)
685 {
686 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
687
688 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
689 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
690 {
691 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
692 }
693 else
694 {
695 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
696 gl::unorm<8>(blendColor.alpha),
697 gl::unorm<8>(blendColor.alpha),
698 gl::unorm<8>(blendColor.alpha)));
699 }
700
701 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
702 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
703 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
704
705 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
706 blendState.destBlendRGB != blendState.destBlendAlpha ||
707 blendState.blendEquationRGB != blendState.blendEquationAlpha)
708 {
709 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
710
711 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
712 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
713 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
714 }
715 else
716 {
717 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
718 }
719 }
720 else
721 {
722 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
723 }
724
725 if (blendState.sampleAlphaToCoverage)
726 {
727 FIXME("Sample alpha to coverage is unimplemented.");
728 }
729
730 // Set the color mask
731 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
732 // Apparently some ATI cards have a bug where a draw with a zero color
733 // write mask can cause later draws to have incorrect results. Instead,
734 // set a nonzero color write mask but modify the blend state so that no
735 // drawing is done.
736 // http://code.google.com/p/angleproject/issues/detail?id=169
737
738 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
739 blendState.colorMaskBlue, blendState.colorMaskAlpha);
740 if (colorMask == 0 && !zeroColorMaskAllowed)
741 {
742 // Enable green channel, but set blending so nothing will be drawn.
743 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
744 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
745
746 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
747 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
748 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
749 }
750 else
751 {
752 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
753 }
754
755 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
756
757 mCurBlendState = blendState;
758 mCurBlendColor = blendColor;
759 }
760
761 if (sampleMaskChanged)
762 {
763 // Set the multisample mask
764 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
765 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
766
767 mCurSampleMask = sampleMask;
768 }
769
770 mForceSetBlendState = false;
771}
772
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000773void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000774 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000775{
776 bool depthStencilStateChanged = mForceSetDepthStencilState ||
777 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000778 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
779 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000780 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000781
782 if (depthStencilStateChanged)
783 {
784 if (depthStencilState.depthTest)
785 {
786 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
787 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
788 }
789 else
790 {
791 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
792 }
793
794 mCurDepthStencilState = depthStencilState;
795 }
796
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000797 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000798 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000799 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000800 {
801 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
802 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
803
804 // FIXME: Unsupported by D3D9
805 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
806 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
807 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
808 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000809 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000810 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
811 {
812 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
813 return error(GL_INVALID_OPERATION);
814 }
815
816 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000817 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000818
819 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
820 depthStencilState.stencilWritemask);
821 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
822 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
823
824 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000825 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000826 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
827 depthStencilState.stencilMask);
828
829 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
830 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
831 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
832 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
833 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
834 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
835
836 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
837 depthStencilState.stencilBackWritemask);
838 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
839 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
840
841 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000842 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000843 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
844 depthStencilState.stencilBackMask);
845
846 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
847 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
848 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
849 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
850 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
851 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
852 }
853 else
854 {
855 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
856 }
857
858 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
859
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000860 mCurStencilRef = stencilRef;
861 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000862 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000863 }
864
865 mForceSetDepthStencilState = false;
866}
867
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000868void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000869{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000870 bool scissorChanged = mForceSetScissor ||
871 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
872 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000873
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000874 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000875 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000876 if (enabled)
877 {
878 RECT rect;
879 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
880 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
881 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
882 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
883 mDevice->SetScissorRect(&rect);
884 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000885
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000886 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
887
888 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000889 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000890 }
891
892 mForceSetScissor = false;
893}
894
daniel@transgaming.com12985182012-12-20 20:56:31 +0000895bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
896 bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000897{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000898 gl::Rectangle actualViewport = viewport;
899 float actualZNear = gl::clamp01(zNear);
900 float actualZFar = gl::clamp01(zFar);
901 if (ignoreViewport)
902 {
903 actualViewport.x = 0;
904 actualViewport.y = 0;
905 actualViewport.width = mRenderTargetDesc.width;
906 actualViewport.height = mRenderTargetDesc.height;
907 actualZNear = 0.0f;
908 actualZFar = 1.0f;
909 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000910
911 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000912 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
913 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
914 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
915 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
916 dxViewport.MinZ = actualZNear;
917 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000918
919 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
920 {
921 return false; // Nothing to render
922 }
923
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000924 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
925 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000926 if (viewportChanged)
927 {
928 mDevice->SetViewport(&dxViewport);
929
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000930 mCurViewport = actualViewport;
931 mCurNear = actualZNear;
932 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000933 }
934
935 if (currentProgram && (viewportChanged || forceSetUniforms))
936 {
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000937 currentProgram->applyDxHalfPixelSize(1.0f / dxViewport.Width, -1.0f / dxViewport.Height);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000938
939 // These values are used for computing gl_FragCoord in Program::linkVaryings().
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000940 currentProgram->applyDxCoord(actualViewport.width * 0.5f,
941 actualViewport.height * 0.5f,
942 actualViewport.x + (actualViewport.width * 0.5f),
943 actualViewport.y + (actualViewport.height * 0.5f));
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000944
daniel@transgaming.com12985182012-12-20 20:56:31 +0000945 GLfloat ccw = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000946 currentProgram->applyDxDepthFront((actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f, ccw);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000947
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000948 currentProgram->applyDxDepthRange(actualZNear, actualZFar, actualZFar - actualZNear);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000949 }
950
951 mForceSetViewport = false;
952 return true;
953}
954
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000955bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
956{
957 switch (mode)
958 {
959 case GL_POINTS:
960 mPrimitiveType = D3DPT_POINTLIST;
961 mPrimitiveCount = count;
962 break;
963 case GL_LINES:
964 mPrimitiveType = D3DPT_LINELIST;
965 mPrimitiveCount = count / 2;
966 break;
967 case GL_LINE_LOOP:
968 mPrimitiveType = D3DPT_LINESTRIP;
969 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
970 break;
971 case GL_LINE_STRIP:
972 mPrimitiveType = D3DPT_LINESTRIP;
973 mPrimitiveCount = count - 1;
974 break;
975 case GL_TRIANGLES:
976 mPrimitiveType = D3DPT_TRIANGLELIST;
977 mPrimitiveCount = count / 3;
978 break;
979 case GL_TRIANGLE_STRIP:
980 mPrimitiveType = D3DPT_TRIANGLESTRIP;
981 mPrimitiveCount = count - 2;
982 break;
983 case GL_TRIANGLE_FAN:
984 mPrimitiveType = D3DPT_TRIANGLEFAN;
985 mPrimitiveCount = count - 2;
986 break;
987 default:
988 return error(GL_INVALID_ENUM, false);
989 }
990
991 return mPrimitiveCount > 0;
992}
993
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000994
995gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
996{
997 if (!depthbuffer)
998 {
999 ERR("Unexpected null depthbuffer for depth-only FBO.");
1000 return NULL;
1001 }
1002
1003 GLsizei width = depthbuffer->getWidth();
1004 GLsizei height = depthbuffer->getHeight();
1005
1006 // search cached nullcolorbuffers
1007 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1008 {
1009 if (mNullColorbufferCache[i].buffer != NULL &&
1010 mNullColorbufferCache[i].width == width &&
1011 mNullColorbufferCache[i].height == height)
1012 {
1013 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1014 return mNullColorbufferCache[i].buffer;
1015 }
1016 }
1017
1018 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1019
1020 // add nullbuffer to the cache
1021 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1022 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1023 {
1024 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1025 {
1026 oldest = &mNullColorbufferCache[i];
1027 }
1028 }
1029
1030 delete oldest->buffer;
1031 oldest->buffer = nullbuffer;
1032 oldest->lruCount = ++mMaxNullColorbufferLRU;
1033 oldest->width = width;
1034 oldest->height = height;
1035
1036 return nullbuffer;
1037}
1038
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001039bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001040{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001041 // if there is no color attachment we must synthesize a NULL colorattachment
1042 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1043 gl::Renderbuffer *renderbufferObject = NULL;
1044 if (framebuffer->getColorbufferType() != GL_NONE)
1045 {
1046 renderbufferObject = framebuffer->getColorbuffer();
1047 }
1048 else
1049 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001050 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001051 }
1052 if (!renderbufferObject)
1053 {
1054 ERR("unable to locate renderbuffer for FBO.");
1055 return false;
1056 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001057
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001058 bool renderTargetChanged = false;
1059 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1060 if (renderTargetSerial != mAppliedRenderTargetSerial)
1061 {
1062 // Apply the render target on the device
1063 IDirect3DSurface9 *renderTargetSurface = NULL;
1064
1065 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1066 if (renderTarget)
1067 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001068 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001069 }
1070
1071 if (!renderTargetSurface)
1072 {
1073 ERR("render target pointer unexpectedly null.");
1074 return false; // Context must be lost
1075 }
1076
1077 mDevice->SetRenderTarget(0, renderTargetSurface);
1078 renderTargetSurface->Release();
1079
1080 mAppliedRenderTargetSerial = renderTargetSerial;
1081 renderTargetChanged = true;
1082 }
1083
1084 gl::Renderbuffer *depthStencil = NULL;
1085 unsigned int depthbufferSerial = 0;
1086 unsigned int stencilbufferSerial = 0;
1087 if (framebuffer->getDepthbufferType() != GL_NONE)
1088 {
1089 depthStencil = framebuffer->getDepthbuffer();
1090 if (!depthStencil)
1091 {
1092 ERR("Depth stencil pointer unexpectedly null.");
1093 return false;
1094 }
1095
1096 depthbufferSerial = depthStencil->getSerial();
1097 }
1098 else if (framebuffer->getStencilbufferType() != GL_NONE)
1099 {
1100 depthStencil = framebuffer->getStencilbuffer();
1101 if (!depthStencil)
1102 {
1103 ERR("Depth stencil pointer unexpectedly null.");
1104 return false;
1105 }
1106
1107 stencilbufferSerial = depthStencil->getSerial();
1108 }
1109
1110 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1111 stencilbufferSerial != mAppliedStencilbufferSerial ||
1112 !mDepthStencilInitialized)
1113 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001114 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001115 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001116
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001117 // Apply the depth stencil on the device
1118 if (depthStencil)
1119 {
1120 IDirect3DSurface9 *depthStencilSurface = NULL;
1121 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1122
1123 if (depthStencilRenderTarget)
1124 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001125 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001126 }
1127
1128 if (!depthStencilSurface)
1129 {
1130 ERR("depth stencil pointer unexpectedly null.");
1131 return false; // Context must be lost
1132 }
1133
1134 mDevice->SetDepthStencilSurface(depthStencilSurface);
1135 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001136
1137 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001138 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001139 }
1140 else
1141 {
1142 mDevice->SetDepthStencilSurface(NULL);
1143 }
1144
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001145 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1146 {
1147 mCurDepthSize = depthSize;
1148 mForceSetRasterState = true;
1149 }
1150
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001151 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1152 {
1153 mCurStencilSize = stencilSize;
1154 mForceSetDepthStencilState = true;
1155 }
1156
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001157 mAppliedDepthbufferSerial = depthbufferSerial;
1158 mAppliedStencilbufferSerial = stencilbufferSerial;
1159 mDepthStencilInitialized = true;
1160 }
1161
1162 if (renderTargetChanged || !mRenderTargetDescInitialized)
1163 {
1164 mForceSetScissor = true;
1165 mForceSetViewport = true;
1166
1167 mRenderTargetDesc.width = renderbufferObject->getWidth();
1168 mRenderTargetDesc.height = renderbufferObject->getHeight();
1169 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1170 mRenderTargetDescInitialized = true;
1171 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001172
1173 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001174}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001175
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001176GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001177{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001178 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001179 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1180 if (err != GL_NO_ERROR)
1181 {
1182 return err;
1183 }
1184
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001185 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1186}
1187
1188// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001189GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001190{
1191 IDirect3DIndexBuffer9 *indexBuffer;
1192 unsigned int serial;
1193 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial);
1194
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001195 if (err == GL_NO_ERROR)
1196 {
1197 if (serial != mAppliedIBSerial)
1198 {
1199 mDevice->SetIndices(indexBuffer);
1200 mAppliedIBSerial = serial;
1201 }
1202 }
1203
1204 return err;
1205}
1206
1207void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1208{
1209 startScene();
1210
1211 if (mode == GL_LINE_LOOP)
1212 {
1213 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1214 }
1215 else if (instances > 0)
1216 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001217 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001218 if (countingIB)
1219 {
1220 if (mAppliedIBSerial != countingIB->getSerial())
1221 {
1222 mDevice->SetIndices(countingIB->getBuffer());
1223 mAppliedIBSerial = countingIB->getSerial();
1224 }
1225
1226 for (int i = 0; i < mRepeatDraw; i++)
1227 {
1228 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1229 }
1230 }
1231 else
1232 {
1233 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1234 return error(GL_OUT_OF_MEMORY);
1235 }
1236 }
1237 else // Regular case
1238 {
1239 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1240 }
1241}
1242
daniel@transgaming.com31240482012-11-28 21:06:41 +00001243void 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 +00001244{
1245 startScene();
1246
1247 if (mode == GL_LINE_LOOP)
1248 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001249 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001250 }
1251 else
1252 {
1253 for (int i = 0; i < mRepeatDraw; i++)
1254 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001255 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1256 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001257 }
1258 }
1259}
1260
1261void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1262{
1263 // Get the raw indices for an indexed draw
1264 if (type != GL_NONE && elementArrayBuffer)
1265 {
1266 gl::Buffer *indexBuffer = elementArrayBuffer;
1267 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1268 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1269 }
1270
1271 UINT startIndex = 0;
1272 bool succeeded = false;
1273
1274 if (get32BitIndexSupport())
1275 {
1276 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1277
1278 if (!mLineLoopIB)
1279 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001280 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001281 }
1282
1283 if (mLineLoopIB)
1284 {
1285 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
1286
1287 UINT offset = 0;
1288 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
1289 startIndex = offset / 4;
1290
1291 if (data)
1292 {
1293 switch (type)
1294 {
1295 case GL_NONE: // Non-indexed draw
1296 for (int i = 0; i < count; i++)
1297 {
1298 data[i] = i;
1299 }
1300 data[count] = 0;
1301 break;
1302 case GL_UNSIGNED_BYTE:
1303 for (int i = 0; i < count; i++)
1304 {
1305 data[i] = static_cast<const GLubyte*>(indices)[i];
1306 }
1307 data[count] = static_cast<const GLubyte*>(indices)[0];
1308 break;
1309 case GL_UNSIGNED_SHORT:
1310 for (int i = 0; i < count; i++)
1311 {
1312 data[i] = static_cast<const GLushort*>(indices)[i];
1313 }
1314 data[count] = static_cast<const GLushort*>(indices)[0];
1315 break;
1316 case GL_UNSIGNED_INT:
1317 for (int i = 0; i < count; i++)
1318 {
1319 data[i] = static_cast<const GLuint*>(indices)[i];
1320 }
1321 data[count] = static_cast<const GLuint*>(indices)[0];
1322 break;
1323 default: UNREACHABLE();
1324 }
1325
1326 mLineLoopIB->unmap();
1327 succeeded = true;
1328 }
1329 }
1330 }
1331 else
1332 {
1333 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
1334
1335 if (!mLineLoopIB)
1336 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001337 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001338 }
1339
1340 if (mLineLoopIB)
1341 {
1342 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
1343
1344 UINT offset = 0;
1345 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
1346 startIndex = offset / 2;
1347
1348 if (data)
1349 {
1350 switch (type)
1351 {
1352 case GL_NONE: // Non-indexed draw
1353 for (int i = 0; i < count; i++)
1354 {
1355 data[i] = i;
1356 }
1357 data[count] = 0;
1358 break;
1359 case GL_UNSIGNED_BYTE:
1360 for (int i = 0; i < count; i++)
1361 {
1362 data[i] = static_cast<const GLubyte*>(indices)[i];
1363 }
1364 data[count] = static_cast<const GLubyte*>(indices)[0];
1365 break;
1366 case GL_UNSIGNED_SHORT:
1367 for (int i = 0; i < count; i++)
1368 {
1369 data[i] = static_cast<const GLushort*>(indices)[i];
1370 }
1371 data[count] = static_cast<const GLushort*>(indices)[0];
1372 break;
1373 case GL_UNSIGNED_INT:
1374 for (int i = 0; i < count; i++)
1375 {
1376 data[i] = static_cast<const GLuint*>(indices)[i];
1377 }
1378 data[count] = static_cast<const GLuint*>(indices)[0];
1379 break;
1380 default: UNREACHABLE();
1381 }
1382
1383 mLineLoopIB->unmap();
1384 succeeded = true;
1385 }
1386 }
1387 }
1388
1389 if (succeeded)
1390 {
1391 if (mAppliedIBSerial != mLineLoopIB->getSerial())
1392 {
1393 mDevice->SetIndices(mLineLoopIB->getBuffer());
1394 mAppliedIBSerial = mLineLoopIB->getSerial();
1395 }
1396
1397 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1398 }
1399 else
1400 {
1401 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
1402 return error(GL_OUT_OF_MEMORY);
1403 }
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001404}
1405
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001406void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1407{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001408 unsigned int programBinarySerial = programBinary->getSerial();
1409 if (programBinarySerial != mAppliedProgramBinarySerial)
1410 {
1411 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1412 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001413
daniel@transgaming.come4991412012-12-20 20:55:34 +00001414 IDirect3DVertexShader9 *vertexShader = NULL;
1415 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001416
daniel@transgaming.come4991412012-12-20 20:55:34 +00001417 IDirect3DPixelShader9 *pixelShader = NULL;
1418 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001419
daniel@transgaming.come4991412012-12-20 20:55:34 +00001420 mDevice->SetPixelShader(pixelShader);
1421 mDevice->SetVertexShader(vertexShader);
1422 programBinary->dirtyAllUniforms();
1423
1424 mAppliedProgramBinarySerial = programBinarySerial;
1425 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001426}
1427
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001428void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001429{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001430 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1431 gl::unorm<8>(clearParams.colorClearValue.red),
1432 gl::unorm<8>(clearParams.colorClearValue.green),
1433 gl::unorm<8>(clearParams.colorClearValue.blue));
1434 float depth = gl::clamp01(clearParams.depthClearValue);
1435 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001436
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001437 unsigned int stencilUnmasked = 0x0;
1438 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1439 {
1440 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1441 stencilUnmasked = (0x1 << stencilSize) - 1;
1442 }
1443
1444 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1445
1446 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1447 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1448 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1449 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1450 clearParams.colorMaskBlue && alphaUnmasked);
1451
1452 if (needMaskedColorClear || needMaskedStencilClear)
1453 {
1454 // State which is altered in all paths from this point to the clear call is saved.
1455 // State which is altered in only some paths will be flagged dirty in the case that
1456 // that path is taken.
1457 HRESULT hr;
1458 if (mMaskedClearSavedState == NULL)
1459 {
1460 hr = mDevice->BeginStateBlock();
1461 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1462
1463 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1464 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1465 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1466 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1467 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1468 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1469 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1470 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1471 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1472 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1473 mDevice->SetPixelShader(NULL);
1474 mDevice->SetVertexShader(NULL);
1475 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1476 mDevice->SetStreamSource(0, NULL, 0, 0);
1477 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1478 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1479 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1480 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1481 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1482 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1483 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1484
1485 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1486 {
1487 mDevice->SetStreamSourceFreq(i, 1);
1488 }
1489
1490 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1491 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1492 }
1493
1494 ASSERT(mMaskedClearSavedState != NULL);
1495
1496 if (mMaskedClearSavedState != NULL)
1497 {
1498 hr = mMaskedClearSavedState->Capture();
1499 ASSERT(SUCCEEDED(hr));
1500 }
1501
1502 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1503 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1504 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1505 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1506 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1507 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1508 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1509 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1510
1511 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1512 {
1513 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1514 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1515 clearParams.colorMaskGreen,
1516 clearParams.colorMaskBlue,
1517 clearParams.colorMaskAlpha));
1518 }
1519 else
1520 {
1521 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1522 }
1523
1524 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1525 {
1526 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1527 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1528 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1529 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1530 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1531 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1532 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1533 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1534 }
1535 else
1536 {
1537 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1538 }
1539
1540 mDevice->SetPixelShader(NULL);
1541 mDevice->SetVertexShader(NULL);
1542 mDevice->SetFVF(D3DFVF_XYZRHW);
1543 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1544 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1545 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1546 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1547 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1548 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1549 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1550
1551 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1552 {
1553 mDevice->SetStreamSourceFreq(i, 1);
1554 }
1555
1556 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1557 quad[0][0] = -0.5f;
1558 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1559 quad[0][2] = 0.0f;
1560 quad[0][3] = 1.0f;
1561
1562 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1563 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1564 quad[1][2] = 0.0f;
1565 quad[1][3] = 1.0f;
1566
1567 quad[2][0] = -0.5f;
1568 quad[2][1] = -0.5f;
1569 quad[2][2] = 0.0f;
1570 quad[2][3] = 1.0f;
1571
1572 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1573 quad[3][1] = -0.5f;
1574 quad[3][2] = 0.0f;
1575 quad[3][3] = 1.0f;
1576
1577 startScene();
1578 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1579
1580 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1581 {
1582 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1583 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1584 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1585 }
1586
1587 if (mMaskedClearSavedState != NULL)
1588 {
1589 mMaskedClearSavedState->Apply();
1590 }
1591 }
1592 else if (clearParams.mask)
1593 {
1594 DWORD dxClearFlags = 0;
1595 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1596 {
1597 dxClearFlags |= D3DCLEAR_TARGET;
1598 }
1599 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1600 {
1601 dxClearFlags |= D3DCLEAR_ZBUFFER;
1602 }
1603 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1604 {
1605 dxClearFlags |= D3DCLEAR_STENCIL;
1606 }
1607
1608 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1609 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001610}
1611
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001612void Renderer9::markAllStateDirty()
1613{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001614 mAppliedRenderTargetSerial = 0;
1615 mAppliedDepthbufferSerial = 0;
1616 mAppliedStencilbufferSerial = 0;
1617 mDepthStencilInitialized = false;
1618 mRenderTargetDescInitialized = false;
1619
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001620 mForceSetDepthStencilState = true;
1621 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001622 mForceSetScissor = true;
1623 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001624 mForceSetBlendState = true;
1625
1626 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001627 mAppliedProgramBinarySerial = 0;
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.com4ba24062012-12-20 20:54:24 +00002792 Image9::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{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002815 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002816}
2817
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002818void Renderer9::generateMipmap(Image *dest, Image *src)
2819{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002820 Image9 *src9 = Image9::makeImage9(src);
2821 Image9 *dst9 = Image9::makeImage9(dest);
2822 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002823}
2824
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002825}