blob: 141d376054c6b6c6f248f5a541ab25c589d7d198 [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00008
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009#include "common/debug.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000010#include "libGLESv2/main.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/mathutil.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000014#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000015#include "libGLESv2/Program.h"
16#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000017#include "libGLESv2/renderer/IndexDataManager.h"
18#include "libGLESv2/renderer/VertexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000019#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000020#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000021#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000022#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +000023#include "libGLESv2/renderer/TextureStorage.h"
daniel@transgaming.com4ba24062012-12-20 20:54:24 +000024#include "libGLESv2/renderer/Image9.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000025#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000026#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000027
daniel@transgaming.com3281f972012-10-31 18:38:51 +000028#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000029#include "libEGL/Display.h"
30
daniel@transgaming.com621ce052012-10-31 17:52:29 +000031// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
32#define REF_RAST 0
33
34// The "Debug This Pixel..." feature in PIX often fails when using the
35// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
36// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
37#if !defined(ANGLE_ENABLE_D3D9EX)
38// Enables use of the IDirect3D9Ex interface, when available
39#define ANGLE_ENABLE_D3D9EX 1
40#endif // !defined(ANGLE_ENABLE_D3D9EX)
41
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000042namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000043{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000044static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000045 {
46 D3DFMT_A1R5G5B5,
47 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
48 D3DFMT_A8R8G8B8,
49 D3DFMT_R5G6B5,
50 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
51 D3DFMT_X8R8G8B8
52 };
53
daniel@transgaming.com222ee082012-11-28 19:31:49 +000054static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000055 {
56 D3DFMT_UNKNOWN,
57 // D3DFMT_D16_LOCKABLE,
58 D3DFMT_D32,
59 // D3DFMT_D15S1,
60 D3DFMT_D24S8,
61 D3DFMT_D24X8,
62 // D3DFMT_D24X4S4,
63 D3DFMT_D16,
64 // D3DFMT_D32F_LOCKABLE,
65 // D3DFMT_D24FS8
66 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000067
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000068Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000069{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000070 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000071
72 mD3d9 = NULL;
73 mD3d9Ex = NULL;
74 mDevice = NULL;
75 mDeviceEx = NULL;
76 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000077 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000078
79 mAdapter = D3DADAPTER_DEFAULT;
80
81 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
82 mDeviceType = D3DDEVTYPE_REF;
83 #else
84 mDeviceType = D3DDEVTYPE_HAL;
85 #endif
86
87 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000088
89 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000090
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000091 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000092
93 mVertexDataManager = NULL;
94 mIndexDataManager = NULL;
95 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +000096
97 mMaxNullColorbufferLRU = 0;
98 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
99 {
100 mNullColorbufferCache[i].lruCount = 0;
101 mNullColorbufferCache[i].width = 0;
102 mNullColorbufferCache[i].height = 0;
103 mNullColorbufferCache[i].buffer = NULL;
104 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000105}
106
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000107Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000108{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000109 releaseDeviceResources();
110
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000111 if (mDevice)
112 {
113 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000114 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000115 {
116 resetDevice();
117 }
118
119 mDevice->Release();
120 mDevice = NULL;
121 }
122
123 if (mDeviceEx)
124 {
125 mDeviceEx->Release();
126 mDeviceEx = NULL;
127 }
128
129 if (mD3d9)
130 {
131 mD3d9->Release();
132 mD3d9 = NULL;
133 }
134
135 if (mDeviceWindow)
136 {
137 DestroyWindow(mDeviceWindow);
138 mDeviceWindow = NULL;
139 }
140
141 if (mD3d9Ex)
142 {
143 mD3d9Ex->Release();
144 mD3d9Ex = NULL;
145 }
146
147 if (mD3d9Module)
148 {
149 mD3d9Module = NULL;
150 }
151
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000152 while (!mMultiSampleSupport.empty())
153 {
154 delete [] mMultiSampleSupport.begin()->second;
155 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
156 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000157}
158
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000159Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
160{
161 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
162 return static_cast<rx::Renderer9*>(renderer);
163}
164
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000165EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000166{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000167 if (!initializeCompiler())
168 {
169 return EGL_NOT_INITIALIZED;
170 }
171
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000172 if (mSoftwareDevice)
173 {
174 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
175 }
176 else
177 {
178 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
179 }
180
181 if (mD3d9Module == NULL)
182 {
183 ERR("No D3D9 module found - aborting!\n");
184 return EGL_NOT_INITIALIZED;
185 }
186
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000187 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
188 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
189
190 // Use Direct3D9Ex if available. Among other things, this version is less
191 // inclined to report a lost context, for example when the user switches
192 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
193 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
194 {
195 ASSERT(mD3d9Ex);
196 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
197 ASSERT(mD3d9);
198 }
199 else
200 {
201 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
202 }
203
204 if (!mD3d9)
205 {
206 ERR("Could not create D3D9 device - aborting!\n");
207 return EGL_NOT_INITIALIZED;
208 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000209
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000210 if (mDc != NULL)
211 {
212 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
213 }
214
215 HRESULT result;
216
217 // Give up on getting device caps after about one second.
218 for (int i = 0; i < 10; ++i)
219 {
220 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
221 if (SUCCEEDED(result))
222 {
223 break;
224 }
225 else if (result == D3DERR_NOTAVAILABLE)
226 {
227 Sleep(100); // Give the driver some time to initialize/recover
228 }
229 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
230 {
231 ERR("failed to get device caps (0x%x)\n", result);
232 return EGL_NOT_INITIALIZED;
233 }
234 }
235
236 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
237 {
238 ERR("Renderer does not support PS 2.0. aborting!\n");
239 return EGL_NOT_INITIALIZED;
240 }
241
242 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
243 // This is required by Texture2D::convertToRenderTarget.
244 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
245 {
246 ERR("Renderer does not support stretctrect from textures!\n");
247 return EGL_NOT_INITIALIZED;
248 }
249
250 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
251
252 // ATI cards on XP have problems with non-power-of-two textures.
253 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
254 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
255 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
256 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
257
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000258 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
259 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
260
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000261 mMinSwapInterval = 4;
262 mMaxSwapInterval = 0;
263
264 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
265 {
266 mMinSwapInterval = std::min(mMinSwapInterval, 0);
267 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
268 }
269 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
270 {
271 mMinSwapInterval = std::min(mMinSwapInterval, 1);
272 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
273 }
274 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
275 {
276 mMinSwapInterval = std::min(mMinSwapInterval, 2);
277 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
278 }
279 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
280 {
281 mMinSwapInterval = std::min(mMinSwapInterval, 3);
282 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
283 }
284 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
285 {
286 mMinSwapInterval = std::min(mMinSwapInterval, 4);
287 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
288 }
289
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000290 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000291 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000292 {
293 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000294 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
295 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000296
297 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
298 {
299 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
300 {
301 max = j;
302 }
303 }
304 }
305
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000306 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000307 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000308 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000309 continue;
310
311 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000312 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
313 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000314
315 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
316 {
317 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
318 {
319 max = j;
320 }
321 }
322 }
323
324 mMaxSupportedSamples = max;
325
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000326 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
327 static const TCHAR className[] = TEXT("STATIC");
328
329 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
330
331 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
332 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
333
334 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
335 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
336 {
337 return EGL_BAD_ALLOC;
338 }
339
340 if (FAILED(result))
341 {
342 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
343
344 if (FAILED(result))
345 {
346 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
347 return EGL_BAD_ALLOC;
348 }
349 }
350
351 if (mD3d9Ex)
352 {
353 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
354 ASSERT(SUCCEEDED(result));
355 }
356
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000357 mVertexShaderCache.initialize(mDevice);
358 mPixelShaderCache.initialize(mDevice);
359
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000360 initializeDevice();
361
362 return EGL_SUCCESS;
363}
364
365// do any one-time device initialization
366// NOTE: this is also needed after a device lost/reset
367// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000368void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000369{
370 // Permanent non-default states
371 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
372 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
373
374 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
375 {
376 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
377 }
378 else
379 {
380 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
381 }
382
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000383 markAllStateDirty();
384
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000385 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000386
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000387 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000388 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000389 mVertexDataManager = new rx::VertexDataManager(this);
390 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000391}
392
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000393D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000394{
395 D3DPRESENT_PARAMETERS presentParameters = {0};
396
397 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
398 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
399 presentParameters.BackBufferCount = 1;
400 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
401 presentParameters.BackBufferWidth = 1;
402 presentParameters.BackBufferHeight = 1;
403 presentParameters.EnableAutoDepthStencil = FALSE;
404 presentParameters.Flags = 0;
405 presentParameters.hDeviceWindow = mDeviceWindow;
406 presentParameters.MultiSampleQuality = 0;
407 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
408 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
409 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
410 presentParameters.Windowed = TRUE;
411
412 return presentParameters;
413}
414
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000415int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000416{
417 D3DDISPLAYMODE currentDisplayMode;
418 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
419
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000420 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
421 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000422 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
423 int numConfigs = 0;
424
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000425 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000426 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000427 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000428
429 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
430
431 if (SUCCEEDED(result))
432 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000434 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000435 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000436 HRESULT result = D3D_OK;
437
438 if(depthStencilFormat != D3DFMT_UNKNOWN)
439 {
440 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
441 }
442
443 if (SUCCEEDED(result))
444 {
445 if(depthStencilFormat != D3DFMT_UNKNOWN)
446 {
447 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
448 }
449
450 if (SUCCEEDED(result))
451 {
452 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000453 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
454 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000455 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
456 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
457
458 (*configDescList)[numConfigs++] = newConfig;
459 }
460 }
461 }
462 }
463 }
464
465 return numConfigs;
466}
467
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000468void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000469{
470 delete [] (configDescList);
471}
472
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000473void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000474{
475 if (!mSceneStarted)
476 {
477 long result = mDevice->BeginScene();
478 if (SUCCEEDED(result)) {
479 // This is defensive checking against the device being
480 // lost at unexpected times.
481 mSceneStarted = true;
482 }
483 }
484}
485
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000486void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000487{
488 if (mSceneStarted)
489 {
490 // EndScene can fail if the device was lost, for example due
491 // to a TDR during a draw call.
492 mDevice->EndScene();
493 mSceneStarted = false;
494 }
495}
496
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000497// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000498void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000499{
500 HRESULT result;
501
502 IDirect3DQuery9* query = allocateEventQuery();
503 if (!query)
504 {
505 return;
506 }
507
508 result = query->Issue(D3DISSUE_END);
509 ASSERT(SUCCEEDED(result));
510
511 do
512 {
513 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
514
515 if(block && result == S_FALSE)
516 {
517 // Keep polling, but allow other threads to do something useful first
518 Sleep(0);
519 // explicitly check for device loss
520 // some drivers seem to return S_FALSE even if the device is lost
521 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000522 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000523 {
524 result = D3DERR_DEVICELOST;
525 }
526 }
527 }
528 while(block && result == S_FALSE);
529
530 freeEventQuery(query);
531
532 if (isDeviceLostError(result))
533 {
534 mDisplay->notifyDeviceLost();
535 }
536}
537
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000538SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
539{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000540 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000541}
542
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000543// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000544IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000545{
546 IDirect3DQuery9 *query = NULL;
547
548 if (mEventQueryPool.empty())
549 {
550 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
551 ASSERT(SUCCEEDED(result));
552 }
553 else
554 {
555 query = mEventQueryPool.back();
556 mEventQueryPool.pop_back();
557 }
558
559 return query;
560}
561
562// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000563void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000564{
565 if (mEventQueryPool.size() > 1000)
566 {
567 query->Release();
568 }
569 else
570 {
571 mEventQueryPool.push_back(query);
572 }
573}
574
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000575IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000576{
577 return mVertexShaderCache.create(function, length);
578}
579
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000580IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000581{
582 return mPixelShaderCache.create(function, length);
583}
584
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000585HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
586{
587 D3DPOOL Pool = getBufferPool(Usage);
588 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
589}
590
591HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
592{
593 D3DPOOL Pool = getBufferPool(Usage);
594 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
595}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000596
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000597void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000598{
599 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
600 int d3dSampler = index + d3dSamplerOffset;
601
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000602 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
603 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000604
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000605 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000606 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000607 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000608 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
609 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
610 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
611 if (mSupportsTextureFilterAnisotropy)
612 {
613 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
614 }
615}
616
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000617void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000618{
619 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
620 int d3dSampler = index + d3dSamplerOffset;
621 IDirect3DBaseTexture9 *d3dTexture = NULL;
622
623 if (texture)
624 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000625 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000626 if (texStorage)
627 {
628 d3dTexture = texStorage->getBaseTexture();
629 }
630 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000631 // in the texture class and we're unexpectedly missing the d3d texture
632 ASSERT(d3dTexture != NULL);
633 }
634
635 mDevice->SetTexture(d3dSampler, d3dTexture);
636}
637
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000638void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000639{
640 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000641
642 if (rasterStateChanged)
643 {
644 // Set the cull mode
645 if (rasterState.cullFace)
646 {
647 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
648 }
649 else
650 {
651 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
652 }
653
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000654 if (rasterState.polygonOffsetFill)
655 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000656 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000657 {
658 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
659
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000660 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000661 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
662 }
663 }
664 else
665 {
666 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
667 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
668 }
669
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000670 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000671 }
672
673 mForceSetRasterState = false;
674}
675
676void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
677{
678 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
679 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
680 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
681
682 if (blendStateChanged || blendColorChanged)
683 {
684 if (blendState.blend)
685 {
686 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
687
688 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
689 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
690 {
691 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
692 }
693 else
694 {
695 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
696 gl::unorm<8>(blendColor.alpha),
697 gl::unorm<8>(blendColor.alpha),
698 gl::unorm<8>(blendColor.alpha)));
699 }
700
701 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
702 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
703 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
704
705 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
706 blendState.destBlendRGB != blendState.destBlendAlpha ||
707 blendState.blendEquationRGB != blendState.blendEquationAlpha)
708 {
709 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
710
711 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
712 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
713 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
714 }
715 else
716 {
717 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
718 }
719 }
720 else
721 {
722 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
723 }
724
725 if (blendState.sampleAlphaToCoverage)
726 {
727 FIXME("Sample alpha to coverage is unimplemented.");
728 }
729
730 // Set the color mask
731 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
732 // Apparently some ATI cards have a bug where a draw with a zero color
733 // write mask can cause later draws to have incorrect results. Instead,
734 // set a nonzero color write mask but modify the blend state so that no
735 // drawing is done.
736 // http://code.google.com/p/angleproject/issues/detail?id=169
737
738 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
739 blendState.colorMaskBlue, blendState.colorMaskAlpha);
740 if (colorMask == 0 && !zeroColorMaskAllowed)
741 {
742 // Enable green channel, but set blending so nothing will be drawn.
743 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
744 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
745
746 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
747 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
748 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
749 }
750 else
751 {
752 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
753 }
754
755 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
756
757 mCurBlendState = blendState;
758 mCurBlendColor = blendColor;
759 }
760
761 if (sampleMaskChanged)
762 {
763 // Set the multisample mask
764 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
765 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
766
767 mCurSampleMask = sampleMask;
768 }
769
770 mForceSetBlendState = false;
771}
772
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000773void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000774 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000775{
776 bool depthStencilStateChanged = mForceSetDepthStencilState ||
777 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000778 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
779 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000780 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000781
782 if (depthStencilStateChanged)
783 {
784 if (depthStencilState.depthTest)
785 {
786 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
787 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
788 }
789 else
790 {
791 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
792 }
793
794 mCurDepthStencilState = depthStencilState;
795 }
796
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000797 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000798 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000799 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000800 {
801 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
802 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
803
804 // FIXME: Unsupported by D3D9
805 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
806 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
807 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
808 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000809 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000810 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
811 {
812 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
813 return error(GL_INVALID_OPERATION);
814 }
815
816 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000817 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000818
819 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
820 depthStencilState.stencilWritemask);
821 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
822 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
823
824 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000825 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000826 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
827 depthStencilState.stencilMask);
828
829 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
830 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
831 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
832 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
833 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
834 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
835
836 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
837 depthStencilState.stencilBackWritemask);
838 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
839 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
840
841 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000842 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000843 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
844 depthStencilState.stencilBackMask);
845
846 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
847 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
848 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
849 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
850 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
851 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
852 }
853 else
854 {
855 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
856 }
857
858 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
859
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000860 mCurStencilRef = stencilRef;
861 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000862 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000863 }
864
865 mForceSetDepthStencilState = false;
866}
867
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000868void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000869{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000870 bool scissorChanged = mForceSetScissor ||
871 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
872 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000873
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000874 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000875 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000876 if (enabled)
877 {
878 RECT rect;
879 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
880 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
881 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
882 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
883 mDevice->SetScissorRect(&rect);
884 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000885
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000886 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
887
888 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000889 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000890 }
891
892 mForceSetScissor = false;
893}
894
daniel@transgaming.com12985182012-12-20 20:56:31 +0000895bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
896 bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000897{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000898 gl::Rectangle actualViewport = viewport;
899 float actualZNear = gl::clamp01(zNear);
900 float actualZFar = gl::clamp01(zFar);
901 if (ignoreViewport)
902 {
903 actualViewport.x = 0;
904 actualViewport.y = 0;
905 actualViewport.width = mRenderTargetDesc.width;
906 actualViewport.height = mRenderTargetDesc.height;
907 actualZNear = 0.0f;
908 actualZFar = 1.0f;
909 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000910
911 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000912 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
913 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
914 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
915 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
916 dxViewport.MinZ = actualZNear;
917 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000918
919 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
920 {
921 return false; // Nothing to render
922 }
923
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000924 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
925 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000926 if (viewportChanged)
927 {
928 mDevice->SetViewport(&dxViewport);
929
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000930 mCurViewport = actualViewport;
931 mCurNear = actualZNear;
932 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000933 }
934
935 if (currentProgram && (viewportChanged || forceSetUniforms))
936 {
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
daniel@transgaming.com12985182012-12-20 20:56:31 +0000949 GLint depthFront = currentProgram->getDxDepthFrontLocation();
950 GLfloat ccw = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
951 GLfloat dz[3] = { (actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f, ccw };
952 currentProgram->setUniform3fv(depthFront, 1, dz);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000953
954 GLint depthRange = currentProgram->getDxDepthRangeLocation();
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000955 GLfloat nearFarDiff[3] = { actualZNear, actualZFar, actualZFar - actualZNear };
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000956 currentProgram->setUniform3fv(depthRange, 1, nearFarDiff);
957 }
958
959 mForceSetViewport = false;
960 return true;
961}
962
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000963bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
964{
965 switch (mode)
966 {
967 case GL_POINTS:
968 mPrimitiveType = D3DPT_POINTLIST;
969 mPrimitiveCount = count;
970 break;
971 case GL_LINES:
972 mPrimitiveType = D3DPT_LINELIST;
973 mPrimitiveCount = count / 2;
974 break;
975 case GL_LINE_LOOP:
976 mPrimitiveType = D3DPT_LINESTRIP;
977 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
978 break;
979 case GL_LINE_STRIP:
980 mPrimitiveType = D3DPT_LINESTRIP;
981 mPrimitiveCount = count - 1;
982 break;
983 case GL_TRIANGLES:
984 mPrimitiveType = D3DPT_TRIANGLELIST;
985 mPrimitiveCount = count / 3;
986 break;
987 case GL_TRIANGLE_STRIP:
988 mPrimitiveType = D3DPT_TRIANGLESTRIP;
989 mPrimitiveCount = count - 2;
990 break;
991 case GL_TRIANGLE_FAN:
992 mPrimitiveType = D3DPT_TRIANGLEFAN;
993 mPrimitiveCount = count - 2;
994 break;
995 default:
996 return error(GL_INVALID_ENUM, false);
997 }
998
999 return mPrimitiveCount > 0;
1000}
1001
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001002
1003gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1004{
1005 if (!depthbuffer)
1006 {
1007 ERR("Unexpected null depthbuffer for depth-only FBO.");
1008 return NULL;
1009 }
1010
1011 GLsizei width = depthbuffer->getWidth();
1012 GLsizei height = depthbuffer->getHeight();
1013
1014 // search cached nullcolorbuffers
1015 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1016 {
1017 if (mNullColorbufferCache[i].buffer != NULL &&
1018 mNullColorbufferCache[i].width == width &&
1019 mNullColorbufferCache[i].height == height)
1020 {
1021 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1022 return mNullColorbufferCache[i].buffer;
1023 }
1024 }
1025
1026 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1027
1028 // add nullbuffer to the cache
1029 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1030 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1031 {
1032 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1033 {
1034 oldest = &mNullColorbufferCache[i];
1035 }
1036 }
1037
1038 delete oldest->buffer;
1039 oldest->buffer = nullbuffer;
1040 oldest->lruCount = ++mMaxNullColorbufferLRU;
1041 oldest->width = width;
1042 oldest->height = height;
1043
1044 return nullbuffer;
1045}
1046
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001047bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001048{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001049 // if there is no color attachment we must synthesize a NULL colorattachment
1050 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1051 gl::Renderbuffer *renderbufferObject = NULL;
1052 if (framebuffer->getColorbufferType() != GL_NONE)
1053 {
1054 renderbufferObject = framebuffer->getColorbuffer();
1055 }
1056 else
1057 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001058 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001059 }
1060 if (!renderbufferObject)
1061 {
1062 ERR("unable to locate renderbuffer for FBO.");
1063 return false;
1064 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001065
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001066 bool renderTargetChanged = false;
1067 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1068 if (renderTargetSerial != mAppliedRenderTargetSerial)
1069 {
1070 // Apply the render target on the device
1071 IDirect3DSurface9 *renderTargetSurface = NULL;
1072
1073 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1074 if (renderTarget)
1075 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001076 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001077 }
1078
1079 if (!renderTargetSurface)
1080 {
1081 ERR("render target pointer unexpectedly null.");
1082 return false; // Context must be lost
1083 }
1084
1085 mDevice->SetRenderTarget(0, renderTargetSurface);
1086 renderTargetSurface->Release();
1087
1088 mAppliedRenderTargetSerial = renderTargetSerial;
1089 renderTargetChanged = true;
1090 }
1091
1092 gl::Renderbuffer *depthStencil = NULL;
1093 unsigned int depthbufferSerial = 0;
1094 unsigned int stencilbufferSerial = 0;
1095 if (framebuffer->getDepthbufferType() != GL_NONE)
1096 {
1097 depthStencil = framebuffer->getDepthbuffer();
1098 if (!depthStencil)
1099 {
1100 ERR("Depth stencil pointer unexpectedly null.");
1101 return false;
1102 }
1103
1104 depthbufferSerial = depthStencil->getSerial();
1105 }
1106 else if (framebuffer->getStencilbufferType() != GL_NONE)
1107 {
1108 depthStencil = framebuffer->getStencilbuffer();
1109 if (!depthStencil)
1110 {
1111 ERR("Depth stencil pointer unexpectedly null.");
1112 return false;
1113 }
1114
1115 stencilbufferSerial = depthStencil->getSerial();
1116 }
1117
1118 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1119 stencilbufferSerial != mAppliedStencilbufferSerial ||
1120 !mDepthStencilInitialized)
1121 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001122 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001123 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001124
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001125 // Apply the depth stencil on the device
1126 if (depthStencil)
1127 {
1128 IDirect3DSurface9 *depthStencilSurface = NULL;
1129 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1130
1131 if (depthStencilRenderTarget)
1132 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001133 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001134 }
1135
1136 if (!depthStencilSurface)
1137 {
1138 ERR("depth stencil pointer unexpectedly null.");
1139 return false; // Context must be lost
1140 }
1141
1142 mDevice->SetDepthStencilSurface(depthStencilSurface);
1143 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001144
1145 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001146 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001147 }
1148 else
1149 {
1150 mDevice->SetDepthStencilSurface(NULL);
1151 }
1152
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001153 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1154 {
1155 mCurDepthSize = depthSize;
1156 mForceSetRasterState = true;
1157 }
1158
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001159 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1160 {
1161 mCurStencilSize = stencilSize;
1162 mForceSetDepthStencilState = true;
1163 }
1164
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001165 mAppliedDepthbufferSerial = depthbufferSerial;
1166 mAppliedStencilbufferSerial = stencilbufferSerial;
1167 mDepthStencilInitialized = true;
1168 }
1169
1170 if (renderTargetChanged || !mRenderTargetDescInitialized)
1171 {
1172 mForceSetScissor = true;
1173 mForceSetViewport = true;
1174
1175 mRenderTargetDesc.width = renderbufferObject->getWidth();
1176 mRenderTargetDesc.height = renderbufferObject->getHeight();
1177 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1178 mRenderTargetDescInitialized = true;
1179 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001180
1181 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001182}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001183
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001184GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001185{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001186 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001187 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1188 if (err != GL_NO_ERROR)
1189 {
1190 return err;
1191 }
1192
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001193 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1194}
1195
1196// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001197GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001198{
1199 IDirect3DIndexBuffer9 *indexBuffer;
1200 unsigned int serial;
1201 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial);
1202
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001203 if (err == GL_NO_ERROR)
1204 {
1205 if (serial != mAppliedIBSerial)
1206 {
1207 mDevice->SetIndices(indexBuffer);
1208 mAppliedIBSerial = serial;
1209 }
1210 }
1211
1212 return err;
1213}
1214
1215void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1216{
1217 startScene();
1218
1219 if (mode == GL_LINE_LOOP)
1220 {
1221 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1222 }
1223 else if (instances > 0)
1224 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001225 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001226 if (countingIB)
1227 {
1228 if (mAppliedIBSerial != countingIB->getSerial())
1229 {
1230 mDevice->SetIndices(countingIB->getBuffer());
1231 mAppliedIBSerial = countingIB->getSerial();
1232 }
1233
1234 for (int i = 0; i < mRepeatDraw; i++)
1235 {
1236 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1237 }
1238 }
1239 else
1240 {
1241 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1242 return error(GL_OUT_OF_MEMORY);
1243 }
1244 }
1245 else // Regular case
1246 {
1247 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1248 }
1249}
1250
daniel@transgaming.com31240482012-11-28 21:06:41 +00001251void 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 +00001252{
1253 startScene();
1254
1255 if (mode == GL_LINE_LOOP)
1256 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001257 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001258 }
1259 else
1260 {
1261 for (int i = 0; i < mRepeatDraw; i++)
1262 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001263 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1264 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001265 }
1266 }
1267}
1268
1269void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1270{
1271 // Get the raw indices for an indexed draw
1272 if (type != GL_NONE && elementArrayBuffer)
1273 {
1274 gl::Buffer *indexBuffer = elementArrayBuffer;
1275 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1276 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1277 }
1278
1279 UINT startIndex = 0;
1280 bool succeeded = false;
1281
1282 if (get32BitIndexSupport())
1283 {
1284 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1285
1286 if (!mLineLoopIB)
1287 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001288 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001289 }
1290
1291 if (mLineLoopIB)
1292 {
1293 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
1294
1295 UINT offset = 0;
1296 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
1297 startIndex = offset / 4;
1298
1299 if (data)
1300 {
1301 switch (type)
1302 {
1303 case GL_NONE: // Non-indexed draw
1304 for (int i = 0; i < count; i++)
1305 {
1306 data[i] = i;
1307 }
1308 data[count] = 0;
1309 break;
1310 case GL_UNSIGNED_BYTE:
1311 for (int i = 0; i < count; i++)
1312 {
1313 data[i] = static_cast<const GLubyte*>(indices)[i];
1314 }
1315 data[count] = static_cast<const GLubyte*>(indices)[0];
1316 break;
1317 case GL_UNSIGNED_SHORT:
1318 for (int i = 0; i < count; i++)
1319 {
1320 data[i] = static_cast<const GLushort*>(indices)[i];
1321 }
1322 data[count] = static_cast<const GLushort*>(indices)[0];
1323 break;
1324 case GL_UNSIGNED_INT:
1325 for (int i = 0; i < count; i++)
1326 {
1327 data[i] = static_cast<const GLuint*>(indices)[i];
1328 }
1329 data[count] = static_cast<const GLuint*>(indices)[0];
1330 break;
1331 default: UNREACHABLE();
1332 }
1333
1334 mLineLoopIB->unmap();
1335 succeeded = true;
1336 }
1337 }
1338 }
1339 else
1340 {
1341 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
1342
1343 if (!mLineLoopIB)
1344 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001345 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001346 }
1347
1348 if (mLineLoopIB)
1349 {
1350 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
1351
1352 UINT offset = 0;
1353 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
1354 startIndex = offset / 2;
1355
1356 if (data)
1357 {
1358 switch (type)
1359 {
1360 case GL_NONE: // Non-indexed draw
1361 for (int i = 0; i < count; i++)
1362 {
1363 data[i] = i;
1364 }
1365 data[count] = 0;
1366 break;
1367 case GL_UNSIGNED_BYTE:
1368 for (int i = 0; i < count; i++)
1369 {
1370 data[i] = static_cast<const GLubyte*>(indices)[i];
1371 }
1372 data[count] = static_cast<const GLubyte*>(indices)[0];
1373 break;
1374 case GL_UNSIGNED_SHORT:
1375 for (int i = 0; i < count; i++)
1376 {
1377 data[i] = static_cast<const GLushort*>(indices)[i];
1378 }
1379 data[count] = static_cast<const GLushort*>(indices)[0];
1380 break;
1381 case GL_UNSIGNED_INT:
1382 for (int i = 0; i < count; i++)
1383 {
1384 data[i] = static_cast<const GLuint*>(indices)[i];
1385 }
1386 data[count] = static_cast<const GLuint*>(indices)[0];
1387 break;
1388 default: UNREACHABLE();
1389 }
1390
1391 mLineLoopIB->unmap();
1392 succeeded = true;
1393 }
1394 }
1395 }
1396
1397 if (succeeded)
1398 {
1399 if (mAppliedIBSerial != mLineLoopIB->getSerial())
1400 {
1401 mDevice->SetIndices(mLineLoopIB->getBuffer());
1402 mAppliedIBSerial = mLineLoopIB->getSerial();
1403 }
1404
1405 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1406 }
1407 else
1408 {
1409 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
1410 return error(GL_OUT_OF_MEMORY);
1411 }
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001412}
1413
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001414void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1415{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001416 unsigned int programBinarySerial = programBinary->getSerial();
1417 if (programBinarySerial != mAppliedProgramBinarySerial)
1418 {
1419 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1420 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001421
daniel@transgaming.come4991412012-12-20 20:55:34 +00001422 IDirect3DVertexShader9 *vertexShader = NULL;
1423 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001424
daniel@transgaming.come4991412012-12-20 20:55:34 +00001425 IDirect3DPixelShader9 *pixelShader = NULL;
1426 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001427
daniel@transgaming.come4991412012-12-20 20:55:34 +00001428 mDevice->SetPixelShader(pixelShader);
1429 mDevice->SetVertexShader(vertexShader);
1430 programBinary->dirtyAllUniforms();
1431
1432 mAppliedProgramBinarySerial = programBinarySerial;
1433 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001434}
1435
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001436void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001437{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001438 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1439 gl::unorm<8>(clearParams.colorClearValue.red),
1440 gl::unorm<8>(clearParams.colorClearValue.green),
1441 gl::unorm<8>(clearParams.colorClearValue.blue));
1442 float depth = gl::clamp01(clearParams.depthClearValue);
1443 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001444
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001445 unsigned int stencilUnmasked = 0x0;
1446 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1447 {
1448 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1449 stencilUnmasked = (0x1 << stencilSize) - 1;
1450 }
1451
1452 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1453
1454 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1455 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1456 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1457 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1458 clearParams.colorMaskBlue && alphaUnmasked);
1459
1460 if (needMaskedColorClear || needMaskedStencilClear)
1461 {
1462 // State which is altered in all paths from this point to the clear call is saved.
1463 // State which is altered in only some paths will be flagged dirty in the case that
1464 // that path is taken.
1465 HRESULT hr;
1466 if (mMaskedClearSavedState == NULL)
1467 {
1468 hr = mDevice->BeginStateBlock();
1469 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1470
1471 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1472 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1473 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1474 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1475 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1476 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1477 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1478 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1479 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1480 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1481 mDevice->SetPixelShader(NULL);
1482 mDevice->SetVertexShader(NULL);
1483 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1484 mDevice->SetStreamSource(0, NULL, 0, 0);
1485 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1486 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1487 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1488 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1489 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1490 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1491 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1492
1493 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1494 {
1495 mDevice->SetStreamSourceFreq(i, 1);
1496 }
1497
1498 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1499 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1500 }
1501
1502 ASSERT(mMaskedClearSavedState != NULL);
1503
1504 if (mMaskedClearSavedState != NULL)
1505 {
1506 hr = mMaskedClearSavedState->Capture();
1507 ASSERT(SUCCEEDED(hr));
1508 }
1509
1510 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1511 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1512 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1513 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1514 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1515 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1516 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1517 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1518
1519 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1520 {
1521 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1522 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1523 clearParams.colorMaskGreen,
1524 clearParams.colorMaskBlue,
1525 clearParams.colorMaskAlpha));
1526 }
1527 else
1528 {
1529 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1530 }
1531
1532 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1533 {
1534 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1535 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1536 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1537 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1538 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1539 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1540 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1541 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1542 }
1543 else
1544 {
1545 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1546 }
1547
1548 mDevice->SetPixelShader(NULL);
1549 mDevice->SetVertexShader(NULL);
1550 mDevice->SetFVF(D3DFVF_XYZRHW);
1551 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1552 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1553 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1554 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1555 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1556 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1557 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1558
1559 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1560 {
1561 mDevice->SetStreamSourceFreq(i, 1);
1562 }
1563
1564 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1565 quad[0][0] = -0.5f;
1566 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1567 quad[0][2] = 0.0f;
1568 quad[0][3] = 1.0f;
1569
1570 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1571 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1572 quad[1][2] = 0.0f;
1573 quad[1][3] = 1.0f;
1574
1575 quad[2][0] = -0.5f;
1576 quad[2][1] = -0.5f;
1577 quad[2][2] = 0.0f;
1578 quad[2][3] = 1.0f;
1579
1580 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1581 quad[3][1] = -0.5f;
1582 quad[3][2] = 0.0f;
1583 quad[3][3] = 1.0f;
1584
1585 startScene();
1586 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1587
1588 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1589 {
1590 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1591 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1592 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1593 }
1594
1595 if (mMaskedClearSavedState != NULL)
1596 {
1597 mMaskedClearSavedState->Apply();
1598 }
1599 }
1600 else if (clearParams.mask)
1601 {
1602 DWORD dxClearFlags = 0;
1603 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1604 {
1605 dxClearFlags |= D3DCLEAR_TARGET;
1606 }
1607 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1608 {
1609 dxClearFlags |= D3DCLEAR_ZBUFFER;
1610 }
1611 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1612 {
1613 dxClearFlags |= D3DCLEAR_STENCIL;
1614 }
1615
1616 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1617 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001618}
1619
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001620void Renderer9::markAllStateDirty()
1621{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001622 mAppliedRenderTargetSerial = 0;
1623 mAppliedDepthbufferSerial = 0;
1624 mAppliedStencilbufferSerial = 0;
1625 mDepthStencilInitialized = false;
1626 mRenderTargetDescInitialized = false;
1627
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001628 mForceSetDepthStencilState = true;
1629 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001630 mForceSetScissor = true;
1631 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001632 mForceSetBlendState = true;
1633
1634 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001635 mAppliedProgramBinarySerial = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001636
1637 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001638}
1639
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001640void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001641{
1642 while (!mEventQueryPool.empty())
1643 {
1644 mEventQueryPool.back()->Release();
1645 mEventQueryPool.pop_back();
1646 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001647
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001648 if (mMaskedClearSavedState)
1649 {
1650 mMaskedClearSavedState->Release();
1651 mMaskedClearSavedState = NULL;
1652 }
1653
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001654 mVertexShaderCache.clear();
1655 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001656
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001657 delete mBlit;
1658 mBlit = NULL;
1659
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001660 delete mVertexDataManager;
1661 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001662
1663 delete mIndexDataManager;
1664 mIndexDataManager = NULL;
1665
1666 delete mLineLoopIB;
1667 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001668
1669 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1670 {
1671 delete mNullColorbufferCache[i].buffer;
1672 mNullColorbufferCache[i].buffer = NULL;
1673 }
1674
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001675}
1676
1677
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001678void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001679{
1680 mDeviceLost = true;
1681}
1682
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001683bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001684{
1685 return mDeviceLost;
1686}
1687
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001688// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001689bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001690{
1691 bool isLost = false;
1692
1693 if (mDeviceEx)
1694 {
1695 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1696 }
1697 else if (mDevice)
1698 {
1699 isLost = FAILED(mDevice->TestCooperativeLevel());
1700 }
1701 else
1702 {
1703 // No device yet, so no reset required
1704 }
1705
1706 if (isLost)
1707 {
1708 // ensure we note the device loss --
1709 // we'll probably get this done again by markDeviceLost
1710 // but best to remember it!
1711 // Note that we don't want to clear the device loss status here
1712 // -- this needs to be done by resetDevice
1713 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001714 if (notify)
1715 {
1716 mDisplay->notifyDeviceLost();
1717 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001718 }
1719
1720 return isLost;
1721}
1722
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001723bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001724{
1725 HRESULT status = D3D_OK;
1726
1727 if (mDeviceEx)
1728 {
1729 status = mDeviceEx->CheckDeviceState(NULL);
1730 }
1731 else if (mDevice)
1732 {
1733 status = mDevice->TestCooperativeLevel();
1734 }
1735
1736 switch (status)
1737 {
1738 case D3DERR_DEVICENOTRESET:
1739 case D3DERR_DEVICEHUNG:
1740 return true;
1741 default:
1742 return false;
1743 }
1744}
1745
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001746bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001747{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001748 releaseDeviceResources();
1749
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001750 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1751
1752 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001753 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001754 int attempts = 3;
1755
1756 while (lost && attempts > 0)
1757 {
1758 if (mDeviceEx)
1759 {
1760 Sleep(500); // Give the graphics driver some CPU time
1761 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1762 }
1763 else
1764 {
1765 result = mDevice->TestCooperativeLevel();
1766 while (result == D3DERR_DEVICELOST)
1767 {
1768 Sleep(100); // Give the graphics driver some CPU time
1769 result = mDevice->TestCooperativeLevel();
1770 }
1771
1772 if (result == D3DERR_DEVICENOTRESET)
1773 {
1774 result = mDevice->Reset(&presentParameters);
1775 }
1776 }
1777
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001778 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001779 attempts --;
1780 }
1781
1782 if (FAILED(result))
1783 {
1784 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1785 return false;
1786 }
1787
1788 // reset device defaults
1789 initializeDevice();
1790 mDeviceLost = false;
1791
1792 return true;
1793}
1794
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001795DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001796{
1797 return mAdapterIdentifier.VendorId;
1798}
1799
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001800const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001801{
1802 return mAdapterIdentifier.Description;
1803}
1804
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001805GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001806{
1807 return mAdapterIdentifier.DeviceIdentifier;
1808}
1809
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001810void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001811{
1812 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1813 {
1814 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1815 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1816
1817 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1818 }
1819}
1820
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001821bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001822{
1823 D3DDISPLAYMODE currentDisplayMode;
1824 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1825
1826 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1827}
1828
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001829bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001830{
1831 D3DDISPLAYMODE currentDisplayMode;
1832 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1833
1834 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1835}
1836
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001837bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001838{
1839 D3DDISPLAYMODE currentDisplayMode;
1840 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1841
1842 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1843}
1844
1845// we use INTZ for depth textures in Direct3D9
1846// we also want NULL texture support to ensure the we can make depth-only FBOs
1847// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001848bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001849{
1850 D3DDISPLAYMODE currentDisplayMode;
1851 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1852
1853 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1854 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1855 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1856 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1857
1858 return intz && null;
1859}
1860
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001861bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001862{
1863 D3DDISPLAYMODE currentDisplayMode;
1864 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1865
1866 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1867 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1868 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1869 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1870
1871 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1872 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1873 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1874 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1875
1876 if (!*filtering && !*renderable)
1877 {
1878 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1879 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1880 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1881 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1882 }
1883 else
1884 {
1885 return true;
1886 }
1887}
1888
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001889bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001890{
1891 D3DDISPLAYMODE currentDisplayMode;
1892 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1893
1894 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1895 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1896 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1897 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1898
1899 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1900 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1901 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1902 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1903
1904 if (!*filtering && !*renderable)
1905 {
1906 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1907 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1908 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1909 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1910 }
1911 else
1912 {
1913 return true;
1914 }
1915}
1916
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001917bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001918{
1919 D3DDISPLAYMODE currentDisplayMode;
1920 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1921
1922 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1923}
1924
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001925bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001926{
1927 D3DDISPLAYMODE currentDisplayMode;
1928 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1929
1930 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1931}
1932
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001933bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001934{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001935 return mSupportsTextureFilterAnisotropy;
1936}
1937
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001938float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001939{
1940 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001941 {
1942 return mDeviceCaps.MaxAnisotropy;
1943 }
1944 return 1.0f;
1945}
1946
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001947bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001948{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001949 IDirect3DQuery9 *query = allocateEventQuery();
1950 if (query)
1951 {
1952 freeEventQuery(query);
1953 return true;
1954 }
1955 else
1956 {
1957 return false;
1958 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001959 return true;
1960}
1961
1962// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1963// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001964bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001965{
1966 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1967 {
1968 return false;
1969 }
1970
1971 D3DDISPLAYMODE currentDisplayMode;
1972 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1973
1974 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1975
1976 return SUCCEEDED(result);
1977}
1978
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001979bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001980{
1981 return mSupportsNonPower2Textures;
1982}
1983
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001984bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001985{
1986 if (!mDevice)
1987 {
1988 return false;
1989 }
1990
1991 IDirect3DQuery9 *query = NULL;
1992 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1993 if (SUCCEEDED(result) && query)
1994 {
1995 query->Release();
1996 return true;
1997 }
1998 else
1999 {
2000 return false;
2001 }
2002}
2003
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002004bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002005{
2006 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2007}
2008
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002009bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002010{
2011 // PIX doesn't seem to support using share handles, so disable them.
2012 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002013 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002014}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002015
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002016int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002017{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002018 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002019}
2020
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002021float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002022{
2023 return mDeviceCaps.MaxPointSize;
2024}
2025
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002026int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002027{
2028 return (int)mDeviceCaps.MaxTextureWidth;
2029}
2030
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002031int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002032{
2033 return (int)mDeviceCaps.MaxTextureHeight;
2034}
2035
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002036bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002037{
2038 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2039}
2040
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002041DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002042{
2043 return mDeviceCaps.DeclTypes;
2044}
2045
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002046int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002047{
2048 return mMinSwapInterval;
2049}
2050
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002051int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002052{
2053 return mMaxSwapInterval;
2054}
2055
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002056int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002057{
2058 return mMaxSupportedSamples;
2059}
2060
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002061int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002062{
2063 if (requested == 0)
2064 {
2065 return requested;
2066 }
2067
2068 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2069 if (itr == mMultiSampleSupport.end())
2070 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002071 if (format == D3DFMT_UNKNOWN)
2072 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002073 return -1;
2074 }
2075
2076 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2077 {
2078 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2079 {
2080 return i;
2081 }
2082 }
2083
2084 return -1;
2085}
2086
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002087D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2088{
2089 switch (internalformat)
2090 {
2091 case GL_DEPTH_COMPONENT16:
2092 case GL_DEPTH_COMPONENT32_OES:
2093 case GL_DEPTH24_STENCIL8_OES:
2094 return D3DFMT_INTZ;
2095 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2096 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2097 return D3DFMT_DXT1;
2098 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2099 return D3DFMT_DXT3;
2100 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2101 return D3DFMT_DXT5;
2102 case GL_RGBA32F_EXT:
2103 case GL_RGB32F_EXT:
2104 case GL_ALPHA32F_EXT:
2105 case GL_LUMINANCE32F_EXT:
2106 case GL_LUMINANCE_ALPHA32F_EXT:
2107 return D3DFMT_A32B32G32R32F;
2108 case GL_RGBA16F_EXT:
2109 case GL_RGB16F_EXT:
2110 case GL_ALPHA16F_EXT:
2111 case GL_LUMINANCE16F_EXT:
2112 case GL_LUMINANCE_ALPHA16F_EXT:
2113 return D3DFMT_A16B16G16R16F;
2114 case GL_LUMINANCE8_EXT:
2115 if (getLuminanceTextureSupport())
2116 {
2117 return D3DFMT_L8;
2118 }
2119 break;
2120 case GL_LUMINANCE8_ALPHA8_EXT:
2121 if (getLuminanceAlphaTextureSupport())
2122 {
2123 return D3DFMT_A8L8;
2124 }
2125 break;
2126 case GL_RGB8_OES:
2127 case GL_RGB565:
2128 return D3DFMT_X8R8G8B8;
2129 }
2130
2131 return D3DFMT_A8R8G8B8;
2132}
2133
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002134bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002135{
2136 bool result = false;
2137
2138 if (source && dest)
2139 {
2140 int levels = source->levelCount();
2141 for (int i = 0; i < levels; ++i)
2142 {
2143 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
2144 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
2145
2146 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2147
2148 if (srcSurf) srcSurf->Release();
2149 if (dstSurf) dstSurf->Release();
2150
2151 if (!result)
2152 return false;
2153 }
2154 }
2155
2156 return result;
2157}
2158
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002159bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002160{
2161 bool result = false;
2162
2163 if (source && dest)
2164 {
2165 int levels = source->levelCount();
2166 for (int f = 0; f < 6; f++)
2167 {
2168 for (int i = 0; i < levels; i++)
2169 {
2170 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2171 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
2172
2173 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2174
2175 if (srcSurf) srcSurf->Release();
2176 if (dstSurf) dstSurf->Release();
2177
2178 if (!result)
2179 return false;
2180 }
2181 }
2182 }
2183
2184 return result;
2185}
2186
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002187D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002188{
2189 if (mD3d9Ex != NULL)
2190 {
2191 return D3DPOOL_DEFAULT;
2192 }
2193 else
2194 {
2195 if (!(usage & D3DUSAGE_DYNAMIC))
2196 {
2197 return D3DPOOL_MANAGED;
2198 }
2199 }
2200
2201 return D3DPOOL_DEFAULT;
2202}
2203
daniel@transgaming.com38380882012-11-28 19:36:39 +00002204bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2205 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002206{
2207 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
2208}
2209
daniel@transgaming.com38380882012-11-28 19:36:39 +00002210bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2211 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002212{
2213 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
2214}
2215
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002216bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2217 bool blitRenderTarget, bool blitDepthStencil)
2218{
2219 endScene();
2220
2221 if (blitRenderTarget)
2222 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002223 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2224 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2225 RenderTarget9 *readRenderTarget = NULL;
2226 RenderTarget9 *drawRenderTarget = NULL;
2227 IDirect3DSurface9* readSurface = NULL;
2228 IDirect3DSurface9* drawSurface = NULL;
2229
2230 if (readBuffer)
2231 {
2232 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2233 }
2234 if (drawBuffer)
2235 {
2236 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2237 }
2238
2239 if (readRenderTarget)
2240 {
2241 readSurface = readRenderTarget->getSurface();
2242 }
2243 if (drawRenderTarget)
2244 {
2245 drawSurface = drawRenderTarget->getSurface();
2246 }
2247
2248 if (!readSurface || !drawSurface)
2249 {
2250 ERR("Failed to retrieve the render target.");
2251 return error(GL_OUT_OF_MEMORY, false);
2252 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002253
2254 RECT srcRect, dstRect;
2255 RECT *srcRectPtr = NULL;
2256 RECT *dstRectPtr = NULL;
2257
2258 if (readRect)
2259 {
2260 srcRect.left = readRect->x;
2261 srcRect.right = readRect->x + readRect->width;
2262 srcRect.top = readRect->y;
2263 srcRect.bottom = readRect->y + readRect->height;
2264 srcRectPtr = &srcRect;
2265 }
2266
2267 if (drawRect)
2268 {
2269 dstRect.left = drawRect->x;
2270 dstRect.right = drawRect->x + drawRect->width;
2271 dstRect.top = drawRect->y;
2272 dstRect.bottom = drawRect->y + drawRect->height;
2273 dstRectPtr = &dstRect;
2274 }
2275
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002276 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002277
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002278 readSurface->Release();
2279 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002280
2281 if (FAILED(result))
2282 {
2283 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2284 return false;
2285 }
2286 }
2287
2288 if (blitDepthStencil)
2289 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002290 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2291 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2292 RenderTarget9 *readDepthStencil = NULL;
2293 RenderTarget9 *drawDepthStencil = NULL;
2294 IDirect3DSurface9* readSurface = NULL;
2295 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002296
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002297 if (readBuffer)
2298 {
2299 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2300 }
2301 if (drawBuffer)
2302 {
2303 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2304 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002305
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002306 if (readDepthStencil)
2307 {
2308 readSurface = readDepthStencil->getSurface();
2309 }
2310 if (drawDepthStencil)
2311 {
2312 drawSurface = drawDepthStencil->getSurface();
2313 }
2314
2315 if (!readSurface || !drawSurface)
2316 {
2317 ERR("Failed to retrieve the render target.");
2318 return error(GL_OUT_OF_MEMORY, false);
2319 }
2320
2321 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2322
2323 readSurface->Release();
2324 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002325
2326 if (FAILED(result))
2327 {
2328 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2329 return false;
2330 }
2331 }
2332
2333 return true;
2334}
2335
2336void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2337 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2338{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002339 RenderTarget9 *renderTarget = NULL;
2340 IDirect3DSurface9 *surface = NULL;
2341 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2342
2343 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002344 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002345 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2346 }
2347
2348 if (renderTarget)
2349 {
2350 surface = renderTarget->getSurface();
2351 }
2352
2353 if (!surface)
2354 {
2355 // context must be lost
2356 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002357 }
2358
2359 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002360 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002361
2362 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2363 {
2364 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002365 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002366 return error(GL_OUT_OF_MEMORY);
2367 }
2368
2369 HRESULT result;
2370 IDirect3DSurface9 *systemSurface = NULL;
2371 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2372 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2373 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2374 if (directToPixels)
2375 {
2376 // Use the pixels ptr as a shared handle to write directly into client's memory
2377 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2378 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2379 if (FAILED(result))
2380 {
2381 // Try again without the shared handle
2382 directToPixels = false;
2383 }
2384 }
2385
2386 if (!directToPixels)
2387 {
2388 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2389 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2390 if (FAILED(result))
2391 {
2392 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002393 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002394 return error(GL_OUT_OF_MEMORY);
2395 }
2396 }
2397
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002398 result = mDevice->GetRenderTargetData(surface, systemSurface);
2399 surface->Release();
2400 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002401
2402 if (FAILED(result))
2403 {
2404 systemSurface->Release();
2405
2406 // It turns out that D3D will sometimes produce more error
2407 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002408 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002409 return error(GL_OUT_OF_MEMORY);
2410 else
2411 {
2412 UNREACHABLE();
2413 return;
2414 }
2415
2416 }
2417
2418 if (directToPixels)
2419 {
2420 systemSurface->Release();
2421 return;
2422 }
2423
2424 RECT rect;
2425 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2426 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2427 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2428 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2429
2430 D3DLOCKED_RECT lock;
2431 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2432
2433 if (FAILED(result))
2434 {
2435 UNREACHABLE();
2436 systemSurface->Release();
2437
2438 return; // No sensible error to generate
2439 }
2440
2441 unsigned char *dest = (unsigned char*)pixels;
2442 unsigned short *dest16 = (unsigned short*)pixels;
2443
2444 unsigned char *source;
2445 int inputPitch;
2446 if (packReverseRowOrder)
2447 {
2448 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2449 inputPitch = -lock.Pitch;
2450 }
2451 else
2452 {
2453 source = (unsigned char*)lock.pBits;
2454 inputPitch = lock.Pitch;
2455 }
2456
2457 unsigned int fastPixelSize = 0;
2458
2459 if (desc.Format == D3DFMT_A8R8G8B8 &&
2460 format == GL_BGRA_EXT &&
2461 type == GL_UNSIGNED_BYTE)
2462 {
2463 fastPixelSize = 4;
2464 }
2465 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2466 format == GL_BGRA_EXT &&
2467 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2468 (desc.Format == D3DFMT_A1R5G5B5 &&
2469 format == GL_BGRA_EXT &&
2470 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2471 {
2472 fastPixelSize = 2;
2473 }
2474 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2475 format == GL_RGBA &&
2476 type == GL_HALF_FLOAT_OES)
2477 {
2478 fastPixelSize = 8;
2479 }
2480 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2481 format == GL_RGBA &&
2482 type == GL_FLOAT)
2483 {
2484 fastPixelSize = 16;
2485 }
2486
2487 for (int j = 0; j < rect.bottom - rect.top; j++)
2488 {
2489 if (fastPixelSize != 0)
2490 {
2491 // Fast path for formats which require no translation:
2492 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2493 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2494 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2495 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2496 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2497 //
2498 // Note that buffers with no alpha go through the slow path below.
2499 memcpy(dest + j * outputPitch,
2500 source + j * inputPitch,
2501 (rect.right - rect.left) * fastPixelSize);
2502 continue;
2503 }
2504
2505 for (int i = 0; i < rect.right - rect.left; i++)
2506 {
2507 float r;
2508 float g;
2509 float b;
2510 float a;
2511
2512 switch (desc.Format)
2513 {
2514 case D3DFMT_R5G6B5:
2515 {
2516 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2517
2518 a = 1.0f;
2519 b = (rgb & 0x001F) * (1.0f / 0x001F);
2520 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2521 r = (rgb & 0xF800) * (1.0f / 0xF800);
2522 }
2523 break;
2524 case D3DFMT_A1R5G5B5:
2525 {
2526 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2527
2528 a = (argb & 0x8000) ? 1.0f : 0.0f;
2529 b = (argb & 0x001F) * (1.0f / 0x001F);
2530 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2531 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2532 }
2533 break;
2534 case D3DFMT_A8R8G8B8:
2535 {
2536 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2537
2538 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2539 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2540 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2541 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2542 }
2543 break;
2544 case D3DFMT_X8R8G8B8:
2545 {
2546 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2547
2548 a = 1.0f;
2549 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2550 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2551 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2552 }
2553 break;
2554 case D3DFMT_A2R10G10B10:
2555 {
2556 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2557
2558 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2559 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2560 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2561 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2562 }
2563 break;
2564 case D3DFMT_A32B32G32R32F:
2565 {
2566 // float formats in D3D are stored rgba, rather than the other way round
2567 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2568 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2569 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2570 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2571 }
2572 break;
2573 case D3DFMT_A16B16G16R16F:
2574 {
2575 // float formats in D3D are stored rgba, rather than the other way round
2576 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2577 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2578 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2579 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2580 }
2581 break;
2582 default:
2583 UNIMPLEMENTED(); // FIXME
2584 UNREACHABLE();
2585 return;
2586 }
2587
2588 switch (format)
2589 {
2590 case GL_RGBA:
2591 switch (type)
2592 {
2593 case GL_UNSIGNED_BYTE:
2594 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2595 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2596 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2597 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2598 break;
2599 default: UNREACHABLE();
2600 }
2601 break;
2602 case GL_BGRA_EXT:
2603 switch (type)
2604 {
2605 case GL_UNSIGNED_BYTE:
2606 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2607 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2608 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2609 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2610 break;
2611 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2612 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2613 // this type is packed as follows:
2614 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2615 // --------------------------------------------------------------------------------
2616 // | 4th | 3rd | 2nd | 1st component |
2617 // --------------------------------------------------------------------------------
2618 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2619 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2620 ((unsigned short)(15 * a + 0.5f) << 12)|
2621 ((unsigned short)(15 * r + 0.5f) << 8) |
2622 ((unsigned short)(15 * g + 0.5f) << 4) |
2623 ((unsigned short)(15 * b + 0.5f) << 0);
2624 break;
2625 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2626 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2627 // this type is packed as follows:
2628 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2629 // --------------------------------------------------------------------------------
2630 // | 4th | 3rd | 2nd | 1st component |
2631 // --------------------------------------------------------------------------------
2632 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2633 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2634 ((unsigned short)( a + 0.5f) << 15) |
2635 ((unsigned short)(31 * r + 0.5f) << 10) |
2636 ((unsigned short)(31 * g + 0.5f) << 5) |
2637 ((unsigned short)(31 * b + 0.5f) << 0);
2638 break;
2639 default: UNREACHABLE();
2640 }
2641 break;
2642 case GL_RGB:
2643 switch (type)
2644 {
2645 case GL_UNSIGNED_SHORT_5_6_5:
2646 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2647 ((unsigned short)(31 * b + 0.5f) << 0) |
2648 ((unsigned short)(63 * g + 0.5f) << 5) |
2649 ((unsigned short)(31 * r + 0.5f) << 11);
2650 break;
2651 case GL_UNSIGNED_BYTE:
2652 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2653 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2654 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2655 break;
2656 default: UNREACHABLE();
2657 }
2658 break;
2659 default: UNREACHABLE();
2660 }
2661 }
2662 }
2663
2664 systemSurface->UnlockRect();
2665
2666 systemSurface->Release();
2667}
2668
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002669RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2670{
2671 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2672 IDirect3DSurface9 *surface = NULL;
2673 if (depth)
2674 {
2675 surface = swapChain9->getDepthStencil();
2676 }
2677 else
2678 {
2679 surface = swapChain9->getRenderTarget();
2680 }
2681
2682 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2683
2684 return renderTarget;
2685}
2686
2687RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2688{
2689 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2690 return renderTarget;
2691}
2692
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002693ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, GLenum type, void *data)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002694{
2695 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002696 D3DConstantTable *table = reinterpret_cast<D3DConstantTable *>(data);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002697
2698 switch (type)
2699 {
2700 case GL_VERTEX_SHADER:
2701 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002702 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002703 if (vshader)
2704 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002705 executable = new ShaderExecutable9(function, length, vshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002706 }
2707 }
2708 break;
2709 case GL_FRAGMENT_SHADER:
2710 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002711 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002712 if (pshader)
2713 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002714 executable = new ShaderExecutable9(function, length, pshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002715 }
2716 }
2717 break;
2718 default:
2719 UNREACHABLE();
2720 break;
2721 }
2722
2723 return executable;
2724}
2725
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002726ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2727{
2728 const char *profile = NULL;
2729
2730 switch (type)
2731 {
2732 case GL_VERTEX_SHADER:
2733 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
2734 break;
2735 case GL_FRAGMENT_SHADER:
2736 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
2737 break;
2738 default:
2739 UNREACHABLE();
2740 return NULL;
2741 }
2742
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002743 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002744 if (!binary)
2745 return NULL;
2746
daniel@transgaming.com31240482012-11-28 21:06:41 +00002747 D3DConstantTable *constantTable = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
daniel@transgaming.combe281b02012-11-28 21:05:48 +00002748 if (constantTable->error())
2749 {
2750 delete constantTable;
2751 binary->Release();
2752 return NULL;
2753 }
2754
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002755 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type, constantTable);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00002756 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002757
2758 return executable;
2759}
2760
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002761bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2762{
2763 return mBlit->boxFilter(source, dest);
2764}
2765
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002766D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002767{
2768 if (mD3d9Ex != NULL)
2769 {
2770 return D3DPOOL_DEFAULT;
2771 }
2772 else
2773 {
2774 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2775 {
2776 return D3DPOOL_MANAGED;
2777 }
2778 }
2779
2780 return D3DPOOL_DEFAULT;
2781}
2782
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002783bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2784{
2785 if (source && dest)
2786 {
2787 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2788 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2789
2790 if (fromManaged)
2791 {
2792 D3DSURFACE_DESC desc;
2793 source->GetDesc(&desc);
2794
2795 IDirect3DSurface9 *surf = 0;
2796 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2797
2798 if (SUCCEEDED(result))
2799 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002800 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002801 result = device->UpdateSurface(surf, NULL, dest, NULL);
2802 surf->Release();
2803 }
2804 }
2805 else
2806 {
2807 endScene();
2808 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2809 }
2810
2811 if (FAILED(result))
2812 {
2813 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2814 return false;
2815 }
2816 }
2817
2818 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002819}
2820
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002821Image *Renderer9::createImage()
2822{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002823 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002824}
2825
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002826void Renderer9::generateMipmap(Image *dest, Image *src)
2827{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002828 Image9 *src9 = Image9::makeImage9(src);
2829 Image9 *dst9 = Image9::makeImage9(dest);
2830 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002831}
2832
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002833}