blob: 930a15d2144e036db64f3364863d6c3688cdaf05 [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 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000639 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000640 }
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
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002019void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002020{
2021 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002022 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002023}
2024
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002025bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002026{
2027 return mDeviceLost;
2028}
2029
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002030// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002031bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002032{
2033 bool isLost = false;
2034
2035 if (mDeviceEx)
2036 {
2037 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
2038 }
2039 else if (mDevice)
2040 {
2041 isLost = FAILED(mDevice->TestCooperativeLevel());
2042 }
2043 else
2044 {
2045 // No device yet, so no reset required
2046 }
2047
2048 if (isLost)
2049 {
2050 // ensure we note the device loss --
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002051 // Note that we don't want to clear the device loss status here
2052 // -- this needs to be done by resetDevice
2053 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002054 if (notify)
2055 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002056 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002057 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002058 }
2059
2060 return isLost;
2061}
2062
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002063bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002064{
2065 HRESULT status = D3D_OK;
2066
2067 if (mDeviceEx)
2068 {
2069 status = mDeviceEx->CheckDeviceState(NULL);
2070 }
2071 else if (mDevice)
2072 {
2073 status = mDevice->TestCooperativeLevel();
2074 }
2075
2076 switch (status)
2077 {
2078 case D3DERR_DEVICENOTRESET:
2079 case D3DERR_DEVICEHUNG:
2080 return true;
2081 default:
2082 return false;
2083 }
2084}
2085
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002086bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002087{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002088 releaseDeviceResources();
2089
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002090 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2091
2092 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002093 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002094 int attempts = 3;
2095
2096 while (lost && attempts > 0)
2097 {
2098 if (mDeviceEx)
2099 {
2100 Sleep(500); // Give the graphics driver some CPU time
2101 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2102 }
2103 else
2104 {
2105 result = mDevice->TestCooperativeLevel();
2106 while (result == D3DERR_DEVICELOST)
2107 {
2108 Sleep(100); // Give the graphics driver some CPU time
2109 result = mDevice->TestCooperativeLevel();
2110 }
2111
2112 if (result == D3DERR_DEVICENOTRESET)
2113 {
2114 result = mDevice->Reset(&presentParameters);
2115 }
2116 }
2117
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002118 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002119 attempts --;
2120 }
2121
2122 if (FAILED(result))
2123 {
2124 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2125 return false;
2126 }
2127
2128 // reset device defaults
2129 initializeDevice();
2130 mDeviceLost = false;
2131
2132 return true;
2133}
2134
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002135DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002136{
2137 return mAdapterIdentifier.VendorId;
2138}
2139
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002140std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002141{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002142 std::ostringstream rendererString;
2143
2144 rendererString << mAdapterIdentifier.Description;
2145 if (getShareHandleSupport())
2146 {
2147 rendererString << " Direct3D9Ex";
2148 }
2149 else
2150 {
2151 rendererString << " Direct3D9";
2152 }
2153
2154 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2155 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2156
2157 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002158}
2159
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002160GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002161{
2162 return mAdapterIdentifier.DeviceIdentifier;
2163}
2164
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002165void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002166{
2167 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
2168 {
2169 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
2170 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2171
2172 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
2173 }
2174}
2175
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002176bool Renderer9::getBGRATextureSupport() const
2177{
2178 // DirectX 9 always supports BGRA
2179 return true;
2180}
2181
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002182bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002183{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002184 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002185}
2186
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002187bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002188{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002189 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002190}
2191
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002192bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002193{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002194 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002195}
2196
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002197bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002198{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002199 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002200}
2201
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002202bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002203{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002204 *filtering = mFloat32FilterSupport;
2205 *renderable = mFloat32RenderSupport;
2206 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002207}
2208
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002209bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002210{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002211 *filtering = mFloat16FilterSupport;
2212 *renderable = mFloat16RenderSupport;
2213 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002214}
2215
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002216bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002217{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002218 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002219}
2220
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002221bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002222{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002223 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002224}
2225
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002226bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002227{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002228 return mSupportsTextureFilterAnisotropy;
2229}
2230
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002231float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002232{
2233 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002234 {
2235 return mDeviceCaps.MaxAnisotropy;
2236 }
2237 return 1.0f;
2238}
2239
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002240bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002241{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002242 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002243}
2244
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002245unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002246{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002247 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2248 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002249}
2250
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002251unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2252{
2253 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2254}
2255
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002256int Renderer9::getMaxVertexUniformVectors() const
2257{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002258 return MAX_VERTEX_UNIFORM_VECTORS;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002259}
2260
2261int Renderer9::getMaxFragmentUniformVectors() const
2262{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002263 return getMajorShaderModel() >= 3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002264}
2265
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002266int Renderer9::getMaxVaryingVectors() const
2267{
2268 return getMajorShaderModel() >= 3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2269}
2270
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002271bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002272{
2273 return mSupportsNonPower2Textures;
2274}
2275
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002276bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002277{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002278 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002279}
2280
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002281bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002282{
2283 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2284}
2285
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002286bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002287{
2288 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002289 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002290}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002291
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002292bool Renderer9::getDerivativeInstructionSupport() const
2293{
2294 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2295}
2296
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002297int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002298{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002299 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002300}
2301
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002302float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002303{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002304 // Point size clamped at 1.0f for SM2
2305 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002306}
2307
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002308int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002309{
2310 return (int)mDeviceCaps.MaxTextureWidth;
2311}
2312
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002313int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002314{
2315 return (int)mDeviceCaps.MaxTextureHeight;
2316}
2317
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002318bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002319{
2320 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2321}
2322
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002323DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002324{
2325 return mDeviceCaps.DeclTypes;
2326}
2327
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002328int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002329{
2330 return mMinSwapInterval;
2331}
2332
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002333int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002334{
2335 return mMaxSwapInterval;
2336}
2337
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002338int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002339{
2340 return mMaxSupportedSamples;
2341}
2342
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002343int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002344{
2345 if (requested == 0)
2346 {
2347 return requested;
2348 }
2349
2350 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2351 if (itr == mMultiSampleSupport.end())
2352 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002353 if (format == D3DFMT_UNKNOWN)
2354 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002355 return -1;
2356 }
2357
2358 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2359 {
2360 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2361 {
2362 return i;
2363 }
2364 }
2365
2366 return -1;
2367}
2368
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002369D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2370{
2371 switch (internalformat)
2372 {
2373 case GL_DEPTH_COMPONENT16:
2374 case GL_DEPTH_COMPONENT32_OES:
2375 case GL_DEPTH24_STENCIL8_OES:
2376 return D3DFMT_INTZ;
2377 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2378 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2379 return D3DFMT_DXT1;
2380 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2381 return D3DFMT_DXT3;
2382 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2383 return D3DFMT_DXT5;
2384 case GL_RGBA32F_EXT:
2385 case GL_RGB32F_EXT:
2386 case GL_ALPHA32F_EXT:
2387 case GL_LUMINANCE32F_EXT:
2388 case GL_LUMINANCE_ALPHA32F_EXT:
2389 return D3DFMT_A32B32G32R32F;
2390 case GL_RGBA16F_EXT:
2391 case GL_RGB16F_EXT:
2392 case GL_ALPHA16F_EXT:
2393 case GL_LUMINANCE16F_EXT:
2394 case GL_LUMINANCE_ALPHA16F_EXT:
2395 return D3DFMT_A16B16G16R16F;
2396 case GL_LUMINANCE8_EXT:
2397 if (getLuminanceTextureSupport())
2398 {
2399 return D3DFMT_L8;
2400 }
2401 break;
2402 case GL_LUMINANCE8_ALPHA8_EXT:
2403 if (getLuminanceAlphaTextureSupport())
2404 {
2405 return D3DFMT_A8L8;
2406 }
2407 break;
2408 case GL_RGB8_OES:
2409 case GL_RGB565:
2410 return D3DFMT_X8R8G8B8;
2411 }
2412
2413 return D3DFMT_A8R8G8B8;
2414}
2415
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002416bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002417{
2418 bool result = false;
2419
2420 if (source && dest)
2421 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002422 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2423 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002424
2425 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002426 for (int i = 0; i < levels; ++i)
2427 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002428 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2429 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002430
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002431 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002432
2433 if (srcSurf) srcSurf->Release();
2434 if (dstSurf) dstSurf->Release();
2435
2436 if (!result)
2437 return false;
2438 }
2439 }
2440
2441 return result;
2442}
2443
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002444bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002445{
2446 bool result = false;
2447
2448 if (source && dest)
2449 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002450 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2451 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002452 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002453 for (int f = 0; f < 6; f++)
2454 {
2455 for (int i = 0; i < levels; i++)
2456 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002457 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2458 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002459
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002460 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002461
2462 if (srcSurf) srcSurf->Release();
2463 if (dstSurf) dstSurf->Release();
2464
2465 if (!result)
2466 return false;
2467 }
2468 }
2469 }
2470
2471 return result;
2472}
2473
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002474D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002475{
2476 if (mD3d9Ex != NULL)
2477 {
2478 return D3DPOOL_DEFAULT;
2479 }
2480 else
2481 {
2482 if (!(usage & D3DUSAGE_DYNAMIC))
2483 {
2484 return D3DPOOL_MANAGED;
2485 }
2486 }
2487
2488 return D3DPOOL_DEFAULT;
2489}
2490
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002491bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002492 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002493{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002494 RECT rect;
2495 rect.left = sourceRect.x;
2496 rect.top = sourceRect.y;
2497 rect.right = sourceRect.x + sourceRect.width;
2498 rect.bottom = sourceRect.y + sourceRect.height;
2499
2500 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002501}
2502
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002503bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002504 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002505{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002506 RECT rect;
2507 rect.left = sourceRect.x;
2508 rect.top = sourceRect.y;
2509 rect.right = sourceRect.x + sourceRect.width;
2510 rect.bottom = sourceRect.y + sourceRect.height;
2511
2512 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002513}
2514
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002515bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2516 bool blitRenderTarget, bool blitDepthStencil)
2517{
2518 endScene();
2519
2520 if (blitRenderTarget)
2521 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002522 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2523 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2524 RenderTarget9 *readRenderTarget = NULL;
2525 RenderTarget9 *drawRenderTarget = NULL;
2526 IDirect3DSurface9* readSurface = NULL;
2527 IDirect3DSurface9* drawSurface = NULL;
2528
2529 if (readBuffer)
2530 {
2531 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2532 }
2533 if (drawBuffer)
2534 {
2535 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2536 }
2537
2538 if (readRenderTarget)
2539 {
2540 readSurface = readRenderTarget->getSurface();
2541 }
2542 if (drawRenderTarget)
2543 {
2544 drawSurface = drawRenderTarget->getSurface();
2545 }
2546
2547 if (!readSurface || !drawSurface)
2548 {
2549 ERR("Failed to retrieve the render target.");
2550 return error(GL_OUT_OF_MEMORY, false);
2551 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002552
2553 RECT srcRect, dstRect;
2554 RECT *srcRectPtr = NULL;
2555 RECT *dstRectPtr = NULL;
2556
2557 if (readRect)
2558 {
2559 srcRect.left = readRect->x;
2560 srcRect.right = readRect->x + readRect->width;
2561 srcRect.top = readRect->y;
2562 srcRect.bottom = readRect->y + readRect->height;
2563 srcRectPtr = &srcRect;
2564 }
2565
2566 if (drawRect)
2567 {
2568 dstRect.left = drawRect->x;
2569 dstRect.right = drawRect->x + drawRect->width;
2570 dstRect.top = drawRect->y;
2571 dstRect.bottom = drawRect->y + drawRect->height;
2572 dstRectPtr = &dstRect;
2573 }
2574
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002575 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002576
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002577 readSurface->Release();
2578 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002579
2580 if (FAILED(result))
2581 {
2582 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2583 return false;
2584 }
2585 }
2586
2587 if (blitDepthStencil)
2588 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002589 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2590 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2591 RenderTarget9 *readDepthStencil = NULL;
2592 RenderTarget9 *drawDepthStencil = NULL;
2593 IDirect3DSurface9* readSurface = NULL;
2594 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002595
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002596 if (readBuffer)
2597 {
2598 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2599 }
2600 if (drawBuffer)
2601 {
2602 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2603 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002604
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002605 if (readDepthStencil)
2606 {
2607 readSurface = readDepthStencil->getSurface();
2608 }
2609 if (drawDepthStencil)
2610 {
2611 drawSurface = drawDepthStencil->getSurface();
2612 }
2613
2614 if (!readSurface || !drawSurface)
2615 {
2616 ERR("Failed to retrieve the render target.");
2617 return error(GL_OUT_OF_MEMORY, false);
2618 }
2619
2620 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2621
2622 readSurface->Release();
2623 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002624
2625 if (FAILED(result))
2626 {
2627 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2628 return false;
2629 }
2630 }
2631
2632 return true;
2633}
2634
2635void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2636 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2637{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002638 RenderTarget9 *renderTarget = NULL;
2639 IDirect3DSurface9 *surface = NULL;
2640 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2641
2642 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002643 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002644 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2645 }
2646
2647 if (renderTarget)
2648 {
2649 surface = renderTarget->getSurface();
2650 }
2651
2652 if (!surface)
2653 {
2654 // context must be lost
2655 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002656 }
2657
2658 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002659 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002660
2661 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2662 {
2663 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002664 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002665 return error(GL_OUT_OF_MEMORY);
2666 }
2667
2668 HRESULT result;
2669 IDirect3DSurface9 *systemSurface = NULL;
2670 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2671 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2672 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2673 if (directToPixels)
2674 {
2675 // Use the pixels ptr as a shared handle to write directly into client's memory
2676 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2677 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2678 if (FAILED(result))
2679 {
2680 // Try again without the shared handle
2681 directToPixels = false;
2682 }
2683 }
2684
2685 if (!directToPixels)
2686 {
2687 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2688 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2689 if (FAILED(result))
2690 {
2691 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002692 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002693 return error(GL_OUT_OF_MEMORY);
2694 }
2695 }
2696
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002697 result = mDevice->GetRenderTargetData(surface, systemSurface);
2698 surface->Release();
2699 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002700
2701 if (FAILED(result))
2702 {
2703 systemSurface->Release();
2704
2705 // It turns out that D3D will sometimes produce more error
2706 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002707 if (d3d9::isDeviceLostError(result))
2708 {
2709 notifyDeviceLost();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002710 return error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002711 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002712 else
2713 {
2714 UNREACHABLE();
2715 return;
2716 }
2717
2718 }
2719
2720 if (directToPixels)
2721 {
2722 systemSurface->Release();
2723 return;
2724 }
2725
2726 RECT rect;
2727 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2728 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2729 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2730 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2731
2732 D3DLOCKED_RECT lock;
2733 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2734
2735 if (FAILED(result))
2736 {
2737 UNREACHABLE();
2738 systemSurface->Release();
2739
2740 return; // No sensible error to generate
2741 }
2742
2743 unsigned char *dest = (unsigned char*)pixels;
2744 unsigned short *dest16 = (unsigned short*)pixels;
2745
2746 unsigned char *source;
2747 int inputPitch;
2748 if (packReverseRowOrder)
2749 {
2750 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2751 inputPitch = -lock.Pitch;
2752 }
2753 else
2754 {
2755 source = (unsigned char*)lock.pBits;
2756 inputPitch = lock.Pitch;
2757 }
2758
2759 unsigned int fastPixelSize = 0;
2760
2761 if (desc.Format == D3DFMT_A8R8G8B8 &&
2762 format == GL_BGRA_EXT &&
2763 type == GL_UNSIGNED_BYTE)
2764 {
2765 fastPixelSize = 4;
2766 }
2767 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2768 format == GL_BGRA_EXT &&
2769 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2770 (desc.Format == D3DFMT_A1R5G5B5 &&
2771 format == GL_BGRA_EXT &&
2772 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2773 {
2774 fastPixelSize = 2;
2775 }
2776 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2777 format == GL_RGBA &&
2778 type == GL_HALF_FLOAT_OES)
2779 {
2780 fastPixelSize = 8;
2781 }
2782 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2783 format == GL_RGBA &&
2784 type == GL_FLOAT)
2785 {
2786 fastPixelSize = 16;
2787 }
2788
2789 for (int j = 0; j < rect.bottom - rect.top; j++)
2790 {
2791 if (fastPixelSize != 0)
2792 {
2793 // Fast path for formats which require no translation:
2794 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2795 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2796 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2797 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2798 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2799 //
2800 // Note that buffers with no alpha go through the slow path below.
2801 memcpy(dest + j * outputPitch,
2802 source + j * inputPitch,
2803 (rect.right - rect.left) * fastPixelSize);
2804 continue;
2805 }
2806
2807 for (int i = 0; i < rect.right - rect.left; i++)
2808 {
2809 float r;
2810 float g;
2811 float b;
2812 float a;
2813
2814 switch (desc.Format)
2815 {
2816 case D3DFMT_R5G6B5:
2817 {
2818 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2819
2820 a = 1.0f;
2821 b = (rgb & 0x001F) * (1.0f / 0x001F);
2822 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2823 r = (rgb & 0xF800) * (1.0f / 0xF800);
2824 }
2825 break;
2826 case D3DFMT_A1R5G5B5:
2827 {
2828 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2829
2830 a = (argb & 0x8000) ? 1.0f : 0.0f;
2831 b = (argb & 0x001F) * (1.0f / 0x001F);
2832 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2833 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2834 }
2835 break;
2836 case D3DFMT_A8R8G8B8:
2837 {
2838 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2839
2840 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2841 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2842 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2843 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2844 }
2845 break;
2846 case D3DFMT_X8R8G8B8:
2847 {
2848 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2849
2850 a = 1.0f;
2851 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2852 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2853 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2854 }
2855 break;
2856 case D3DFMT_A2R10G10B10:
2857 {
2858 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2859
2860 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2861 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2862 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2863 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2864 }
2865 break;
2866 case D3DFMT_A32B32G32R32F:
2867 {
2868 // float formats in D3D are stored rgba, rather than the other way round
2869 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2870 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2871 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2872 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2873 }
2874 break;
2875 case D3DFMT_A16B16G16R16F:
2876 {
2877 // float formats in D3D are stored rgba, rather than the other way round
2878 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2879 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2880 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2881 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2882 }
2883 break;
2884 default:
2885 UNIMPLEMENTED(); // FIXME
2886 UNREACHABLE();
2887 return;
2888 }
2889
2890 switch (format)
2891 {
2892 case GL_RGBA:
2893 switch (type)
2894 {
2895 case GL_UNSIGNED_BYTE:
2896 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2897 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2898 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2899 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2900 break;
2901 default: UNREACHABLE();
2902 }
2903 break;
2904 case GL_BGRA_EXT:
2905 switch (type)
2906 {
2907 case GL_UNSIGNED_BYTE:
2908 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2909 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2910 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2911 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2912 break;
2913 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2914 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2915 // this type is packed as follows:
2916 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2917 // --------------------------------------------------------------------------------
2918 // | 4th | 3rd | 2nd | 1st component |
2919 // --------------------------------------------------------------------------------
2920 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2921 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2922 ((unsigned short)(15 * a + 0.5f) << 12)|
2923 ((unsigned short)(15 * r + 0.5f) << 8) |
2924 ((unsigned short)(15 * g + 0.5f) << 4) |
2925 ((unsigned short)(15 * b + 0.5f) << 0);
2926 break;
2927 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2928 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2929 // this type is packed as follows:
2930 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2931 // --------------------------------------------------------------------------------
2932 // | 4th | 3rd | 2nd | 1st component |
2933 // --------------------------------------------------------------------------------
2934 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2935 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2936 ((unsigned short)( a + 0.5f) << 15) |
2937 ((unsigned short)(31 * r + 0.5f) << 10) |
2938 ((unsigned short)(31 * g + 0.5f) << 5) |
2939 ((unsigned short)(31 * b + 0.5f) << 0);
2940 break;
2941 default: UNREACHABLE();
2942 }
2943 break;
2944 case GL_RGB:
2945 switch (type)
2946 {
2947 case GL_UNSIGNED_SHORT_5_6_5:
2948 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2949 ((unsigned short)(31 * b + 0.5f) << 0) |
2950 ((unsigned short)(63 * g + 0.5f) << 5) |
2951 ((unsigned short)(31 * r + 0.5f) << 11);
2952 break;
2953 case GL_UNSIGNED_BYTE:
2954 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2955 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2956 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2957 break;
2958 default: UNREACHABLE();
2959 }
2960 break;
2961 default: UNREACHABLE();
2962 }
2963 }
2964 }
2965
2966 systemSurface->UnlockRect();
2967
2968 systemSurface->Release();
2969}
2970
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002971RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2972{
2973 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2974 IDirect3DSurface9 *surface = NULL;
2975 if (depth)
2976 {
2977 surface = swapChain9->getDepthStencil();
2978 }
2979 else
2980 {
2981 surface = swapChain9->getRenderTarget();
2982 }
2983
2984 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2985
2986 return renderTarget;
2987}
2988
2989RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2990{
2991 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2992 return renderTarget;
2993}
2994
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002995ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002996{
2997 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002998
2999 switch (type)
3000 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003001 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003002 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003003 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003004 if (vshader)
3005 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003006 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003007 }
3008 }
3009 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003010 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003011 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003012 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003013 if (pshader)
3014 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003015 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003016 }
3017 }
3018 break;
3019 default:
3020 UNREACHABLE();
3021 break;
3022 }
3023
3024 return executable;
3025}
3026
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003027ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003028{
3029 const char *profile = NULL;
3030
3031 switch (type)
3032 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003033 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003034 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3035 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003036 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003037 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3038 break;
3039 default:
3040 UNREACHABLE();
3041 return NULL;
3042 }
3043
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00003044 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003045 if (!binary)
3046 return NULL;
3047
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003048 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003049 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003050
3051 return executable;
3052}
3053
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003054bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3055{
3056 return mBlit->boxFilter(source, dest);
3057}
3058
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003059D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003060{
3061 if (mD3d9Ex != NULL)
3062 {
3063 return D3DPOOL_DEFAULT;
3064 }
3065 else
3066 {
3067 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3068 {
3069 return D3DPOOL_MANAGED;
3070 }
3071 }
3072
3073 return D3DPOOL_DEFAULT;
3074}
3075
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003076bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3077{
3078 if (source && dest)
3079 {
3080 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003081
3082 if (fromManaged)
3083 {
3084 D3DSURFACE_DESC desc;
3085 source->GetDesc(&desc);
3086
3087 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003088 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003089
3090 if (SUCCEEDED(result))
3091 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003092 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003093 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003094 surf->Release();
3095 }
3096 }
3097 else
3098 {
3099 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003100 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003101 }
3102
3103 if (FAILED(result))
3104 {
3105 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3106 return false;
3107 }
3108 }
3109
3110 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003111}
3112
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003113Image *Renderer9::createImage()
3114{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003115 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003116}
3117
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003118void Renderer9::generateMipmap(Image *dest, Image *src)
3119{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003120 Image9 *src9 = Image9::makeImage9(src);
3121 Image9 *dst9 = Image9::makeImage9(dest);
3122 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003123}
3124
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003125TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3126{
3127 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3128 return new TextureStorage9_2D(this, swapChain9);
3129}
3130
3131TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3132{
3133 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3134}
3135
3136TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3137{
3138 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3139}
3140
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003141}