blob: ac8a10febc15333585d08d8de6152033ea6a50f0 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002//
daniel@transgaming.com669c9952013-01-11 04:08:16 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00008// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009
Geoff Lang2a64ee42013-05-31 11:22:40 -040010#include "common/utilities.h"
11
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/main.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000014#include "libGLESv2/Texture.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000015#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000016#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000017#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000018#include "libGLESv2/renderer/IndexDataManager.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"
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000021#include "libGLESv2/renderer/formatutils9.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000022#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000023#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com34da3972012-12-20 21:10:29 +000024#include "libGLESv2/renderer/TextureStorage9.h"
daniel@transgaming.com4ba24062012-12-20 20:54:24 +000025#include "libGLESv2/renderer/Image9.h"
Geoff Langdce735c2013-06-04 10:21:18 -040026#include "libGLESv2/renderer/Blit9.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000027#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com3f255b42012-12-20 21:07:35 +000028#include "libGLESv2/renderer/VertexBuffer9.h"
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +000029#include "libGLESv2/renderer/IndexBuffer9.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000030#include "libGLESv2/renderer/BufferStorage9.h"
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +000031#include "libGLESv2/renderer/Query9.h"
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +000032#include "libGLESv2/renderer/Fence9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000033
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
daniel@transgaming.com621ce052012-10-31 17:52:29 +000036// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
37#define REF_RAST 0
38
39// The "Debug This Pixel..." feature in PIX often fails when using the
40// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
41// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
42#if !defined(ANGLE_ENABLE_D3D9EX)
43// Enables use of the IDirect3D9Ex interface, when available
44#define ANGLE_ENABLE_D3D9EX 1
45#endif // !defined(ANGLE_ENABLE_D3D9EX)
46
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000047#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
48#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
49#endif
50
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000051const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z')));
52const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')));
53
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000054namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000055{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000056static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000057 {
58 D3DFMT_A1R5G5B5,
59 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
60 D3DFMT_A8R8G8B8,
61 D3DFMT_R5G6B5,
62 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
63 D3DFMT_X8R8G8B8
64 };
65
daniel@transgaming.com222ee082012-11-28 19:31:49 +000066static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000067 {
68 D3DFMT_UNKNOWN,
69 // D3DFMT_D16_LOCKABLE,
70 D3DFMT_D32,
71 // D3DFMT_D15S1,
72 D3DFMT_D24S8,
73 D3DFMT_D24X8,
74 // D3DFMT_D24X4S4,
75 D3DFMT_D16,
76 // D3DFMT_D32F_LOCKABLE,
77 // D3DFMT_D24FS8
78 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000079
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000080enum
81{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +000082 MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
83 MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32,
84 MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224,
85 MAX_VARYING_VECTORS_SM2 = 8,
86 MAX_VARYING_VECTORS_SM3 = 10,
87
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000088 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
89};
90
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000091Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000092{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000093 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000094
95 mD3d9 = NULL;
96 mD3d9Ex = NULL;
97 mDevice = NULL;
98 mDeviceEx = NULL;
99 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000100 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000101
102 mAdapter = D3DADAPTER_DEFAULT;
103
104 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
105 mDeviceType = D3DDEVTYPE_REF;
106 #else
107 mDeviceType = D3DDEVTYPE_HAL;
108 #endif
109
110 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000111
112 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000113
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +0000114 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000115
116 mVertexDataManager = NULL;
117 mIndexDataManager = NULL;
118 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000119
120 mMaxNullColorbufferLRU = 0;
121 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
122 {
123 mNullColorbufferCache[i].lruCount = 0;
124 mNullColorbufferCache[i].width = 0;
125 mNullColorbufferCache[i].height = 0;
126 mNullColorbufferCache[i].buffer = NULL;
127 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000128}
129
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000130Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000131{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000132 releaseDeviceResources();
133
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000134 if (mDevice)
135 {
136 // 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 +0000137 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000138 {
139 resetDevice();
140 }
141
142 mDevice->Release();
143 mDevice = NULL;
144 }
145
146 if (mDeviceEx)
147 {
148 mDeviceEx->Release();
149 mDeviceEx = NULL;
150 }
151
152 if (mD3d9)
153 {
154 mD3d9->Release();
155 mD3d9 = NULL;
156 }
157
158 if (mDeviceWindow)
159 {
160 DestroyWindow(mDeviceWindow);
161 mDeviceWindow = NULL;
162 }
163
164 if (mD3d9Ex)
165 {
166 mD3d9Ex->Release();
167 mD3d9Ex = NULL;
168 }
169
170 if (mD3d9Module)
171 {
172 mD3d9Module = NULL;
173 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000174}
175
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000176Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
177{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000178 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000179 return static_cast<rx::Renderer9*>(renderer);
180}
181
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000182EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000183{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000184 if (!initializeCompiler())
185 {
186 return EGL_NOT_INITIALIZED;
187 }
188
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000189 if (mSoftwareDevice)
190 {
191 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
192 }
193 else
194 {
195 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
196 }
197
198 if (mD3d9Module == NULL)
199 {
200 ERR("No D3D9 module found - aborting!\n");
201 return EGL_NOT_INITIALIZED;
202 }
203
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000204 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
205 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
206
207 // Use Direct3D9Ex if available. Among other things, this version is less
208 // inclined to report a lost context, for example when the user switches
209 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
210 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
211 {
212 ASSERT(mD3d9Ex);
213 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
214 ASSERT(mD3d9);
215 }
216 else
217 {
218 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
219 }
220
221 if (!mD3d9)
222 {
223 ERR("Could not create D3D9 device - aborting!\n");
224 return EGL_NOT_INITIALIZED;
225 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000226
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000227 if (mDc != NULL)
228 {
229 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
230 }
231
232 HRESULT result;
233
234 // Give up on getting device caps after about one second.
235 for (int i = 0; i < 10; ++i)
236 {
237 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
238 if (SUCCEEDED(result))
239 {
240 break;
241 }
242 else if (result == D3DERR_NOTAVAILABLE)
243 {
244 Sleep(100); // Give the driver some time to initialize/recover
245 }
246 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
247 {
248 ERR("failed to get device caps (0x%x)\n", result);
249 return EGL_NOT_INITIALIZED;
250 }
251 }
252
253 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
254 {
255 ERR("Renderer does not support PS 2.0. aborting!\n");
256 return EGL_NOT_INITIALIZED;
257 }
258
259 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
260 // This is required by Texture2D::convertToRenderTarget.
261 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
262 {
263 ERR("Renderer does not support stretctrect from textures!\n");
264 return EGL_NOT_INITIALIZED;
265 }
266
267 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
268
269 // ATI cards on XP have problems with non-power-of-two textures.
270 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
271 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
272 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
273 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
274
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000275 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
276 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
277
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000278 mMinSwapInterval = 4;
279 mMaxSwapInterval = 0;
280
281 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
282 {
283 mMinSwapInterval = std::min(mMinSwapInterval, 0);
284 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
285 }
286 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
287 {
288 mMinSwapInterval = std::min(mMinSwapInterval, 1);
289 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
290 }
291 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
292 {
293 mMinSwapInterval = std::min(mMinSwapInterval, 2);
294 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
295 }
296 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
297 {
298 mMinSwapInterval = std::min(mMinSwapInterval, 3);
299 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
300 }
301 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
302 {
303 mMinSwapInterval = std::min(mMinSwapInterval, 4);
304 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
305 }
306
Geoff Lang61e49a52013-05-29 10:22:58 -0400307 mMaxSupportedSamples = 0;
308
309 const d3d9::D3DFormatSet &d3d9Formats = d3d9::GetAllUsedD3DFormats();
310 for (d3d9::D3DFormatSet::const_iterator i = d3d9Formats.begin(); i != d3d9Formats.end(); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000311 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400312 MultisampleSupportInfo support = getMultiSampleSupport(*i);
313 mMultiSampleSupport[*i] = support;
314 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
daniel@transgaming.com92955622012-10-31 18:38:41 +0000315 }
316
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000317 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
318 static const TCHAR className[] = TEXT("STATIC");
319
320 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
321
322 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
323 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
324
325 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
326 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
327 {
328 return EGL_BAD_ALLOC;
329 }
330
331 if (FAILED(result))
332 {
333 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
334
335 if (FAILED(result))
336 {
337 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
338 return EGL_BAD_ALLOC;
339 }
340 }
341
342 if (mD3d9Ex)
343 {
344 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
345 ASSERT(SUCCEEDED(result));
346 }
347
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000348 mVertexShaderCache.initialize(mDevice);
349 mPixelShaderCache.initialize(mDevice);
350
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000351 // Check occlusion query support
352 IDirect3DQuery9 *occlusionQuery = NULL;
353 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
354 {
355 occlusionQuery->Release();
356 mOcclusionQuerySupport = true;
357 }
358 else
359 {
360 mOcclusionQuerySupport = false;
361 }
362
363 // Check event query support
364 IDirect3DQuery9 *eventQuery = NULL;
365 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
366 {
367 eventQuery->Release();
368 mEventQuerySupport = true;
369 }
370 else
371 {
372 mEventQuerySupport = false;
373 }
374
375 D3DDISPLAYMODE currentDisplayMode;
376 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
377
378 // Check vertex texture support
379 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
380 // We test this using D3D9 by checking support for the R16F format.
381 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
382 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
383 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
384
Geoff Langd42cf4e2013-06-05 16:09:17 -0400385 // Check RGB565 texture support
386 mRGB565TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
387 D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_R5G6B5));
388
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000389 // Check depth texture support
390 // we use INTZ for depth textures in Direct3D9
391 // we also want NULL texture support to ensure the we can make depth-only FBOs
392 // see http://aras-p.info/texts/D3D9GPUHacks.html
393 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
394 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
395 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
396 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
397
398 // Check 32 bit floating point texture support
399 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
400 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
401 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
402 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
403
404 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
405 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
406 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
407 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
408
409 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
410 {
411 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
412 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
413 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
414 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
415 }
416 else
417 {
418 mFloat32TextureSupport = true;
419 }
420
421 // Check 16 bit floating point texture support
422 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
423 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
424 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
425 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
426
427 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
428 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
429 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
430 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
431
432 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
433 {
434 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
435 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
436 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
437 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
438 }
439 else
440 {
441 mFloat16TextureSupport = true;
442 }
443
444 // Check DXT texture support
445 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
446 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
447 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
448
449 // Check luminance[alpha] texture support
450 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
451 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
452
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000453 initializeDevice();
454
455 return EGL_SUCCESS;
456}
457
458// do any one-time device initialization
459// NOTE: this is also needed after a device lost/reset
460// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000461void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000462{
463 // Permanent non-default states
464 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
465 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
466
467 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
468 {
469 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
470 }
471 else
472 {
473 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
474 }
475
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000476 markAllStateDirty();
477
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000478 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000479
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000480 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400481 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000482 mVertexDataManager = new rx::VertexDataManager(this);
483 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000484}
485
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000486D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000487{
488 D3DPRESENT_PARAMETERS presentParameters = {0};
489
490 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
491 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
492 presentParameters.BackBufferCount = 1;
493 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
494 presentParameters.BackBufferWidth = 1;
495 presentParameters.BackBufferHeight = 1;
496 presentParameters.EnableAutoDepthStencil = FALSE;
497 presentParameters.Flags = 0;
498 presentParameters.hDeviceWindow = mDeviceWindow;
499 presentParameters.MultiSampleQuality = 0;
500 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
501 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
502 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
503 presentParameters.Windowed = TRUE;
504
505 return presentParameters;
506}
507
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000508int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000509{
510 D3DDISPLAYMODE currentDisplayMode;
511 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
512
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000513 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
514 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000515 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
516 int numConfigs = 0;
517
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000518 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000519 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000520 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000521
522 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
523
524 if (SUCCEEDED(result))
525 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000526 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000527 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000528 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000529 HRESULT result = D3D_OK;
530
531 if(depthStencilFormat != D3DFMT_UNKNOWN)
532 {
533 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
534 }
535
536 if (SUCCEEDED(result))
537 {
538 if(depthStencilFormat != D3DFMT_UNKNOWN)
539 {
540 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
541 }
542
543 if (SUCCEEDED(result))
544 {
545 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000546 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
547 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000548 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
549 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000550 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000551
552 (*configDescList)[numConfigs++] = newConfig;
553 }
554 }
555 }
556 }
557 }
558
559 return numConfigs;
560}
561
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000562void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000563{
564 delete [] (configDescList);
565}
566
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000567void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000568{
569 if (!mSceneStarted)
570 {
571 long result = mDevice->BeginScene();
572 if (SUCCEEDED(result)) {
573 // This is defensive checking against the device being
574 // lost at unexpected times.
575 mSceneStarted = true;
576 }
577 }
578}
579
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000580void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000581{
582 if (mSceneStarted)
583 {
584 // EndScene can fail if the device was lost, for example due
585 // to a TDR during a draw call.
586 mDevice->EndScene();
587 mSceneStarted = false;
588 }
589}
590
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000591void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000592{
593 HRESULT result;
594
595 IDirect3DQuery9* query = allocateEventQuery();
596 if (!query)
597 {
598 return;
599 }
600
601 result = query->Issue(D3DISSUE_END);
602 ASSERT(SUCCEEDED(result));
603
604 do
605 {
606 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
607
608 if(block && result == S_FALSE)
609 {
610 // Keep polling, but allow other threads to do something useful first
611 Sleep(0);
612 // explicitly check for device loss
613 // some drivers seem to return S_FALSE even if the device is lost
614 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000615 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000616 {
617 result = D3DERR_DEVICELOST;
618 }
619 }
620 }
621 while(block && result == S_FALSE);
622
623 freeEventQuery(query);
624
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000625 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000626 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000627 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000628 }
629}
630
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000631SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
632{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000633 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000634}
635
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000636IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000637{
638 IDirect3DQuery9 *query = NULL;
639
640 if (mEventQueryPool.empty())
641 {
642 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
643 ASSERT(SUCCEEDED(result));
644 }
645 else
646 {
647 query = mEventQueryPool.back();
648 mEventQueryPool.pop_back();
649 }
650
651 return query;
652}
653
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000654void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000655{
656 if (mEventQueryPool.size() > 1000)
657 {
658 query->Release();
659 }
660 else
661 {
662 mEventQueryPool.push_back(query);
663 }
664}
665
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000666IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000667{
668 return mVertexShaderCache.create(function, length);
669}
670
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000671IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000672{
673 return mPixelShaderCache.create(function, length);
674}
675
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000676HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
677{
678 D3DPOOL Pool = getBufferPool(Usage);
679 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
680}
681
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000682VertexBuffer *Renderer9::createVertexBuffer()
683{
684 return new VertexBuffer9(this);
685}
686
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000687HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
688{
689 D3DPOOL Pool = getBufferPool(Usage);
690 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
691}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000692
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000693IndexBuffer *Renderer9::createIndexBuffer()
694{
695 return new IndexBuffer9(this);
696}
697
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000698BufferStorage *Renderer9::createBufferStorage()
699{
700 return new BufferStorage9();
701}
702
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000703QueryImpl *Renderer9::createQuery(GLenum type)
704{
705 return new Query9(this, type);
706}
707
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000708FenceImpl *Renderer9::createFence()
709{
710 return new Fence9(this);
711}
712
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000713void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000714{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000715 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
716 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000717
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000718 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000719 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000720 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
721 int d3dSampler = index + d3dSamplerOffset;
722
723 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
724 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
725
726 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
727 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
728 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
729 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
730 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
731 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
732 if (mSupportsTextureFilterAnisotropy)
733 {
734 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
735 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000736 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000737
738 forceSetSamplers[index] = false;
739 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000740}
741
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000742void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000743{
744 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
745 int d3dSampler = index + d3dSamplerOffset;
746 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000747 unsigned int serial = 0;
748 bool forceSetTexture = false;
749
750 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000751
752 if (texture)
753 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000754 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000755 if (texStorage)
756 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000757 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000758 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000759 }
760 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000761 // in the texture class and we're unexpectedly missing the d3d texture
762 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000763
764 serial = texture->getTextureSerial();
765 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000766 }
767
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000768 if (forceSetTexture || appliedSerials[index] != serial)
769 {
770 mDevice->SetTexture(d3dSampler, d3dTexture);
771 }
772
773 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000774}
775
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000776bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
777{
778 // No effect in ES2/D3D9
779 return true;
780}
781
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000782void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000783{
784 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000785
786 if (rasterStateChanged)
787 {
788 // Set the cull mode
789 if (rasterState.cullFace)
790 {
791 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
792 }
793 else
794 {
795 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
796 }
797
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000798 if (rasterState.polygonOffsetFill)
799 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000800 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000801 {
802 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
803
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000804 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000805 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
806 }
807 }
808 else
809 {
810 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
811 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
812 }
813
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000814 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000815 }
816
817 mForceSetRasterState = false;
818}
819
Geoff Lang2a64ee42013-05-31 11:22:40 -0400820void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor, unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000821{
822 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400823 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000824 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
825
826 if (blendStateChanged || blendColorChanged)
827 {
828 if (blendState.blend)
829 {
830 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
831
832 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
833 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
834 {
835 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
836 }
837 else
838 {
839 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
840 gl::unorm<8>(blendColor.alpha),
841 gl::unorm<8>(blendColor.alpha),
842 gl::unorm<8>(blendColor.alpha)));
843 }
844
845 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
846 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
847 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
848
849 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
850 blendState.destBlendRGB != blendState.destBlendAlpha ||
851 blendState.blendEquationRGB != blendState.blendEquationAlpha)
852 {
853 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
854
855 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
856 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
857 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
858 }
859 else
860 {
861 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
862 }
863 }
864 else
865 {
866 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
867 }
868
869 if (blendState.sampleAlphaToCoverage)
870 {
871 FIXME("Sample alpha to coverage is unimplemented.");
872 }
873
874 // Set the color mask
875 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
876 // Apparently some ATI cards have a bug where a draw with a zero color
877 // write mask can cause later draws to have incorrect results. Instead,
878 // set a nonzero color write mask but modify the blend state so that no
879 // drawing is done.
880 // http://code.google.com/p/angleproject/issues/detail?id=169
881
882 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
883 blendState.colorMaskBlue, blendState.colorMaskAlpha);
884 if (colorMask == 0 && !zeroColorMaskAllowed)
885 {
886 // Enable green channel, but set blending so nothing will be drawn.
887 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
888 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
889
890 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
891 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
892 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
893 }
894 else
895 {
896 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
897 }
898
899 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
900
901 mCurBlendState = blendState;
902 mCurBlendColor = blendColor;
903 }
904
905 if (sampleMaskChanged)
906 {
907 // Set the multisample mask
908 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
909 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
910
911 mCurSampleMask = sampleMask;
912 }
913
914 mForceSetBlendState = false;
915}
916
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000917void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000918 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000919{
920 bool depthStencilStateChanged = mForceSetDepthStencilState ||
921 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000922 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
923 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000924 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000925
926 if (depthStencilStateChanged)
927 {
928 if (depthStencilState.depthTest)
929 {
930 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
931 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
932 }
933 else
934 {
935 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
936 }
937
938 mCurDepthStencilState = depthStencilState;
939 }
940
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000941 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000942 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000943 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000944 {
945 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
946 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
947
948 // FIXME: Unsupported by D3D9
949 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
950 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
951 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
952 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000953 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000954 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
955 {
956 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000957 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000958 }
959
960 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000961 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000962
963 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
964 depthStencilState.stencilWritemask);
965 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
966 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
967
968 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000969 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000970 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
971 depthStencilState.stencilMask);
972
973 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
974 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
975 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
976 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
977 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
978 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
979
980 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
981 depthStencilState.stencilBackWritemask);
982 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
983 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
984
985 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000986 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000987 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
988 depthStencilState.stencilBackMask);
989
990 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
991 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
992 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
993 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
994 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
995 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
996 }
997 else
998 {
999 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1000 }
1001
1002 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
1003
daniel@transgaming.com08c331d2012-11-28 19:38:39 +00001004 mCurStencilRef = stencilRef;
1005 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001006 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001007 }
1008
1009 mForceSetDepthStencilState = false;
1010}
1011
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001012void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001013{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001014 bool scissorChanged = mForceSetScissor ||
1015 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1016 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001017
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001018 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001019 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001020 if (enabled)
1021 {
1022 RECT rect;
1023 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1024 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1025 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1026 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1027 mDevice->SetScissorRect(&rect);
1028 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001029
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001030 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1031
1032 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001033 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001034 }
1035
1036 mForceSetScissor = false;
1037}
1038
daniel@transgaming.com12985182012-12-20 20:56:31 +00001039bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001040 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001041{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001042 gl::Rectangle actualViewport = viewport;
1043 float actualZNear = gl::clamp01(zNear);
1044 float actualZFar = gl::clamp01(zFar);
1045 if (ignoreViewport)
1046 {
1047 actualViewport.x = 0;
1048 actualViewport.y = 0;
1049 actualViewport.width = mRenderTargetDesc.width;
1050 actualViewport.height = mRenderTargetDesc.height;
1051 actualZNear = 0.0f;
1052 actualZFar = 1.0f;
1053 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001054
1055 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001056 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1057 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1058 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1059 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1060 dxViewport.MinZ = actualZNear;
1061 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001062
1063 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1064 {
1065 return false; // Nothing to render
1066 }
1067
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001068 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1069 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001070 if (viewportChanged)
1071 {
1072 mDevice->SetViewport(&dxViewport);
1073
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001074 mCurViewport = actualViewport;
1075 mCurNear = actualZNear;
1076 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001077
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001078 dx_VertexConstants vc = {0};
1079 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001080
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001081 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1082 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1083 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1084 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001085
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001086 pc.viewCoords[0] = actualViewport.width * 0.5f;
1087 pc.viewCoords[1] = actualViewport.height * 0.5f;
1088 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1089 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001090
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001091 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1092 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1093 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1094
1095 vc.depthRange[0] = actualZNear;
1096 vc.depthRange[1] = actualZFar;
1097 vc.depthRange[2] = actualZFar - actualZNear;
1098
1099 pc.depthRange[0] = actualZNear;
1100 pc.depthRange[1] = actualZFar;
1101 pc.depthRange[2] = actualZFar - actualZNear;
1102
1103 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1104 {
1105 mVertexConstants = vc;
1106 mDxUniformsDirty = true;
1107 }
1108
1109 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1110 {
1111 mPixelConstants = pc;
1112 mDxUniformsDirty = true;
1113 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001114 }
1115
1116 mForceSetViewport = false;
1117 return true;
1118}
1119
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001120bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1121{
1122 switch (mode)
1123 {
1124 case GL_POINTS:
1125 mPrimitiveType = D3DPT_POINTLIST;
1126 mPrimitiveCount = count;
1127 break;
1128 case GL_LINES:
1129 mPrimitiveType = D3DPT_LINELIST;
1130 mPrimitiveCount = count / 2;
1131 break;
1132 case GL_LINE_LOOP:
1133 mPrimitiveType = D3DPT_LINESTRIP;
1134 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1135 break;
1136 case GL_LINE_STRIP:
1137 mPrimitiveType = D3DPT_LINESTRIP;
1138 mPrimitiveCount = count - 1;
1139 break;
1140 case GL_TRIANGLES:
1141 mPrimitiveType = D3DPT_TRIANGLELIST;
1142 mPrimitiveCount = count / 3;
1143 break;
1144 case GL_TRIANGLE_STRIP:
1145 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1146 mPrimitiveCount = count - 2;
1147 break;
1148 case GL_TRIANGLE_FAN:
1149 mPrimitiveType = D3DPT_TRIANGLEFAN;
1150 mPrimitiveCount = count - 2;
1151 break;
1152 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001153 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001154 }
1155
1156 return mPrimitiveCount > 0;
1157}
1158
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001159
1160gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1161{
1162 if (!depthbuffer)
1163 {
1164 ERR("Unexpected null depthbuffer for depth-only FBO.");
1165 return NULL;
1166 }
1167
1168 GLsizei width = depthbuffer->getWidth();
1169 GLsizei height = depthbuffer->getHeight();
1170
1171 // search cached nullcolorbuffers
1172 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1173 {
1174 if (mNullColorbufferCache[i].buffer != NULL &&
1175 mNullColorbufferCache[i].width == width &&
1176 mNullColorbufferCache[i].height == height)
1177 {
1178 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1179 return mNullColorbufferCache[i].buffer;
1180 }
1181 }
1182
1183 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1184
1185 // add nullbuffer to the cache
1186 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1187 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1188 {
1189 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1190 {
1191 oldest = &mNullColorbufferCache[i];
1192 }
1193 }
1194
1195 delete oldest->buffer;
1196 oldest->buffer = nullbuffer;
1197 oldest->lruCount = ++mMaxNullColorbufferLRU;
1198 oldest->width = width;
1199 oldest->height = height;
1200
1201 return nullbuffer;
1202}
1203
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001204bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001205{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001206 // if there is no color attachment we must synthesize a NULL colorattachment
1207 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1208 gl::Renderbuffer *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001209 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001210 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001211 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001212 }
1213 else
1214 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001215 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001216 }
1217 if (!renderbufferObject)
1218 {
1219 ERR("unable to locate renderbuffer for FBO.");
1220 return false;
1221 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001222
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001223 bool renderTargetChanged = false;
1224 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1225 if (renderTargetSerial != mAppliedRenderTargetSerial)
1226 {
1227 // Apply the render target on the device
1228 IDirect3DSurface9 *renderTargetSurface = NULL;
1229
1230 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1231 if (renderTarget)
1232 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001233 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001234 }
1235
1236 if (!renderTargetSurface)
1237 {
1238 ERR("render target pointer unexpectedly null.");
1239 return false; // Context must be lost
1240 }
1241
1242 mDevice->SetRenderTarget(0, renderTargetSurface);
1243 renderTargetSurface->Release();
1244
1245 mAppliedRenderTargetSerial = renderTargetSerial;
1246 renderTargetChanged = true;
1247 }
1248
1249 gl::Renderbuffer *depthStencil = NULL;
1250 unsigned int depthbufferSerial = 0;
1251 unsigned int stencilbufferSerial = 0;
1252 if (framebuffer->getDepthbufferType() != GL_NONE)
1253 {
1254 depthStencil = framebuffer->getDepthbuffer();
1255 if (!depthStencil)
1256 {
1257 ERR("Depth stencil pointer unexpectedly null.");
1258 return false;
1259 }
1260
1261 depthbufferSerial = depthStencil->getSerial();
1262 }
1263 else if (framebuffer->getStencilbufferType() != GL_NONE)
1264 {
1265 depthStencil = framebuffer->getStencilbuffer();
1266 if (!depthStencil)
1267 {
1268 ERR("Depth stencil pointer unexpectedly null.");
1269 return false;
1270 }
1271
1272 stencilbufferSerial = depthStencil->getSerial();
1273 }
1274
1275 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1276 stencilbufferSerial != mAppliedStencilbufferSerial ||
1277 !mDepthStencilInitialized)
1278 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001279 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001280 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001281
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001282 // Apply the depth stencil on the device
1283 if (depthStencil)
1284 {
1285 IDirect3DSurface9 *depthStencilSurface = NULL;
1286 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1287
1288 if (depthStencilRenderTarget)
1289 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001290 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001291 }
1292
1293 if (!depthStencilSurface)
1294 {
1295 ERR("depth stencil pointer unexpectedly null.");
1296 return false; // Context must be lost
1297 }
1298
1299 mDevice->SetDepthStencilSurface(depthStencilSurface);
1300 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001301
1302 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001303 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001304 }
1305 else
1306 {
1307 mDevice->SetDepthStencilSurface(NULL);
1308 }
1309
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001310 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1311 {
1312 mCurDepthSize = depthSize;
1313 mForceSetRasterState = true;
1314 }
1315
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001316 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1317 {
1318 mCurStencilSize = stencilSize;
1319 mForceSetDepthStencilState = true;
1320 }
1321
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001322 mAppliedDepthbufferSerial = depthbufferSerial;
1323 mAppliedStencilbufferSerial = stencilbufferSerial;
1324 mDepthStencilInitialized = true;
1325 }
1326
1327 if (renderTargetChanged || !mRenderTargetDescInitialized)
1328 {
1329 mForceSetScissor = true;
1330 mForceSetViewport = true;
1331
1332 mRenderTargetDesc.width = renderbufferObject->getWidth();
1333 mRenderTargetDesc.height = renderbufferObject->getHeight();
1334 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1335 mRenderTargetDescInitialized = true;
1336 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001337
1338 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001339}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001340
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001341GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001342{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001343 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001344 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1345 if (err != GL_NO_ERROR)
1346 {
1347 return err;
1348 }
1349
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001350 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1351}
1352
1353// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001354GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001355{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001356 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001357
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001358 if (err == GL_NO_ERROR)
1359 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001360 // Directly binding the storage buffer is not supported for d3d9
1361 ASSERT(indexInfo->storage == NULL);
1362
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001363 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001364 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001365 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1366
1367 mDevice->SetIndices(indexBuffer->getBuffer());
1368 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001369 }
1370 }
1371
1372 return err;
1373}
1374
1375void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1376{
1377 startScene();
1378
1379 if (mode == GL_LINE_LOOP)
1380 {
1381 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1382 }
1383 else if (instances > 0)
1384 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001385 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001386 if (countingIB)
1387 {
1388 if (mAppliedIBSerial != countingIB->getSerial())
1389 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001390 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1391
1392 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001393 mAppliedIBSerial = countingIB->getSerial();
1394 }
1395
1396 for (int i = 0; i < mRepeatDraw; i++)
1397 {
1398 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1399 }
1400 }
1401 else
1402 {
1403 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001404 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001405 }
1406 }
1407 else // Regular case
1408 {
1409 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1410 }
1411}
1412
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001413void 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 +00001414{
1415 startScene();
1416
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001417 if (mode == GL_POINTS)
1418 {
1419 drawIndexedPoints(count, type, indices, elementArrayBuffer);
1420 }
1421 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001422 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001423 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001424 }
1425 else
1426 {
1427 for (int i = 0; i < mRepeatDraw; i++)
1428 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001429 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1430 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001431 }
1432 }
1433}
1434
1435void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1436{
1437 // Get the raw indices for an indexed draw
1438 if (type != GL_NONE && elementArrayBuffer)
1439 {
1440 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001441 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001442 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001443 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001444 }
1445
1446 UINT startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001447
1448 if (get32BitIndexSupport())
1449 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001450 if (!mLineLoopIB)
1451 {
1452 mLineLoopIB = new StreamingIndexBufferInterface(this);
1453 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1454 {
1455 delete mLineLoopIB;
1456 mLineLoopIB = NULL;
1457
1458 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001459 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001460 }
1461 }
1462
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001463 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001464 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001465 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001466 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001467 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001468 }
1469
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001470 void* mappedMemory = NULL;
1471 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1472 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001473 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001474 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001475 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001476 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001477
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001478 startIndex = static_cast<UINT>(offset) / 4;
1479 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1480
1481 switch (type)
1482 {
1483 case GL_NONE: // Non-indexed draw
1484 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001485 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001486 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001487 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001488 data[count] = 0;
1489 break;
1490 case GL_UNSIGNED_BYTE:
1491 for (int i = 0; i < count; i++)
1492 {
1493 data[i] = static_cast<const GLubyte*>(indices)[i];
1494 }
1495 data[count] = static_cast<const GLubyte*>(indices)[0];
1496 break;
1497 case GL_UNSIGNED_SHORT:
1498 for (int i = 0; i < count; i++)
1499 {
1500 data[i] = static_cast<const GLushort*>(indices)[i];
1501 }
1502 data[count] = static_cast<const GLushort*>(indices)[0];
1503 break;
1504 case GL_UNSIGNED_INT:
1505 for (int i = 0; i < count; i++)
1506 {
1507 data[i] = static_cast<const GLuint*>(indices)[i];
1508 }
1509 data[count] = static_cast<const GLuint*>(indices)[0];
1510 break;
1511 default: UNREACHABLE();
1512 }
1513
1514 if (!mLineLoopIB->unmapBuffer())
1515 {
1516 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001517 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001518 }
1519 }
1520 else
1521 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001522 if (!mLineLoopIB)
1523 {
1524 mLineLoopIB = new StreamingIndexBufferInterface(this);
1525 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1526 {
1527 delete mLineLoopIB;
1528 mLineLoopIB = NULL;
1529
1530 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001531 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001532 }
1533 }
1534
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001535 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001536 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001537 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001538 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001539 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001540 }
1541
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001542 void* mappedMemory = NULL;
1543 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1544 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001545 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001546 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001547 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001548 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001549
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001550 startIndex = static_cast<UINT>(offset) / 2;
1551 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1552
1553 switch (type)
1554 {
1555 case GL_NONE: // Non-indexed draw
1556 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001557 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001558 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001559 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001560 data[count] = 0;
1561 break;
1562 case GL_UNSIGNED_BYTE:
1563 for (int i = 0; i < count; i++)
1564 {
1565 data[i] = static_cast<const GLubyte*>(indices)[i];
1566 }
1567 data[count] = static_cast<const GLubyte*>(indices)[0];
1568 break;
1569 case GL_UNSIGNED_SHORT:
1570 for (int i = 0; i < count; i++)
1571 {
1572 data[i] = static_cast<const GLushort*>(indices)[i];
1573 }
1574 data[count] = static_cast<const GLushort*>(indices)[0];
1575 break;
1576 case GL_UNSIGNED_INT:
1577 for (int i = 0; i < count; i++)
1578 {
1579 data[i] = static_cast<const GLuint*>(indices)[i];
1580 }
1581 data[count] = static_cast<const GLuint*>(indices)[0];
1582 break;
1583 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001584 }
1585
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001586 if (!mLineLoopIB->unmapBuffer())
1587 {
1588 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001589 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001590 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001591 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001592
1593 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001594 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001595 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1596
1597 mDevice->SetIndices(indexBuffer->getBuffer());
1598 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001599 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001600
1601 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001602}
1603
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001604template <typename T>
1605static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1606{
1607 for (int i = 0; i < count; i++)
1608 {
1609 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1610 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1611 }
1612}
1613
1614void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1615{
1616 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1617 // for each individual point. This call is not expected to happen often.
1618
1619 if (elementArrayBuffer)
1620 {
1621 BufferStorage *storage = elementArrayBuffer->getStorage();
1622 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1623 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1624 }
1625
1626 switch (type)
1627 {
1628 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1629 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1630 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1631 default: UNREACHABLE();
1632 }
1633}
1634
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001635void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1636{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001637 unsigned int programBinarySerial = programBinary->getSerial();
1638 if (programBinarySerial != mAppliedProgramBinarySerial)
1639 {
1640 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1641 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001642
daniel@transgaming.come4991412012-12-20 20:55:34 +00001643 IDirect3DVertexShader9 *vertexShader = NULL;
1644 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001645
daniel@transgaming.come4991412012-12-20 20:55:34 +00001646 IDirect3DPixelShader9 *pixelShader = NULL;
1647 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001648
daniel@transgaming.come4991412012-12-20 20:55:34 +00001649 mDevice->SetPixelShader(pixelShader);
1650 mDevice->SetVertexShader(vertexShader);
1651 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001652 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001653
1654 mAppliedProgramBinarySerial = programBinarySerial;
1655 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001656}
1657
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001658void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001659{
1660 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1661 {
1662 gl::Uniform *targetUniform = *ub;
1663
1664 if (targetUniform->dirty)
1665 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001666 GLfloat *f = (GLfloat*)targetUniform->data;
1667 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001668
1669 switch (targetUniform->type)
1670 {
1671 case GL_SAMPLER_2D:
1672 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001673 break;
1674 case GL_BOOL:
1675 case GL_BOOL_VEC2:
1676 case GL_BOOL_VEC3:
1677 case GL_BOOL_VEC4:
1678 applyUniformnbv(targetUniform, i);
1679 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001680 case GL_FLOAT:
1681 case GL_FLOAT_VEC2:
1682 case GL_FLOAT_VEC3:
1683 case GL_FLOAT_VEC4:
1684 case GL_FLOAT_MAT2:
1685 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001686 case GL_FLOAT_MAT4:
1687 applyUniformnfv(targetUniform, f);
1688 break;
1689 case GL_INT:
1690 case GL_INT_VEC2:
1691 case GL_INT_VEC3:
1692 case GL_INT_VEC4:
1693 applyUniformniv(targetUniform, i);
1694 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001695 default:
1696 UNREACHABLE();
1697 }
1698
1699 targetUniform->dirty = false;
1700 }
1701 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001702
1703 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001704 if (mDxUniformsDirty)
1705 {
1706 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1707 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1708 mDxUniformsDirty = false;
1709 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001710}
1711
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001712void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001713{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001714 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001715 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001716 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001717 }
1718
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001719 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001720 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001721 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001722 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001723}
1724
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001725void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001726{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001727 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1728 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001729
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001730 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001731 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001732 vector[i][0] = (GLfloat)v[4 * i + 0];
1733 vector[i][1] = (GLfloat)v[4 * i + 1];
1734 vector[i][2] = (GLfloat)v[4 * i + 2];
1735 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736 }
1737
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001738 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739}
1740
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001741void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001743 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1744 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001745
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001746 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001747 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001748 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1749 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1750 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1751 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001752 }
1753
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001754 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001755}
1756
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001757void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001758{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001759 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1760 gl::unorm<8>(clearParams.colorClearValue.red),
1761 gl::unorm<8>(clearParams.colorClearValue.green),
1762 gl::unorm<8>(clearParams.colorClearValue.blue));
1763 float depth = gl::clamp01(clearParams.depthClearValue);
1764 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001765
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001766 unsigned int stencilUnmasked = 0x0;
1767 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1768 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001769 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1770 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001771 stencilUnmasked = (0x1 << stencilSize) - 1;
1772 }
1773
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001774 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1775 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001776
1777 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1778 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1779 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1780 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1781 clearParams.colorMaskBlue && alphaUnmasked);
1782
1783 if (needMaskedColorClear || needMaskedStencilClear)
1784 {
1785 // State which is altered in all paths from this point to the clear call is saved.
1786 // State which is altered in only some paths will be flagged dirty in the case that
1787 // that path is taken.
1788 HRESULT hr;
1789 if (mMaskedClearSavedState == NULL)
1790 {
1791 hr = mDevice->BeginStateBlock();
1792 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1793
1794 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1795 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1796 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1797 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1798 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1799 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1800 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1801 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1802 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1803 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1804 mDevice->SetPixelShader(NULL);
1805 mDevice->SetVertexShader(NULL);
1806 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1807 mDevice->SetStreamSource(0, NULL, 0, 0);
1808 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1809 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1810 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1811 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1812 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1813 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1814 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1815
1816 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1817 {
1818 mDevice->SetStreamSourceFreq(i, 1);
1819 }
1820
1821 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1822 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1823 }
1824
1825 ASSERT(mMaskedClearSavedState != NULL);
1826
1827 if (mMaskedClearSavedState != NULL)
1828 {
1829 hr = mMaskedClearSavedState->Capture();
1830 ASSERT(SUCCEEDED(hr));
1831 }
1832
1833 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1834 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1835 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1836 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1837 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1838 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1839 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1840 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1841
1842 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1843 {
1844 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1845 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1846 clearParams.colorMaskGreen,
1847 clearParams.colorMaskBlue,
1848 clearParams.colorMaskAlpha));
1849 }
1850 else
1851 {
1852 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1853 }
1854
1855 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1856 {
1857 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1858 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1859 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1860 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1861 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1862 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1863 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1864 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1865 }
1866 else
1867 {
1868 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1869 }
1870
1871 mDevice->SetPixelShader(NULL);
1872 mDevice->SetVertexShader(NULL);
1873 mDevice->SetFVF(D3DFVF_XYZRHW);
1874 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1875 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1876 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1877 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1878 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1879 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1880 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1881
1882 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1883 {
1884 mDevice->SetStreamSourceFreq(i, 1);
1885 }
1886
1887 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1888 quad[0][0] = -0.5f;
1889 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1890 quad[0][2] = 0.0f;
1891 quad[0][3] = 1.0f;
1892
1893 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1894 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1895 quad[1][2] = 0.0f;
1896 quad[1][3] = 1.0f;
1897
1898 quad[2][0] = -0.5f;
1899 quad[2][1] = -0.5f;
1900 quad[2][2] = 0.0f;
1901 quad[2][3] = 1.0f;
1902
1903 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1904 quad[3][1] = -0.5f;
1905 quad[3][2] = 0.0f;
1906 quad[3][3] = 1.0f;
1907
1908 startScene();
1909 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1910
1911 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1912 {
1913 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1914 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1915 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1916 }
1917
1918 if (mMaskedClearSavedState != NULL)
1919 {
1920 mMaskedClearSavedState->Apply();
1921 }
1922 }
1923 else if (clearParams.mask)
1924 {
1925 DWORD dxClearFlags = 0;
1926 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1927 {
1928 dxClearFlags |= D3DCLEAR_TARGET;
1929 }
1930 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1931 {
1932 dxClearFlags |= D3DCLEAR_ZBUFFER;
1933 }
1934 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1935 {
1936 dxClearFlags |= D3DCLEAR_STENCIL;
1937 }
1938
1939 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1940 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001941}
1942
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001943void Renderer9::markAllStateDirty()
1944{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001945 mAppliedRenderTargetSerial = 0;
1946 mAppliedDepthbufferSerial = 0;
1947 mAppliedStencilbufferSerial = 0;
1948 mDepthStencilInitialized = false;
1949 mRenderTargetDescInitialized = false;
1950
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001951 mForceSetDepthStencilState = true;
1952 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001953 mForceSetScissor = true;
1954 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001955 mForceSetBlendState = true;
1956
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001957 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001958 {
1959 mForceSetVertexSamplerStates[i] = true;
1960 mCurVertexTextureSerials[i] = 0;
1961 }
1962 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1963 {
1964 mForceSetPixelSamplerStates[i] = true;
1965 mCurPixelTextureSerials[i] = 0;
1966 }
1967
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001968 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001969 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001970 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001971
1972 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001973}
1974
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001975void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001976{
1977 while (!mEventQueryPool.empty())
1978 {
1979 mEventQueryPool.back()->Release();
1980 mEventQueryPool.pop_back();
1981 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001982
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001983 if (mMaskedClearSavedState)
1984 {
1985 mMaskedClearSavedState->Release();
1986 mMaskedClearSavedState = NULL;
1987 }
1988
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001989 mVertexShaderCache.clear();
1990 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001991
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001992 delete mBlit;
1993 mBlit = NULL;
1994
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001995 delete mVertexDataManager;
1996 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001997
1998 delete mIndexDataManager;
1999 mIndexDataManager = NULL;
2000
2001 delete mLineLoopIB;
2002 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002003
2004 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2005 {
2006 delete mNullColorbufferCache[i].buffer;
2007 mNullColorbufferCache[i].buffer = NULL;
2008 }
2009
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002010}
2011
2012
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002013void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002014{
2015 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002016 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002017}
2018
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002019bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002020{
2021 return mDeviceLost;
2022}
2023
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002024// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002025bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002026{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002027 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002028
2029 if (mDeviceEx)
2030 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002031 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002032 }
2033 else if (mDevice)
2034 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002035 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002036 }
2037 else
2038 {
2039 // No device yet, so no reset required
2040 }
2041
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002042 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2043
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002044 if (isLost)
2045 {
2046 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002047 // we'll probably get this done again by notifyDeviceLost
2048 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002049 // Note that we don't want to clear the device loss status here
2050 // -- this needs to be done by resetDevice
2051 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002052 if (notify)
2053 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002054 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002055 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002056 }
2057
2058 return isLost;
2059}
2060
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002061bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002062{
2063 HRESULT status = D3D_OK;
2064
2065 if (mDeviceEx)
2066 {
2067 status = mDeviceEx->CheckDeviceState(NULL);
2068 }
2069 else if (mDevice)
2070 {
2071 status = mDevice->TestCooperativeLevel();
2072 }
2073
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002074 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002075 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002076 switch (status)
2077 {
2078 case D3DERR_DEVICENOTRESET:
2079 case D3DERR_DEVICEHUNG:
2080 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002081 case D3DERR_DEVICELOST:
2082 return (mDeviceEx != NULL);
2083 case D3DERR_DEVICEREMOVED:
2084 UNIMPLEMENTED();
2085 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002086 default:
2087 return false;
2088 }
2089}
2090
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002091bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002092{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002093 releaseDeviceResources();
2094
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002095 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2096
2097 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002098 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002099 int attempts = 3;
2100
2101 while (lost && attempts > 0)
2102 {
2103 if (mDeviceEx)
2104 {
2105 Sleep(500); // Give the graphics driver some CPU time
2106 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2107 }
2108 else
2109 {
2110 result = mDevice->TestCooperativeLevel();
2111 while (result == D3DERR_DEVICELOST)
2112 {
2113 Sleep(100); // Give the graphics driver some CPU time
2114 result = mDevice->TestCooperativeLevel();
2115 }
2116
2117 if (result == D3DERR_DEVICENOTRESET)
2118 {
2119 result = mDevice->Reset(&presentParameters);
2120 }
2121 }
2122
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002123 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002124 attempts --;
2125 }
2126
2127 if (FAILED(result))
2128 {
2129 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2130 return false;
2131 }
2132
2133 // reset device defaults
2134 initializeDevice();
2135 mDeviceLost = false;
2136
2137 return true;
2138}
2139
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002140DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002141{
2142 return mAdapterIdentifier.VendorId;
2143}
2144
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002145std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002146{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002147 std::ostringstream rendererString;
2148
2149 rendererString << mAdapterIdentifier.Description;
2150 if (getShareHandleSupport())
2151 {
2152 rendererString << " Direct3D9Ex";
2153 }
2154 else
2155 {
2156 rendererString << " Direct3D9";
2157 }
2158
2159 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2160 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2161
2162 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002163}
2164
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002165GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002166{
2167 return mAdapterIdentifier.DeviceIdentifier;
2168}
2169
Geoff Lang61e49a52013-05-29 10:22:58 -04002170Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002171{
Geoff Lang61e49a52013-05-29 10:22:58 -04002172 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002173
Geoff Lang61e49a52013-05-29 10:22:58 -04002174 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2175 {
2176 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2177 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2178
2179 if (SUCCEEDED(result))
2180 {
2181 support.supportedSamples[multiSampleIndex] = true;
2182 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2183 {
2184 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2185 }
2186 }
2187 else
2188 {
2189 support.supportedSamples[multiSampleIndex] = false;
2190 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002191 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002192
2193 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002194}
2195
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002196bool Renderer9::getBGRATextureSupport() const
2197{
2198 // DirectX 9 always supports BGRA
2199 return true;
2200}
2201
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002202bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002203{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002204 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002205}
2206
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002207bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002208{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002209 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002210}
2211
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002212bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002213{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002214 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002215}
2216
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002217bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002218{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002219 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002220}
2221
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002222bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002223{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002224 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002225}
2226
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002227bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002228{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002229 return mFloat32FilterSupport;
2230}
2231
2232bool Renderer9::getFloat32TextureRenderingSupport() const
2233{
2234 return mFloat32RenderSupport;
2235}
2236
2237bool Renderer9::getFloat16TextureSupport() const
2238{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002239 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002240}
2241
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002242bool Renderer9::getFloat16TextureFilteringSupport() const
2243{
2244 return mFloat16FilterSupport;
2245}
2246
2247bool Renderer9::getFloat16TextureRenderingSupport() const
2248{
2249 return mFloat16RenderSupport;
2250}
2251
Geoff Langd42cf4e2013-06-05 16:09:17 -04002252bool Renderer9::getRGB565TextureSupport() const
2253{
2254 return mRGB565TextureSupport;
2255}
2256
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002257bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002258{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002259 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002260}
2261
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002262bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002263{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002264 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002265}
2266
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002267bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002268{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002269 return mSupportsTextureFilterAnisotropy;
2270}
2271
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002272float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002273{
2274 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002275 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002276 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002277 }
2278 return 1.0f;
2279}
2280
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002281bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002282{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002283 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002284}
2285
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002286unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002287{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002288 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2289 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002290}
2291
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002292unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2293{
2294 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2295}
2296
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002297unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002298{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002299 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002300}
2301
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002302unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002303{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002304 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002305}
2306
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002307unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002308{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002309 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2310}
2311
2312unsigned int Renderer9::getMaxFragmentUniformVectors() const
2313{
2314 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2315
2316 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2317}
2318
2319unsigned int Renderer9::getMaxVaryingVectors() const
2320{
2321 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002322}
2323
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002324unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2325{
2326 return 0;
2327}
2328
2329unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2330{
2331 return 0;
2332}
2333
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002334unsigned int Renderer9::getReservedVertexUniformBuffers() const
2335{
2336 return 0;
2337}
2338
2339unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2340{
2341 return 0;
2342}
2343
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002344unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2345{
2346 return 0;
2347}
2348
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002349unsigned int Renderer9::getMaxUniformBufferSize() const
2350{
2351 return 0;
2352}
2353
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002354bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002355{
2356 return mSupportsNonPower2Textures;
2357}
2358
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002359bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002360{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002361 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002362}
2363
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002364bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002365{
2366 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2367}
2368
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002369bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002370{
2371 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002372 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002373}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002374
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002375bool Renderer9::getDerivativeInstructionSupport() const
2376{
2377 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2378}
2379
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002380bool Renderer9::getPostSubBufferSupport() const
2381{
2382 return true;
2383}
2384
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002385int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002386{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002387 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002388}
2389
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002390float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002391{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002392 // Point size clamped at 1.0f for SM2
2393 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002394}
2395
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002396int Renderer9::getMaxViewportDimension() const
2397{
2398 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002399 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002400 return maxTextureDimension;
2401}
2402
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002403int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002404{
2405 return (int)mDeviceCaps.MaxTextureWidth;
2406}
2407
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002408int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002409{
2410 return (int)mDeviceCaps.MaxTextureHeight;
2411}
2412
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002413int Renderer9::getMaxTextureDepth() const
2414{
2415 // 3D textures are not available in the D3D9 backend.
2416 return 1;
2417}
2418
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002419int Renderer9::getMaxTextureArrayLayers() const
2420{
2421 // 2D array textures are not available in the D3D9 backend.
2422 return 1;
2423}
2424
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002425bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002426{
2427 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2428}
2429
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002430DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002431{
2432 return mDeviceCaps.DeclTypes;
2433}
2434
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002435int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002436{
2437 return mMinSwapInterval;
2438}
2439
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002440int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002441{
2442 return mMaxSwapInterval;
2443}
2444
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002445int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002446{
2447 return mMaxSupportedSamples;
2448}
2449
Geoff Lang0e120e32013-05-29 10:23:55 -04002450GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2451{
2452 D3DFORMAT format = gl_d3d9::GetTexureFormat(internalFormat, this);
2453 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2454 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2455}
2456
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002457int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002458{
2459 if (requested == 0)
2460 {
2461 return requested;
2462 }
2463
Geoff Lang61e49a52013-05-29 10:22:58 -04002464 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002465 if (itr == mMultiSampleSupport.end())
2466 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002467 if (format == D3DFMT_UNKNOWN)
2468 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002469 return -1;
2470 }
2471
Geoff Lang61e49a52013-05-29 10:22:58 -04002472 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002473 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002474 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002475 {
2476 return i;
2477 }
2478 }
2479
2480 return -1;
2481}
2482
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002483unsigned int Renderer9::getMaxRenderTargets() const
2484{
2485 // we do not support MRT in d3d9
2486 return 1;
2487}
2488
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002489D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2490{
2491 switch (internalformat)
2492 {
2493 case GL_DEPTH_COMPONENT16:
2494 case GL_DEPTH_COMPONENT32_OES:
2495 case GL_DEPTH24_STENCIL8_OES:
2496 return D3DFMT_INTZ;
2497 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2498 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2499 return D3DFMT_DXT1;
2500 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2501 return D3DFMT_DXT3;
2502 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2503 return D3DFMT_DXT5;
2504 case GL_RGBA32F_EXT:
2505 case GL_RGB32F_EXT:
2506 case GL_ALPHA32F_EXT:
2507 case GL_LUMINANCE32F_EXT:
2508 case GL_LUMINANCE_ALPHA32F_EXT:
2509 return D3DFMT_A32B32G32R32F;
2510 case GL_RGBA16F_EXT:
2511 case GL_RGB16F_EXT:
2512 case GL_ALPHA16F_EXT:
2513 case GL_LUMINANCE16F_EXT:
2514 case GL_LUMINANCE_ALPHA16F_EXT:
2515 return D3DFMT_A16B16G16R16F;
2516 case GL_LUMINANCE8_EXT:
2517 if (getLuminanceTextureSupport())
2518 {
2519 return D3DFMT_L8;
2520 }
2521 break;
2522 case GL_LUMINANCE8_ALPHA8_EXT:
2523 if (getLuminanceAlphaTextureSupport())
2524 {
2525 return D3DFMT_A8L8;
2526 }
2527 break;
2528 case GL_RGB8_OES:
2529 case GL_RGB565:
2530 return D3DFMT_X8R8G8B8;
2531 }
2532
2533 return D3DFMT_A8R8G8B8;
2534}
2535
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002536bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002537{
2538 bool result = false;
2539
2540 if (source && dest)
2541 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002542 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2543 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002544
2545 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002546 for (int i = 0; i < levels; ++i)
2547 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002548 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2549 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002550
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002551 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002552
2553 if (srcSurf) srcSurf->Release();
2554 if (dstSurf) dstSurf->Release();
2555
2556 if (!result)
2557 return false;
2558 }
2559 }
2560
2561 return result;
2562}
2563
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002564bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002565{
2566 bool result = false;
2567
2568 if (source && dest)
2569 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002570 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2571 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002572 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002573 for (int f = 0; f < 6; f++)
2574 {
2575 for (int i = 0; i < levels; i++)
2576 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002577 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2578 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002579
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002580 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002581
2582 if (srcSurf) srcSurf->Release();
2583 if (dstSurf) dstSurf->Release();
2584
2585 if (!result)
2586 return false;
2587 }
2588 }
2589 }
2590
2591 return result;
2592}
2593
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002594bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2595{
2596 // 3D textures are not available in the D3D9 backend.
2597 UNREACHABLE();
2598 return false;
2599}
2600
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002601bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2602{
2603 // 2D array textures are not supported by the D3D9 backend.
2604 UNREACHABLE();
2605 return false;
2606}
2607
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002608D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002609{
2610 if (mD3d9Ex != NULL)
2611 {
2612 return D3DPOOL_DEFAULT;
2613 }
2614 else
2615 {
2616 if (!(usage & D3DUSAGE_DYNAMIC))
2617 {
2618 return D3DPOOL_MANAGED;
2619 }
2620 }
2621
2622 return D3DPOOL_DEFAULT;
2623}
2624
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002625bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002626 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002627{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002628 RECT rect;
2629 rect.left = sourceRect.x;
2630 rect.top = sourceRect.y;
2631 rect.right = sourceRect.x + sourceRect.width;
2632 rect.bottom = sourceRect.y + sourceRect.height;
2633
2634 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002635}
2636
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002637bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002638 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002639{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002640 RECT rect;
2641 rect.left = sourceRect.x;
2642 rect.top = sourceRect.y;
2643 rect.right = sourceRect.x + sourceRect.width;
2644 rect.bottom = sourceRect.y + sourceRect.height;
2645
2646 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002647}
2648
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002649bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2650 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2651{
2652 // 3D textures are not available in the D3D9 backend.
2653 UNREACHABLE();
2654 return false;
2655}
2656
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002657bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2658 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2659{
2660 // 2D array textures are not available in the D3D9 backend.
2661 UNREACHABLE();
2662 return false;
2663}
2664
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002665bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang758d5b22013-06-11 11:42:50 -04002666 bool blitRenderTarget, bool blitDepthStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002667{
Geoff Lang758d5b22013-06-11 11:42:50 -04002668 ASSERT(filter == GL_NEAREST);
2669
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002670 endScene();
2671
2672 if (blitRenderTarget)
2673 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002674 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2675 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002676 RenderTarget9 *readRenderTarget = NULL;
2677 RenderTarget9 *drawRenderTarget = NULL;
2678 IDirect3DSurface9* readSurface = NULL;
2679 IDirect3DSurface9* drawSurface = NULL;
2680
2681 if (readBuffer)
2682 {
2683 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2684 }
2685 if (drawBuffer)
2686 {
2687 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2688 }
2689
2690 if (readRenderTarget)
2691 {
2692 readSurface = readRenderTarget->getSurface();
2693 }
2694 if (drawRenderTarget)
2695 {
2696 drawSurface = drawRenderTarget->getSurface();
2697 }
2698
2699 if (!readSurface || !drawSurface)
2700 {
2701 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002702 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002703 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002704
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002705 RECT srcRect;
2706 srcRect.left = readRect.x;
2707 srcRect.right = readRect.x + readRect.width;
2708 srcRect.top = readRect.y;
2709 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002710
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002711 RECT dstRect;
2712 dstRect.left = drawRect.x;
2713 dstRect.right = drawRect.x + drawRect.width;
2714 dstRect.top = drawRect.y;
2715 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002716
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002717 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002718
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002719 readSurface->Release();
2720 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002721
2722 if (FAILED(result))
2723 {
2724 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2725 return false;
2726 }
2727 }
2728
2729 if (blitDepthStencil)
2730 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002731 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2732 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2733 RenderTarget9 *readDepthStencil = NULL;
2734 RenderTarget9 *drawDepthStencil = NULL;
2735 IDirect3DSurface9* readSurface = NULL;
2736 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002737
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002738 if (readBuffer)
2739 {
2740 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2741 }
2742 if (drawBuffer)
2743 {
2744 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2745 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002746
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002747 if (readDepthStencil)
2748 {
2749 readSurface = readDepthStencil->getSurface();
2750 }
2751 if (drawDepthStencil)
2752 {
2753 drawSurface = drawDepthStencil->getSurface();
2754 }
2755
2756 if (!readSurface || !drawSurface)
2757 {
2758 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002759 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002760 }
2761
2762 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2763
2764 readSurface->Release();
2765 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002766
2767 if (FAILED(result))
2768 {
2769 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2770 return false;
2771 }
2772 }
2773
2774 return true;
2775}
2776
2777void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2778 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2779{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002780 RenderTarget9 *renderTarget = NULL;
2781 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002782 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002783
2784 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002785 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002786 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2787 }
2788
2789 if (renderTarget)
2790 {
2791 surface = renderTarget->getSurface();
2792 }
2793
2794 if (!surface)
2795 {
2796 // context must be lost
2797 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002798 }
2799
2800 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002801 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002802
2803 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2804 {
2805 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002806 surface->Release();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002807 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002808 }
2809
2810 HRESULT result;
2811 IDirect3DSurface9 *systemSurface = NULL;
2812 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2813 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2814 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2815 if (directToPixels)
2816 {
2817 // Use the pixels ptr as a shared handle to write directly into client's memory
2818 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2819 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2820 if (FAILED(result))
2821 {
2822 // Try again without the shared handle
2823 directToPixels = false;
2824 }
2825 }
2826
2827 if (!directToPixels)
2828 {
2829 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2830 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2831 if (FAILED(result))
2832 {
2833 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002834 surface->Release();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002835 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002836 }
2837 }
2838
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002839 result = mDevice->GetRenderTargetData(surface, systemSurface);
2840 surface->Release();
2841 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002842
2843 if (FAILED(result))
2844 {
2845 systemSurface->Release();
2846
2847 // It turns out that D3D will sometimes produce more error
2848 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002849 if (d3d9::isDeviceLostError(result))
2850 {
2851 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002852 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002853 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002854 else
2855 {
2856 UNREACHABLE();
2857 return;
2858 }
2859
2860 }
2861
2862 if (directToPixels)
2863 {
2864 systemSurface->Release();
2865 return;
2866 }
2867
2868 RECT rect;
2869 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2870 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2871 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2872 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2873
2874 D3DLOCKED_RECT lock;
2875 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2876
2877 if (FAILED(result))
2878 {
2879 UNREACHABLE();
2880 systemSurface->Release();
2881
2882 return; // No sensible error to generate
2883 }
2884
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002885 unsigned char *source;
2886 int inputPitch;
2887 if (packReverseRowOrder)
2888 {
2889 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2890 inputPitch = -lock.Pitch;
2891 }
2892 else
2893 {
2894 source = (unsigned char*)lock.pBits;
2895 inputPitch = lock.Pitch;
2896 }
2897
Geoff Lang697ad3e2013-06-04 10:11:28 -04002898 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002899
Geoff Lang697ad3e2013-06-04 10:11:28 -04002900 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
2901 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
2902 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002903
Geoff Lang697ad3e2013-06-04 10:11:28 -04002904 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2905
2906 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002907 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002908 // Direct copy possible
2909 unsigned char *dest = static_cast<unsigned char*>(pixels);
2910 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002911 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002912 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002913 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002914 }
2915 else
2916 {
2917 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
2918 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
2919 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2920
2921 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
2922 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002923 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002924 // Fast copy is possible through some special function
2925 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002926 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002927 for (int x = 0; x < rect.right - rect.left; x++)
2928 {
2929 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2930 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
2931
2932 fastCopyFunc(src, dest);
2933 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002934 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002935 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002936 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002937 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002938 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
2939 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002940
Geoff Lang697ad3e2013-06-04 10:11:28 -04002941 gl::ColorF temp;
2942
2943 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002944 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002945 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002946 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002947 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2948 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002949
Geoff Lang697ad3e2013-06-04 10:11:28 -04002950 // readFunc and writeFunc will be using the same type of color, CopyTexImage
2951 // will not allow the copy otherwise.
2952 readFunc(src, &temp);
2953 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002954 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002955 }
2956 }
2957 }
2958
2959 systemSurface->UnlockRect();
2960
2961 systemSurface->Release();
2962}
2963
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002964RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2965{
2966 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2967 IDirect3DSurface9 *surface = NULL;
2968 if (depth)
2969 {
2970 surface = swapChain9->getDepthStencil();
2971 }
2972 else
2973 {
2974 surface = swapChain9->getRenderTarget();
2975 }
2976
2977 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2978
2979 return renderTarget;
2980}
2981
Geoff Langa2d97f12013-06-11 11:44:02 -04002982RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002983{
2984 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2985 return renderTarget;
2986}
2987
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002988ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002989{
2990 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002991
2992 switch (type)
2993 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002994 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00002995 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002996 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002997 if (vshader)
2998 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002999 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003000 }
3001 }
3002 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003003 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003004 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003005 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003006 if (pshader)
3007 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003008 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003009 }
3010 }
3011 break;
3012 default:
3013 UNREACHABLE();
3014 break;
3015 }
3016
3017 return executable;
3018}
3019
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003020ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003021{
3022 const char *profile = NULL;
3023
3024 switch (type)
3025 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003026 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003027 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3028 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003029 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003030 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3031 break;
3032 default:
3033 UNREACHABLE();
3034 return NULL;
3035 }
3036
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003037 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003038 if (!binary)
3039 return NULL;
3040
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003041 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003042 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003043
3044 return executable;
3045}
3046
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003047bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3048{
3049 return mBlit->boxFilter(source, dest);
3050}
3051
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003052D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003053{
3054 if (mD3d9Ex != NULL)
3055 {
3056 return D3DPOOL_DEFAULT;
3057 }
3058 else
3059 {
3060 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3061 {
3062 return D3DPOOL_MANAGED;
3063 }
3064 }
3065
3066 return D3DPOOL_DEFAULT;
3067}
3068
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003069bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3070{
3071 if (source && dest)
3072 {
3073 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003074
3075 if (fromManaged)
3076 {
3077 D3DSURFACE_DESC desc;
3078 source->GetDesc(&desc);
3079
3080 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003081 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003082
3083 if (SUCCEEDED(result))
3084 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003085 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003086 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003087 surf->Release();
3088 }
3089 }
3090 else
3091 {
3092 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003093 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003094 }
3095
3096 if (FAILED(result))
3097 {
3098 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3099 return false;
3100 }
3101 }
3102
3103 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003104}
3105
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003106Image *Renderer9::createImage()
3107{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003108 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003109}
3110
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003111void Renderer9::generateMipmap(Image *dest, Image *src)
3112{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003113 Image9 *src9 = Image9::makeImage9(src);
3114 Image9 *dst9 = Image9::makeImage9(dest);
3115 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003116}
3117
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003118TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3119{
3120 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3121 return new TextureStorage9_2D(this, swapChain9);
3122}
3123
3124TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3125{
3126 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3127}
3128
3129TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3130{
3131 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3132}
3133
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003134TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3135{
3136 // 3D textures are not supported by the D3D9 backend.
3137 UNREACHABLE();
3138
3139 return NULL;
3140}
3141
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003142TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3143{
3144 // 2D array textures are not supported by the D3D9 backend.
3145 UNREACHABLE();
3146
3147 return NULL;
3148}
3149
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003150bool Renderer9::getLUID(LUID *adapterLuid) const
3151{
3152 adapterLuid->HighPart = 0;
3153 adapterLuid->LowPart = 0;
3154
3155 if (mD3d9Ex)
3156 {
3157 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3158 return true;
3159 }
3160
3161 return false;
3162}
3163
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003164}