blob: 927e2e5667f737117e084905202c9af8fb54ffa4 [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
Geoff Langea228632013-07-30 15:17:12 -0400142 SafeRelease(mDevice);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000143 }
144
Geoff Langea228632013-07-30 15:17:12 -0400145 SafeRelease(mDeviceEx);
146 SafeRelease(mD3d9);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000147
148 if (mDeviceWindow)
149 {
150 DestroyWindow(mDeviceWindow);
151 mDeviceWindow = NULL;
152 }
153
Geoff Langea228632013-07-30 15:17:12 -0400154 SafeRelease(mD3d9Ex);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000155
Geoff Langea228632013-07-30 15:17:12 -0400156 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000157}
158
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000159Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
160{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000161 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000162 return static_cast<rx::Renderer9*>(renderer);
163}
164
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000165EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000166{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000167 if (!initializeCompiler())
168 {
169 return EGL_NOT_INITIALIZED;
170 }
171
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000172 if (mSoftwareDevice)
173 {
174 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
175 }
176 else
177 {
178 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
179 }
180
181 if (mD3d9Module == NULL)
182 {
183 ERR("No D3D9 module found - aborting!\n");
184 return EGL_NOT_INITIALIZED;
185 }
186
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000187 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
188 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
189
190 // Use Direct3D9Ex if available. Among other things, this version is less
191 // inclined to report a lost context, for example when the user switches
192 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
193 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
194 {
195 ASSERT(mD3d9Ex);
196 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
197 ASSERT(mD3d9);
198 }
199 else
200 {
201 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
202 }
203
204 if (!mD3d9)
205 {
206 ERR("Could not create D3D9 device - aborting!\n");
207 return EGL_NOT_INITIALIZED;
208 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000209
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000210 if (mDc != NULL)
211 {
212 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
213 }
214
215 HRESULT result;
216
217 // Give up on getting device caps after about one second.
218 for (int i = 0; i < 10; ++i)
219 {
220 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
221 if (SUCCEEDED(result))
222 {
223 break;
224 }
225 else if (result == D3DERR_NOTAVAILABLE)
226 {
227 Sleep(100); // Give the driver some time to initialize/recover
228 }
229 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
230 {
231 ERR("failed to get device caps (0x%x)\n", result);
232 return EGL_NOT_INITIALIZED;
233 }
234 }
235
236 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
237 {
238 ERR("Renderer does not support PS 2.0. aborting!\n");
239 return EGL_NOT_INITIALIZED;
240 }
241
242 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
243 // This is required by Texture2D::convertToRenderTarget.
244 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
245 {
246 ERR("Renderer does not support stretctrect from textures!\n");
247 return EGL_NOT_INITIALIZED;
248 }
249
250 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
251
252 // ATI cards on XP have problems with non-power-of-two textures.
253 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
254 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
255 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
256 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
257
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000258 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
259 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
260
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000261 mMinSwapInterval = 4;
262 mMaxSwapInterval = 0;
263
264 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
265 {
266 mMinSwapInterval = std::min(mMinSwapInterval, 0);
267 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
268 }
269 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
270 {
271 mMinSwapInterval = std::min(mMinSwapInterval, 1);
272 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
273 }
274 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
275 {
276 mMinSwapInterval = std::min(mMinSwapInterval, 2);
277 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
278 }
279 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
280 {
281 mMinSwapInterval = std::min(mMinSwapInterval, 3);
282 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
283 }
284 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
285 {
286 mMinSwapInterval = std::min(mMinSwapInterval, 4);
287 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
288 }
289
Geoff Lang61e49a52013-05-29 10:22:58 -0400290 mMaxSupportedSamples = 0;
291
292 const d3d9::D3DFormatSet &d3d9Formats = d3d9::GetAllUsedD3DFormats();
293 for (d3d9::D3DFormatSet::const_iterator i = d3d9Formats.begin(); i != d3d9Formats.end(); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000294 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400295 MultisampleSupportInfo support = getMultiSampleSupport(*i);
296 mMultiSampleSupport[*i] = support;
297 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
daniel@transgaming.com92955622012-10-31 18:38:41 +0000298 }
299
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000300 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
301 static const TCHAR className[] = TEXT("STATIC");
302
303 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
304
305 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
306 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
307
308 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
309 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
310 {
311 return EGL_BAD_ALLOC;
312 }
313
314 if (FAILED(result))
315 {
316 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
317
318 if (FAILED(result))
319 {
320 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
321 return EGL_BAD_ALLOC;
322 }
323 }
324
325 if (mD3d9Ex)
326 {
327 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
328 ASSERT(SUCCEEDED(result));
329 }
330
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000331 mVertexShaderCache.initialize(mDevice);
332 mPixelShaderCache.initialize(mDevice);
333
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000334 // Check occlusion query support
335 IDirect3DQuery9 *occlusionQuery = NULL;
336 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
337 {
Geoff Langea228632013-07-30 15:17:12 -0400338 SafeRelease(occlusionQuery);
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000339 mOcclusionQuerySupport = true;
340 }
341 else
342 {
343 mOcclusionQuerySupport = false;
344 }
345
346 // Check event query support
347 IDirect3DQuery9 *eventQuery = NULL;
348 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
349 {
Geoff Langea228632013-07-30 15:17:12 -0400350 SafeRelease(eventQuery);
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000351 mEventQuerySupport = true;
352 }
353 else
354 {
355 mEventQuerySupport = false;
356 }
357
358 D3DDISPLAYMODE currentDisplayMode;
359 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
360
361 // Check vertex texture support
362 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
363 // We test this using D3D9 by checking support for the R16F format.
364 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
365 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
366 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
367
Geoff Langd42cf4e2013-06-05 16:09:17 -0400368 // Check RGB565 texture support
369 mRGB565TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
370 D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_R5G6B5));
371
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000372 // Check depth texture support
373 // we use INTZ for depth textures in Direct3D9
374 // we also want NULL texture support to ensure the we can make depth-only FBOs
375 // see http://aras-p.info/texts/D3D9GPUHacks.html
376 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
377 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
378 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
379 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
380
381 // Check 32 bit floating point texture support
382 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
383 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
384 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
385 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
386
387 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
388 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
389 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
390 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
391
392 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
393 {
394 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
395 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
396 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
397 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
398 }
399 else
400 {
401 mFloat32TextureSupport = true;
402 }
403
404 // Check 16 bit floating point texture support
405 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
406 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
407 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
408 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
409
410 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
411 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
412 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
413 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
414
415 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
416 {
417 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
418 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
419 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
420 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
421 }
422 else
423 {
424 mFloat16TextureSupport = true;
425 }
426
427 // Check DXT texture support
428 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
429 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
430 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
431
432 // Check luminance[alpha] texture support
433 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
434 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
435
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000436 initializeDevice();
437
438 return EGL_SUCCESS;
439}
440
441// do any one-time device initialization
442// NOTE: this is also needed after a device lost/reset
443// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000444void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000445{
446 // Permanent non-default states
447 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
448 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
449
450 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
451 {
452 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
453 }
454 else
455 {
456 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
457 }
458
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000459 markAllStateDirty();
460
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000461 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000462
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000463 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400464 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000465 mVertexDataManager = new rx::VertexDataManager(this);
466 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000467}
468
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000469D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000470{
471 D3DPRESENT_PARAMETERS presentParameters = {0};
472
473 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
474 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
475 presentParameters.BackBufferCount = 1;
476 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
477 presentParameters.BackBufferWidth = 1;
478 presentParameters.BackBufferHeight = 1;
479 presentParameters.EnableAutoDepthStencil = FALSE;
480 presentParameters.Flags = 0;
481 presentParameters.hDeviceWindow = mDeviceWindow;
482 presentParameters.MultiSampleQuality = 0;
483 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
484 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
485 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
486 presentParameters.Windowed = TRUE;
487
488 return presentParameters;
489}
490
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000491int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000492{
493 D3DDISPLAYMODE currentDisplayMode;
494 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
495
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000496 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
497 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000498 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
499 int numConfigs = 0;
500
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000501 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000502 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000503 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000504
505 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
506
507 if (SUCCEEDED(result))
508 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000509 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000510 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000511 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000512 HRESULT result = D3D_OK;
513
514 if(depthStencilFormat != D3DFMT_UNKNOWN)
515 {
516 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
517 }
518
519 if (SUCCEEDED(result))
520 {
521 if(depthStencilFormat != D3DFMT_UNKNOWN)
522 {
523 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
524 }
525
526 if (SUCCEEDED(result))
527 {
528 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000529 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
530 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000531 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
532 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000533 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000534
535 (*configDescList)[numConfigs++] = newConfig;
536 }
537 }
538 }
539 }
540 }
541
542 return numConfigs;
543}
544
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000545void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000546{
547 delete [] (configDescList);
548}
549
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000550void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000551{
552 if (!mSceneStarted)
553 {
554 long result = mDevice->BeginScene();
555 if (SUCCEEDED(result)) {
556 // This is defensive checking against the device being
557 // lost at unexpected times.
558 mSceneStarted = true;
559 }
560 }
561}
562
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000563void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000564{
565 if (mSceneStarted)
566 {
567 // EndScene can fail if the device was lost, for example due
568 // to a TDR during a draw call.
569 mDevice->EndScene();
570 mSceneStarted = false;
571 }
572}
573
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000574void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000575{
576 HRESULT result;
577
578 IDirect3DQuery9* query = allocateEventQuery();
579 if (!query)
580 {
581 return;
582 }
583
584 result = query->Issue(D3DISSUE_END);
585 ASSERT(SUCCEEDED(result));
586
587 do
588 {
589 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
590
591 if(block && result == S_FALSE)
592 {
593 // Keep polling, but allow other threads to do something useful first
594 Sleep(0);
595 // explicitly check for device loss
596 // some drivers seem to return S_FALSE even if the device is lost
597 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000598 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000599 {
600 result = D3DERR_DEVICELOST;
601 }
602 }
603 }
604 while(block && result == S_FALSE);
605
606 freeEventQuery(query);
607
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000608 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000609 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000610 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000611 }
612}
613
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000614SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
615{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000616 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000617}
618
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000619IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000620{
621 IDirect3DQuery9 *query = NULL;
622
623 if (mEventQueryPool.empty())
624 {
625 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
626 ASSERT(SUCCEEDED(result));
627 }
628 else
629 {
630 query = mEventQueryPool.back();
631 mEventQueryPool.pop_back();
632 }
633
634 return query;
635}
636
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000637void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000638{
639 if (mEventQueryPool.size() > 1000)
640 {
Geoff Langea228632013-07-30 15:17:12 -0400641 SafeRelease(query);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000642 }
643 else
644 {
645 mEventQueryPool.push_back(query);
646 }
647}
648
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000649IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000650{
651 return mVertexShaderCache.create(function, length);
652}
653
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000654IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000655{
656 return mPixelShaderCache.create(function, length);
657}
658
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000659HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
660{
661 D3DPOOL Pool = getBufferPool(Usage);
662 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
663}
664
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000665VertexBuffer *Renderer9::createVertexBuffer()
666{
667 return new VertexBuffer9(this);
668}
669
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000670HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
671{
672 D3DPOOL Pool = getBufferPool(Usage);
673 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
674}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000675
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000676IndexBuffer *Renderer9::createIndexBuffer()
677{
678 return new IndexBuffer9(this);
679}
680
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000681BufferStorage *Renderer9::createBufferStorage()
682{
683 return new BufferStorage9();
684}
685
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000686QueryImpl *Renderer9::createQuery(GLenum type)
687{
688 return new Query9(this, type);
689}
690
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000691FenceImpl *Renderer9::createFence()
692{
693 return new Fence9(this);
694}
695
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000696void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000697{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000698 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
699 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000700
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000701 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000702 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000703 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
704 int d3dSampler = index + d3dSamplerOffset;
705
706 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
707 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
708
709 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
710 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
711 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
712 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
713 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
714 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
715 if (mSupportsTextureFilterAnisotropy)
716 {
717 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
718 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000719 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000720
721 forceSetSamplers[index] = false;
722 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000723}
724
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000725void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000726{
727 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
728 int d3dSampler = index + d3dSamplerOffset;
729 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000730 unsigned int serial = 0;
731 bool forceSetTexture = false;
732
733 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000734
735 if (texture)
736 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000737 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000738 if (texStorage)
739 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000740 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000741 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000742 }
743 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000744 // in the texture class and we're unexpectedly missing the d3d texture
745 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000746
747 serial = texture->getTextureSerial();
748 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000749 }
750
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000751 if (forceSetTexture || appliedSerials[index] != serial)
752 {
753 mDevice->SetTexture(d3dSampler, d3dTexture);
754 }
755
756 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000757}
758
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000759bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
760{
761 // No effect in ES2/D3D9
762 return true;
763}
764
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000765void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000766{
767 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000768
769 if (rasterStateChanged)
770 {
771 // Set the cull mode
772 if (rasterState.cullFace)
773 {
774 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
775 }
776 else
777 {
778 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
779 }
780
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000781 if (rasterState.polygonOffsetFill)
782 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000783 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000784 {
785 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
786
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000787 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000788 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
789 }
790 }
791 else
792 {
793 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
794 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
795 }
796
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000797 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000798 }
799
800 mForceSetRasterState = false;
801}
802
Geoff Lang2a64ee42013-05-31 11:22:40 -0400803void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor, unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000804{
805 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400806 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000807 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
808
809 if (blendStateChanged || blendColorChanged)
810 {
811 if (blendState.blend)
812 {
813 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
814
815 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
816 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
817 {
818 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
819 }
820 else
821 {
822 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
823 gl::unorm<8>(blendColor.alpha),
824 gl::unorm<8>(blendColor.alpha),
825 gl::unorm<8>(blendColor.alpha)));
826 }
827
828 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
829 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
830 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
831
832 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
833 blendState.destBlendRGB != blendState.destBlendAlpha ||
834 blendState.blendEquationRGB != blendState.blendEquationAlpha)
835 {
836 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
837
838 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
839 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
840 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
841 }
842 else
843 {
844 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
845 }
846 }
847 else
848 {
849 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
850 }
851
852 if (blendState.sampleAlphaToCoverage)
853 {
854 FIXME("Sample alpha to coverage is unimplemented.");
855 }
856
857 // Set the color mask
858 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
859 // Apparently some ATI cards have a bug where a draw with a zero color
860 // write mask can cause later draws to have incorrect results. Instead,
861 // set a nonzero color write mask but modify the blend state so that no
862 // drawing is done.
863 // http://code.google.com/p/angleproject/issues/detail?id=169
864
865 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
866 blendState.colorMaskBlue, blendState.colorMaskAlpha);
867 if (colorMask == 0 && !zeroColorMaskAllowed)
868 {
869 // Enable green channel, but set blending so nothing will be drawn.
870 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
871 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
872
873 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
874 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
875 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
876 }
877 else
878 {
879 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
880 }
881
882 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
883
884 mCurBlendState = blendState;
885 mCurBlendColor = blendColor;
886 }
887
888 if (sampleMaskChanged)
889 {
890 // Set the multisample mask
891 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
892 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
893
894 mCurSampleMask = sampleMask;
895 }
896
897 mForceSetBlendState = false;
898}
899
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000900void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000901 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000902{
903 bool depthStencilStateChanged = mForceSetDepthStencilState ||
904 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000905 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
906 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000907 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000908
909 if (depthStencilStateChanged)
910 {
911 if (depthStencilState.depthTest)
912 {
913 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
914 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
915 }
916 else
917 {
918 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
919 }
920
921 mCurDepthStencilState = depthStencilState;
922 }
923
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000924 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000925 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000926 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000927 {
928 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
929 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
930
931 // FIXME: Unsupported by D3D9
932 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
933 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
934 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
935 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000936 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000937 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
938 {
939 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 +0000940 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000941 }
942
943 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000944 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000945
946 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
947 depthStencilState.stencilWritemask);
948 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
949 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
950
951 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000952 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000953 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
954 depthStencilState.stencilMask);
955
956 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
957 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
958 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
959 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
960 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
961 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
962
963 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
964 depthStencilState.stencilBackWritemask);
965 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
966 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
967
968 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000969 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000970 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
971 depthStencilState.stencilBackMask);
972
973 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
974 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
975 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
976 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
977 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
978 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
979 }
980 else
981 {
982 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
983 }
984
985 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
986
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000987 mCurStencilRef = stencilRef;
988 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000989 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000990 }
991
992 mForceSetDepthStencilState = false;
993}
994
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000995void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000996{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000997 bool scissorChanged = mForceSetScissor ||
998 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
999 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001000
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001001 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001002 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001003 if (enabled)
1004 {
1005 RECT rect;
1006 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1007 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1008 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1009 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1010 mDevice->SetScissorRect(&rect);
1011 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001012
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001013 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1014
1015 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001016 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001017 }
1018
1019 mForceSetScissor = false;
1020}
1021
daniel@transgaming.com12985182012-12-20 20:56:31 +00001022bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001023 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001024{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001025 gl::Rectangle actualViewport = viewport;
1026 float actualZNear = gl::clamp01(zNear);
1027 float actualZFar = gl::clamp01(zFar);
1028 if (ignoreViewport)
1029 {
1030 actualViewport.x = 0;
1031 actualViewport.y = 0;
1032 actualViewport.width = mRenderTargetDesc.width;
1033 actualViewport.height = mRenderTargetDesc.height;
1034 actualZNear = 0.0f;
1035 actualZFar = 1.0f;
1036 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001037
1038 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001039 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1040 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1041 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1042 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1043 dxViewport.MinZ = actualZNear;
1044 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001045
1046 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1047 {
1048 return false; // Nothing to render
1049 }
1050
Geoff Langde14d602013-08-14 12:28:33 -04001051 float depthFront = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
1052
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001053 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
Geoff Langde14d602013-08-14 12:28:33 -04001054 actualZNear != mCurNear || actualZFar != mCurFar || mCurDepthFront != depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001055 if (viewportChanged)
1056 {
1057 mDevice->SetViewport(&dxViewport);
1058
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001059 mCurViewport = actualViewport;
1060 mCurNear = actualZNear;
1061 mCurFar = actualZFar;
Geoff Langde14d602013-08-14 12:28:33 -04001062 mCurDepthFront = depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001063
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001064 dx_VertexConstants vc = {0};
1065 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001066
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001067 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1068 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1069 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1070 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001071
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001072 pc.viewCoords[0] = actualViewport.width * 0.5f;
1073 pc.viewCoords[1] = actualViewport.height * 0.5f;
1074 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1075 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001076
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001077 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1078 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
Geoff Langde14d602013-08-14 12:28:33 -04001079 pc.depthFront[2] = depthFront;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001080
1081 vc.depthRange[0] = actualZNear;
1082 vc.depthRange[1] = actualZFar;
1083 vc.depthRange[2] = actualZFar - actualZNear;
1084
1085 pc.depthRange[0] = actualZNear;
1086 pc.depthRange[1] = actualZFar;
1087 pc.depthRange[2] = actualZFar - actualZNear;
1088
1089 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1090 {
1091 mVertexConstants = vc;
1092 mDxUniformsDirty = true;
1093 }
1094
1095 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1096 {
1097 mPixelConstants = pc;
1098 mDxUniformsDirty = true;
1099 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001100 }
1101
1102 mForceSetViewport = false;
1103 return true;
1104}
1105
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1107{
1108 switch (mode)
1109 {
1110 case GL_POINTS:
1111 mPrimitiveType = D3DPT_POINTLIST;
1112 mPrimitiveCount = count;
1113 break;
1114 case GL_LINES:
1115 mPrimitiveType = D3DPT_LINELIST;
1116 mPrimitiveCount = count / 2;
1117 break;
1118 case GL_LINE_LOOP:
1119 mPrimitiveType = D3DPT_LINESTRIP;
1120 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1121 break;
1122 case GL_LINE_STRIP:
1123 mPrimitiveType = D3DPT_LINESTRIP;
1124 mPrimitiveCount = count - 1;
1125 break;
1126 case GL_TRIANGLES:
1127 mPrimitiveType = D3DPT_TRIANGLELIST;
1128 mPrimitiveCount = count / 3;
1129 break;
1130 case GL_TRIANGLE_STRIP:
1131 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1132 mPrimitiveCount = count - 2;
1133 break;
1134 case GL_TRIANGLE_FAN:
1135 mPrimitiveType = D3DPT_TRIANGLEFAN;
1136 mPrimitiveCount = count - 2;
1137 break;
1138 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001139 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001140 }
1141
1142 return mPrimitiveCount > 0;
1143}
1144
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001145
1146gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1147{
1148 if (!depthbuffer)
1149 {
1150 ERR("Unexpected null depthbuffer for depth-only FBO.");
1151 return NULL;
1152 }
1153
1154 GLsizei width = depthbuffer->getWidth();
1155 GLsizei height = depthbuffer->getHeight();
1156
1157 // search cached nullcolorbuffers
1158 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1159 {
1160 if (mNullColorbufferCache[i].buffer != NULL &&
1161 mNullColorbufferCache[i].width == width &&
1162 mNullColorbufferCache[i].height == height)
1163 {
1164 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1165 return mNullColorbufferCache[i].buffer;
1166 }
1167 }
1168
1169 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1170
1171 // add nullbuffer to the cache
1172 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1173 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1174 {
1175 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1176 {
1177 oldest = &mNullColorbufferCache[i];
1178 }
1179 }
1180
1181 delete oldest->buffer;
1182 oldest->buffer = nullbuffer;
1183 oldest->lruCount = ++mMaxNullColorbufferLRU;
1184 oldest->width = width;
1185 oldest->height = height;
1186
1187 return nullbuffer;
1188}
1189
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001190bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001191{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001192 // if there is no color attachment we must synthesize a NULL colorattachment
1193 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1194 gl::Renderbuffer *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001195 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001196 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001197 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001198 }
1199 else
1200 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001201 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001202 }
1203 if (!renderbufferObject)
1204 {
1205 ERR("unable to locate renderbuffer for FBO.");
1206 return false;
1207 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001208
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001209 bool renderTargetChanged = false;
1210 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1211 if (renderTargetSerial != mAppliedRenderTargetSerial)
1212 {
1213 // Apply the render target on the device
1214 IDirect3DSurface9 *renderTargetSurface = NULL;
1215
1216 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1217 if (renderTarget)
1218 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001219 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001220 }
1221
1222 if (!renderTargetSurface)
1223 {
1224 ERR("render target pointer unexpectedly null.");
1225 return false; // Context must be lost
1226 }
1227
1228 mDevice->SetRenderTarget(0, renderTargetSurface);
Geoff Langea228632013-07-30 15:17:12 -04001229 SafeRelease(renderTargetSurface);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001230
1231 mAppliedRenderTargetSerial = renderTargetSerial;
1232 renderTargetChanged = true;
1233 }
1234
1235 gl::Renderbuffer *depthStencil = NULL;
1236 unsigned int depthbufferSerial = 0;
1237 unsigned int stencilbufferSerial = 0;
1238 if (framebuffer->getDepthbufferType() != GL_NONE)
1239 {
1240 depthStencil = framebuffer->getDepthbuffer();
1241 if (!depthStencil)
1242 {
1243 ERR("Depth stencil pointer unexpectedly null.");
1244 return false;
1245 }
1246
1247 depthbufferSerial = depthStencil->getSerial();
1248 }
1249 else if (framebuffer->getStencilbufferType() != GL_NONE)
1250 {
1251 depthStencil = framebuffer->getStencilbuffer();
1252 if (!depthStencil)
1253 {
1254 ERR("Depth stencil pointer unexpectedly null.");
1255 return false;
1256 }
1257
1258 stencilbufferSerial = depthStencil->getSerial();
1259 }
1260
1261 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1262 stencilbufferSerial != mAppliedStencilbufferSerial ||
1263 !mDepthStencilInitialized)
1264 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001265 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001266 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001267
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001268 // Apply the depth stencil on the device
1269 if (depthStencil)
1270 {
1271 IDirect3DSurface9 *depthStencilSurface = NULL;
1272 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1273
1274 if (depthStencilRenderTarget)
1275 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001276 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001277 }
1278
1279 if (!depthStencilSurface)
1280 {
1281 ERR("depth stencil pointer unexpectedly null.");
1282 return false; // Context must be lost
1283 }
1284
1285 mDevice->SetDepthStencilSurface(depthStencilSurface);
Geoff Langea228632013-07-30 15:17:12 -04001286 SafeRelease(depthStencilSurface);
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001287
1288 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001289 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001290 }
1291 else
1292 {
1293 mDevice->SetDepthStencilSurface(NULL);
1294 }
1295
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001296 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1297 {
1298 mCurDepthSize = depthSize;
1299 mForceSetRasterState = true;
1300 }
1301
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001302 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1303 {
1304 mCurStencilSize = stencilSize;
1305 mForceSetDepthStencilState = true;
1306 }
1307
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001308 mAppliedDepthbufferSerial = depthbufferSerial;
1309 mAppliedStencilbufferSerial = stencilbufferSerial;
1310 mDepthStencilInitialized = true;
1311 }
1312
1313 if (renderTargetChanged || !mRenderTargetDescInitialized)
1314 {
1315 mForceSetScissor = true;
1316 mForceSetViewport = true;
1317
1318 mRenderTargetDesc.width = renderbufferObject->getWidth();
1319 mRenderTargetDesc.height = renderbufferObject->getHeight();
1320 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1321 mRenderTargetDescInitialized = true;
1322 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001323
1324 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001325}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001326
Jamie Madill57a89722013-07-02 11:57:03 -04001327GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001328 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001329{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001330 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001331 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001332 if (err != GL_NO_ERROR)
1333 {
1334 return err;
1335 }
1336
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001337 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1338}
1339
1340// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001341GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001342{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001343 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001344
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001345 if (err == GL_NO_ERROR)
1346 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001347 // Directly binding the storage buffer is not supported for d3d9
1348 ASSERT(indexInfo->storage == NULL);
1349
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001350 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001351 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001352 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1353
1354 mDevice->SetIndices(indexBuffer->getBuffer());
1355 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001356 }
1357 }
1358
1359 return err;
1360}
1361
1362void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1363{
1364 startScene();
1365
1366 if (mode == GL_LINE_LOOP)
1367 {
1368 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1369 }
1370 else if (instances > 0)
1371 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001372 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001373 if (countingIB)
1374 {
1375 if (mAppliedIBSerial != countingIB->getSerial())
1376 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001377 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1378
1379 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001380 mAppliedIBSerial = countingIB->getSerial();
1381 }
1382
1383 for (int i = 0; i < mRepeatDraw; i++)
1384 {
1385 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1386 }
1387 }
1388 else
1389 {
1390 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001391 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001392 }
1393 }
1394 else // Regular case
1395 {
1396 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1397 }
1398}
1399
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001400void 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 +00001401{
1402 startScene();
1403
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001404 if (mode == GL_POINTS)
1405 {
1406 drawIndexedPoints(count, type, indices, elementArrayBuffer);
1407 }
1408 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001409 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001410 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001411 }
1412 else
1413 {
1414 for (int i = 0; i < mRepeatDraw; i++)
1415 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001416 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1417 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001418 }
1419 }
1420}
1421
1422void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1423{
1424 // Get the raw indices for an indexed draw
1425 if (type != GL_NONE && elementArrayBuffer)
1426 {
1427 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001428 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001429 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001430 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001431 }
1432
Geoff Langa36ead42013-08-02 11:54:08 -04001433 unsigned int startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001434
1435 if (get32BitIndexSupport())
1436 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001437 if (!mLineLoopIB)
1438 {
1439 mLineLoopIB = new StreamingIndexBufferInterface(this);
1440 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1441 {
1442 delete mLineLoopIB;
1443 mLineLoopIB = NULL;
1444
1445 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001446 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001447 }
1448 }
1449
Geoff Lang57e713e2013-07-31 17:01:58 -04001450 // Checked by Renderer9::applyPrimitiveType
1451 ASSERT(count >= 0);
1452
1453 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001454 {
1455 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1456 return gl::error(GL_OUT_OF_MEMORY);
1457 }
1458
Geoff Lang57e713e2013-07-31 17:01:58 -04001459 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001460 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001461 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001462 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001463 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001464 }
1465
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001466 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001467 unsigned int offset = 0;
1468 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001469 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001470 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001471 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001472 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001473
Geoff Langa36ead42013-08-02 11:54:08 -04001474 startIndex = static_cast<unsigned int>(offset) / 4;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001475 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1476
1477 switch (type)
1478 {
1479 case GL_NONE: // Non-indexed draw
1480 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001481 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001482 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001483 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001484 data[count] = 0;
1485 break;
1486 case GL_UNSIGNED_BYTE:
1487 for (int i = 0; i < count; i++)
1488 {
1489 data[i] = static_cast<const GLubyte*>(indices)[i];
1490 }
1491 data[count] = static_cast<const GLubyte*>(indices)[0];
1492 break;
1493 case GL_UNSIGNED_SHORT:
1494 for (int i = 0; i < count; i++)
1495 {
1496 data[i] = static_cast<const GLushort*>(indices)[i];
1497 }
1498 data[count] = static_cast<const GLushort*>(indices)[0];
1499 break;
1500 case GL_UNSIGNED_INT:
1501 for (int i = 0; i < count; i++)
1502 {
1503 data[i] = static_cast<const GLuint*>(indices)[i];
1504 }
1505 data[count] = static_cast<const GLuint*>(indices)[0];
1506 break;
1507 default: UNREACHABLE();
1508 }
1509
1510 if (!mLineLoopIB->unmapBuffer())
1511 {
1512 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001513 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001514 }
1515 }
1516 else
1517 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001518 if (!mLineLoopIB)
1519 {
1520 mLineLoopIB = new StreamingIndexBufferInterface(this);
1521 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1522 {
1523 delete mLineLoopIB;
1524 mLineLoopIB = NULL;
1525
1526 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001527 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001528 }
1529 }
1530
Geoff Lang57e713e2013-07-31 17:01:58 -04001531 // Checked by Renderer9::applyPrimitiveType
1532 ASSERT(count >= 0);
1533
1534 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001535 {
1536 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1537 return gl::error(GL_OUT_OF_MEMORY);
1538 }
1539
Geoff Lang57e713e2013-07-31 17:01:58 -04001540 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001541 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001542 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001543 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001544 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001545 }
1546
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001547 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001548 unsigned int offset;
1549 if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001550 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001551 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001552 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001553 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001554
Geoff Langa36ead42013-08-02 11:54:08 -04001555 startIndex = static_cast<unsigned int>(offset) / 2;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001556 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1557
1558 switch (type)
1559 {
1560 case GL_NONE: // Non-indexed draw
1561 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001562 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001563 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001564 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001565 data[count] = 0;
1566 break;
1567 case GL_UNSIGNED_BYTE:
1568 for (int i = 0; i < count; i++)
1569 {
1570 data[i] = static_cast<const GLubyte*>(indices)[i];
1571 }
1572 data[count] = static_cast<const GLubyte*>(indices)[0];
1573 break;
1574 case GL_UNSIGNED_SHORT:
1575 for (int i = 0; i < count; i++)
1576 {
1577 data[i] = static_cast<const GLushort*>(indices)[i];
1578 }
1579 data[count] = static_cast<const GLushort*>(indices)[0];
1580 break;
1581 case GL_UNSIGNED_INT:
1582 for (int i = 0; i < count; i++)
1583 {
1584 data[i] = static_cast<const GLuint*>(indices)[i];
1585 }
1586 data[count] = static_cast<const GLuint*>(indices)[0];
1587 break;
1588 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001589 }
1590
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001591 if (!mLineLoopIB->unmapBuffer())
1592 {
1593 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001594 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001595 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001596 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001597
1598 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001599 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001600 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1601
1602 mDevice->SetIndices(indexBuffer->getBuffer());
1603 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001604 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001605
1606 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001607}
1608
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001609template <typename T>
1610static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1611{
1612 for (int i = 0; i < count; i++)
1613 {
1614 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1615 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1616 }
1617}
1618
1619void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1620{
1621 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1622 // for each individual point. This call is not expected to happen often.
1623
1624 if (elementArrayBuffer)
1625 {
1626 BufferStorage *storage = elementArrayBuffer->getStorage();
1627 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1628 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1629 }
1630
1631 switch (type)
1632 {
1633 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1634 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1635 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1636 default: UNREACHABLE();
1637 }
1638}
1639
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001640void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1641{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001642 unsigned int programBinarySerial = programBinary->getSerial();
1643 if (programBinarySerial != mAppliedProgramBinarySerial)
1644 {
1645 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1646 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001647
daniel@transgaming.come4991412012-12-20 20:55:34 +00001648 IDirect3DVertexShader9 *vertexShader = NULL;
1649 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001650
daniel@transgaming.come4991412012-12-20 20:55:34 +00001651 IDirect3DPixelShader9 *pixelShader = NULL;
1652 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001653
daniel@transgaming.come4991412012-12-20 20:55:34 +00001654 mDevice->SetPixelShader(pixelShader);
1655 mDevice->SetVertexShader(vertexShader);
1656 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001657 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001658
1659 mAppliedProgramBinarySerial = programBinarySerial;
1660 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001661}
1662
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001663void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001664{
1665 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1666 {
1667 gl::Uniform *targetUniform = *ub;
1668
1669 if (targetUniform->dirty)
1670 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001671 GLfloat *f = (GLfloat*)targetUniform->data;
1672 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001673
1674 switch (targetUniform->type)
1675 {
1676 case GL_SAMPLER_2D:
1677 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001678 break;
1679 case GL_BOOL:
1680 case GL_BOOL_VEC2:
1681 case GL_BOOL_VEC3:
1682 case GL_BOOL_VEC4:
1683 applyUniformnbv(targetUniform, i);
1684 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001685 case GL_FLOAT:
1686 case GL_FLOAT_VEC2:
1687 case GL_FLOAT_VEC3:
1688 case GL_FLOAT_VEC4:
1689 case GL_FLOAT_MAT2:
1690 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001691 case GL_FLOAT_MAT4:
1692 applyUniformnfv(targetUniform, f);
1693 break;
1694 case GL_INT:
1695 case GL_INT_VEC2:
1696 case GL_INT_VEC3:
1697 case GL_INT_VEC4:
1698 applyUniformniv(targetUniform, i);
1699 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001700 default:
1701 UNREACHABLE();
1702 }
1703
1704 targetUniform->dirty = false;
1705 }
1706 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001707
1708 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001709 if (mDxUniformsDirty)
1710 {
1711 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1712 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1713 mDxUniformsDirty = false;
1714 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001715}
1716
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001717void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001718{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001719 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001720 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001721 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001722 }
1723
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001724 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001725 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001726 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001727 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001728}
1729
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001730void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001731{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001732 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1733 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001734
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001735 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001737 vector[i][0] = (GLfloat)v[4 * i + 0];
1738 vector[i][1] = (GLfloat)v[4 * i + 1];
1739 vector[i][2] = (GLfloat)v[4 * i + 2];
1740 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001741 }
1742
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001743 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001744}
1745
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001746void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001747{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001748 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1749 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001750
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001751 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001752 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001753 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1754 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1755 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1756 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001757 }
1758
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001759 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001760}
1761
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001762void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001763{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001764 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1765 gl::unorm<8>(clearParams.colorClearValue.red),
1766 gl::unorm<8>(clearParams.colorClearValue.green),
1767 gl::unorm<8>(clearParams.colorClearValue.blue));
1768 float depth = gl::clamp01(clearParams.depthClearValue);
1769 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001770
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001771 unsigned int stencilUnmasked = 0x0;
1772 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1773 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001774 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1775 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001776 stencilUnmasked = (0x1 << stencilSize) - 1;
1777 }
1778
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001779 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1780 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001781
1782 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1783 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1784 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1785 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1786 clearParams.colorMaskBlue && alphaUnmasked);
1787
1788 if (needMaskedColorClear || needMaskedStencilClear)
1789 {
1790 // State which is altered in all paths from this point to the clear call is saved.
1791 // State which is altered in only some paths will be flagged dirty in the case that
1792 // that path is taken.
1793 HRESULT hr;
1794 if (mMaskedClearSavedState == NULL)
1795 {
1796 hr = mDevice->BeginStateBlock();
1797 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1798
1799 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1800 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1801 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1802 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1803 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1804 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1805 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1806 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1807 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1808 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1809 mDevice->SetPixelShader(NULL);
1810 mDevice->SetVertexShader(NULL);
1811 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1812 mDevice->SetStreamSource(0, NULL, 0, 0);
1813 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1814 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1815 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1816 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1817 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1818 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1819 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1820
1821 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1822 {
1823 mDevice->SetStreamSourceFreq(i, 1);
1824 }
1825
1826 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1827 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1828 }
1829
1830 ASSERT(mMaskedClearSavedState != NULL);
1831
1832 if (mMaskedClearSavedState != NULL)
1833 {
1834 hr = mMaskedClearSavedState->Capture();
1835 ASSERT(SUCCEEDED(hr));
1836 }
1837
1838 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1839 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1840 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1841 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1842 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1843 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1844 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1845 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1846
1847 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1848 {
1849 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1850 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1851 clearParams.colorMaskGreen,
1852 clearParams.colorMaskBlue,
1853 clearParams.colorMaskAlpha));
1854 }
1855 else
1856 {
1857 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1858 }
1859
1860 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1861 {
1862 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1863 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1864 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1865 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1866 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1867 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1868 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1869 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1870 }
1871 else
1872 {
1873 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1874 }
1875
1876 mDevice->SetPixelShader(NULL);
1877 mDevice->SetVertexShader(NULL);
1878 mDevice->SetFVF(D3DFVF_XYZRHW);
1879 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1880 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1881 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1882 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1883 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1884 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1885 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1886
1887 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1888 {
1889 mDevice->SetStreamSourceFreq(i, 1);
1890 }
1891
1892 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1893 quad[0][0] = -0.5f;
1894 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1895 quad[0][2] = 0.0f;
1896 quad[0][3] = 1.0f;
1897
1898 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1899 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1900 quad[1][2] = 0.0f;
1901 quad[1][3] = 1.0f;
1902
1903 quad[2][0] = -0.5f;
1904 quad[2][1] = -0.5f;
1905 quad[2][2] = 0.0f;
1906 quad[2][3] = 1.0f;
1907
1908 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1909 quad[3][1] = -0.5f;
1910 quad[3][2] = 0.0f;
1911 quad[3][3] = 1.0f;
1912
1913 startScene();
1914 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1915
1916 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1917 {
1918 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1919 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1920 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1921 }
1922
1923 if (mMaskedClearSavedState != NULL)
1924 {
1925 mMaskedClearSavedState->Apply();
1926 }
1927 }
1928 else if (clearParams.mask)
1929 {
1930 DWORD dxClearFlags = 0;
1931 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1932 {
1933 dxClearFlags |= D3DCLEAR_TARGET;
1934 }
1935 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1936 {
1937 dxClearFlags |= D3DCLEAR_ZBUFFER;
1938 }
1939 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1940 {
1941 dxClearFlags |= D3DCLEAR_STENCIL;
1942 }
1943
1944 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1945 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001946}
1947
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001948void Renderer9::markAllStateDirty()
1949{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001950 mAppliedRenderTargetSerial = 0;
1951 mAppliedDepthbufferSerial = 0;
1952 mAppliedStencilbufferSerial = 0;
1953 mDepthStencilInitialized = false;
1954 mRenderTargetDescInitialized = false;
1955
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001956 mForceSetDepthStencilState = true;
1957 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001958 mForceSetScissor = true;
1959 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001960 mForceSetBlendState = true;
1961
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001962 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001963 {
1964 mForceSetVertexSamplerStates[i] = true;
1965 mCurVertexTextureSerials[i] = 0;
1966 }
1967 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1968 {
1969 mForceSetPixelSamplerStates[i] = true;
1970 mCurPixelTextureSerials[i] = 0;
1971 }
1972
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001973 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001974 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001975 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001976
1977 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001978}
1979
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001980void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001981{
Geoff Langea228632013-07-30 15:17:12 -04001982 for (size_t i = 0; i < mEventQueryPool.size(); i++)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001983 {
Geoff Langea228632013-07-30 15:17:12 -04001984 SafeRelease(mEventQueryPool[i]);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001985 }
Geoff Langea228632013-07-30 15:17:12 -04001986 mEventQueryPool.clear();
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001987
Geoff Langea228632013-07-30 15:17:12 -04001988 SafeRelease(mMaskedClearSavedState);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001989
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001990 mVertexShaderCache.clear();
1991 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001992
Geoff Langea228632013-07-30 15:17:12 -04001993 SafeDelete(mBlit);
1994 SafeDelete(mVertexDataManager);
1995 SafeDelete(mIndexDataManager);
1996 SafeDelete(mLineLoopIB);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001997
1998 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1999 {
Geoff Langea228632013-07-30 15:17:12 -04002000 SafeDelete(mNullColorbufferCache[i].buffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002001 }
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002002}
2003
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002004void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002005{
2006 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002007 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002008}
2009
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002010bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002011{
2012 return mDeviceLost;
2013}
2014
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002015// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002016bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002017{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002018 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002019
2020 if (mDeviceEx)
2021 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002022 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002023 }
2024 else if (mDevice)
2025 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002026 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002027 }
2028 else
2029 {
2030 // No device yet, so no reset required
2031 }
2032
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002033 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2034
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002035 if (isLost)
2036 {
2037 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002038 // we'll probably get this done again by notifyDeviceLost
2039 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002040 // Note that we don't want to clear the device loss status here
2041 // -- this needs to be done by resetDevice
2042 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002043 if (notify)
2044 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002045 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002046 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002047 }
2048
2049 return isLost;
2050}
2051
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002052bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002053{
2054 HRESULT status = D3D_OK;
2055
2056 if (mDeviceEx)
2057 {
2058 status = mDeviceEx->CheckDeviceState(NULL);
2059 }
2060 else if (mDevice)
2061 {
2062 status = mDevice->TestCooperativeLevel();
2063 }
2064
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002065 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002066 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002067 switch (status)
2068 {
2069 case D3DERR_DEVICENOTRESET:
2070 case D3DERR_DEVICEHUNG:
2071 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002072 case D3DERR_DEVICELOST:
2073 return (mDeviceEx != NULL);
2074 case D3DERR_DEVICEREMOVED:
2075 UNIMPLEMENTED();
2076 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002077 default:
2078 return false;
2079 }
2080}
2081
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002082bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002083{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002084 releaseDeviceResources();
2085
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002086 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2087
2088 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002089 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002090 int attempts = 3;
2091
2092 while (lost && attempts > 0)
2093 {
2094 if (mDeviceEx)
2095 {
2096 Sleep(500); // Give the graphics driver some CPU time
2097 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2098 }
2099 else
2100 {
2101 result = mDevice->TestCooperativeLevel();
2102 while (result == D3DERR_DEVICELOST)
2103 {
2104 Sleep(100); // Give the graphics driver some CPU time
2105 result = mDevice->TestCooperativeLevel();
2106 }
2107
2108 if (result == D3DERR_DEVICENOTRESET)
2109 {
2110 result = mDevice->Reset(&presentParameters);
2111 }
2112 }
2113
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002114 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002115 attempts --;
2116 }
2117
2118 if (FAILED(result))
2119 {
2120 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2121 return false;
2122 }
2123
2124 // reset device defaults
2125 initializeDevice();
2126 mDeviceLost = false;
2127
2128 return true;
2129}
2130
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002131DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002132{
2133 return mAdapterIdentifier.VendorId;
2134}
2135
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002136std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002137{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002138 std::ostringstream rendererString;
2139
2140 rendererString << mAdapterIdentifier.Description;
2141 if (getShareHandleSupport())
2142 {
2143 rendererString << " Direct3D9Ex";
2144 }
2145 else
2146 {
2147 rendererString << " Direct3D9";
2148 }
2149
2150 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2151 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2152
2153 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002154}
2155
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002156GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002157{
2158 return mAdapterIdentifier.DeviceIdentifier;
2159}
2160
Geoff Lang61e49a52013-05-29 10:22:58 -04002161Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002162{
Geoff Lang61e49a52013-05-29 10:22:58 -04002163 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002164
Geoff Lang61e49a52013-05-29 10:22:58 -04002165 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2166 {
2167 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2168 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2169
2170 if (SUCCEEDED(result))
2171 {
2172 support.supportedSamples[multiSampleIndex] = true;
2173 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2174 {
2175 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2176 }
2177 }
2178 else
2179 {
2180 support.supportedSamples[multiSampleIndex] = false;
2181 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002182 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002183
2184 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002185}
2186
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002187bool Renderer9::getBGRATextureSupport() const
2188{
2189 // DirectX 9 always supports BGRA
2190 return true;
2191}
2192
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002193bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002194{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002195 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002196}
2197
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002198bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002199{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002200 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002201}
2202
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002203bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002204{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002205 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002206}
2207
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002208bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002209{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002210 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002211}
2212
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002213bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002214{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002215 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002216}
2217
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002218bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002219{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002220 return mFloat32FilterSupport;
2221}
2222
2223bool Renderer9::getFloat32TextureRenderingSupport() const
2224{
2225 return mFloat32RenderSupport;
2226}
2227
2228bool Renderer9::getFloat16TextureSupport() const
2229{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002230 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002231}
2232
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002233bool Renderer9::getFloat16TextureFilteringSupport() const
2234{
2235 return mFloat16FilterSupport;
2236}
2237
2238bool Renderer9::getFloat16TextureRenderingSupport() const
2239{
2240 return mFloat16RenderSupport;
2241}
2242
Geoff Langd42cf4e2013-06-05 16:09:17 -04002243bool Renderer9::getRGB565TextureSupport() const
2244{
2245 return mRGB565TextureSupport;
2246}
2247
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002248bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002249{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002250 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002251}
2252
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002253bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002254{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002255 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002256}
2257
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002258bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002259{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002260 return mSupportsTextureFilterAnisotropy;
2261}
2262
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002263float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002264{
2265 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002266 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002267 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002268 }
2269 return 1.0f;
2270}
2271
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002272bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002273{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002274 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002275}
2276
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002277unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002278{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002279 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2280 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002281}
2282
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002283unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2284{
2285 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2286}
2287
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002288unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002289{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002290 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002291}
2292
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002293unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002294{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002295 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002296}
2297
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002298unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002299{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002300 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2301}
2302
2303unsigned int Renderer9::getMaxFragmentUniformVectors() const
2304{
2305 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2306
2307 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2308}
2309
2310unsigned int Renderer9::getMaxVaryingVectors() const
2311{
2312 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002313}
2314
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002315unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2316{
2317 return 0;
2318}
2319
2320unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2321{
2322 return 0;
2323}
2324
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002325unsigned int Renderer9::getReservedVertexUniformBuffers() const
2326{
2327 return 0;
2328}
2329
2330unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2331{
2332 return 0;
2333}
2334
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002335unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2336{
2337 return 0;
2338}
2339
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002340unsigned int Renderer9::getMaxUniformBufferSize() const
2341{
2342 return 0;
2343}
2344
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002345bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002346{
2347 return mSupportsNonPower2Textures;
2348}
2349
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002350bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002351{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002352 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002353}
2354
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002355bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002356{
2357 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2358}
2359
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002360bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002361{
2362 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002363 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002364}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002365
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002366bool Renderer9::getDerivativeInstructionSupport() const
2367{
2368 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2369}
2370
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002371bool Renderer9::getPostSubBufferSupport() const
2372{
2373 return true;
2374}
2375
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002376int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002377{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002378 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002379}
2380
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002381float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002382{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002383 // Point size clamped at 1.0f for SM2
2384 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002385}
2386
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002387int Renderer9::getMaxViewportDimension() const
2388{
2389 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002390 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002391 return maxTextureDimension;
2392}
2393
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002394int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002395{
2396 return (int)mDeviceCaps.MaxTextureWidth;
2397}
2398
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002399int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002400{
2401 return (int)mDeviceCaps.MaxTextureHeight;
2402}
2403
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002404int Renderer9::getMaxTextureDepth() const
2405{
2406 // 3D textures are not available in the D3D9 backend.
2407 return 1;
2408}
2409
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002410int Renderer9::getMaxTextureArrayLayers() const
2411{
2412 // 2D array textures are not available in the D3D9 backend.
2413 return 1;
2414}
2415
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002416bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002417{
2418 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2419}
2420
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002421DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002422{
2423 return mDeviceCaps.DeclTypes;
2424}
2425
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002426int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002427{
2428 return mMinSwapInterval;
2429}
2430
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002431int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002432{
2433 return mMaxSwapInterval;
2434}
2435
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002436int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002437{
2438 return mMaxSupportedSamples;
2439}
2440
Geoff Lang0e120e32013-05-29 10:23:55 -04002441GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2442{
Shannon Woodsddb785c2013-07-08 10:32:13 -04002443 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
Geoff Lang0e120e32013-05-29 10:23:55 -04002444 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2445 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2446}
2447
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002448GLsizei Renderer9::getNumSampleCounts(GLint internalFormat) const
2449{
2450 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2451 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2452
2453 unsigned int numCounts = 0;
2454 if (iter != mMultiSampleSupport.end())
2455 {
2456 const MultisampleSupportInfo& info = iter->second;
2457 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2458 {
2459 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2460 {
2461 numCounts++;
2462 }
2463 }
2464 }
2465
2466 return numCounts;
2467}
2468
2469void Renderer9::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2470{
2471 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2472 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2473
2474 if (iter != mMultiSampleSupport.end())
2475 {
2476 const MultisampleSupportInfo& info = iter->second;
2477 int bufPos = 0;
2478 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2479 {
2480 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2481 {
2482 params[bufPos++] = i;
2483 }
2484 }
2485 }
2486}
2487
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002488int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002489{
2490 if (requested == 0)
2491 {
2492 return requested;
2493 }
2494
Geoff Lang61e49a52013-05-29 10:22:58 -04002495 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002496 if (itr == mMultiSampleSupport.end())
2497 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002498 if (format == D3DFMT_UNKNOWN)
2499 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002500 return -1;
2501 }
2502
Geoff Lang61e49a52013-05-29 10:22:58 -04002503 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002504 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002505 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002506 {
2507 return i;
2508 }
2509 }
2510
2511 return -1;
2512}
2513
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002514unsigned int Renderer9::getMaxRenderTargets() const
2515{
2516 // we do not support MRT in d3d9
2517 return 1;
2518}
2519
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002520D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2521{
2522 switch (internalformat)
2523 {
2524 case GL_DEPTH_COMPONENT16:
2525 case GL_DEPTH_COMPONENT32_OES:
2526 case GL_DEPTH24_STENCIL8_OES:
2527 return D3DFMT_INTZ;
2528 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2529 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2530 return D3DFMT_DXT1;
2531 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2532 return D3DFMT_DXT3;
2533 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2534 return D3DFMT_DXT5;
2535 case GL_RGBA32F_EXT:
2536 case GL_RGB32F_EXT:
2537 case GL_ALPHA32F_EXT:
2538 case GL_LUMINANCE32F_EXT:
2539 case GL_LUMINANCE_ALPHA32F_EXT:
2540 return D3DFMT_A32B32G32R32F;
2541 case GL_RGBA16F_EXT:
2542 case GL_RGB16F_EXT:
2543 case GL_ALPHA16F_EXT:
2544 case GL_LUMINANCE16F_EXT:
2545 case GL_LUMINANCE_ALPHA16F_EXT:
2546 return D3DFMT_A16B16G16R16F;
2547 case GL_LUMINANCE8_EXT:
2548 if (getLuminanceTextureSupport())
2549 {
2550 return D3DFMT_L8;
2551 }
2552 break;
2553 case GL_LUMINANCE8_ALPHA8_EXT:
2554 if (getLuminanceAlphaTextureSupport())
2555 {
2556 return D3DFMT_A8L8;
2557 }
2558 break;
2559 case GL_RGB8_OES:
2560 case GL_RGB565:
2561 return D3DFMT_X8R8G8B8;
2562 }
2563
2564 return D3DFMT_A8R8G8B8;
2565}
2566
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002567bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002568{
2569 bool result = false;
2570
2571 if (source && dest)
2572 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002573 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2574 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002575
2576 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002577 for (int i = 0; i < levels; ++i)
2578 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002579 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2580 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002581
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002582 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002583
Geoff Langea228632013-07-30 15:17:12 -04002584 SafeRelease(srcSurf);
2585 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002586
2587 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002588 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002589 return false;
Geoff Langea228632013-07-30 15:17:12 -04002590 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002591 }
2592 }
2593
2594 return result;
2595}
2596
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002597bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002598{
2599 bool result = false;
2600
2601 if (source && dest)
2602 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002603 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2604 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002605 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002606 for (int f = 0; f < 6; f++)
2607 {
2608 for (int i = 0; i < levels; i++)
2609 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002610 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2611 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002612
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002613 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002614
Geoff Langea228632013-07-30 15:17:12 -04002615 SafeRelease(srcSurf);
2616 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002617
2618 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002619 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002620 return false;
Geoff Langea228632013-07-30 15:17:12 -04002621 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002622 }
2623 }
2624 }
2625
2626 return result;
2627}
2628
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002629bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2630{
2631 // 3D textures are not available in the D3D9 backend.
2632 UNREACHABLE();
2633 return false;
2634}
2635
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002636bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2637{
2638 // 2D array textures are not supported by the D3D9 backend.
2639 UNREACHABLE();
2640 return false;
2641}
2642
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002643D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002644{
2645 if (mD3d9Ex != NULL)
2646 {
2647 return D3DPOOL_DEFAULT;
2648 }
2649 else
2650 {
2651 if (!(usage & D3DUSAGE_DYNAMIC))
2652 {
2653 return D3DPOOL_MANAGED;
2654 }
2655 }
2656
2657 return D3DPOOL_DEFAULT;
2658}
2659
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002660bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002661 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002662{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002663 RECT rect;
2664 rect.left = sourceRect.x;
2665 rect.top = sourceRect.y;
2666 rect.right = sourceRect.x + sourceRect.width;
2667 rect.bottom = sourceRect.y + sourceRect.height;
2668
2669 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002670}
2671
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002672bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002673 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002674{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002675 RECT rect;
2676 rect.left = sourceRect.x;
2677 rect.top = sourceRect.y;
2678 rect.right = sourceRect.x + sourceRect.width;
2679 rect.bottom = sourceRect.y + sourceRect.height;
2680
2681 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002682}
2683
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002684bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2685 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2686{
2687 // 3D textures are not available in the D3D9 backend.
2688 UNREACHABLE();
2689 return false;
2690}
2691
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002692bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2693 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2694{
2695 // 2D array textures are not available in the D3D9 backend.
2696 UNREACHABLE();
2697 return false;
2698}
2699
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002700bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04002701 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002702{
Geoff Lang758d5b22013-06-11 11:42:50 -04002703 ASSERT(filter == GL_NEAREST);
2704
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002705 endScene();
2706
2707 if (blitRenderTarget)
2708 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002709 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2710 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002711 RenderTarget9 *readRenderTarget = NULL;
2712 RenderTarget9 *drawRenderTarget = NULL;
2713 IDirect3DSurface9* readSurface = NULL;
2714 IDirect3DSurface9* drawSurface = NULL;
2715
2716 if (readBuffer)
2717 {
2718 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2719 }
2720 if (drawBuffer)
2721 {
2722 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2723 }
2724
2725 if (readRenderTarget)
2726 {
2727 readSurface = readRenderTarget->getSurface();
2728 }
2729 if (drawRenderTarget)
2730 {
2731 drawSurface = drawRenderTarget->getSurface();
2732 }
2733
2734 if (!readSurface || !drawSurface)
2735 {
2736 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002737 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002738 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002739
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002740 RECT srcRect;
2741 srcRect.left = readRect.x;
2742 srcRect.right = readRect.x + readRect.width;
2743 srcRect.top = readRect.y;
2744 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002745
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002746 RECT dstRect;
2747 dstRect.left = drawRect.x;
2748 dstRect.right = drawRect.x + drawRect.width;
2749 dstRect.top = drawRect.y;
2750 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002751
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002752 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002753
Geoff Langea228632013-07-30 15:17:12 -04002754 SafeRelease(readSurface);
2755 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002756
2757 if (FAILED(result))
2758 {
2759 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2760 return false;
2761 }
2762 }
2763
Geoff Lang685806d2013-06-12 11:16:36 -04002764 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002765 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002766 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2767 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2768 RenderTarget9 *readDepthStencil = NULL;
2769 RenderTarget9 *drawDepthStencil = NULL;
2770 IDirect3DSurface9* readSurface = NULL;
2771 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002772
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002773 if (readBuffer)
2774 {
2775 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2776 }
2777 if (drawBuffer)
2778 {
2779 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2780 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002781
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002782 if (readDepthStencil)
2783 {
2784 readSurface = readDepthStencil->getSurface();
2785 }
2786 if (drawDepthStencil)
2787 {
2788 drawSurface = drawDepthStencil->getSurface();
2789 }
2790
2791 if (!readSurface || !drawSurface)
2792 {
2793 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002794 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002795 }
2796
2797 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2798
Geoff Langea228632013-07-30 15:17:12 -04002799 SafeRelease(readSurface);
2800 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002801
2802 if (FAILED(result))
2803 {
2804 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2805 return false;
2806 }
2807 }
2808
2809 return true;
2810}
2811
2812void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2813 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2814{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002815 RenderTarget9 *renderTarget = NULL;
2816 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002817 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002818
2819 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002820 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002821 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2822 }
2823
2824 if (renderTarget)
2825 {
2826 surface = renderTarget->getSurface();
2827 }
2828
2829 if (!surface)
2830 {
2831 // context must be lost
2832 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002833 }
2834
2835 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002836 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002837
2838 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2839 {
2840 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
Geoff Langea228632013-07-30 15:17:12 -04002841 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002842 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002843 }
2844
2845 HRESULT result;
2846 IDirect3DSurface9 *systemSurface = NULL;
2847 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2848 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2849 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2850 if (directToPixels)
2851 {
2852 // Use the pixels ptr as a shared handle to write directly into client's memory
2853 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2854 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2855 if (FAILED(result))
2856 {
2857 // Try again without the shared handle
2858 directToPixels = false;
2859 }
2860 }
2861
2862 if (!directToPixels)
2863 {
2864 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2865 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2866 if (FAILED(result))
2867 {
2868 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
Geoff Langea228632013-07-30 15:17:12 -04002869 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002870 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002871 }
2872 }
2873
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002874 result = mDevice->GetRenderTargetData(surface, systemSurface);
Geoff Langea228632013-07-30 15:17:12 -04002875 SafeRelease(surface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002876
2877 if (FAILED(result))
2878 {
Geoff Langea228632013-07-30 15:17:12 -04002879 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002880
2881 // It turns out that D3D will sometimes produce more error
2882 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002883 if (d3d9::isDeviceLostError(result))
2884 {
2885 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002886 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002887 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002888 else
2889 {
2890 UNREACHABLE();
2891 return;
2892 }
2893
2894 }
2895
2896 if (directToPixels)
2897 {
Geoff Langea228632013-07-30 15:17:12 -04002898 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002899 return;
2900 }
2901
2902 RECT rect;
2903 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2904 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2905 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2906 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2907
2908 D3DLOCKED_RECT lock;
2909 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2910
2911 if (FAILED(result))
2912 {
2913 UNREACHABLE();
Geoff Langea228632013-07-30 15:17:12 -04002914 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002915
2916 return; // No sensible error to generate
2917 }
2918
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002919 unsigned char *source;
2920 int inputPitch;
2921 if (packReverseRowOrder)
2922 {
2923 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2924 inputPitch = -lock.Pitch;
2925 }
2926 else
2927 {
2928 source = (unsigned char*)lock.pBits;
2929 inputPitch = lock.Pitch;
2930 }
2931
Geoff Lang697ad3e2013-06-04 10:11:28 -04002932 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002933
Geoff Lang697ad3e2013-06-04 10:11:28 -04002934 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
2935 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
2936 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002937
Geoff Lang697ad3e2013-06-04 10:11:28 -04002938 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2939
2940 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002941 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002942 // Direct copy possible
2943 unsigned char *dest = static_cast<unsigned char*>(pixels);
2944 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002945 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002946 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002947 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002948 }
2949 else
2950 {
2951 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
2952 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
2953 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2954
2955 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
2956 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002957 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002958 // Fast copy is possible through some special function
2959 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002960 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002961 for (int x = 0; x < rect.right - rect.left; x++)
2962 {
2963 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2964 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
2965
2966 fastCopyFunc(src, dest);
2967 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002968 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002969 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002970 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002971 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002972 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
2973 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002974
Geoff Lang697ad3e2013-06-04 10:11:28 -04002975 gl::ColorF temp;
2976
2977 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002978 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002979 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002980 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002981 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2982 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002983
Geoff Lang697ad3e2013-06-04 10:11:28 -04002984 // readFunc and writeFunc will be using the same type of color, CopyTexImage
2985 // will not allow the copy otherwise.
2986 readFunc(src, &temp);
2987 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002988 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002989 }
2990 }
2991 }
2992
2993 systemSurface->UnlockRect();
Geoff Langea228632013-07-30 15:17:12 -04002994 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002995}
2996
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002997RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2998{
2999 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3000 IDirect3DSurface9 *surface = NULL;
3001 if (depth)
3002 {
3003 surface = swapChain9->getDepthStencil();
3004 }
3005 else
3006 {
3007 surface = swapChain9->getRenderTarget();
3008 }
3009
3010 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3011
3012 return renderTarget;
3013}
3014
Geoff Langa2d97f12013-06-11 11:44:02 -04003015RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003016{
3017 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3018 return renderTarget;
3019}
3020
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003021ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003022{
3023 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003024
3025 switch (type)
3026 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003027 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003028 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003029 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003030 if (vshader)
3031 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003032 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003033 }
3034 }
3035 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003036 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003037 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003038 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003039 if (pshader)
3040 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003041 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003042 }
3043 }
3044 break;
3045 default:
3046 UNREACHABLE();
3047 break;
3048 }
3049
3050 return executable;
3051}
3052
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003053ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003054{
3055 const char *profile = NULL;
3056
3057 switch (type)
3058 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003059 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003060 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3061 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003062 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003063 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3064 break;
3065 default:
3066 UNREACHABLE();
3067 return NULL;
3068 }
3069
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003070 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003071 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04003072 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003073 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04003074 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003075
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003076 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04003077 SafeRelease(binary);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003078
3079 return executable;
3080}
3081
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003082bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3083{
3084 return mBlit->boxFilter(source, dest);
3085}
3086
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003087D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003088{
3089 if (mD3d9Ex != NULL)
3090 {
3091 return D3DPOOL_DEFAULT;
3092 }
3093 else
3094 {
3095 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3096 {
3097 return D3DPOOL_MANAGED;
3098 }
3099 }
3100
3101 return D3DPOOL_DEFAULT;
3102}
3103
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003104bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3105{
3106 if (source && dest)
3107 {
3108 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003109
3110 if (fromManaged)
3111 {
3112 D3DSURFACE_DESC desc;
3113 source->GetDesc(&desc);
3114
3115 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003116 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003117
3118 if (SUCCEEDED(result))
3119 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003120 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003121 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
Geoff Langea228632013-07-30 15:17:12 -04003122 SafeRelease(surf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003123 }
3124 }
3125 else
3126 {
3127 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003128 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003129 }
3130
3131 if (FAILED(result))
3132 {
3133 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3134 return false;
3135 }
3136 }
3137
3138 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003139}
3140
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003141Image *Renderer9::createImage()
3142{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003143 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003144}
3145
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003146void Renderer9::generateMipmap(Image *dest, Image *src)
3147{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003148 Image9 *src9 = Image9::makeImage9(src);
3149 Image9 *dst9 = Image9::makeImage9(dest);
3150 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003151}
3152
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003153TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3154{
3155 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3156 return new TextureStorage9_2D(this, swapChain9);
3157}
3158
3159TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3160{
3161 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3162}
3163
3164TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3165{
3166 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3167}
3168
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003169TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3170{
3171 // 3D textures are not supported by the D3D9 backend.
3172 UNREACHABLE();
3173
3174 return NULL;
3175}
3176
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003177TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3178{
3179 // 2D array textures are not supported by the D3D9 backend.
3180 UNREACHABLE();
3181
3182 return NULL;
3183}
3184
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003185bool Renderer9::getLUID(LUID *adapterLuid) const
3186{
3187 adapterLuid->HighPart = 0;
3188 adapterLuid->LowPart = 0;
3189
3190 if (mD3d9Ex)
3191 {
3192 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3193 return true;
3194 }
3195
3196 return false;
3197}
3198
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003199}