blob: fb4b5a35605974d8dc3eaa711560365389d382c6 [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.com3f255b42012-12-20 21:07:35 +000027#include "libGLESv2/renderer/VertexBuffer9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000028
daniel@transgaming.com3281f972012-10-31 18:38:51 +000029#include "libEGL/Config.h"
daniel@transgaming.comef21ab22012-10-31 17:52:47 +000030#include "libEGL/Display.h"
31
daniel@transgaming.com621ce052012-10-31 17:52:29 +000032// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
33#define REF_RAST 0
34
35// The "Debug This Pixel..." feature in PIX often fails when using the
36// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
37// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
38#if !defined(ANGLE_ENABLE_D3D9EX)
39// Enables use of the IDirect3D9Ex interface, when available
40#define ANGLE_ENABLE_D3D9EX 1
41#endif // !defined(ANGLE_ENABLE_D3D9EX)
42
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000043namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000044{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000045static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000046 {
47 D3DFMT_A1R5G5B5,
48 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
49 D3DFMT_A8R8G8B8,
50 D3DFMT_R5G6B5,
51 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
52 D3DFMT_X8R8G8B8
53 };
54
daniel@transgaming.com222ee082012-11-28 19:31:49 +000055static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000056 {
57 D3DFMT_UNKNOWN,
58 // D3DFMT_D16_LOCKABLE,
59 D3DFMT_D32,
60 // D3DFMT_D15S1,
61 D3DFMT_D24S8,
62 D3DFMT_D24X8,
63 // D3DFMT_D24X4S4,
64 D3DFMT_D16,
65 // D3DFMT_D32F_LOCKABLE,
66 // D3DFMT_D24FS8
67 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000068
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000069Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000070{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000071 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000072
73 mD3d9 = NULL;
74 mD3d9Ex = NULL;
75 mDevice = NULL;
76 mDeviceEx = NULL;
77 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000078 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000079
80 mAdapter = D3DADAPTER_DEFAULT;
81
82 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
83 mDeviceType = D3DDEVTYPE_REF;
84 #else
85 mDeviceType = D3DDEVTYPE_HAL;
86 #endif
87
88 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000089
90 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000091
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000092 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000093
94 mVertexDataManager = NULL;
95 mIndexDataManager = NULL;
96 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +000097
98 mMaxNullColorbufferLRU = 0;
99 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
100 {
101 mNullColorbufferCache[i].lruCount = 0;
102 mNullColorbufferCache[i].width = 0;
103 mNullColorbufferCache[i].height = 0;
104 mNullColorbufferCache[i].buffer = NULL;
105 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000106}
107
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000108Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000109{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000110 releaseDeviceResources();
111
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000112 if (mDevice)
113 {
114 // 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 +0000115 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000116 {
117 resetDevice();
118 }
119
120 mDevice->Release();
121 mDevice = NULL;
122 }
123
124 if (mDeviceEx)
125 {
126 mDeviceEx->Release();
127 mDeviceEx = NULL;
128 }
129
130 if (mD3d9)
131 {
132 mD3d9->Release();
133 mD3d9 = NULL;
134 }
135
136 if (mDeviceWindow)
137 {
138 DestroyWindow(mDeviceWindow);
139 mDeviceWindow = NULL;
140 }
141
142 if (mD3d9Ex)
143 {
144 mD3d9Ex->Release();
145 mD3d9Ex = NULL;
146 }
147
148 if (mD3d9Module)
149 {
150 mD3d9Module = NULL;
151 }
152
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000153 while (!mMultiSampleSupport.empty())
154 {
155 delete [] mMultiSampleSupport.begin()->second;
156 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
157 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000158}
159
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000160Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
161{
162 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
163 return static_cast<rx::Renderer9*>(renderer);
164}
165
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000166EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000167{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000168 if (!initializeCompiler())
169 {
170 return EGL_NOT_INITIALIZED;
171 }
172
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000173 if (mSoftwareDevice)
174 {
175 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
176 }
177 else
178 {
179 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
180 }
181
182 if (mD3d9Module == NULL)
183 {
184 ERR("No D3D9 module found - aborting!\n");
185 return EGL_NOT_INITIALIZED;
186 }
187
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000188 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
189 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
190
191 // Use Direct3D9Ex if available. Among other things, this version is less
192 // inclined to report a lost context, for example when the user switches
193 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
194 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
195 {
196 ASSERT(mD3d9Ex);
197 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
198 ASSERT(mD3d9);
199 }
200 else
201 {
202 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
203 }
204
205 if (!mD3d9)
206 {
207 ERR("Could not create D3D9 device - aborting!\n");
208 return EGL_NOT_INITIALIZED;
209 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000210
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000211 if (mDc != NULL)
212 {
213 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
214 }
215
216 HRESULT result;
217
218 // Give up on getting device caps after about one second.
219 for (int i = 0; i < 10; ++i)
220 {
221 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
222 if (SUCCEEDED(result))
223 {
224 break;
225 }
226 else if (result == D3DERR_NOTAVAILABLE)
227 {
228 Sleep(100); // Give the driver some time to initialize/recover
229 }
230 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
231 {
232 ERR("failed to get device caps (0x%x)\n", result);
233 return EGL_NOT_INITIALIZED;
234 }
235 }
236
237 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
238 {
239 ERR("Renderer does not support PS 2.0. aborting!\n");
240 return EGL_NOT_INITIALIZED;
241 }
242
243 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
244 // This is required by Texture2D::convertToRenderTarget.
245 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
246 {
247 ERR("Renderer does not support stretctrect from textures!\n");
248 return EGL_NOT_INITIALIZED;
249 }
250
251 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
252
253 // ATI cards on XP have problems with non-power-of-two textures.
254 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
255 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
256 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
257 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
258
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000259 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
260 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
261
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000262 mMinSwapInterval = 4;
263 mMaxSwapInterval = 0;
264
265 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
266 {
267 mMinSwapInterval = std::min(mMinSwapInterval, 0);
268 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
269 }
270 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
271 {
272 mMinSwapInterval = std::min(mMinSwapInterval, 1);
273 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
274 }
275 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
276 {
277 mMinSwapInterval = std::min(mMinSwapInterval, 2);
278 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
279 }
280 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
281 {
282 mMinSwapInterval = std::min(mMinSwapInterval, 3);
283 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
284 }
285 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
286 {
287 mMinSwapInterval = std::min(mMinSwapInterval, 4);
288 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
289 }
290
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000291 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000292 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000293 {
294 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000295 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
296 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000297
298 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
299 {
300 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
301 {
302 max = j;
303 }
304 }
305 }
306
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000307 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000308 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000309 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000310 continue;
311
312 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000313 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
314 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000315
316 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
317 {
318 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
319 {
320 max = j;
321 }
322 }
323 }
324
325 mMaxSupportedSamples = max;
326
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000327 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
328 static const TCHAR className[] = TEXT("STATIC");
329
330 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
331
332 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
333 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
334
335 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
336 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
337 {
338 return EGL_BAD_ALLOC;
339 }
340
341 if (FAILED(result))
342 {
343 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
344
345 if (FAILED(result))
346 {
347 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
348 return EGL_BAD_ALLOC;
349 }
350 }
351
352 if (mD3d9Ex)
353 {
354 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
355 ASSERT(SUCCEEDED(result));
356 }
357
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000358 mVertexShaderCache.initialize(mDevice);
359 mPixelShaderCache.initialize(mDevice);
360
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000361 initializeDevice();
362
363 return EGL_SUCCESS;
364}
365
366// do any one-time device initialization
367// NOTE: this is also needed after a device lost/reset
368// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000369void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000370{
371 // Permanent non-default states
372 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
373 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
374
375 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
376 {
377 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
378 }
379 else
380 {
381 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
382 }
383
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000384 markAllStateDirty();
385
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000386 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000387
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000388 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000389 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000390 mVertexDataManager = new rx::VertexDataManager(this);
391 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000392}
393
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000394D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000395{
396 D3DPRESENT_PARAMETERS presentParameters = {0};
397
398 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
399 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
400 presentParameters.BackBufferCount = 1;
401 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
402 presentParameters.BackBufferWidth = 1;
403 presentParameters.BackBufferHeight = 1;
404 presentParameters.EnableAutoDepthStencil = FALSE;
405 presentParameters.Flags = 0;
406 presentParameters.hDeviceWindow = mDeviceWindow;
407 presentParameters.MultiSampleQuality = 0;
408 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
409 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
410 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
411 presentParameters.Windowed = TRUE;
412
413 return presentParameters;
414}
415
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000416int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000417{
418 D3DDISPLAYMODE currentDisplayMode;
419 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
420
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000421 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
422 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000423 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
424 int numConfigs = 0;
425
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000426 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000427 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000428 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000429
430 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
431
432 if (SUCCEEDED(result))
433 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000434 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000435 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000436 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000437 HRESULT result = D3D_OK;
438
439 if(depthStencilFormat != D3DFMT_UNKNOWN)
440 {
441 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
442 }
443
444 if (SUCCEEDED(result))
445 {
446 if(depthStencilFormat != D3DFMT_UNKNOWN)
447 {
448 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
449 }
450
451 if (SUCCEEDED(result))
452 {
453 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000454 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
455 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000456 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
457 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
458
459 (*configDescList)[numConfigs++] = newConfig;
460 }
461 }
462 }
463 }
464 }
465
466 return numConfigs;
467}
468
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000469void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000470{
471 delete [] (configDescList);
472}
473
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000474void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000475{
476 if (!mSceneStarted)
477 {
478 long result = mDevice->BeginScene();
479 if (SUCCEEDED(result)) {
480 // This is defensive checking against the device being
481 // lost at unexpected times.
482 mSceneStarted = true;
483 }
484 }
485}
486
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000487void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000488{
489 if (mSceneStarted)
490 {
491 // EndScene can fail if the device was lost, for example due
492 // to a TDR during a draw call.
493 mDevice->EndScene();
494 mSceneStarted = false;
495 }
496}
497
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000498// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000499void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000500{
501 HRESULT result;
502
503 IDirect3DQuery9* query = allocateEventQuery();
504 if (!query)
505 {
506 return;
507 }
508
509 result = query->Issue(D3DISSUE_END);
510 ASSERT(SUCCEEDED(result));
511
512 do
513 {
514 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
515
516 if(block && result == S_FALSE)
517 {
518 // Keep polling, but allow other threads to do something useful first
519 Sleep(0);
520 // explicitly check for device loss
521 // some drivers seem to return S_FALSE even if the device is lost
522 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000523 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000524 {
525 result = D3DERR_DEVICELOST;
526 }
527 }
528 }
529 while(block && result == S_FALSE);
530
531 freeEventQuery(query);
532
533 if (isDeviceLostError(result))
534 {
535 mDisplay->notifyDeviceLost();
536 }
537}
538
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000539SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
540{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000541 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000542}
543
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000544// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000545IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000546{
547 IDirect3DQuery9 *query = NULL;
548
549 if (mEventQueryPool.empty())
550 {
551 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
552 ASSERT(SUCCEEDED(result));
553 }
554 else
555 {
556 query = mEventQueryPool.back();
557 mEventQueryPool.pop_back();
558 }
559
560 return query;
561}
562
563// D3D9_REPLACE
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000564void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000565{
566 if (mEventQueryPool.size() > 1000)
567 {
568 query->Release();
569 }
570 else
571 {
572 mEventQueryPool.push_back(query);
573 }
574}
575
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000576IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000577{
578 return mVertexShaderCache.create(function, length);
579}
580
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000581IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000582{
583 return mPixelShaderCache.create(function, length);
584}
585
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000586HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
587{
588 D3DPOOL Pool = getBufferPool(Usage);
589 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
590}
591
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000592VertexBuffer *Renderer9::createVertexBuffer()
593{
594 return new VertexBuffer9(this);
595}
596
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000597HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
598{
599 D3DPOOL Pool = getBufferPool(Usage);
600 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
601}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000602
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000603void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000604{
605 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
606 int d3dSampler = index + d3dSamplerOffset;
607
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000608 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
609 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000610
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000611 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000612 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000613 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000614 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
615 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
616 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
617 if (mSupportsTextureFilterAnisotropy)
618 {
619 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
620 }
621}
622
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000623void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000624{
625 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
626 int d3dSampler = index + d3dSamplerOffset;
627 IDirect3DBaseTexture9 *d3dTexture = NULL;
628
629 if (texture)
630 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000631 TextureStorage *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000632 if (texStorage)
633 {
634 d3dTexture = texStorage->getBaseTexture();
635 }
636 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000637 // in the texture class and we're unexpectedly missing the d3d texture
638 ASSERT(d3dTexture != NULL);
639 }
640
641 mDevice->SetTexture(d3dSampler, d3dTexture);
642}
643
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000644void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000645{
646 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000647
648 if (rasterStateChanged)
649 {
650 // Set the cull mode
651 if (rasterState.cullFace)
652 {
653 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
654 }
655 else
656 {
657 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
658 }
659
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000660 if (rasterState.polygonOffsetFill)
661 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000662 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000663 {
664 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
665
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000666 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000667 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
668 }
669 }
670 else
671 {
672 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
673 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
674 }
675
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000676 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000677 }
678
679 mForceSetRasterState = false;
680}
681
682void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
683{
684 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
685 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
686 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
687
688 if (blendStateChanged || blendColorChanged)
689 {
690 if (blendState.blend)
691 {
692 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
693
694 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
695 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
696 {
697 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
698 }
699 else
700 {
701 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
702 gl::unorm<8>(blendColor.alpha),
703 gl::unorm<8>(blendColor.alpha),
704 gl::unorm<8>(blendColor.alpha)));
705 }
706
707 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
708 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
709 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
710
711 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
712 blendState.destBlendRGB != blendState.destBlendAlpha ||
713 blendState.blendEquationRGB != blendState.blendEquationAlpha)
714 {
715 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
716
717 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
718 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
719 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
720 }
721 else
722 {
723 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
724 }
725 }
726 else
727 {
728 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
729 }
730
731 if (blendState.sampleAlphaToCoverage)
732 {
733 FIXME("Sample alpha to coverage is unimplemented.");
734 }
735
736 // Set the color mask
737 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
738 // Apparently some ATI cards have a bug where a draw with a zero color
739 // write mask can cause later draws to have incorrect results. Instead,
740 // set a nonzero color write mask but modify the blend state so that no
741 // drawing is done.
742 // http://code.google.com/p/angleproject/issues/detail?id=169
743
744 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
745 blendState.colorMaskBlue, blendState.colorMaskAlpha);
746 if (colorMask == 0 && !zeroColorMaskAllowed)
747 {
748 // Enable green channel, but set blending so nothing will be drawn.
749 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
750 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
751
752 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
753 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
754 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
755 }
756 else
757 {
758 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
759 }
760
761 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
762
763 mCurBlendState = blendState;
764 mCurBlendColor = blendColor;
765 }
766
767 if (sampleMaskChanged)
768 {
769 // Set the multisample mask
770 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
771 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
772
773 mCurSampleMask = sampleMask;
774 }
775
776 mForceSetBlendState = false;
777}
778
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000779void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000780 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000781{
782 bool depthStencilStateChanged = mForceSetDepthStencilState ||
783 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000784 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
785 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000786 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000787
788 if (depthStencilStateChanged)
789 {
790 if (depthStencilState.depthTest)
791 {
792 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
793 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
794 }
795 else
796 {
797 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
798 }
799
800 mCurDepthStencilState = depthStencilState;
801 }
802
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000803 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000804 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000805 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000806 {
807 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
808 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
809
810 // FIXME: Unsupported by D3D9
811 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
812 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
813 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
814 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000815 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000816 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
817 {
818 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
819 return error(GL_INVALID_OPERATION);
820 }
821
822 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000823 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000824
825 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
826 depthStencilState.stencilWritemask);
827 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
828 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
829
830 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000831 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000832 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
833 depthStencilState.stencilMask);
834
835 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
836 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
837 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
838 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
839 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
840 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
841
842 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
843 depthStencilState.stencilBackWritemask);
844 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
845 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
846
847 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000848 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000849 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
850 depthStencilState.stencilBackMask);
851
852 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
853 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
854 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
855 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
856 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
857 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
858 }
859 else
860 {
861 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
862 }
863
864 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
865
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000866 mCurStencilRef = stencilRef;
867 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000868 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000869 }
870
871 mForceSetDepthStencilState = false;
872}
873
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000874void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000875{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000876 bool scissorChanged = mForceSetScissor ||
877 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
878 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000879
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000880 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000881 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000882 if (enabled)
883 {
884 RECT rect;
885 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
886 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
887 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
888 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
889 mDevice->SetScissorRect(&rect);
890 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000891
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000892 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
893
894 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000895 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000896 }
897
898 mForceSetScissor = false;
899}
900
daniel@transgaming.com12985182012-12-20 20:56:31 +0000901bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
902 bool ignoreViewport, gl::ProgramBinary *currentProgram, bool forceSetUniforms)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000903{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000904 gl::Rectangle actualViewport = viewport;
905 float actualZNear = gl::clamp01(zNear);
906 float actualZFar = gl::clamp01(zFar);
907 if (ignoreViewport)
908 {
909 actualViewport.x = 0;
910 actualViewport.y = 0;
911 actualViewport.width = mRenderTargetDesc.width;
912 actualViewport.height = mRenderTargetDesc.height;
913 actualZNear = 0.0f;
914 actualZFar = 1.0f;
915 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000916
917 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000918 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
919 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
920 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
921 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
922 dxViewport.MinZ = actualZNear;
923 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000924
925 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
926 {
927 return false; // Nothing to render
928 }
929
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000930 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
931 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000932 if (viewportChanged)
933 {
934 mDevice->SetViewport(&dxViewport);
935
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000936 mCurViewport = actualViewport;
937 mCurNear = actualZNear;
938 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000939 }
940
941 if (currentProgram && (viewportChanged || forceSetUniforms))
942 {
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000943 currentProgram->applyDxHalfPixelSize(1.0f / dxViewport.Width, -1.0f / dxViewport.Height);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000944
945 // These values are used for computing gl_FragCoord in Program::linkVaryings().
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000946 currentProgram->applyDxCoord(actualViewport.width * 0.5f,
947 actualViewport.height * 0.5f,
948 actualViewport.x + (actualViewport.width * 0.5f),
949 actualViewport.y + (actualViewport.height * 0.5f));
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000950
daniel@transgaming.com12985182012-12-20 20:56:31 +0000951 GLfloat ccw = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000952 currentProgram->applyDxDepthFront((actualZFar - actualZNear) * 0.5f, (actualZNear + actualZFar) * 0.5f, ccw);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000953
daniel@transgaming.com88853c52012-12-20 20:56:40 +0000954 currentProgram->applyDxDepthRange(actualZNear, actualZFar, actualZFar - actualZNear);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000955 }
956
957 mForceSetViewport = false;
958 return true;
959}
960
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000961bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
962{
963 switch (mode)
964 {
965 case GL_POINTS:
966 mPrimitiveType = D3DPT_POINTLIST;
967 mPrimitiveCount = count;
968 break;
969 case GL_LINES:
970 mPrimitiveType = D3DPT_LINELIST;
971 mPrimitiveCount = count / 2;
972 break;
973 case GL_LINE_LOOP:
974 mPrimitiveType = D3DPT_LINESTRIP;
975 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
976 break;
977 case GL_LINE_STRIP:
978 mPrimitiveType = D3DPT_LINESTRIP;
979 mPrimitiveCount = count - 1;
980 break;
981 case GL_TRIANGLES:
982 mPrimitiveType = D3DPT_TRIANGLELIST;
983 mPrimitiveCount = count / 3;
984 break;
985 case GL_TRIANGLE_STRIP:
986 mPrimitiveType = D3DPT_TRIANGLESTRIP;
987 mPrimitiveCount = count - 2;
988 break;
989 case GL_TRIANGLE_FAN:
990 mPrimitiveType = D3DPT_TRIANGLEFAN;
991 mPrimitiveCount = count - 2;
992 break;
993 default:
994 return error(GL_INVALID_ENUM, false);
995 }
996
997 return mPrimitiveCount > 0;
998}
999
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001000
1001gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1002{
1003 if (!depthbuffer)
1004 {
1005 ERR("Unexpected null depthbuffer for depth-only FBO.");
1006 return NULL;
1007 }
1008
1009 GLsizei width = depthbuffer->getWidth();
1010 GLsizei height = depthbuffer->getHeight();
1011
1012 // search cached nullcolorbuffers
1013 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1014 {
1015 if (mNullColorbufferCache[i].buffer != NULL &&
1016 mNullColorbufferCache[i].width == width &&
1017 mNullColorbufferCache[i].height == height)
1018 {
1019 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1020 return mNullColorbufferCache[i].buffer;
1021 }
1022 }
1023
1024 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1025
1026 // add nullbuffer to the cache
1027 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1028 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1029 {
1030 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1031 {
1032 oldest = &mNullColorbufferCache[i];
1033 }
1034 }
1035
1036 delete oldest->buffer;
1037 oldest->buffer = nullbuffer;
1038 oldest->lruCount = ++mMaxNullColorbufferLRU;
1039 oldest->width = width;
1040 oldest->height = height;
1041
1042 return nullbuffer;
1043}
1044
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001045bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001046{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001047 // if there is no color attachment we must synthesize a NULL colorattachment
1048 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1049 gl::Renderbuffer *renderbufferObject = NULL;
1050 if (framebuffer->getColorbufferType() != GL_NONE)
1051 {
1052 renderbufferObject = framebuffer->getColorbuffer();
1053 }
1054 else
1055 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001056 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001057 }
1058 if (!renderbufferObject)
1059 {
1060 ERR("unable to locate renderbuffer for FBO.");
1061 return false;
1062 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001063
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001064 bool renderTargetChanged = false;
1065 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1066 if (renderTargetSerial != mAppliedRenderTargetSerial)
1067 {
1068 // Apply the render target on the device
1069 IDirect3DSurface9 *renderTargetSurface = NULL;
1070
1071 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1072 if (renderTarget)
1073 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001074 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001075 }
1076
1077 if (!renderTargetSurface)
1078 {
1079 ERR("render target pointer unexpectedly null.");
1080 return false; // Context must be lost
1081 }
1082
1083 mDevice->SetRenderTarget(0, renderTargetSurface);
1084 renderTargetSurface->Release();
1085
1086 mAppliedRenderTargetSerial = renderTargetSerial;
1087 renderTargetChanged = true;
1088 }
1089
1090 gl::Renderbuffer *depthStencil = NULL;
1091 unsigned int depthbufferSerial = 0;
1092 unsigned int stencilbufferSerial = 0;
1093 if (framebuffer->getDepthbufferType() != GL_NONE)
1094 {
1095 depthStencil = framebuffer->getDepthbuffer();
1096 if (!depthStencil)
1097 {
1098 ERR("Depth stencil pointer unexpectedly null.");
1099 return false;
1100 }
1101
1102 depthbufferSerial = depthStencil->getSerial();
1103 }
1104 else if (framebuffer->getStencilbufferType() != GL_NONE)
1105 {
1106 depthStencil = framebuffer->getStencilbuffer();
1107 if (!depthStencil)
1108 {
1109 ERR("Depth stencil pointer unexpectedly null.");
1110 return false;
1111 }
1112
1113 stencilbufferSerial = depthStencil->getSerial();
1114 }
1115
1116 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1117 stencilbufferSerial != mAppliedStencilbufferSerial ||
1118 !mDepthStencilInitialized)
1119 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001120 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001121 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001122
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001123 // Apply the depth stencil on the device
1124 if (depthStencil)
1125 {
1126 IDirect3DSurface9 *depthStencilSurface = NULL;
1127 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1128
1129 if (depthStencilRenderTarget)
1130 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001131 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001132 }
1133
1134 if (!depthStencilSurface)
1135 {
1136 ERR("depth stencil pointer unexpectedly null.");
1137 return false; // Context must be lost
1138 }
1139
1140 mDevice->SetDepthStencilSurface(depthStencilSurface);
1141 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001142
1143 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001144 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001145 }
1146 else
1147 {
1148 mDevice->SetDepthStencilSurface(NULL);
1149 }
1150
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001151 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1152 {
1153 mCurDepthSize = depthSize;
1154 mForceSetRasterState = true;
1155 }
1156
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001157 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1158 {
1159 mCurStencilSize = stencilSize;
1160 mForceSetDepthStencilState = true;
1161 }
1162
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001163 mAppliedDepthbufferSerial = depthbufferSerial;
1164 mAppliedStencilbufferSerial = stencilbufferSerial;
1165 mDepthStencilInitialized = true;
1166 }
1167
1168 if (renderTargetChanged || !mRenderTargetDescInitialized)
1169 {
1170 mForceSetScissor = true;
1171 mForceSetViewport = true;
1172
1173 mRenderTargetDesc.width = renderbufferObject->getWidth();
1174 mRenderTargetDesc.height = renderbufferObject->getHeight();
1175 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1176 mRenderTargetDescInitialized = true;
1177 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001178
1179 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001180}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001181
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001182GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001183{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001184 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001185 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1186 if (err != GL_NO_ERROR)
1187 {
1188 return err;
1189 }
1190
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001191 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1192}
1193
1194// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001195GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001196{
1197 IDirect3DIndexBuffer9 *indexBuffer;
1198 unsigned int serial;
1199 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo, &indexBuffer, &serial);
1200
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001201 if (err == GL_NO_ERROR)
1202 {
1203 if (serial != mAppliedIBSerial)
1204 {
1205 mDevice->SetIndices(indexBuffer);
1206 mAppliedIBSerial = serial;
1207 }
1208 }
1209
1210 return err;
1211}
1212
1213void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1214{
1215 startScene();
1216
1217 if (mode == GL_LINE_LOOP)
1218 {
1219 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1220 }
1221 else if (instances > 0)
1222 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001223 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001224 if (countingIB)
1225 {
1226 if (mAppliedIBSerial != countingIB->getSerial())
1227 {
1228 mDevice->SetIndices(countingIB->getBuffer());
1229 mAppliedIBSerial = countingIB->getSerial();
1230 }
1231
1232 for (int i = 0; i < mRepeatDraw; i++)
1233 {
1234 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1235 }
1236 }
1237 else
1238 {
1239 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1240 return error(GL_OUT_OF_MEMORY);
1241 }
1242 }
1243 else // Regular case
1244 {
1245 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1246 }
1247}
1248
daniel@transgaming.com31240482012-11-28 21:06:41 +00001249void 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 +00001250{
1251 startScene();
1252
1253 if (mode == GL_LINE_LOOP)
1254 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001255 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001256 }
1257 else
1258 {
1259 for (int i = 0; i < mRepeatDraw; i++)
1260 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001261 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1262 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001263 }
1264 }
1265}
1266
1267void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1268{
1269 // Get the raw indices for an indexed draw
1270 if (type != GL_NONE && elementArrayBuffer)
1271 {
1272 gl::Buffer *indexBuffer = elementArrayBuffer;
1273 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1274 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1275 }
1276
1277 UINT startIndex = 0;
1278 bool succeeded = false;
1279
1280 if (get32BitIndexSupport())
1281 {
1282 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1283
1284 if (!mLineLoopIB)
1285 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001286 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001287 }
1288
1289 if (mLineLoopIB)
1290 {
1291 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
1292
1293 UINT offset = 0;
1294 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
1295 startIndex = offset / 4;
1296
1297 if (data)
1298 {
1299 switch (type)
1300 {
1301 case GL_NONE: // Non-indexed draw
1302 for (int i = 0; i < count; i++)
1303 {
1304 data[i] = i;
1305 }
1306 data[count] = 0;
1307 break;
1308 case GL_UNSIGNED_BYTE:
1309 for (int i = 0; i < count; i++)
1310 {
1311 data[i] = static_cast<const GLubyte*>(indices)[i];
1312 }
1313 data[count] = static_cast<const GLubyte*>(indices)[0];
1314 break;
1315 case GL_UNSIGNED_SHORT:
1316 for (int i = 0; i < count; i++)
1317 {
1318 data[i] = static_cast<const GLushort*>(indices)[i];
1319 }
1320 data[count] = static_cast<const GLushort*>(indices)[0];
1321 break;
1322 case GL_UNSIGNED_INT:
1323 for (int i = 0; i < count; i++)
1324 {
1325 data[i] = static_cast<const GLuint*>(indices)[i];
1326 }
1327 data[count] = static_cast<const GLuint*>(indices)[0];
1328 break;
1329 default: UNREACHABLE();
1330 }
1331
1332 mLineLoopIB->unmap();
1333 succeeded = true;
1334 }
1335 }
1336 }
1337 else
1338 {
1339 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
1340
1341 if (!mLineLoopIB)
1342 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001343 mLineLoopIB = new StreamingIndexBuffer(this, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001344 }
1345
1346 if (mLineLoopIB)
1347 {
1348 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
1349
1350 UINT offset = 0;
1351 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
1352 startIndex = offset / 2;
1353
1354 if (data)
1355 {
1356 switch (type)
1357 {
1358 case GL_NONE: // Non-indexed draw
1359 for (int i = 0; i < count; i++)
1360 {
1361 data[i] = i;
1362 }
1363 data[count] = 0;
1364 break;
1365 case GL_UNSIGNED_BYTE:
1366 for (int i = 0; i < count; i++)
1367 {
1368 data[i] = static_cast<const GLubyte*>(indices)[i];
1369 }
1370 data[count] = static_cast<const GLubyte*>(indices)[0];
1371 break;
1372 case GL_UNSIGNED_SHORT:
1373 for (int i = 0; i < count; i++)
1374 {
1375 data[i] = static_cast<const GLushort*>(indices)[i];
1376 }
1377 data[count] = static_cast<const GLushort*>(indices)[0];
1378 break;
1379 case GL_UNSIGNED_INT:
1380 for (int i = 0; i < count; i++)
1381 {
1382 data[i] = static_cast<const GLuint*>(indices)[i];
1383 }
1384 data[count] = static_cast<const GLuint*>(indices)[0];
1385 break;
1386 default: UNREACHABLE();
1387 }
1388
1389 mLineLoopIB->unmap();
1390 succeeded = true;
1391 }
1392 }
1393 }
1394
1395 if (succeeded)
1396 {
1397 if (mAppliedIBSerial != mLineLoopIB->getSerial())
1398 {
1399 mDevice->SetIndices(mLineLoopIB->getBuffer());
1400 mAppliedIBSerial = mLineLoopIB->getSerial();
1401 }
1402
1403 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1404 }
1405 else
1406 {
1407 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
1408 return error(GL_OUT_OF_MEMORY);
1409 }
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001410}
1411
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001412void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1413{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001414 unsigned int programBinarySerial = programBinary->getSerial();
1415 if (programBinarySerial != mAppliedProgramBinarySerial)
1416 {
1417 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1418 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001419
daniel@transgaming.come4991412012-12-20 20:55:34 +00001420 IDirect3DVertexShader9 *vertexShader = NULL;
1421 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001422
daniel@transgaming.come4991412012-12-20 20:55:34 +00001423 IDirect3DPixelShader9 *pixelShader = NULL;
1424 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001425
daniel@transgaming.come4991412012-12-20 20:55:34 +00001426 mDevice->SetPixelShader(pixelShader);
1427 mDevice->SetVertexShader(vertexShader);
1428 programBinary->dirtyAllUniforms();
1429
1430 mAppliedProgramBinarySerial = programBinarySerial;
1431 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001432}
1433
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001434void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001435{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001436 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1437 gl::unorm<8>(clearParams.colorClearValue.red),
1438 gl::unorm<8>(clearParams.colorClearValue.green),
1439 gl::unorm<8>(clearParams.colorClearValue.blue));
1440 float depth = gl::clamp01(clearParams.depthClearValue);
1441 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001442
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001443 unsigned int stencilUnmasked = 0x0;
1444 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1445 {
1446 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1447 stencilUnmasked = (0x1 << stencilSize) - 1;
1448 }
1449
1450 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1451
1452 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1453 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1454 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1455 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1456 clearParams.colorMaskBlue && alphaUnmasked);
1457
1458 if (needMaskedColorClear || needMaskedStencilClear)
1459 {
1460 // State which is altered in all paths from this point to the clear call is saved.
1461 // State which is altered in only some paths will be flagged dirty in the case that
1462 // that path is taken.
1463 HRESULT hr;
1464 if (mMaskedClearSavedState == NULL)
1465 {
1466 hr = mDevice->BeginStateBlock();
1467 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1468
1469 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1470 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1471 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1472 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1473 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1474 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1475 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1476 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1477 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1478 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1479 mDevice->SetPixelShader(NULL);
1480 mDevice->SetVertexShader(NULL);
1481 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1482 mDevice->SetStreamSource(0, NULL, 0, 0);
1483 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1484 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1485 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1486 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1487 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1488 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1489 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1490
1491 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1492 {
1493 mDevice->SetStreamSourceFreq(i, 1);
1494 }
1495
1496 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1497 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1498 }
1499
1500 ASSERT(mMaskedClearSavedState != NULL);
1501
1502 if (mMaskedClearSavedState != NULL)
1503 {
1504 hr = mMaskedClearSavedState->Capture();
1505 ASSERT(SUCCEEDED(hr));
1506 }
1507
1508 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1509 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1510 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1511 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1512 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1513 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1514 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1515 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1516
1517 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1518 {
1519 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1520 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1521 clearParams.colorMaskGreen,
1522 clearParams.colorMaskBlue,
1523 clearParams.colorMaskAlpha));
1524 }
1525 else
1526 {
1527 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1528 }
1529
1530 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1531 {
1532 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1533 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1534 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1535 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1536 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1537 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1538 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1539 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1540 }
1541 else
1542 {
1543 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1544 }
1545
1546 mDevice->SetPixelShader(NULL);
1547 mDevice->SetVertexShader(NULL);
1548 mDevice->SetFVF(D3DFVF_XYZRHW);
1549 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1550 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1551 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1552 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1553 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1554 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1555 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1556
1557 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1558 {
1559 mDevice->SetStreamSourceFreq(i, 1);
1560 }
1561
1562 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1563 quad[0][0] = -0.5f;
1564 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1565 quad[0][2] = 0.0f;
1566 quad[0][3] = 1.0f;
1567
1568 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1569 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1570 quad[1][2] = 0.0f;
1571 quad[1][3] = 1.0f;
1572
1573 quad[2][0] = -0.5f;
1574 quad[2][1] = -0.5f;
1575 quad[2][2] = 0.0f;
1576 quad[2][3] = 1.0f;
1577
1578 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1579 quad[3][1] = -0.5f;
1580 quad[3][2] = 0.0f;
1581 quad[3][3] = 1.0f;
1582
1583 startScene();
1584 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1585
1586 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1587 {
1588 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1589 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1590 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1591 }
1592
1593 if (mMaskedClearSavedState != NULL)
1594 {
1595 mMaskedClearSavedState->Apply();
1596 }
1597 }
1598 else if (clearParams.mask)
1599 {
1600 DWORD dxClearFlags = 0;
1601 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1602 {
1603 dxClearFlags |= D3DCLEAR_TARGET;
1604 }
1605 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1606 {
1607 dxClearFlags |= D3DCLEAR_ZBUFFER;
1608 }
1609 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1610 {
1611 dxClearFlags |= D3DCLEAR_STENCIL;
1612 }
1613
1614 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1615 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001616}
1617
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001618void Renderer9::markAllStateDirty()
1619{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001620 mAppliedRenderTargetSerial = 0;
1621 mAppliedDepthbufferSerial = 0;
1622 mAppliedStencilbufferSerial = 0;
1623 mDepthStencilInitialized = false;
1624 mRenderTargetDescInitialized = false;
1625
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001626 mForceSetDepthStencilState = true;
1627 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001628 mForceSetScissor = true;
1629 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001630 mForceSetBlendState = true;
1631
1632 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001633 mAppliedProgramBinarySerial = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001634
1635 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001636}
1637
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001638void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001639{
1640 while (!mEventQueryPool.empty())
1641 {
1642 mEventQueryPool.back()->Release();
1643 mEventQueryPool.pop_back();
1644 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001645
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001646 if (mMaskedClearSavedState)
1647 {
1648 mMaskedClearSavedState->Release();
1649 mMaskedClearSavedState = NULL;
1650 }
1651
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001652 mVertexShaderCache.clear();
1653 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001654
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001655 delete mBlit;
1656 mBlit = NULL;
1657
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001658 delete mVertexDataManager;
1659 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001660
1661 delete mIndexDataManager;
1662 mIndexDataManager = NULL;
1663
1664 delete mLineLoopIB;
1665 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001666
1667 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1668 {
1669 delete mNullColorbufferCache[i].buffer;
1670 mNullColorbufferCache[i].buffer = NULL;
1671 }
1672
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001673}
1674
1675
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001676void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001677{
1678 mDeviceLost = true;
1679}
1680
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001681bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001682{
1683 return mDeviceLost;
1684}
1685
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001686// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001687bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001688{
1689 bool isLost = false;
1690
1691 if (mDeviceEx)
1692 {
1693 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
1694 }
1695 else if (mDevice)
1696 {
1697 isLost = FAILED(mDevice->TestCooperativeLevel());
1698 }
1699 else
1700 {
1701 // No device yet, so no reset required
1702 }
1703
1704 if (isLost)
1705 {
1706 // ensure we note the device loss --
1707 // we'll probably get this done again by markDeviceLost
1708 // but best to remember it!
1709 // Note that we don't want to clear the device loss status here
1710 // -- this needs to be done by resetDevice
1711 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001712 if (notify)
1713 {
1714 mDisplay->notifyDeviceLost();
1715 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001716 }
1717
1718 return isLost;
1719}
1720
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001721bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001722{
1723 HRESULT status = D3D_OK;
1724
1725 if (mDeviceEx)
1726 {
1727 status = mDeviceEx->CheckDeviceState(NULL);
1728 }
1729 else if (mDevice)
1730 {
1731 status = mDevice->TestCooperativeLevel();
1732 }
1733
1734 switch (status)
1735 {
1736 case D3DERR_DEVICENOTRESET:
1737 case D3DERR_DEVICEHUNG:
1738 return true;
1739 default:
1740 return false;
1741 }
1742}
1743
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001744bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001745{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001746 releaseDeviceResources();
1747
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001748 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
1749
1750 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001751 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001752 int attempts = 3;
1753
1754 while (lost && attempts > 0)
1755 {
1756 if (mDeviceEx)
1757 {
1758 Sleep(500); // Give the graphics driver some CPU time
1759 result = mDeviceEx->ResetEx(&presentParameters, NULL);
1760 }
1761 else
1762 {
1763 result = mDevice->TestCooperativeLevel();
1764 while (result == D3DERR_DEVICELOST)
1765 {
1766 Sleep(100); // Give the graphics driver some CPU time
1767 result = mDevice->TestCooperativeLevel();
1768 }
1769
1770 if (result == D3DERR_DEVICENOTRESET)
1771 {
1772 result = mDevice->Reset(&presentParameters);
1773 }
1774 }
1775
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00001776 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001777 attempts --;
1778 }
1779
1780 if (FAILED(result))
1781 {
1782 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
1783 return false;
1784 }
1785
1786 // reset device defaults
1787 initializeDevice();
1788 mDeviceLost = false;
1789
1790 return true;
1791}
1792
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001793DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001794{
1795 return mAdapterIdentifier.VendorId;
1796}
1797
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001798const char *Renderer9::getAdapterDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001799{
1800 return mAdapterIdentifier.Description;
1801}
1802
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001803GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001804{
1805 return mAdapterIdentifier.DeviceIdentifier;
1806}
1807
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001808void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001809{
1810 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
1811 {
1812 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
1813 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
1814
1815 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
1816 }
1817}
1818
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001819bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001820{
1821 D3DDISPLAYMODE currentDisplayMode;
1822 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1823
1824 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
1825}
1826
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001827bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001828{
1829 D3DDISPLAYMODE currentDisplayMode;
1830 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1831
1832 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
1833}
1834
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001835bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001836{
1837 D3DDISPLAYMODE currentDisplayMode;
1838 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1839
1840 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
1841}
1842
1843// we use INTZ for depth textures in Direct3D9
1844// we also want NULL texture support to ensure the we can make depth-only FBOs
1845// see http://aras-p.info/texts/D3D9GPUHacks.html
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001846bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001847{
1848 D3DDISPLAYMODE currentDisplayMode;
1849 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1850
1851 bool intz = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1852 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ));
1853 bool null = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
1854 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
1855
1856 return intz && null;
1857}
1858
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001859bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001860{
1861 D3DDISPLAYMODE currentDisplayMode;
1862 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1863
1864 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1865 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1866 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1867 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1868
1869 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1870 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
1871 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1872 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1873
1874 if (!*filtering && !*renderable)
1875 {
1876 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1877 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
1878 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1879 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
1880 }
1881 else
1882 {
1883 return true;
1884 }
1885}
1886
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001887bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001888{
1889 D3DDISPLAYMODE currentDisplayMode;
1890 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1891
1892 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1893 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1894 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
1895 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1896
1897 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1898 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1899 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
1900 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1901
1902 if (!*filtering && !*renderable)
1903 {
1904 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1905 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
1906 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
1907 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
1908 }
1909 else
1910 {
1911 return true;
1912 }
1913}
1914
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001915bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001916{
1917 D3DDISPLAYMODE currentDisplayMode;
1918 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1919
1920 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
1921}
1922
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001923bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001924{
1925 D3DDISPLAYMODE currentDisplayMode;
1926 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1927
1928 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
1929}
1930
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001931bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001932{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001933 return mSupportsTextureFilterAnisotropy;
1934}
1935
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001936float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00001937{
1938 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001939 {
1940 return mDeviceCaps.MaxAnisotropy;
1941 }
1942 return 1.0f;
1943}
1944
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001945bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001946{
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001947 IDirect3DQuery9 *query = allocateEventQuery();
1948 if (query)
1949 {
1950 freeEventQuery(query);
1951 return true;
1952 }
1953 else
1954 {
1955 return false;
1956 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001957 return true;
1958}
1959
1960// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
1961// We test this using D3D9 by checking support for the R16F format.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001962bool Renderer9::getVertexTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001963{
1964 if (!mDevice || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
1965 {
1966 return false;
1967 }
1968
1969 D3DDISPLAYMODE currentDisplayMode;
1970 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
1971
1972 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
1973
1974 return SUCCEEDED(result);
1975}
1976
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001977bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001978{
1979 return mSupportsNonPower2Textures;
1980}
1981
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001982bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001983{
1984 if (!mDevice)
1985 {
1986 return false;
1987 }
1988
1989 IDirect3DQuery9 *query = NULL;
1990 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &query);
1991 if (SUCCEEDED(result) && query)
1992 {
1993 query->Release();
1994 return true;
1995 }
1996 else
1997 {
1998 return false;
1999 }
2000}
2001
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002002bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002003{
2004 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2005}
2006
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002007bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002008{
2009 // PIX doesn't seem to support using share handles, so disable them.
2010 // D3D9_REPLACE
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002011 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002012}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002013
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002014int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002015{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002016 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002017}
2018
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002019float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002020{
2021 return mDeviceCaps.MaxPointSize;
2022}
2023
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002024int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002025{
2026 return (int)mDeviceCaps.MaxTextureWidth;
2027}
2028
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002029int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002030{
2031 return (int)mDeviceCaps.MaxTextureHeight;
2032}
2033
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002034bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002035{
2036 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2037}
2038
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002039DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002040{
2041 return mDeviceCaps.DeclTypes;
2042}
2043
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002044int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002045{
2046 return mMinSwapInterval;
2047}
2048
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002049int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002050{
2051 return mMaxSwapInterval;
2052}
2053
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002054int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002055{
2056 return mMaxSupportedSamples;
2057}
2058
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002059int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002060{
2061 if (requested == 0)
2062 {
2063 return requested;
2064 }
2065
2066 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2067 if (itr == mMultiSampleSupport.end())
2068 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002069 if (format == D3DFMT_UNKNOWN)
2070 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002071 return -1;
2072 }
2073
2074 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2075 {
2076 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2077 {
2078 return i;
2079 }
2080 }
2081
2082 return -1;
2083}
2084
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002085D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2086{
2087 switch (internalformat)
2088 {
2089 case GL_DEPTH_COMPONENT16:
2090 case GL_DEPTH_COMPONENT32_OES:
2091 case GL_DEPTH24_STENCIL8_OES:
2092 return D3DFMT_INTZ;
2093 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2094 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2095 return D3DFMT_DXT1;
2096 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2097 return D3DFMT_DXT3;
2098 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2099 return D3DFMT_DXT5;
2100 case GL_RGBA32F_EXT:
2101 case GL_RGB32F_EXT:
2102 case GL_ALPHA32F_EXT:
2103 case GL_LUMINANCE32F_EXT:
2104 case GL_LUMINANCE_ALPHA32F_EXT:
2105 return D3DFMT_A32B32G32R32F;
2106 case GL_RGBA16F_EXT:
2107 case GL_RGB16F_EXT:
2108 case GL_ALPHA16F_EXT:
2109 case GL_LUMINANCE16F_EXT:
2110 case GL_LUMINANCE_ALPHA16F_EXT:
2111 return D3DFMT_A16B16G16R16F;
2112 case GL_LUMINANCE8_EXT:
2113 if (getLuminanceTextureSupport())
2114 {
2115 return D3DFMT_L8;
2116 }
2117 break;
2118 case GL_LUMINANCE8_ALPHA8_EXT:
2119 if (getLuminanceAlphaTextureSupport())
2120 {
2121 return D3DFMT_A8L8;
2122 }
2123 break;
2124 case GL_RGB8_OES:
2125 case GL_RGB565:
2126 return D3DFMT_X8R8G8B8;
2127 }
2128
2129 return D3DFMT_A8R8G8B8;
2130}
2131
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002132bool Renderer9::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002133{
2134 bool result = false;
2135
2136 if (source && dest)
2137 {
2138 int levels = source->levelCount();
2139 for (int i = 0; i < levels; ++i)
2140 {
2141 IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
2142 IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
2143
2144 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2145
2146 if (srcSurf) srcSurf->Release();
2147 if (dstSurf) dstSurf->Release();
2148
2149 if (!result)
2150 return false;
2151 }
2152 }
2153
2154 return result;
2155}
2156
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00002157bool Renderer9::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002158{
2159 bool result = false;
2160
2161 if (source && dest)
2162 {
2163 int levels = source->levelCount();
2164 for (int f = 0; f < 6; f++)
2165 {
2166 for (int i = 0; i < levels; i++)
2167 {
2168 IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2169 IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
2170
2171 result = copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
2172
2173 if (srcSurf) srcSurf->Release();
2174 if (dstSurf) dstSurf->Release();
2175
2176 if (!result)
2177 return false;
2178 }
2179 }
2180 }
2181
2182 return result;
2183}
2184
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002185D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002186{
2187 if (mD3d9Ex != NULL)
2188 {
2189 return D3DPOOL_DEFAULT;
2190 }
2191 else
2192 {
2193 if (!(usage & D3DUSAGE_DYNAMIC))
2194 {
2195 return D3DPOOL_MANAGED;
2196 }
2197 }
2198
2199 return D3DPOOL_DEFAULT;
2200}
2201
daniel@transgaming.com38380882012-11-28 19:36:39 +00002202bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2203 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002204{
2205 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, level);
2206}
2207
daniel@transgaming.com38380882012-11-28 19:36:39 +00002208bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
2209 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002210{
2211 return mBlit->copy(framebuffer, sourceRect, destFormat, xoffset, yoffset, storage, target, level);
2212}
2213
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002214bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2215 bool blitRenderTarget, bool blitDepthStencil)
2216{
2217 endScene();
2218
2219 if (blitRenderTarget)
2220 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002221 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2222 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2223 RenderTarget9 *readRenderTarget = NULL;
2224 RenderTarget9 *drawRenderTarget = NULL;
2225 IDirect3DSurface9* readSurface = NULL;
2226 IDirect3DSurface9* drawSurface = NULL;
2227
2228 if (readBuffer)
2229 {
2230 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2231 }
2232 if (drawBuffer)
2233 {
2234 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2235 }
2236
2237 if (readRenderTarget)
2238 {
2239 readSurface = readRenderTarget->getSurface();
2240 }
2241 if (drawRenderTarget)
2242 {
2243 drawSurface = drawRenderTarget->getSurface();
2244 }
2245
2246 if (!readSurface || !drawSurface)
2247 {
2248 ERR("Failed to retrieve the render target.");
2249 return error(GL_OUT_OF_MEMORY, false);
2250 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002251
2252 RECT srcRect, dstRect;
2253 RECT *srcRectPtr = NULL;
2254 RECT *dstRectPtr = NULL;
2255
2256 if (readRect)
2257 {
2258 srcRect.left = readRect->x;
2259 srcRect.right = readRect->x + readRect->width;
2260 srcRect.top = readRect->y;
2261 srcRect.bottom = readRect->y + readRect->height;
2262 srcRectPtr = &srcRect;
2263 }
2264
2265 if (drawRect)
2266 {
2267 dstRect.left = drawRect->x;
2268 dstRect.right = drawRect->x + drawRect->width;
2269 dstRect.top = drawRect->y;
2270 dstRect.bottom = drawRect->y + drawRect->height;
2271 dstRectPtr = &dstRect;
2272 }
2273
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002274 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002275
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002276 readSurface->Release();
2277 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002278
2279 if (FAILED(result))
2280 {
2281 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2282 return false;
2283 }
2284 }
2285
2286 if (blitDepthStencil)
2287 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002288 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2289 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2290 RenderTarget9 *readDepthStencil = NULL;
2291 RenderTarget9 *drawDepthStencil = NULL;
2292 IDirect3DSurface9* readSurface = NULL;
2293 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002294
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002295 if (readBuffer)
2296 {
2297 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2298 }
2299 if (drawBuffer)
2300 {
2301 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2302 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002303
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002304 if (readDepthStencil)
2305 {
2306 readSurface = readDepthStencil->getSurface();
2307 }
2308 if (drawDepthStencil)
2309 {
2310 drawSurface = drawDepthStencil->getSurface();
2311 }
2312
2313 if (!readSurface || !drawSurface)
2314 {
2315 ERR("Failed to retrieve the render target.");
2316 return error(GL_OUT_OF_MEMORY, false);
2317 }
2318
2319 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2320
2321 readSurface->Release();
2322 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002323
2324 if (FAILED(result))
2325 {
2326 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2327 return false;
2328 }
2329 }
2330
2331 return true;
2332}
2333
2334void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2335 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2336{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002337 RenderTarget9 *renderTarget = NULL;
2338 IDirect3DSurface9 *surface = NULL;
2339 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2340
2341 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002342 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002343 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2344 }
2345
2346 if (renderTarget)
2347 {
2348 surface = renderTarget->getSurface();
2349 }
2350
2351 if (!surface)
2352 {
2353 // context must be lost
2354 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002355 }
2356
2357 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002358 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002359
2360 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2361 {
2362 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002363 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002364 return error(GL_OUT_OF_MEMORY);
2365 }
2366
2367 HRESULT result;
2368 IDirect3DSurface9 *systemSurface = NULL;
2369 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2370 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2371 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2372 if (directToPixels)
2373 {
2374 // Use the pixels ptr as a shared handle to write directly into client's memory
2375 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2376 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2377 if (FAILED(result))
2378 {
2379 // Try again without the shared handle
2380 directToPixels = false;
2381 }
2382 }
2383
2384 if (!directToPixels)
2385 {
2386 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2387 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2388 if (FAILED(result))
2389 {
2390 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002391 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002392 return error(GL_OUT_OF_MEMORY);
2393 }
2394 }
2395
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002396 result = mDevice->GetRenderTargetData(surface, systemSurface);
2397 surface->Release();
2398 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002399
2400 if (FAILED(result))
2401 {
2402 systemSurface->Release();
2403
2404 // It turns out that D3D will sometimes produce more error
2405 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002406 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002407 return error(GL_OUT_OF_MEMORY);
2408 else
2409 {
2410 UNREACHABLE();
2411 return;
2412 }
2413
2414 }
2415
2416 if (directToPixels)
2417 {
2418 systemSurface->Release();
2419 return;
2420 }
2421
2422 RECT rect;
2423 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2424 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2425 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2426 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2427
2428 D3DLOCKED_RECT lock;
2429 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2430
2431 if (FAILED(result))
2432 {
2433 UNREACHABLE();
2434 systemSurface->Release();
2435
2436 return; // No sensible error to generate
2437 }
2438
2439 unsigned char *dest = (unsigned char*)pixels;
2440 unsigned short *dest16 = (unsigned short*)pixels;
2441
2442 unsigned char *source;
2443 int inputPitch;
2444 if (packReverseRowOrder)
2445 {
2446 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2447 inputPitch = -lock.Pitch;
2448 }
2449 else
2450 {
2451 source = (unsigned char*)lock.pBits;
2452 inputPitch = lock.Pitch;
2453 }
2454
2455 unsigned int fastPixelSize = 0;
2456
2457 if (desc.Format == D3DFMT_A8R8G8B8 &&
2458 format == GL_BGRA_EXT &&
2459 type == GL_UNSIGNED_BYTE)
2460 {
2461 fastPixelSize = 4;
2462 }
2463 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2464 format == GL_BGRA_EXT &&
2465 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2466 (desc.Format == D3DFMT_A1R5G5B5 &&
2467 format == GL_BGRA_EXT &&
2468 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2469 {
2470 fastPixelSize = 2;
2471 }
2472 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2473 format == GL_RGBA &&
2474 type == GL_HALF_FLOAT_OES)
2475 {
2476 fastPixelSize = 8;
2477 }
2478 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2479 format == GL_RGBA &&
2480 type == GL_FLOAT)
2481 {
2482 fastPixelSize = 16;
2483 }
2484
2485 for (int j = 0; j < rect.bottom - rect.top; j++)
2486 {
2487 if (fastPixelSize != 0)
2488 {
2489 // Fast path for formats which require no translation:
2490 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2491 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2492 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2493 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2494 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2495 //
2496 // Note that buffers with no alpha go through the slow path below.
2497 memcpy(dest + j * outputPitch,
2498 source + j * inputPitch,
2499 (rect.right - rect.left) * fastPixelSize);
2500 continue;
2501 }
2502
2503 for (int i = 0; i < rect.right - rect.left; i++)
2504 {
2505 float r;
2506 float g;
2507 float b;
2508 float a;
2509
2510 switch (desc.Format)
2511 {
2512 case D3DFMT_R5G6B5:
2513 {
2514 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2515
2516 a = 1.0f;
2517 b = (rgb & 0x001F) * (1.0f / 0x001F);
2518 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2519 r = (rgb & 0xF800) * (1.0f / 0xF800);
2520 }
2521 break;
2522 case D3DFMT_A1R5G5B5:
2523 {
2524 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2525
2526 a = (argb & 0x8000) ? 1.0f : 0.0f;
2527 b = (argb & 0x001F) * (1.0f / 0x001F);
2528 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2529 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2530 }
2531 break;
2532 case D3DFMT_A8R8G8B8:
2533 {
2534 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2535
2536 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2537 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2538 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2539 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2540 }
2541 break;
2542 case D3DFMT_X8R8G8B8:
2543 {
2544 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2545
2546 a = 1.0f;
2547 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2548 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2549 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2550 }
2551 break;
2552 case D3DFMT_A2R10G10B10:
2553 {
2554 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2555
2556 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2557 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2558 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2559 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2560 }
2561 break;
2562 case D3DFMT_A32B32G32R32F:
2563 {
2564 // float formats in D3D are stored rgba, rather than the other way round
2565 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2566 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2567 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2568 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2569 }
2570 break;
2571 case D3DFMT_A16B16G16R16F:
2572 {
2573 // float formats in D3D are stored rgba, rather than the other way round
2574 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2575 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2576 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2577 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2578 }
2579 break;
2580 default:
2581 UNIMPLEMENTED(); // FIXME
2582 UNREACHABLE();
2583 return;
2584 }
2585
2586 switch (format)
2587 {
2588 case GL_RGBA:
2589 switch (type)
2590 {
2591 case GL_UNSIGNED_BYTE:
2592 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2593 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2594 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2595 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2596 break;
2597 default: UNREACHABLE();
2598 }
2599 break;
2600 case GL_BGRA_EXT:
2601 switch (type)
2602 {
2603 case GL_UNSIGNED_BYTE:
2604 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2605 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2606 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2607 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2608 break;
2609 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2610 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2611 // this type is packed as follows:
2612 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2613 // --------------------------------------------------------------------------------
2614 // | 4th | 3rd | 2nd | 1st component |
2615 // --------------------------------------------------------------------------------
2616 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2617 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2618 ((unsigned short)(15 * a + 0.5f) << 12)|
2619 ((unsigned short)(15 * r + 0.5f) << 8) |
2620 ((unsigned short)(15 * g + 0.5f) << 4) |
2621 ((unsigned short)(15 * b + 0.5f) << 0);
2622 break;
2623 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2624 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2625 // this type is packed as follows:
2626 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2627 // --------------------------------------------------------------------------------
2628 // | 4th | 3rd | 2nd | 1st component |
2629 // --------------------------------------------------------------------------------
2630 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2631 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2632 ((unsigned short)( a + 0.5f) << 15) |
2633 ((unsigned short)(31 * r + 0.5f) << 10) |
2634 ((unsigned short)(31 * g + 0.5f) << 5) |
2635 ((unsigned short)(31 * b + 0.5f) << 0);
2636 break;
2637 default: UNREACHABLE();
2638 }
2639 break;
2640 case GL_RGB:
2641 switch (type)
2642 {
2643 case GL_UNSIGNED_SHORT_5_6_5:
2644 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2645 ((unsigned short)(31 * b + 0.5f) << 0) |
2646 ((unsigned short)(63 * g + 0.5f) << 5) |
2647 ((unsigned short)(31 * r + 0.5f) << 11);
2648 break;
2649 case GL_UNSIGNED_BYTE:
2650 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2651 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2652 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2653 break;
2654 default: UNREACHABLE();
2655 }
2656 break;
2657 default: UNREACHABLE();
2658 }
2659 }
2660 }
2661
2662 systemSurface->UnlockRect();
2663
2664 systemSurface->Release();
2665}
2666
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002667RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2668{
2669 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2670 IDirect3DSurface9 *surface = NULL;
2671 if (depth)
2672 {
2673 surface = swapChain9->getDepthStencil();
2674 }
2675 else
2676 {
2677 surface = swapChain9->getRenderTarget();
2678 }
2679
2680 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2681
2682 return renderTarget;
2683}
2684
2685RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2686{
2687 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2688 return renderTarget;
2689}
2690
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002691ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, GLenum type, void *data)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002692{
2693 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002694 D3DConstantTable *table = reinterpret_cast<D3DConstantTable *>(data);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002695
2696 switch (type)
2697 {
2698 case GL_VERTEX_SHADER:
2699 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002700 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002701 if (vshader)
2702 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002703 executable = new ShaderExecutable9(function, length, vshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002704 }
2705 }
2706 break;
2707 case GL_FRAGMENT_SHADER:
2708 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002709 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002710 if (pshader)
2711 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002712 executable = new ShaderExecutable9(function, length, pshader, table);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002713 }
2714 }
2715 break;
2716 default:
2717 UNREACHABLE();
2718 break;
2719 }
2720
2721 return executable;
2722}
2723
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002724ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
2725{
2726 const char *profile = NULL;
2727
2728 switch (type)
2729 {
2730 case GL_VERTEX_SHADER:
2731 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
2732 break;
2733 case GL_FRAGMENT_SHADER:
2734 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
2735 break;
2736 default:
2737 UNREACHABLE();
2738 return NULL;
2739 }
2740
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00002741 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002742 if (!binary)
2743 return NULL;
2744
daniel@transgaming.com31240482012-11-28 21:06:41 +00002745 D3DConstantTable *constantTable = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
daniel@transgaming.combe281b02012-11-28 21:05:48 +00002746 if (constantTable->error())
2747 {
2748 delete constantTable;
2749 binary->Release();
2750 return NULL;
2751 }
2752
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002753 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type, constantTable);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00002754 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002755
2756 return executable;
2757}
2758
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002759bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2760{
2761 return mBlit->boxFilter(source, dest);
2762}
2763
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002764D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002765{
2766 if (mD3d9Ex != NULL)
2767 {
2768 return D3DPOOL_DEFAULT;
2769 }
2770 else
2771 {
2772 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2773 {
2774 return D3DPOOL_MANAGED;
2775 }
2776 }
2777
2778 return D3DPOOL_DEFAULT;
2779}
2780
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002781bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2782{
2783 if (source && dest)
2784 {
2785 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2786 IDirect3DDevice9 *device = getDevice(); // D3D9_REPLACE
2787
2788 if (fromManaged)
2789 {
2790 D3DSURFACE_DESC desc;
2791 source->GetDesc(&desc);
2792
2793 IDirect3DSurface9 *surf = 0;
2794 result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
2795
2796 if (SUCCEEDED(result))
2797 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002798 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002799 result = device->UpdateSurface(surf, NULL, dest, NULL);
2800 surf->Release();
2801 }
2802 }
2803 else
2804 {
2805 endScene();
2806 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
2807 }
2808
2809 if (FAILED(result))
2810 {
2811 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2812 return false;
2813 }
2814 }
2815
2816 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002817}
2818
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002819Image *Renderer9::createImage()
2820{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002821 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002822}
2823
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002824void Renderer9::generateMipmap(Image *dest, Image *src)
2825{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00002826 Image9 *src9 = Image9::makeImage9(src);
2827 Image9 *dst9 = Image9::makeImage9(dest);
2828 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002829}
2830
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002831}