blob: be10afdbcef0f555982a28d3c45e82b62365b351 [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00008
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009#include "common/debug.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000010#include "libGLESv2/main.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/mathutil.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000014#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000015#include "libGLESv2/Program.h"
16#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000017#include "libGLESv2/renderer/IndexDataManager.h"
18#include "libGLESv2/renderer/VertexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000019#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000020#include "libGLESv2/renderer/renderer9_utils.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000021#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000022#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com34da3972012-12-20 21:10:29 +000023#include "libGLESv2/renderer/TextureStorage9.h"
daniel@transgaming.com4ba24062012-12-20 20:54:24 +000024#include "libGLESv2/renderer/Image9.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000025#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000026#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com3f255b42012-12-20 21:07:35 +000027#include "libGLESv2/renderer/VertexBuffer9.h"
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +000028#include "libGLESv2/renderer/IndexBuffer9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000029
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000030#include <sstream>
31
daniel@transgaming.com621ce052012-10-31 17:52:29 +000032// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
33#define REF_RAST 0
34
35// The "Debug This Pixel..." feature in PIX often fails when using the
36// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
37// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
38#if !defined(ANGLE_ENABLE_D3D9EX)
39// Enables use of the IDirect3D9Ex interface, when available
40#define ANGLE_ENABLE_D3D9EX 1
41#endif // !defined(ANGLE_ENABLE_D3D9EX)
42
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000043namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000044{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000045static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000046 {
47 D3DFMT_A1R5G5B5,
48 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
49 D3DFMT_A8R8G8B8,
50 D3DFMT_R5G6B5,
51 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
52 D3DFMT_X8R8G8B8
53 };
54
daniel@transgaming.com222ee082012-11-28 19:31:49 +000055static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000056 {
57 D3DFMT_UNKNOWN,
58 // D3DFMT_D16_LOCKABLE,
59 D3DFMT_D32,
60 // D3DFMT_D15S1,
61 D3DFMT_D24S8,
62 D3DFMT_D24X8,
63 // D3DFMT_D24X4S4,
64 D3DFMT_D16,
65 // D3DFMT_D32F_LOCKABLE,
66 // D3DFMT_D24FS8
67 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000068
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000069enum
70{
71 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
72};
73
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000074Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000075{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000076 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000077
78 mD3d9 = NULL;
79 mD3d9Ex = NULL;
80 mDevice = NULL;
81 mDeviceEx = NULL;
82 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000083 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000084
85 mAdapter = D3DADAPTER_DEFAULT;
86
87 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
88 mDeviceType = D3DDEVTYPE_REF;
89 #else
90 mDeviceType = D3DDEVTYPE_HAL;
91 #endif
92
93 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000094
95 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000096
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000097 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000098
99 mVertexDataManager = NULL;
100 mIndexDataManager = NULL;
101 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000102
103 mMaxNullColorbufferLRU = 0;
104 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
105 {
106 mNullColorbufferCache[i].lruCount = 0;
107 mNullColorbufferCache[i].width = 0;
108 mNullColorbufferCache[i].height = 0;
109 mNullColorbufferCache[i].buffer = NULL;
110 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000111}
112
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000113Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000114{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000115 releaseDeviceResources();
116
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000117 if (mDevice)
118 {
119 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000120 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000121 {
122 resetDevice();
123 }
124
125 mDevice->Release();
126 mDevice = NULL;
127 }
128
129 if (mDeviceEx)
130 {
131 mDeviceEx->Release();
132 mDeviceEx = NULL;
133 }
134
135 if (mD3d9)
136 {
137 mD3d9->Release();
138 mD3d9 = NULL;
139 }
140
141 if (mDeviceWindow)
142 {
143 DestroyWindow(mDeviceWindow);
144 mDeviceWindow = NULL;
145 }
146
147 if (mD3d9Ex)
148 {
149 mD3d9Ex->Release();
150 mD3d9Ex = NULL;
151 }
152
153 if (mD3d9Module)
154 {
155 mD3d9Module = NULL;
156 }
157
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000158 while (!mMultiSampleSupport.empty())
159 {
160 delete [] mMultiSampleSupport.begin()->second;
161 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
162 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000163}
164
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000165Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
166{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000167 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000168 return static_cast<rx::Renderer9*>(renderer);
169}
170
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000171EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000172{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000173 if (!initializeCompiler())
174 {
175 return EGL_NOT_INITIALIZED;
176 }
177
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000178 if (mSoftwareDevice)
179 {
180 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
181 }
182 else
183 {
184 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
185 }
186
187 if (mD3d9Module == NULL)
188 {
189 ERR("No D3D9 module found - aborting!\n");
190 return EGL_NOT_INITIALIZED;
191 }
192
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000193 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
194 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
195
196 // Use Direct3D9Ex if available. Among other things, this version is less
197 // inclined to report a lost context, for example when the user switches
198 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
199 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
200 {
201 ASSERT(mD3d9Ex);
202 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
203 ASSERT(mD3d9);
204 }
205 else
206 {
207 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
208 }
209
210 if (!mD3d9)
211 {
212 ERR("Could not create D3D9 device - aborting!\n");
213 return EGL_NOT_INITIALIZED;
214 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000215
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000216 if (mDc != NULL)
217 {
218 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
219 }
220
221 HRESULT result;
222
223 // Give up on getting device caps after about one second.
224 for (int i = 0; i < 10; ++i)
225 {
226 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
227 if (SUCCEEDED(result))
228 {
229 break;
230 }
231 else if (result == D3DERR_NOTAVAILABLE)
232 {
233 Sleep(100); // Give the driver some time to initialize/recover
234 }
235 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
236 {
237 ERR("failed to get device caps (0x%x)\n", result);
238 return EGL_NOT_INITIALIZED;
239 }
240 }
241
242 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
243 {
244 ERR("Renderer does not support PS 2.0. aborting!\n");
245 return EGL_NOT_INITIALIZED;
246 }
247
248 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
249 // This is required by Texture2D::convertToRenderTarget.
250 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
251 {
252 ERR("Renderer does not support stretctrect from textures!\n");
253 return EGL_NOT_INITIALIZED;
254 }
255
256 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
257
258 // ATI cards on XP have problems with non-power-of-two textures.
259 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
260 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
261 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
262 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
263
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000264 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
265 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
266
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000267 mMinSwapInterval = 4;
268 mMaxSwapInterval = 0;
269
270 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
271 {
272 mMinSwapInterval = std::min(mMinSwapInterval, 0);
273 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
274 }
275 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
276 {
277 mMinSwapInterval = std::min(mMinSwapInterval, 1);
278 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
279 }
280 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
281 {
282 mMinSwapInterval = std::min(mMinSwapInterval, 2);
283 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
284 }
285 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
286 {
287 mMinSwapInterval = std::min(mMinSwapInterval, 3);
288 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
289 }
290 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
291 {
292 mMinSwapInterval = std::min(mMinSwapInterval, 4);
293 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
294 }
295
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000296 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000297 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000298 {
299 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000300 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
301 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000302
303 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
304 {
305 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
306 {
307 max = j;
308 }
309 }
310 }
311
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000312 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000313 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000314 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000315 continue;
316
317 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000318 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
319 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000320
321 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
322 {
323 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
324 {
325 max = j;
326 }
327 }
328 }
329
330 mMaxSupportedSamples = max;
331
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000332 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
333 static const TCHAR className[] = TEXT("STATIC");
334
335 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
336
337 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
338 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
339
340 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
341 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
342 {
343 return EGL_BAD_ALLOC;
344 }
345
346 if (FAILED(result))
347 {
348 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
349
350 if (FAILED(result))
351 {
352 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
353 return EGL_BAD_ALLOC;
354 }
355 }
356
357 if (mD3d9Ex)
358 {
359 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
360 ASSERT(SUCCEEDED(result));
361 }
362
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000363 mVertexShaderCache.initialize(mDevice);
364 mPixelShaderCache.initialize(mDevice);
365
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000366 // Check occlusion query support
367 IDirect3DQuery9 *occlusionQuery = NULL;
368 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
369 {
370 occlusionQuery->Release();
371 mOcclusionQuerySupport = true;
372 }
373 else
374 {
375 mOcclusionQuerySupport = false;
376 }
377
378 // Check event query support
379 IDirect3DQuery9 *eventQuery = NULL;
380 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
381 {
382 eventQuery->Release();
383 mEventQuerySupport = true;
384 }
385 else
386 {
387 mEventQuerySupport = false;
388 }
389
390 D3DDISPLAYMODE currentDisplayMode;
391 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
392
393 // Check vertex texture support
394 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
395 // We test this using D3D9 by checking support for the R16F format.
396 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
397 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
398 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
399
400 // Check depth texture support
401 // we use INTZ for depth textures in Direct3D9
402 // we also want NULL texture support to ensure the we can make depth-only FBOs
403 // see http://aras-p.info/texts/D3D9GPUHacks.html
404 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
405 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
406 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
407 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
408
409 // Check 32 bit floating point texture support
410 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
411 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
412 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
413 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
414
415 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
416 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
417 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
418 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
419
420 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
421 {
422 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
423 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
424 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
425 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
426 }
427 else
428 {
429 mFloat32TextureSupport = true;
430 }
431
432 // Check 16 bit floating point texture support
433 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
434 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
435 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
436 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
437
438 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
439 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
440 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
441 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
442
443 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
444 {
445 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
446 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
447 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
448 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
449 }
450 else
451 {
452 mFloat16TextureSupport = true;
453 }
454
455 // Check DXT texture support
456 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
457 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
458 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
459
460 // Check luminance[alpha] texture support
461 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
462 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
463
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000464 initializeDevice();
465
466 return EGL_SUCCESS;
467}
468
469// do any one-time device initialization
470// NOTE: this is also needed after a device lost/reset
471// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000472void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000473{
474 // Permanent non-default states
475 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
476 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
477
478 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
479 {
480 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
481 }
482 else
483 {
484 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
485 }
486
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000487 markAllStateDirty();
488
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000489 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000490
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000491 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000492 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000493 mVertexDataManager = new rx::VertexDataManager(this);
494 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000495}
496
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000497D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000498{
499 D3DPRESENT_PARAMETERS presentParameters = {0};
500
501 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
502 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
503 presentParameters.BackBufferCount = 1;
504 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
505 presentParameters.BackBufferWidth = 1;
506 presentParameters.BackBufferHeight = 1;
507 presentParameters.EnableAutoDepthStencil = FALSE;
508 presentParameters.Flags = 0;
509 presentParameters.hDeviceWindow = mDeviceWindow;
510 presentParameters.MultiSampleQuality = 0;
511 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
512 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
513 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
514 presentParameters.Windowed = TRUE;
515
516 return presentParameters;
517}
518
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000519int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000520{
521 D3DDISPLAYMODE currentDisplayMode;
522 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
523
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000524 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
525 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000526 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
527 int numConfigs = 0;
528
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000529 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000530 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000531 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000532
533 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
534
535 if (SUCCEEDED(result))
536 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000537 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000538 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000539 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000540 HRESULT result = D3D_OK;
541
542 if(depthStencilFormat != D3DFMT_UNKNOWN)
543 {
544 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
545 }
546
547 if (SUCCEEDED(result))
548 {
549 if(depthStencilFormat != D3DFMT_UNKNOWN)
550 {
551 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
552 }
553
554 if (SUCCEEDED(result))
555 {
556 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000557 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
558 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000559 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
560 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
561
562 (*configDescList)[numConfigs++] = newConfig;
563 }
564 }
565 }
566 }
567 }
568
569 return numConfigs;
570}
571
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000572void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000573{
574 delete [] (configDescList);
575}
576
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000577void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000578{
579 if (!mSceneStarted)
580 {
581 long result = mDevice->BeginScene();
582 if (SUCCEEDED(result)) {
583 // This is defensive checking against the device being
584 // lost at unexpected times.
585 mSceneStarted = true;
586 }
587 }
588}
589
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000590void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000591{
592 if (mSceneStarted)
593 {
594 // EndScene can fail if the device was lost, for example due
595 // to a TDR during a draw call.
596 mDevice->EndScene();
597 mSceneStarted = false;
598 }
599}
600
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000601void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000602{
603 HRESULT result;
604
605 IDirect3DQuery9* query = allocateEventQuery();
606 if (!query)
607 {
608 return;
609 }
610
611 result = query->Issue(D3DISSUE_END);
612 ASSERT(SUCCEEDED(result));
613
614 do
615 {
616 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
617
618 if(block && result == S_FALSE)
619 {
620 // Keep polling, but allow other threads to do something useful first
621 Sleep(0);
622 // explicitly check for device loss
623 // some drivers seem to return S_FALSE even if the device is lost
624 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000625 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000626 {
627 result = D3DERR_DEVICELOST;
628 }
629 }
630 }
631 while(block && result == S_FALSE);
632
633 freeEventQuery(query);
634
635 if (isDeviceLostError(result))
636 {
637 mDisplay->notifyDeviceLost();
638 }
639}
640
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000641SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
642{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000643 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000644}
645
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000646IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000647{
648 IDirect3DQuery9 *query = NULL;
649
650 if (mEventQueryPool.empty())
651 {
652 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
653 ASSERT(SUCCEEDED(result));
654 }
655 else
656 {
657 query = mEventQueryPool.back();
658 mEventQueryPool.pop_back();
659 }
660
661 return query;
662}
663
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000664void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000665{
666 if (mEventQueryPool.size() > 1000)
667 {
668 query->Release();
669 }
670 else
671 {
672 mEventQueryPool.push_back(query);
673 }
674}
675
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000676IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000677{
678 return mVertexShaderCache.create(function, length);
679}
680
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000681IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000682{
683 return mPixelShaderCache.create(function, length);
684}
685
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000686HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
687{
688 D3DPOOL Pool = getBufferPool(Usage);
689 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
690}
691
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000692VertexBuffer *Renderer9::createVertexBuffer()
693{
694 return new VertexBuffer9(this);
695}
696
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000697HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
698{
699 D3DPOOL Pool = getBufferPool(Usage);
700 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
701}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000702
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000703IndexBuffer *Renderer9::createIndexBuffer()
704{
705 return new IndexBuffer9(this);
706}
707
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000708void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000709{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000710 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
711 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000712
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000713 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000714 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000715 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
716 int d3dSampler = index + d3dSamplerOffset;
717
718 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
719 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
720
721 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
722 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
723 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
724 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
725 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
726 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
727 if (mSupportsTextureFilterAnisotropy)
728 {
729 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
730 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000731 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000732
733 forceSetSamplers[index] = false;
734 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000735}
736
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000737void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000738{
739 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
740 int d3dSampler = index + d3dSamplerOffset;
741 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000742 unsigned int serial = 0;
743 bool forceSetTexture = false;
744
745 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000746
747 if (texture)
748 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000749 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000750 if (texStorage)
751 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000752 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000753 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000754 }
755 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000756 // in the texture class and we're unexpectedly missing the d3d texture
757 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000758
759 serial = texture->getTextureSerial();
760 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000761 }
762
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000763 if (forceSetTexture || appliedSerials[index] != serial)
764 {
765 mDevice->SetTexture(d3dSampler, d3dTexture);
766 }
767
768 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000769}
770
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000771void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000772{
773 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000774
775 if (rasterStateChanged)
776 {
777 // Set the cull mode
778 if (rasterState.cullFace)
779 {
780 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
781 }
782 else
783 {
784 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
785 }
786
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000787 if (rasterState.polygonOffsetFill)
788 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000789 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000790 {
791 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
792
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000793 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000794 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
795 }
796 }
797 else
798 {
799 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
800 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
801 }
802
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000803 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000804 }
805
806 mForceSetRasterState = false;
807}
808
809void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
810{
811 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
812 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
813 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
814
815 if (blendStateChanged || blendColorChanged)
816 {
817 if (blendState.blend)
818 {
819 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
820
821 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
822 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
823 {
824 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
825 }
826 else
827 {
828 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
829 gl::unorm<8>(blendColor.alpha),
830 gl::unorm<8>(blendColor.alpha),
831 gl::unorm<8>(blendColor.alpha)));
832 }
833
834 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
835 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
836 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
837
838 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
839 blendState.destBlendRGB != blendState.destBlendAlpha ||
840 blendState.blendEquationRGB != blendState.blendEquationAlpha)
841 {
842 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
843
844 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
845 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
846 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
847 }
848 else
849 {
850 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
851 }
852 }
853 else
854 {
855 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
856 }
857
858 if (blendState.sampleAlphaToCoverage)
859 {
860 FIXME("Sample alpha to coverage is unimplemented.");
861 }
862
863 // Set the color mask
864 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
865 // Apparently some ATI cards have a bug where a draw with a zero color
866 // write mask can cause later draws to have incorrect results. Instead,
867 // set a nonzero color write mask but modify the blend state so that no
868 // drawing is done.
869 // http://code.google.com/p/angleproject/issues/detail?id=169
870
871 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
872 blendState.colorMaskBlue, blendState.colorMaskAlpha);
873 if (colorMask == 0 && !zeroColorMaskAllowed)
874 {
875 // Enable green channel, but set blending so nothing will be drawn.
876 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
877 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
878
879 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
880 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
881 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
882 }
883 else
884 {
885 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
886 }
887
888 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
889
890 mCurBlendState = blendState;
891 mCurBlendColor = blendColor;
892 }
893
894 if (sampleMaskChanged)
895 {
896 // Set the multisample mask
897 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
898 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
899
900 mCurSampleMask = sampleMask;
901 }
902
903 mForceSetBlendState = false;
904}
905
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000906void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000907 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000908{
909 bool depthStencilStateChanged = mForceSetDepthStencilState ||
910 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000911 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
912 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000913 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000914
915 if (depthStencilStateChanged)
916 {
917 if (depthStencilState.depthTest)
918 {
919 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
920 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
921 }
922 else
923 {
924 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
925 }
926
927 mCurDepthStencilState = depthStencilState;
928 }
929
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000930 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000931 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000932 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000933 {
934 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
935 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
936
937 // FIXME: Unsupported by D3D9
938 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
939 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
940 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
941 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000942 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000943 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
944 {
945 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
946 return error(GL_INVALID_OPERATION);
947 }
948
949 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000950 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000951
952 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
953 depthStencilState.stencilWritemask);
954 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
955 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
956
957 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000958 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000959 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
960 depthStencilState.stencilMask);
961
962 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
963 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
964 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
965 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
966 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
967 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
968
969 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
970 depthStencilState.stencilBackWritemask);
971 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
972 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
973
974 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000975 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000976 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
977 depthStencilState.stencilBackMask);
978
979 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
980 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
981 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
982 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
983 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
984 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
985 }
986 else
987 {
988 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
989 }
990
991 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
992
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000993 mCurStencilRef = stencilRef;
994 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000995 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000996 }
997
998 mForceSetDepthStencilState = false;
999}
1000
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001001void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001002{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001003 bool scissorChanged = mForceSetScissor ||
1004 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1005 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001006
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001007 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001008 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001009 if (enabled)
1010 {
1011 RECT rect;
1012 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1013 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1014 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1015 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1016 mDevice->SetScissorRect(&rect);
1017 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001018
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001019 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1020
1021 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001022 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001023 }
1024
1025 mForceSetScissor = false;
1026}
1027
daniel@transgaming.com12985182012-12-20 20:56:31 +00001028bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001029 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001030{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001031 gl::Rectangle actualViewport = viewport;
1032 float actualZNear = gl::clamp01(zNear);
1033 float actualZFar = gl::clamp01(zFar);
1034 if (ignoreViewport)
1035 {
1036 actualViewport.x = 0;
1037 actualViewport.y = 0;
1038 actualViewport.width = mRenderTargetDesc.width;
1039 actualViewport.height = mRenderTargetDesc.height;
1040 actualZNear = 0.0f;
1041 actualZFar = 1.0f;
1042 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001043
1044 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001045 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1046 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1047 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1048 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1049 dxViewport.MinZ = actualZNear;
1050 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001051
1052 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1053 {
1054 return false; // Nothing to render
1055 }
1056
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001057 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1058 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001059 if (viewportChanged)
1060 {
1061 mDevice->SetViewport(&dxViewport);
1062
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001063 mCurViewport = actualViewport;
1064 mCurNear = actualZNear;
1065 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001066
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001067 dx_VertexConstants vc = {0};
1068 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001069
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001070 vc.halfPixelSize[0] = 1.0f / dxViewport.Width;
1071 vc.halfPixelSize[1] = -1.0f / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001072
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001073 pc.coord[0] = actualViewport.width * 0.5f;
1074 pc.coord[1] = actualViewport.height * 0.5f;
1075 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
1076 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001077
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001078 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1079 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1080 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1081
1082 vc.depthRange[0] = actualZNear;
1083 vc.depthRange[1] = actualZFar;
1084 vc.depthRange[2] = actualZFar - actualZNear;
1085
1086 pc.depthRange[0] = actualZNear;
1087 pc.depthRange[1] = actualZFar;
1088 pc.depthRange[2] = actualZFar - actualZNear;
1089
1090 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1091 {
1092 mVertexConstants = vc;
1093 mDxUniformsDirty = true;
1094 }
1095
1096 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1097 {
1098 mPixelConstants = pc;
1099 mDxUniformsDirty = true;
1100 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001101 }
1102
1103 mForceSetViewport = false;
1104 return true;
1105}
1106
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001107bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1108{
1109 switch (mode)
1110 {
1111 case GL_POINTS:
1112 mPrimitiveType = D3DPT_POINTLIST;
1113 mPrimitiveCount = count;
1114 break;
1115 case GL_LINES:
1116 mPrimitiveType = D3DPT_LINELIST;
1117 mPrimitiveCount = count / 2;
1118 break;
1119 case GL_LINE_LOOP:
1120 mPrimitiveType = D3DPT_LINESTRIP;
1121 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1122 break;
1123 case GL_LINE_STRIP:
1124 mPrimitiveType = D3DPT_LINESTRIP;
1125 mPrimitiveCount = count - 1;
1126 break;
1127 case GL_TRIANGLES:
1128 mPrimitiveType = D3DPT_TRIANGLELIST;
1129 mPrimitiveCount = count / 3;
1130 break;
1131 case GL_TRIANGLE_STRIP:
1132 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1133 mPrimitiveCount = count - 2;
1134 break;
1135 case GL_TRIANGLE_FAN:
1136 mPrimitiveType = D3DPT_TRIANGLEFAN;
1137 mPrimitiveCount = count - 2;
1138 break;
1139 default:
1140 return error(GL_INVALID_ENUM, false);
1141 }
1142
1143 return mPrimitiveCount > 0;
1144}
1145
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001146
1147gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1148{
1149 if (!depthbuffer)
1150 {
1151 ERR("Unexpected null depthbuffer for depth-only FBO.");
1152 return NULL;
1153 }
1154
1155 GLsizei width = depthbuffer->getWidth();
1156 GLsizei height = depthbuffer->getHeight();
1157
1158 // search cached nullcolorbuffers
1159 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1160 {
1161 if (mNullColorbufferCache[i].buffer != NULL &&
1162 mNullColorbufferCache[i].width == width &&
1163 mNullColorbufferCache[i].height == height)
1164 {
1165 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1166 return mNullColorbufferCache[i].buffer;
1167 }
1168 }
1169
1170 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1171
1172 // add nullbuffer to the cache
1173 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1174 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1175 {
1176 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1177 {
1178 oldest = &mNullColorbufferCache[i];
1179 }
1180 }
1181
1182 delete oldest->buffer;
1183 oldest->buffer = nullbuffer;
1184 oldest->lruCount = ++mMaxNullColorbufferLRU;
1185 oldest->width = width;
1186 oldest->height = height;
1187
1188 return nullbuffer;
1189}
1190
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001191bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001192{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001193 // if there is no color attachment we must synthesize a NULL colorattachment
1194 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1195 gl::Renderbuffer *renderbufferObject = NULL;
1196 if (framebuffer->getColorbufferType() != GL_NONE)
1197 {
1198 renderbufferObject = framebuffer->getColorbuffer();
1199 }
1200 else
1201 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001202 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001203 }
1204 if (!renderbufferObject)
1205 {
1206 ERR("unable to locate renderbuffer for FBO.");
1207 return false;
1208 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001209
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001210 bool renderTargetChanged = false;
1211 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1212 if (renderTargetSerial != mAppliedRenderTargetSerial)
1213 {
1214 // Apply the render target on the device
1215 IDirect3DSurface9 *renderTargetSurface = NULL;
1216
1217 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1218 if (renderTarget)
1219 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001220 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001221 }
1222
1223 if (!renderTargetSurface)
1224 {
1225 ERR("render target pointer unexpectedly null.");
1226 return false; // Context must be lost
1227 }
1228
1229 mDevice->SetRenderTarget(0, renderTargetSurface);
1230 renderTargetSurface->Release();
1231
1232 mAppliedRenderTargetSerial = renderTargetSerial;
1233 renderTargetChanged = true;
1234 }
1235
1236 gl::Renderbuffer *depthStencil = NULL;
1237 unsigned int depthbufferSerial = 0;
1238 unsigned int stencilbufferSerial = 0;
1239 if (framebuffer->getDepthbufferType() != GL_NONE)
1240 {
1241 depthStencil = framebuffer->getDepthbuffer();
1242 if (!depthStencil)
1243 {
1244 ERR("Depth stencil pointer unexpectedly null.");
1245 return false;
1246 }
1247
1248 depthbufferSerial = depthStencil->getSerial();
1249 }
1250 else if (framebuffer->getStencilbufferType() != GL_NONE)
1251 {
1252 depthStencil = framebuffer->getStencilbuffer();
1253 if (!depthStencil)
1254 {
1255 ERR("Depth stencil pointer unexpectedly null.");
1256 return false;
1257 }
1258
1259 stencilbufferSerial = depthStencil->getSerial();
1260 }
1261
1262 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1263 stencilbufferSerial != mAppliedStencilbufferSerial ||
1264 !mDepthStencilInitialized)
1265 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001266 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001267 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001268
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001269 // Apply the depth stencil on the device
1270 if (depthStencil)
1271 {
1272 IDirect3DSurface9 *depthStencilSurface = NULL;
1273 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1274
1275 if (depthStencilRenderTarget)
1276 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001277 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001278 }
1279
1280 if (!depthStencilSurface)
1281 {
1282 ERR("depth stencil pointer unexpectedly null.");
1283 return false; // Context must be lost
1284 }
1285
1286 mDevice->SetDepthStencilSurface(depthStencilSurface);
1287 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001288
1289 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001290 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001291 }
1292 else
1293 {
1294 mDevice->SetDepthStencilSurface(NULL);
1295 }
1296
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001297 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1298 {
1299 mCurDepthSize = depthSize;
1300 mForceSetRasterState = true;
1301 }
1302
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001303 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1304 {
1305 mCurStencilSize = stencilSize;
1306 mForceSetDepthStencilState = true;
1307 }
1308
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001309 mAppliedDepthbufferSerial = depthbufferSerial;
1310 mAppliedStencilbufferSerial = stencilbufferSerial;
1311 mDepthStencilInitialized = true;
1312 }
1313
1314 if (renderTargetChanged || !mRenderTargetDescInitialized)
1315 {
1316 mForceSetScissor = true;
1317 mForceSetViewport = true;
1318
1319 mRenderTargetDesc.width = renderbufferObject->getWidth();
1320 mRenderTargetDesc.height = renderbufferObject->getHeight();
1321 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1322 mRenderTargetDescInitialized = true;
1323 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001324
1325 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001326}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001327
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001328GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001329{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001330 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001331 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1332 if (err != GL_NO_ERROR)
1333 {
1334 return err;
1335 }
1336
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001337 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1338}
1339
1340// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001341GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001342{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001343 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001344
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001345 if (err == GL_NO_ERROR)
1346 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001347 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001348 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001349 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1350
1351 mDevice->SetIndices(indexBuffer->getBuffer());
1352 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001353 }
1354 }
1355
1356 return err;
1357}
1358
1359void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1360{
1361 startScene();
1362
1363 if (mode == GL_LINE_LOOP)
1364 {
1365 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1366 }
1367 else if (instances > 0)
1368 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001369 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001370 if (countingIB)
1371 {
1372 if (mAppliedIBSerial != countingIB->getSerial())
1373 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001374 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1375
1376 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001377 mAppliedIBSerial = countingIB->getSerial();
1378 }
1379
1380 for (int i = 0; i < mRepeatDraw; i++)
1381 {
1382 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1383 }
1384 }
1385 else
1386 {
1387 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1388 return error(GL_OUT_OF_MEMORY);
1389 }
1390 }
1391 else // Regular case
1392 {
1393 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1394 }
1395}
1396
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001397void 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 +00001398{
1399 startScene();
1400
1401 if (mode == GL_LINE_LOOP)
1402 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001403 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001404 }
1405 else
1406 {
1407 for (int i = 0; i < mRepeatDraw; i++)
1408 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001409 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1410 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001411 }
1412 }
1413}
1414
1415void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1416{
1417 // Get the raw indices for an indexed draw
1418 if (type != GL_NONE && elementArrayBuffer)
1419 {
1420 gl::Buffer *indexBuffer = elementArrayBuffer;
1421 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1422 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1423 }
1424
1425 UINT startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001426
1427 if (get32BitIndexSupport())
1428 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001429 if (!mLineLoopIB)
1430 {
1431 mLineLoopIB = new StreamingIndexBufferInterface(this);
1432 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1433 {
1434 delete mLineLoopIB;
1435 mLineLoopIB = NULL;
1436
1437 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
1438 return error(GL_OUT_OF_MEMORY);
1439 }
1440 }
1441
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001442 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001443 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001444 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001445 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1446 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001447 }
1448
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001449 void* mappedMemory = NULL;
1450 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1451 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001452 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001453 ERR("Could not map index buffer for GL_LINE_LOOP.");
1454 return error(GL_OUT_OF_MEMORY);
1455 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001456
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001457 startIndex = static_cast<UINT>(offset) / 4;
1458 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1459
1460 switch (type)
1461 {
1462 case GL_NONE: // Non-indexed draw
1463 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001464 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001465 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001466 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001467 data[count] = 0;
1468 break;
1469 case GL_UNSIGNED_BYTE:
1470 for (int i = 0; i < count; i++)
1471 {
1472 data[i] = static_cast<const GLubyte*>(indices)[i];
1473 }
1474 data[count] = static_cast<const GLubyte*>(indices)[0];
1475 break;
1476 case GL_UNSIGNED_SHORT:
1477 for (int i = 0; i < count; i++)
1478 {
1479 data[i] = static_cast<const GLushort*>(indices)[i];
1480 }
1481 data[count] = static_cast<const GLushort*>(indices)[0];
1482 break;
1483 case GL_UNSIGNED_INT:
1484 for (int i = 0; i < count; i++)
1485 {
1486 data[i] = static_cast<const GLuint*>(indices)[i];
1487 }
1488 data[count] = static_cast<const GLuint*>(indices)[0];
1489 break;
1490 default: UNREACHABLE();
1491 }
1492
1493 if (!mLineLoopIB->unmapBuffer())
1494 {
1495 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1496 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001497 }
1498 }
1499 else
1500 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001501 if (!mLineLoopIB)
1502 {
1503 mLineLoopIB = new StreamingIndexBufferInterface(this);
1504 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1505 {
1506 delete mLineLoopIB;
1507 mLineLoopIB = NULL;
1508
1509 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
1510 return error(GL_OUT_OF_MEMORY);
1511 }
1512 }
1513
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001514 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001515 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001516 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001517 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1518 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001519 }
1520
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001521 void* mappedMemory = NULL;
1522 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1523 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001524 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001525 ERR("Could not map index buffer for GL_LINE_LOOP.");
1526 return error(GL_OUT_OF_MEMORY);
1527 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001528
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001529 startIndex = static_cast<UINT>(offset) / 2;
1530 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1531
1532 switch (type)
1533 {
1534 case GL_NONE: // Non-indexed draw
1535 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001536 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001537 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001538 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001539 data[count] = 0;
1540 break;
1541 case GL_UNSIGNED_BYTE:
1542 for (int i = 0; i < count; i++)
1543 {
1544 data[i] = static_cast<const GLubyte*>(indices)[i];
1545 }
1546 data[count] = static_cast<const GLubyte*>(indices)[0];
1547 break;
1548 case GL_UNSIGNED_SHORT:
1549 for (int i = 0; i < count; i++)
1550 {
1551 data[i] = static_cast<const GLushort*>(indices)[i];
1552 }
1553 data[count] = static_cast<const GLushort*>(indices)[0];
1554 break;
1555 case GL_UNSIGNED_INT:
1556 for (int i = 0; i < count; i++)
1557 {
1558 data[i] = static_cast<const GLuint*>(indices)[i];
1559 }
1560 data[count] = static_cast<const GLuint*>(indices)[0];
1561 break;
1562 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001563 }
1564
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001565 if (!mLineLoopIB->unmapBuffer())
1566 {
1567 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1568 return error(GL_OUT_OF_MEMORY);
1569 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001570 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001571
1572 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001573 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001574 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1575
1576 mDevice->SetIndices(indexBuffer->getBuffer());
1577 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001578 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001579
1580 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001581}
1582
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001583void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1584{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001585 unsigned int programBinarySerial = programBinary->getSerial();
1586 if (programBinarySerial != mAppliedProgramBinarySerial)
1587 {
1588 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1589 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001590
daniel@transgaming.come4991412012-12-20 20:55:34 +00001591 IDirect3DVertexShader9 *vertexShader = NULL;
1592 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001593
daniel@transgaming.come4991412012-12-20 20:55:34 +00001594 IDirect3DPixelShader9 *pixelShader = NULL;
1595 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001596
daniel@transgaming.come4991412012-12-20 20:55:34 +00001597 mDevice->SetPixelShader(pixelShader);
1598 mDevice->SetVertexShader(vertexShader);
1599 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001600 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001601
1602 mAppliedProgramBinarySerial = programBinarySerial;
1603 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001604}
1605
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001606void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001607{
1608 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1609 {
1610 gl::Uniform *targetUniform = *ub;
1611
1612 if (targetUniform->dirty)
1613 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001614 int count = targetUniform->elementCount();
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001615 GLfloat *f = (GLfloat*)targetUniform->data;
1616 GLint *i = (GLint*)targetUniform->data;
1617 GLboolean *b = (GLboolean*)targetUniform->data;
1618
1619 switch (targetUniform->type)
1620 {
1621 case GL_SAMPLER_2D:
1622 case GL_SAMPLER_CUBE:
1623 break;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001624 case GL_BOOL: applyUniformnbv(targetUniform, count, 1, b); break;
1625 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, count, 2, b); break;
1626 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, count, 3, b); break;
1627 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, count, 4, b); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001628 case GL_FLOAT:
1629 case GL_FLOAT_VEC2:
1630 case GL_FLOAT_VEC3:
1631 case GL_FLOAT_VEC4:
1632 case GL_FLOAT_MAT2:
1633 case GL_FLOAT_MAT3:
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001634 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1635 case GL_INT: applyUniform1iv(targetUniform, count, i); break;
1636 case GL_INT_VEC2: applyUniform2iv(targetUniform, count, i); break;
1637 case GL_INT_VEC3: applyUniform3iv(targetUniform, count, i); break;
1638 case GL_INT_VEC4: applyUniform4iv(targetUniform, count, i); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001639 default:
1640 UNREACHABLE();
1641 }
1642
1643 targetUniform->dirty = false;
1644 }
1645 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001646
1647 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001648 if (mDxUniformsDirty)
1649 {
1650 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1651 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1652 mDxUniformsDirty = false;
1653 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001654}
1655
1656void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
1657{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001658 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001659
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001660 if (targetUniform->psRegisterIndex >= 0 || targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001661 {
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001662 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001663 for (int i = 0; i < count; i++)
1664 {
1665 for (int j = 0; j < 4; j++)
1666 {
1667 if (j < width)
1668 {
1669 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
1670 }
1671 else
1672 {
1673 vector[i * 4 + j] = 0.0f;
1674 }
1675 }
1676 }
1677 }
1678
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001679 applyUniformnfv(targetUniform, vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001680}
1681
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001682void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001683{
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001684 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001685 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001686 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001687 }
1688
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001689 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001690 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001691 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001692 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001693}
1694
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001695void Renderer9::applyUniform1iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001696{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001697 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1698 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001699
1700 for (int i = 0; i < count; i++)
1701 {
1702 vector[i] = gl::Vector4((float)v[i], 0, 0, 0);
1703 }
1704
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001705 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001706}
1707
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001708void Renderer9::applyUniform2iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001709{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001710 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1711 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001712
1713 for (int i = 0; i < count; i++)
1714 {
1715 vector[i] = gl::Vector4((float)v[0], (float)v[1], 0, 0);
1716
1717 v += 2;
1718 }
1719
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001720 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001721}
1722
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001723void Renderer9::applyUniform3iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001724{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001725 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1726 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001727
1728 for (int i = 0; i < count; i++)
1729 {
1730 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], 0);
1731
1732 v += 3;
1733 }
1734
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001735 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736}
1737
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001738void Renderer9::applyUniform4iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001740 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1741 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742
1743 for (int i = 0; i < count; i++)
1744 {
1745 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1746
1747 v += 4;
1748 }
1749
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001750 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001751}
1752
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001753void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001754{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001755 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1756 gl::unorm<8>(clearParams.colorClearValue.red),
1757 gl::unorm<8>(clearParams.colorClearValue.green),
1758 gl::unorm<8>(clearParams.colorClearValue.blue));
1759 float depth = gl::clamp01(clearParams.depthClearValue);
1760 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001761
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001762 unsigned int stencilUnmasked = 0x0;
1763 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1764 {
1765 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1766 stencilUnmasked = (0x1 << stencilSize) - 1;
1767 }
1768
1769 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1770
1771 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1772 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1773 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1774 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1775 clearParams.colorMaskBlue && alphaUnmasked);
1776
1777 if (needMaskedColorClear || needMaskedStencilClear)
1778 {
1779 // State which is altered in all paths from this point to the clear call is saved.
1780 // State which is altered in only some paths will be flagged dirty in the case that
1781 // that path is taken.
1782 HRESULT hr;
1783 if (mMaskedClearSavedState == NULL)
1784 {
1785 hr = mDevice->BeginStateBlock();
1786 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1787
1788 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1789 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1790 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1791 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1792 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1793 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1794 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1795 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1796 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1797 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1798 mDevice->SetPixelShader(NULL);
1799 mDevice->SetVertexShader(NULL);
1800 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1801 mDevice->SetStreamSource(0, NULL, 0, 0);
1802 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1803 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1804 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1805 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1806 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1807 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1808 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1809
1810 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1811 {
1812 mDevice->SetStreamSourceFreq(i, 1);
1813 }
1814
1815 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1816 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1817 }
1818
1819 ASSERT(mMaskedClearSavedState != NULL);
1820
1821 if (mMaskedClearSavedState != NULL)
1822 {
1823 hr = mMaskedClearSavedState->Capture();
1824 ASSERT(SUCCEEDED(hr));
1825 }
1826
1827 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1828 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1829 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1830 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1831 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1832 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1833 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1834 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1835
1836 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1837 {
1838 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1839 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1840 clearParams.colorMaskGreen,
1841 clearParams.colorMaskBlue,
1842 clearParams.colorMaskAlpha));
1843 }
1844 else
1845 {
1846 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1847 }
1848
1849 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1850 {
1851 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1852 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1853 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1854 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1855 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1856 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1857 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1858 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1859 }
1860 else
1861 {
1862 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1863 }
1864
1865 mDevice->SetPixelShader(NULL);
1866 mDevice->SetVertexShader(NULL);
1867 mDevice->SetFVF(D3DFVF_XYZRHW);
1868 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1869 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1870 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1871 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1872 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1873 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1874 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1875
1876 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1877 {
1878 mDevice->SetStreamSourceFreq(i, 1);
1879 }
1880
1881 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1882 quad[0][0] = -0.5f;
1883 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1884 quad[0][2] = 0.0f;
1885 quad[0][3] = 1.0f;
1886
1887 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1888 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1889 quad[1][2] = 0.0f;
1890 quad[1][3] = 1.0f;
1891
1892 quad[2][0] = -0.5f;
1893 quad[2][1] = -0.5f;
1894 quad[2][2] = 0.0f;
1895 quad[2][3] = 1.0f;
1896
1897 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1898 quad[3][1] = -0.5f;
1899 quad[3][2] = 0.0f;
1900 quad[3][3] = 1.0f;
1901
1902 startScene();
1903 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1904
1905 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1906 {
1907 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1908 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1909 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1910 }
1911
1912 if (mMaskedClearSavedState != NULL)
1913 {
1914 mMaskedClearSavedState->Apply();
1915 }
1916 }
1917 else if (clearParams.mask)
1918 {
1919 DWORD dxClearFlags = 0;
1920 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1921 {
1922 dxClearFlags |= D3DCLEAR_TARGET;
1923 }
1924 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1925 {
1926 dxClearFlags |= D3DCLEAR_ZBUFFER;
1927 }
1928 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1929 {
1930 dxClearFlags |= D3DCLEAR_STENCIL;
1931 }
1932
1933 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1934 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001935}
1936
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001937void Renderer9::markAllStateDirty()
1938{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001939 mAppliedRenderTargetSerial = 0;
1940 mAppliedDepthbufferSerial = 0;
1941 mAppliedStencilbufferSerial = 0;
1942 mDepthStencilInitialized = false;
1943 mRenderTargetDescInitialized = false;
1944
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001945 mForceSetDepthStencilState = true;
1946 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001947 mForceSetScissor = true;
1948 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001949 mForceSetBlendState = true;
1950
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001951 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001952 {
1953 mForceSetVertexSamplerStates[i] = true;
1954 mCurVertexTextureSerials[i] = 0;
1955 }
1956 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1957 {
1958 mForceSetPixelSamplerStates[i] = true;
1959 mCurPixelTextureSerials[i] = 0;
1960 }
1961
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001962 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001963 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001964 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001965
1966 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001967}
1968
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001969void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001970{
1971 while (!mEventQueryPool.empty())
1972 {
1973 mEventQueryPool.back()->Release();
1974 mEventQueryPool.pop_back();
1975 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001976
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001977 if (mMaskedClearSavedState)
1978 {
1979 mMaskedClearSavedState->Release();
1980 mMaskedClearSavedState = NULL;
1981 }
1982
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001983 mVertexShaderCache.clear();
1984 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001985
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001986 delete mBlit;
1987 mBlit = NULL;
1988
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001989 delete mVertexDataManager;
1990 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001991
1992 delete mIndexDataManager;
1993 mIndexDataManager = NULL;
1994
1995 delete mLineLoopIB;
1996 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001997
1998 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1999 {
2000 delete mNullColorbufferCache[i].buffer;
2001 mNullColorbufferCache[i].buffer = NULL;
2002 }
2003
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002004}
2005
2006
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002007void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002008{
2009 mDeviceLost = true;
2010}
2011
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002012bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002013{
2014 return mDeviceLost;
2015}
2016
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002017// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002018bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002019{
2020 bool isLost = false;
2021
2022 if (mDeviceEx)
2023 {
2024 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
2025 }
2026 else if (mDevice)
2027 {
2028 isLost = FAILED(mDevice->TestCooperativeLevel());
2029 }
2030 else
2031 {
2032 // No device yet, so no reset required
2033 }
2034
2035 if (isLost)
2036 {
2037 // ensure we note the device loss --
2038 // we'll probably get this done again by markDeviceLost
2039 // but best to remember it!
2040 // Note that we don't want to clear the device loss status here
2041 // -- this needs to be done by resetDevice
2042 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002043 if (notify)
2044 {
2045 mDisplay->notifyDeviceLost();
2046 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002047 }
2048
2049 return isLost;
2050}
2051
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002052bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002053{
2054 HRESULT status = D3D_OK;
2055
2056 if (mDeviceEx)
2057 {
2058 status = mDeviceEx->CheckDeviceState(NULL);
2059 }
2060 else if (mDevice)
2061 {
2062 status = mDevice->TestCooperativeLevel();
2063 }
2064
2065 switch (status)
2066 {
2067 case D3DERR_DEVICENOTRESET:
2068 case D3DERR_DEVICEHUNG:
2069 return true;
2070 default:
2071 return false;
2072 }
2073}
2074
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002075bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002076{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002077 releaseDeviceResources();
2078
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002079 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2080
2081 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002082 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002083 int attempts = 3;
2084
2085 while (lost && attempts > 0)
2086 {
2087 if (mDeviceEx)
2088 {
2089 Sleep(500); // Give the graphics driver some CPU time
2090 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2091 }
2092 else
2093 {
2094 result = mDevice->TestCooperativeLevel();
2095 while (result == D3DERR_DEVICELOST)
2096 {
2097 Sleep(100); // Give the graphics driver some CPU time
2098 result = mDevice->TestCooperativeLevel();
2099 }
2100
2101 if (result == D3DERR_DEVICENOTRESET)
2102 {
2103 result = mDevice->Reset(&presentParameters);
2104 }
2105 }
2106
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002107 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002108 attempts --;
2109 }
2110
2111 if (FAILED(result))
2112 {
2113 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2114 return false;
2115 }
2116
2117 // reset device defaults
2118 initializeDevice();
2119 mDeviceLost = false;
2120
2121 return true;
2122}
2123
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002124DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002125{
2126 return mAdapterIdentifier.VendorId;
2127}
2128
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002129std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002130{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002131 std::ostringstream rendererString;
2132
2133 rendererString << mAdapterIdentifier.Description;
2134 if (getShareHandleSupport())
2135 {
2136 rendererString << " Direct3D9Ex";
2137 }
2138 else
2139 {
2140 rendererString << " Direct3D9";
2141 }
2142
2143 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2144 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2145
2146 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002147}
2148
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002149GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002150{
2151 return mAdapterIdentifier.DeviceIdentifier;
2152}
2153
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002154void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002155{
2156 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
2157 {
2158 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
2159 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2160
2161 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
2162 }
2163}
2164
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002165bool Renderer9::getBGRATextureSupport() const
2166{
2167 // DirectX 9 always supports BGRA
2168 return true;
2169}
2170
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002171bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002172{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002173 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002174}
2175
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002176bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002177{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002178 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002179}
2180
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002181bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002182{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002183 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002184}
2185
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002186bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002187{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002188 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002189}
2190
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002191bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002192{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002193 *filtering = mFloat32FilterSupport;
2194 *renderable = mFloat32RenderSupport;
2195 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002196}
2197
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002198bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002199{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002200 *filtering = mFloat16FilterSupport;
2201 *renderable = mFloat16RenderSupport;
2202 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002203}
2204
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002205bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002206{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002207 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002208}
2209
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002210bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002211{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002212 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002213}
2214
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002215bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002216{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002217 return mSupportsTextureFilterAnisotropy;
2218}
2219
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002220float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002221{
2222 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002223 {
2224 return mDeviceCaps.MaxAnisotropy;
2225 }
2226 return 1.0f;
2227}
2228
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002229bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002230{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002231 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002232}
2233
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002234unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002235{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002236 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2237 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002238}
2239
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002240unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2241{
2242 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2243}
2244
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002245int Renderer9::getMaxVertexUniformVectors() const
2246{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002247 return MAX_VERTEX_UNIFORM_VECTORS;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002248}
2249
2250int Renderer9::getMaxFragmentUniformVectors() const
2251{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002252 return getMajorShaderModel() >= 3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002253}
2254
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002255int Renderer9::getMaxVaryingVectors() const
2256{
2257 return getMajorShaderModel() >= 3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2258}
2259
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002260bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002261{
2262 return mSupportsNonPower2Textures;
2263}
2264
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002265bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002266{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002267 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002268}
2269
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002270bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002271{
2272 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2273}
2274
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002275bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002276{
2277 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002278 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002279}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002280
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002281bool Renderer9::getDerivativeInstructionSupport() const
2282{
2283 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2284}
2285
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002286int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002287{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002288 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002289}
2290
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002291float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002292{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002293 // Point size clamped at 1.0f for SM2
2294 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002295}
2296
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002297int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002298{
2299 return (int)mDeviceCaps.MaxTextureWidth;
2300}
2301
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002302int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002303{
2304 return (int)mDeviceCaps.MaxTextureHeight;
2305}
2306
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002307bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002308{
2309 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2310}
2311
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002312DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002313{
2314 return mDeviceCaps.DeclTypes;
2315}
2316
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002317int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002318{
2319 return mMinSwapInterval;
2320}
2321
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002322int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002323{
2324 return mMaxSwapInterval;
2325}
2326
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002327int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002328{
2329 return mMaxSupportedSamples;
2330}
2331
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002332int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002333{
2334 if (requested == 0)
2335 {
2336 return requested;
2337 }
2338
2339 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2340 if (itr == mMultiSampleSupport.end())
2341 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002342 if (format == D3DFMT_UNKNOWN)
2343 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002344 return -1;
2345 }
2346
2347 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2348 {
2349 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2350 {
2351 return i;
2352 }
2353 }
2354
2355 return -1;
2356}
2357
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002358D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2359{
2360 switch (internalformat)
2361 {
2362 case GL_DEPTH_COMPONENT16:
2363 case GL_DEPTH_COMPONENT32_OES:
2364 case GL_DEPTH24_STENCIL8_OES:
2365 return D3DFMT_INTZ;
2366 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2367 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2368 return D3DFMT_DXT1;
2369 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2370 return D3DFMT_DXT3;
2371 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2372 return D3DFMT_DXT5;
2373 case GL_RGBA32F_EXT:
2374 case GL_RGB32F_EXT:
2375 case GL_ALPHA32F_EXT:
2376 case GL_LUMINANCE32F_EXT:
2377 case GL_LUMINANCE_ALPHA32F_EXT:
2378 return D3DFMT_A32B32G32R32F;
2379 case GL_RGBA16F_EXT:
2380 case GL_RGB16F_EXT:
2381 case GL_ALPHA16F_EXT:
2382 case GL_LUMINANCE16F_EXT:
2383 case GL_LUMINANCE_ALPHA16F_EXT:
2384 return D3DFMT_A16B16G16R16F;
2385 case GL_LUMINANCE8_EXT:
2386 if (getLuminanceTextureSupport())
2387 {
2388 return D3DFMT_L8;
2389 }
2390 break;
2391 case GL_LUMINANCE8_ALPHA8_EXT:
2392 if (getLuminanceAlphaTextureSupport())
2393 {
2394 return D3DFMT_A8L8;
2395 }
2396 break;
2397 case GL_RGB8_OES:
2398 case GL_RGB565:
2399 return D3DFMT_X8R8G8B8;
2400 }
2401
2402 return D3DFMT_A8R8G8B8;
2403}
2404
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002405bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002406{
2407 bool result = false;
2408
2409 if (source && dest)
2410 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002411 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2412 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002413
2414 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002415 for (int i = 0; i < levels; ++i)
2416 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002417 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2418 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002419
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002420 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002421
2422 if (srcSurf) srcSurf->Release();
2423 if (dstSurf) dstSurf->Release();
2424
2425 if (!result)
2426 return false;
2427 }
2428 }
2429
2430 return result;
2431}
2432
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002433bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002434{
2435 bool result = false;
2436
2437 if (source && dest)
2438 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002439 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2440 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002441 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002442 for (int f = 0; f < 6; f++)
2443 {
2444 for (int i = 0; i < levels; i++)
2445 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002446 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2447 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002448
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002449 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002450
2451 if (srcSurf) srcSurf->Release();
2452 if (dstSurf) dstSurf->Release();
2453
2454 if (!result)
2455 return false;
2456 }
2457 }
2458 }
2459
2460 return result;
2461}
2462
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002463D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002464{
2465 if (mD3d9Ex != NULL)
2466 {
2467 return D3DPOOL_DEFAULT;
2468 }
2469 else
2470 {
2471 if (!(usage & D3DUSAGE_DYNAMIC))
2472 {
2473 return D3DPOOL_MANAGED;
2474 }
2475 }
2476
2477 return D3DPOOL_DEFAULT;
2478}
2479
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002480bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002481 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002482{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002483 RECT rect;
2484 rect.left = sourceRect.x;
2485 rect.top = sourceRect.y;
2486 rect.right = sourceRect.x + sourceRect.width;
2487 rect.bottom = sourceRect.y + sourceRect.height;
2488
2489 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002490}
2491
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002492bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002493 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002494{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002495 RECT rect;
2496 rect.left = sourceRect.x;
2497 rect.top = sourceRect.y;
2498 rect.right = sourceRect.x + sourceRect.width;
2499 rect.bottom = sourceRect.y + sourceRect.height;
2500
2501 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002502}
2503
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002504bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2505 bool blitRenderTarget, bool blitDepthStencil)
2506{
2507 endScene();
2508
2509 if (blitRenderTarget)
2510 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002511 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2512 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2513 RenderTarget9 *readRenderTarget = NULL;
2514 RenderTarget9 *drawRenderTarget = NULL;
2515 IDirect3DSurface9* readSurface = NULL;
2516 IDirect3DSurface9* drawSurface = NULL;
2517
2518 if (readBuffer)
2519 {
2520 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2521 }
2522 if (drawBuffer)
2523 {
2524 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2525 }
2526
2527 if (readRenderTarget)
2528 {
2529 readSurface = readRenderTarget->getSurface();
2530 }
2531 if (drawRenderTarget)
2532 {
2533 drawSurface = drawRenderTarget->getSurface();
2534 }
2535
2536 if (!readSurface || !drawSurface)
2537 {
2538 ERR("Failed to retrieve the render target.");
2539 return error(GL_OUT_OF_MEMORY, false);
2540 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002541
2542 RECT srcRect, dstRect;
2543 RECT *srcRectPtr = NULL;
2544 RECT *dstRectPtr = NULL;
2545
2546 if (readRect)
2547 {
2548 srcRect.left = readRect->x;
2549 srcRect.right = readRect->x + readRect->width;
2550 srcRect.top = readRect->y;
2551 srcRect.bottom = readRect->y + readRect->height;
2552 srcRectPtr = &srcRect;
2553 }
2554
2555 if (drawRect)
2556 {
2557 dstRect.left = drawRect->x;
2558 dstRect.right = drawRect->x + drawRect->width;
2559 dstRect.top = drawRect->y;
2560 dstRect.bottom = drawRect->y + drawRect->height;
2561 dstRectPtr = &dstRect;
2562 }
2563
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002564 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002565
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002566 readSurface->Release();
2567 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002568
2569 if (FAILED(result))
2570 {
2571 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2572 return false;
2573 }
2574 }
2575
2576 if (blitDepthStencil)
2577 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002578 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2579 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2580 RenderTarget9 *readDepthStencil = NULL;
2581 RenderTarget9 *drawDepthStencil = NULL;
2582 IDirect3DSurface9* readSurface = NULL;
2583 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002584
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002585 if (readBuffer)
2586 {
2587 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2588 }
2589 if (drawBuffer)
2590 {
2591 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2592 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002593
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002594 if (readDepthStencil)
2595 {
2596 readSurface = readDepthStencil->getSurface();
2597 }
2598 if (drawDepthStencil)
2599 {
2600 drawSurface = drawDepthStencil->getSurface();
2601 }
2602
2603 if (!readSurface || !drawSurface)
2604 {
2605 ERR("Failed to retrieve the render target.");
2606 return error(GL_OUT_OF_MEMORY, false);
2607 }
2608
2609 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2610
2611 readSurface->Release();
2612 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002613
2614 if (FAILED(result))
2615 {
2616 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2617 return false;
2618 }
2619 }
2620
2621 return true;
2622}
2623
2624void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2625 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2626{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002627 RenderTarget9 *renderTarget = NULL;
2628 IDirect3DSurface9 *surface = NULL;
2629 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2630
2631 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002632 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002633 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2634 }
2635
2636 if (renderTarget)
2637 {
2638 surface = renderTarget->getSurface();
2639 }
2640
2641 if (!surface)
2642 {
2643 // context must be lost
2644 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002645 }
2646
2647 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002648 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002649
2650 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2651 {
2652 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002653 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002654 return error(GL_OUT_OF_MEMORY);
2655 }
2656
2657 HRESULT result;
2658 IDirect3DSurface9 *systemSurface = NULL;
2659 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2660 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2661 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2662 if (directToPixels)
2663 {
2664 // Use the pixels ptr as a shared handle to write directly into client's memory
2665 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2666 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2667 if (FAILED(result))
2668 {
2669 // Try again without the shared handle
2670 directToPixels = false;
2671 }
2672 }
2673
2674 if (!directToPixels)
2675 {
2676 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2677 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2678 if (FAILED(result))
2679 {
2680 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002681 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002682 return error(GL_OUT_OF_MEMORY);
2683 }
2684 }
2685
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002686 result = mDevice->GetRenderTargetData(surface, systemSurface);
2687 surface->Release();
2688 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002689
2690 if (FAILED(result))
2691 {
2692 systemSurface->Release();
2693
2694 // It turns out that D3D will sometimes produce more error
2695 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002696 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002697 return error(GL_OUT_OF_MEMORY);
2698 else
2699 {
2700 UNREACHABLE();
2701 return;
2702 }
2703
2704 }
2705
2706 if (directToPixels)
2707 {
2708 systemSurface->Release();
2709 return;
2710 }
2711
2712 RECT rect;
2713 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2714 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2715 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2716 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2717
2718 D3DLOCKED_RECT lock;
2719 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2720
2721 if (FAILED(result))
2722 {
2723 UNREACHABLE();
2724 systemSurface->Release();
2725
2726 return; // No sensible error to generate
2727 }
2728
2729 unsigned char *dest = (unsigned char*)pixels;
2730 unsigned short *dest16 = (unsigned short*)pixels;
2731
2732 unsigned char *source;
2733 int inputPitch;
2734 if (packReverseRowOrder)
2735 {
2736 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2737 inputPitch = -lock.Pitch;
2738 }
2739 else
2740 {
2741 source = (unsigned char*)lock.pBits;
2742 inputPitch = lock.Pitch;
2743 }
2744
2745 unsigned int fastPixelSize = 0;
2746
2747 if (desc.Format == D3DFMT_A8R8G8B8 &&
2748 format == GL_BGRA_EXT &&
2749 type == GL_UNSIGNED_BYTE)
2750 {
2751 fastPixelSize = 4;
2752 }
2753 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2754 format == GL_BGRA_EXT &&
2755 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2756 (desc.Format == D3DFMT_A1R5G5B5 &&
2757 format == GL_BGRA_EXT &&
2758 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2759 {
2760 fastPixelSize = 2;
2761 }
2762 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2763 format == GL_RGBA &&
2764 type == GL_HALF_FLOAT_OES)
2765 {
2766 fastPixelSize = 8;
2767 }
2768 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2769 format == GL_RGBA &&
2770 type == GL_FLOAT)
2771 {
2772 fastPixelSize = 16;
2773 }
2774
2775 for (int j = 0; j < rect.bottom - rect.top; j++)
2776 {
2777 if (fastPixelSize != 0)
2778 {
2779 // Fast path for formats which require no translation:
2780 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2781 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2782 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2783 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2784 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2785 //
2786 // Note that buffers with no alpha go through the slow path below.
2787 memcpy(dest + j * outputPitch,
2788 source + j * inputPitch,
2789 (rect.right - rect.left) * fastPixelSize);
2790 continue;
2791 }
2792
2793 for (int i = 0; i < rect.right - rect.left; i++)
2794 {
2795 float r;
2796 float g;
2797 float b;
2798 float a;
2799
2800 switch (desc.Format)
2801 {
2802 case D3DFMT_R5G6B5:
2803 {
2804 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2805
2806 a = 1.0f;
2807 b = (rgb & 0x001F) * (1.0f / 0x001F);
2808 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2809 r = (rgb & 0xF800) * (1.0f / 0xF800);
2810 }
2811 break;
2812 case D3DFMT_A1R5G5B5:
2813 {
2814 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2815
2816 a = (argb & 0x8000) ? 1.0f : 0.0f;
2817 b = (argb & 0x001F) * (1.0f / 0x001F);
2818 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2819 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2820 }
2821 break;
2822 case D3DFMT_A8R8G8B8:
2823 {
2824 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2825
2826 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2827 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2828 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2829 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2830 }
2831 break;
2832 case D3DFMT_X8R8G8B8:
2833 {
2834 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2835
2836 a = 1.0f;
2837 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2838 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2839 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2840 }
2841 break;
2842 case D3DFMT_A2R10G10B10:
2843 {
2844 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2845
2846 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2847 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2848 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2849 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2850 }
2851 break;
2852 case D3DFMT_A32B32G32R32F:
2853 {
2854 // float formats in D3D are stored rgba, rather than the other way round
2855 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2856 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2857 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2858 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2859 }
2860 break;
2861 case D3DFMT_A16B16G16R16F:
2862 {
2863 // float formats in D3D are stored rgba, rather than the other way round
2864 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2865 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2866 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2867 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2868 }
2869 break;
2870 default:
2871 UNIMPLEMENTED(); // FIXME
2872 UNREACHABLE();
2873 return;
2874 }
2875
2876 switch (format)
2877 {
2878 case GL_RGBA:
2879 switch (type)
2880 {
2881 case GL_UNSIGNED_BYTE:
2882 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2883 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2884 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2885 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2886 break;
2887 default: UNREACHABLE();
2888 }
2889 break;
2890 case GL_BGRA_EXT:
2891 switch (type)
2892 {
2893 case GL_UNSIGNED_BYTE:
2894 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2895 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2896 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2897 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2898 break;
2899 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2900 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2901 // this type is packed as follows:
2902 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2903 // --------------------------------------------------------------------------------
2904 // | 4th | 3rd | 2nd | 1st component |
2905 // --------------------------------------------------------------------------------
2906 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2907 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2908 ((unsigned short)(15 * a + 0.5f) << 12)|
2909 ((unsigned short)(15 * r + 0.5f) << 8) |
2910 ((unsigned short)(15 * g + 0.5f) << 4) |
2911 ((unsigned short)(15 * b + 0.5f) << 0);
2912 break;
2913 case GL_UNSIGNED_SHORT_1_5_5_5_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)( a + 0.5f) << 15) |
2923 ((unsigned short)(31 * r + 0.5f) << 10) |
2924 ((unsigned short)(31 * g + 0.5f) << 5) |
2925 ((unsigned short)(31 * b + 0.5f) << 0);
2926 break;
2927 default: UNREACHABLE();
2928 }
2929 break;
2930 case GL_RGB:
2931 switch (type)
2932 {
2933 case GL_UNSIGNED_SHORT_5_6_5:
2934 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2935 ((unsigned short)(31 * b + 0.5f) << 0) |
2936 ((unsigned short)(63 * g + 0.5f) << 5) |
2937 ((unsigned short)(31 * r + 0.5f) << 11);
2938 break;
2939 case GL_UNSIGNED_BYTE:
2940 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2941 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2942 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2943 break;
2944 default: UNREACHABLE();
2945 }
2946 break;
2947 default: UNREACHABLE();
2948 }
2949 }
2950 }
2951
2952 systemSurface->UnlockRect();
2953
2954 systemSurface->Release();
2955}
2956
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002957RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2958{
2959 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2960 IDirect3DSurface9 *surface = NULL;
2961 if (depth)
2962 {
2963 surface = swapChain9->getDepthStencil();
2964 }
2965 else
2966 {
2967 surface = swapChain9->getRenderTarget();
2968 }
2969
2970 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2971
2972 return renderTarget;
2973}
2974
2975RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2976{
2977 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2978 return renderTarget;
2979}
2980
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002981ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002982{
2983 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002984
2985 switch (type)
2986 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002987 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00002988 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002989 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002990 if (vshader)
2991 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002992 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002993 }
2994 }
2995 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002996 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00002997 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002998 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002999 if (pshader)
3000 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003001 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003002 }
3003 }
3004 break;
3005 default:
3006 UNREACHABLE();
3007 break;
3008 }
3009
3010 return executable;
3011}
3012
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003013ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003014{
3015 const char *profile = NULL;
3016
3017 switch (type)
3018 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003019 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003020 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3021 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003022 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003023 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3024 break;
3025 default:
3026 UNREACHABLE();
3027 return NULL;
3028 }
3029
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00003030 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003031 if (!binary)
3032 return NULL;
3033
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003034 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003035 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003036
3037 return executable;
3038}
3039
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003040bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3041{
3042 return mBlit->boxFilter(source, dest);
3043}
3044
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003045D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003046{
3047 if (mD3d9Ex != NULL)
3048 {
3049 return D3DPOOL_DEFAULT;
3050 }
3051 else
3052 {
3053 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3054 {
3055 return D3DPOOL_MANAGED;
3056 }
3057 }
3058
3059 return D3DPOOL_DEFAULT;
3060}
3061
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003062bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3063{
3064 if (source && dest)
3065 {
3066 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003067
3068 if (fromManaged)
3069 {
3070 D3DSURFACE_DESC desc;
3071 source->GetDesc(&desc);
3072
3073 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003074 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003075
3076 if (SUCCEEDED(result))
3077 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003078 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003079 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003080 surf->Release();
3081 }
3082 }
3083 else
3084 {
3085 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003086 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003087 }
3088
3089 if (FAILED(result))
3090 {
3091 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3092 return false;
3093 }
3094 }
3095
3096 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003097}
3098
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003099Image *Renderer9::createImage()
3100{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003101 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003102}
3103
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003104void Renderer9::generateMipmap(Image *dest, Image *src)
3105{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003106 Image9 *src9 = Image9::makeImage9(src);
3107 Image9 *dst9 = Image9::makeImage9(dest);
3108 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003109}
3110
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003111TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3112{
3113 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3114 return new TextureStorage9_2D(this, swapChain9);
3115}
3116
3117TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3118{
3119 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3120}
3121
3122TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3123{
3124 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3125}
3126
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003127}