blob: f3c949ec6930f20dcd5d5ae53168c57be1ea75a6 [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003// 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.com34da3972012-12-20 21:10:29 +000023#include "libGLESv2/renderer/TextureStorage9.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.com0b6d7742012-12-20 21:09:47 +000028#include "libGLESv2/renderer/IndexBuffer9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000029
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000030#include <sstream>
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
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000069enum
70{
71 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
72};
73
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000074Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000075{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000076 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000077
78 mD3d9 = NULL;
79 mD3d9Ex = NULL;
80 mDevice = NULL;
81 mDeviceEx = NULL;
82 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000083 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000084
85 mAdapter = D3DADAPTER_DEFAULT;
86
87 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
88 mDeviceType = D3DDEVTYPE_REF;
89 #else
90 mDeviceType = D3DDEVTYPE_HAL;
91 #endif
92
93 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000094
95 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000096
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000097 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000098
99 mVertexDataManager = NULL;
100 mIndexDataManager = NULL;
101 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000102
103 mMaxNullColorbufferLRU = 0;
104 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
105 {
106 mNullColorbufferCache[i].lruCount = 0;
107 mNullColorbufferCache[i].width = 0;
108 mNullColorbufferCache[i].height = 0;
109 mNullColorbufferCache[i].buffer = NULL;
110 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000111}
112
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000113Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000114{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000115 releaseDeviceResources();
116
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000117 if (mDevice)
118 {
119 // 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 +0000120 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000121 {
122 resetDevice();
123 }
124
125 mDevice->Release();
126 mDevice = NULL;
127 }
128
129 if (mDeviceEx)
130 {
131 mDeviceEx->Release();
132 mDeviceEx = NULL;
133 }
134
135 if (mD3d9)
136 {
137 mD3d9->Release();
138 mD3d9 = NULL;
139 }
140
141 if (mDeviceWindow)
142 {
143 DestroyWindow(mDeviceWindow);
144 mDeviceWindow = NULL;
145 }
146
147 if (mD3d9Ex)
148 {
149 mD3d9Ex->Release();
150 mD3d9Ex = NULL;
151 }
152
153 if (mD3d9Module)
154 {
155 mD3d9Module = NULL;
156 }
157
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000158 while (!mMultiSampleSupport.empty())
159 {
160 delete [] mMultiSampleSupport.begin()->second;
161 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
162 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000163}
164
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000165Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
166{
167 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL);
168 return static_cast<rx::Renderer9*>(renderer);
169}
170
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000171EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000172{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000173 if (!initializeCompiler())
174 {
175 return EGL_NOT_INITIALIZED;
176 }
177
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000178 if (mSoftwareDevice)
179 {
180 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
181 }
182 else
183 {
184 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
185 }
186
187 if (mD3d9Module == NULL)
188 {
189 ERR("No D3D9 module found - aborting!\n");
190 return EGL_NOT_INITIALIZED;
191 }
192
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000193 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
194 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
195
196 // Use Direct3D9Ex if available. Among other things, this version is less
197 // inclined to report a lost context, for example when the user switches
198 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
199 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
200 {
201 ASSERT(mD3d9Ex);
202 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
203 ASSERT(mD3d9);
204 }
205 else
206 {
207 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
208 }
209
210 if (!mD3d9)
211 {
212 ERR("Could not create D3D9 device - aborting!\n");
213 return EGL_NOT_INITIALIZED;
214 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000215
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000216 if (mDc != NULL)
217 {
218 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
219 }
220
221 HRESULT result;
222
223 // Give up on getting device caps after about one second.
224 for (int i = 0; i < 10; ++i)
225 {
226 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
227 if (SUCCEEDED(result))
228 {
229 break;
230 }
231 else if (result == D3DERR_NOTAVAILABLE)
232 {
233 Sleep(100); // Give the driver some time to initialize/recover
234 }
235 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
236 {
237 ERR("failed to get device caps (0x%x)\n", result);
238 return EGL_NOT_INITIALIZED;
239 }
240 }
241
242 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
243 {
244 ERR("Renderer does not support PS 2.0. aborting!\n");
245 return EGL_NOT_INITIALIZED;
246 }
247
248 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
249 // This is required by Texture2D::convertToRenderTarget.
250 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
251 {
252 ERR("Renderer does not support stretctrect from textures!\n");
253 return EGL_NOT_INITIALIZED;
254 }
255
256 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
257
258 // ATI cards on XP have problems with non-power-of-two textures.
259 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
260 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
261 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
262 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
263
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000264 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
265 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
266
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000267 mMinSwapInterval = 4;
268 mMaxSwapInterval = 0;
269
270 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
271 {
272 mMinSwapInterval = std::min(mMinSwapInterval, 0);
273 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
274 }
275 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
276 {
277 mMinSwapInterval = std::min(mMinSwapInterval, 1);
278 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
279 }
280 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
281 {
282 mMinSwapInterval = std::min(mMinSwapInterval, 2);
283 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
284 }
285 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
286 {
287 mMinSwapInterval = std::min(mMinSwapInterval, 3);
288 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
289 }
290 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
291 {
292 mMinSwapInterval = std::min(mMinSwapInterval, 4);
293 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
294 }
295
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000296 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000297 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000298 {
299 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000300 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
301 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000302
303 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
304 {
305 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
306 {
307 max = j;
308 }
309 }
310 }
311
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000312 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000313 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000314 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000315 continue;
316
317 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000318 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
319 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000320
321 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
322 {
323 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
324 {
325 max = j;
326 }
327 }
328 }
329
330 mMaxSupportedSamples = max;
331
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000332 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
333 static const TCHAR className[] = TEXT("STATIC");
334
335 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
336
337 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
338 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
339
340 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
341 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
342 {
343 return EGL_BAD_ALLOC;
344 }
345
346 if (FAILED(result))
347 {
348 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
349
350 if (FAILED(result))
351 {
352 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
353 return EGL_BAD_ALLOC;
354 }
355 }
356
357 if (mD3d9Ex)
358 {
359 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
360 ASSERT(SUCCEEDED(result));
361 }
362
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000363 mVertexShaderCache.initialize(mDevice);
364 mPixelShaderCache.initialize(mDevice);
365
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000366 // Check occlusion query support
367 IDirect3DQuery9 *occlusionQuery = NULL;
368 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
369 {
370 occlusionQuery->Release();
371 mOcclusionQuerySupport = true;
372 }
373 else
374 {
375 mOcclusionQuerySupport = false;
376 }
377
378 // Check event query support
379 IDirect3DQuery9 *eventQuery = NULL;
380 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
381 {
382 eventQuery->Release();
383 mEventQuerySupport = true;
384 }
385 else
386 {
387 mEventQuerySupport = false;
388 }
389
390 D3DDISPLAYMODE currentDisplayMode;
391 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
392
393 // Check vertex texture support
394 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
395 // We test this using D3D9 by checking support for the R16F format.
396 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
397 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
398 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
399
400 // Check depth texture support
401 // we use INTZ for depth textures in Direct3D9
402 // we also want NULL texture support to ensure the we can make depth-only FBOs
403 // see http://aras-p.info/texts/D3D9GPUHacks.html
404 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
405 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
406 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
407 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
408
409 // Check 32 bit floating point texture support
410 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
411 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
412 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
413 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
414
415 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
416 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
417 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
418 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
419
420 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
421 {
422 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
423 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
424 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
425 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
426 }
427 else
428 {
429 mFloat32TextureSupport = true;
430 }
431
432 // Check 16 bit floating point texture support
433 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
434 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
435 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
436 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
437
438 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
439 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
440 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
441 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
442
443 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
444 {
445 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
446 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
447 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
448 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
449 }
450 else
451 {
452 mFloat16TextureSupport = true;
453 }
454
455 // Check DXT texture support
456 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
457 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
458 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
459
460 // Check luminance[alpha] texture support
461 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
462 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
463
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000464 initializeDevice();
465
466 return EGL_SUCCESS;
467}
468
469// do any one-time device initialization
470// NOTE: this is also needed after a device lost/reset
471// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000472void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000473{
474 // Permanent non-default states
475 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
476 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
477
478 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
479 {
480 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
481 }
482 else
483 {
484 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
485 }
486
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000487 markAllStateDirty();
488
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000489 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000490
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000491 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000492 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000493 mVertexDataManager = new rx::VertexDataManager(this);
494 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000495}
496
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000497D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000498{
499 D3DPRESENT_PARAMETERS presentParameters = {0};
500
501 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
502 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
503 presentParameters.BackBufferCount = 1;
504 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
505 presentParameters.BackBufferWidth = 1;
506 presentParameters.BackBufferHeight = 1;
507 presentParameters.EnableAutoDepthStencil = FALSE;
508 presentParameters.Flags = 0;
509 presentParameters.hDeviceWindow = mDeviceWindow;
510 presentParameters.MultiSampleQuality = 0;
511 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
512 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
513 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
514 presentParameters.Windowed = TRUE;
515
516 return presentParameters;
517}
518
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000519int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000520{
521 D3DDISPLAYMODE currentDisplayMode;
522 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
523
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000524 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
525 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000526 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
527 int numConfigs = 0;
528
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000529 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000530 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000531 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000532
533 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
534
535 if (SUCCEEDED(result))
536 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000537 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000538 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000539 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000540 HRESULT result = D3D_OK;
541
542 if(depthStencilFormat != D3DFMT_UNKNOWN)
543 {
544 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
545 }
546
547 if (SUCCEEDED(result))
548 {
549 if(depthStencilFormat != D3DFMT_UNKNOWN)
550 {
551 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
552 }
553
554 if (SUCCEEDED(result))
555 {
556 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000557 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
558 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000559 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
560 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
561
562 (*configDescList)[numConfigs++] = newConfig;
563 }
564 }
565 }
566 }
567 }
568
569 return numConfigs;
570}
571
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000572void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000573{
574 delete [] (configDescList);
575}
576
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000577void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000578{
579 if (!mSceneStarted)
580 {
581 long result = mDevice->BeginScene();
582 if (SUCCEEDED(result)) {
583 // This is defensive checking against the device being
584 // lost at unexpected times.
585 mSceneStarted = true;
586 }
587 }
588}
589
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000590void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000591{
592 if (mSceneStarted)
593 {
594 // EndScene can fail if the device was lost, for example due
595 // to a TDR during a draw call.
596 mDevice->EndScene();
597 mSceneStarted = false;
598 }
599}
600
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000601void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000602{
603 HRESULT result;
604
605 IDirect3DQuery9* query = allocateEventQuery();
606 if (!query)
607 {
608 return;
609 }
610
611 result = query->Issue(D3DISSUE_END);
612 ASSERT(SUCCEEDED(result));
613
614 do
615 {
616 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
617
618 if(block && result == S_FALSE)
619 {
620 // Keep polling, but allow other threads to do something useful first
621 Sleep(0);
622 // explicitly check for device loss
623 // some drivers seem to return S_FALSE even if the device is lost
624 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000625 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000626 {
627 result = D3DERR_DEVICELOST;
628 }
629 }
630 }
631 while(block && result == S_FALSE);
632
633 freeEventQuery(query);
634
635 if (isDeviceLostError(result))
636 {
637 mDisplay->notifyDeviceLost();
638 }
639}
640
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000641SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
642{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000643 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000644}
645
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000646IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000647{
648 IDirect3DQuery9 *query = NULL;
649
650 if (mEventQueryPool.empty())
651 {
652 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
653 ASSERT(SUCCEEDED(result));
654 }
655 else
656 {
657 query = mEventQueryPool.back();
658 mEventQueryPool.pop_back();
659 }
660
661 return query;
662}
663
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000664void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000665{
666 if (mEventQueryPool.size() > 1000)
667 {
668 query->Release();
669 }
670 else
671 {
672 mEventQueryPool.push_back(query);
673 }
674}
675
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000676IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000677{
678 return mVertexShaderCache.create(function, length);
679}
680
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000681IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000682{
683 return mPixelShaderCache.create(function, length);
684}
685
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000686HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
687{
688 D3DPOOL Pool = getBufferPool(Usage);
689 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
690}
691
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000692VertexBuffer *Renderer9::createVertexBuffer()
693{
694 return new VertexBuffer9(this);
695}
696
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000697HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
698{
699 D3DPOOL Pool = getBufferPool(Usage);
700 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
701}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000702
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000703IndexBuffer *Renderer9::createIndexBuffer()
704{
705 return new IndexBuffer9(this);
706}
707
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000708void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000709{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000710 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
711 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000712
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000713 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000714 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000715 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
716 int d3dSampler = index + d3dSamplerOffset;
717
718 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
719 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
720
721 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
722 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
723 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
724 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
725 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
726 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
727 if (mSupportsTextureFilterAnisotropy)
728 {
729 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
730 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000731 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000732
733 forceSetSamplers[index] = false;
734 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000735}
736
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000737void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000738{
739 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
740 int d3dSampler = index + d3dSamplerOffset;
741 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000742 unsigned int serial = 0;
743 bool forceSetTexture = false;
744
745 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000746
747 if (texture)
748 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000749 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000750 if (texStorage)
751 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000752 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000753 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000754 }
755 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000756 // in the texture class and we're unexpectedly missing the d3d texture
757 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000758
759 serial = texture->getTextureSerial();
760 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000761 }
762
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000763 if (forceSetTexture || appliedSerials[index] != serial)
764 {
765 mDevice->SetTexture(d3dSampler, d3dTexture);
766 }
767
768 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000769}
770
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000771void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000772{
773 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000774
775 if (rasterStateChanged)
776 {
777 // Set the cull mode
778 if (rasterState.cullFace)
779 {
780 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
781 }
782 else
783 {
784 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
785 }
786
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000787 if (rasterState.polygonOffsetFill)
788 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000789 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000790 {
791 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
792
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000793 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000794 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
795 }
796 }
797 else
798 {
799 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
800 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
801 }
802
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000803 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000804 }
805
806 mForceSetRasterState = false;
807}
808
809void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
810{
811 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
812 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
813 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
814
815 if (blendStateChanged || blendColorChanged)
816 {
817 if (blendState.blend)
818 {
819 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
820
821 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
822 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
823 {
824 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
825 }
826 else
827 {
828 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
829 gl::unorm<8>(blendColor.alpha),
830 gl::unorm<8>(blendColor.alpha),
831 gl::unorm<8>(blendColor.alpha)));
832 }
833
834 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
835 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
836 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
837
838 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
839 blendState.destBlendRGB != blendState.destBlendAlpha ||
840 blendState.blendEquationRGB != blendState.blendEquationAlpha)
841 {
842 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
843
844 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
845 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
846 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
847 }
848 else
849 {
850 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
851 }
852 }
853 else
854 {
855 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
856 }
857
858 if (blendState.sampleAlphaToCoverage)
859 {
860 FIXME("Sample alpha to coverage is unimplemented.");
861 }
862
863 // Set the color mask
864 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
865 // Apparently some ATI cards have a bug where a draw with a zero color
866 // write mask can cause later draws to have incorrect results. Instead,
867 // set a nonzero color write mask but modify the blend state so that no
868 // drawing is done.
869 // http://code.google.com/p/angleproject/issues/detail?id=169
870
871 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
872 blendState.colorMaskBlue, blendState.colorMaskAlpha);
873 if (colorMask == 0 && !zeroColorMaskAllowed)
874 {
875 // Enable green channel, but set blending so nothing will be drawn.
876 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
877 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
878
879 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
880 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
881 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
882 }
883 else
884 {
885 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
886 }
887
888 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
889
890 mCurBlendState = blendState;
891 mCurBlendColor = blendColor;
892 }
893
894 if (sampleMaskChanged)
895 {
896 // Set the multisample mask
897 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
898 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
899
900 mCurSampleMask = sampleMask;
901 }
902
903 mForceSetBlendState = false;
904}
905
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000906void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000907 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000908{
909 bool depthStencilStateChanged = mForceSetDepthStencilState ||
910 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000911 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
912 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000913 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000914
915 if (depthStencilStateChanged)
916 {
917 if (depthStencilState.depthTest)
918 {
919 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
920 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
921 }
922 else
923 {
924 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
925 }
926
927 mCurDepthStencilState = depthStencilState;
928 }
929
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000930 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000931 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000932 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000933 {
934 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
935 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
936
937 // FIXME: Unsupported by D3D9
938 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
939 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
940 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
941 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000942 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000943 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
944 {
945 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
946 return error(GL_INVALID_OPERATION);
947 }
948
949 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000950 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000951
952 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
953 depthStencilState.stencilWritemask);
954 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
955 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
956
957 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000958 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000959 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
960 depthStencilState.stencilMask);
961
962 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
963 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
964 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
965 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
966 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
967 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
968
969 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
970 depthStencilState.stencilBackWritemask);
971 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
972 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
973
974 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000975 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000976 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
977 depthStencilState.stencilBackMask);
978
979 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
980 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
981 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
982 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
983 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
984 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
985 }
986 else
987 {
988 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
989 }
990
991 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
992
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000993 mCurStencilRef = stencilRef;
994 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000995 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000996 }
997
998 mForceSetDepthStencilState = false;
999}
1000
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001001void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001002{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001003 bool scissorChanged = mForceSetScissor ||
1004 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1005 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001006
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001007 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001008 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001009 if (enabled)
1010 {
1011 RECT rect;
1012 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1013 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1014 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1015 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1016 mDevice->SetScissorRect(&rect);
1017 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001018
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001019 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1020
1021 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001022 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001023 }
1024
1025 mForceSetScissor = false;
1026}
1027
daniel@transgaming.com12985182012-12-20 20:56:31 +00001028bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001029 bool ignoreViewport, gl::ProgramBinary *currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001030{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001031 gl::Rectangle actualViewport = viewport;
1032 float actualZNear = gl::clamp01(zNear);
1033 float actualZFar = gl::clamp01(zFar);
1034 if (ignoreViewport)
1035 {
1036 actualViewport.x = 0;
1037 actualViewport.y = 0;
1038 actualViewport.width = mRenderTargetDesc.width;
1039 actualViewport.height = mRenderTargetDesc.height;
1040 actualZNear = 0.0f;
1041 actualZFar = 1.0f;
1042 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001043
1044 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001045 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1046 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1047 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1048 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1049 dxViewport.MinZ = actualZNear;
1050 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001051
1052 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1053 {
1054 return false; // Nothing to render
1055 }
1056
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001057 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1058 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001059 if (viewportChanged)
1060 {
1061 mDevice->SetViewport(&dxViewport);
1062
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001063 mCurViewport = actualViewport;
1064 mCurNear = actualZNear;
1065 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001066 }
1067
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001068 if (currentProgram)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001069 {
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001070 dx_VertexConstants vc = {0};
1071 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001072
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001073 vc.halfPixelSize[0] = 1.0f / dxViewport.Width;
1074 vc.halfPixelSize[1] = -1.0f / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001075
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001076 pc.coord[0] = actualViewport.width * 0.5f;
1077 pc.coord[1] = actualViewport.height * 0.5f;
1078 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
1079 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001080
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001081 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1082 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1083 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1084
1085 vc.depthRange[0] = actualZNear;
1086 vc.depthRange[1] = actualZFar;
1087 vc.depthRange[2] = actualZFar - actualZNear;
1088
1089 pc.depthRange[0] = actualZNear;
1090 pc.depthRange[1] = actualZFar;
1091 pc.depthRange[2] = actualZFar - actualZNear;
1092
1093 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1094 {
1095 mVertexConstants = vc;
1096 mDxUniformsDirty = true;
1097 }
1098
1099 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1100 {
1101 mPixelConstants = pc;
1102 mDxUniformsDirty = true;
1103 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001104 }
1105
1106 mForceSetViewport = false;
1107 return true;
1108}
1109
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001110bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1111{
1112 switch (mode)
1113 {
1114 case GL_POINTS:
1115 mPrimitiveType = D3DPT_POINTLIST;
1116 mPrimitiveCount = count;
1117 break;
1118 case GL_LINES:
1119 mPrimitiveType = D3DPT_LINELIST;
1120 mPrimitiveCount = count / 2;
1121 break;
1122 case GL_LINE_LOOP:
1123 mPrimitiveType = D3DPT_LINESTRIP;
1124 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1125 break;
1126 case GL_LINE_STRIP:
1127 mPrimitiveType = D3DPT_LINESTRIP;
1128 mPrimitiveCount = count - 1;
1129 break;
1130 case GL_TRIANGLES:
1131 mPrimitiveType = D3DPT_TRIANGLELIST;
1132 mPrimitiveCount = count / 3;
1133 break;
1134 case GL_TRIANGLE_STRIP:
1135 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1136 mPrimitiveCount = count - 2;
1137 break;
1138 case GL_TRIANGLE_FAN:
1139 mPrimitiveType = D3DPT_TRIANGLEFAN;
1140 mPrimitiveCount = count - 2;
1141 break;
1142 default:
1143 return error(GL_INVALID_ENUM, false);
1144 }
1145
1146 return mPrimitiveCount > 0;
1147}
1148
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001149
1150gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1151{
1152 if (!depthbuffer)
1153 {
1154 ERR("Unexpected null depthbuffer for depth-only FBO.");
1155 return NULL;
1156 }
1157
1158 GLsizei width = depthbuffer->getWidth();
1159 GLsizei height = depthbuffer->getHeight();
1160
1161 // search cached nullcolorbuffers
1162 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1163 {
1164 if (mNullColorbufferCache[i].buffer != NULL &&
1165 mNullColorbufferCache[i].width == width &&
1166 mNullColorbufferCache[i].height == height)
1167 {
1168 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1169 return mNullColorbufferCache[i].buffer;
1170 }
1171 }
1172
1173 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1174
1175 // add nullbuffer to the cache
1176 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1177 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1178 {
1179 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1180 {
1181 oldest = &mNullColorbufferCache[i];
1182 }
1183 }
1184
1185 delete oldest->buffer;
1186 oldest->buffer = nullbuffer;
1187 oldest->lruCount = ++mMaxNullColorbufferLRU;
1188 oldest->width = width;
1189 oldest->height = height;
1190
1191 return nullbuffer;
1192}
1193
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001194bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001195{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001196 // if there is no color attachment we must synthesize a NULL colorattachment
1197 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1198 gl::Renderbuffer *renderbufferObject = NULL;
1199 if (framebuffer->getColorbufferType() != GL_NONE)
1200 {
1201 renderbufferObject = framebuffer->getColorbuffer();
1202 }
1203 else
1204 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001205 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001206 }
1207 if (!renderbufferObject)
1208 {
1209 ERR("unable to locate renderbuffer for FBO.");
1210 return false;
1211 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001212
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001213 bool renderTargetChanged = false;
1214 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1215 if (renderTargetSerial != mAppliedRenderTargetSerial)
1216 {
1217 // Apply the render target on the device
1218 IDirect3DSurface9 *renderTargetSurface = NULL;
1219
1220 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1221 if (renderTarget)
1222 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001223 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001224 }
1225
1226 if (!renderTargetSurface)
1227 {
1228 ERR("render target pointer unexpectedly null.");
1229 return false; // Context must be lost
1230 }
1231
1232 mDevice->SetRenderTarget(0, renderTargetSurface);
1233 renderTargetSurface->Release();
1234
1235 mAppliedRenderTargetSerial = renderTargetSerial;
1236 renderTargetChanged = true;
1237 }
1238
1239 gl::Renderbuffer *depthStencil = NULL;
1240 unsigned int depthbufferSerial = 0;
1241 unsigned int stencilbufferSerial = 0;
1242 if (framebuffer->getDepthbufferType() != GL_NONE)
1243 {
1244 depthStencil = framebuffer->getDepthbuffer();
1245 if (!depthStencil)
1246 {
1247 ERR("Depth stencil pointer unexpectedly null.");
1248 return false;
1249 }
1250
1251 depthbufferSerial = depthStencil->getSerial();
1252 }
1253 else if (framebuffer->getStencilbufferType() != GL_NONE)
1254 {
1255 depthStencil = framebuffer->getStencilbuffer();
1256 if (!depthStencil)
1257 {
1258 ERR("Depth stencil pointer unexpectedly null.");
1259 return false;
1260 }
1261
1262 stencilbufferSerial = depthStencil->getSerial();
1263 }
1264
1265 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1266 stencilbufferSerial != mAppliedStencilbufferSerial ||
1267 !mDepthStencilInitialized)
1268 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001269 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001270 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001271
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001272 // Apply the depth stencil on the device
1273 if (depthStencil)
1274 {
1275 IDirect3DSurface9 *depthStencilSurface = NULL;
1276 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1277
1278 if (depthStencilRenderTarget)
1279 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001280 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001281 }
1282
1283 if (!depthStencilSurface)
1284 {
1285 ERR("depth stencil pointer unexpectedly null.");
1286 return false; // Context must be lost
1287 }
1288
1289 mDevice->SetDepthStencilSurface(depthStencilSurface);
1290 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001291
1292 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001293 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001294 }
1295 else
1296 {
1297 mDevice->SetDepthStencilSurface(NULL);
1298 }
1299
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001300 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1301 {
1302 mCurDepthSize = depthSize;
1303 mForceSetRasterState = true;
1304 }
1305
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001306 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1307 {
1308 mCurStencilSize = stencilSize;
1309 mForceSetDepthStencilState = true;
1310 }
1311
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001312 mAppliedDepthbufferSerial = depthbufferSerial;
1313 mAppliedStencilbufferSerial = stencilbufferSerial;
1314 mDepthStencilInitialized = true;
1315 }
1316
1317 if (renderTargetChanged || !mRenderTargetDescInitialized)
1318 {
1319 mForceSetScissor = true;
1320 mForceSetViewport = true;
1321
1322 mRenderTargetDesc.width = renderbufferObject->getWidth();
1323 mRenderTargetDesc.height = renderbufferObject->getHeight();
1324 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1325 mRenderTargetDescInitialized = true;
1326 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001327
1328 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001329}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001330
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001331GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001332{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001333 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001334 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1335 if (err != GL_NO_ERROR)
1336 {
1337 return err;
1338 }
1339
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001340 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1341}
1342
1343// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001344GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001345{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001346 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001347
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001348 if (err == GL_NO_ERROR)
1349 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001350 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001351 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001352 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1353
1354 mDevice->SetIndices(indexBuffer->getBuffer());
1355 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001356 }
1357 }
1358
1359 return err;
1360}
1361
1362void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1363{
1364 startScene();
1365
1366 if (mode == GL_LINE_LOOP)
1367 {
1368 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1369 }
1370 else if (instances > 0)
1371 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001372 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001373 if (countingIB)
1374 {
1375 if (mAppliedIBSerial != countingIB->getSerial())
1376 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001377 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1378
1379 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001380 mAppliedIBSerial = countingIB->getSerial();
1381 }
1382
1383 for (int i = 0; i < mRepeatDraw; i++)
1384 {
1385 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1386 }
1387 }
1388 else
1389 {
1390 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1391 return error(GL_OUT_OF_MEMORY);
1392 }
1393 }
1394 else // Regular case
1395 {
1396 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1397 }
1398}
1399
daniel@transgaming.com31240482012-11-28 21:06:41 +00001400void 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 +00001401{
1402 startScene();
1403
1404 if (mode == GL_LINE_LOOP)
1405 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001406 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001407 }
1408 else
1409 {
1410 for (int i = 0; i < mRepeatDraw; i++)
1411 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001412 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1413 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001414 }
1415 }
1416}
1417
1418void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1419{
1420 // Get the raw indices for an indexed draw
1421 if (type != GL_NONE && elementArrayBuffer)
1422 {
1423 gl::Buffer *indexBuffer = elementArrayBuffer;
1424 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1425 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1426 }
1427
1428 UINT startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001429
1430 if (get32BitIndexSupport())
1431 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001432 if (!mLineLoopIB)
1433 {
1434 mLineLoopIB = new StreamingIndexBufferInterface(this);
1435 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1436 {
1437 delete mLineLoopIB;
1438 mLineLoopIB = NULL;
1439
1440 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
1441 return error(GL_OUT_OF_MEMORY);
1442 }
1443 }
1444
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001445 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001446 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001447 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001448 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1449 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001450 }
1451
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001452 void* mappedMemory = NULL;
1453 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1454 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001455 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001456 ERR("Could not map index buffer for GL_LINE_LOOP.");
1457 return error(GL_OUT_OF_MEMORY);
1458 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001459
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001460 startIndex = static_cast<UINT>(offset) / 4;
1461 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1462
1463 switch (type)
1464 {
1465 case GL_NONE: // Non-indexed draw
1466 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001467 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001468 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001469 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001470 data[count] = 0;
1471 break;
1472 case GL_UNSIGNED_BYTE:
1473 for (int i = 0; i < count; i++)
1474 {
1475 data[i] = static_cast<const GLubyte*>(indices)[i];
1476 }
1477 data[count] = static_cast<const GLubyte*>(indices)[0];
1478 break;
1479 case GL_UNSIGNED_SHORT:
1480 for (int i = 0; i < count; i++)
1481 {
1482 data[i] = static_cast<const GLushort*>(indices)[i];
1483 }
1484 data[count] = static_cast<const GLushort*>(indices)[0];
1485 break;
1486 case GL_UNSIGNED_INT:
1487 for (int i = 0; i < count; i++)
1488 {
1489 data[i] = static_cast<const GLuint*>(indices)[i];
1490 }
1491 data[count] = static_cast<const GLuint*>(indices)[0];
1492 break;
1493 default: UNREACHABLE();
1494 }
1495
1496 if (!mLineLoopIB->unmapBuffer())
1497 {
1498 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1499 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001500 }
1501 }
1502 else
1503 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001504 if (!mLineLoopIB)
1505 {
1506 mLineLoopIB = new StreamingIndexBufferInterface(this);
1507 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1508 {
1509 delete mLineLoopIB;
1510 mLineLoopIB = NULL;
1511
1512 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
1513 return error(GL_OUT_OF_MEMORY);
1514 }
1515 }
1516
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001517 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001518 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001519 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001520 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1521 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001522 }
1523
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001524 void* mappedMemory = NULL;
1525 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1526 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001527 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001528 ERR("Could not map index buffer for GL_LINE_LOOP.");
1529 return error(GL_OUT_OF_MEMORY);
1530 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001531
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001532 startIndex = static_cast<UINT>(offset) / 2;
1533 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1534
1535 switch (type)
1536 {
1537 case GL_NONE: // Non-indexed draw
1538 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001539 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001540 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001541 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001542 data[count] = 0;
1543 break;
1544 case GL_UNSIGNED_BYTE:
1545 for (int i = 0; i < count; i++)
1546 {
1547 data[i] = static_cast<const GLubyte*>(indices)[i];
1548 }
1549 data[count] = static_cast<const GLubyte*>(indices)[0];
1550 break;
1551 case GL_UNSIGNED_SHORT:
1552 for (int i = 0; i < count; i++)
1553 {
1554 data[i] = static_cast<const GLushort*>(indices)[i];
1555 }
1556 data[count] = static_cast<const GLushort*>(indices)[0];
1557 break;
1558 case GL_UNSIGNED_INT:
1559 for (int i = 0; i < count; i++)
1560 {
1561 data[i] = static_cast<const GLuint*>(indices)[i];
1562 }
1563 data[count] = static_cast<const GLuint*>(indices)[0];
1564 break;
1565 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001566 }
1567
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001568 if (!mLineLoopIB->unmapBuffer())
1569 {
1570 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1571 return error(GL_OUT_OF_MEMORY);
1572 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001573 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001574
1575 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001576 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001577 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1578
1579 mDevice->SetIndices(indexBuffer->getBuffer());
1580 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001581 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001582
1583 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001584}
1585
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001586void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1587{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001588 unsigned int programBinarySerial = programBinary->getSerial();
1589 if (programBinarySerial != mAppliedProgramBinarySerial)
1590 {
1591 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1592 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001593
daniel@transgaming.come4991412012-12-20 20:55:34 +00001594 IDirect3DVertexShader9 *vertexShader = NULL;
1595 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001596
daniel@transgaming.come4991412012-12-20 20:55:34 +00001597 IDirect3DPixelShader9 *pixelShader = NULL;
1598 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001599
daniel@transgaming.come4991412012-12-20 20:55:34 +00001600 mDevice->SetPixelShader(pixelShader);
1601 mDevice->SetVertexShader(vertexShader);
1602 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001603 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001604
1605 mAppliedProgramBinarySerial = programBinarySerial;
1606 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001607}
1608
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001609void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001610{
1611 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1612 {
1613 gl::Uniform *targetUniform = *ub;
1614
1615 if (targetUniform->dirty)
1616 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001617 int count = targetUniform->elementCount();
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001618 GLfloat *f = (GLfloat*)targetUniform->data;
1619 GLint *i = (GLint*)targetUniform->data;
1620 GLboolean *b = (GLboolean*)targetUniform->data;
1621
1622 switch (targetUniform->type)
1623 {
1624 case GL_SAMPLER_2D:
1625 case GL_SAMPLER_CUBE:
1626 break;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001627 case GL_BOOL: applyUniformnbv(targetUniform, count, 1, b); break;
1628 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, count, 2, b); break;
1629 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, count, 3, b); break;
1630 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, count, 4, b); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001631 case GL_FLOAT:
1632 case GL_FLOAT_VEC2:
1633 case GL_FLOAT_VEC3:
1634 case GL_FLOAT_VEC4:
1635 case GL_FLOAT_MAT2:
1636 case GL_FLOAT_MAT3:
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001637 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1638 case GL_INT: applyUniform1iv(targetUniform, count, i); break;
1639 case GL_INT_VEC2: applyUniform2iv(targetUniform, count, i); break;
1640 case GL_INT_VEC3: applyUniform3iv(targetUniform, count, i); break;
1641 case GL_INT_VEC4: applyUniform4iv(targetUniform, count, i); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001642 default:
1643 UNREACHABLE();
1644 }
1645
1646 targetUniform->dirty = false;
1647 }
1648 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001649
1650 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001651 if (mDxUniformsDirty)
1652 {
1653 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1654 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1655 mDxUniformsDirty = false;
1656 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001657}
1658
1659void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
1660{
1661 float vector[gl::D3D9_MAX_FLOAT_CONSTANTS * 4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001662
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001663 if (targetUniform->psRegisterIndex >= 0 || targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001664 {
1665 ASSERT(count <= gl::D3D9_MAX_FLOAT_CONSTANTS);
1666 for (int i = 0; i < count; i++)
1667 {
1668 for (int j = 0; j < 4; j++)
1669 {
1670 if (j < width)
1671 {
1672 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
1673 }
1674 else
1675 {
1676 vector[i * 4 + j] = 0.0f;
1677 }
1678 }
1679 }
1680 }
1681
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001682 applyUniformnfv(targetUniform, vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001683}
1684
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001685void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001686{
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001687 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001688 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001689 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001690 }
1691
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001692 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001693 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001694 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001695 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001696}
1697
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001698void Renderer9::applyUniform1iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001699{
1700 ASSERT(count <= gl::D3D9_MAX_FLOAT_CONSTANTS);
1701 gl::Vector4 vector[gl::D3D9_MAX_FLOAT_CONSTANTS];
1702
1703 for (int i = 0; i < count; i++)
1704 {
1705 vector[i] = gl::Vector4((float)v[i], 0, 0, 0);
1706 }
1707
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001708 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001709}
1710
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001711void Renderer9::applyUniform2iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001712{
1713 ASSERT(count <= gl::D3D9_MAX_FLOAT_CONSTANTS);
1714 gl::Vector4 vector[gl::D3D9_MAX_FLOAT_CONSTANTS];
1715
1716 for (int i = 0; i < count; i++)
1717 {
1718 vector[i] = gl::Vector4((float)v[0], (float)v[1], 0, 0);
1719
1720 v += 2;
1721 }
1722
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001723 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001724}
1725
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001726void Renderer9::applyUniform3iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001727{
1728 ASSERT(count <= gl::D3D9_MAX_FLOAT_CONSTANTS);
1729 gl::Vector4 vector[gl::D3D9_MAX_FLOAT_CONSTANTS];
1730
1731 for (int i = 0; i < count; i++)
1732 {
1733 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], 0);
1734
1735 v += 3;
1736 }
1737
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001738 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739}
1740
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001741void Renderer9::applyUniform4iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742{
1743 ASSERT(count <= gl::D3D9_MAX_FLOAT_CONSTANTS);
1744 gl::Vector4 vector[gl::D3D9_MAX_FLOAT_CONSTANTS];
1745
1746 for (int i = 0; i < count; i++)
1747 {
1748 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1749
1750 v += 4;
1751 }
1752
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001753 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001754}
1755
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001756void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001757{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001758 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1759 gl::unorm<8>(clearParams.colorClearValue.red),
1760 gl::unorm<8>(clearParams.colorClearValue.green),
1761 gl::unorm<8>(clearParams.colorClearValue.blue));
1762 float depth = gl::clamp01(clearParams.depthClearValue);
1763 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001764
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001765 unsigned int stencilUnmasked = 0x0;
1766 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1767 {
1768 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1769 stencilUnmasked = (0x1 << stencilSize) - 1;
1770 }
1771
1772 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1773
1774 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1775 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1776 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1777 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1778 clearParams.colorMaskBlue && alphaUnmasked);
1779
1780 if (needMaskedColorClear || needMaskedStencilClear)
1781 {
1782 // State which is altered in all paths from this point to the clear call is saved.
1783 // State which is altered in only some paths will be flagged dirty in the case that
1784 // that path is taken.
1785 HRESULT hr;
1786 if (mMaskedClearSavedState == NULL)
1787 {
1788 hr = mDevice->BeginStateBlock();
1789 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1790
1791 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1792 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1793 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1794 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1795 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1796 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1797 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1798 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1799 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1800 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1801 mDevice->SetPixelShader(NULL);
1802 mDevice->SetVertexShader(NULL);
1803 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1804 mDevice->SetStreamSource(0, NULL, 0, 0);
1805 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1806 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1807 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1808 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1809 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1810 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1811 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1812
1813 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1814 {
1815 mDevice->SetStreamSourceFreq(i, 1);
1816 }
1817
1818 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1819 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1820 }
1821
1822 ASSERT(mMaskedClearSavedState != NULL);
1823
1824 if (mMaskedClearSavedState != NULL)
1825 {
1826 hr = mMaskedClearSavedState->Capture();
1827 ASSERT(SUCCEEDED(hr));
1828 }
1829
1830 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1831 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1832 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1833 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1834 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1835 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1836 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1837 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1838
1839 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1840 {
1841 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1842 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1843 clearParams.colorMaskGreen,
1844 clearParams.colorMaskBlue,
1845 clearParams.colorMaskAlpha));
1846 }
1847 else
1848 {
1849 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1850 }
1851
1852 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1853 {
1854 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1855 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1856 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1857 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1858 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1859 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1860 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1861 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1862 }
1863 else
1864 {
1865 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1866 }
1867
1868 mDevice->SetPixelShader(NULL);
1869 mDevice->SetVertexShader(NULL);
1870 mDevice->SetFVF(D3DFVF_XYZRHW);
1871 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1872 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1873 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1874 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1875 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1876 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1877 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1878
1879 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1880 {
1881 mDevice->SetStreamSourceFreq(i, 1);
1882 }
1883
1884 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1885 quad[0][0] = -0.5f;
1886 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1887 quad[0][2] = 0.0f;
1888 quad[0][3] = 1.0f;
1889
1890 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1891 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1892 quad[1][2] = 0.0f;
1893 quad[1][3] = 1.0f;
1894
1895 quad[2][0] = -0.5f;
1896 quad[2][1] = -0.5f;
1897 quad[2][2] = 0.0f;
1898 quad[2][3] = 1.0f;
1899
1900 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1901 quad[3][1] = -0.5f;
1902 quad[3][2] = 0.0f;
1903 quad[3][3] = 1.0f;
1904
1905 startScene();
1906 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1907
1908 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1909 {
1910 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1911 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1912 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1913 }
1914
1915 if (mMaskedClearSavedState != NULL)
1916 {
1917 mMaskedClearSavedState->Apply();
1918 }
1919 }
1920 else if (clearParams.mask)
1921 {
1922 DWORD dxClearFlags = 0;
1923 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1924 {
1925 dxClearFlags |= D3DCLEAR_TARGET;
1926 }
1927 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1928 {
1929 dxClearFlags |= D3DCLEAR_ZBUFFER;
1930 }
1931 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1932 {
1933 dxClearFlags |= D3DCLEAR_STENCIL;
1934 }
1935
1936 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1937 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001938}
1939
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001940void Renderer9::markAllStateDirty()
1941{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001942 mAppliedRenderTargetSerial = 0;
1943 mAppliedDepthbufferSerial = 0;
1944 mAppliedStencilbufferSerial = 0;
1945 mDepthStencilInitialized = false;
1946 mRenderTargetDescInitialized = false;
1947
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001948 mForceSetDepthStencilState = true;
1949 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001950 mForceSetScissor = true;
1951 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001952 mForceSetBlendState = true;
1953
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001954 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001955 {
1956 mForceSetVertexSamplerStates[i] = true;
1957 mCurVertexTextureSerials[i] = 0;
1958 }
1959 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1960 {
1961 mForceSetPixelSamplerStates[i] = true;
1962 mCurPixelTextureSerials[i] = 0;
1963 }
1964
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001965 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001966 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001967 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001968
1969 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001970}
1971
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001972void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001973{
1974 while (!mEventQueryPool.empty())
1975 {
1976 mEventQueryPool.back()->Release();
1977 mEventQueryPool.pop_back();
1978 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001979
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001980 if (mMaskedClearSavedState)
1981 {
1982 mMaskedClearSavedState->Release();
1983 mMaskedClearSavedState = NULL;
1984 }
1985
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001986 mVertexShaderCache.clear();
1987 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001988
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001989 delete mBlit;
1990 mBlit = NULL;
1991
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001992 delete mVertexDataManager;
1993 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001994
1995 delete mIndexDataManager;
1996 mIndexDataManager = NULL;
1997
1998 delete mLineLoopIB;
1999 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002000
2001 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2002 {
2003 delete mNullColorbufferCache[i].buffer;
2004 mNullColorbufferCache[i].buffer = NULL;
2005 }
2006
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002007}
2008
2009
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002010void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002011{
2012 mDeviceLost = true;
2013}
2014
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002015bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002016{
2017 return mDeviceLost;
2018}
2019
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002020// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002021bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002022{
2023 bool isLost = false;
2024
2025 if (mDeviceEx)
2026 {
2027 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
2028 }
2029 else if (mDevice)
2030 {
2031 isLost = FAILED(mDevice->TestCooperativeLevel());
2032 }
2033 else
2034 {
2035 // No device yet, so no reset required
2036 }
2037
2038 if (isLost)
2039 {
2040 // ensure we note the device loss --
2041 // we'll probably get this done again by markDeviceLost
2042 // but best to remember it!
2043 // Note that we don't want to clear the device loss status here
2044 // -- this needs to be done by resetDevice
2045 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002046 if (notify)
2047 {
2048 mDisplay->notifyDeviceLost();
2049 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002050 }
2051
2052 return isLost;
2053}
2054
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002055bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002056{
2057 HRESULT status = D3D_OK;
2058
2059 if (mDeviceEx)
2060 {
2061 status = mDeviceEx->CheckDeviceState(NULL);
2062 }
2063 else if (mDevice)
2064 {
2065 status = mDevice->TestCooperativeLevel();
2066 }
2067
2068 switch (status)
2069 {
2070 case D3DERR_DEVICENOTRESET:
2071 case D3DERR_DEVICEHUNG:
2072 return true;
2073 default:
2074 return false;
2075 }
2076}
2077
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002078bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002079{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002080 releaseDeviceResources();
2081
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002082 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2083
2084 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002085 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002086 int attempts = 3;
2087
2088 while (lost && attempts > 0)
2089 {
2090 if (mDeviceEx)
2091 {
2092 Sleep(500); // Give the graphics driver some CPU time
2093 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2094 }
2095 else
2096 {
2097 result = mDevice->TestCooperativeLevel();
2098 while (result == D3DERR_DEVICELOST)
2099 {
2100 Sleep(100); // Give the graphics driver some CPU time
2101 result = mDevice->TestCooperativeLevel();
2102 }
2103
2104 if (result == D3DERR_DEVICENOTRESET)
2105 {
2106 result = mDevice->Reset(&presentParameters);
2107 }
2108 }
2109
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002110 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002111 attempts --;
2112 }
2113
2114 if (FAILED(result))
2115 {
2116 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2117 return false;
2118 }
2119
2120 // reset device defaults
2121 initializeDevice();
2122 mDeviceLost = false;
2123
2124 return true;
2125}
2126
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002127DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002128{
2129 return mAdapterIdentifier.VendorId;
2130}
2131
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002132std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002133{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002134 std::ostringstream rendererString;
2135
2136 rendererString << mAdapterIdentifier.Description;
2137 if (getShareHandleSupport())
2138 {
2139 rendererString << " Direct3D9Ex";
2140 }
2141 else
2142 {
2143 rendererString << " Direct3D9";
2144 }
2145
2146 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2147 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2148
2149 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002150}
2151
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002152GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002153{
2154 return mAdapterIdentifier.DeviceIdentifier;
2155}
2156
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002157void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002158{
2159 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
2160 {
2161 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
2162 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2163
2164 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
2165 }
2166}
2167
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002168bool Renderer9::getBGRATextureSupport() const
2169{
2170 // DirectX 9 always supports BGRA
2171 return true;
2172}
2173
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002174bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002175{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002176 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002177}
2178
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002179bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002180{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002181 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002182}
2183
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002184bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002185{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002186 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002187}
2188
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002189bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002190{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002191 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002192}
2193
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002194bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002195{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002196 *filtering = mFloat32FilterSupport;
2197 *renderable = mFloat32RenderSupport;
2198 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002199}
2200
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002201bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002202{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002203 *filtering = mFloat16FilterSupport;
2204 *renderable = mFloat16RenderSupport;
2205 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002206}
2207
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002208bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002209{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002210 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002211}
2212
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002213bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002214{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002215 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002216}
2217
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002218bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002219{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002220 return mSupportsTextureFilterAnisotropy;
2221}
2222
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002223float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002224{
2225 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002226 {
2227 return mDeviceCaps.MaxAnisotropy;
2228 }
2229 return 1.0f;
2230}
2231
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002232bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002233{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002234 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002235}
2236
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002237unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002238{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002239 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2240 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002241}
2242
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002243int Renderer9::getMaxVertexUniformVectors() const
2244{
2245 return gl::MAX_VERTEX_UNIFORM_VECTORS;
2246}
2247
2248int Renderer9::getMaxFragmentUniformVectors() const
2249{
2250 return getMajorShaderModel() >= 3 ? gl::MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : gl::MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2251}
2252
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002253bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002254{
2255 return mSupportsNonPower2Textures;
2256}
2257
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002258bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002259{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002260 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002261}
2262
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002263bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002264{
2265 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2266}
2267
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002268bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002269{
2270 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002271 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002272}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002273
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002274bool Renderer9::getDerivativeInstructionSupport() const
2275{
2276 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2277}
2278
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002279int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002280{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002281 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002282}
2283
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002284float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002285{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002286 // Point size clamped at 1.0f for SM2
2287 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002288}
2289
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002290int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002291{
2292 return (int)mDeviceCaps.MaxTextureWidth;
2293}
2294
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002295int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002296{
2297 return (int)mDeviceCaps.MaxTextureHeight;
2298}
2299
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002300bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002301{
2302 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2303}
2304
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002305DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002306{
2307 return mDeviceCaps.DeclTypes;
2308}
2309
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002310int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002311{
2312 return mMinSwapInterval;
2313}
2314
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002315int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002316{
2317 return mMaxSwapInterval;
2318}
2319
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002320int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002321{
2322 return mMaxSupportedSamples;
2323}
2324
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002325int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002326{
2327 if (requested == 0)
2328 {
2329 return requested;
2330 }
2331
2332 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2333 if (itr == mMultiSampleSupport.end())
2334 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002335 if (format == D3DFMT_UNKNOWN)
2336 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002337 return -1;
2338 }
2339
2340 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2341 {
2342 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2343 {
2344 return i;
2345 }
2346 }
2347
2348 return -1;
2349}
2350
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002351D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2352{
2353 switch (internalformat)
2354 {
2355 case GL_DEPTH_COMPONENT16:
2356 case GL_DEPTH_COMPONENT32_OES:
2357 case GL_DEPTH24_STENCIL8_OES:
2358 return D3DFMT_INTZ;
2359 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2360 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2361 return D3DFMT_DXT1;
2362 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2363 return D3DFMT_DXT3;
2364 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2365 return D3DFMT_DXT5;
2366 case GL_RGBA32F_EXT:
2367 case GL_RGB32F_EXT:
2368 case GL_ALPHA32F_EXT:
2369 case GL_LUMINANCE32F_EXT:
2370 case GL_LUMINANCE_ALPHA32F_EXT:
2371 return D3DFMT_A32B32G32R32F;
2372 case GL_RGBA16F_EXT:
2373 case GL_RGB16F_EXT:
2374 case GL_ALPHA16F_EXT:
2375 case GL_LUMINANCE16F_EXT:
2376 case GL_LUMINANCE_ALPHA16F_EXT:
2377 return D3DFMT_A16B16G16R16F;
2378 case GL_LUMINANCE8_EXT:
2379 if (getLuminanceTextureSupport())
2380 {
2381 return D3DFMT_L8;
2382 }
2383 break;
2384 case GL_LUMINANCE8_ALPHA8_EXT:
2385 if (getLuminanceAlphaTextureSupport())
2386 {
2387 return D3DFMT_A8L8;
2388 }
2389 break;
2390 case GL_RGB8_OES:
2391 case GL_RGB565:
2392 return D3DFMT_X8R8G8B8;
2393 }
2394
2395 return D3DFMT_A8R8G8B8;
2396}
2397
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002398bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002399{
2400 bool result = false;
2401
2402 if (source && dest)
2403 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002404 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2405 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002406
2407 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002408 for (int i = 0; i < levels; ++i)
2409 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002410 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2411 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002412
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002413 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002414
2415 if (srcSurf) srcSurf->Release();
2416 if (dstSurf) dstSurf->Release();
2417
2418 if (!result)
2419 return false;
2420 }
2421 }
2422
2423 return result;
2424}
2425
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002426bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002427{
2428 bool result = false;
2429
2430 if (source && dest)
2431 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002432 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2433 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002434 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002435 for (int f = 0; f < 6; f++)
2436 {
2437 for (int i = 0; i < levels; i++)
2438 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002439 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2440 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002441
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002442 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002443
2444 if (srcSurf) srcSurf->Release();
2445 if (dstSurf) dstSurf->Release();
2446
2447 if (!result)
2448 return false;
2449 }
2450 }
2451 }
2452
2453 return result;
2454}
2455
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002456D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002457{
2458 if (mD3d9Ex != NULL)
2459 {
2460 return D3DPOOL_DEFAULT;
2461 }
2462 else
2463 {
2464 if (!(usage & D3DUSAGE_DYNAMIC))
2465 {
2466 return D3DPOOL_MANAGED;
2467 }
2468 }
2469
2470 return D3DPOOL_DEFAULT;
2471}
2472
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002473bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002474 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002475{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002476 RECT rect;
2477 rect.left = sourceRect.x;
2478 rect.top = sourceRect.y;
2479 rect.right = sourceRect.x + sourceRect.width;
2480 rect.bottom = sourceRect.y + sourceRect.height;
2481
2482 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002483}
2484
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002485bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002486 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002487{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002488 RECT rect;
2489 rect.left = sourceRect.x;
2490 rect.top = sourceRect.y;
2491 rect.right = sourceRect.x + sourceRect.width;
2492 rect.bottom = sourceRect.y + sourceRect.height;
2493
2494 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002495}
2496
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002497bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2498 bool blitRenderTarget, bool blitDepthStencil)
2499{
2500 endScene();
2501
2502 if (blitRenderTarget)
2503 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002504 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2505 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2506 RenderTarget9 *readRenderTarget = NULL;
2507 RenderTarget9 *drawRenderTarget = NULL;
2508 IDirect3DSurface9* readSurface = NULL;
2509 IDirect3DSurface9* drawSurface = NULL;
2510
2511 if (readBuffer)
2512 {
2513 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2514 }
2515 if (drawBuffer)
2516 {
2517 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2518 }
2519
2520 if (readRenderTarget)
2521 {
2522 readSurface = readRenderTarget->getSurface();
2523 }
2524 if (drawRenderTarget)
2525 {
2526 drawSurface = drawRenderTarget->getSurface();
2527 }
2528
2529 if (!readSurface || !drawSurface)
2530 {
2531 ERR("Failed to retrieve the render target.");
2532 return error(GL_OUT_OF_MEMORY, false);
2533 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002534
2535 RECT srcRect, dstRect;
2536 RECT *srcRectPtr = NULL;
2537 RECT *dstRectPtr = NULL;
2538
2539 if (readRect)
2540 {
2541 srcRect.left = readRect->x;
2542 srcRect.right = readRect->x + readRect->width;
2543 srcRect.top = readRect->y;
2544 srcRect.bottom = readRect->y + readRect->height;
2545 srcRectPtr = &srcRect;
2546 }
2547
2548 if (drawRect)
2549 {
2550 dstRect.left = drawRect->x;
2551 dstRect.right = drawRect->x + drawRect->width;
2552 dstRect.top = drawRect->y;
2553 dstRect.bottom = drawRect->y + drawRect->height;
2554 dstRectPtr = &dstRect;
2555 }
2556
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002557 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002558
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002559 readSurface->Release();
2560 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002561
2562 if (FAILED(result))
2563 {
2564 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2565 return false;
2566 }
2567 }
2568
2569 if (blitDepthStencil)
2570 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002571 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2572 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2573 RenderTarget9 *readDepthStencil = NULL;
2574 RenderTarget9 *drawDepthStencil = NULL;
2575 IDirect3DSurface9* readSurface = NULL;
2576 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002577
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002578 if (readBuffer)
2579 {
2580 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2581 }
2582 if (drawBuffer)
2583 {
2584 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2585 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002586
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002587 if (readDepthStencil)
2588 {
2589 readSurface = readDepthStencil->getSurface();
2590 }
2591 if (drawDepthStencil)
2592 {
2593 drawSurface = drawDepthStencil->getSurface();
2594 }
2595
2596 if (!readSurface || !drawSurface)
2597 {
2598 ERR("Failed to retrieve the render target.");
2599 return error(GL_OUT_OF_MEMORY, false);
2600 }
2601
2602 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2603
2604 readSurface->Release();
2605 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002606
2607 if (FAILED(result))
2608 {
2609 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2610 return false;
2611 }
2612 }
2613
2614 return true;
2615}
2616
2617void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2618 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2619{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002620 RenderTarget9 *renderTarget = NULL;
2621 IDirect3DSurface9 *surface = NULL;
2622 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2623
2624 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002625 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002626 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2627 }
2628
2629 if (renderTarget)
2630 {
2631 surface = renderTarget->getSurface();
2632 }
2633
2634 if (!surface)
2635 {
2636 // context must be lost
2637 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002638 }
2639
2640 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002641 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002642
2643 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2644 {
2645 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002646 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002647 return error(GL_OUT_OF_MEMORY);
2648 }
2649
2650 HRESULT result;
2651 IDirect3DSurface9 *systemSurface = NULL;
2652 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2653 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2654 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2655 if (directToPixels)
2656 {
2657 // Use the pixels ptr as a shared handle to write directly into client's memory
2658 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2659 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2660 if (FAILED(result))
2661 {
2662 // Try again without the shared handle
2663 directToPixels = false;
2664 }
2665 }
2666
2667 if (!directToPixels)
2668 {
2669 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2670 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2671 if (FAILED(result))
2672 {
2673 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002674 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002675 return error(GL_OUT_OF_MEMORY);
2676 }
2677 }
2678
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002679 result = mDevice->GetRenderTargetData(surface, systemSurface);
2680 surface->Release();
2681 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002682
2683 if (FAILED(result))
2684 {
2685 systemSurface->Release();
2686
2687 // It turns out that D3D will sometimes produce more error
2688 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002689 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002690 return error(GL_OUT_OF_MEMORY);
2691 else
2692 {
2693 UNREACHABLE();
2694 return;
2695 }
2696
2697 }
2698
2699 if (directToPixels)
2700 {
2701 systemSurface->Release();
2702 return;
2703 }
2704
2705 RECT rect;
2706 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2707 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2708 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2709 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2710
2711 D3DLOCKED_RECT lock;
2712 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2713
2714 if (FAILED(result))
2715 {
2716 UNREACHABLE();
2717 systemSurface->Release();
2718
2719 return; // No sensible error to generate
2720 }
2721
2722 unsigned char *dest = (unsigned char*)pixels;
2723 unsigned short *dest16 = (unsigned short*)pixels;
2724
2725 unsigned char *source;
2726 int inputPitch;
2727 if (packReverseRowOrder)
2728 {
2729 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2730 inputPitch = -lock.Pitch;
2731 }
2732 else
2733 {
2734 source = (unsigned char*)lock.pBits;
2735 inputPitch = lock.Pitch;
2736 }
2737
2738 unsigned int fastPixelSize = 0;
2739
2740 if (desc.Format == D3DFMT_A8R8G8B8 &&
2741 format == GL_BGRA_EXT &&
2742 type == GL_UNSIGNED_BYTE)
2743 {
2744 fastPixelSize = 4;
2745 }
2746 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2747 format == GL_BGRA_EXT &&
2748 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2749 (desc.Format == D3DFMT_A1R5G5B5 &&
2750 format == GL_BGRA_EXT &&
2751 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2752 {
2753 fastPixelSize = 2;
2754 }
2755 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2756 format == GL_RGBA &&
2757 type == GL_HALF_FLOAT_OES)
2758 {
2759 fastPixelSize = 8;
2760 }
2761 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2762 format == GL_RGBA &&
2763 type == GL_FLOAT)
2764 {
2765 fastPixelSize = 16;
2766 }
2767
2768 for (int j = 0; j < rect.bottom - rect.top; j++)
2769 {
2770 if (fastPixelSize != 0)
2771 {
2772 // Fast path for formats which require no translation:
2773 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2774 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2775 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2776 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2777 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2778 //
2779 // Note that buffers with no alpha go through the slow path below.
2780 memcpy(dest + j * outputPitch,
2781 source + j * inputPitch,
2782 (rect.right - rect.left) * fastPixelSize);
2783 continue;
2784 }
2785
2786 for (int i = 0; i < rect.right - rect.left; i++)
2787 {
2788 float r;
2789 float g;
2790 float b;
2791 float a;
2792
2793 switch (desc.Format)
2794 {
2795 case D3DFMT_R5G6B5:
2796 {
2797 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2798
2799 a = 1.0f;
2800 b = (rgb & 0x001F) * (1.0f / 0x001F);
2801 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2802 r = (rgb & 0xF800) * (1.0f / 0xF800);
2803 }
2804 break;
2805 case D3DFMT_A1R5G5B5:
2806 {
2807 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2808
2809 a = (argb & 0x8000) ? 1.0f : 0.0f;
2810 b = (argb & 0x001F) * (1.0f / 0x001F);
2811 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2812 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2813 }
2814 break;
2815 case D3DFMT_A8R8G8B8:
2816 {
2817 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2818
2819 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2820 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2821 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2822 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2823 }
2824 break;
2825 case D3DFMT_X8R8G8B8:
2826 {
2827 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2828
2829 a = 1.0f;
2830 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2831 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2832 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2833 }
2834 break;
2835 case D3DFMT_A2R10G10B10:
2836 {
2837 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2838
2839 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2840 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2841 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2842 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2843 }
2844 break;
2845 case D3DFMT_A32B32G32R32F:
2846 {
2847 // float formats in D3D are stored rgba, rather than the other way round
2848 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2849 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2850 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2851 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2852 }
2853 break;
2854 case D3DFMT_A16B16G16R16F:
2855 {
2856 // float formats in D3D are stored rgba, rather than the other way round
2857 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2858 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2859 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2860 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2861 }
2862 break;
2863 default:
2864 UNIMPLEMENTED(); // FIXME
2865 UNREACHABLE();
2866 return;
2867 }
2868
2869 switch (format)
2870 {
2871 case GL_RGBA:
2872 switch (type)
2873 {
2874 case GL_UNSIGNED_BYTE:
2875 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2876 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2877 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2878 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2879 break;
2880 default: UNREACHABLE();
2881 }
2882 break;
2883 case GL_BGRA_EXT:
2884 switch (type)
2885 {
2886 case GL_UNSIGNED_BYTE:
2887 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2888 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2889 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2890 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2891 break;
2892 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2893 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2894 // this type is packed as follows:
2895 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2896 // --------------------------------------------------------------------------------
2897 // | 4th | 3rd | 2nd | 1st component |
2898 // --------------------------------------------------------------------------------
2899 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2900 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2901 ((unsigned short)(15 * a + 0.5f) << 12)|
2902 ((unsigned short)(15 * r + 0.5f) << 8) |
2903 ((unsigned short)(15 * g + 0.5f) << 4) |
2904 ((unsigned short)(15 * b + 0.5f) << 0);
2905 break;
2906 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2907 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2908 // this type is packed as follows:
2909 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2910 // --------------------------------------------------------------------------------
2911 // | 4th | 3rd | 2nd | 1st component |
2912 // --------------------------------------------------------------------------------
2913 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2914 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2915 ((unsigned short)( a + 0.5f) << 15) |
2916 ((unsigned short)(31 * r + 0.5f) << 10) |
2917 ((unsigned short)(31 * g + 0.5f) << 5) |
2918 ((unsigned short)(31 * b + 0.5f) << 0);
2919 break;
2920 default: UNREACHABLE();
2921 }
2922 break;
2923 case GL_RGB:
2924 switch (type)
2925 {
2926 case GL_UNSIGNED_SHORT_5_6_5:
2927 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2928 ((unsigned short)(31 * b + 0.5f) << 0) |
2929 ((unsigned short)(63 * g + 0.5f) << 5) |
2930 ((unsigned short)(31 * r + 0.5f) << 11);
2931 break;
2932 case GL_UNSIGNED_BYTE:
2933 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2934 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2935 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2936 break;
2937 default: UNREACHABLE();
2938 }
2939 break;
2940 default: UNREACHABLE();
2941 }
2942 }
2943 }
2944
2945 systemSurface->UnlockRect();
2946
2947 systemSurface->Release();
2948}
2949
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002950RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2951{
2952 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2953 IDirect3DSurface9 *surface = NULL;
2954 if (depth)
2955 {
2956 surface = swapChain9->getDepthStencil();
2957 }
2958 else
2959 {
2960 surface = swapChain9->getRenderTarget();
2961 }
2962
2963 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2964
2965 return renderTarget;
2966}
2967
2968RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2969{
2970 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2971 return renderTarget;
2972}
2973
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002974ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, GLenum type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002975{
2976 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002977
2978 switch (type)
2979 {
2980 case GL_VERTEX_SHADER:
2981 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002982 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002983 if (vshader)
2984 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002985 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002986 }
2987 }
2988 break;
2989 case GL_FRAGMENT_SHADER:
2990 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002991 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002992 if (pshader)
2993 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002994 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002995 }
2996 }
2997 break;
2998 default:
2999 UNREACHABLE();
3000 break;
3001 }
3002
3003 return executable;
3004}
3005
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003006ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type)
3007{
3008 const char *profile = NULL;
3009
3010 switch (type)
3011 {
3012 case GL_VERTEX_SHADER:
3013 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3014 break;
3015 case GL_FRAGMENT_SHADER:
3016 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3017 break;
3018 default:
3019 UNREACHABLE();
3020 return NULL;
3021 }
3022
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00003023 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003024 if (!binary)
3025 return NULL;
3026
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003027 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003028 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003029
3030 return executable;
3031}
3032
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003033bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3034{
3035 return mBlit->boxFilter(source, dest);
3036}
3037
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003038D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003039{
3040 if (mD3d9Ex != NULL)
3041 {
3042 return D3DPOOL_DEFAULT;
3043 }
3044 else
3045 {
3046 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3047 {
3048 return D3DPOOL_MANAGED;
3049 }
3050 }
3051
3052 return D3DPOOL_DEFAULT;
3053}
3054
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003055bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3056{
3057 if (source && dest)
3058 {
3059 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003060
3061 if (fromManaged)
3062 {
3063 D3DSURFACE_DESC desc;
3064 source->GetDesc(&desc);
3065
3066 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003067 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003068
3069 if (SUCCEEDED(result))
3070 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003071 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003072 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003073 surf->Release();
3074 }
3075 }
3076 else
3077 {
3078 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003079 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003080 }
3081
3082 if (FAILED(result))
3083 {
3084 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3085 return false;
3086 }
3087 }
3088
3089 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003090}
3091
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003092Image *Renderer9::createImage()
3093{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003094 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003095}
3096
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003097void Renderer9::generateMipmap(Image *dest, Image *src)
3098{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003099 Image9 *src9 = Image9::makeImage9(src);
3100 Image9 *dst9 = Image9::makeImage9(dest);
3101 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003102}
3103
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003104TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3105{
3106 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3107 return new TextureStorage9_2D(this, swapChain9);
3108}
3109
3110TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3111{
3112 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3113}
3114
3115TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3116{
3117 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3118}
3119
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003120}