blob: f422743a77f68d1cc3d04fc5d9df869bd2f2fd05 [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//
Geoff Langeeba6e12014-02-03 13:12:30 -05003// Copyright (c) 2012-2014 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"
Jamie Madille261b442014-06-25 12:42:21 -040016#include "libGLESv2/FramebufferAttachment.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000017#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000018#include "libGLESv2/ProgramBinary.h"
Brandon Jonesc7a41042014-06-23 12:03:25 -070019#include "libGLESv2/renderer/d3d/IndexDataManager.h"
Brandon Jonesf47bebc2014-07-09 14:28:42 -070020#include "libGLESv2/renderer/d3d/TextureD3D.h"
Brandon Jonesc7a41042014-06-23 12:03:25 -070021#include "libGLESv2/renderer/d3d/d3d9/Renderer9.h"
22#include "libGLESv2/renderer/d3d/d3d9/renderer9_utils.h"
23#include "libGLESv2/renderer/d3d/d3d9/formatutils9.h"
24#include "libGLESv2/renderer/d3d/d3d9/ShaderExecutable9.h"
25#include "libGLESv2/renderer/d3d/d3d9/SwapChain9.h"
26#include "libGLESv2/renderer/d3d/d3d9/TextureStorage9.h"
27#include "libGLESv2/renderer/d3d/d3d9/Image9.h"
28#include "libGLESv2/renderer/d3d/d3d9/Blit9.h"
29#include "libGLESv2/renderer/d3d/d3d9/RenderTarget9.h"
30#include "libGLESv2/renderer/d3d/d3d9/VertexBuffer9.h"
31#include "libGLESv2/renderer/d3d/d3d9/IndexBuffer9.h"
Brandon Jonesd38f9262014-06-18 16:26:45 -070032#include "libGLESv2/renderer/d3d/d3d9/Buffer9.h"
Brandon Jonesc7a41042014-06-23 12:03:25 -070033#include "libGLESv2/renderer/d3d/d3d9/Query9.h"
34#include "libGLESv2/renderer/d3d/d3d9/Fence9.h"
35#include "libGLESv2/renderer/d3d/d3d9/VertexArray9.h"
Jamie Madillc5a83002014-02-14 16:41:25 -050036#include "libGLESv2/angletypes.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000037
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000038#include "libEGL/Display.h"
39
Al Patrick3c6344e2013-09-23 14:57:00 -040040#include "third_party/trace_event/trace_event.h"
41
daniel@transgaming.com621ce052012-10-31 17:52:29 +000042// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
43#define REF_RAST 0
44
45// The "Debug This Pixel..." feature in PIX often fails when using the
46// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
47// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
48#if !defined(ANGLE_ENABLE_D3D9EX)
49// Enables use of the IDirect3D9Ex interface, when available
50#define ANGLE_ENABLE_D3D9EX 1
51#endif // !defined(ANGLE_ENABLE_D3D9EX)
52
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000053#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
54#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
55#endif
56
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000057const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z')));
58const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')));
59
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000060namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000061{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000062static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000063 {
64 D3DFMT_A1R5G5B5,
65 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
66 D3DFMT_A8R8G8B8,
67 D3DFMT_R5G6B5,
68 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
69 D3DFMT_X8R8G8B8
70 };
71
daniel@transgaming.com222ee082012-11-28 19:31:49 +000072static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000073 {
74 D3DFMT_UNKNOWN,
75 // D3DFMT_D16_LOCKABLE,
76 D3DFMT_D32,
77 // D3DFMT_D15S1,
78 D3DFMT_D24S8,
79 D3DFMT_D24X8,
80 // D3DFMT_D24X4S4,
81 D3DFMT_D16,
82 // D3DFMT_D32F_LOCKABLE,
83 // D3DFMT_D24FS8
84 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000085
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000086enum
87{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +000088 MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
89 MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32,
90 MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224,
91 MAX_VARYING_VECTORS_SM2 = 8,
92 MAX_VARYING_VECTORS_SM3 = 10,
93
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000094 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
95};
96
Geoff Lang591e6af2014-06-18 18:08:57 -040097Renderer9::Renderer9(egl::Display *display, EGLNativeDisplayType hDc, EGLint requestedDisplay)
98 : Renderer(display),
99 mDc(hDc)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000100{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000101 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000102
103 mD3d9 = NULL;
104 mD3d9Ex = NULL;
105 mDevice = NULL;
106 mDeviceEx = NULL;
107 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000108 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000109
110 mAdapter = D3DADAPTER_DEFAULT;
111
112 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
113 mDeviceType = D3DDEVTYPE_REF;
114 #else
115 mDeviceType = D3DDEVTYPE_HAL;
116 #endif
117
118 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000119
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +0000120 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000121
122 mVertexDataManager = NULL;
123 mIndexDataManager = NULL;
124 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000125
126 mMaxNullColorbufferLRU = 0;
127 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
128 {
129 mNullColorbufferCache[i].lruCount = 0;
130 mNullColorbufferCache[i].width = 0;
131 mNullColorbufferCache[i].height = 0;
132 mNullColorbufferCache[i].buffer = NULL;
133 }
Jamie Madill6246dc82014-01-29 09:26:47 -0500134
135 mAppliedVertexShader = NULL;
136 mAppliedPixelShader = NULL;
Jamie Madilla5c9a142014-05-26 16:39:33 -0400137 mAppliedProgramSerial = 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000138}
139
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000140Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000141{
142 if (mDevice)
143 {
144 // 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 +0000145 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000146 {
147 resetDevice();
148 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000149 }
150
Geoff Langcec35902014-04-16 10:52:36 -0400151 release();
Jamie Madillb7935e52013-11-20 15:47:47 -0500152}
153
Geoff Langcec35902014-04-16 10:52:36 -0400154void Renderer9::release()
Jamie Madillb7935e52013-11-20 15:47:47 -0500155{
156 releaseDeviceResources();
157
158 SafeRelease(mDevice);
Geoff Langea228632013-07-30 15:17:12 -0400159 SafeRelease(mDeviceEx);
160 SafeRelease(mD3d9);
Jamie Madillb7935e52013-11-20 15:47:47 -0500161 SafeRelease(mD3d9Ex);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000162
Geoff Langdad5ed32014-02-10 12:59:17 -0500163 mCompiler.release();
164
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000165 if (mDeviceWindow)
166 {
167 DestroyWindow(mDeviceWindow);
168 mDeviceWindow = NULL;
169 }
170
Geoff Langea228632013-07-30 15:17:12 -0400171 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000172}
173
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000174Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
175{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000176 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000177 return static_cast<rx::Renderer9*>(renderer);
178}
179
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000180EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000181{
Geoff Langdad5ed32014-02-10 12:59:17 -0500182 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000183 {
184 return EGL_NOT_INITIALIZED;
185 }
186
Geoff Lang64c83242014-05-06 10:49:24 -0400187 TRACE_EVENT0("gpu", "GetModuleHandle_d3d9");
188 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000189
190 if (mD3d9Module == NULL)
191 {
192 ERR("No D3D9 module found - aborting!\n");
193 return EGL_NOT_INITIALIZED;
194 }
195
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000196 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
197 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
198
199 // Use Direct3D9Ex if available. Among other things, this version is less
200 // inclined to report a lost context, for example when the user switches
201 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
202 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
203 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400204 TRACE_EVENT0("gpu", "D3d9Ex_QueryInterface");
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000205 ASSERT(mD3d9Ex);
Geoff Lang8e328842014-02-10 13:11:20 -0500206 mD3d9Ex->QueryInterface(__uuidof(IDirect3D9), reinterpret_cast<void**>(&mD3d9));
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000207 ASSERT(mD3d9);
208 }
209 else
210 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400211 TRACE_EVENT0("gpu", "Direct3DCreate9");
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000212 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
213 }
214
215 if (!mD3d9)
216 {
217 ERR("Could not create D3D9 device - aborting!\n");
218 return EGL_NOT_INITIALIZED;
219 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000220
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000221 if (mDc != NULL)
222 {
223 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
224 }
225
226 HRESULT result;
227
228 // Give up on getting device caps after about one second.
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000229 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400230 TRACE_EVENT0("gpu", "GetDeviceCaps");
231 for (int i = 0; i < 10; ++i)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000232 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400233 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
234 if (SUCCEEDED(result))
235 {
236 break;
237 }
238 else if (result == D3DERR_NOTAVAILABLE)
239 {
240 Sleep(100); // Give the driver some time to initialize/recover
241 }
242 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
243 {
244 ERR("failed to get device caps (0x%x)\n", result);
245 return EGL_NOT_INITIALIZED;
246 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000247 }
248 }
249
250 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
251 {
252 ERR("Renderer does not support PS 2.0. aborting!\n");
253 return EGL_NOT_INITIALIZED;
254 }
255
256 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
Jamie Madille83d1a92013-10-24 17:49:33 -0400257 // This is required by Texture2D::ensureRenderTarget.
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000258 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
259 {
260 ERR("Renderer does not support stretctrect from textures!\n");
261 return EGL_NOT_INITIALIZED;
262 }
263
Al Patrick3c6344e2013-09-23 14:57:00 -0400264 {
265 TRACE_EVENT0("gpu", "GetAdapterIdentifier");
266 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
267 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000268
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000269 mMinSwapInterval = 4;
270 mMaxSwapInterval = 0;
271
272 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
273 {
274 mMinSwapInterval = std::min(mMinSwapInterval, 0);
275 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
276 }
277 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
278 {
279 mMinSwapInterval = std::min(mMinSwapInterval, 1);
280 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
281 }
282 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
283 {
284 mMinSwapInterval = std::min(mMinSwapInterval, 2);
285 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
286 }
287 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
288 {
289 mMinSwapInterval = std::min(mMinSwapInterval, 3);
290 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
291 }
292 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
293 {
294 mMinSwapInterval = std::min(mMinSwapInterval, 4);
295 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
296 }
297
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000298 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
299 static const TCHAR className[] = TEXT("STATIC");
300
Al Patrick3c6344e2013-09-23 14:57:00 -0400301 {
302 TRACE_EVENT0("gpu", "CreateWindowEx");
303 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
304 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000305
306 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
307 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
308
Al Patrick3c6344e2013-09-23 14:57:00 -0400309 {
310 TRACE_EVENT0("gpu", "D3d9_CreateDevice");
311 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
312 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000313 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
314 {
315 return EGL_BAD_ALLOC;
316 }
317
318 if (FAILED(result))
319 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400320 TRACE_EVENT0("gpu", "D3d9_CreateDevice2");
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000321 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
322
323 if (FAILED(result))
324 {
325 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
326 return EGL_BAD_ALLOC;
327 }
328 }
329
330 if (mD3d9Ex)
331 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400332 TRACE_EVENT0("gpu", "mDevice_QueryInterface");
Geoff Lang8e328842014-02-10 13:11:20 -0500333 result = mDevice->QueryInterface(__uuidof(IDirect3DDevice9Ex), (void**)&mDeviceEx);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000334 ASSERT(SUCCEEDED(result));
335 }
336
Al Patrick3c6344e2013-09-23 14:57:00 -0400337 {
338 TRACE_EVENT0("gpu", "ShaderCache initialize");
339 mVertexShaderCache.initialize(mDevice);
340 mPixelShaderCache.initialize(mDevice);
341 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000342
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000343 D3DDISPLAYMODE currentDisplayMode;
344 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
345
346 // Check vertex texture support
347 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
348 // We test this using D3D9 by checking support for the R16F format.
349 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
350 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
351 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
352
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000353 initializeDevice();
354
Jamie Madill80177872014-02-04 16:04:10 -0500355 d3d9::InitializeVertexTranslations(this);
356
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000357 return EGL_SUCCESS;
358}
359
360// do any one-time device initialization
361// NOTE: this is also needed after a device lost/reset
362// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000363void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000364{
365 // Permanent non-default states
366 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
367 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
368
369 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
370 {
371 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
372 }
373 else
374 {
375 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
376 }
377
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000378 markAllStateDirty();
379
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000380 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000381
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000382 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400383 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000384 mVertexDataManager = new rx::VertexDataManager(this);
385 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000386}
387
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000388D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000389{
390 D3DPRESENT_PARAMETERS presentParameters = {0};
391
392 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
393 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
394 presentParameters.BackBufferCount = 1;
395 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
396 presentParameters.BackBufferWidth = 1;
397 presentParameters.BackBufferHeight = 1;
398 presentParameters.EnableAutoDepthStencil = FALSE;
399 presentParameters.Flags = 0;
400 presentParameters.hDeviceWindow = mDeviceWindow;
401 presentParameters.MultiSampleQuality = 0;
402 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
403 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
404 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
405 presentParameters.Windowed = TRUE;
406
407 return presentParameters;
408}
409
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000410int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000411{
412 D3DDISPLAYMODE currentDisplayMode;
413 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
414
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000415 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
416 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000417 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
418 int numConfigs = 0;
419
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000420 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000421 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000422 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000423
424 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
425
426 if (SUCCEEDED(result))
427 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000428 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000429 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000430 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000431 HRESULT result = D3D_OK;
432
433 if(depthStencilFormat != D3DFMT_UNKNOWN)
434 {
435 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
436 }
437
438 if (SUCCEEDED(result))
439 {
440 if(depthStencilFormat != D3DFMT_UNKNOWN)
441 {
442 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
443 }
444
445 if (SUCCEEDED(result))
446 {
447 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000448 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
449 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000450 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
451 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000452 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000453
454 (*configDescList)[numConfigs++] = newConfig;
455 }
456 }
457 }
458 }
459 }
460
461 return numConfigs;
462}
463
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000464void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000465{
466 delete [] (configDescList);
467}
468
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000469void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000470{
471 if (!mSceneStarted)
472 {
473 long result = mDevice->BeginScene();
474 if (SUCCEEDED(result)) {
475 // This is defensive checking against the device being
476 // lost at unexpected times.
477 mSceneStarted = true;
478 }
479 }
480}
481
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000482void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000483{
484 if (mSceneStarted)
485 {
486 // EndScene can fail if the device was lost, for example due
487 // to a TDR during a draw call.
488 mDevice->EndScene();
489 mSceneStarted = false;
490 }
491}
492
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000493void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000494{
495 HRESULT result;
496
497 IDirect3DQuery9* query = allocateEventQuery();
498 if (!query)
499 {
500 return;
501 }
502
503 result = query->Issue(D3DISSUE_END);
504 ASSERT(SUCCEEDED(result));
505
506 do
507 {
508 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
509
510 if(block && result == S_FALSE)
511 {
512 // Keep polling, but allow other threads to do something useful first
513 Sleep(0);
514 // explicitly check for device loss
515 // some drivers seem to return S_FALSE even if the device is lost
516 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000517 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000518 {
519 result = D3DERR_DEVICELOST;
520 }
521 }
522 }
523 while(block && result == S_FALSE);
524
525 freeEventQuery(query);
526
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000527 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000528 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000529 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000530 }
531}
532
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000533SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
534{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000535 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000536}
537
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000538IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000539{
540 IDirect3DQuery9 *query = NULL;
541
542 if (mEventQueryPool.empty())
543 {
544 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
Geoff Lang9cd19152014-05-28 15:54:34 -0400545 UNUSED_ASSERTION_VARIABLE(result);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000546 ASSERT(SUCCEEDED(result));
547 }
548 else
549 {
550 query = mEventQueryPool.back();
551 mEventQueryPool.pop_back();
552 }
553
554 return query;
555}
556
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000557void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000558{
559 if (mEventQueryPool.size() > 1000)
560 {
Geoff Langea228632013-07-30 15:17:12 -0400561 SafeRelease(query);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000562 }
563 else
564 {
565 mEventQueryPool.push_back(query);
566 }
567}
568
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000569IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000570{
571 return mVertexShaderCache.create(function, length);
572}
573
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000574IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000575{
576 return mPixelShaderCache.create(function, length);
577}
578
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000579HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
580{
581 D3DPOOL Pool = getBufferPool(Usage);
582 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
583}
584
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000585VertexBuffer *Renderer9::createVertexBuffer()
586{
587 return new VertexBuffer9(this);
588}
589
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000590HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
591{
592 D3DPOOL Pool = getBufferPool(Usage);
593 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
594}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000595
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000596IndexBuffer *Renderer9::createIndexBuffer()
597{
598 return new IndexBuffer9(this);
599}
600
Brandon Jonesd38f9262014-06-18 16:26:45 -0700601BufferImpl *Renderer9::createBuffer()
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000602{
Brandon Jonesd38f9262014-06-18 16:26:45 -0700603 return new Buffer9(this);
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000604}
605
Brandon Jones5bf98292014-06-06 17:19:38 -0700606VertexArrayImpl *Renderer9::createVertexArray()
607{
608 return new VertexArray9(this);
609}
610
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000611QueryImpl *Renderer9::createQuery(GLenum type)
612{
613 return new Query9(this, type);
614}
615
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000616FenceImpl *Renderer9::createFence()
617{
618 return new Fence9(this);
619}
620
Geoff Lang005df412013-10-16 14:12:50 -0400621bool Renderer9::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -0400622{
623 // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
624 return false;
625}
626
Jamie Madilla21eea32013-09-18 14:36:25 -0400627bool Renderer9::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
628 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
629{
630 // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
631 UNREACHABLE();
632 return false;
633}
634
Geoff Lange2e0ce02013-09-17 17:05:08 -0400635void Renderer9::generateSwizzle(gl::Texture *texture)
636{
637 // Swizzled textures are not available in ES2 or D3D9
638 UNREACHABLE();
639}
640
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000641void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000642{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000643 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
644 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000645
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000646 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000647 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000648 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
649 int d3dSampler = index + d3dSamplerOffset;
650
651 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
652 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
653
654 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
655 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
656 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
657 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
658 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
Nicolas Capens8de68282014-04-04 11:10:27 -0400659 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.baseLevel);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400660 if (getRendererExtensions().textureFilterAnisotropic)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000661 {
662 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
663 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000664 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000665
666 forceSetSamplers[index] = false;
667 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000668}
669
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000670void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000671{
672 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
673 int d3dSampler = index + d3dSamplerOffset;
674 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000675 unsigned int serial = 0;
676 bool forceSetTexture = false;
677
678 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000679
680 if (texture)
681 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000682 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000683 if (texStorage)
684 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000685 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000686 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000687 }
688 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000689 // in the texture class and we're unexpectedly missing the d3d texture
690 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000691
Brandon Jones6b19b002014-07-16 14:32:05 -0700692 TextureD3D* textureImpl = TextureD3D::makeTextureD3D(texture->getImplementation());
693
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000694 serial = texture->getTextureSerial();
Brandon Jones6b19b002014-07-16 14:32:05 -0700695 forceSetTexture = textureImpl->hasDirtyImages();
696 textureImpl->resetDirty();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000697 }
698
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000699 if (forceSetTexture || appliedSerials[index] != serial)
700 {
701 mDevice->SetTexture(d3dSampler, d3dTexture);
702 }
703
704 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000705}
706
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000707bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
708{
709 // No effect in ES2/D3D9
710 return true;
711}
712
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000713void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000714{
715 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000716
717 if (rasterStateChanged)
718 {
719 // Set the cull mode
720 if (rasterState.cullFace)
721 {
722 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
723 }
724 else
725 {
726 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
727 }
728
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000729 if (rasterState.polygonOffsetFill)
730 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000731 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000732 {
733 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
734
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000735 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000736 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
737 }
738 }
739 else
740 {
741 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
742 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
743 }
744
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000745 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000746 }
747
748 mForceSetRasterState = false;
749}
750
Geoff Langc142e9d2013-09-30 15:19:47 -0400751void Renderer9::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
752 unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000753{
754 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400755 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000756 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
757
758 if (blendStateChanged || blendColorChanged)
759 {
760 if (blendState.blend)
761 {
762 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
763
764 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
765 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
766 {
767 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
768 }
769 else
770 {
771 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
772 gl::unorm<8>(blendColor.alpha),
773 gl::unorm<8>(blendColor.alpha),
774 gl::unorm<8>(blendColor.alpha)));
775 }
776
777 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
778 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
779 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
780
781 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
782 blendState.destBlendRGB != blendState.destBlendAlpha ||
783 blendState.blendEquationRGB != blendState.blendEquationAlpha)
784 {
785 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
786
787 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
788 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
789 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
790 }
791 else
792 {
793 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
794 }
795 }
796 else
797 {
798 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
799 }
800
801 if (blendState.sampleAlphaToCoverage)
802 {
803 FIXME("Sample alpha to coverage is unimplemented.");
804 }
805
Jamie Madill3c7fa222014-06-05 13:08:51 -0400806 gl::FramebufferAttachment *attachment = framebuffer->getFirstColorbuffer();
807 GLenum internalFormat = attachment ? attachment->getInternalFormat() : GL_NONE;
Geoff Langc142e9d2013-09-30 15:19:47 -0400808
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000809 // Set the color mask
810 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
811 // Apparently some ATI cards have a bug where a draw with a zero color
812 // write mask can cause later draws to have incorrect results. Instead,
813 // set a nonzero color write mask but modify the blend state so that no
814 // drawing is done.
815 // http://code.google.com/p/angleproject/issues/detail?id=169
816
Geoff Lang5d601382014-07-22 15:14:06 -0400817 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
818 DWORD colorMask = gl_d3d9::ConvertColorMask(formatInfo.redBits > 0 && blendState.colorMaskRed,
819 formatInfo.greenBits > 0 && blendState.colorMaskGreen,
820 formatInfo.blueBits > 0 && blendState.colorMaskBlue,
821 formatInfo.alphaBits > 0 && blendState.colorMaskAlpha);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000822 if (colorMask == 0 && !zeroColorMaskAllowed)
823 {
824 // Enable green channel, but set blending so nothing will be drawn.
825 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
826 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
827
828 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
829 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
830 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
831 }
832 else
833 {
834 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
835 }
836
837 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
838
839 mCurBlendState = blendState;
840 mCurBlendColor = blendColor;
841 }
842
843 if (sampleMaskChanged)
844 {
845 // Set the multisample mask
846 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
847 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
848
849 mCurSampleMask = sampleMask;
850 }
851
852 mForceSetBlendState = false;
853}
854
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000855void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000856 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000857{
858 bool depthStencilStateChanged = mForceSetDepthStencilState ||
859 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000860 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
861 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000862 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000863
864 if (depthStencilStateChanged)
865 {
866 if (depthStencilState.depthTest)
867 {
868 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
869 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
870 }
871 else
872 {
873 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
874 }
875
876 mCurDepthStencilState = depthStencilState;
877 }
878
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000879 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000880 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000881 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000882 {
883 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
884 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
885
886 // FIXME: Unsupported by D3D9
887 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
888 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
889 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
Jamie Madillac528012014-06-20 13:21:23 -0400890
891 ASSERT(depthStencilState.stencilWritemask == depthStencilState.stencilBackWritemask);
892 ASSERT(stencilRef == stencilBackRef);
893 ASSERT(depthStencilState.stencilMask == depthStencilState.stencilBackMask);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000894
895 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000896 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000897
898 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
899 depthStencilState.stencilWritemask);
900 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
901 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
902
903 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000904 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000905 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
906 depthStencilState.stencilMask);
907
908 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
909 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
910 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
911 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
912 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
913 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
914
915 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
916 depthStencilState.stencilBackWritemask);
917 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
918 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
919
920 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000921 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000922 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
923 depthStencilState.stencilBackMask);
924
925 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
926 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
927 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
928 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
929 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
930 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
931 }
932 else
933 {
934 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
935 }
936
937 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
938
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000939 mCurStencilRef = stencilRef;
940 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000941 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000942 }
943
944 mForceSetDepthStencilState = false;
945}
946
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000947void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000948{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000949 bool scissorChanged = mForceSetScissor ||
950 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
951 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000952
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000953 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000954 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000955 if (enabled)
956 {
957 RECT rect;
958 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
959 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
960 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
961 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
962 mDevice->SetScissorRect(&rect);
963 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000964
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000965 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
966
967 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000968 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000969 }
970
971 mForceSetScissor = false;
972}
973
daniel@transgaming.com12985182012-12-20 20:56:31 +0000974bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000975 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000976{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000977 gl::Rectangle actualViewport = viewport;
978 float actualZNear = gl::clamp01(zNear);
979 float actualZFar = gl::clamp01(zFar);
980 if (ignoreViewport)
981 {
982 actualViewport.x = 0;
983 actualViewport.y = 0;
984 actualViewport.width = mRenderTargetDesc.width;
985 actualViewport.height = mRenderTargetDesc.height;
986 actualZNear = 0.0f;
987 actualZFar = 1.0f;
988 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000989
990 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000991 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
992 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
993 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
994 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
995 dxViewport.MinZ = actualZNear;
996 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000997
998 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
999 {
1000 return false; // Nothing to render
1001 }
1002
Geoff Langde14d602013-08-14 12:28:33 -04001003 float depthFront = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
1004
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001005 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
Geoff Langde14d602013-08-14 12:28:33 -04001006 actualZNear != mCurNear || actualZFar != mCurFar || mCurDepthFront != depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001007 if (viewportChanged)
1008 {
1009 mDevice->SetViewport(&dxViewport);
1010
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001011 mCurViewport = actualViewport;
1012 mCurNear = actualZNear;
1013 mCurFar = actualZFar;
Geoff Langde14d602013-08-14 12:28:33 -04001014 mCurDepthFront = depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001015
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001016 dx_VertexConstants vc = {0};
1017 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001018
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001019 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1020 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1021 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1022 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001023
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001024 pc.viewCoords[0] = actualViewport.width * 0.5f;
1025 pc.viewCoords[1] = actualViewport.height * 0.5f;
1026 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1027 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001028
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001029 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1030 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
Geoff Langde14d602013-08-14 12:28:33 -04001031 pc.depthFront[2] = depthFront;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001032
1033 vc.depthRange[0] = actualZNear;
1034 vc.depthRange[1] = actualZFar;
1035 vc.depthRange[2] = actualZFar - actualZNear;
1036
1037 pc.depthRange[0] = actualZNear;
1038 pc.depthRange[1] = actualZFar;
1039 pc.depthRange[2] = actualZFar - actualZNear;
1040
1041 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1042 {
1043 mVertexConstants = vc;
1044 mDxUniformsDirty = true;
1045 }
1046
1047 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1048 {
1049 mPixelConstants = pc;
1050 mDxUniformsDirty = true;
1051 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001052 }
1053
1054 mForceSetViewport = false;
1055 return true;
1056}
1057
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001058bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1059{
1060 switch (mode)
1061 {
1062 case GL_POINTS:
1063 mPrimitiveType = D3DPT_POINTLIST;
1064 mPrimitiveCount = count;
1065 break;
1066 case GL_LINES:
1067 mPrimitiveType = D3DPT_LINELIST;
1068 mPrimitiveCount = count / 2;
1069 break;
1070 case GL_LINE_LOOP:
1071 mPrimitiveType = D3DPT_LINESTRIP;
1072 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1073 break;
1074 case GL_LINE_STRIP:
1075 mPrimitiveType = D3DPT_LINESTRIP;
1076 mPrimitiveCount = count - 1;
1077 break;
1078 case GL_TRIANGLES:
1079 mPrimitiveType = D3DPT_TRIANGLELIST;
1080 mPrimitiveCount = count / 3;
1081 break;
1082 case GL_TRIANGLE_STRIP:
1083 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1084 mPrimitiveCount = count - 2;
1085 break;
1086 case GL_TRIANGLE_FAN:
1087 mPrimitiveType = D3DPT_TRIANGLEFAN;
1088 mPrimitiveCount = count - 2;
1089 break;
1090 default:
Jamie Madill1aeb1312014-06-20 13:21:25 -04001091 UNREACHABLE();
1092 return false;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001093 }
1094
1095 return mPrimitiveCount > 0;
1096}
1097
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001098
Jamie Madill3c7fa222014-06-05 13:08:51 -04001099gl::FramebufferAttachment *Renderer9::getNullColorbuffer(gl::FramebufferAttachment *depthbuffer)
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001100{
1101 if (!depthbuffer)
1102 {
1103 ERR("Unexpected null depthbuffer for depth-only FBO.");
1104 return NULL;
1105 }
1106
1107 GLsizei width = depthbuffer->getWidth();
1108 GLsizei height = depthbuffer->getHeight();
1109
1110 // search cached nullcolorbuffers
1111 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1112 {
1113 if (mNullColorbufferCache[i].buffer != NULL &&
1114 mNullColorbufferCache[i].width == width &&
1115 mNullColorbufferCache[i].height == height)
1116 {
1117 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1118 return mNullColorbufferCache[i].buffer;
1119 }
1120 }
1121
Geoff Lange4a492b2014-06-19 14:14:41 -04001122 gl::Renderbuffer *nullRenderbuffer = new gl::Renderbuffer(0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
Jamie Madille261b442014-06-25 12:42:21 -04001123 gl::RenderbufferAttachment *nullbuffer = new gl::RenderbufferAttachment(nullRenderbuffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001124
1125 // add nullbuffer to the cache
1126 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1127 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1128 {
1129 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1130 {
1131 oldest = &mNullColorbufferCache[i];
1132 }
1133 }
1134
1135 delete oldest->buffer;
1136 oldest->buffer = nullbuffer;
1137 oldest->lruCount = ++mMaxNullColorbufferLRU;
1138 oldest->width = width;
1139 oldest->height = height;
1140
1141 return nullbuffer;
1142}
1143
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001144bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001145{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001146 // if there is no color attachment we must synthesize a NULL colorattachment
1147 // to keep the D3D runtime happy. This should only be possible if depth texturing.
Jamie Madille92a3542014-07-03 10:38:58 -04001148 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(0);
1149 if (!attachment)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001150 {
Jamie Madille92a3542014-07-03 10:38:58 -04001151 attachment = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001152 }
Jamie Madille92a3542014-07-03 10:38:58 -04001153 if (!attachment)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001154 {
1155 ERR("unable to locate renderbuffer for FBO.");
1156 return false;
1157 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001158
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001159 bool renderTargetChanged = false;
Jamie Madille92a3542014-07-03 10:38:58 -04001160 unsigned int renderTargetSerial = attachment->getSerial();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001161 if (renderTargetSerial != mAppliedRenderTargetSerial)
1162 {
1163 // Apply the render target on the device
1164 IDirect3DSurface9 *renderTargetSurface = NULL;
1165
Jamie Madille92a3542014-07-03 10:38:58 -04001166 RenderTarget *renderTarget = attachment->getRenderTarget();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001167 if (renderTarget)
1168 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001169 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001170 }
1171
1172 if (!renderTargetSurface)
1173 {
1174 ERR("render target pointer unexpectedly null.");
1175 return false; // Context must be lost
1176 }
1177
1178 mDevice->SetRenderTarget(0, renderTargetSurface);
Geoff Langea228632013-07-30 15:17:12 -04001179 SafeRelease(renderTargetSurface);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001180
1181 mAppliedRenderTargetSerial = renderTargetSerial;
1182 renderTargetChanged = true;
1183 }
1184
Jamie Madille92a3542014-07-03 10:38:58 -04001185 gl::FramebufferAttachment *depthStencil = framebuffer->getDepthbuffer();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001186 unsigned int depthbufferSerial = 0;
1187 unsigned int stencilbufferSerial = 0;
Jamie Madille92a3542014-07-03 10:38:58 -04001188 if (depthStencil)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001189 {
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001190 depthbufferSerial = depthStencil->getSerial();
1191 }
Jamie Madille92a3542014-07-03 10:38:58 -04001192 else if (framebuffer->getStencilbuffer())
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001193 {
1194 depthStencil = framebuffer->getStencilbuffer();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001195 stencilbufferSerial = depthStencil->getSerial();
1196 }
1197
1198 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1199 stencilbufferSerial != mAppliedStencilbufferSerial ||
1200 !mDepthStencilInitialized)
1201 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001202 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001203 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001204
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001205 // Apply the depth stencil on the device
1206 if (depthStencil)
1207 {
1208 IDirect3DSurface9 *depthStencilSurface = NULL;
1209 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1210
1211 if (depthStencilRenderTarget)
1212 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001213 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001214 }
1215
1216 if (!depthStencilSurface)
1217 {
1218 ERR("depth stencil pointer unexpectedly null.");
1219 return false; // Context must be lost
1220 }
1221
1222 mDevice->SetDepthStencilSurface(depthStencilSurface);
Geoff Langea228632013-07-30 15:17:12 -04001223 SafeRelease(depthStencilSurface);
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001224
Geoff Lange4a492b2014-06-19 14:14:41 -04001225 depthSize = depthStencil->getDepthSize();
1226 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001227 }
1228 else
1229 {
1230 mDevice->SetDepthStencilSurface(NULL);
1231 }
1232
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001233 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1234 {
1235 mCurDepthSize = depthSize;
1236 mForceSetRasterState = true;
1237 }
1238
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001239 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1240 {
1241 mCurStencilSize = stencilSize;
1242 mForceSetDepthStencilState = true;
1243 }
1244
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001245 mAppliedDepthbufferSerial = depthbufferSerial;
1246 mAppliedStencilbufferSerial = stencilbufferSerial;
1247 mDepthStencilInitialized = true;
1248 }
1249
1250 if (renderTargetChanged || !mRenderTargetDescInitialized)
1251 {
1252 mForceSetScissor = true;
1253 mForceSetViewport = true;
Geoff Langc142e9d2013-09-30 15:19:47 -04001254 mForceSetBlendState = true;
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001255
Jamie Madille92a3542014-07-03 10:38:58 -04001256 mRenderTargetDesc.width = attachment->getWidth();
1257 mRenderTargetDesc.height = attachment->getHeight();
1258 mRenderTargetDesc.format = attachment->getActualFormat();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001259 mRenderTargetDescInitialized = true;
1260 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001261
1262 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001263}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001264
Shannon Woods53a94a82014-06-24 15:20:36 -04001265GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001266 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001267{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001268 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001269 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001270 if (err != GL_NO_ERROR)
1271 {
1272 return err;
1273 }
Jamie Madill13a2f852013-12-11 16:35:08 -05001274
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001275 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1276}
1277
1278// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001279GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001280{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001281 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001282
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001283 if (err == GL_NO_ERROR)
1284 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001285 // Directly binding the storage buffer is not supported for d3d9
1286 ASSERT(indexInfo->storage == NULL);
1287
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001288 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001289 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001290 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1291
1292 mDevice->SetIndices(indexBuffer->getBuffer());
1293 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001294 }
1295 }
1296
1297 return err;
1298}
1299
Geoff Langeeba6e12014-02-03 13:12:30 -05001300void Renderer9::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1301{
1302 UNREACHABLE();
1303}
1304
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001305void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001306{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001307 ASSERT(!transformFeedbackActive);
1308
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001309 startScene();
Jamie Madill13a2f852013-12-11 16:35:08 -05001310
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001311 if (mode == GL_LINE_LOOP)
1312 {
1313 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1314 }
1315 else if (instances > 0)
1316 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001317 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001318 if (countingIB)
1319 {
1320 if (mAppliedIBSerial != countingIB->getSerial())
1321 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001322 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1323
1324 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001325 mAppliedIBSerial = countingIB->getSerial();
1326 }
1327
1328 for (int i = 0; i < mRepeatDraw; i++)
1329 {
1330 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1331 }
1332 }
1333 else
1334 {
1335 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001336 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001337 }
1338 }
1339 else // Regular case
1340 {
1341 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1342 }
1343}
1344
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001345void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1346 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001347{
1348 startScene();
1349
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001350 if (mode == GL_POINTS)
1351 {
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001352 drawIndexedPoints(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001353 }
1354 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001355 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001356 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001357 }
1358 else
1359 {
1360 for (int i = 0; i < mRepeatDraw; i++)
1361 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001362 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1363 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001364 }
1365 }
1366}
1367
1368void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1369{
1370 // Get the raw indices for an indexed draw
1371 if (type != GL_NONE && elementArrayBuffer)
1372 {
1373 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001374 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001375 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001376 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001377 }
1378
Geoff Langa36ead42013-08-02 11:54:08 -04001379 unsigned int startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001380
Geoff Langc0b9ef42014-07-02 10:02:37 -04001381 if (getRendererExtensions().elementIndexUint)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001382 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001383 if (!mLineLoopIB)
1384 {
1385 mLineLoopIB = new StreamingIndexBufferInterface(this);
1386 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1387 {
1388 delete mLineLoopIB;
1389 mLineLoopIB = NULL;
1390
1391 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001392 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001393 }
1394 }
1395
Geoff Lang57e713e2013-07-31 17:01:58 -04001396 // Checked by Renderer9::applyPrimitiveType
1397 ASSERT(count >= 0);
1398
1399 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001400 {
1401 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1402 return gl::error(GL_OUT_OF_MEMORY);
1403 }
1404
Geoff Lang57e713e2013-07-31 17:01:58 -04001405 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001406 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001407 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001408 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001409 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001410 }
1411
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001412 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001413 unsigned int offset = 0;
1414 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001415 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001416 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001417 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001418 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001419
Geoff Langa36ead42013-08-02 11:54:08 -04001420 startIndex = static_cast<unsigned int>(offset) / 4;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001421 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1422
1423 switch (type)
1424 {
1425 case GL_NONE: // Non-indexed draw
1426 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001427 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001428 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001429 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001430 data[count] = 0;
1431 break;
1432 case GL_UNSIGNED_BYTE:
1433 for (int i = 0; i < count; i++)
1434 {
1435 data[i] = static_cast<const GLubyte*>(indices)[i];
1436 }
1437 data[count] = static_cast<const GLubyte*>(indices)[0];
1438 break;
1439 case GL_UNSIGNED_SHORT:
1440 for (int i = 0; i < count; i++)
1441 {
1442 data[i] = static_cast<const GLushort*>(indices)[i];
1443 }
1444 data[count] = static_cast<const GLushort*>(indices)[0];
1445 break;
1446 case GL_UNSIGNED_INT:
1447 for (int i = 0; i < count; i++)
1448 {
1449 data[i] = static_cast<const GLuint*>(indices)[i];
1450 }
1451 data[count] = static_cast<const GLuint*>(indices)[0];
1452 break;
1453 default: UNREACHABLE();
1454 }
1455
1456 if (!mLineLoopIB->unmapBuffer())
1457 {
1458 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001459 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001460 }
1461 }
1462 else
1463 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001464 if (!mLineLoopIB)
1465 {
1466 mLineLoopIB = new StreamingIndexBufferInterface(this);
1467 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1468 {
1469 delete mLineLoopIB;
1470 mLineLoopIB = NULL;
1471
1472 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001473 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001474 }
1475 }
1476
Geoff Lang57e713e2013-07-31 17:01:58 -04001477 // Checked by Renderer9::applyPrimitiveType
1478 ASSERT(count >= 0);
1479
1480 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001481 {
1482 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1483 return gl::error(GL_OUT_OF_MEMORY);
1484 }
1485
Geoff Lang57e713e2013-07-31 17:01:58 -04001486 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001487 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001488 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001489 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001490 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001491 }
1492
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001493 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001494 unsigned int offset;
1495 if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001496 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001497 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001498 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001499 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001500
Geoff Langa36ead42013-08-02 11:54:08 -04001501 startIndex = static_cast<unsigned int>(offset) / 2;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001502 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1503
1504 switch (type)
1505 {
1506 case GL_NONE: // Non-indexed draw
1507 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001508 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001509 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001510 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001511 data[count] = 0;
1512 break;
1513 case GL_UNSIGNED_BYTE:
1514 for (int i = 0; i < count; i++)
1515 {
1516 data[i] = static_cast<const GLubyte*>(indices)[i];
1517 }
1518 data[count] = static_cast<const GLubyte*>(indices)[0];
1519 break;
1520 case GL_UNSIGNED_SHORT:
1521 for (int i = 0; i < count; i++)
1522 {
1523 data[i] = static_cast<const GLushort*>(indices)[i];
1524 }
1525 data[count] = static_cast<const GLushort*>(indices)[0];
1526 break;
1527 case GL_UNSIGNED_INT:
1528 for (int i = 0; i < count; i++)
1529 {
1530 data[i] = static_cast<const GLuint*>(indices)[i];
1531 }
1532 data[count] = static_cast<const GLuint*>(indices)[0];
1533 break;
1534 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001535 }
1536
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001537 if (!mLineLoopIB->unmapBuffer())
1538 {
1539 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001540 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001541 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001542 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001543
1544 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001545 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001546 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1547
1548 mDevice->SetIndices(indexBuffer->getBuffer());
1549 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001550 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001551
1552 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001553}
1554
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001555template <typename T>
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001556static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices, int minIndex)
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001557{
1558 for (int i = 0; i < count; i++)
1559 {
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001560 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]) - minIndex;
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001561 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1562 }
1563}
1564
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001565void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001566{
1567 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1568 // for each individual point. This call is not expected to happen often.
1569
1570 if (elementArrayBuffer)
1571 {
Brandon Jonesd38f9262014-06-18 16:26:45 -07001572 BufferImpl *storage = elementArrayBuffer->getImplementation();
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001573 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1574 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1575 }
1576
1577 switch (type)
1578 {
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001579 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices, minIndex); break;
1580 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices, minIndex); break;
1581 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices, minIndex); break;
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001582 default: UNREACHABLE();
1583 }
1584}
1585
Geoff Lang04fb89a2014-06-09 15:05:36 -04001586void Renderer9::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1587 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001588{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001589 ASSERT(!transformFeedbackActive);
Geoff Lang0550d032014-01-30 11:29:07 -05001590 ASSERT(!rasterizerDiscard);
1591
Jamie Madillc5a83002014-02-14 16:41:25 -05001592 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001593 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001594
1595 IDirect3DVertexShader9 *vertexShader = (vertexExe ? ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader() : NULL);
1596 IDirect3DPixelShader9 *pixelShader = (pixelExe ? ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader() : NULL);
1597
Jamie Madill6246dc82014-01-29 09:26:47 -05001598 if (vertexShader != mAppliedVertexShader)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001599 {
daniel@transgaming.come4991412012-12-20 20:55:34 +00001600 mDevice->SetVertexShader(vertexShader);
Jamie Madill6246dc82014-01-29 09:26:47 -05001601 mAppliedVertexShader = vertexShader;
Jamie Madill6246dc82014-01-29 09:26:47 -05001602 }
daniel@transgaming.come4991412012-12-20 20:55:34 +00001603
Jamie Madill6246dc82014-01-29 09:26:47 -05001604 if (pixelShader != mAppliedPixelShader)
1605 {
1606 mDevice->SetPixelShader(pixelShader);
1607 mAppliedPixelShader = pixelShader;
Jamie Madill6246dc82014-01-29 09:26:47 -05001608 }
1609
Jamie Madilla5c9a142014-05-26 16:39:33 -04001610 // D3D9 has a quirk where creating multiple shaders with the same content
1611 // can return the same shader pointer. Because GL programs store different data
1612 // per-program, checking the program serial guarantees we upload fresh
1613 // uniform data even if our shader pointers are the same.
1614 // https://code.google.com/p/angleproject/issues/detail?id=661
1615 unsigned int programSerial = programBinary->getSerial();
1616 if (programSerial != mAppliedProgramSerial)
Jamie Madill6246dc82014-01-29 09:26:47 -05001617 {
1618 programBinary->dirtyAllUniforms();
Jamie Madilla5c9a142014-05-26 16:39:33 -04001619 mDxUniformsDirty = true;
1620 mAppliedProgramSerial = programSerial;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001621 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001622}
1623
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001624void Renderer9::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001625{
Jamie Madill834e8b72014-04-11 13:33:58 -04001626 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001627
1628 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001629 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001630 gl::LinkedUniform *targetUniform = uniformArray[uniformIndex];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001631
1632 if (targetUniform->dirty)
1633 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001634 GLfloat *f = (GLfloat*)targetUniform->data;
1635 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001636
1637 switch (targetUniform->type)
1638 {
1639 case GL_SAMPLER_2D:
1640 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001641 break;
1642 case GL_BOOL:
1643 case GL_BOOL_VEC2:
1644 case GL_BOOL_VEC3:
1645 case GL_BOOL_VEC4:
1646 applyUniformnbv(targetUniform, i);
1647 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001648 case GL_FLOAT:
1649 case GL_FLOAT_VEC2:
1650 case GL_FLOAT_VEC3:
1651 case GL_FLOAT_VEC4:
1652 case GL_FLOAT_MAT2:
1653 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001654 case GL_FLOAT_MAT4:
1655 applyUniformnfv(targetUniform, f);
1656 break;
1657 case GL_INT:
1658 case GL_INT_VEC2:
1659 case GL_INT_VEC3:
1660 case GL_INT_VEC4:
1661 applyUniformniv(targetUniform, i);
1662 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001663 default:
1664 UNREACHABLE();
1665 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001666 }
1667 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001668
1669 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001670 if (mDxUniformsDirty)
1671 {
1672 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1673 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1674 mDxUniformsDirty = false;
1675 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001676}
1677
Jamie Madill834e8b72014-04-11 13:33:58 -04001678void Renderer9::applyUniformnfv(gl::LinkedUniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001679{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001680 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001681 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001682 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001683 }
1684
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001685 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001686 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001687 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001688 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001689}
1690
Jamie Madill834e8b72014-04-11 13:33:58 -04001691void Renderer9::applyUniformniv(gl::LinkedUniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001692{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001693 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1694 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001695
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001696 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001697 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001698 vector[i][0] = (GLfloat)v[4 * i + 0];
1699 vector[i][1] = (GLfloat)v[4 * i + 1];
1700 vector[i][2] = (GLfloat)v[4 * i + 2];
1701 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001702 }
1703
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001704 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001705}
1706
Jamie Madill834e8b72014-04-11 13:33:58 -04001707void Renderer9::applyUniformnbv(gl::LinkedUniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001708{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001709 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1710 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001711
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001712 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001713 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001714 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1715 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1716 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1717 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001718 }
1719
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001720 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001721}
1722
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001723void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001724{
Geoff Langda507fe2013-08-20 12:01:42 -04001725 if (clearParams.colorClearType != GL_FLOAT)
1726 {
1727 // Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0
1728 UNREACHABLE();
1729 return;
1730 }
1731
1732 bool clearColor = clearParams.clearColor[0];
1733 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1734 {
1735 if (clearParams.clearColor[i] != clearColor)
1736 {
1737 // Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0
1738 UNREACHABLE();
1739 return;
1740 }
1741 }
1742
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001743 float depth = gl::clamp01(clearParams.depthClearValue);
Geoff Lange8bc3192014-02-14 13:04:03 -05001744 DWORD stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001745
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001746 unsigned int stencilUnmasked = 0x0;
Geoff Langda507fe2013-08-20 12:01:42 -04001747 if (clearParams.clearStencil && frameBuffer->hasStencil())
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001748 {
Geoff Lang5d601382014-07-22 15:14:06 -04001749 unsigned int stencilSize = gl::GetInternalFormatInfo((frameBuffer->getStencilbuffer()->getActualFormat())).stencilBits;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001750 stencilUnmasked = (0x1 << stencilSize) - 1;
1751 }
1752
Geoff Langda507fe2013-08-20 12:01:42 -04001753 const bool needMaskedStencilClear = clearParams.clearStencil &&
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001754 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
Geoff Lange8bc3192014-02-14 13:04:03 -05001755
1756 bool needMaskedColorClear = false;
1757 D3DCOLOR color = D3DCOLOR_ARGB(255, 0, 0, 0);
1758 if (clearColor)
1759 {
Geoff Lang5d601382014-07-22 15:14:06 -04001760 const gl::FramebufferAttachment *attachment = frameBuffer->getFirstColorbuffer();
1761 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(attachment->getInternalFormat());
1762 const gl::InternalFormat &actualFormatInfo = gl::GetInternalFormatInfo(attachment->getActualFormat());
Geoff Lange8bc3192014-02-14 13:04:03 -05001763
Geoff Lang5d601382014-07-22 15:14:06 -04001764 color = D3DCOLOR_ARGB(gl::unorm<8>((formatInfo.alphaBits == 0 && actualFormatInfo.alphaBits > 0) ? 1.0f : clearParams.colorFClearValue.alpha),
1765 gl::unorm<8>((formatInfo.redBits == 0 && actualFormatInfo.redBits > 0) ? 0.0f : clearParams.colorFClearValue.red),
1766 gl::unorm<8>((formatInfo.greenBits == 0 && actualFormatInfo.greenBits > 0) ? 0.0f : clearParams.colorFClearValue.green),
1767 gl::unorm<8>((formatInfo.blueBits == 0 && actualFormatInfo.blueBits > 0) ? 0.0f : clearParams.colorFClearValue.blue));
Geoff Lange8bc3192014-02-14 13:04:03 -05001768
Geoff Lang5d601382014-07-22 15:14:06 -04001769 if ((formatInfo.redBits > 0 && !clearParams.colorMaskRed) ||
1770 (formatInfo.greenBits > 0 && !clearParams.colorMaskGreen) ||
1771 (formatInfo.blueBits > 0 && !clearParams.colorMaskBlue) ||
1772 (formatInfo.alphaBits > 0 && !clearParams.colorMaskAlpha))
Geoff Lange8bc3192014-02-14 13:04:03 -05001773 {
1774 needMaskedColorClear = true;
1775 }
1776 }
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001777
1778 if (needMaskedColorClear || needMaskedStencilClear)
1779 {
1780 // State which is altered in all paths from this point to the clear call is saved.
1781 // State which is altered in only some paths will be flagged dirty in the case that
1782 // that path is taken.
1783 HRESULT hr;
1784 if (mMaskedClearSavedState == NULL)
1785 {
1786 hr = mDevice->BeginStateBlock();
1787 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1788
1789 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1790 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1791 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1792 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1793 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1794 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1795 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1796 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1797 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1798 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1799 mDevice->SetPixelShader(NULL);
1800 mDevice->SetVertexShader(NULL);
1801 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1802 mDevice->SetStreamSource(0, NULL, 0, 0);
1803 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1804 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1805 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1806 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1807 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1808 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1809 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1810
1811 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1812 {
1813 mDevice->SetStreamSourceFreq(i, 1);
1814 }
1815
1816 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1817 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1818 }
1819
1820 ASSERT(mMaskedClearSavedState != NULL);
1821
1822 if (mMaskedClearSavedState != NULL)
1823 {
1824 hr = mMaskedClearSavedState->Capture();
1825 ASSERT(SUCCEEDED(hr));
1826 }
1827
1828 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1829 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1830 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1831 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1832 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1833 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1834 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1835 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1836
Geoff Langda507fe2013-08-20 12:01:42 -04001837 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001838 {
1839 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1840 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1841 clearParams.colorMaskGreen,
1842 clearParams.colorMaskBlue,
1843 clearParams.colorMaskAlpha));
1844 }
1845 else
1846 {
1847 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1848 }
1849
Geoff Langda507fe2013-08-20 12:01:42 -04001850 if (stencilUnmasked != 0x0 && clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001851 {
1852 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1853 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1854 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1855 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1856 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1857 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1858 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1859 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1860 }
1861 else
1862 {
1863 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1864 }
1865
1866 mDevice->SetPixelShader(NULL);
1867 mDevice->SetVertexShader(NULL);
1868 mDevice->SetFVF(D3DFVF_XYZRHW);
1869 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1870 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1871 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1872 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1873 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1874 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1875 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1876
1877 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1878 {
1879 mDevice->SetStreamSourceFreq(i, 1);
1880 }
1881
1882 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1883 quad[0][0] = -0.5f;
1884 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1885 quad[0][2] = 0.0f;
1886 quad[0][3] = 1.0f;
1887
1888 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1889 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1890 quad[1][2] = 0.0f;
1891 quad[1][3] = 1.0f;
1892
1893 quad[2][0] = -0.5f;
1894 quad[2][1] = -0.5f;
1895 quad[2][2] = 0.0f;
1896 quad[2][3] = 1.0f;
1897
1898 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1899 quad[3][1] = -0.5f;
1900 quad[3][2] = 0.0f;
1901 quad[3][3] = 1.0f;
1902
1903 startScene();
1904 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1905
Geoff Langda507fe2013-08-20 12:01:42 -04001906 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001907 {
1908 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1909 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1910 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1911 }
1912
1913 if (mMaskedClearSavedState != NULL)
1914 {
1915 mMaskedClearSavedState->Apply();
1916 }
1917 }
Geoff Langda507fe2013-08-20 12:01:42 -04001918 else if (clearColor || clearParams.clearDepth || clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001919 {
1920 DWORD dxClearFlags = 0;
Geoff Langda507fe2013-08-20 12:01:42 -04001921 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001922 {
1923 dxClearFlags |= D3DCLEAR_TARGET;
1924 }
Geoff Langda507fe2013-08-20 12:01:42 -04001925 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001926 {
1927 dxClearFlags |= D3DCLEAR_ZBUFFER;
1928 }
Geoff Langda507fe2013-08-20 12:01:42 -04001929 if (clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001930 {
1931 dxClearFlags |= D3DCLEAR_STENCIL;
1932 }
1933
1934 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1935 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001936}
1937
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001938void Renderer9::markAllStateDirty()
1939{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001940 mAppliedRenderTargetSerial = 0;
1941 mAppliedDepthbufferSerial = 0;
1942 mAppliedStencilbufferSerial = 0;
1943 mDepthStencilInitialized = false;
1944 mRenderTargetDescInitialized = false;
1945
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001946 mForceSetDepthStencilState = true;
1947 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001948 mForceSetScissor = true;
1949 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001950 mForceSetBlendState = true;
1951
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001952 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001953 {
1954 mForceSetVertexSamplerStates[i] = true;
1955 mCurVertexTextureSerials[i] = 0;
1956 }
1957 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1958 {
1959 mForceSetPixelSamplerStates[i] = true;
1960 mCurPixelTextureSerials[i] = 0;
1961 }
1962
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001963 mAppliedIBSerial = 0;
Jamie Madill6246dc82014-01-29 09:26:47 -05001964 mAppliedVertexShader = NULL;
1965 mAppliedPixelShader = NULL;
Jamie Madilla5c9a142014-05-26 16:39:33 -04001966 mAppliedProgramSerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001967 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001968
1969 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001970}
1971
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001972void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001973{
Geoff Langea228632013-07-30 15:17:12 -04001974 for (size_t i = 0; i < mEventQueryPool.size(); i++)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001975 {
Geoff Langea228632013-07-30 15:17:12 -04001976 SafeRelease(mEventQueryPool[i]);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001977 }
Geoff Langea228632013-07-30 15:17:12 -04001978 mEventQueryPool.clear();
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001979
Geoff Langea228632013-07-30 15:17:12 -04001980 SafeRelease(mMaskedClearSavedState);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001981
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001982 mVertexShaderCache.clear();
1983 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001984
Geoff Langea228632013-07-30 15:17:12 -04001985 SafeDelete(mBlit);
1986 SafeDelete(mVertexDataManager);
1987 SafeDelete(mIndexDataManager);
1988 SafeDelete(mLineLoopIB);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001989
1990 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1991 {
Geoff Langea228632013-07-30 15:17:12 -04001992 SafeDelete(mNullColorbufferCache[i].buffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001993 }
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001994}
1995
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001996void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001997{
1998 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001999 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002000}
2001
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002002bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002003{
2004 return mDeviceLost;
2005}
2006
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002007// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002008bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002009{
Jamie Madillb7935e52013-11-20 15:47:47 -05002010 HRESULT status = getDeviceStatusCode();
Jamie Madillcd65ae12013-12-17 15:20:14 -05002011 bool isLost = FAILED(status);
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002012
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002013 if (isLost)
2014 {
2015 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002016 // we'll probably get this done again by notifyDeviceLost
2017 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002018 // Note that we don't want to clear the device loss status here
2019 // -- this needs to be done by resetDevice
2020 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002021 if (notify)
2022 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002023 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002024 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002025 }
2026
2027 return isLost;
2028}
2029
Jamie Madillb7935e52013-11-20 15:47:47 -05002030HRESULT Renderer9::getDeviceStatusCode()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002031{
2032 HRESULT status = D3D_OK;
2033
2034 if (mDeviceEx)
2035 {
2036 status = mDeviceEx->CheckDeviceState(NULL);
2037 }
2038 else if (mDevice)
2039 {
2040 status = mDevice->TestCooperativeLevel();
2041 }
2042
Jamie Madillb7935e52013-11-20 15:47:47 -05002043 return status;
2044}
2045
2046bool Renderer9::testDeviceResettable()
2047{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002048 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002049 // DEVICEREMOVED indicates the device has been stopped and must be recreated
Jamie Madillb7935e52013-11-20 15:47:47 -05002050 switch (getDeviceStatusCode())
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002051 {
2052 case D3DERR_DEVICENOTRESET:
2053 case D3DERR_DEVICEHUNG:
2054 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002055 case D3DERR_DEVICELOST:
2056 return (mDeviceEx != NULL);
2057 case D3DERR_DEVICEREMOVED:
Jamie Madillb7935e52013-11-20 15:47:47 -05002058 ASSERT(mDeviceEx != NULL);
Jamie Madill88f779d2013-12-03 10:57:56 -05002059 return isRemovedDeviceResettable();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002060 default:
2061 return false;
2062 }
2063}
2064
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002065bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002066{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002067 releaseDeviceResources();
2068
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002069 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2070
2071 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002072 bool lost = testDeviceLost(false);
Jamie Madillb7935e52013-11-20 15:47:47 -05002073 bool removedDevice = (getDeviceStatusCode() == D3DERR_DEVICEREMOVED);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002074
Jamie Madill88f779d2013-12-03 10:57:56 -05002075 // Device Removed is a feature which is only present with D3D9Ex
2076 ASSERT(mDeviceEx != NULL || !removedDevice);
2077
2078 for (int attempts = 3; lost && attempts > 0; attempts--)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002079 {
Jamie Madill88f779d2013-12-03 10:57:56 -05002080 if (removedDevice)
2081 {
2082 // Device removed, which may trigger on driver reinstallation,
2083 // may cause a longer wait other reset attempts before the
2084 // system is ready to handle creating a new device.
2085 Sleep(800);
2086 lost = !resetRemovedDevice();
2087 }
2088 else if (mDeviceEx)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002089 {
2090 Sleep(500); // Give the graphics driver some CPU time
Jamie Madill88f779d2013-12-03 10:57:56 -05002091 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2092 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002093 }
2094 else
2095 {
2096 result = mDevice->TestCooperativeLevel();
2097 while (result == D3DERR_DEVICELOST)
2098 {
2099 Sleep(100); // Give the graphics driver some CPU time
2100 result = mDevice->TestCooperativeLevel();
2101 }
2102
2103 if (result == D3DERR_DEVICENOTRESET)
2104 {
2105 result = mDevice->Reset(&presentParameters);
2106 }
Jamie Madill88f779d2013-12-03 10:57:56 -05002107 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002108 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002109 }
2110
2111 if (FAILED(result))
2112 {
2113 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2114 return false;
2115 }
2116
Jamie Madill88f779d2013-12-03 10:57:56 -05002117 if (removedDevice && lost)
2118 {
2119 ERR("Device lost reset failed multiple times");
2120 return false;
2121 }
2122
Jamie Madillb7935e52013-11-20 15:47:47 -05002123 // If the device was removed, we already finished re-initialization in resetRemovedDevice
2124 if (!removedDevice)
2125 {
2126 // reset device defaults
2127 initializeDevice();
2128 }
2129
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002130 mDeviceLost = false;
2131
2132 return true;
2133}
2134
Jamie Madill88f779d2013-12-03 10:57:56 -05002135bool Renderer9::isRemovedDeviceResettable() const
2136{
2137 bool success = false;
2138
2139#ifdef ANGLE_ENABLE_D3D9EX
2140 IDirect3D9Ex *d3d9Ex = NULL;
2141 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
2142 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
2143
2144 if (Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &d3d9Ex)))
2145 {
2146 D3DCAPS9 deviceCaps;
2147 HRESULT result = d3d9Ex->GetDeviceCaps(mAdapter, mDeviceType, &deviceCaps);
2148 success = SUCCEEDED(result);
2149 }
2150
2151 SafeRelease(d3d9Ex);
2152#else
2153 ASSERT(UNREACHABLE());
2154#endif
2155
2156 return success;
2157}
2158
2159bool Renderer9::resetRemovedDevice()
Jamie Madillb7935e52013-11-20 15:47:47 -05002160{
2161 // From http://msdn.microsoft.com/en-us/library/windows/desktop/bb172554(v=vs.85).aspx:
2162 // The hardware adapter has been removed. Application must destroy the device, do enumeration of
2163 // adapters and create another Direct3D device. If application continues rendering without
2164 // calling Reset, the rendering calls will succeed. Applies to Direct3D 9Ex only.
Geoff Langcec35902014-04-16 10:52:36 -04002165 release();
Jamie Madill88f779d2013-12-03 10:57:56 -05002166 return (initialize() == EGL_SUCCESS);
Jamie Madillb7935e52013-11-20 15:47:47 -05002167}
2168
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002169DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002170{
2171 return mAdapterIdentifier.VendorId;
2172}
2173
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002174std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002175{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002176 std::ostringstream rendererString;
2177
2178 rendererString << mAdapterIdentifier.Description;
2179 if (getShareHandleSupport())
2180 {
2181 rendererString << " Direct3D9Ex";
2182 }
2183 else
2184 {
2185 rendererString << " Direct3D9";
2186 }
2187
2188 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2189 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2190
2191 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002192}
2193
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002194GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002195{
2196 return mAdapterIdentifier.DeviceIdentifier;
2197}
2198
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002199unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002200{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002201 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2202 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002203}
2204
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002205unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2206{
2207 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2208}
2209
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002210unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002211{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002212 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002213}
2214
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002215unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002216{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002217 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002218}
2219
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002220unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002221{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002222 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2223}
2224
2225unsigned int Renderer9::getMaxFragmentUniformVectors() const
2226{
2227 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2228
2229 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2230}
2231
2232unsigned int Renderer9::getMaxVaryingVectors() const
2233{
2234 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002235}
2236
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002237unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2238{
2239 return 0;
2240}
2241
2242unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2243{
2244 return 0;
2245}
2246
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002247unsigned int Renderer9::getReservedVertexUniformBuffers() const
2248{
2249 return 0;
2250}
2251
2252unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2253{
2254 return 0;
2255}
2256
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002257unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2258{
2259 return 0;
2260}
2261
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002262unsigned int Renderer9::getMaxTransformFeedbackSeparateComponents() const
2263{
2264 return 0;
2265}
2266
2267unsigned int Renderer9::getMaxTransformFeedbackInterleavedComponents() const
2268{
2269 return 0;
2270}
2271
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002272unsigned int Renderer9::getMaxUniformBufferSize() const
2273{
2274 return 0;
2275}
2276
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002277bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002278{
2279 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002280 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002281}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002282
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002283bool Renderer9::getPostSubBufferSupport() const
2284{
2285 return true;
2286}
2287
Jamie Madill13a2f852013-12-11 16:35:08 -05002288int Renderer9::getMaxRecommendedElementsIndices() const
2289{
2290 // ES3 only
2291 UNREACHABLE();
2292 return 0;
2293}
2294
2295int Renderer9::getMaxRecommendedElementsVertices() const
2296{
2297 // ES3 only
2298 UNREACHABLE();
2299 return 0;
2300}
2301
Geoff Lang05b05022014-06-11 15:31:45 -04002302bool Renderer9::getSRGBTextureSupport() const
2303{
2304 return false;
2305}
2306
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002307int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002308{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002309 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002310}
2311
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002312DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002313{
2314 return mDeviceCaps.DeclTypes;
2315}
2316
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002317int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002318{
2319 return mMinSwapInterval;
2320}
2321
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002322int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002323{
2324 return mMaxSwapInterval;
2325}
2326
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002327bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002328{
2329 bool result = false;
2330
2331 if (source && dest)
2332 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002333 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2334 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002335
Nicolas Capensbf712d02014-03-31 14:23:35 -04002336 int levels = source9->getLevelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002337 for (int i = 0; i < levels; ++i)
2338 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002339 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2340 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
Jamie Madill13a2f852013-12-11 16:35:08 -05002341
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002342 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002343
Geoff Langea228632013-07-30 15:17:12 -04002344 SafeRelease(srcSurf);
2345 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002346
2347 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002348 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002349 return false;
Geoff Langea228632013-07-30 15:17:12 -04002350 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002351 }
2352 }
2353
2354 return result;
2355}
2356
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002357bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002358{
2359 bool result = false;
2360
2361 if (source && dest)
2362 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002363 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2364 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
Nicolas Capensbf712d02014-03-31 14:23:35 -04002365 int levels = source9->getLevelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002366 for (int f = 0; f < 6; f++)
2367 {
2368 for (int i = 0; i < levels; i++)
2369 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002370 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2371 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002372
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002373 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002374
Geoff Langea228632013-07-30 15:17:12 -04002375 SafeRelease(srcSurf);
2376 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002377
2378 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002379 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002380 return false;
Geoff Langea228632013-07-30 15:17:12 -04002381 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002382 }
2383 }
2384 }
2385
2386 return result;
2387}
2388
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002389bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2390{
2391 // 3D textures are not available in the D3D9 backend.
2392 UNREACHABLE();
2393 return false;
2394}
2395
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002396bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2397{
2398 // 2D array textures are not supported by the D3D9 backend.
2399 UNREACHABLE();
2400 return false;
2401}
2402
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002403D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002404{
2405 if (mD3d9Ex != NULL)
2406 {
2407 return D3DPOOL_DEFAULT;
2408 }
2409 else
2410 {
2411 if (!(usage & D3DUSAGE_DYNAMIC))
2412 {
2413 return D3DPOOL_MANAGED;
2414 }
2415 }
2416
2417 return D3DPOOL_DEFAULT;
2418}
2419
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002420bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002421 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002422{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002423 RECT rect;
2424 rect.left = sourceRect.x;
2425 rect.top = sourceRect.y;
2426 rect.right = sourceRect.x + sourceRect.width;
2427 rect.bottom = sourceRect.y + sourceRect.height;
2428
2429 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002430}
2431
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002432bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002433 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002434{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002435 RECT rect;
2436 rect.left = sourceRect.x;
2437 rect.top = sourceRect.y;
2438 rect.right = sourceRect.x + sourceRect.width;
2439 rect.bottom = sourceRect.y + sourceRect.height;
2440
2441 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002442}
2443
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002444bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2445 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2446{
2447 // 3D textures are not available in the D3D9 backend.
2448 UNREACHABLE();
2449 return false;
2450}
2451
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002452bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2453 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2454{
2455 // 2D array textures are not available in the D3D9 backend.
2456 UNREACHABLE();
2457 return false;
2458}
2459
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002460bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002461 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002462{
Geoff Lang758d5b22013-06-11 11:42:50 -04002463 ASSERT(filter == GL_NEAREST);
2464
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002465 endScene();
2466
2467 if (blitRenderTarget)
2468 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002469 gl::FramebufferAttachment *readBuffer = readFramebuffer->getColorbuffer(0);
2470 gl::FramebufferAttachment *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002471 RenderTarget9 *readRenderTarget = NULL;
2472 RenderTarget9 *drawRenderTarget = NULL;
2473 IDirect3DSurface9* readSurface = NULL;
2474 IDirect3DSurface9* drawSurface = NULL;
2475
2476 if (readBuffer)
2477 {
2478 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2479 }
2480 if (drawBuffer)
2481 {
2482 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2483 }
2484
2485 if (readRenderTarget)
2486 {
2487 readSurface = readRenderTarget->getSurface();
2488 }
2489 if (drawRenderTarget)
2490 {
2491 drawSurface = drawRenderTarget->getSurface();
2492 }
2493
2494 if (!readSurface || !drawSurface)
2495 {
2496 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002497 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002498 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002499
Geoff Lang125deab2013-08-09 13:34:16 -04002500 gl::Extents srcSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
2501 gl::Extents dstSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
2502
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002503 RECT srcRect;
2504 srcRect.left = readRect.x;
2505 srcRect.right = readRect.x + readRect.width;
2506 srcRect.top = readRect.y;
2507 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002508
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002509 RECT dstRect;
2510 dstRect.left = drawRect.x;
2511 dstRect.right = drawRect.x + drawRect.width;
2512 dstRect.top = drawRect.y;
2513 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002514
Geoff Lang125deab2013-08-09 13:34:16 -04002515 // Clip the rectangles to the scissor rectangle
2516 if (scissor)
2517 {
2518 if (dstRect.left < scissor->x)
2519 {
2520 srcRect.left += (scissor->x - dstRect.left);
2521 dstRect.left = scissor->x;
2522 }
2523 if (dstRect.top < scissor->y)
2524 {
2525 srcRect.top += (scissor->y - dstRect.top);
2526 dstRect.top = scissor->y;
2527 }
2528 if (dstRect.right > scissor->x + scissor->width)
2529 {
2530 srcRect.right -= (dstRect.right - (scissor->x + scissor->width));
2531 dstRect.right = scissor->x + scissor->width;
2532 }
2533 if (dstRect.bottom > scissor->y + scissor->height)
2534 {
2535 srcRect.bottom -= (dstRect.bottom - (scissor->y + scissor->height));
2536 dstRect.bottom = scissor->y + scissor->height;
2537 }
2538 }
2539
2540 // Clip the rectangles to the destination size
2541 if (dstRect.left < 0)
2542 {
2543 srcRect.left += -dstRect.left;
2544 dstRect.left = 0;
2545 }
2546 if (dstRect.right > dstSize.width)
2547 {
2548 srcRect.right -= (dstRect.right - dstSize.width);
2549 dstRect.right = dstSize.width;
2550 }
2551 if (dstRect.top < 0)
2552 {
2553 srcRect.top += -dstRect.top;
2554 dstRect.top = 0;
2555 }
2556 if (dstRect.bottom > dstSize.height)
2557 {
2558 srcRect.bottom -= (dstRect.bottom - dstSize.height);
2559 dstRect.bottom = dstSize.height;
2560 }
2561
2562 // Clip the rectangles to the source size
2563 if (srcRect.left < 0)
2564 {
2565 dstRect.left += -srcRect.left;
2566 srcRect.left = 0;
2567 }
2568 if (srcRect.right > srcSize.width)
2569 {
2570 dstRect.right -= (srcRect.right - srcSize.width);
2571 srcRect.right = srcSize.width;
2572 }
2573 if (srcRect.top < 0)
2574 {
2575 dstRect.top += -srcRect.top;
2576 srcRect.top = 0;
2577 }
2578 if (srcRect.bottom > srcSize.height)
2579 {
2580 dstRect.bottom -= (srcRect.bottom - srcSize.height);
2581 srcRect.bottom = srcSize.height;
2582 }
2583
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002584 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002585
Geoff Langea228632013-07-30 15:17:12 -04002586 SafeRelease(readSurface);
2587 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002588
2589 if (FAILED(result))
2590 {
2591 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2592 return false;
2593 }
2594 }
2595
Geoff Lang685806d2013-06-12 11:16:36 -04002596 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002597 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002598 gl::FramebufferAttachment *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2599 gl::FramebufferAttachment *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002600 RenderTarget9 *readDepthStencil = NULL;
2601 RenderTarget9 *drawDepthStencil = NULL;
2602 IDirect3DSurface9* readSurface = NULL;
2603 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002604
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002605 if (readBuffer)
2606 {
2607 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2608 }
2609 if (drawBuffer)
2610 {
2611 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2612 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002613
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002614 if (readDepthStencil)
2615 {
2616 readSurface = readDepthStencil->getSurface();
2617 }
2618 if (drawDepthStencil)
2619 {
2620 drawSurface = drawDepthStencil->getSurface();
2621 }
2622
2623 if (!readSurface || !drawSurface)
2624 {
2625 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002626 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002627 }
2628
2629 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2630
Geoff Langea228632013-07-30 15:17:12 -04002631 SafeRelease(readSurface);
2632 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002633
2634 if (FAILED(result))
2635 {
2636 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2637 return false;
2638 }
2639 }
2640
2641 return true;
2642}
2643
Jamie Madilleb9baab2014-03-24 13:19:43 -04002644void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2645 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002646{
Jamie Madilleb9baab2014-03-24 13:19:43 -04002647 ASSERT(pack.pixelBuffer.get() == NULL);
2648
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002649 RenderTarget9 *renderTarget = NULL;
2650 IDirect3DSurface9 *surface = NULL;
Jamie Madill3c7fa222014-06-05 13:08:51 -04002651 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002652
2653 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002654 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002655 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2656 }
Jamie Madill13a2f852013-12-11 16:35:08 -05002657
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002658 if (renderTarget)
2659 {
2660 surface = renderTarget->getSurface();
2661 }
2662
2663 if (!surface)
2664 {
2665 // context must be lost
2666 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002667 }
2668
2669 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002670 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002671
2672 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2673 {
2674 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
Geoff Langea228632013-07-30 15:17:12 -04002675 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002676 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002677 }
2678
2679 HRESULT result;
2680 IDirect3DSurface9 *systemSurface = NULL;
Jamie Madilleb9baab2014-03-24 13:19:43 -04002681 bool directToPixels = !pack.reverseRowOrder && pack.alignment <= 4 && getShareHandleSupport() &&
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002682 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2683 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2684 if (directToPixels)
2685 {
2686 // Use the pixels ptr as a shared handle to write directly into client's memory
2687 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2688 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2689 if (FAILED(result))
2690 {
2691 // Try again without the shared handle
2692 directToPixels = false;
2693 }
2694 }
2695
2696 if (!directToPixels)
2697 {
2698 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2699 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2700 if (FAILED(result))
2701 {
2702 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
Geoff Langea228632013-07-30 15:17:12 -04002703 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002704 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002705 }
2706 }
2707
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002708 result = mDevice->GetRenderTargetData(surface, systemSurface);
Geoff Langea228632013-07-30 15:17:12 -04002709 SafeRelease(surface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002710
2711 if (FAILED(result))
2712 {
Geoff Langea228632013-07-30 15:17:12 -04002713 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002714
2715 // It turns out that D3D will sometimes produce more error
2716 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002717 if (d3d9::isDeviceLostError(result))
2718 {
2719 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002720 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002721 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002722 else
2723 {
2724 UNREACHABLE();
2725 return;
2726 }
2727
2728 }
2729
2730 if (directToPixels)
2731 {
Geoff Langea228632013-07-30 15:17:12 -04002732 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002733 return;
2734 }
2735
2736 RECT rect;
2737 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2738 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2739 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2740 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2741
2742 D3DLOCKED_RECT lock;
2743 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2744
2745 if (FAILED(result))
2746 {
2747 UNREACHABLE();
Geoff Langea228632013-07-30 15:17:12 -04002748 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002749
2750 return; // No sensible error to generate
2751 }
2752
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002753 unsigned char *source;
2754 int inputPitch;
Jamie Madilleb9baab2014-03-24 13:19:43 -04002755 if (pack.reverseRowOrder)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002756 {
2757 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2758 inputPitch = -lock.Pitch;
2759 }
2760 else
2761 {
2762 source = (unsigned char*)lock.pBits;
2763 inputPitch = lock.Pitch;
2764 }
2765
Geoff Lang5d601382014-07-22 15:14:06 -04002766 const gl::InternalFormat &sourceFormatInfo = gl::GetInternalFormatInfo(d3d9_gl::GetInternalFormat(desc.Format));
2767 if (sourceFormatInfo.format == format && sourceFormatInfo.type == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002768 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002769 // Direct copy possible
2770 unsigned char *dest = static_cast<unsigned char*>(pixels);
2771 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002772 {
Geoff Lang5d601382014-07-22 15:14:06 -04002773 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourceFormatInfo.pixelBytes);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002774 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002775 }
2776 else
2777 {
Geoff Lang5d601382014-07-22 15:14:06 -04002778 const gl::FormatType &destFormatTypeInfo = gl::GetFormatTypeInfo(format, type);
2779 const gl::InternalFormat &destFormatInfo = gl::GetInternalFormatInfo(destFormatTypeInfo.internalFormat);
Geoff Lange4a492b2014-06-19 14:14:41 -04002780 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04002781 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002782 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002783 // Fast copy is possible through some special function
2784 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002785 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002786 for (int x = 0; x < rect.right - rect.left; x++)
2787 {
Geoff Lang5d601382014-07-22 15:14:06 -04002788 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destFormatInfo.pixelBytes;
2789 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourceFormatInfo.pixelBytes;
Geoff Lang697ad3e2013-06-04 10:11:28 -04002790
2791 fastCopyFunc(src, dest);
2792 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002793 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002794 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002795 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002796 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002797 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002798
Geoff Lang697ad3e2013-06-04 10:11:28 -04002799 gl::ColorF temp;
Geoff Lang697ad3e2013-06-04 10:11:28 -04002800 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002801 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002802 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002803 {
Geoff Lang5d601382014-07-22 15:14:06 -04002804 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destFormatInfo.pixelBytes;
2805 void *src = source + y * inputPitch + x * sourceFormatInfo.pixelBytes;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002806
Geoff Lang697ad3e2013-06-04 10:11:28 -04002807 // readFunc and writeFunc will be using the same type of color, CopyTexImage
2808 // will not allow the copy otherwise.
2809 readFunc(src, &temp);
Geoff Lang5d601382014-07-22 15:14:06 -04002810 destFormatTypeInfo.colorWriteFunction(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002811 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002812 }
2813 }
2814 }
2815
2816 systemSurface->UnlockRect();
Geoff Langea228632013-07-30 15:17:12 -04002817 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002818}
2819
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002820RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2821{
2822 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2823 IDirect3DSurface9 *surface = NULL;
2824 if (depth)
2825 {
2826 surface = swapChain9->getDepthStencil();
2827 }
2828 else
2829 {
2830 surface = swapChain9->getRenderTarget();
2831 }
2832
2833 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2834
2835 return renderTarget;
2836}
2837
Geoff Langa2d97f12013-06-11 11:44:02 -04002838RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002839{
2840 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2841 return renderTarget;
2842}
2843
Geoff Lang48dcae72014-02-05 16:28:24 -05002844ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2845 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2846 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002847{
Geoff Lang48dcae72014-02-05 16:28:24 -05002848 // Transform feedback is not supported in ES2 or D3D9
2849 ASSERT(transformFeedbackVaryings.size() == 0);
2850
daniel@transgaming.com55318902012-11-28 20:58:58 +00002851 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002852
2853 switch (type)
2854 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002855 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00002856 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002857 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002858 if (vshader)
2859 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002860 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002861 }
2862 }
2863 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002864 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00002865 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002866 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002867 if (pshader)
2868 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002869 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002870 }
2871 }
2872 break;
2873 default:
2874 UNREACHABLE();
2875 break;
2876 }
2877
2878 return executable;
2879}
2880
Geoff Lang48dcae72014-02-05 16:28:24 -05002881ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2882 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2883 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002884{
Geoff Lang48dcae72014-02-05 16:28:24 -05002885 // Transform feedback is not supported in ES2 or D3D9
2886 ASSERT(transformFeedbackVaryings.size() == 0);
2887
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002888 const char *profile = NULL;
2889
2890 switch (type)
2891 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002892 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002893 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
2894 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002895 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002896 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
2897 break;
2898 default:
2899 UNREACHABLE();
2900 return NULL;
2901 }
2902
Nicolas Capens93faad92014-05-10 12:14:13 -04002903 UINT flags = ANGLE_COMPILE_OPTIMIZATION_LEVEL;
Nicolas Capens655fe362014-04-11 13:12:34 -04002904
2905 if (workaround == ANGLE_D3D_WORKAROUND_SKIP_OPTIMIZATION)
2906 {
Nicolas Capens93faad92014-05-10 12:14:13 -04002907 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
Nicolas Capens655fe362014-04-11 13:12:34 -04002908 }
2909 else if (workaround == ANGLE_D3D_WORKAROUND_MAX_OPTIMIZATION)
2910 {
Nicolas Capens93faad92014-05-10 12:14:13 -04002911 flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
Nicolas Capens655fe362014-04-11 13:12:34 -04002912 }
2913 else ASSERT(workaround == ANGLE_D3D_WORKAROUND_NONE);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002914
Nicolas Capens93faad92014-05-10 12:14:13 -04002915 if (gl::perfActive())
2916 {
2917#ifndef NDEBUG
2918 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2919#endif
2920
2921 flags |= D3DCOMPILE_DEBUG;
2922
2923 std::string sourcePath = getTempPath();
2924 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2925 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2926 }
2927
2928 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2929 // Try the default flags first and if compilation fails, try some alternatives.
2930 const UINT extraFlags[] =
2931 {
2932 flags,
2933 flags | D3DCOMPILE_AVOID_FLOW_CONTROL,
2934 flags | D3DCOMPILE_PREFER_FLOW_CONTROL
2935 };
2936
2937 const static char *extraFlagNames[] =
2938 {
2939 "default",
2940 "avoid flow control",
2941 "prefer flow control"
2942 };
2943
2944 int attempts = ArraySize(extraFlags);
2945
2946 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002947 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002948 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002949 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002950 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002951
Geoff Lang48dcae72014-02-05 16:28:24 -05002952 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type,
2953 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002954 SafeRelease(binary);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002955
2956 return executable;
2957}
2958
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002959rx::UniformStorage *Renderer9::createUniformStorage(size_t storageSize)
2960{
2961 return new UniformStorage(storageSize);
2962}
2963
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002964bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
2965{
2966 return mBlit->boxFilter(source, dest);
2967}
2968
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002969D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002970{
2971 if (mD3d9Ex != NULL)
2972 {
2973 return D3DPOOL_DEFAULT;
2974 }
2975 else
2976 {
2977 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2978 {
2979 return D3DPOOL_MANAGED;
2980 }
2981 }
2982
2983 return D3DPOOL_DEFAULT;
2984}
2985
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002986bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
2987{
2988 if (source && dest)
2989 {
2990 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002991
2992 if (fromManaged)
2993 {
2994 D3DSURFACE_DESC desc;
2995 source->GetDesc(&desc);
2996
2997 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00002998 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002999
3000 if (SUCCEEDED(result))
3001 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003002 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003003 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
Geoff Langea228632013-07-30 15:17:12 -04003004 SafeRelease(surf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003005 }
3006 }
3007 else
3008 {
3009 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003010 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003011 }
3012
3013 if (FAILED(result))
3014 {
3015 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3016 return false;
3017 }
3018 }
3019
3020 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003021}
3022
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003023Image *Renderer9::createImage()
3024{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003025 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003026}
3027
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003028void Renderer9::generateMipmap(Image *dest, Image *src)
3029{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003030 Image9 *src9 = Image9::makeImage9(src);
3031 Image9 *dst9 = Image9::makeImage9(dest);
3032 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003033}
3034
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003035TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3036{
3037 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3038 return new TextureStorage9_2D(this, swapChain9);
3039}
3040
Nicolas Capensbf712d02014-03-31 14:23:35 -04003041TextureStorage *Renderer9::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003042{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003043 return new TextureStorage9_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003044}
3045
Nicolas Capensbf712d02014-03-31 14:23:35 -04003046TextureStorage *Renderer9::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003047{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003048 return new TextureStorage9_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003049}
3050
Nicolas Capensbf712d02014-03-31 14:23:35 -04003051TextureStorage *Renderer9::createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels)
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003052{
3053 // 3D textures are not supported by the D3D9 backend.
3054 UNREACHABLE();
3055
3056 return NULL;
3057}
3058
Nicolas Capensbf712d02014-03-31 14:23:35 -04003059TextureStorage *Renderer9::createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels)
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003060{
3061 // 2D array textures are not supported by the D3D9 backend.
3062 UNREACHABLE();
3063
3064 return NULL;
3065}
3066
Brandon Jonesf47bebc2014-07-09 14:28:42 -07003067Texture2DImpl *Renderer9::createTexture2D()
3068{
3069 return new TextureD3D_2D(this);
3070}
3071
Brandon Jones0511e802014-07-14 16:27:26 -07003072TextureCubeImpl *Renderer9::createTextureCube()
3073{
3074 return new TextureD3D_Cube(this);
3075}
3076
Brandon Jones78b1acd2014-07-15 15:33:07 -07003077Texture3DImpl *Renderer9::createTexture3D()
3078{
3079 return new TextureD3D_3D(this);
3080}
3081
Brandon Jones142ec422014-07-16 10:31:30 -07003082Texture2DArrayImpl *Renderer9::createTexture2DArray()
3083{
3084 return new TextureD3D_2DArray(this);
3085}
3086
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003087bool Renderer9::getLUID(LUID *adapterLuid) const
3088{
3089 adapterLuid->HighPart = 0;
3090 adapterLuid->LowPart = 0;
3091
3092 if (mD3d9Ex)
3093 {
3094 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3095 return true;
3096 }
3097
3098 return false;
3099}
3100
Geoff Lang005df412013-10-16 14:12:50 -04003101GLenum Renderer9::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003102{
Geoff Langcec35902014-04-16 10:52:36 -04003103 return d3d9_gl::GetInternalFormat(gl_d3d9::GetTextureFormat(internalFormat));
Jamie Madillc8c102b2013-10-10 15:10:24 -04003104}
3105
Jamie Madill95ffb862014-01-29 09:26:59 -05003106rx::VertexConversionType Renderer9::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3107{
3108 return d3d9::GetVertexConversionType(vertexFormat);
3109}
3110
3111GLenum Renderer9::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3112{
3113 D3DDECLTYPE declType = d3d9::GetNativeVertexFormat(vertexFormat);
3114 return d3d9::GetDeclTypeComponentType(declType);
3115}
3116
Geoff Langc0b9ef42014-07-02 10:02:37 -04003117void Renderer9::generateCaps(gl::Caps *outCaps, gl::TextureCapsMap *outTextureCaps, gl::Extensions *outExtensions) const
Geoff Langcec35902014-04-16 10:52:36 -04003118{
Geoff Langc0b9ef42014-07-02 10:02:37 -04003119 d3d9_gl::GenerateCaps(mD3d9, mDevice, mDeviceType, mAdapter, outCaps, outTextureCaps, outExtensions);
Geoff Langcec35902014-04-16 10:52:36 -04003120}
3121
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003122}