blob: b2e2b70a507a9729775d8701362948a28df6d186 [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.com4c4ce232012-11-28 21:01:40 +0000895bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000896 gl::ProgramBinary *currentProgram, bool forceSetUniforms)
897{
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 {
937 GLint halfPixelSize = currentProgram->getDxHalfPixelSizeLocation();
938 GLfloat xy[2] = { 1.0f / dxViewport.Width, -1.0f / dxViewport.Height };
939 currentProgram->setUniform2fv(halfPixelSize, 1, xy);
940
941 // These values are used for computing gl_FragCoord in Program::linkVaryings().
942 GLint coord = currentProgram->getDxCoordLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000943 GLfloat whxy[4] = { actualViewport.width * 0.5f,
944 actualViewport.height * 0.5f,
945 actualViewport.x + (actualViewport.width * 0.5f),
946 actualViewport.y + (actualViewport.height * 0.5f) };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000947 currentProgram->setUniform4fv(coord, 1, whxy);
948
949 GLint depth = currentProgram->getDxDepthLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000950 GLfloat dz[2] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000951 currentProgram->setUniform2fv(depth, 1, dz);
952
953 GLint depthRange = currentProgram->getDxDepthRangeLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000954 GLfloat nearFarDiff[3] = { actualZNear, actualZFar, actualZFar - actualZNear };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000955 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
956 }
957
958 mForceSetViewport = false;
959 return true;
960}
961
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000962bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
963{
964 switch (mode)
965 {
966 case GL_POINTS:
967 mPrimitiveType = D3DPT_POINTLIST;
968 mPrimitiveCount = count;
969 break;
970 case GL_LINES:
971 mPrimitiveType = D3DPT_LINELIST;
972 mPrimitiveCount = count / 2;
973 break;
974 case GL_LINE_LOOP:
975 mPrimitiveType = D3DPT_LINESTRIP;
976 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
977 break;
978 case GL_LINE_STRIP:
979 mPrimitiveType = D3DPT_LINESTRIP;
980 mPrimitiveCount = count - 1;
981 break;
982 case GL_TRIANGLES:
983 mPrimitiveType = D3DPT_TRIANGLELIST;
984 mPrimitiveCount = count / 3;
985 break;
986 case GL_TRIANGLE_STRIP:
987 mPrimitiveType = D3DPT_TRIANGLESTRIP;
988 mPrimitiveCount = count - 2;
989 break;
990 case GL_TRIANGLE_FAN:
991 mPrimitiveType = D3DPT_TRIANGLEFAN;
992 mPrimitiveCount = count - 2;
993 break;
994 default:
995 return error(GL_INVALID_ENUM, false);
996 }
997
998 return mPrimitiveCount > 0;
999}
1000
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001001
1002gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1003{
1004 if (!depthbuffer)
1005 {
1006 ERR("Unexpected null depthbuffer for depth-only FBO.");
1007 return NULL;
1008 }
1009
1010 GLsizei width = depthbuffer->getWidth();
1011 GLsizei height = depthbuffer->getHeight();
1012
1013 // search cached nullcolorbuffers
1014 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1015 {
1016 if (mNullColorbufferCache[i].buffer != NULL &&
1017 mNullColorbufferCache[i].width == width &&
1018 mNullColorbufferCache[i].height == height)
1019 {
1020 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1021 return mNullColorbufferCache[i].buffer;
1022 }
1023 }
1024
1025 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1026
1027 // add nullbuffer to the cache
1028 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1029 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1030 {
1031 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1032 {
1033 oldest = &mNullColorbufferCache[i];
1034 }
1035 }
1036
1037 delete oldest->buffer;
1038 oldest->buffer = nullbuffer;
1039 oldest->lruCount = ++mMaxNullColorbufferLRU;
1040 oldest->width = width;
1041 oldest->height = height;
1042
1043 return nullbuffer;
1044}
1045
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001046bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001047{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001048 // if there is no color attachment we must synthesize a NULL colorattachment
1049 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1050 gl::Renderbuffer *renderbufferObject = NULL;
1051 if (framebuffer->getColorbufferType() != GL_NONE)
1052 {
1053 renderbufferObject = framebuffer->getColorbuffer();
1054 }
1055 else
1056 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001057 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001058 }
1059 if (!renderbufferObject)
1060 {
1061 ERR("unable to locate renderbuffer for FBO.");
1062 return false;
1063 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001064
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001065 bool renderTargetChanged = false;
1066 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1067 if (renderTargetSerial != mAppliedRenderTargetSerial)
1068 {
1069 // Apply the render target on the device
1070 IDirect3DSurface9 *renderTargetSurface = NULL;
1071
1072 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1073 if (renderTarget)
1074 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001075 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001076 }
1077
1078 if (!renderTargetSurface)
1079 {
1080 ERR("render target pointer unexpectedly null.");
1081 return false; // Context must be lost
1082 }
1083
1084 mDevice->SetRenderTarget(0, renderTargetSurface);
1085 renderTargetSurface->Release();
1086
1087 mAppliedRenderTargetSerial = renderTargetSerial;
1088 renderTargetChanged = true;
1089 }
1090
1091 gl::Renderbuffer *depthStencil = NULL;
1092 unsigned int depthbufferSerial = 0;
1093 unsigned int stencilbufferSerial = 0;
1094 if (framebuffer->getDepthbufferType() != GL_NONE)
1095 {
1096 depthStencil = framebuffer->getDepthbuffer();
1097 if (!depthStencil)
1098 {
1099 ERR("Depth stencil pointer unexpectedly null.");
1100 return false;
1101 }
1102
1103 depthbufferSerial = depthStencil->getSerial();
1104 }
1105 else if (framebuffer->getStencilbufferType() != GL_NONE)
1106 {
1107 depthStencil = framebuffer->getStencilbuffer();
1108 if (!depthStencil)
1109 {
1110 ERR("Depth stencil pointer unexpectedly null.");
1111 return false;
1112 }
1113
1114 stencilbufferSerial = depthStencil->getSerial();
1115 }
1116
1117 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1118 stencilbufferSerial != mAppliedStencilbufferSerial ||
1119 !mDepthStencilInitialized)
1120 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001121 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001122 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001123
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001124 // Apply the depth stencil on the device
1125 if (depthStencil)
1126 {
1127 IDirect3DSurface9 *depthStencilSurface = NULL;
1128 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1129
1130 if (depthStencilRenderTarget)
1131 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001132 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001133 }
1134
1135 if (!depthStencilSurface)
1136 {
1137 ERR("depth stencil pointer unexpectedly null.");
1138 return false; // Context must be lost
1139 }
1140
1141 mDevice->SetDepthStencilSurface(depthStencilSurface);
1142 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001143
1144 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001145 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001146 }
1147 else
1148 {
1149 mDevice->SetDepthStencilSurface(NULL);
1150 }
1151
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001152 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1153 {
1154 mCurDepthSize = depthSize;
1155 mForceSetRasterState = true;
1156 }
1157
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001158 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1159 {
1160 mCurStencilSize = stencilSize;
1161 mForceSetDepthStencilState = true;
1162 }
1163
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001164 mAppliedDepthbufferSerial = depthbufferSerial;
1165 mAppliedStencilbufferSerial = stencilbufferSerial;
1166 mDepthStencilInitialized = true;
1167 }
1168
1169 if (renderTargetChanged || !mRenderTargetDescInitialized)
1170 {
1171 mForceSetScissor = true;
1172 mForceSetViewport = true;
1173
1174 mRenderTargetDesc.width = renderbufferObject->getWidth();
1175 mRenderTargetDesc.height = renderbufferObject->getHeight();
1176 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1177 mRenderTargetDescInitialized = true;
1178 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001179
1180 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001181}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001182
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001183GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001184{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001185 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001186 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1187 if (err != GL_NO_ERROR)
1188 {
1189 return err;
1190 }
1191
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001192 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1193}
1194
1195// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001196GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001197{
1198 IDirect3DIndexBuffer9 *indexBuffer;
1199 unsigned int serial;
1200 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial);
1201
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001202 if (err == GL_NO_ERROR)
1203 {
1204 if (serial != mAppliedIBSerial)
1205 {
1206 mDevice->SetIndices(indexBuffer);
1207 mAppliedIBSerial = serial;
1208 }
1209 }
1210
1211 return err;
1212}
1213
1214void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1215{
1216 startScene();
1217
1218 if (mode == GL_LINE_LOOP)
1219 {
1220 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1221 }
1222 else if (instances > 0)
1223 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001224 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001225 if (countingIB)
1226 {
1227 if (mAppliedIBSerial != countingIB->getSerial())
1228 {
1229 mDevice->SetIndices(countingIB->getBuffer());
1230 mAppliedIBSerial = countingIB->getSerial();
1231 }
1232
1233 for (int i = 0; i < mRepeatDraw; i++)
1234 {
1235 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1236 }
1237 }
1238 else
1239 {
1240 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1241 return error(GL_OUT_OF_MEMORY);
1242 }
1243 }
1244 else // Regular case
1245 {
1246 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1247 }
1248}
1249
daniel@transgaming.com31240482012-11-28 21:06:41 +00001250void 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 +00001251{
1252 startScene();
1253
1254 if (mode == GL_LINE_LOOP)
1255 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001256 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001257 }
1258 else
1259 {
1260 for (int i = 0; i < mRepeatDraw; i++)
1261 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001262 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1263 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001264 }
1265 }
1266}
1267
1268void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1269{
1270 // Get the raw indices for an indexed draw
1271 if (type != GL_NONE && elementArrayBuffer)
1272 {
1273 gl::Buffer *indexBuffer = elementArrayBuffer;
1274 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1275 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1276 }
1277
1278 UINT startIndex = 0;
1279 bool succeeded = false;
1280
1281 if (get32BitIndexSupport())
1282 {
1283 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1284
1285 if (!mLineLoopIB)
1286 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001287 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001288 }
1289
1290 if (mLineLoopIB)
1291 {
1292 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
1293
1294 UINT offset = 0;
1295 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
1296 startIndex = offset / 4;
1297
1298 if (data)
1299 {
1300 switch (type)
1301 {
1302 case GL_NONE: // Non-indexed draw
1303 for (int i = 0; i < count; i++)
1304 {
1305 data[i] = i;
1306 }
1307 data[count] = 0;
1308 break;
1309 case GL_UNSIGNED_BYTE:
1310 for (int i = 0; i < count; i++)
1311 {
1312 data[i] = static_cast<const GLubyte*>(indices)[i];
1313 }
1314 data[count] = static_cast<const GLubyte*>(indices)[0];
1315 break;
1316 case GL_UNSIGNED_SHORT:
1317 for (int i = 0; i < count; i++)
1318 {
1319 data[i] = static_cast<const GLushort*>(indices)[i];
1320 }
1321 data[count] = static_cast<const GLushort*>(indices)[0];
1322 break;
1323 case GL_UNSIGNED_INT:
1324 for (int i = 0; i < count; i++)
1325 {
1326 data[i] = static_cast<const GLuint*>(indices)[i];
1327 }
1328 data[count] = static_cast<const GLuint*>(indices)[0];
1329 break;
1330 default: UNREACHABLE();
1331 }
1332
1333 mLineLoopIB->unmap();
1334 succeeded = true;
1335 }
1336 }
1337 }
1338 else
1339 {
1340 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
1341
1342 if (!mLineLoopIB)
1343 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001344 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001345 }
1346
1347 if (mLineLoopIB)
1348 {
1349 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
1350
1351 UINT offset = 0;
1352 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
1353 startIndex = offset / 2;
1354
1355 if (data)
1356 {
1357 switch (type)
1358 {
1359 case GL_NONE: // Non-indexed draw
1360 for (int i = 0; i < count; i++)
1361 {
1362 data[i] = i;
1363 }
1364 data[count] = 0;
1365 break;
1366 case GL_UNSIGNED_BYTE:
1367 for (int i = 0; i < count; i++)
1368 {
1369 data[i] = static_cast<const GLubyte*>(indices)[i];
1370 }
1371 data[count] = static_cast<const GLubyte*>(indices)[0];
1372 break;
1373 case GL_UNSIGNED_SHORT:
1374 for (int i = 0; i < count; i++)
1375 {
1376 data[i] = static_cast<const GLushort*>(indices)[i];
1377 }
1378 data[count] = static_cast<const GLushort*>(indices)[0];
1379 break;
1380 case GL_UNSIGNED_INT:
1381 for (int i = 0; i < count; i++)
1382 {
1383 data[i] = static_cast<const GLuint*>(indices)[i];
1384 }
1385 data[count] = static_cast<const GLuint*>(indices)[0];
1386 break;
1387 default: UNREACHABLE();
1388 }
1389
1390 mLineLoopIB->unmap();
1391 succeeded = true;
1392 }
1393 }
1394 }
1395
1396 if (succeeded)
1397 {
1398 if (mAppliedIBSerial != mLineLoopIB->getSerial())
1399 {
1400 mDevice->SetIndices(mLineLoopIB->getBuffer());
1401 mAppliedIBSerial = mLineLoopIB->getSerial();
1402 }
1403
1404 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1405 }
1406 else
1407 {
1408 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
1409 return error(GL_OUT_OF_MEMORY);
1410 }
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001411}
1412
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001413void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1414{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001415 unsigned int programBinarySerial = programBinary->getSerial();
1416 if (programBinarySerial != mAppliedProgramBinarySerial)
1417 {
1418 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1419 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001420
daniel@transgaming.come4991412012-12-20 20:55:34 +00001421 IDirect3DVertexShader9 *vertexShader = NULL;
1422 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001423
daniel@transgaming.come4991412012-12-20 20:55:34 +00001424 IDirect3DPixelShader9 *pixelShader = NULL;
1425 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001426
daniel@transgaming.come4991412012-12-20 20:55:34 +00001427 mDevice->SetPixelShader(pixelShader);
1428 mDevice->SetVertexShader(vertexShader);
1429 programBinary->dirtyAllUniforms();
1430
1431 mAppliedProgramBinarySerial = programBinarySerial;
1432 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001433}
1434
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001435void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001436{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001437 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1438 gl::unorm<8>(clearParams.colorClearValue.red),
1439 gl::unorm<8>(clearParams.colorClearValue.green),
1440 gl::unorm<8>(clearParams.colorClearValue.blue));
1441 float depth = gl::clamp01(clearParams.depthClearValue);
1442 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001443
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001444 unsigned int stencilUnmasked = 0x0;
1445 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1446 {
1447 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1448 stencilUnmasked = (0x1 << stencilSize) - 1;
1449 }
1450
1451 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1452
1453 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1454 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1455 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1456 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1457 clearParams.colorMaskBlue && alphaUnmasked);
1458
1459 if (needMaskedColorClear || needMaskedStencilClear)
1460 {
1461 // State which is altered in all paths from this point to the clear call is saved.
1462 // State which is altered in only some paths will be flagged dirty in the case that
1463 // that path is taken.
1464 HRESULT hr;
1465 if (mMaskedClearSavedState == NULL)
1466 {
1467 hr = mDevice->BeginStateBlock();
1468 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1469
1470 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1471 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1472 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1473 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1474 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1475 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1476 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1477 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1478 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1479 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1480 mDevice->SetPixelShader(NULL);
1481 mDevice->SetVertexShader(NULL);
1482 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1483 mDevice->SetStreamSource(0, NULL, 0, 0);
1484 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1485 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1486 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1487 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1488 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1489 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1490 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1491
1492 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1493 {
1494 mDevice->SetStreamSourceFreq(i, 1);
1495 }
1496
1497 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1498 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1499 }
1500
1501 ASSERT(mMaskedClearSavedState != NULL);
1502
1503 if (mMaskedClearSavedState != NULL)
1504 {
1505 hr = mMaskedClearSavedState->Capture();
1506 ASSERT(SUCCEEDED(hr));
1507 }
1508
1509 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1510 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1511 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1512 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1513 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1514 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1515 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1516 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1517
1518 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1519 {
1520 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1521 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1522 clearParams.colorMaskGreen,
1523 clearParams.colorMaskBlue,
1524 clearParams.colorMaskAlpha));
1525 }
1526 else
1527 {
1528 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1529 }
1530
1531 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1532 {
1533 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1534 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1535 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1536 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1537 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1538 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1539 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1540 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1541 }
1542 else
1543 {
1544 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1545 }
1546
1547 mDevice->SetPixelShader(NULL);
1548 mDevice->SetVertexShader(NULL);
1549 mDevice->SetFVF(D3DFVF_XYZRHW);
1550 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1551 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1552 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1553 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1554 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1555 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1556 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1557
1558 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1559 {
1560 mDevice->SetStreamSourceFreq(i, 1);
1561 }
1562
1563 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1564 quad[0][0] = -0.5f;
1565 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1566 quad[0][2] = 0.0f;
1567 quad[0][3] = 1.0f;
1568
1569 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1570 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1571 quad[1][2] = 0.0f;
1572 quad[1][3] = 1.0f;
1573
1574 quad[2][0] = -0.5f;
1575 quad[2][1] = -0.5f;
1576 quad[2][2] = 0.0f;
1577 quad[2][3] = 1.0f;
1578
1579 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1580 quad[3][1] = -0.5f;
1581 quad[3][2] = 0.0f;
1582 quad[3][3] = 1.0f;
1583
1584 startScene();
1585 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1586
1587 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1588 {
1589 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1590 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1591 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1592 }
1593
1594 if (mMaskedClearSavedState != NULL)
1595 {
1596 mMaskedClearSavedState->Apply();
1597 }
1598 }
1599 else if (clearParams.mask)
1600 {
1601 DWORD dxClearFlags = 0;
1602 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1603 {
1604 dxClearFlags |= D3DCLEAR_TARGET;
1605 }
1606 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1607 {
1608 dxClearFlags |= D3DCLEAR_ZBUFFER;
1609 }
1610 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1611 {
1612 dxClearFlags |= D3DCLEAR_STENCIL;
1613 }
1614
1615 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1616 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001617}
1618
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001619void Renderer9::markAllStateDirty()
1620{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001621 mAppliedRenderTargetSerial = 0;
1622 mAppliedDepthbufferSerial = 0;
1623 mAppliedStencilbufferSerial = 0;
1624 mDepthStencilInitialized = false;
1625 mRenderTargetDescInitialized = false;
1626
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001627 mForceSetDepthStencilState = true;
1628 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001629 mForceSetScissor = true;
1630 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001631 mForceSetBlendState = true;
1632
1633 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001634 mAppliedProgramBinarySerial = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001635
1636 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001637}
1638
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001639void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001640{
1641 while (!mEventQueryPool.empty())
1642 {
1643 mEventQueryPool.back()->Release();
1644 mEventQueryPool.pop_back();
1645 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001646
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001647 if (mMaskedClearSavedState)
1648 {
1649 mMaskedClearSavedState->Release();
1650 mMaskedClearSavedState = NULL;
1651 }
1652
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001653 mVertexShaderCache.clear();
1654 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001655
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001656 delete mBlit;
1657 mBlit = NULL;
1658
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001659 delete mVertexDataManager;
1660 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001661
1662 delete mIndexDataManager;
1663 mIndexDataManager = NULL;
1664
1665 delete mLineLoopIB;
1666 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001667
1668 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1669 {
1670 delete mNullColorbufferCache[i].buffer;
1671 mNullColorbufferCache[i].buffer = NULL;
1672 }
1673
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001674}
1675
1676
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001677void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001678{
1679 mDeviceLost = true;
1680}
1681
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001682bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001683{
1684 return mDeviceLost;
1685}
1686
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001687// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001688bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001689{
1690 bool isLost = false;
1691
1692 if (mDeviceEx)
1693 {
1694 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1695 }
1696 else if (mDevice)
1697 {
1698 isLost = FAILED(mDevice->TestCooperativeLevel());
1699 }
1700 else
1701 {
1702 // No device yet, so no reset required
1703 }
1704
1705 if (isLost)
1706 {
1707 // ensure we note the device loss --
1708 // we'll probably get this done again by markDeviceLost
1709 // but best to remember it!
1710 // Note that we don't want to clear the device loss status here
1711 // -- this needs to be done by resetDevice
1712 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001713 if (notify)
1714 {
1715 mDisplay->notifyDeviceLost();
1716 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001717 }
1718
1719 return isLost;
1720}
1721
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001722bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001723{
1724 HRESULT status = D3D_OK;
1725
1726 if (mDeviceEx)
1727 {
1728 status = mDeviceEx->CheckDeviceState(NULL);
1729 }
1730 else if (mDevice)
1731 {
1732 status = mDevice->TestCooperativeLevel();
1733 }
1734
1735 switch (status)
1736 {
1737 case D3DERR_DEVICENOTRESET:
1738 case D3DERR_DEVICEHUNG:
1739 return true;
1740 default:
1741 return false;
1742 }
1743}
1744
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001745bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001746{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001747 releaseDeviceResources();
1748
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001749 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1750
1751 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001752 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001753 int attempts = 3;
1754
1755 while (lost && attempts > 0)
1756 {
1757 if (mDeviceEx)
1758 {
1759 Sleep(500); // Give the graphics driver some CPU time
1760 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1761 }
1762 else
1763 {
1764 result = mDevice->TestCooperativeLevel();
1765 while (result == D3DERR_DEVICELOST)
1766 {
1767 Sleep(100); // Give the graphics driver some CPU time
1768 result = mDevice->TestCooperativeLevel();
1769 }
1770
1771 if (result == D3DERR_DEVICENOTRESET)
1772 {
1773 result = mDevice->Reset(&presentParameters);
1774 }
1775 }
1776
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001777 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001778 attempts --;
1779 }
1780
1781 if (FAILED(result))
1782 {
1783 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1784 return false;
1785 }
1786
1787 // reset device defaults
1788 initializeDevice();
1789 mDeviceLost = false;
1790
1791 return true;
1792}
1793
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001794DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001795{
1796 return mAdapterIdentifier.VendorId;
1797}
1798
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001799const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001800{
1801 return mAdapterIdentifier.Description;
1802}
1803
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001804GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001805{
1806 return mAdapterIdentifier.DeviceIdentifier;
1807}
1808
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001809void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001810{
1811 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1812 {
1813 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1814 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1815
1816 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1817 }
1818}
1819
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001820bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001821{
1822 D3DDISPLAYMODE currentDisplayMode;
1823 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1824
1825 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1826}
1827
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001828bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001829{
1830 D3DDISPLAYMODE currentDisplayMode;
1831 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1832
1833 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1834}
1835
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001836bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001837{
1838 D3DDISPLAYMODE currentDisplayMode;
1839 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1840
1841 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1842}
1843
1844// we use INTZ for depth textures in Direct3D9
1845// we also want NULL texture support to ensure the we can make depth-only FBOs
1846// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001847bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001848{
1849 D3DDISPLAYMODE currentDisplayMode;
1850 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1851
1852 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1853 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1854 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1855 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1856
1857 return intz && null;
1858}
1859
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001860bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001861{
1862 D3DDISPLAYMODE currentDisplayMode;
1863 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1864
1865 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1866 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1867 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1868 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1869
1870 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1871 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1872 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1873 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1874
1875 if (!*filtering && !*renderable)
1876 {
1877 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1878 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1879 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1880 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1881 }
1882 else
1883 {
1884 return true;
1885 }
1886}
1887
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001888bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001889{
1890 D3DDISPLAYMODE currentDisplayMode;
1891 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1892
1893 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1894 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1895 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1896 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1897
1898 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1899 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1900 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1901 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1902
1903 if (!*filtering && !*renderable)
1904 {
1905 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1906 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1907 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1908 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1909 }
1910 else
1911 {
1912 return true;
1913 }
1914}
1915
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001916bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001917{
1918 D3DDISPLAYMODE currentDisplayMode;
1919 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1920
1921 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1922}
1923
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001924bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001925{
1926 D3DDISPLAYMODE currentDisplayMode;
1927 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1928
1929 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1930}
1931
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001932bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001933{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001934 return mSupportsTextureFilterAnisotropy;
1935}
1936
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001937float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001938{
1939 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001940 {
1941 return mDeviceCaps.MaxAnisotropy;
1942 }
1943 return 1.0f;
1944}
1945
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001946bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001947{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001948 IDirect3DQuery9 *query = allocateEventQuery();
1949 if (query)
1950 {
1951 freeEventQuery(query);
1952 return true;
1953 }
1954 else
1955 {
1956 return false;
1957 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001958 return true;
1959}
1960
1961// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1962// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001963bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001964{
1965 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1966 {
1967 return false;
1968 }
1969
1970 D3DDISPLAYMODE currentDisplayMode;
1971 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1972
1973 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1974
1975 return SUCCEEDED(result);
1976}
1977
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001978bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001979{
1980 return mSupportsNonPower2Textures;
1981}
1982
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001983bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001984{
1985 if (!mDevice)
1986 {
1987 return false;
1988 }
1989
1990 IDirect3DQuery9 *query = NULL;
1991 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1992 if (SUCCEEDED(result) && query)
1993 {
1994 query->Release();
1995 return true;
1996 }
1997 else
1998 {
1999 return false;
2000 }
2001}
2002
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002003bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002004{
2005 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2006}
2007
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002008bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002009{
2010 // PIX doesn't seem to support using share handles, so disable them.
2011 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002012 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002013}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002014
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002015int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002016{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002017 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002018}
2019
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002020float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002021{
2022 return mDeviceCaps.MaxPointSize;
2023}
2024
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002025int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002026{
2027 return (int)mDeviceCaps.MaxTextureWidth;
2028}
2029
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002030int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002031{
2032 return (int)mDeviceCaps.MaxTextureHeight;
2033}
2034
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002035bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002036{
2037 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2038}
2039
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002040DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002041{
2042 return mDeviceCaps.DeclTypes;
2043}
2044
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002045int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002046{
2047 return mMinSwapInterval;
2048}
2049
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002050int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002051{
2052 return mMaxSwapInterval;
2053}
2054
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002055int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002056{
2057 return mMaxSupportedSamples;
2058}
2059
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002060int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002061{
2062 if (requested == 0)
2063 {
2064 return requested;
2065 }
2066
2067 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2068 if (itr == mMultiSampleSupport.end())
2069 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002070 if (format == D3DFMT_UNKNOWN)
2071 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002072 return -1;
2073 }
2074
2075 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2076 {
2077 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2078 {
2079 return i;
2080 }
2081 }
2082
2083 return -1;
2084}
2085
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002086D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2087{
2088 switch (internalformat)
2089 {
2090 case GL_DEPTH_COMPONENT16:
2091 case GL_DEPTH_COMPONENT32_OES:
2092 case GL_DEPTH24_STENCIL8_OES:
2093 return D3DFMT_INTZ;
2094 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2095 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2096 return D3DFMT_DXT1;
2097 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2098 return D3DFMT_DXT3;
2099 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2100 return D3DFMT_DXT5;
2101 case GL_RGBA32F_EXT:
2102 case GL_RGB32F_EXT:
2103 case GL_ALPHA32F_EXT:
2104 case GL_LUMINANCE32F_EXT:
2105 case GL_LUMINANCE_ALPHA32F_EXT:
2106 return D3DFMT_A32B32G32R32F;
2107 case GL_RGBA16F_EXT:
2108 case GL_RGB16F_EXT:
2109 case GL_ALPHA16F_EXT:
2110 case GL_LUMINANCE16F_EXT:
2111 case GL_LUMINANCE_ALPHA16F_EXT:
2112 return D3DFMT_A16B16G16R16F;
2113 case GL_LUMINANCE8_EXT:
2114 if (getLuminanceTextureSupport())
2115 {
2116 return D3DFMT_L8;
2117 }
2118 break;
2119 case GL_LUMINANCE8_ALPHA8_EXT:
2120 if (getLuminanceAlphaTextureSupport())
2121 {
2122 return D3DFMT_A8L8;
2123 }
2124 break;
2125 case GL_RGB8_OES:
2126 case GL_RGB565:
2127 return D3DFMT_X8R8G8B8;
2128 }
2129
2130 return D3DFMT_A8R8G8B8;
2131}
2132
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002133bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002134{
2135 bool result = false;
2136
2137 if (source && dest)
2138 {
2139 int levels = source->levelCount();
2140 for (int i = 0; i < levels; ++i)
2141 {
2142 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
2143 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
2144
2145 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2146
2147 if (srcSurf) srcSurf->Release();
2148 if (dstSurf) dstSurf->Release();
2149
2150 if (!result)
2151 return false;
2152 }
2153 }
2154
2155 return result;
2156}
2157
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002158bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002159{
2160 bool result = false;
2161
2162 if (source && dest)
2163 {
2164 int levels = source->levelCount();
2165 for (int f = 0; f < 6; f++)
2166 {
2167 for (int i = 0; i < levels; i++)
2168 {
2169 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2170 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
2171
2172 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2173
2174 if (srcSurf) srcSurf->Release();
2175 if (dstSurf) dstSurf->Release();
2176
2177 if (!result)
2178 return false;
2179 }
2180 }
2181 }
2182
2183 return result;
2184}
2185
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002186D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002187{
2188 if (mD3d9Ex != NULL)
2189 {
2190 return D3DPOOL_DEFAULT;
2191 }
2192 else
2193 {
2194 if (!(usage & D3DUSAGE_DYNAMIC))
2195 {
2196 return D3DPOOL_MANAGED;
2197 }
2198 }
2199
2200 return D3DPOOL_DEFAULT;
2201}
2202
daniel@transgaming.com38380882012-11-28 19:36:39 +00002203bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2204 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002205{
2206 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
2207}
2208
daniel@transgaming.com38380882012-11-28 19:36:39 +00002209bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2210 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002211{
2212 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
2213}
2214
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002215bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2216 bool blitRenderTarget, bool blitDepthStencil)
2217{
2218 endScene();
2219
2220 if (blitRenderTarget)
2221 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002222 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2223 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2224 RenderTarget9 *readRenderTarget = NULL;
2225 RenderTarget9 *drawRenderTarget = NULL;
2226 IDirect3DSurface9* readSurface = NULL;
2227 IDirect3DSurface9* drawSurface = NULL;
2228
2229 if (readBuffer)
2230 {
2231 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2232 }
2233 if (drawBuffer)
2234 {
2235 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2236 }
2237
2238 if (readRenderTarget)
2239 {
2240 readSurface = readRenderTarget->getSurface();
2241 }
2242 if (drawRenderTarget)
2243 {
2244 drawSurface = drawRenderTarget->getSurface();
2245 }
2246
2247 if (!readSurface || !drawSurface)
2248 {
2249 ERR("Failed to retrieve the render target.");
2250 return error(GL_OUT_OF_MEMORY, false);
2251 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002252
2253 RECT srcRect, dstRect;
2254 RECT *srcRectPtr = NULL;
2255 RECT *dstRectPtr = NULL;
2256
2257 if (readRect)
2258 {
2259 srcRect.left = readRect->x;
2260 srcRect.right = readRect->x + readRect->width;
2261 srcRect.top = readRect->y;
2262 srcRect.bottom = readRect->y + readRect->height;
2263 srcRectPtr = &srcRect;
2264 }
2265
2266 if (drawRect)
2267 {
2268 dstRect.left = drawRect->x;
2269 dstRect.right = drawRect->x + drawRect->width;
2270 dstRect.top = drawRect->y;
2271 dstRect.bottom = drawRect->y + drawRect->height;
2272 dstRectPtr = &dstRect;
2273 }
2274
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002275 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002276
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002277 readSurface->Release();
2278 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002279
2280 if (FAILED(result))
2281 {
2282 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2283 return false;
2284 }
2285 }
2286
2287 if (blitDepthStencil)
2288 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002289 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2290 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2291 RenderTarget9 *readDepthStencil = NULL;
2292 RenderTarget9 *drawDepthStencil = NULL;
2293 IDirect3DSurface9* readSurface = NULL;
2294 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002295
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002296 if (readBuffer)
2297 {
2298 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2299 }
2300 if (drawBuffer)
2301 {
2302 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2303 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002304
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002305 if (readDepthStencil)
2306 {
2307 readSurface = readDepthStencil->getSurface();
2308 }
2309 if (drawDepthStencil)
2310 {
2311 drawSurface = drawDepthStencil->getSurface();
2312 }
2313
2314 if (!readSurface || !drawSurface)
2315 {
2316 ERR("Failed to retrieve the render target.");
2317 return error(GL_OUT_OF_MEMORY, false);
2318 }
2319
2320 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2321
2322 readSurface->Release();
2323 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002324
2325 if (FAILED(result))
2326 {
2327 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2328 return false;
2329 }
2330 }
2331
2332 return true;
2333}
2334
2335void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2336 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2337{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002338 RenderTarget9 *renderTarget = NULL;
2339 IDirect3DSurface9 *surface = NULL;
2340 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2341
2342 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002343 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002344 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2345 }
2346
2347 if (renderTarget)
2348 {
2349 surface = renderTarget->getSurface();
2350 }
2351
2352 if (!surface)
2353 {
2354 // context must be lost
2355 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002356 }
2357
2358 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002359 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002360
2361 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2362 {
2363 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002364 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002365 return error(GL_OUT_OF_MEMORY);
2366 }
2367
2368 HRESULT result;
2369 IDirect3DSurface9 *systemSurface = NULL;
2370 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2371 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2372 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2373 if (directToPixels)
2374 {
2375 // Use the pixels ptr as a shared handle to write directly into client's memory
2376 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2377 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2378 if (FAILED(result))
2379 {
2380 // Try again without the shared handle
2381 directToPixels = false;
2382 }
2383 }
2384
2385 if (!directToPixels)
2386 {
2387 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2388 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2389 if (FAILED(result))
2390 {
2391 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002392 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002393 return error(GL_OUT_OF_MEMORY);
2394 }
2395 }
2396
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002397 result = mDevice->GetRenderTargetData(surface, systemSurface);
2398 surface->Release();
2399 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002400
2401 if (FAILED(result))
2402 {
2403 systemSurface->Release();
2404
2405 // It turns out that D3D will sometimes produce more error
2406 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002407 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002408 return error(GL_OUT_OF_MEMORY);
2409 else
2410 {
2411 UNREACHABLE();
2412 return;
2413 }
2414
2415 }
2416
2417 if (directToPixels)
2418 {
2419 systemSurface->Release();
2420 return;
2421 }
2422
2423 RECT rect;
2424 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2425 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2426 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2427 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2428
2429 D3DLOCKED_RECT lock;
2430 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2431
2432 if (FAILED(result))
2433 {
2434 UNREACHABLE();
2435 systemSurface->Release();
2436
2437 return; // No sensible error to generate
2438 }
2439
2440 unsigned char *dest = (unsigned char*)pixels;
2441 unsigned short *dest16 = (unsigned short*)pixels;
2442
2443 unsigned char *source;
2444 int inputPitch;
2445 if (packReverseRowOrder)
2446 {
2447 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2448 inputPitch = -lock.Pitch;
2449 }
2450 else
2451 {
2452 source = (unsigned char*)lock.pBits;
2453 inputPitch = lock.Pitch;
2454 }
2455
2456 unsigned int fastPixelSize = 0;
2457
2458 if (desc.Format == D3DFMT_A8R8G8B8 &&
2459 format == GL_BGRA_EXT &&
2460 type == GL_UNSIGNED_BYTE)
2461 {
2462 fastPixelSize = 4;
2463 }
2464 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2465 format == GL_BGRA_EXT &&
2466 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2467 (desc.Format == D3DFMT_A1R5G5B5 &&
2468 format == GL_BGRA_EXT &&
2469 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2470 {
2471 fastPixelSize = 2;
2472 }
2473 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2474 format == GL_RGBA &&
2475 type == GL_HALF_FLOAT_OES)
2476 {
2477 fastPixelSize = 8;
2478 }
2479 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2480 format == GL_RGBA &&
2481 type == GL_FLOAT)
2482 {
2483 fastPixelSize = 16;
2484 }
2485
2486 for (int j = 0; j < rect.bottom - rect.top; j++)
2487 {
2488 if (fastPixelSize != 0)
2489 {
2490 // Fast path for formats which require no translation:
2491 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2492 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2493 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2494 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2495 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2496 //
2497 // Note that buffers with no alpha go through the slow path below.
2498 memcpy(dest + j * outputPitch,
2499 source + j * inputPitch,
2500 (rect.right - rect.left) * fastPixelSize);
2501 continue;
2502 }
2503
2504 for (int i = 0; i < rect.right - rect.left; i++)
2505 {
2506 float r;
2507 float g;
2508 float b;
2509 float a;
2510
2511 switch (desc.Format)
2512 {
2513 case D3DFMT_R5G6B5:
2514 {
2515 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2516
2517 a = 1.0f;
2518 b = (rgb & 0x001F) * (1.0f / 0x001F);
2519 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2520 r = (rgb & 0xF800) * (1.0f / 0xF800);
2521 }
2522 break;
2523 case D3DFMT_A1R5G5B5:
2524 {
2525 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2526
2527 a = (argb & 0x8000) ? 1.0f : 0.0f;
2528 b = (argb & 0x001F) * (1.0f / 0x001F);
2529 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2530 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2531 }
2532 break;
2533 case D3DFMT_A8R8G8B8:
2534 {
2535 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2536
2537 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2538 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2539 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2540 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2541 }
2542 break;
2543 case D3DFMT_X8R8G8B8:
2544 {
2545 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2546
2547 a = 1.0f;
2548 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2549 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2550 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2551 }
2552 break;
2553 case D3DFMT_A2R10G10B10:
2554 {
2555 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2556
2557 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2558 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2559 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2560 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2561 }
2562 break;
2563 case D3DFMT_A32B32G32R32F:
2564 {
2565 // float formats in D3D are stored rgba, rather than the other way round
2566 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2567 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2568 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2569 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2570 }
2571 break;
2572 case D3DFMT_A16B16G16R16F:
2573 {
2574 // float formats in D3D are stored rgba, rather than the other way round
2575 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2576 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2577 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2578 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2579 }
2580 break;
2581 default:
2582 UNIMPLEMENTED(); // FIXME
2583 UNREACHABLE();
2584 return;
2585 }
2586
2587 switch (format)
2588 {
2589 case GL_RGBA:
2590 switch (type)
2591 {
2592 case GL_UNSIGNED_BYTE:
2593 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2594 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2595 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2596 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2597 break;
2598 default: UNREACHABLE();
2599 }
2600 break;
2601 case GL_BGRA_EXT:
2602 switch (type)
2603 {
2604 case GL_UNSIGNED_BYTE:
2605 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2606 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2607 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2608 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2609 break;
2610 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2611 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2612 // this type is packed as follows:
2613 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2614 // --------------------------------------------------------------------------------
2615 // | 4th | 3rd | 2nd | 1st component |
2616 // --------------------------------------------------------------------------------
2617 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2618 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2619 ((unsigned short)(15 * a + 0.5f) << 12)|
2620 ((unsigned short)(15 * r + 0.5f) << 8) |
2621 ((unsigned short)(15 * g + 0.5f) << 4) |
2622 ((unsigned short)(15 * b + 0.5f) << 0);
2623 break;
2624 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2625 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2626 // this type is packed as follows:
2627 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2628 // --------------------------------------------------------------------------------
2629 // | 4th | 3rd | 2nd | 1st component |
2630 // --------------------------------------------------------------------------------
2631 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2632 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2633 ((unsigned short)( a + 0.5f) << 15) |
2634 ((unsigned short)(31 * r + 0.5f) << 10) |
2635 ((unsigned short)(31 * g + 0.5f) << 5) |
2636 ((unsigned short)(31 * b + 0.5f) << 0);
2637 break;
2638 default: UNREACHABLE();
2639 }
2640 break;
2641 case GL_RGB:
2642 switch (type)
2643 {
2644 case GL_UNSIGNED_SHORT_5_6_5:
2645 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2646 ((unsigned short)(31 * b + 0.5f) << 0) |
2647 ((unsigned short)(63 * g + 0.5f) << 5) |
2648 ((unsigned short)(31 * r + 0.5f) << 11);
2649 break;
2650 case GL_UNSIGNED_BYTE:
2651 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2652 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2653 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2654 break;
2655 default: UNREACHABLE();
2656 }
2657 break;
2658 default: UNREACHABLE();
2659 }
2660 }
2661 }
2662
2663 systemSurface->UnlockRect();
2664
2665 systemSurface->Release();
2666}
2667
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002668RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2669{
2670 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2671 IDirect3DSurface9 *surface = NULL;
2672 if (depth)
2673 {
2674 surface = swapChain9->getDepthStencil();
2675 }
2676 else
2677 {
2678 surface = swapChain9->getRenderTarget();
2679 }
2680
2681 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2682
2683 return renderTarget;
2684}
2685
2686RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2687{
2688 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2689 return renderTarget;
2690}
2691
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002692ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, GLenum type, void *data)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002693{
2694 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002695 D3DConstantTable *table = reinterpret_cast<D3DConstantTable *>(data);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002696
2697 switch (type)
2698 {
2699 case GL_VERTEX_SHADER:
2700 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002701 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002702 if (vshader)
2703 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002704 executable = new ShaderExecutable9(function, length, vshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002705 }
2706 }
2707 break;
2708 case GL_FRAGMENT_SHADER:
2709 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002710 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002711 if (pshader)
2712 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002713 executable = new ShaderExecutable9(function, length, pshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002714 }
2715 }
2716 break;
2717 default:
2718 UNREACHABLE();
2719 break;
2720 }
2721
2722 return executable;
2723}
2724
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002725ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2726{
2727 const char *profile = NULL;
2728
2729 switch (type)
2730 {
2731 case GL_VERTEX_SHADER:
2732 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
2733 break;
2734 case GL_FRAGMENT_SHADER:
2735 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
2736 break;
2737 default:
2738 UNREACHABLE();
2739 return NULL;
2740 }
2741
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002742 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002743 if (!binary)
2744 return NULL;
2745
daniel@transgaming.com31240482012-11-28 21:06:41 +00002746 D3DConstantTable *constantTable = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
daniel@transgaming.combe281b02012-11-28 21:05:48 +00002747 if (constantTable->error())
2748 {
2749 delete constantTable;
2750 binary->Release();
2751 return NULL;
2752 }
2753
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002754 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type, constantTable);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00002755 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002756
2757 return executable;
2758}
2759
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002760bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2761{
2762 return mBlit->boxFilter(source, dest);
2763}
2764
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002765D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002766{
2767 if (mD3d9Ex != NULL)
2768 {
2769 return D3DPOOL_DEFAULT;
2770 }
2771 else
2772 {
2773 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2774 {
2775 return D3DPOOL_MANAGED;
2776 }
2777 }
2778
2779 return D3DPOOL_DEFAULT;
2780}
2781
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002782bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2783{
2784 if (source && dest)
2785 {
2786 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2787 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2788
2789 if (fromManaged)
2790 {
2791 D3DSURFACE_DESC desc;
2792 source->GetDesc(&desc);
2793
2794 IDirect3DSurface9 *surf = 0;
2795 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2796
2797 if (SUCCEEDED(result))
2798 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002799 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002800 result = device->UpdateSurface(surf, NULL, dest, NULL);
2801 surf->Release();
2802 }
2803 }
2804 else
2805 {
2806 endScene();
2807 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2808 }
2809
2810 if (FAILED(result))
2811 {
2812 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2813 return false;
2814 }
2815 }
2816
2817 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002818}
2819
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002820Image *Renderer9::createImage()
2821{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002822 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002823}
2824
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002825void Renderer9::generateMipmap(Image *dest, Image *src)
2826{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002827 Image9 *src9 = Image9::makeImage9(src);
2828 Image9 *dst9 = Image9::makeImage9(dest);
2829 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002830}
2831
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002832}