blob: 237986f443013cf5996c53b3f18a7c1d4fce5aea [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
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001051 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1052 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001053 if (viewportChanged)
1054 {
1055 mDevice->SetViewport(&dxViewport);
1056
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001057 mCurViewport = actualViewport;
1058 mCurNear = actualZNear;
1059 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001060
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001061 dx_VertexConstants vc = {0};
1062 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001063
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001064 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1065 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1066 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1067 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001068
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001069 pc.viewCoords[0] = actualViewport.width * 0.5f;
1070 pc.viewCoords[1] = actualViewport.height * 0.5f;
1071 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1072 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001073
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001074 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1075 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1076 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1077
1078 vc.depthRange[0] = actualZNear;
1079 vc.depthRange[1] = actualZFar;
1080 vc.depthRange[2] = actualZFar - actualZNear;
1081
1082 pc.depthRange[0] = actualZNear;
1083 pc.depthRange[1] = actualZFar;
1084 pc.depthRange[2] = actualZFar - actualZNear;
1085
1086 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1087 {
1088 mVertexConstants = vc;
1089 mDxUniformsDirty = true;
1090 }
1091
1092 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1093 {
1094 mPixelConstants = pc;
1095 mDxUniformsDirty = true;
1096 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001097 }
1098
1099 mForceSetViewport = false;
1100 return true;
1101}
1102
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001103bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1104{
1105 switch (mode)
1106 {
1107 case GL_POINTS:
1108 mPrimitiveType = D3DPT_POINTLIST;
1109 mPrimitiveCount = count;
1110 break;
1111 case GL_LINES:
1112 mPrimitiveType = D3DPT_LINELIST;
1113 mPrimitiveCount = count / 2;
1114 break;
1115 case GL_LINE_LOOP:
1116 mPrimitiveType = D3DPT_LINESTRIP;
1117 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1118 break;
1119 case GL_LINE_STRIP:
1120 mPrimitiveType = D3DPT_LINESTRIP;
1121 mPrimitiveCount = count - 1;
1122 break;
1123 case GL_TRIANGLES:
1124 mPrimitiveType = D3DPT_TRIANGLELIST;
1125 mPrimitiveCount = count / 3;
1126 break;
1127 case GL_TRIANGLE_STRIP:
1128 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1129 mPrimitiveCount = count - 2;
1130 break;
1131 case GL_TRIANGLE_FAN:
1132 mPrimitiveType = D3DPT_TRIANGLEFAN;
1133 mPrimitiveCount = count - 2;
1134 break;
1135 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001136 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001137 }
1138
1139 return mPrimitiveCount > 0;
1140}
1141
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001142
1143gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1144{
1145 if (!depthbuffer)
1146 {
1147 ERR("Unexpected null depthbuffer for depth-only FBO.");
1148 return NULL;
1149 }
1150
1151 GLsizei width = depthbuffer->getWidth();
1152 GLsizei height = depthbuffer->getHeight();
1153
1154 // search cached nullcolorbuffers
1155 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1156 {
1157 if (mNullColorbufferCache[i].buffer != NULL &&
1158 mNullColorbufferCache[i].width == width &&
1159 mNullColorbufferCache[i].height == height)
1160 {
1161 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1162 return mNullColorbufferCache[i].buffer;
1163 }
1164 }
1165
1166 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1167
1168 // add nullbuffer to the cache
1169 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1170 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1171 {
1172 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1173 {
1174 oldest = &mNullColorbufferCache[i];
1175 }
1176 }
1177
1178 delete oldest->buffer;
1179 oldest->buffer = nullbuffer;
1180 oldest->lruCount = ++mMaxNullColorbufferLRU;
1181 oldest->width = width;
1182 oldest->height = height;
1183
1184 return nullbuffer;
1185}
1186
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001187bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001188{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001189 // if there is no color attachment we must synthesize a NULL colorattachment
1190 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1191 gl::Renderbuffer *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001192 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001193 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001194 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001195 }
1196 else
1197 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001198 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001199 }
1200 if (!renderbufferObject)
1201 {
1202 ERR("unable to locate renderbuffer for FBO.");
1203 return false;
1204 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001205
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001206 bool renderTargetChanged = false;
1207 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1208 if (renderTargetSerial != mAppliedRenderTargetSerial)
1209 {
1210 // Apply the render target on the device
1211 IDirect3DSurface9 *renderTargetSurface = NULL;
1212
1213 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1214 if (renderTarget)
1215 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001216 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001217 }
1218
1219 if (!renderTargetSurface)
1220 {
1221 ERR("render target pointer unexpectedly null.");
1222 return false; // Context must be lost
1223 }
1224
1225 mDevice->SetRenderTarget(0, renderTargetSurface);
Geoff Langea228632013-07-30 15:17:12 -04001226 SafeRelease(renderTargetSurface);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001227
1228 mAppliedRenderTargetSerial = renderTargetSerial;
1229 renderTargetChanged = true;
1230 }
1231
1232 gl::Renderbuffer *depthStencil = NULL;
1233 unsigned int depthbufferSerial = 0;
1234 unsigned int stencilbufferSerial = 0;
1235 if (framebuffer->getDepthbufferType() != GL_NONE)
1236 {
1237 depthStencil = framebuffer->getDepthbuffer();
1238 if (!depthStencil)
1239 {
1240 ERR("Depth stencil pointer unexpectedly null.");
1241 return false;
1242 }
1243
1244 depthbufferSerial = depthStencil->getSerial();
1245 }
1246 else if (framebuffer->getStencilbufferType() != GL_NONE)
1247 {
1248 depthStencil = framebuffer->getStencilbuffer();
1249 if (!depthStencil)
1250 {
1251 ERR("Depth stencil pointer unexpectedly null.");
1252 return false;
1253 }
1254
1255 stencilbufferSerial = depthStencil->getSerial();
1256 }
1257
1258 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1259 stencilbufferSerial != mAppliedStencilbufferSerial ||
1260 !mDepthStencilInitialized)
1261 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001262 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001263 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001264
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001265 // Apply the depth stencil on the device
1266 if (depthStencil)
1267 {
1268 IDirect3DSurface9 *depthStencilSurface = NULL;
1269 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1270
1271 if (depthStencilRenderTarget)
1272 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001273 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001274 }
1275
1276 if (!depthStencilSurface)
1277 {
1278 ERR("depth stencil pointer unexpectedly null.");
1279 return false; // Context must be lost
1280 }
1281
1282 mDevice->SetDepthStencilSurface(depthStencilSurface);
Geoff Langea228632013-07-30 15:17:12 -04001283 SafeRelease(depthStencilSurface);
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001284
1285 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001286 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001287 }
1288 else
1289 {
1290 mDevice->SetDepthStencilSurface(NULL);
1291 }
1292
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001293 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1294 {
1295 mCurDepthSize = depthSize;
1296 mForceSetRasterState = true;
1297 }
1298
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001299 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1300 {
1301 mCurStencilSize = stencilSize;
1302 mForceSetDepthStencilState = true;
1303 }
1304
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001305 mAppliedDepthbufferSerial = depthbufferSerial;
1306 mAppliedStencilbufferSerial = stencilbufferSerial;
1307 mDepthStencilInitialized = true;
1308 }
1309
1310 if (renderTargetChanged || !mRenderTargetDescInitialized)
1311 {
1312 mForceSetScissor = true;
1313 mForceSetViewport = true;
1314
1315 mRenderTargetDesc.width = renderbufferObject->getWidth();
1316 mRenderTargetDesc.height = renderbufferObject->getHeight();
1317 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1318 mRenderTargetDescInitialized = true;
1319 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001320
1321 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001322}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001323
Jamie Madill57a89722013-07-02 11:57:03 -04001324GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001325 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001326{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001327 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001328 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001329 if (err != GL_NO_ERROR)
1330 {
1331 return err;
1332 }
1333
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001334 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1335}
1336
1337// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001338GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001339{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001340 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001341
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001342 if (err == GL_NO_ERROR)
1343 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001344 // Directly binding the storage buffer is not supported for d3d9
1345 ASSERT(indexInfo->storage == NULL);
1346
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001347 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001348 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001349 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1350
1351 mDevice->SetIndices(indexBuffer->getBuffer());
1352 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001353 }
1354 }
1355
1356 return err;
1357}
1358
1359void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1360{
1361 startScene();
1362
1363 if (mode == GL_LINE_LOOP)
1364 {
1365 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1366 }
1367 else if (instances > 0)
1368 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001369 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001370 if (countingIB)
1371 {
1372 if (mAppliedIBSerial != countingIB->getSerial())
1373 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001374 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1375
1376 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001377 mAppliedIBSerial = countingIB->getSerial();
1378 }
1379
1380 for (int i = 0; i < mRepeatDraw; i++)
1381 {
1382 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1383 }
1384 }
1385 else
1386 {
1387 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001388 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001389 }
1390 }
1391 else // Regular case
1392 {
1393 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1394 }
1395}
1396
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001397void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001398{
1399 startScene();
1400
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001401 if (mode == GL_POINTS)
1402 {
1403 drawIndexedPoints(count, type, indices, elementArrayBuffer);
1404 }
1405 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001406 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001407 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001408 }
1409 else
1410 {
1411 for (int i = 0; i < mRepeatDraw; i++)
1412 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001413 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1414 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001415 }
1416 }
1417}
1418
1419void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1420{
1421 // Get the raw indices for an indexed draw
1422 if (type != GL_NONE && elementArrayBuffer)
1423 {
1424 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001425 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001426 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001427 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001428 }
1429
Geoff Langa36ead42013-08-02 11:54:08 -04001430 unsigned int startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001431
1432 if (get32BitIndexSupport())
1433 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001434 if (!mLineLoopIB)
1435 {
1436 mLineLoopIB = new StreamingIndexBufferInterface(this);
1437 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1438 {
1439 delete mLineLoopIB;
1440 mLineLoopIB = NULL;
1441
1442 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001443 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001444 }
1445 }
1446
Geoff Lang57e713e2013-07-31 17:01:58 -04001447 // Checked by Renderer9::applyPrimitiveType
1448 ASSERT(count >= 0);
1449
1450 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001451 {
1452 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1453 return gl::error(GL_OUT_OF_MEMORY);
1454 }
1455
Geoff Lang57e713e2013-07-31 17:01:58 -04001456 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001457 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001458 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001459 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001460 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001461 }
1462
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001463 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001464 unsigned int offset = 0;
1465 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001466 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001467 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001468 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001469 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001470
Geoff Langa36ead42013-08-02 11:54:08 -04001471 startIndex = static_cast<unsigned int>(offset) / 4;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001472 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1473
1474 switch (type)
1475 {
1476 case GL_NONE: // Non-indexed draw
1477 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001478 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001479 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001480 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001481 data[count] = 0;
1482 break;
1483 case GL_UNSIGNED_BYTE:
1484 for (int i = 0; i < count; i++)
1485 {
1486 data[i] = static_cast<const GLubyte*>(indices)[i];
1487 }
1488 data[count] = static_cast<const GLubyte*>(indices)[0];
1489 break;
1490 case GL_UNSIGNED_SHORT:
1491 for (int i = 0; i < count; i++)
1492 {
1493 data[i] = static_cast<const GLushort*>(indices)[i];
1494 }
1495 data[count] = static_cast<const GLushort*>(indices)[0];
1496 break;
1497 case GL_UNSIGNED_INT:
1498 for (int i = 0; i < count; i++)
1499 {
1500 data[i] = static_cast<const GLuint*>(indices)[i];
1501 }
1502 data[count] = static_cast<const GLuint*>(indices)[0];
1503 break;
1504 default: UNREACHABLE();
1505 }
1506
1507 if (!mLineLoopIB->unmapBuffer())
1508 {
1509 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001510 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001511 }
1512 }
1513 else
1514 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001515 if (!mLineLoopIB)
1516 {
1517 mLineLoopIB = new StreamingIndexBufferInterface(this);
1518 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1519 {
1520 delete mLineLoopIB;
1521 mLineLoopIB = NULL;
1522
1523 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001524 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001525 }
1526 }
1527
Geoff Lang57e713e2013-07-31 17:01:58 -04001528 // Checked by Renderer9::applyPrimitiveType
1529 ASSERT(count >= 0);
1530
1531 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001532 {
1533 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1534 return gl::error(GL_OUT_OF_MEMORY);
1535 }
1536
Geoff Lang57e713e2013-07-31 17:01:58 -04001537 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001538 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001539 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001540 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001541 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001542 }
1543
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001544 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001545 unsigned int offset;
1546 if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001547 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001548 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001549 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001550 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001551
Geoff Langa36ead42013-08-02 11:54:08 -04001552 startIndex = static_cast<unsigned int>(offset) / 2;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001553 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1554
1555 switch (type)
1556 {
1557 case GL_NONE: // Non-indexed draw
1558 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001559 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001560 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001561 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001562 data[count] = 0;
1563 break;
1564 case GL_UNSIGNED_BYTE:
1565 for (int i = 0; i < count; i++)
1566 {
1567 data[i] = static_cast<const GLubyte*>(indices)[i];
1568 }
1569 data[count] = static_cast<const GLubyte*>(indices)[0];
1570 break;
1571 case GL_UNSIGNED_SHORT:
1572 for (int i = 0; i < count; i++)
1573 {
1574 data[i] = static_cast<const GLushort*>(indices)[i];
1575 }
1576 data[count] = static_cast<const GLushort*>(indices)[0];
1577 break;
1578 case GL_UNSIGNED_INT:
1579 for (int i = 0; i < count; i++)
1580 {
1581 data[i] = static_cast<const GLuint*>(indices)[i];
1582 }
1583 data[count] = static_cast<const GLuint*>(indices)[0];
1584 break;
1585 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001586 }
1587
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001588 if (!mLineLoopIB->unmapBuffer())
1589 {
1590 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001591 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001592 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001593 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001594
1595 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001596 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001597 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1598
1599 mDevice->SetIndices(indexBuffer->getBuffer());
1600 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001601 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001602
1603 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001604}
1605
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001606template <typename T>
1607static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1608{
1609 for (int i = 0; i < count; i++)
1610 {
1611 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1612 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1613 }
1614}
1615
1616void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1617{
1618 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1619 // for each individual point. This call is not expected to happen often.
1620
1621 if (elementArrayBuffer)
1622 {
1623 BufferStorage *storage = elementArrayBuffer->getStorage();
1624 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1625 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1626 }
1627
1628 switch (type)
1629 {
1630 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1631 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1632 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1633 default: UNREACHABLE();
1634 }
1635}
1636
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001637void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1638{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001639 unsigned int programBinarySerial = programBinary->getSerial();
1640 if (programBinarySerial != mAppliedProgramBinarySerial)
1641 {
1642 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1643 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001644
daniel@transgaming.come4991412012-12-20 20:55:34 +00001645 IDirect3DVertexShader9 *vertexShader = NULL;
1646 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001647
daniel@transgaming.come4991412012-12-20 20:55:34 +00001648 IDirect3DPixelShader9 *pixelShader = NULL;
1649 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001650
daniel@transgaming.come4991412012-12-20 20:55:34 +00001651 mDevice->SetPixelShader(pixelShader);
1652 mDevice->SetVertexShader(vertexShader);
1653 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001654 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001655
1656 mAppliedProgramBinarySerial = programBinarySerial;
1657 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001658}
1659
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001660void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001661{
1662 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1663 {
1664 gl::Uniform *targetUniform = *ub;
1665
1666 if (targetUniform->dirty)
1667 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001668 GLfloat *f = (GLfloat*)targetUniform->data;
1669 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001670
1671 switch (targetUniform->type)
1672 {
1673 case GL_SAMPLER_2D:
1674 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001675 break;
1676 case GL_BOOL:
1677 case GL_BOOL_VEC2:
1678 case GL_BOOL_VEC3:
1679 case GL_BOOL_VEC4:
1680 applyUniformnbv(targetUniform, i);
1681 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001682 case GL_FLOAT:
1683 case GL_FLOAT_VEC2:
1684 case GL_FLOAT_VEC3:
1685 case GL_FLOAT_VEC4:
1686 case GL_FLOAT_MAT2:
1687 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001688 case GL_FLOAT_MAT4:
1689 applyUniformnfv(targetUniform, f);
1690 break;
1691 case GL_INT:
1692 case GL_INT_VEC2:
1693 case GL_INT_VEC3:
1694 case GL_INT_VEC4:
1695 applyUniformniv(targetUniform, i);
1696 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001697 default:
1698 UNREACHABLE();
1699 }
1700
1701 targetUniform->dirty = false;
1702 }
1703 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001704
1705 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001706 if (mDxUniformsDirty)
1707 {
1708 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1709 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1710 mDxUniformsDirty = false;
1711 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001712}
1713
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001714void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001715{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001716 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001717 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001718 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001719 }
1720
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001721 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001722 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001723 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001724 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001725}
1726
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001727void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001728{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001729 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1730 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001731
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001732 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001733 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001734 vector[i][0] = (GLfloat)v[4 * i + 0];
1735 vector[i][1] = (GLfloat)v[4 * i + 1];
1736 vector[i][2] = (GLfloat)v[4 * i + 2];
1737 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001738 }
1739
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001740 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001741}
1742
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001743void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001744{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001745 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1746 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001747
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001748 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001749 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001750 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1751 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1752 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1753 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001754 }
1755
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001756 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001757}
1758
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001759void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001760{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001761 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1762 gl::unorm<8>(clearParams.colorClearValue.red),
1763 gl::unorm<8>(clearParams.colorClearValue.green),
1764 gl::unorm<8>(clearParams.colorClearValue.blue));
1765 float depth = gl::clamp01(clearParams.depthClearValue);
1766 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001767
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001768 unsigned int stencilUnmasked = 0x0;
1769 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1770 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001771 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1772 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001773 stencilUnmasked = (0x1 << stencilSize) - 1;
1774 }
1775
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001776 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1777 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001778
1779 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1780 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1781 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1782 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1783 clearParams.colorMaskBlue && alphaUnmasked);
1784
1785 if (needMaskedColorClear || needMaskedStencilClear)
1786 {
1787 // State which is altered in all paths from this point to the clear call is saved.
1788 // State which is altered in only some paths will be flagged dirty in the case that
1789 // that path is taken.
1790 HRESULT hr;
1791 if (mMaskedClearSavedState == NULL)
1792 {
1793 hr = mDevice->BeginStateBlock();
1794 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1795
1796 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1797 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1798 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1799 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1800 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1801 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1802 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1803 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1804 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1805 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1806 mDevice->SetPixelShader(NULL);
1807 mDevice->SetVertexShader(NULL);
1808 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1809 mDevice->SetStreamSource(0, NULL, 0, 0);
1810 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1811 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1812 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1813 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1814 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1815 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1816 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1817
1818 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1819 {
1820 mDevice->SetStreamSourceFreq(i, 1);
1821 }
1822
1823 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1824 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1825 }
1826
1827 ASSERT(mMaskedClearSavedState != NULL);
1828
1829 if (mMaskedClearSavedState != NULL)
1830 {
1831 hr = mMaskedClearSavedState->Capture();
1832 ASSERT(SUCCEEDED(hr));
1833 }
1834
1835 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1836 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1837 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1838 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1839 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1840 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1841 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1842 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1843
1844 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1845 {
1846 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1847 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1848 clearParams.colorMaskGreen,
1849 clearParams.colorMaskBlue,
1850 clearParams.colorMaskAlpha));
1851 }
1852 else
1853 {
1854 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1855 }
1856
1857 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1858 {
1859 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1860 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1861 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1862 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1863 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1864 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1865 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1866 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1867 }
1868 else
1869 {
1870 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1871 }
1872
1873 mDevice->SetPixelShader(NULL);
1874 mDevice->SetVertexShader(NULL);
1875 mDevice->SetFVF(D3DFVF_XYZRHW);
1876 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1877 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1878 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1879 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1880 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1881 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1882 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1883
1884 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1885 {
1886 mDevice->SetStreamSourceFreq(i, 1);
1887 }
1888
1889 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1890 quad[0][0] = -0.5f;
1891 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1892 quad[0][2] = 0.0f;
1893 quad[0][3] = 1.0f;
1894
1895 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1896 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1897 quad[1][2] = 0.0f;
1898 quad[1][3] = 1.0f;
1899
1900 quad[2][0] = -0.5f;
1901 quad[2][1] = -0.5f;
1902 quad[2][2] = 0.0f;
1903 quad[2][3] = 1.0f;
1904
1905 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1906 quad[3][1] = -0.5f;
1907 quad[3][2] = 0.0f;
1908 quad[3][3] = 1.0f;
1909
1910 startScene();
1911 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1912
1913 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1914 {
1915 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1916 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1917 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1918 }
1919
1920 if (mMaskedClearSavedState != NULL)
1921 {
1922 mMaskedClearSavedState->Apply();
1923 }
1924 }
1925 else if (clearParams.mask)
1926 {
1927 DWORD dxClearFlags = 0;
1928 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1929 {
1930 dxClearFlags |= D3DCLEAR_TARGET;
1931 }
1932 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1933 {
1934 dxClearFlags |= D3DCLEAR_ZBUFFER;
1935 }
1936 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1937 {
1938 dxClearFlags |= D3DCLEAR_STENCIL;
1939 }
1940
1941 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1942 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001943}
1944
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001945void Renderer9::markAllStateDirty()
1946{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001947 mAppliedRenderTargetSerial = 0;
1948 mAppliedDepthbufferSerial = 0;
1949 mAppliedStencilbufferSerial = 0;
1950 mDepthStencilInitialized = false;
1951 mRenderTargetDescInitialized = false;
1952
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001953 mForceSetDepthStencilState = true;
1954 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001955 mForceSetScissor = true;
1956 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001957 mForceSetBlendState = true;
1958
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001959 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001960 {
1961 mForceSetVertexSamplerStates[i] = true;
1962 mCurVertexTextureSerials[i] = 0;
1963 }
1964 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1965 {
1966 mForceSetPixelSamplerStates[i] = true;
1967 mCurPixelTextureSerials[i] = 0;
1968 }
1969
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001970 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001971 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001972 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001973
1974 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001975}
1976
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001977void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001978{
Geoff Langea228632013-07-30 15:17:12 -04001979 for (size_t i = 0; i < mEventQueryPool.size(); i++)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001980 {
Geoff Langea228632013-07-30 15:17:12 -04001981 SafeRelease(mEventQueryPool[i]);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001982 }
Geoff Langea228632013-07-30 15:17:12 -04001983 mEventQueryPool.clear();
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001984
Geoff Langea228632013-07-30 15:17:12 -04001985 SafeRelease(mMaskedClearSavedState);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001986
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001987 mVertexShaderCache.clear();
1988 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001989
Geoff Langea228632013-07-30 15:17:12 -04001990 SafeDelete(mBlit);
1991 SafeDelete(mVertexDataManager);
1992 SafeDelete(mIndexDataManager);
1993 SafeDelete(mLineLoopIB);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001994
1995 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1996 {
Geoff Langea228632013-07-30 15:17:12 -04001997 SafeDelete(mNullColorbufferCache[i].buffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001998 }
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001999}
2000
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002001void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002002{
2003 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002004 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002005}
2006
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002007bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002008{
2009 return mDeviceLost;
2010}
2011
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002012// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002013bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002014{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002015 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002016
2017 if (mDeviceEx)
2018 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002019 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002020 }
2021 else if (mDevice)
2022 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002023 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002024 }
2025 else
2026 {
2027 // No device yet, so no reset required
2028 }
2029
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002030 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2031
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002032 if (isLost)
2033 {
2034 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002035 // we'll probably get this done again by notifyDeviceLost
2036 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002037 // Note that we don't want to clear the device loss status here
2038 // -- this needs to be done by resetDevice
2039 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002040 if (notify)
2041 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002042 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002043 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002044 }
2045
2046 return isLost;
2047}
2048
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002049bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002050{
2051 HRESULT status = D3D_OK;
2052
2053 if (mDeviceEx)
2054 {
2055 status = mDeviceEx->CheckDeviceState(NULL);
2056 }
2057 else if (mDevice)
2058 {
2059 status = mDevice->TestCooperativeLevel();
2060 }
2061
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002062 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002063 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002064 switch (status)
2065 {
2066 case D3DERR_DEVICENOTRESET:
2067 case D3DERR_DEVICEHUNG:
2068 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002069 case D3DERR_DEVICELOST:
2070 return (mDeviceEx != NULL);
2071 case D3DERR_DEVICEREMOVED:
2072 UNIMPLEMENTED();
2073 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002074 default:
2075 return false;
2076 }
2077}
2078
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002079bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002080{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002081 releaseDeviceResources();
2082
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002083 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2084
2085 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002086 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002087 int attempts = 3;
2088
2089 while (lost && attempts > 0)
2090 {
2091 if (mDeviceEx)
2092 {
2093 Sleep(500); // Give the graphics driver some CPU time
2094 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2095 }
2096 else
2097 {
2098 result = mDevice->TestCooperativeLevel();
2099 while (result == D3DERR_DEVICELOST)
2100 {
2101 Sleep(100); // Give the graphics driver some CPU time
2102 result = mDevice->TestCooperativeLevel();
2103 }
2104
2105 if (result == D3DERR_DEVICENOTRESET)
2106 {
2107 result = mDevice->Reset(&presentParameters);
2108 }
2109 }
2110
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002111 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002112 attempts --;
2113 }
2114
2115 if (FAILED(result))
2116 {
2117 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2118 return false;
2119 }
2120
2121 // reset device defaults
2122 initializeDevice();
2123 mDeviceLost = false;
2124
2125 return true;
2126}
2127
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002128DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002129{
2130 return mAdapterIdentifier.VendorId;
2131}
2132
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002133std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002134{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002135 std::ostringstream rendererString;
2136
2137 rendererString << mAdapterIdentifier.Description;
2138 if (getShareHandleSupport())
2139 {
2140 rendererString << " Direct3D9Ex";
2141 }
2142 else
2143 {
2144 rendererString << " Direct3D9";
2145 }
2146
2147 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2148 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2149
2150 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002151}
2152
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002153GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002154{
2155 return mAdapterIdentifier.DeviceIdentifier;
2156}
2157
Geoff Lang61e49a52013-05-29 10:22:58 -04002158Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002159{
Geoff Lang61e49a52013-05-29 10:22:58 -04002160 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002161
Geoff Lang61e49a52013-05-29 10:22:58 -04002162 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2163 {
2164 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2165 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2166
2167 if (SUCCEEDED(result))
2168 {
2169 support.supportedSamples[multiSampleIndex] = true;
2170 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2171 {
2172 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2173 }
2174 }
2175 else
2176 {
2177 support.supportedSamples[multiSampleIndex] = false;
2178 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002179 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002180
2181 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002182}
2183
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002184bool Renderer9::getBGRATextureSupport() const
2185{
2186 // DirectX 9 always supports BGRA
2187 return true;
2188}
2189
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002190bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002191{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002192 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002193}
2194
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002195bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002196{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002197 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002198}
2199
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002200bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002201{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002202 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002203}
2204
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002205bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002206{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002207 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002208}
2209
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002210bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002211{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002212 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002213}
2214
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002215bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002216{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002217 return mFloat32FilterSupport;
2218}
2219
2220bool Renderer9::getFloat32TextureRenderingSupport() const
2221{
2222 return mFloat32RenderSupport;
2223}
2224
2225bool Renderer9::getFloat16TextureSupport() const
2226{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002227 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002228}
2229
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002230bool Renderer9::getFloat16TextureFilteringSupport() const
2231{
2232 return mFloat16FilterSupport;
2233}
2234
2235bool Renderer9::getFloat16TextureRenderingSupport() const
2236{
2237 return mFloat16RenderSupport;
2238}
2239
Geoff Langd42cf4e2013-06-05 16:09:17 -04002240bool Renderer9::getRGB565TextureSupport() const
2241{
2242 return mRGB565TextureSupport;
2243}
2244
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002245bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002246{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002247 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002248}
2249
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002250bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002251{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002252 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002253}
2254
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002255bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002256{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002257 return mSupportsTextureFilterAnisotropy;
2258}
2259
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002260float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002261{
2262 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002263 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002264 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002265 }
2266 return 1.0f;
2267}
2268
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002269bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002270{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002271 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002272}
2273
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002274unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002275{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002276 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2277 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002278}
2279
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002280unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2281{
2282 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2283}
2284
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002285unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002286{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002287 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002288}
2289
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002290unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002291{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002292 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002293}
2294
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002295unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002296{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002297 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2298}
2299
2300unsigned int Renderer9::getMaxFragmentUniformVectors() const
2301{
2302 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2303
2304 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2305}
2306
2307unsigned int Renderer9::getMaxVaryingVectors() const
2308{
2309 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002310}
2311
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002312unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2313{
2314 return 0;
2315}
2316
2317unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2318{
2319 return 0;
2320}
2321
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002322unsigned int Renderer9::getReservedVertexUniformBuffers() const
2323{
2324 return 0;
2325}
2326
2327unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2328{
2329 return 0;
2330}
2331
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002332unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2333{
2334 return 0;
2335}
2336
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002337unsigned int Renderer9::getMaxUniformBufferSize() const
2338{
2339 return 0;
2340}
2341
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002342bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002343{
2344 return mSupportsNonPower2Textures;
2345}
2346
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002347bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002348{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002349 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002350}
2351
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002352bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002353{
2354 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2355}
2356
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002357bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002358{
2359 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002360 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002361}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002362
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002363bool Renderer9::getDerivativeInstructionSupport() const
2364{
2365 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2366}
2367
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002368bool Renderer9::getPostSubBufferSupport() const
2369{
2370 return true;
2371}
2372
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002373int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002374{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002375 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002376}
2377
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002378float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002379{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002380 // Point size clamped at 1.0f for SM2
2381 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002382}
2383
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002384int Renderer9::getMaxViewportDimension() const
2385{
2386 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002387 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002388 return maxTextureDimension;
2389}
2390
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002391int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002392{
2393 return (int)mDeviceCaps.MaxTextureWidth;
2394}
2395
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002396int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002397{
2398 return (int)mDeviceCaps.MaxTextureHeight;
2399}
2400
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002401int Renderer9::getMaxTextureDepth() const
2402{
2403 // 3D textures are not available in the D3D9 backend.
2404 return 1;
2405}
2406
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002407int Renderer9::getMaxTextureArrayLayers() const
2408{
2409 // 2D array textures are not available in the D3D9 backend.
2410 return 1;
2411}
2412
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002413bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002414{
2415 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2416}
2417
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002418DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002419{
2420 return mDeviceCaps.DeclTypes;
2421}
2422
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002423int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002424{
2425 return mMinSwapInterval;
2426}
2427
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002428int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002429{
2430 return mMaxSwapInterval;
2431}
2432
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002433int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002434{
2435 return mMaxSupportedSamples;
2436}
2437
Geoff Lang0e120e32013-05-29 10:23:55 -04002438GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2439{
Shannon Woodsddb785c2013-07-08 10:32:13 -04002440 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
Geoff Lang0e120e32013-05-29 10:23:55 -04002441 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2442 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2443}
2444
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002445GLsizei Renderer9::getNumSampleCounts(GLint internalFormat) const
2446{
2447 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2448 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2449
2450 unsigned int numCounts = 0;
2451 if (iter != mMultiSampleSupport.end())
2452 {
2453 const MultisampleSupportInfo& info = iter->second;
2454 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2455 {
2456 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2457 {
2458 numCounts++;
2459 }
2460 }
2461 }
2462
2463 return numCounts;
2464}
2465
2466void Renderer9::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2467{
2468 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2469 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2470
2471 if (iter != mMultiSampleSupport.end())
2472 {
2473 const MultisampleSupportInfo& info = iter->second;
2474 int bufPos = 0;
2475 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2476 {
2477 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2478 {
2479 params[bufPos++] = i;
2480 }
2481 }
2482 }
2483}
2484
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002485int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002486{
2487 if (requested == 0)
2488 {
2489 return requested;
2490 }
2491
Geoff Lang61e49a52013-05-29 10:22:58 -04002492 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002493 if (itr == mMultiSampleSupport.end())
2494 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002495 if (format == D3DFMT_UNKNOWN)
2496 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002497 return -1;
2498 }
2499
Geoff Lang61e49a52013-05-29 10:22:58 -04002500 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002501 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002502 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002503 {
2504 return i;
2505 }
2506 }
2507
2508 return -1;
2509}
2510
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002511unsigned int Renderer9::getMaxRenderTargets() const
2512{
2513 // we do not support MRT in d3d9
2514 return 1;
2515}
2516
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002517D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2518{
2519 switch (internalformat)
2520 {
2521 case GL_DEPTH_COMPONENT16:
2522 case GL_DEPTH_COMPONENT32_OES:
2523 case GL_DEPTH24_STENCIL8_OES:
2524 return D3DFMT_INTZ;
2525 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2526 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2527 return D3DFMT_DXT1;
2528 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2529 return D3DFMT_DXT3;
2530 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2531 return D3DFMT_DXT5;
2532 case GL_RGBA32F_EXT:
2533 case GL_RGB32F_EXT:
2534 case GL_ALPHA32F_EXT:
2535 case GL_LUMINANCE32F_EXT:
2536 case GL_LUMINANCE_ALPHA32F_EXT:
2537 return D3DFMT_A32B32G32R32F;
2538 case GL_RGBA16F_EXT:
2539 case GL_RGB16F_EXT:
2540 case GL_ALPHA16F_EXT:
2541 case GL_LUMINANCE16F_EXT:
2542 case GL_LUMINANCE_ALPHA16F_EXT:
2543 return D3DFMT_A16B16G16R16F;
2544 case GL_LUMINANCE8_EXT:
2545 if (getLuminanceTextureSupport())
2546 {
2547 return D3DFMT_L8;
2548 }
2549 break;
2550 case GL_LUMINANCE8_ALPHA8_EXT:
2551 if (getLuminanceAlphaTextureSupport())
2552 {
2553 return D3DFMT_A8L8;
2554 }
2555 break;
2556 case GL_RGB8_OES:
2557 case GL_RGB565:
2558 return D3DFMT_X8R8G8B8;
2559 }
2560
2561 return D3DFMT_A8R8G8B8;
2562}
2563
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002564bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002565{
2566 bool result = false;
2567
2568 if (source && dest)
2569 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002570 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2571 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002572
2573 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002574 for (int i = 0; i < levels; ++i)
2575 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002576 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2577 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002578
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002579 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002580
Geoff Langea228632013-07-30 15:17:12 -04002581 SafeRelease(srcSurf);
2582 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002583
2584 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002585 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002586 return false;
Geoff Langea228632013-07-30 15:17:12 -04002587 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002588 }
2589 }
2590
2591 return result;
2592}
2593
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002594bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002595{
2596 bool result = false;
2597
2598 if (source && dest)
2599 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002600 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2601 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002602 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002603 for (int f = 0; f < 6; f++)
2604 {
2605 for (int i = 0; i < levels; i++)
2606 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002607 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2608 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002609
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002610 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002611
Geoff Langea228632013-07-30 15:17:12 -04002612 SafeRelease(srcSurf);
2613 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002614
2615 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002616 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002617 return false;
Geoff Langea228632013-07-30 15:17:12 -04002618 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002619 }
2620 }
2621 }
2622
2623 return result;
2624}
2625
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002626bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2627{
2628 // 3D textures are not available in the D3D9 backend.
2629 UNREACHABLE();
2630 return false;
2631}
2632
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002633bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2634{
2635 // 2D array textures are not supported by the D3D9 backend.
2636 UNREACHABLE();
2637 return false;
2638}
2639
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002640D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002641{
2642 if (mD3d9Ex != NULL)
2643 {
2644 return D3DPOOL_DEFAULT;
2645 }
2646 else
2647 {
2648 if (!(usage & D3DUSAGE_DYNAMIC))
2649 {
2650 return D3DPOOL_MANAGED;
2651 }
2652 }
2653
2654 return D3DPOOL_DEFAULT;
2655}
2656
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002657bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002658 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002659{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002660 RECT rect;
2661 rect.left = sourceRect.x;
2662 rect.top = sourceRect.y;
2663 rect.right = sourceRect.x + sourceRect.width;
2664 rect.bottom = sourceRect.y + sourceRect.height;
2665
2666 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002667}
2668
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002669bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002670 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002671{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002672 RECT rect;
2673 rect.left = sourceRect.x;
2674 rect.top = sourceRect.y;
2675 rect.right = sourceRect.x + sourceRect.width;
2676 rect.bottom = sourceRect.y + sourceRect.height;
2677
2678 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002679}
2680
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002681bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2682 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2683{
2684 // 3D textures are not available in the D3D9 backend.
2685 UNREACHABLE();
2686 return false;
2687}
2688
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002689bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2690 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2691{
2692 // 2D array textures are not available in the D3D9 backend.
2693 UNREACHABLE();
2694 return false;
2695}
2696
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002697bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04002698 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002699{
Geoff Lang758d5b22013-06-11 11:42:50 -04002700 ASSERT(filter == GL_NEAREST);
2701
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002702 endScene();
2703
2704 if (blitRenderTarget)
2705 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002706 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2707 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002708 RenderTarget9 *readRenderTarget = NULL;
2709 RenderTarget9 *drawRenderTarget = NULL;
2710 IDirect3DSurface9* readSurface = NULL;
2711 IDirect3DSurface9* drawSurface = NULL;
2712
2713 if (readBuffer)
2714 {
2715 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2716 }
2717 if (drawBuffer)
2718 {
2719 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2720 }
2721
2722 if (readRenderTarget)
2723 {
2724 readSurface = readRenderTarget->getSurface();
2725 }
2726 if (drawRenderTarget)
2727 {
2728 drawSurface = drawRenderTarget->getSurface();
2729 }
2730
2731 if (!readSurface || !drawSurface)
2732 {
2733 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002734 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002735 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002736
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002737 RECT srcRect;
2738 srcRect.left = readRect.x;
2739 srcRect.right = readRect.x + readRect.width;
2740 srcRect.top = readRect.y;
2741 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002742
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002743 RECT dstRect;
2744 dstRect.left = drawRect.x;
2745 dstRect.right = drawRect.x + drawRect.width;
2746 dstRect.top = drawRect.y;
2747 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002748
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002749 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002750
Geoff Langea228632013-07-30 15:17:12 -04002751 SafeRelease(readSurface);
2752 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002753
2754 if (FAILED(result))
2755 {
2756 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2757 return false;
2758 }
2759 }
2760
Geoff Lang685806d2013-06-12 11:16:36 -04002761 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002762 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002763 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2764 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2765 RenderTarget9 *readDepthStencil = NULL;
2766 RenderTarget9 *drawDepthStencil = NULL;
2767 IDirect3DSurface9* readSurface = NULL;
2768 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002769
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002770 if (readBuffer)
2771 {
2772 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2773 }
2774 if (drawBuffer)
2775 {
2776 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2777 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002778
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002779 if (readDepthStencil)
2780 {
2781 readSurface = readDepthStencil->getSurface();
2782 }
2783 if (drawDepthStencil)
2784 {
2785 drawSurface = drawDepthStencil->getSurface();
2786 }
2787
2788 if (!readSurface || !drawSurface)
2789 {
2790 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002791 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002792 }
2793
2794 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2795
Geoff Langea228632013-07-30 15:17:12 -04002796 SafeRelease(readSurface);
2797 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002798
2799 if (FAILED(result))
2800 {
2801 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2802 return false;
2803 }
2804 }
2805
2806 return true;
2807}
2808
2809void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2810 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2811{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002812 RenderTarget9 *renderTarget = NULL;
2813 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002814 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002815
2816 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002817 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002818 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2819 }
2820
2821 if (renderTarget)
2822 {
2823 surface = renderTarget->getSurface();
2824 }
2825
2826 if (!surface)
2827 {
2828 // context must be lost
2829 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002830 }
2831
2832 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002833 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002834
2835 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2836 {
2837 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
Geoff Langea228632013-07-30 15:17:12 -04002838 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002839 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002840 }
2841
2842 HRESULT result;
2843 IDirect3DSurface9 *systemSurface = NULL;
2844 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2845 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2846 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2847 if (directToPixels)
2848 {
2849 // Use the pixels ptr as a shared handle to write directly into client's memory
2850 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2851 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2852 if (FAILED(result))
2853 {
2854 // Try again without the shared handle
2855 directToPixels = false;
2856 }
2857 }
2858
2859 if (!directToPixels)
2860 {
2861 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2862 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2863 if (FAILED(result))
2864 {
2865 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
Geoff Langea228632013-07-30 15:17:12 -04002866 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002867 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002868 }
2869 }
2870
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002871 result = mDevice->GetRenderTargetData(surface, systemSurface);
Geoff Langea228632013-07-30 15:17:12 -04002872 SafeRelease(surface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002873
2874 if (FAILED(result))
2875 {
Geoff Langea228632013-07-30 15:17:12 -04002876 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002877
2878 // It turns out that D3D will sometimes produce more error
2879 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002880 if (d3d9::isDeviceLostError(result))
2881 {
2882 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002883 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002884 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002885 else
2886 {
2887 UNREACHABLE();
2888 return;
2889 }
2890
2891 }
2892
2893 if (directToPixels)
2894 {
Geoff Langea228632013-07-30 15:17:12 -04002895 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002896 return;
2897 }
2898
2899 RECT rect;
2900 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2901 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2902 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2903 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2904
2905 D3DLOCKED_RECT lock;
2906 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2907
2908 if (FAILED(result))
2909 {
2910 UNREACHABLE();
Geoff Langea228632013-07-30 15:17:12 -04002911 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002912
2913 return; // No sensible error to generate
2914 }
2915
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002916 unsigned char *source;
2917 int inputPitch;
2918 if (packReverseRowOrder)
2919 {
2920 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2921 inputPitch = -lock.Pitch;
2922 }
2923 else
2924 {
2925 source = (unsigned char*)lock.pBits;
2926 inputPitch = lock.Pitch;
2927 }
2928
Geoff Lang697ad3e2013-06-04 10:11:28 -04002929 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002930
Geoff Lang697ad3e2013-06-04 10:11:28 -04002931 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
2932 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
2933 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002934
Geoff Lang697ad3e2013-06-04 10:11:28 -04002935 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2936
2937 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002938 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002939 // Direct copy possible
2940 unsigned char *dest = static_cast<unsigned char*>(pixels);
2941 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002942 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002943 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002944 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002945 }
2946 else
2947 {
2948 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
2949 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
2950 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2951
2952 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
2953 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002954 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002955 // Fast copy is possible through some special function
2956 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002957 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002958 for (int x = 0; x < rect.right - rect.left; x++)
2959 {
2960 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2961 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
2962
2963 fastCopyFunc(src, dest);
2964 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002965 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002966 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002967 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002968 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002969 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
2970 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002971
Geoff Lang697ad3e2013-06-04 10:11:28 -04002972 gl::ColorF temp;
2973
2974 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002975 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002976 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002977 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002978 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2979 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002980
Geoff Lang697ad3e2013-06-04 10:11:28 -04002981 // readFunc and writeFunc will be using the same type of color, CopyTexImage
2982 // will not allow the copy otherwise.
2983 readFunc(src, &temp);
2984 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002985 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002986 }
2987 }
2988 }
2989
2990 systemSurface->UnlockRect();
Geoff Langea228632013-07-30 15:17:12 -04002991 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002992}
2993
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002994RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2995{
2996 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2997 IDirect3DSurface9 *surface = NULL;
2998 if (depth)
2999 {
3000 surface = swapChain9->getDepthStencil();
3001 }
3002 else
3003 {
3004 surface = swapChain9->getRenderTarget();
3005 }
3006
3007 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3008
3009 return renderTarget;
3010}
3011
Geoff Langa2d97f12013-06-11 11:44:02 -04003012RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003013{
3014 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3015 return renderTarget;
3016}
3017
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003018ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003019{
3020 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003021
3022 switch (type)
3023 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003024 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003025 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003026 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003027 if (vshader)
3028 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003029 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003030 }
3031 }
3032 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003033 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003034 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003035 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003036 if (pshader)
3037 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003038 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003039 }
3040 }
3041 break;
3042 default:
3043 UNREACHABLE();
3044 break;
3045 }
3046
3047 return executable;
3048}
3049
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003050ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003051{
3052 const char *profile = NULL;
3053
3054 switch (type)
3055 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003056 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003057 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3058 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003059 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003060 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3061 break;
3062 default:
3063 UNREACHABLE();
3064 return NULL;
3065 }
3066
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003067 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003068 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04003069 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003070 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04003071 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003072
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003073 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04003074 SafeRelease(binary);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003075
3076 return executable;
3077}
3078
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003079bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3080{
3081 return mBlit->boxFilter(source, dest);
3082}
3083
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003084D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003085{
3086 if (mD3d9Ex != NULL)
3087 {
3088 return D3DPOOL_DEFAULT;
3089 }
3090 else
3091 {
3092 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3093 {
3094 return D3DPOOL_MANAGED;
3095 }
3096 }
3097
3098 return D3DPOOL_DEFAULT;
3099}
3100
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003101bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3102{
3103 if (source && dest)
3104 {
3105 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003106
3107 if (fromManaged)
3108 {
3109 D3DSURFACE_DESC desc;
3110 source->GetDesc(&desc);
3111
3112 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003113 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003114
3115 if (SUCCEEDED(result))
3116 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003117 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003118 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
Geoff Langea228632013-07-30 15:17:12 -04003119 SafeRelease(surf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003120 }
3121 }
3122 else
3123 {
3124 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003125 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003126 }
3127
3128 if (FAILED(result))
3129 {
3130 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3131 return false;
3132 }
3133 }
3134
3135 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003136}
3137
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003138Image *Renderer9::createImage()
3139{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003140 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003141}
3142
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003143void Renderer9::generateMipmap(Image *dest, Image *src)
3144{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003145 Image9 *src9 = Image9::makeImage9(src);
3146 Image9 *dst9 = Image9::makeImage9(dest);
3147 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003148}
3149
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003150TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3151{
3152 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3153 return new TextureStorage9_2D(this, swapChain9);
3154}
3155
3156TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3157{
3158 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3159}
3160
3161TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3162{
3163 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3164}
3165
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003166TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3167{
3168 // 3D textures are not supported by the D3D9 backend.
3169 UNREACHABLE();
3170
3171 return NULL;
3172}
3173
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003174TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3175{
3176 // 2D array textures are not supported by the D3D9 backend.
3177 UNREACHABLE();
3178
3179 return NULL;
3180}
3181
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003182bool Renderer9::getLUID(LUID *adapterLuid) const
3183{
3184 adapterLuid->HighPart = 0;
3185 adapterLuid->LowPart = 0;
3186
3187 if (mD3d9Ex)
3188 {
3189 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3190 return true;
3191 }
3192
3193 return false;
3194}
3195
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003196}