blob: 2e7d3eca4e0e440c76454a31ec7e9cc5d4d10bfd [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"
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +000029#include "libGLESv2/renderer/Query9.h"
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +000030#include "libGLESv2/renderer/Fence9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000031
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000032#include <sstream>
33
daniel@transgaming.com621ce052012-10-31 17:52:29 +000034// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
35#define REF_RAST 0
36
37// The "Debug This Pixel..." feature in PIX often fails when using the
38// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
39// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
40#if !defined(ANGLE_ENABLE_D3D9EX)
41// Enables use of the IDirect3D9Ex interface, when available
42#define ANGLE_ENABLE_D3D9EX 1
43#endif // !defined(ANGLE_ENABLE_D3D9EX)
44
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000045namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000046{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000047static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000048 {
49 D3DFMT_A1R5G5B5,
50 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
51 D3DFMT_A8R8G8B8,
52 D3DFMT_R5G6B5,
53 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
54 D3DFMT_X8R8G8B8
55 };
56
daniel@transgaming.com222ee082012-11-28 19:31:49 +000057static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000058 {
59 D3DFMT_UNKNOWN,
60 // D3DFMT_D16_LOCKABLE,
61 D3DFMT_D32,
62 // D3DFMT_D15S1,
63 D3DFMT_D24S8,
64 D3DFMT_D24X8,
65 // D3DFMT_D24X4S4,
66 D3DFMT_D16,
67 // D3DFMT_D32F_LOCKABLE,
68 // D3DFMT_D24FS8
69 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000070
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000071enum
72{
73 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
74};
75
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000076Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000077{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000078 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000079
80 mD3d9 = NULL;
81 mD3d9Ex = NULL;
82 mDevice = NULL;
83 mDeviceEx = NULL;
84 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000085 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000086
87 mAdapter = D3DADAPTER_DEFAULT;
88
89 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
90 mDeviceType = D3DDEVTYPE_REF;
91 #else
92 mDeviceType = D3DDEVTYPE_HAL;
93 #endif
94
95 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000096
97 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000098
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000099 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000100
101 mVertexDataManager = NULL;
102 mIndexDataManager = NULL;
103 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000104
105 mMaxNullColorbufferLRU = 0;
106 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
107 {
108 mNullColorbufferCache[i].lruCount = 0;
109 mNullColorbufferCache[i].width = 0;
110 mNullColorbufferCache[i].height = 0;
111 mNullColorbufferCache[i].buffer = NULL;
112 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000113}
114
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000115Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000116{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000117 releaseDeviceResources();
118
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000119 if (mDevice)
120 {
121 // 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 +0000122 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000123 {
124 resetDevice();
125 }
126
127 mDevice->Release();
128 mDevice = NULL;
129 }
130
131 if (mDeviceEx)
132 {
133 mDeviceEx->Release();
134 mDeviceEx = NULL;
135 }
136
137 if (mD3d9)
138 {
139 mD3d9->Release();
140 mD3d9 = NULL;
141 }
142
143 if (mDeviceWindow)
144 {
145 DestroyWindow(mDeviceWindow);
146 mDeviceWindow = NULL;
147 }
148
149 if (mD3d9Ex)
150 {
151 mD3d9Ex->Release();
152 mD3d9Ex = NULL;
153 }
154
155 if (mD3d9Module)
156 {
157 mD3d9Module = NULL;
158 }
159
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000160 while (!mMultiSampleSupport.empty())
161 {
162 delete [] mMultiSampleSupport.begin()->second;
163 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
164 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000165}
166
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000167Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
168{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000169 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000170 return static_cast<rx::Renderer9*>(renderer);
171}
172
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000173EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000174{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000175 if (!initializeCompiler())
176 {
177 return EGL_NOT_INITIALIZED;
178 }
179
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000180 if (mSoftwareDevice)
181 {
182 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
183 }
184 else
185 {
186 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
187 }
188
189 if (mD3d9Module == NULL)
190 {
191 ERR("No D3D9 module found - aborting!\n");
192 return EGL_NOT_INITIALIZED;
193 }
194
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000195 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
196 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
197
198 // Use Direct3D9Ex if available. Among other things, this version is less
199 // inclined to report a lost context, for example when the user switches
200 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
201 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
202 {
203 ASSERT(mD3d9Ex);
204 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
205 ASSERT(mD3d9);
206 }
207 else
208 {
209 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
210 }
211
212 if (!mD3d9)
213 {
214 ERR("Could not create D3D9 device - aborting!\n");
215 return EGL_NOT_INITIALIZED;
216 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000217
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000218 if (mDc != NULL)
219 {
220 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
221 }
222
223 HRESULT result;
224
225 // Give up on getting device caps after about one second.
226 for (int i = 0; i < 10; ++i)
227 {
228 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
229 if (SUCCEEDED(result))
230 {
231 break;
232 }
233 else if (result == D3DERR_NOTAVAILABLE)
234 {
235 Sleep(100); // Give the driver some time to initialize/recover
236 }
237 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
238 {
239 ERR("failed to get device caps (0x%x)\n", result);
240 return EGL_NOT_INITIALIZED;
241 }
242 }
243
244 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
245 {
246 ERR("Renderer does not support PS 2.0. aborting!\n");
247 return EGL_NOT_INITIALIZED;
248 }
249
250 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
251 // This is required by Texture2D::convertToRenderTarget.
252 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
253 {
254 ERR("Renderer does not support stretctrect from textures!\n");
255 return EGL_NOT_INITIALIZED;
256 }
257
258 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
259
260 // ATI cards on XP have problems with non-power-of-two textures.
261 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
262 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
263 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
264 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
265
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000266 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
267 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
268
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000269 mMinSwapInterval = 4;
270 mMaxSwapInterval = 0;
271
272 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
273 {
274 mMinSwapInterval = std::min(mMinSwapInterval, 0);
275 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
276 }
277 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
278 {
279 mMinSwapInterval = std::min(mMinSwapInterval, 1);
280 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
281 }
282 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
283 {
284 mMinSwapInterval = std::min(mMinSwapInterval, 2);
285 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
286 }
287 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
288 {
289 mMinSwapInterval = std::min(mMinSwapInterval, 3);
290 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
291 }
292 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
293 {
294 mMinSwapInterval = std::min(mMinSwapInterval, 4);
295 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
296 }
297
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000298 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000299 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000300 {
301 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000302 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
303 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000304
305 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
306 {
307 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
308 {
309 max = j;
310 }
311 }
312 }
313
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000314 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000315 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000316 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000317 continue;
318
319 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000320 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
321 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000322
323 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
324 {
325 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
326 {
327 max = j;
328 }
329 }
330 }
331
332 mMaxSupportedSamples = max;
333
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000334 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
335 static const TCHAR className[] = TEXT("STATIC");
336
337 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
338
339 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
340 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
341
342 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
343 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
344 {
345 return EGL_BAD_ALLOC;
346 }
347
348 if (FAILED(result))
349 {
350 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
351
352 if (FAILED(result))
353 {
354 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
355 return EGL_BAD_ALLOC;
356 }
357 }
358
359 if (mD3d9Ex)
360 {
361 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
362 ASSERT(SUCCEEDED(result));
363 }
364
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000365 mVertexShaderCache.initialize(mDevice);
366 mPixelShaderCache.initialize(mDevice);
367
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000368 // Check occlusion query support
369 IDirect3DQuery9 *occlusionQuery = NULL;
370 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
371 {
372 occlusionQuery->Release();
373 mOcclusionQuerySupport = true;
374 }
375 else
376 {
377 mOcclusionQuerySupport = false;
378 }
379
380 // Check event query support
381 IDirect3DQuery9 *eventQuery = NULL;
382 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
383 {
384 eventQuery->Release();
385 mEventQuerySupport = true;
386 }
387 else
388 {
389 mEventQuerySupport = false;
390 }
391
392 D3DDISPLAYMODE currentDisplayMode;
393 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
394
395 // Check vertex texture support
396 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
397 // We test this using D3D9 by checking support for the R16F format.
398 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
399 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
400 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
401
402 // Check depth texture support
403 // we use INTZ for depth textures in Direct3D9
404 // we also want NULL texture support to ensure the we can make depth-only FBOs
405 // see http://aras-p.info/texts/D3D9GPUHacks.html
406 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
407 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
408 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
409 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
410
411 // Check 32 bit floating point texture support
412 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
413 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
414 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
415 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
416
417 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
418 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
419 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
420 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
421
422 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
423 {
424 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
425 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
426 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
427 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
428 }
429 else
430 {
431 mFloat32TextureSupport = true;
432 }
433
434 // Check 16 bit floating point texture support
435 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
436 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
437 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
438 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
439
440 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
441 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
442 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
443 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
444
445 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
446 {
447 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
448 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
449 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
450 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
451 }
452 else
453 {
454 mFloat16TextureSupport = true;
455 }
456
457 // Check DXT texture support
458 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
459 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
460 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
461
462 // Check luminance[alpha] texture support
463 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
464 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
465
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000466 initializeDevice();
467
468 return EGL_SUCCESS;
469}
470
471// do any one-time device initialization
472// NOTE: this is also needed after a device lost/reset
473// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000474void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000475{
476 // Permanent non-default states
477 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
478 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
479
480 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
481 {
482 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
483 }
484 else
485 {
486 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
487 }
488
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000489 markAllStateDirty();
490
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000491 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000492
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000493 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000494 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000495 mVertexDataManager = new rx::VertexDataManager(this);
496 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000497}
498
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000499D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000500{
501 D3DPRESENT_PARAMETERS presentParameters = {0};
502
503 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
504 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
505 presentParameters.BackBufferCount = 1;
506 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
507 presentParameters.BackBufferWidth = 1;
508 presentParameters.BackBufferHeight = 1;
509 presentParameters.EnableAutoDepthStencil = FALSE;
510 presentParameters.Flags = 0;
511 presentParameters.hDeviceWindow = mDeviceWindow;
512 presentParameters.MultiSampleQuality = 0;
513 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
514 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
515 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
516 presentParameters.Windowed = TRUE;
517
518 return presentParameters;
519}
520
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000521int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000522{
523 D3DDISPLAYMODE currentDisplayMode;
524 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
525
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000526 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
527 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000528 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
529 int numConfigs = 0;
530
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000531 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000532 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000533 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000534
535 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
536
537 if (SUCCEEDED(result))
538 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000539 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000540 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000541 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000542 HRESULT result = D3D_OK;
543
544 if(depthStencilFormat != D3DFMT_UNKNOWN)
545 {
546 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
547 }
548
549 if (SUCCEEDED(result))
550 {
551 if(depthStencilFormat != D3DFMT_UNKNOWN)
552 {
553 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
554 }
555
556 if (SUCCEEDED(result))
557 {
558 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000559 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
560 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000561 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
562 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
563
564 (*configDescList)[numConfigs++] = newConfig;
565 }
566 }
567 }
568 }
569 }
570
571 return numConfigs;
572}
573
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000574void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000575{
576 delete [] (configDescList);
577}
578
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000579void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000580{
581 if (!mSceneStarted)
582 {
583 long result = mDevice->BeginScene();
584 if (SUCCEEDED(result)) {
585 // This is defensive checking against the device being
586 // lost at unexpected times.
587 mSceneStarted = true;
588 }
589 }
590}
591
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000592void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000593{
594 if (mSceneStarted)
595 {
596 // EndScene can fail if the device was lost, for example due
597 // to a TDR during a draw call.
598 mDevice->EndScene();
599 mSceneStarted = false;
600 }
601}
602
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000603void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000604{
605 HRESULT result;
606
607 IDirect3DQuery9* query = allocateEventQuery();
608 if (!query)
609 {
610 return;
611 }
612
613 result = query->Issue(D3DISSUE_END);
614 ASSERT(SUCCEEDED(result));
615
616 do
617 {
618 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
619
620 if(block && result == S_FALSE)
621 {
622 // Keep polling, but allow other threads to do something useful first
623 Sleep(0);
624 // explicitly check for device loss
625 // some drivers seem to return S_FALSE even if the device is lost
626 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000627 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000628 {
629 result = D3DERR_DEVICELOST;
630 }
631 }
632 }
633 while(block && result == S_FALSE);
634
635 freeEventQuery(query);
636
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000637 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000638 {
639 mDisplay->notifyDeviceLost();
640 }
641}
642
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000643SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
644{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000645 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000646}
647
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000648IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000649{
650 IDirect3DQuery9 *query = NULL;
651
652 if (mEventQueryPool.empty())
653 {
654 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
655 ASSERT(SUCCEEDED(result));
656 }
657 else
658 {
659 query = mEventQueryPool.back();
660 mEventQueryPool.pop_back();
661 }
662
663 return query;
664}
665
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000666void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000667{
668 if (mEventQueryPool.size() > 1000)
669 {
670 query->Release();
671 }
672 else
673 {
674 mEventQueryPool.push_back(query);
675 }
676}
677
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000678IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000679{
680 return mVertexShaderCache.create(function, length);
681}
682
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000683IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000684{
685 return mPixelShaderCache.create(function, length);
686}
687
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000688HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
689{
690 D3DPOOL Pool = getBufferPool(Usage);
691 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
692}
693
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000694VertexBuffer *Renderer9::createVertexBuffer()
695{
696 return new VertexBuffer9(this);
697}
698
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000699HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
700{
701 D3DPOOL Pool = getBufferPool(Usage);
702 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
703}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000704
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000705IndexBuffer *Renderer9::createIndexBuffer()
706{
707 return new IndexBuffer9(this);
708}
709
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000710QueryImpl *Renderer9::createQuery(GLenum type)
711{
712 return new Query9(this, type);
713}
714
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000715FenceImpl *Renderer9::createFence()
716{
717 return new Fence9(this);
718}
719
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000720void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000721{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000722 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
723 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000724
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000725 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000726 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000727 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
728 int d3dSampler = index + d3dSamplerOffset;
729
730 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
731 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
732
733 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
734 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
735 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
736 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
737 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
738 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
739 if (mSupportsTextureFilterAnisotropy)
740 {
741 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
742 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000743 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000744
745 forceSetSamplers[index] = false;
746 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000747}
748
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000749void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000750{
751 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
752 int d3dSampler = index + d3dSamplerOffset;
753 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000754 unsigned int serial = 0;
755 bool forceSetTexture = false;
756
757 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000758
759 if (texture)
760 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000761 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000762 if (texStorage)
763 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000764 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000765 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000766 }
767 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000768 // in the texture class and we're unexpectedly missing the d3d texture
769 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000770
771 serial = texture->getTextureSerial();
772 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000773 }
774
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000775 if (forceSetTexture || appliedSerials[index] != serial)
776 {
777 mDevice->SetTexture(d3dSampler, d3dTexture);
778 }
779
780 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000781}
782
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000783void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000784{
785 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000786
787 if (rasterStateChanged)
788 {
789 // Set the cull mode
790 if (rasterState.cullFace)
791 {
792 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
793 }
794 else
795 {
796 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
797 }
798
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000799 if (rasterState.polygonOffsetFill)
800 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000801 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802 {
803 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
804
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000805 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000806 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
807 }
808 }
809 else
810 {
811 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
812 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
813 }
814
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000815 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000816 }
817
818 mForceSetRasterState = false;
819}
820
821void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
822{
823 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
824 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
825 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
826
827 if (blendStateChanged || blendColorChanged)
828 {
829 if (blendState.blend)
830 {
831 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
832
833 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
834 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
835 {
836 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
837 }
838 else
839 {
840 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
841 gl::unorm<8>(blendColor.alpha),
842 gl::unorm<8>(blendColor.alpha),
843 gl::unorm<8>(blendColor.alpha)));
844 }
845
846 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
847 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
848 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
849
850 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
851 blendState.destBlendRGB != blendState.destBlendAlpha ||
852 blendState.blendEquationRGB != blendState.blendEquationAlpha)
853 {
854 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
855
856 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
857 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
858 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
859 }
860 else
861 {
862 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
863 }
864 }
865 else
866 {
867 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
868 }
869
870 if (blendState.sampleAlphaToCoverage)
871 {
872 FIXME("Sample alpha to coverage is unimplemented.");
873 }
874
875 // Set the color mask
876 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
877 // Apparently some ATI cards have a bug where a draw with a zero color
878 // write mask can cause later draws to have incorrect results. Instead,
879 // set a nonzero color write mask but modify the blend state so that no
880 // drawing is done.
881 // http://code.google.com/p/angleproject/issues/detail?id=169
882
883 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
884 blendState.colorMaskBlue, blendState.colorMaskAlpha);
885 if (colorMask == 0 && !zeroColorMaskAllowed)
886 {
887 // Enable green channel, but set blending so nothing will be drawn.
888 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
889 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
890
891 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
892 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
893 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
894 }
895 else
896 {
897 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
898 }
899
900 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
901
902 mCurBlendState = blendState;
903 mCurBlendColor = blendColor;
904 }
905
906 if (sampleMaskChanged)
907 {
908 // Set the multisample mask
909 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
910 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
911
912 mCurSampleMask = sampleMask;
913 }
914
915 mForceSetBlendState = false;
916}
917
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000918void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000919 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000920{
921 bool depthStencilStateChanged = mForceSetDepthStencilState ||
922 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000923 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
924 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000925 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000926
927 if (depthStencilStateChanged)
928 {
929 if (depthStencilState.depthTest)
930 {
931 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
932 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
933 }
934 else
935 {
936 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
937 }
938
939 mCurDepthStencilState = depthStencilState;
940 }
941
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000942 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000943 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000944 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000945 {
946 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
947 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
948
949 // FIXME: Unsupported by D3D9
950 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
951 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
952 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
953 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000954 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000955 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
956 {
957 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
958 return error(GL_INVALID_OPERATION);
959 }
960
961 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000962 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000963
964 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
965 depthStencilState.stencilWritemask);
966 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
967 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
968
969 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000970 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000971 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
972 depthStencilState.stencilMask);
973
974 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
975 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
976 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
977 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
978 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
979 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
980
981 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
982 depthStencilState.stencilBackWritemask);
983 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
984 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
985
986 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000987 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000988 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
989 depthStencilState.stencilBackMask);
990
991 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
992 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
993 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
994 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
995 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
996 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
997 }
998 else
999 {
1000 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1001 }
1002
1003 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
1004
daniel@transgaming.com08c331d2012-11-28 19:38:39 +00001005 mCurStencilRef = stencilRef;
1006 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001007 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001008 }
1009
1010 mForceSetDepthStencilState = false;
1011}
1012
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001013void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001014{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001015 bool scissorChanged = mForceSetScissor ||
1016 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1017 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001018
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001019 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001020 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001021 if (enabled)
1022 {
1023 RECT rect;
1024 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1025 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1026 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1027 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1028 mDevice->SetScissorRect(&rect);
1029 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001030
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001031 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1032
1033 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001034 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001035 }
1036
1037 mForceSetScissor = false;
1038}
1039
daniel@transgaming.com12985182012-12-20 20:56:31 +00001040bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001041 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001042{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001043 gl::Rectangle actualViewport = viewport;
1044 float actualZNear = gl::clamp01(zNear);
1045 float actualZFar = gl::clamp01(zFar);
1046 if (ignoreViewport)
1047 {
1048 actualViewport.x = 0;
1049 actualViewport.y = 0;
1050 actualViewport.width = mRenderTargetDesc.width;
1051 actualViewport.height = mRenderTargetDesc.height;
1052 actualZNear = 0.0f;
1053 actualZFar = 1.0f;
1054 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001055
1056 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001057 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1058 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1059 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1060 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1061 dxViewport.MinZ = actualZNear;
1062 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001063
1064 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1065 {
1066 return false; // Nothing to render
1067 }
1068
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001069 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1070 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001071 if (viewportChanged)
1072 {
1073 mDevice->SetViewport(&dxViewport);
1074
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001075 mCurViewport = actualViewport;
1076 mCurNear = actualZNear;
1077 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001078
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001079 dx_VertexConstants vc = {0};
1080 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001081
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001082 vc.halfPixelSize[0] = 1.0f / dxViewport.Width;
1083 vc.halfPixelSize[1] = -1.0f / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001084
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001085 pc.coord[0] = actualViewport.width * 0.5f;
1086 pc.coord[1] = actualViewport.height * 0.5f;
1087 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
1088 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001089
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001090 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1091 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1092 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1093
1094 vc.depthRange[0] = actualZNear;
1095 vc.depthRange[1] = actualZFar;
1096 vc.depthRange[2] = actualZFar - actualZNear;
1097
1098 pc.depthRange[0] = actualZNear;
1099 pc.depthRange[1] = actualZFar;
1100 pc.depthRange[2] = actualZFar - actualZNear;
1101
1102 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1103 {
1104 mVertexConstants = vc;
1105 mDxUniformsDirty = true;
1106 }
1107
1108 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1109 {
1110 mPixelConstants = pc;
1111 mDxUniformsDirty = true;
1112 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001113 }
1114
1115 mForceSetViewport = false;
1116 return true;
1117}
1118
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001119bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1120{
1121 switch (mode)
1122 {
1123 case GL_POINTS:
1124 mPrimitiveType = D3DPT_POINTLIST;
1125 mPrimitiveCount = count;
1126 break;
1127 case GL_LINES:
1128 mPrimitiveType = D3DPT_LINELIST;
1129 mPrimitiveCount = count / 2;
1130 break;
1131 case GL_LINE_LOOP:
1132 mPrimitiveType = D3DPT_LINESTRIP;
1133 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1134 break;
1135 case GL_LINE_STRIP:
1136 mPrimitiveType = D3DPT_LINESTRIP;
1137 mPrimitiveCount = count - 1;
1138 break;
1139 case GL_TRIANGLES:
1140 mPrimitiveType = D3DPT_TRIANGLELIST;
1141 mPrimitiveCount = count / 3;
1142 break;
1143 case GL_TRIANGLE_STRIP:
1144 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1145 mPrimitiveCount = count - 2;
1146 break;
1147 case GL_TRIANGLE_FAN:
1148 mPrimitiveType = D3DPT_TRIANGLEFAN;
1149 mPrimitiveCount = count - 2;
1150 break;
1151 default:
1152 return error(GL_INVALID_ENUM, false);
1153 }
1154
1155 return mPrimitiveCount > 0;
1156}
1157
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001158
1159gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1160{
1161 if (!depthbuffer)
1162 {
1163 ERR("Unexpected null depthbuffer for depth-only FBO.");
1164 return NULL;
1165 }
1166
1167 GLsizei width = depthbuffer->getWidth();
1168 GLsizei height = depthbuffer->getHeight();
1169
1170 // search cached nullcolorbuffers
1171 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1172 {
1173 if (mNullColorbufferCache[i].buffer != NULL &&
1174 mNullColorbufferCache[i].width == width &&
1175 mNullColorbufferCache[i].height == height)
1176 {
1177 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1178 return mNullColorbufferCache[i].buffer;
1179 }
1180 }
1181
1182 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1183
1184 // add nullbuffer to the cache
1185 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1186 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1187 {
1188 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1189 {
1190 oldest = &mNullColorbufferCache[i];
1191 }
1192 }
1193
1194 delete oldest->buffer;
1195 oldest->buffer = nullbuffer;
1196 oldest->lruCount = ++mMaxNullColorbufferLRU;
1197 oldest->width = width;
1198 oldest->height = height;
1199
1200 return nullbuffer;
1201}
1202
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001203bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001204{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001205 // if there is no color attachment we must synthesize a NULL colorattachment
1206 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1207 gl::Renderbuffer *renderbufferObject = NULL;
1208 if (framebuffer->getColorbufferType() != GL_NONE)
1209 {
1210 renderbufferObject = framebuffer->getColorbuffer();
1211 }
1212 else
1213 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001214 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001215 }
1216 if (!renderbufferObject)
1217 {
1218 ERR("unable to locate renderbuffer for FBO.");
1219 return false;
1220 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001221
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001222 bool renderTargetChanged = false;
1223 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1224 if (renderTargetSerial != mAppliedRenderTargetSerial)
1225 {
1226 // Apply the render target on the device
1227 IDirect3DSurface9 *renderTargetSurface = NULL;
1228
1229 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1230 if (renderTarget)
1231 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001232 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001233 }
1234
1235 if (!renderTargetSurface)
1236 {
1237 ERR("render target pointer unexpectedly null.");
1238 return false; // Context must be lost
1239 }
1240
1241 mDevice->SetRenderTarget(0, renderTargetSurface);
1242 renderTargetSurface->Release();
1243
1244 mAppliedRenderTargetSerial = renderTargetSerial;
1245 renderTargetChanged = true;
1246 }
1247
1248 gl::Renderbuffer *depthStencil = NULL;
1249 unsigned int depthbufferSerial = 0;
1250 unsigned int stencilbufferSerial = 0;
1251 if (framebuffer->getDepthbufferType() != GL_NONE)
1252 {
1253 depthStencil = framebuffer->getDepthbuffer();
1254 if (!depthStencil)
1255 {
1256 ERR("Depth stencil pointer unexpectedly null.");
1257 return false;
1258 }
1259
1260 depthbufferSerial = depthStencil->getSerial();
1261 }
1262 else if (framebuffer->getStencilbufferType() != GL_NONE)
1263 {
1264 depthStencil = framebuffer->getStencilbuffer();
1265 if (!depthStencil)
1266 {
1267 ERR("Depth stencil pointer unexpectedly null.");
1268 return false;
1269 }
1270
1271 stencilbufferSerial = depthStencil->getSerial();
1272 }
1273
1274 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1275 stencilbufferSerial != mAppliedStencilbufferSerial ||
1276 !mDepthStencilInitialized)
1277 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001278 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001279 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001280
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001281 // Apply the depth stencil on the device
1282 if (depthStencil)
1283 {
1284 IDirect3DSurface9 *depthStencilSurface = NULL;
1285 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1286
1287 if (depthStencilRenderTarget)
1288 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001289 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001290 }
1291
1292 if (!depthStencilSurface)
1293 {
1294 ERR("depth stencil pointer unexpectedly null.");
1295 return false; // Context must be lost
1296 }
1297
1298 mDevice->SetDepthStencilSurface(depthStencilSurface);
1299 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001300
1301 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001302 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001303 }
1304 else
1305 {
1306 mDevice->SetDepthStencilSurface(NULL);
1307 }
1308
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001309 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1310 {
1311 mCurDepthSize = depthSize;
1312 mForceSetRasterState = true;
1313 }
1314
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001315 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1316 {
1317 mCurStencilSize = stencilSize;
1318 mForceSetDepthStencilState = true;
1319 }
1320
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001321 mAppliedDepthbufferSerial = depthbufferSerial;
1322 mAppliedStencilbufferSerial = stencilbufferSerial;
1323 mDepthStencilInitialized = true;
1324 }
1325
1326 if (renderTargetChanged || !mRenderTargetDescInitialized)
1327 {
1328 mForceSetScissor = true;
1329 mForceSetViewport = true;
1330
1331 mRenderTargetDesc.width = renderbufferObject->getWidth();
1332 mRenderTargetDesc.height = renderbufferObject->getHeight();
1333 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1334 mRenderTargetDescInitialized = true;
1335 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001336
1337 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001338}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001339
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001340GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001341{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001342 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001343 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1344 if (err != GL_NO_ERROR)
1345 {
1346 return err;
1347 }
1348
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001349 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1350}
1351
1352// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001353GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001354{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001355 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001356
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001357 if (err == GL_NO_ERROR)
1358 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001359 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001360 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001361 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1362
1363 mDevice->SetIndices(indexBuffer->getBuffer());
1364 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001365 }
1366 }
1367
1368 return err;
1369}
1370
1371void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1372{
1373 startScene();
1374
1375 if (mode == GL_LINE_LOOP)
1376 {
1377 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1378 }
1379 else if (instances > 0)
1380 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001381 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001382 if (countingIB)
1383 {
1384 if (mAppliedIBSerial != countingIB->getSerial())
1385 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001386 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1387
1388 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001389 mAppliedIBSerial = countingIB->getSerial();
1390 }
1391
1392 for (int i = 0; i < mRepeatDraw; i++)
1393 {
1394 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1395 }
1396 }
1397 else
1398 {
1399 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1400 return error(GL_OUT_OF_MEMORY);
1401 }
1402 }
1403 else // Regular case
1404 {
1405 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1406 }
1407}
1408
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001409void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001410{
1411 startScene();
1412
1413 if (mode == GL_LINE_LOOP)
1414 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001415 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001416 }
1417 else
1418 {
1419 for (int i = 0; i < mRepeatDraw; i++)
1420 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001421 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1422 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001423 }
1424 }
1425}
1426
1427void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1428{
1429 // Get the raw indices for an indexed draw
1430 if (type != GL_NONE && elementArrayBuffer)
1431 {
1432 gl::Buffer *indexBuffer = elementArrayBuffer;
1433 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1434 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1435 }
1436
1437 UINT startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001438
1439 if (get32BitIndexSupport())
1440 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001441 if (!mLineLoopIB)
1442 {
1443 mLineLoopIB = new StreamingIndexBufferInterface(this);
1444 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1445 {
1446 delete mLineLoopIB;
1447 mLineLoopIB = NULL;
1448
1449 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
1450 return error(GL_OUT_OF_MEMORY);
1451 }
1452 }
1453
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001454 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001455 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001456 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001457 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1458 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001459 }
1460
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001461 void* mappedMemory = NULL;
1462 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1463 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001464 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001465 ERR("Could not map index buffer for GL_LINE_LOOP.");
1466 return error(GL_OUT_OF_MEMORY);
1467 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001468
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001469 startIndex = static_cast<UINT>(offset) / 4;
1470 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1471
1472 switch (type)
1473 {
1474 case GL_NONE: // Non-indexed draw
1475 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001476 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001477 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001478 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001479 data[count] = 0;
1480 break;
1481 case GL_UNSIGNED_BYTE:
1482 for (int i = 0; i < count; i++)
1483 {
1484 data[i] = static_cast<const GLubyte*>(indices)[i];
1485 }
1486 data[count] = static_cast<const GLubyte*>(indices)[0];
1487 break;
1488 case GL_UNSIGNED_SHORT:
1489 for (int i = 0; i < count; i++)
1490 {
1491 data[i] = static_cast<const GLushort*>(indices)[i];
1492 }
1493 data[count] = static_cast<const GLushort*>(indices)[0];
1494 break;
1495 case GL_UNSIGNED_INT:
1496 for (int i = 0; i < count; i++)
1497 {
1498 data[i] = static_cast<const GLuint*>(indices)[i];
1499 }
1500 data[count] = static_cast<const GLuint*>(indices)[0];
1501 break;
1502 default: UNREACHABLE();
1503 }
1504
1505 if (!mLineLoopIB->unmapBuffer())
1506 {
1507 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1508 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001509 }
1510 }
1511 else
1512 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001513 if (!mLineLoopIB)
1514 {
1515 mLineLoopIB = new StreamingIndexBufferInterface(this);
1516 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1517 {
1518 delete mLineLoopIB;
1519 mLineLoopIB = NULL;
1520
1521 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
1522 return error(GL_OUT_OF_MEMORY);
1523 }
1524 }
1525
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001526 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001527 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001528 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001529 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1530 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001531 }
1532
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001533 void* mappedMemory = NULL;
1534 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1535 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001536 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001537 ERR("Could not map index buffer for GL_LINE_LOOP.");
1538 return error(GL_OUT_OF_MEMORY);
1539 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001540
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001541 startIndex = static_cast<UINT>(offset) / 2;
1542 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1543
1544 switch (type)
1545 {
1546 case GL_NONE: // Non-indexed draw
1547 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001548 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001549 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001550 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001551 data[count] = 0;
1552 break;
1553 case GL_UNSIGNED_BYTE:
1554 for (int i = 0; i < count; i++)
1555 {
1556 data[i] = static_cast<const GLubyte*>(indices)[i];
1557 }
1558 data[count] = static_cast<const GLubyte*>(indices)[0];
1559 break;
1560 case GL_UNSIGNED_SHORT:
1561 for (int i = 0; i < count; i++)
1562 {
1563 data[i] = static_cast<const GLushort*>(indices)[i];
1564 }
1565 data[count] = static_cast<const GLushort*>(indices)[0];
1566 break;
1567 case GL_UNSIGNED_INT:
1568 for (int i = 0; i < count; i++)
1569 {
1570 data[i] = static_cast<const GLuint*>(indices)[i];
1571 }
1572 data[count] = static_cast<const GLuint*>(indices)[0];
1573 break;
1574 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001575 }
1576
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001577 if (!mLineLoopIB->unmapBuffer())
1578 {
1579 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1580 return error(GL_OUT_OF_MEMORY);
1581 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001582 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001583
1584 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001585 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001586 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1587
1588 mDevice->SetIndices(indexBuffer->getBuffer());
1589 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001590 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001591
1592 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001593}
1594
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001595void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1596{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001597 unsigned int programBinarySerial = programBinary->getSerial();
1598 if (programBinarySerial != mAppliedProgramBinarySerial)
1599 {
1600 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1601 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001602
daniel@transgaming.come4991412012-12-20 20:55:34 +00001603 IDirect3DVertexShader9 *vertexShader = NULL;
1604 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001605
daniel@transgaming.come4991412012-12-20 20:55:34 +00001606 IDirect3DPixelShader9 *pixelShader = NULL;
1607 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001608
daniel@transgaming.come4991412012-12-20 20:55:34 +00001609 mDevice->SetPixelShader(pixelShader);
1610 mDevice->SetVertexShader(vertexShader);
1611 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001612 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001613
1614 mAppliedProgramBinarySerial = programBinarySerial;
1615 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001616}
1617
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001618void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001619{
1620 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1621 {
1622 gl::Uniform *targetUniform = *ub;
1623
1624 if (targetUniform->dirty)
1625 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001626 int count = targetUniform->elementCount();
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001627 GLfloat *f = (GLfloat*)targetUniform->data;
1628 GLint *i = (GLint*)targetUniform->data;
1629 GLboolean *b = (GLboolean*)targetUniform->data;
1630
1631 switch (targetUniform->type)
1632 {
1633 case GL_SAMPLER_2D:
1634 case GL_SAMPLER_CUBE:
1635 break;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001636 case GL_BOOL: applyUniformnbv(targetUniform, count, 1, b); break;
1637 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, count, 2, b); break;
1638 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, count, 3, b); break;
1639 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, count, 4, b); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001640 case GL_FLOAT:
1641 case GL_FLOAT_VEC2:
1642 case GL_FLOAT_VEC3:
1643 case GL_FLOAT_VEC4:
1644 case GL_FLOAT_MAT2:
1645 case GL_FLOAT_MAT3:
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001646 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1647 case GL_INT: applyUniform1iv(targetUniform, count, i); break;
1648 case GL_INT_VEC2: applyUniform2iv(targetUniform, count, i); break;
1649 case GL_INT_VEC3: applyUniform3iv(targetUniform, count, i); break;
1650 case GL_INT_VEC4: applyUniform4iv(targetUniform, count, i); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001651 default:
1652 UNREACHABLE();
1653 }
1654
1655 targetUniform->dirty = false;
1656 }
1657 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001658
1659 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001660 if (mDxUniformsDirty)
1661 {
1662 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1663 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1664 mDxUniformsDirty = false;
1665 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001666}
1667
1668void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
1669{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001670 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001671
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001672 if (targetUniform->psRegisterIndex >= 0 || targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001673 {
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001674 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001675 for (int i = 0; i < count; i++)
1676 {
1677 for (int j = 0; j < 4; j++)
1678 {
1679 if (j < width)
1680 {
1681 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
1682 }
1683 else
1684 {
1685 vector[i * 4 + j] = 0.0f;
1686 }
1687 }
1688 }
1689 }
1690
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001691 applyUniformnfv(targetUniform, vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001692}
1693
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001694void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001695{
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001696 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001697 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001698 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001699 }
1700
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001701 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001702 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001703 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001704 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001705}
1706
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001707void Renderer9::applyUniform1iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001708{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001709 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1710 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001711
1712 for (int i = 0; i < count; i++)
1713 {
1714 vector[i] = gl::Vector4((float)v[i], 0, 0, 0);
1715 }
1716
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001717 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001718}
1719
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001720void Renderer9::applyUniform2iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001721{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001722 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1723 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001724
1725 for (int i = 0; i < count; i++)
1726 {
1727 vector[i] = gl::Vector4((float)v[0], (float)v[1], 0, 0);
1728
1729 v += 2;
1730 }
1731
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001732 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001733}
1734
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001735void Renderer9::applyUniform3iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001737 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1738 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739
1740 for (int i = 0; i < count; i++)
1741 {
1742 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], 0);
1743
1744 v += 3;
1745 }
1746
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001747 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001748}
1749
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001750void Renderer9::applyUniform4iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001751{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001752 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1753 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001754
1755 for (int i = 0; i < count; i++)
1756 {
1757 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1758
1759 v += 4;
1760 }
1761
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001762 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001763}
1764
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001765void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001766{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001767 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1768 gl::unorm<8>(clearParams.colorClearValue.red),
1769 gl::unorm<8>(clearParams.colorClearValue.green),
1770 gl::unorm<8>(clearParams.colorClearValue.blue));
1771 float depth = gl::clamp01(clearParams.depthClearValue);
1772 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001773
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001774 unsigned int stencilUnmasked = 0x0;
1775 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1776 {
1777 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1778 stencilUnmasked = (0x1 << stencilSize) - 1;
1779 }
1780
1781 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1782
1783 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1784 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1785 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1786 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1787 clearParams.colorMaskBlue && alphaUnmasked);
1788
1789 if (needMaskedColorClear || needMaskedStencilClear)
1790 {
1791 // State which is altered in all paths from this point to the clear call is saved.
1792 // State which is altered in only some paths will be flagged dirty in the case that
1793 // that path is taken.
1794 HRESULT hr;
1795 if (mMaskedClearSavedState == NULL)
1796 {
1797 hr = mDevice->BeginStateBlock();
1798 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1799
1800 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1801 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1802 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1803 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1804 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1805 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1806 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1807 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1808 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1809 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1810 mDevice->SetPixelShader(NULL);
1811 mDevice->SetVertexShader(NULL);
1812 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1813 mDevice->SetStreamSource(0, NULL, 0, 0);
1814 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1815 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1816 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1817 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1818 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1819 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1820 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1821
1822 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1823 {
1824 mDevice->SetStreamSourceFreq(i, 1);
1825 }
1826
1827 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1828 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1829 }
1830
1831 ASSERT(mMaskedClearSavedState != NULL);
1832
1833 if (mMaskedClearSavedState != NULL)
1834 {
1835 hr = mMaskedClearSavedState->Capture();
1836 ASSERT(SUCCEEDED(hr));
1837 }
1838
1839 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1840 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1841 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1842 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1843 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1844 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1845 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1846 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1847
1848 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1849 {
1850 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1851 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1852 clearParams.colorMaskGreen,
1853 clearParams.colorMaskBlue,
1854 clearParams.colorMaskAlpha));
1855 }
1856 else
1857 {
1858 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1859 }
1860
1861 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1862 {
1863 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1864 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1865 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1866 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1867 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1868 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1869 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1870 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1871 }
1872 else
1873 {
1874 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1875 }
1876
1877 mDevice->SetPixelShader(NULL);
1878 mDevice->SetVertexShader(NULL);
1879 mDevice->SetFVF(D3DFVF_XYZRHW);
1880 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1881 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1882 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1883 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1884 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1885 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1886 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1887
1888 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1889 {
1890 mDevice->SetStreamSourceFreq(i, 1);
1891 }
1892
1893 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1894 quad[0][0] = -0.5f;
1895 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1896 quad[0][2] = 0.0f;
1897 quad[0][3] = 1.0f;
1898
1899 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1900 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1901 quad[1][2] = 0.0f;
1902 quad[1][3] = 1.0f;
1903
1904 quad[2][0] = -0.5f;
1905 quad[2][1] = -0.5f;
1906 quad[2][2] = 0.0f;
1907 quad[2][3] = 1.0f;
1908
1909 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1910 quad[3][1] = -0.5f;
1911 quad[3][2] = 0.0f;
1912 quad[3][3] = 1.0f;
1913
1914 startScene();
1915 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1916
1917 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1918 {
1919 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1920 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1921 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1922 }
1923
1924 if (mMaskedClearSavedState != NULL)
1925 {
1926 mMaskedClearSavedState->Apply();
1927 }
1928 }
1929 else if (clearParams.mask)
1930 {
1931 DWORD dxClearFlags = 0;
1932 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1933 {
1934 dxClearFlags |= D3DCLEAR_TARGET;
1935 }
1936 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1937 {
1938 dxClearFlags |= D3DCLEAR_ZBUFFER;
1939 }
1940 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1941 {
1942 dxClearFlags |= D3DCLEAR_STENCIL;
1943 }
1944
1945 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1946 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001947}
1948
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001949void Renderer9::markAllStateDirty()
1950{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001951 mAppliedRenderTargetSerial = 0;
1952 mAppliedDepthbufferSerial = 0;
1953 mAppliedStencilbufferSerial = 0;
1954 mDepthStencilInitialized = false;
1955 mRenderTargetDescInitialized = false;
1956
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001957 mForceSetDepthStencilState = true;
1958 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001959 mForceSetScissor = true;
1960 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001961 mForceSetBlendState = true;
1962
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001963 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001964 {
1965 mForceSetVertexSamplerStates[i] = true;
1966 mCurVertexTextureSerials[i] = 0;
1967 }
1968 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1969 {
1970 mForceSetPixelSamplerStates[i] = true;
1971 mCurPixelTextureSerials[i] = 0;
1972 }
1973
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001974 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001975 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001976 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001977
1978 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001979}
1980
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001981void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001982{
1983 while (!mEventQueryPool.empty())
1984 {
1985 mEventQueryPool.back()->Release();
1986 mEventQueryPool.pop_back();
1987 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001988
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001989 if (mMaskedClearSavedState)
1990 {
1991 mMaskedClearSavedState->Release();
1992 mMaskedClearSavedState = NULL;
1993 }
1994
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001995 mVertexShaderCache.clear();
1996 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001997
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001998 delete mBlit;
1999 mBlit = NULL;
2000
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002001 delete mVertexDataManager;
2002 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00002003
2004 delete mIndexDataManager;
2005 mIndexDataManager = NULL;
2006
2007 delete mLineLoopIB;
2008 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002009
2010 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2011 {
2012 delete mNullColorbufferCache[i].buffer;
2013 mNullColorbufferCache[i].buffer = NULL;
2014 }
2015
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002016}
2017
2018
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002019void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002020{
2021 mDeviceLost = true;
2022}
2023
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002024bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002025{
2026 return mDeviceLost;
2027}
2028
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002029// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002030bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002031{
2032 bool isLost = false;
2033
2034 if (mDeviceEx)
2035 {
2036 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
2037 }
2038 else if (mDevice)
2039 {
2040 isLost = FAILED(mDevice->TestCooperativeLevel());
2041 }
2042 else
2043 {
2044 // No device yet, so no reset required
2045 }
2046
2047 if (isLost)
2048 {
2049 // ensure we note the device loss --
2050 // we'll probably get this done again by markDeviceLost
2051 // but best to remember it!
2052 // Note that we don't want to clear the device loss status here
2053 // -- this needs to be done by resetDevice
2054 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002055 if (notify)
2056 {
2057 mDisplay->notifyDeviceLost();
2058 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002059 }
2060
2061 return isLost;
2062}
2063
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002064bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002065{
2066 HRESULT status = D3D_OK;
2067
2068 if (mDeviceEx)
2069 {
2070 status = mDeviceEx->CheckDeviceState(NULL);
2071 }
2072 else if (mDevice)
2073 {
2074 status = mDevice->TestCooperativeLevel();
2075 }
2076
2077 switch (status)
2078 {
2079 case D3DERR_DEVICENOTRESET:
2080 case D3DERR_DEVICEHUNG:
2081 return true;
2082 default:
2083 return false;
2084 }
2085}
2086
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002087bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002088{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002089 releaseDeviceResources();
2090
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002091 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2092
2093 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002094 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002095 int attempts = 3;
2096
2097 while (lost && attempts > 0)
2098 {
2099 if (mDeviceEx)
2100 {
2101 Sleep(500); // Give the graphics driver some CPU time
2102 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2103 }
2104 else
2105 {
2106 result = mDevice->TestCooperativeLevel();
2107 while (result == D3DERR_DEVICELOST)
2108 {
2109 Sleep(100); // Give the graphics driver some CPU time
2110 result = mDevice->TestCooperativeLevel();
2111 }
2112
2113 if (result == D3DERR_DEVICENOTRESET)
2114 {
2115 result = mDevice->Reset(&presentParameters);
2116 }
2117 }
2118
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002119 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002120 attempts --;
2121 }
2122
2123 if (FAILED(result))
2124 {
2125 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2126 return false;
2127 }
2128
2129 // reset device defaults
2130 initializeDevice();
2131 mDeviceLost = false;
2132
2133 return true;
2134}
2135
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002136DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002137{
2138 return mAdapterIdentifier.VendorId;
2139}
2140
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002141std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002142{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002143 std::ostringstream rendererString;
2144
2145 rendererString << mAdapterIdentifier.Description;
2146 if (getShareHandleSupport())
2147 {
2148 rendererString << " Direct3D9Ex";
2149 }
2150 else
2151 {
2152 rendererString << " Direct3D9";
2153 }
2154
2155 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2156 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2157
2158 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002159}
2160
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002161GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002162{
2163 return mAdapterIdentifier.DeviceIdentifier;
2164}
2165
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002166void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002167{
2168 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
2169 {
2170 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
2171 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2172
2173 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
2174 }
2175}
2176
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002177bool Renderer9::getBGRATextureSupport() const
2178{
2179 // DirectX 9 always supports BGRA
2180 return true;
2181}
2182
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002183bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002184{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002185 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002186}
2187
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002188bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002189{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002190 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002191}
2192
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002193bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002194{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002195 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002196}
2197
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002198bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002199{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002200 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002201}
2202
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002203bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002204{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002205 *filtering = mFloat32FilterSupport;
2206 *renderable = mFloat32RenderSupport;
2207 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002208}
2209
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002210bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002211{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002212 *filtering = mFloat16FilterSupport;
2213 *renderable = mFloat16RenderSupport;
2214 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002215}
2216
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002217bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002218{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002219 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002220}
2221
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002222bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002223{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002224 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002225}
2226
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002227bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002228{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002229 return mSupportsTextureFilterAnisotropy;
2230}
2231
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002232float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002233{
2234 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002235 {
2236 return mDeviceCaps.MaxAnisotropy;
2237 }
2238 return 1.0f;
2239}
2240
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002241bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002242{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002243 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002244}
2245
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002246unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002247{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002248 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2249 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002250}
2251
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002252unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2253{
2254 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2255}
2256
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002257int Renderer9::getMaxVertexUniformVectors() const
2258{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002259 return MAX_VERTEX_UNIFORM_VECTORS;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002260}
2261
2262int Renderer9::getMaxFragmentUniformVectors() const
2263{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002264 return getMajorShaderModel() >= 3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002265}
2266
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002267int Renderer9::getMaxVaryingVectors() const
2268{
2269 return getMajorShaderModel() >= 3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2270}
2271
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002272bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002273{
2274 return mSupportsNonPower2Textures;
2275}
2276
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002277bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002278{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002279 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002280}
2281
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002282bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002283{
2284 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2285}
2286
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002287bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002288{
2289 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002290 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002291}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002292
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002293bool Renderer9::getDerivativeInstructionSupport() const
2294{
2295 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2296}
2297
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002298int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002299{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002300 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002301}
2302
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002303float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002304{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002305 // Point size clamped at 1.0f for SM2
2306 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002307}
2308
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002309int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002310{
2311 return (int)mDeviceCaps.MaxTextureWidth;
2312}
2313
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002314int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002315{
2316 return (int)mDeviceCaps.MaxTextureHeight;
2317}
2318
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002319bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002320{
2321 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2322}
2323
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002324DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002325{
2326 return mDeviceCaps.DeclTypes;
2327}
2328
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002329int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002330{
2331 return mMinSwapInterval;
2332}
2333
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002334int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002335{
2336 return mMaxSwapInterval;
2337}
2338
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002339int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002340{
2341 return mMaxSupportedSamples;
2342}
2343
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002344int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002345{
2346 if (requested == 0)
2347 {
2348 return requested;
2349 }
2350
2351 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2352 if (itr == mMultiSampleSupport.end())
2353 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002354 if (format == D3DFMT_UNKNOWN)
2355 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002356 return -1;
2357 }
2358
2359 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2360 {
2361 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2362 {
2363 return i;
2364 }
2365 }
2366
2367 return -1;
2368}
2369
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002370D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2371{
2372 switch (internalformat)
2373 {
2374 case GL_DEPTH_COMPONENT16:
2375 case GL_DEPTH_COMPONENT32_OES:
2376 case GL_DEPTH24_STENCIL8_OES:
2377 return D3DFMT_INTZ;
2378 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2379 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2380 return D3DFMT_DXT1;
2381 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2382 return D3DFMT_DXT3;
2383 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2384 return D3DFMT_DXT5;
2385 case GL_RGBA32F_EXT:
2386 case GL_RGB32F_EXT:
2387 case GL_ALPHA32F_EXT:
2388 case GL_LUMINANCE32F_EXT:
2389 case GL_LUMINANCE_ALPHA32F_EXT:
2390 return D3DFMT_A32B32G32R32F;
2391 case GL_RGBA16F_EXT:
2392 case GL_RGB16F_EXT:
2393 case GL_ALPHA16F_EXT:
2394 case GL_LUMINANCE16F_EXT:
2395 case GL_LUMINANCE_ALPHA16F_EXT:
2396 return D3DFMT_A16B16G16R16F;
2397 case GL_LUMINANCE8_EXT:
2398 if (getLuminanceTextureSupport())
2399 {
2400 return D3DFMT_L8;
2401 }
2402 break;
2403 case GL_LUMINANCE8_ALPHA8_EXT:
2404 if (getLuminanceAlphaTextureSupport())
2405 {
2406 return D3DFMT_A8L8;
2407 }
2408 break;
2409 case GL_RGB8_OES:
2410 case GL_RGB565:
2411 return D3DFMT_X8R8G8B8;
2412 }
2413
2414 return D3DFMT_A8R8G8B8;
2415}
2416
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002417bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002418{
2419 bool result = false;
2420
2421 if (source && dest)
2422 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002423 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2424 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002425
2426 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002427 for (int i = 0; i < levels; ++i)
2428 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002429 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2430 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002431
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002432 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002433
2434 if (srcSurf) srcSurf->Release();
2435 if (dstSurf) dstSurf->Release();
2436
2437 if (!result)
2438 return false;
2439 }
2440 }
2441
2442 return result;
2443}
2444
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002445bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002446{
2447 bool result = false;
2448
2449 if (source && dest)
2450 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002451 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2452 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002453 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002454 for (int f = 0; f < 6; f++)
2455 {
2456 for (int i = 0; i < levels; i++)
2457 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002458 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2459 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002460
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002461 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002462
2463 if (srcSurf) srcSurf->Release();
2464 if (dstSurf) dstSurf->Release();
2465
2466 if (!result)
2467 return false;
2468 }
2469 }
2470 }
2471
2472 return result;
2473}
2474
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002475D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002476{
2477 if (mD3d9Ex != NULL)
2478 {
2479 return D3DPOOL_DEFAULT;
2480 }
2481 else
2482 {
2483 if (!(usage & D3DUSAGE_DYNAMIC))
2484 {
2485 return D3DPOOL_MANAGED;
2486 }
2487 }
2488
2489 return D3DPOOL_DEFAULT;
2490}
2491
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002492bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002493 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002494{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002495 RECT rect;
2496 rect.left = sourceRect.x;
2497 rect.top = sourceRect.y;
2498 rect.right = sourceRect.x + sourceRect.width;
2499 rect.bottom = sourceRect.y + sourceRect.height;
2500
2501 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002502}
2503
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002504bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002505 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002506{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002507 RECT rect;
2508 rect.left = sourceRect.x;
2509 rect.top = sourceRect.y;
2510 rect.right = sourceRect.x + sourceRect.width;
2511 rect.bottom = sourceRect.y + sourceRect.height;
2512
2513 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002514}
2515
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002516bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2517 bool blitRenderTarget, bool blitDepthStencil)
2518{
2519 endScene();
2520
2521 if (blitRenderTarget)
2522 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002523 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2524 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2525 RenderTarget9 *readRenderTarget = NULL;
2526 RenderTarget9 *drawRenderTarget = NULL;
2527 IDirect3DSurface9* readSurface = NULL;
2528 IDirect3DSurface9* drawSurface = NULL;
2529
2530 if (readBuffer)
2531 {
2532 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2533 }
2534 if (drawBuffer)
2535 {
2536 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2537 }
2538
2539 if (readRenderTarget)
2540 {
2541 readSurface = readRenderTarget->getSurface();
2542 }
2543 if (drawRenderTarget)
2544 {
2545 drawSurface = drawRenderTarget->getSurface();
2546 }
2547
2548 if (!readSurface || !drawSurface)
2549 {
2550 ERR("Failed to retrieve the render target.");
2551 return error(GL_OUT_OF_MEMORY, false);
2552 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002553
2554 RECT srcRect, dstRect;
2555 RECT *srcRectPtr = NULL;
2556 RECT *dstRectPtr = NULL;
2557
2558 if (readRect)
2559 {
2560 srcRect.left = readRect->x;
2561 srcRect.right = readRect->x + readRect->width;
2562 srcRect.top = readRect->y;
2563 srcRect.bottom = readRect->y + readRect->height;
2564 srcRectPtr = &srcRect;
2565 }
2566
2567 if (drawRect)
2568 {
2569 dstRect.left = drawRect->x;
2570 dstRect.right = drawRect->x + drawRect->width;
2571 dstRect.top = drawRect->y;
2572 dstRect.bottom = drawRect->y + drawRect->height;
2573 dstRectPtr = &dstRect;
2574 }
2575
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002576 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002577
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002578 readSurface->Release();
2579 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002580
2581 if (FAILED(result))
2582 {
2583 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2584 return false;
2585 }
2586 }
2587
2588 if (blitDepthStencil)
2589 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002590 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2591 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2592 RenderTarget9 *readDepthStencil = NULL;
2593 RenderTarget9 *drawDepthStencil = NULL;
2594 IDirect3DSurface9* readSurface = NULL;
2595 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002596
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002597 if (readBuffer)
2598 {
2599 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2600 }
2601 if (drawBuffer)
2602 {
2603 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2604 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002605
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002606 if (readDepthStencil)
2607 {
2608 readSurface = readDepthStencil->getSurface();
2609 }
2610 if (drawDepthStencil)
2611 {
2612 drawSurface = drawDepthStencil->getSurface();
2613 }
2614
2615 if (!readSurface || !drawSurface)
2616 {
2617 ERR("Failed to retrieve the render target.");
2618 return error(GL_OUT_OF_MEMORY, false);
2619 }
2620
2621 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2622
2623 readSurface->Release();
2624 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002625
2626 if (FAILED(result))
2627 {
2628 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2629 return false;
2630 }
2631 }
2632
2633 return true;
2634}
2635
2636void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2637 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2638{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002639 RenderTarget9 *renderTarget = NULL;
2640 IDirect3DSurface9 *surface = NULL;
2641 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2642
2643 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002644 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002645 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2646 }
2647
2648 if (renderTarget)
2649 {
2650 surface = renderTarget->getSurface();
2651 }
2652
2653 if (!surface)
2654 {
2655 // context must be lost
2656 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002657 }
2658
2659 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002660 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002661
2662 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2663 {
2664 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002665 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002666 return error(GL_OUT_OF_MEMORY);
2667 }
2668
2669 HRESULT result;
2670 IDirect3DSurface9 *systemSurface = NULL;
2671 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2672 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2673 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2674 if (directToPixels)
2675 {
2676 // Use the pixels ptr as a shared handle to write directly into client's memory
2677 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2678 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2679 if (FAILED(result))
2680 {
2681 // Try again without the shared handle
2682 directToPixels = false;
2683 }
2684 }
2685
2686 if (!directToPixels)
2687 {
2688 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2689 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2690 if (FAILED(result))
2691 {
2692 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002693 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002694 return error(GL_OUT_OF_MEMORY);
2695 }
2696 }
2697
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002698 result = mDevice->GetRenderTargetData(surface, systemSurface);
2699 surface->Release();
2700 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002701
2702 if (FAILED(result))
2703 {
2704 systemSurface->Release();
2705
2706 // It turns out that D3D will sometimes produce more error
2707 // codes than those documented.
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +00002708 if (d3d9::checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002709 return error(GL_OUT_OF_MEMORY);
2710 else
2711 {
2712 UNREACHABLE();
2713 return;
2714 }
2715
2716 }
2717
2718 if (directToPixels)
2719 {
2720 systemSurface->Release();
2721 return;
2722 }
2723
2724 RECT rect;
2725 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2726 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2727 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2728 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2729
2730 D3DLOCKED_RECT lock;
2731 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2732
2733 if (FAILED(result))
2734 {
2735 UNREACHABLE();
2736 systemSurface->Release();
2737
2738 return; // No sensible error to generate
2739 }
2740
2741 unsigned char *dest = (unsigned char*)pixels;
2742 unsigned short *dest16 = (unsigned short*)pixels;
2743
2744 unsigned char *source;
2745 int inputPitch;
2746 if (packReverseRowOrder)
2747 {
2748 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2749 inputPitch = -lock.Pitch;
2750 }
2751 else
2752 {
2753 source = (unsigned char*)lock.pBits;
2754 inputPitch = lock.Pitch;
2755 }
2756
2757 unsigned int fastPixelSize = 0;
2758
2759 if (desc.Format == D3DFMT_A8R8G8B8 &&
2760 format == GL_BGRA_EXT &&
2761 type == GL_UNSIGNED_BYTE)
2762 {
2763 fastPixelSize = 4;
2764 }
2765 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2766 format == GL_BGRA_EXT &&
2767 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2768 (desc.Format == D3DFMT_A1R5G5B5 &&
2769 format == GL_BGRA_EXT &&
2770 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2771 {
2772 fastPixelSize = 2;
2773 }
2774 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2775 format == GL_RGBA &&
2776 type == GL_HALF_FLOAT_OES)
2777 {
2778 fastPixelSize = 8;
2779 }
2780 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2781 format == GL_RGBA &&
2782 type == GL_FLOAT)
2783 {
2784 fastPixelSize = 16;
2785 }
2786
2787 for (int j = 0; j < rect.bottom - rect.top; j++)
2788 {
2789 if (fastPixelSize != 0)
2790 {
2791 // Fast path for formats which require no translation:
2792 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2793 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2794 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2795 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2796 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2797 //
2798 // Note that buffers with no alpha go through the slow path below.
2799 memcpy(dest + j * outputPitch,
2800 source + j * inputPitch,
2801 (rect.right - rect.left) * fastPixelSize);
2802 continue;
2803 }
2804
2805 for (int i = 0; i < rect.right - rect.left; i++)
2806 {
2807 float r;
2808 float g;
2809 float b;
2810 float a;
2811
2812 switch (desc.Format)
2813 {
2814 case D3DFMT_R5G6B5:
2815 {
2816 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2817
2818 a = 1.0f;
2819 b = (rgb & 0x001F) * (1.0f / 0x001F);
2820 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2821 r = (rgb & 0xF800) * (1.0f / 0xF800);
2822 }
2823 break;
2824 case D3DFMT_A1R5G5B5:
2825 {
2826 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2827
2828 a = (argb & 0x8000) ? 1.0f : 0.0f;
2829 b = (argb & 0x001F) * (1.0f / 0x001F);
2830 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2831 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2832 }
2833 break;
2834 case D3DFMT_A8R8G8B8:
2835 {
2836 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2837
2838 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2839 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2840 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2841 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2842 }
2843 break;
2844 case D3DFMT_X8R8G8B8:
2845 {
2846 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2847
2848 a = 1.0f;
2849 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2850 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2851 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2852 }
2853 break;
2854 case D3DFMT_A2R10G10B10:
2855 {
2856 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2857
2858 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2859 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2860 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2861 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2862 }
2863 break;
2864 case D3DFMT_A32B32G32R32F:
2865 {
2866 // float formats in D3D are stored rgba, rather than the other way round
2867 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2868 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2869 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2870 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2871 }
2872 break;
2873 case D3DFMT_A16B16G16R16F:
2874 {
2875 // float formats in D3D are stored rgba, rather than the other way round
2876 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2877 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2878 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2879 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2880 }
2881 break;
2882 default:
2883 UNIMPLEMENTED(); // FIXME
2884 UNREACHABLE();
2885 return;
2886 }
2887
2888 switch (format)
2889 {
2890 case GL_RGBA:
2891 switch (type)
2892 {
2893 case GL_UNSIGNED_BYTE:
2894 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2895 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2896 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2897 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2898 break;
2899 default: UNREACHABLE();
2900 }
2901 break;
2902 case GL_BGRA_EXT:
2903 switch (type)
2904 {
2905 case GL_UNSIGNED_BYTE:
2906 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2907 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2908 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2909 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2910 break;
2911 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2912 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2913 // this type is packed as follows:
2914 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2915 // --------------------------------------------------------------------------------
2916 // | 4th | 3rd | 2nd | 1st component |
2917 // --------------------------------------------------------------------------------
2918 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2919 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2920 ((unsigned short)(15 * a + 0.5f) << 12)|
2921 ((unsigned short)(15 * r + 0.5f) << 8) |
2922 ((unsigned short)(15 * g + 0.5f) << 4) |
2923 ((unsigned short)(15 * b + 0.5f) << 0);
2924 break;
2925 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2926 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2927 // this type is packed as follows:
2928 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2929 // --------------------------------------------------------------------------------
2930 // | 4th | 3rd | 2nd | 1st component |
2931 // --------------------------------------------------------------------------------
2932 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2933 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2934 ((unsigned short)( a + 0.5f) << 15) |
2935 ((unsigned short)(31 * r + 0.5f) << 10) |
2936 ((unsigned short)(31 * g + 0.5f) << 5) |
2937 ((unsigned short)(31 * b + 0.5f) << 0);
2938 break;
2939 default: UNREACHABLE();
2940 }
2941 break;
2942 case GL_RGB:
2943 switch (type)
2944 {
2945 case GL_UNSIGNED_SHORT_5_6_5:
2946 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2947 ((unsigned short)(31 * b + 0.5f) << 0) |
2948 ((unsigned short)(63 * g + 0.5f) << 5) |
2949 ((unsigned short)(31 * r + 0.5f) << 11);
2950 break;
2951 case GL_UNSIGNED_BYTE:
2952 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2953 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2954 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2955 break;
2956 default: UNREACHABLE();
2957 }
2958 break;
2959 default: UNREACHABLE();
2960 }
2961 }
2962 }
2963
2964 systemSurface->UnlockRect();
2965
2966 systemSurface->Release();
2967}
2968
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002969RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2970{
2971 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2972 IDirect3DSurface9 *surface = NULL;
2973 if (depth)
2974 {
2975 surface = swapChain9->getDepthStencil();
2976 }
2977 else
2978 {
2979 surface = swapChain9->getRenderTarget();
2980 }
2981
2982 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2983
2984 return renderTarget;
2985}
2986
2987RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2988{
2989 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2990 return renderTarget;
2991}
2992
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002993ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002994{
2995 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002996
2997 switch (type)
2998 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002999 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003000 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003001 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003002 if (vshader)
3003 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003004 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003005 }
3006 }
3007 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003008 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003009 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003010 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003011 if (pshader)
3012 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003013 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003014 }
3015 }
3016 break;
3017 default:
3018 UNREACHABLE();
3019 break;
3020 }
3021
3022 return executable;
3023}
3024
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003025ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003026{
3027 const char *profile = NULL;
3028
3029 switch (type)
3030 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003031 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003032 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3033 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003034 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003035 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3036 break;
3037 default:
3038 UNREACHABLE();
3039 return NULL;
3040 }
3041
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00003042 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003043 if (!binary)
3044 return NULL;
3045
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003046 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003047 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003048
3049 return executable;
3050}
3051
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003052bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3053{
3054 return mBlit->boxFilter(source, dest);
3055}
3056
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003057D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003058{
3059 if (mD3d9Ex != NULL)
3060 {
3061 return D3DPOOL_DEFAULT;
3062 }
3063 else
3064 {
3065 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3066 {
3067 return D3DPOOL_MANAGED;
3068 }
3069 }
3070
3071 return D3DPOOL_DEFAULT;
3072}
3073
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003074bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3075{
3076 if (source && dest)
3077 {
3078 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003079
3080 if (fromManaged)
3081 {
3082 D3DSURFACE_DESC desc;
3083 source->GetDesc(&desc);
3084
3085 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003086 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003087
3088 if (SUCCEEDED(result))
3089 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003090 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003091 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003092 surf->Release();
3093 }
3094 }
3095 else
3096 {
3097 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003098 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003099 }
3100
3101 if (FAILED(result))
3102 {
3103 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3104 return false;
3105 }
3106 }
3107
3108 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003109}
3110
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003111Image *Renderer9::createImage()
3112{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003113 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003114}
3115
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003116void Renderer9::generateMipmap(Image *dest, Image *src)
3117{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003118 Image9 *src9 = Image9::makeImage9(src);
3119 Image9 *dst9 = Image9::makeImage9(dest);
3120 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003121}
3122
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003123TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3124{
3125 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3126 return new TextureStorage9_2D(this, swapChain9);
3127}
3128
3129TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3130{
3131 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3132}
3133
3134TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3135{
3136 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3137}
3138
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003139}