blob: 05916d8158cabe5e297cfcd4674fabf227a44f46 [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"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000016#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000017#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000018#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040019#include "libGLESv2/renderer/d3d9/Renderer9.h"
20#include "libGLESv2/renderer/d3d9/renderer9_utils.h"
21#include "libGLESv2/renderer/d3d9/formatutils9.h"
22#include "libGLESv2/renderer/d3d9/ShaderExecutable9.h"
23#include "libGLESv2/renderer/d3d9/SwapChain9.h"
24#include "libGLESv2/renderer/d3d9/TextureStorage9.h"
25#include "libGLESv2/renderer/d3d9/Image9.h"
26#include "libGLESv2/renderer/d3d9/Blit9.h"
27#include "libGLESv2/renderer/d3d9/RenderTarget9.h"
28#include "libGLESv2/renderer/d3d9/VertexBuffer9.h"
29#include "libGLESv2/renderer/d3d9/IndexBuffer9.h"
30#include "libGLESv2/renderer/d3d9/BufferStorage9.h"
31#include "libGLESv2/renderer/d3d9/Query9.h"
32#include "libGLESv2/renderer/d3d9/Fence9.h"
Brandon Jones5bf98292014-06-06 17:19:38 -070033#include "libGLESv2/renderer/d3d9/VertexArray9.h"
Jamie Madillc5a83002014-02-14 16:41:25 -050034#include "libGLESv2/angletypes.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000035
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000036#include "libEGL/Display.h"
37
Al Patrick3c6344e2013-09-23 14:57:00 -040038#include "third_party/trace_event/trace_event.h"
39
daniel@transgaming.com621ce052012-10-31 17:52:29 +000040// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
41#define REF_RAST 0
42
43// The "Debug This Pixel..." feature in PIX often fails when using the
44// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
45// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
46#if !defined(ANGLE_ENABLE_D3D9EX)
47// Enables use of the IDirect3D9Ex interface, when available
48#define ANGLE_ENABLE_D3D9EX 1
49#endif // !defined(ANGLE_ENABLE_D3D9EX)
50
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000051#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
52#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
53#endif
54
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000055const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z')));
56const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')));
57
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000058namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000059{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000060static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000061 {
62 D3DFMT_A1R5G5B5,
63 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
64 D3DFMT_A8R8G8B8,
65 D3DFMT_R5G6B5,
66 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
67 D3DFMT_X8R8G8B8
68 };
69
daniel@transgaming.com222ee082012-11-28 19:31:49 +000070static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000071 {
72 D3DFMT_UNKNOWN,
73 // D3DFMT_D16_LOCKABLE,
74 D3DFMT_D32,
75 // D3DFMT_D15S1,
76 D3DFMT_D24S8,
77 D3DFMT_D24X8,
78 // D3DFMT_D24X4S4,
79 D3DFMT_D16,
80 // D3DFMT_D32F_LOCKABLE,
81 // D3DFMT_D24FS8
82 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000083
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000084enum
85{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +000086 MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
87 MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32,
88 MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224,
89 MAX_VARYING_VECTORS_SM2 = 8,
90 MAX_VARYING_VECTORS_SM3 = 10,
91
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000092 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
93};
94
Geoff Lang64c83242014-05-06 10:49:24 -040095Renderer9::Renderer9(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000096{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000097 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000098
99 mD3d9 = NULL;
100 mD3d9Ex = NULL;
101 mDevice = NULL;
102 mDeviceEx = NULL;
103 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000104 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000105
106 mAdapter = D3DADAPTER_DEFAULT;
107
108 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
109 mDeviceType = D3DDEVTYPE_REF;
110 #else
111 mDeviceType = D3DDEVTYPE_HAL;
112 #endif
113
114 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000115
116 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000117
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +0000118 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000119
120 mVertexDataManager = NULL;
121 mIndexDataManager = NULL;
122 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000123
124 mMaxNullColorbufferLRU = 0;
125 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
126 {
127 mNullColorbufferCache[i].lruCount = 0;
128 mNullColorbufferCache[i].width = 0;
129 mNullColorbufferCache[i].height = 0;
130 mNullColorbufferCache[i].buffer = NULL;
131 }
Jamie Madill6246dc82014-01-29 09:26:47 -0500132
133 mAppliedVertexShader = NULL;
134 mAppliedPixelShader = NULL;
Jamie Madilla5c9a142014-05-26 16:39:33 -0400135 mAppliedProgramSerial = 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000136}
137
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000138Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000139{
140 if (mDevice)
141 {
142 // 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 +0000143 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000144 {
145 resetDevice();
146 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000147 }
148
Geoff Langcec35902014-04-16 10:52:36 -0400149 release();
Jamie Madillb7935e52013-11-20 15:47:47 -0500150}
151
Geoff Langcec35902014-04-16 10:52:36 -0400152void Renderer9::release()
Jamie Madillb7935e52013-11-20 15:47:47 -0500153{
154 releaseDeviceResources();
155
156 SafeRelease(mDevice);
Geoff Langea228632013-07-30 15:17:12 -0400157 SafeRelease(mDeviceEx);
158 SafeRelease(mD3d9);
Jamie Madillb7935e52013-11-20 15:47:47 -0500159 SafeRelease(mD3d9Ex);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000160
Geoff Langdad5ed32014-02-10 12:59:17 -0500161 mCompiler.release();
162
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000163 if (mDeviceWindow)
164 {
165 DestroyWindow(mDeviceWindow);
166 mDeviceWindow = NULL;
167 }
168
Geoff Langea228632013-07-30 15:17:12 -0400169 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000170}
171
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000172Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
173{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000174 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000175 return static_cast<rx::Renderer9*>(renderer);
176}
177
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000178EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000179{
Geoff Langdad5ed32014-02-10 12:59:17 -0500180 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000181 {
182 return EGL_NOT_INITIALIZED;
183 }
184
Geoff Lang64c83242014-05-06 10:49:24 -0400185 TRACE_EVENT0("gpu", "GetModuleHandle_d3d9");
186 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000187
188 if (mD3d9Module == NULL)
189 {
190 ERR("No D3D9 module found - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000194 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
195 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
196
197 // Use Direct3D9Ex if available. Among other things, this version is less
198 // inclined to report a lost context, for example when the user switches
199 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
200 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
201 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400202 TRACE_EVENT0("gpu", "D3d9Ex_QueryInterface");
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000203 ASSERT(mD3d9Ex);
Geoff Lang8e328842014-02-10 13:11:20 -0500204 mD3d9Ex->QueryInterface(__uuidof(IDirect3D9), reinterpret_cast<void**>(&mD3d9));
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000205 ASSERT(mD3d9);
206 }
207 else
208 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400209 TRACE_EVENT0("gpu", "Direct3DCreate9");
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000210 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
211 }
212
213 if (!mD3d9)
214 {
215 ERR("Could not create D3D9 device - aborting!\n");
216 return EGL_NOT_INITIALIZED;
217 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000218
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000219 if (mDc != NULL)
220 {
221 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
222 }
223
224 HRESULT result;
225
226 // Give up on getting device caps after about one second.
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000227 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400228 TRACE_EVENT0("gpu", "GetDeviceCaps");
229 for (int i = 0; i < 10; ++i)
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000230 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400231 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
232 if (SUCCEEDED(result))
233 {
234 break;
235 }
236 else if (result == D3DERR_NOTAVAILABLE)
237 {
238 Sleep(100); // Give the driver some time to initialize/recover
239 }
240 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
241 {
242 ERR("failed to get device caps (0x%x)\n", result);
243 return EGL_NOT_INITIALIZED;
244 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000245 }
246 }
247
248 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
249 {
250 ERR("Renderer does not support PS 2.0. aborting!\n");
251 return EGL_NOT_INITIALIZED;
252 }
253
254 // 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 -0400255 // This is required by Texture2D::ensureRenderTarget.
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000256 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
257 {
258 ERR("Renderer does not support stretctrect from textures!\n");
259 return EGL_NOT_INITIALIZED;
260 }
261
Al Patrick3c6344e2013-09-23 14:57:00 -0400262 {
263 TRACE_EVENT0("gpu", "GetAdapterIdentifier");
264 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
265 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000266
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000267 mMinSwapInterval = 4;
268 mMaxSwapInterval = 0;
269
270 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
271 {
272 mMinSwapInterval = std::min(mMinSwapInterval, 0);
273 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
274 }
275 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
276 {
277 mMinSwapInterval = std::min(mMinSwapInterval, 1);
278 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
279 }
280 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
281 {
282 mMinSwapInterval = std::min(mMinSwapInterval, 2);
283 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
284 }
285 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
286 {
287 mMinSwapInterval = std::min(mMinSwapInterval, 3);
288 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
289 }
290 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
291 {
292 mMinSwapInterval = std::min(mMinSwapInterval, 4);
293 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
294 }
295
Geoff Lang61e49a52013-05-29 10:22:58 -0400296 mMaxSupportedSamples = 0;
297
298 const d3d9::D3DFormatSet &d3d9Formats = d3d9::GetAllUsedD3DFormats();
299 for (d3d9::D3DFormatSet::const_iterator i = d3d9Formats.begin(); i != d3d9Formats.end(); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000300 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400301 TRACE_EVENT0("gpu", "getMultiSampleSupport");
Geoff Lang61e49a52013-05-29 10:22:58 -0400302 MultisampleSupportInfo support = getMultiSampleSupport(*i);
303 mMultiSampleSupport[*i] = support;
304 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
daniel@transgaming.com92955622012-10-31 18:38:41 +0000305 }
306
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000307 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
308 static const TCHAR className[] = TEXT("STATIC");
309
Al Patrick3c6344e2013-09-23 14:57:00 -0400310 {
311 TRACE_EVENT0("gpu", "CreateWindowEx");
312 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
313 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000314
315 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
316 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
317
Al Patrick3c6344e2013-09-23 14:57:00 -0400318 {
319 TRACE_EVENT0("gpu", "D3d9_CreateDevice");
320 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
321 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000322 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
323 {
324 return EGL_BAD_ALLOC;
325 }
326
327 if (FAILED(result))
328 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400329 TRACE_EVENT0("gpu", "D3d9_CreateDevice2");
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000330 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
331
332 if (FAILED(result))
333 {
334 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
335 return EGL_BAD_ALLOC;
336 }
337 }
338
339 if (mD3d9Ex)
340 {
Al Patrick3c6344e2013-09-23 14:57:00 -0400341 TRACE_EVENT0("gpu", "mDevice_QueryInterface");
Geoff Lang8e328842014-02-10 13:11:20 -0500342 result = mDevice->QueryInterface(__uuidof(IDirect3DDevice9Ex), (void**)&mDeviceEx);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000343 ASSERT(SUCCEEDED(result));
344 }
345
Al Patrick3c6344e2013-09-23 14:57:00 -0400346 {
347 TRACE_EVENT0("gpu", "ShaderCache initialize");
348 mVertexShaderCache.initialize(mDevice);
349 mPixelShaderCache.initialize(mDevice);
350 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000351
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000352 D3DDISPLAYMODE currentDisplayMode;
353 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
354
355 // Check vertex texture support
356 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
357 // We test this using D3D9 by checking support for the R16F format.
358 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
359 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
360 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
361
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000362 initializeDevice();
363
Jamie Madill80177872014-02-04 16:04:10 -0500364 d3d9::InitializeVertexTranslations(this);
365
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000366 return EGL_SUCCESS;
367}
368
369// do any one-time device initialization
370// NOTE: this is also needed after a device lost/reset
371// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000372void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000373{
374 // Permanent non-default states
375 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
376 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
377
378 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
379 {
380 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
381 }
382 else
383 {
384 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
385 }
386
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000387 markAllStateDirty();
388
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000389 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000390
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000391 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400392 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000393 mVertexDataManager = new rx::VertexDataManager(this);
394 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000395}
396
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000397D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000398{
399 D3DPRESENT_PARAMETERS presentParameters = {0};
400
401 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
402 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
403 presentParameters.BackBufferCount = 1;
404 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
405 presentParameters.BackBufferWidth = 1;
406 presentParameters.BackBufferHeight = 1;
407 presentParameters.EnableAutoDepthStencil = FALSE;
408 presentParameters.Flags = 0;
409 presentParameters.hDeviceWindow = mDeviceWindow;
410 presentParameters.MultiSampleQuality = 0;
411 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
412 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
413 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
414 presentParameters.Windowed = TRUE;
415
416 return presentParameters;
417}
418
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000419int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000420{
421 D3DDISPLAYMODE currentDisplayMode;
422 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
423
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000424 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
425 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000426 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
427 int numConfigs = 0;
428
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000429 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000430 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000431 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000432
433 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
434
435 if (SUCCEEDED(result))
436 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000437 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000438 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000439 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000440 HRESULT result = D3D_OK;
441
442 if(depthStencilFormat != D3DFMT_UNKNOWN)
443 {
444 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
445 }
446
447 if (SUCCEEDED(result))
448 {
449 if(depthStencilFormat != D3DFMT_UNKNOWN)
450 {
451 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
452 }
453
454 if (SUCCEEDED(result))
455 {
456 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000457 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
458 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000459 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
460 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000461 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000462
463 (*configDescList)[numConfigs++] = newConfig;
464 }
465 }
466 }
467 }
468 }
469
470 return numConfigs;
471}
472
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000473void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000474{
475 delete [] (configDescList);
476}
477
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000478void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000479{
480 if (!mSceneStarted)
481 {
482 long result = mDevice->BeginScene();
483 if (SUCCEEDED(result)) {
484 // This is defensive checking against the device being
485 // lost at unexpected times.
486 mSceneStarted = true;
487 }
488 }
489}
490
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000491void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000492{
493 if (mSceneStarted)
494 {
495 // EndScene can fail if the device was lost, for example due
496 // to a TDR during a draw call.
497 mDevice->EndScene();
498 mSceneStarted = false;
499 }
500}
501
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000502void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000503{
504 HRESULT result;
505
506 IDirect3DQuery9* query = allocateEventQuery();
507 if (!query)
508 {
509 return;
510 }
511
512 result = query->Issue(D3DISSUE_END);
513 ASSERT(SUCCEEDED(result));
514
515 do
516 {
517 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
518
519 if(block && result == S_FALSE)
520 {
521 // Keep polling, but allow other threads to do something useful first
522 Sleep(0);
523 // explicitly check for device loss
524 // some drivers seem to return S_FALSE even if the device is lost
525 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000526 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000527 {
528 result = D3DERR_DEVICELOST;
529 }
530 }
531 }
532 while(block && result == S_FALSE);
533
534 freeEventQuery(query);
535
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000536 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000537 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000538 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000539 }
540}
541
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000542SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
543{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000544 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000545}
546
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000547IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000548{
549 IDirect3DQuery9 *query = NULL;
550
551 if (mEventQueryPool.empty())
552 {
553 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
Geoff Lang9cd19152014-05-28 15:54:34 -0400554 UNUSED_ASSERTION_VARIABLE(result);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000555 ASSERT(SUCCEEDED(result));
556 }
557 else
558 {
559 query = mEventQueryPool.back();
560 mEventQueryPool.pop_back();
561 }
562
563 return query;
564}
565
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000566void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000567{
568 if (mEventQueryPool.size() > 1000)
569 {
Geoff Langea228632013-07-30 15:17:12 -0400570 SafeRelease(query);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000571 }
572 else
573 {
574 mEventQueryPool.push_back(query);
575 }
576}
577
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000578IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000579{
580 return mVertexShaderCache.create(function, length);
581}
582
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000583IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000584{
585 return mPixelShaderCache.create(function, length);
586}
587
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000588HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
589{
590 D3DPOOL Pool = getBufferPool(Usage);
591 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
592}
593
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000594VertexBuffer *Renderer9::createVertexBuffer()
595{
596 return new VertexBuffer9(this);
597}
598
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000599HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
600{
601 D3DPOOL Pool = getBufferPool(Usage);
602 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
603}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000604
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000605IndexBuffer *Renderer9::createIndexBuffer()
606{
607 return new IndexBuffer9(this);
608}
609
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000610BufferStorage *Renderer9::createBufferStorage()
611{
612 return new BufferStorage9();
613}
614
Brandon Jones5bf98292014-06-06 17:19:38 -0700615VertexArrayImpl *Renderer9::createVertexArray()
616{
617 return new VertexArray9(this);
618}
619
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000620QueryImpl *Renderer9::createQuery(GLenum type)
621{
622 return new Query9(this, type);
623}
624
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000625FenceImpl *Renderer9::createFence()
626{
627 return new Fence9(this);
628}
629
Geoff Lang005df412013-10-16 14:12:50 -0400630bool Renderer9::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -0400631{
632 // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
633 return false;
634}
635
Jamie Madilla21eea32013-09-18 14:36:25 -0400636bool Renderer9::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
637 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
638{
639 // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
640 UNREACHABLE();
641 return false;
642}
643
Geoff Lange2e0ce02013-09-17 17:05:08 -0400644void Renderer9::generateSwizzle(gl::Texture *texture)
645{
646 // Swizzled textures are not available in ES2 or D3D9
647 UNREACHABLE();
648}
649
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000650void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000651{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000652 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
653 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000654
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000655 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000656 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000657 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
658 int d3dSampler = index + d3dSamplerOffset;
659
660 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
661 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
662
663 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
664 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
665 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
666 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
667 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
Nicolas Capens8de68282014-04-04 11:10:27 -0400668 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.baseLevel);
Geoff Langcec35902014-04-16 10:52:36 -0400669 if (getCaps().extensions.textureFilterAnisotropic)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000670 {
671 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
672 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000673 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000674
675 forceSetSamplers[index] = false;
676 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000677}
678
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000679void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000680{
681 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
682 int d3dSampler = index + d3dSamplerOffset;
683 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000684 unsigned int serial = 0;
685 bool forceSetTexture = false;
686
687 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000688
689 if (texture)
690 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000691 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000692 if (texStorage)
693 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000694 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000695 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000696 }
697 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000698 // in the texture class and we're unexpectedly missing the d3d texture
699 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000700
701 serial = texture->getTextureSerial();
702 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000703 }
704
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000705 if (forceSetTexture || appliedSerials[index] != serial)
706 {
707 mDevice->SetTexture(d3dSampler, d3dTexture);
708 }
709
710 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000711}
712
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000713bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
714{
715 // No effect in ES2/D3D9
716 return true;
717}
718
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000719void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000720{
721 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000722
723 if (rasterStateChanged)
724 {
725 // Set the cull mode
726 if (rasterState.cullFace)
727 {
728 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
729 }
730 else
731 {
732 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
733 }
734
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000735 if (rasterState.polygonOffsetFill)
736 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000737 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000738 {
739 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
740
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000741 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000742 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
743 }
744 }
745 else
746 {
747 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
748 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
749 }
750
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000751 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000752 }
753
754 mForceSetRasterState = false;
755}
756
Geoff Langc142e9d2013-09-30 15:19:47 -0400757void Renderer9::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
758 unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000759{
760 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400761 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000762 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
763
764 if (blendStateChanged || blendColorChanged)
765 {
766 if (blendState.blend)
767 {
768 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
769
770 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
771 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
772 {
773 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
774 }
775 else
776 {
777 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
778 gl::unorm<8>(blendColor.alpha),
779 gl::unorm<8>(blendColor.alpha),
780 gl::unorm<8>(blendColor.alpha)));
781 }
782
783 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
784 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
785 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
786
787 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
788 blendState.destBlendRGB != blendState.destBlendAlpha ||
789 blendState.blendEquationRGB != blendState.blendEquationAlpha)
790 {
791 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
792
793 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
794 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
795 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
796 }
797 else
798 {
799 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
800 }
801 }
802 else
803 {
804 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
805 }
806
807 if (blendState.sampleAlphaToCoverage)
808 {
809 FIXME("Sample alpha to coverage is unimplemented.");
810 }
811
Jamie Madill3c7fa222014-06-05 13:08:51 -0400812 gl::FramebufferAttachment *attachment = framebuffer->getFirstColorbuffer();
813 GLenum internalFormat = attachment ? attachment->getInternalFormat() : GL_NONE;
Geoff Langc142e9d2013-09-30 15:19:47 -0400814 GLuint clientVersion = getCurrentClientVersion();
815
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000816 // Set the color mask
817 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
818 // Apparently some ATI cards have a bug where a draw with a zero color
819 // write mask can cause later draws to have incorrect results. Instead,
820 // set a nonzero color write mask but modify the blend state so that no
821 // drawing is done.
822 // http://code.google.com/p/angleproject/issues/detail?id=169
823
Geoff Langc142e9d2013-09-30 15:19:47 -0400824 DWORD colorMask = gl_d3d9::ConvertColorMask(gl::GetRedBits(internalFormat, clientVersion) > 0 && blendState.colorMaskRed,
825 gl::GetGreenBits(internalFormat, clientVersion) > 0 && blendState.colorMaskGreen,
826 gl::GetBlueBits(internalFormat, clientVersion) > 0 && blendState.colorMaskBlue,
827 gl::GetAlphaBits(internalFormat, clientVersion) > 0 && blendState.colorMaskAlpha);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000828 if (colorMask == 0 && !zeroColorMaskAllowed)
829 {
830 // Enable green channel, but set blending so nothing will be drawn.
831 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
832 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
833
834 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
835 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
836 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
837 }
838 else
839 {
840 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
841 }
842
843 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
844
845 mCurBlendState = blendState;
846 mCurBlendColor = blendColor;
847 }
848
849 if (sampleMaskChanged)
850 {
851 // Set the multisample mask
852 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
853 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
854
855 mCurSampleMask = sampleMask;
856 }
857
858 mForceSetBlendState = false;
859}
860
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000861void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000862 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000863{
864 bool depthStencilStateChanged = mForceSetDepthStencilState ||
865 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000866 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
867 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000868 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000869
870 if (depthStencilStateChanged)
871 {
872 if (depthStencilState.depthTest)
873 {
874 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
875 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
876 }
877 else
878 {
879 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
880 }
881
882 mCurDepthStencilState = depthStencilState;
883 }
884
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000885 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000886 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000887 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000888 {
889 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
890 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
891
892 // FIXME: Unsupported by D3D9
893 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
894 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
895 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
896 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000897 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000898 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
899 {
900 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000901 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000902 }
903
904 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000905 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000906
907 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
908 depthStencilState.stencilWritemask);
909 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
910 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
911
912 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000913 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000914 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
915 depthStencilState.stencilMask);
916
917 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
918 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
919 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
920 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
921 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
922 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
923
924 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
925 depthStencilState.stencilBackWritemask);
926 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
927 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
928
929 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000930 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000931 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
932 depthStencilState.stencilBackMask);
933
934 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
935 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
936 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
937 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
938 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
939 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
940 }
941 else
942 {
943 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
944 }
945
946 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
947
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000948 mCurStencilRef = stencilRef;
949 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000950 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000951 }
952
953 mForceSetDepthStencilState = false;
954}
955
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000956void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000957{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000958 bool scissorChanged = mForceSetScissor ||
959 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
960 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000961
daniel@transgaming.com04f1b332012-11-28 21:00:40 +0000962 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000963 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000964 if (enabled)
965 {
966 RECT rect;
967 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
968 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
969 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
970 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
971 mDevice->SetScissorRect(&rect);
972 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000973
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000974 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
975
976 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000977 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000978 }
979
980 mForceSetScissor = false;
981}
982
daniel@transgaming.com12985182012-12-20 20:56:31 +0000983bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000984 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000985{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000986 gl::Rectangle actualViewport = viewport;
987 float actualZNear = gl::clamp01(zNear);
988 float actualZFar = gl::clamp01(zFar);
989 if (ignoreViewport)
990 {
991 actualViewport.x = 0;
992 actualViewport.y = 0;
993 actualViewport.width = mRenderTargetDesc.width;
994 actualViewport.height = mRenderTargetDesc.height;
995 actualZNear = 0.0f;
996 actualZFar = 1.0f;
997 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000998
999 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001000 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1001 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1002 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1003 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1004 dxViewport.MinZ = actualZNear;
1005 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001006
1007 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1008 {
1009 return false; // Nothing to render
1010 }
1011
Geoff Langde14d602013-08-14 12:28:33 -04001012 float depthFront = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
1013
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001014 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
Geoff Langde14d602013-08-14 12:28:33 -04001015 actualZNear != mCurNear || actualZFar != mCurFar || mCurDepthFront != depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001016 if (viewportChanged)
1017 {
1018 mDevice->SetViewport(&dxViewport);
1019
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001020 mCurViewport = actualViewport;
1021 mCurNear = actualZNear;
1022 mCurFar = actualZFar;
Geoff Langde14d602013-08-14 12:28:33 -04001023 mCurDepthFront = depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001024
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001025 dx_VertexConstants vc = {0};
1026 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001027
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001028 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1029 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1030 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1031 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001032
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001033 pc.viewCoords[0] = actualViewport.width * 0.5f;
1034 pc.viewCoords[1] = actualViewport.height * 0.5f;
1035 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1036 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001037
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001038 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1039 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
Geoff Langde14d602013-08-14 12:28:33 -04001040 pc.depthFront[2] = depthFront;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001041
1042 vc.depthRange[0] = actualZNear;
1043 vc.depthRange[1] = actualZFar;
1044 vc.depthRange[2] = actualZFar - actualZNear;
1045
1046 pc.depthRange[0] = actualZNear;
1047 pc.depthRange[1] = actualZFar;
1048 pc.depthRange[2] = actualZFar - actualZNear;
1049
1050 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1051 {
1052 mVertexConstants = vc;
1053 mDxUniformsDirty = true;
1054 }
1055
1056 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1057 {
1058 mPixelConstants = pc;
1059 mDxUniformsDirty = true;
1060 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001061 }
1062
1063 mForceSetViewport = false;
1064 return true;
1065}
1066
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001067bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1068{
1069 switch (mode)
1070 {
1071 case GL_POINTS:
1072 mPrimitiveType = D3DPT_POINTLIST;
1073 mPrimitiveCount = count;
1074 break;
1075 case GL_LINES:
1076 mPrimitiveType = D3DPT_LINELIST;
1077 mPrimitiveCount = count / 2;
1078 break;
1079 case GL_LINE_LOOP:
1080 mPrimitiveType = D3DPT_LINESTRIP;
1081 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1082 break;
1083 case GL_LINE_STRIP:
1084 mPrimitiveType = D3DPT_LINESTRIP;
1085 mPrimitiveCount = count - 1;
1086 break;
1087 case GL_TRIANGLES:
1088 mPrimitiveType = D3DPT_TRIANGLELIST;
1089 mPrimitiveCount = count / 3;
1090 break;
1091 case GL_TRIANGLE_STRIP:
1092 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1093 mPrimitiveCount = count - 2;
1094 break;
1095 case GL_TRIANGLE_FAN:
1096 mPrimitiveType = D3DPT_TRIANGLEFAN;
1097 mPrimitiveCount = count - 2;
1098 break;
1099 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001100 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001101 }
1102
1103 return mPrimitiveCount > 0;
1104}
1105
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001106
Jamie Madill3c7fa222014-06-05 13:08:51 -04001107gl::FramebufferAttachment *Renderer9::getNullColorbuffer(gl::FramebufferAttachment *depthbuffer)
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001108{
1109 if (!depthbuffer)
1110 {
1111 ERR("Unexpected null depthbuffer for depth-only FBO.");
1112 return NULL;
1113 }
1114
1115 GLsizei width = depthbuffer->getWidth();
1116 GLsizei height = depthbuffer->getHeight();
1117
1118 // search cached nullcolorbuffers
1119 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1120 {
1121 if (mNullColorbufferCache[i].buffer != NULL &&
1122 mNullColorbufferCache[i].width == width &&
1123 mNullColorbufferCache[i].height == height)
1124 {
1125 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1126 return mNullColorbufferCache[i].buffer;
1127 }
1128 }
1129
Jamie Madill6c7b4ad2014-06-16 10:33:59 -04001130 gl::Renderbuffer *nullRenderbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
Jamie Madill1e3fa742014-06-16 10:34:00 -04001131 gl::FramebufferAttachment *nullbuffer = new gl::FramebufferAttachment(0, new gl::RenderbufferAttachment(nullRenderbuffer));
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001132
1133 // add nullbuffer to the cache
1134 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1135 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1136 {
1137 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1138 {
1139 oldest = &mNullColorbufferCache[i];
1140 }
1141 }
1142
1143 delete oldest->buffer;
1144 oldest->buffer = nullbuffer;
1145 oldest->lruCount = ++mMaxNullColorbufferLRU;
1146 oldest->width = width;
1147 oldest->height = height;
1148
1149 return nullbuffer;
1150}
1151
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001152bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001153{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001154 // if there is no color attachment we must synthesize a NULL colorattachment
1155 // to keep the D3D runtime happy. This should only be possible if depth texturing.
Jamie Madill3c7fa222014-06-05 13:08:51 -04001156 gl::FramebufferAttachment *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001157 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001158 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001159 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001160 }
1161 else
1162 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001163 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001164 }
1165 if (!renderbufferObject)
1166 {
1167 ERR("unable to locate renderbuffer for FBO.");
1168 return false;
1169 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001170
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001171 bool renderTargetChanged = false;
1172 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1173 if (renderTargetSerial != mAppliedRenderTargetSerial)
1174 {
1175 // Apply the render target on the device
1176 IDirect3DSurface9 *renderTargetSurface = NULL;
1177
1178 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1179 if (renderTarget)
1180 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001181 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001182 }
1183
1184 if (!renderTargetSurface)
1185 {
1186 ERR("render target pointer unexpectedly null.");
1187 return false; // Context must be lost
1188 }
1189
1190 mDevice->SetRenderTarget(0, renderTargetSurface);
Geoff Langea228632013-07-30 15:17:12 -04001191 SafeRelease(renderTargetSurface);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001192
1193 mAppliedRenderTargetSerial = renderTargetSerial;
1194 renderTargetChanged = true;
1195 }
1196
Jamie Madill3c7fa222014-06-05 13:08:51 -04001197 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001198 unsigned int depthbufferSerial = 0;
1199 unsigned int stencilbufferSerial = 0;
1200 if (framebuffer->getDepthbufferType() != GL_NONE)
1201 {
1202 depthStencil = framebuffer->getDepthbuffer();
1203 if (!depthStencil)
1204 {
1205 ERR("Depth stencil pointer unexpectedly null.");
1206 return false;
1207 }
1208
1209 depthbufferSerial = depthStencil->getSerial();
1210 }
1211 else if (framebuffer->getStencilbufferType() != GL_NONE)
1212 {
1213 depthStencil = framebuffer->getStencilbuffer();
1214 if (!depthStencil)
1215 {
1216 ERR("Depth stencil pointer unexpectedly null.");
1217 return false;
1218 }
1219
1220 stencilbufferSerial = depthStencil->getSerial();
1221 }
1222
1223 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1224 stencilbufferSerial != mAppliedStencilbufferSerial ||
1225 !mDepthStencilInitialized)
1226 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001227 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001228 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001229
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001230 // Apply the depth stencil on the device
1231 if (depthStencil)
1232 {
1233 IDirect3DSurface9 *depthStencilSurface = NULL;
1234 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1235
1236 if (depthStencilRenderTarget)
1237 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001238 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001239 }
1240
1241 if (!depthStencilSurface)
1242 {
1243 ERR("depth stencil pointer unexpectedly null.");
1244 return false; // Context must be lost
1245 }
1246
1247 mDevice->SetDepthStencilSurface(depthStencilSurface);
Geoff Langea228632013-07-30 15:17:12 -04001248 SafeRelease(depthStencilSurface);
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001249
Jamie Madill1e3fa742014-06-16 10:34:00 -04001250 depthSize = depthStencil->getDepthSize(getCurrentClientVersion());
1251 stencilSize = depthStencil->getStencilSize(getCurrentClientVersion());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001252 }
1253 else
1254 {
1255 mDevice->SetDepthStencilSurface(NULL);
1256 }
1257
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001258 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1259 {
1260 mCurDepthSize = depthSize;
1261 mForceSetRasterState = true;
1262 }
1263
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001264 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1265 {
1266 mCurStencilSize = stencilSize;
1267 mForceSetDepthStencilState = true;
1268 }
1269
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001270 mAppliedDepthbufferSerial = depthbufferSerial;
1271 mAppliedStencilbufferSerial = stencilbufferSerial;
1272 mDepthStencilInitialized = true;
1273 }
1274
1275 if (renderTargetChanged || !mRenderTargetDescInitialized)
1276 {
1277 mForceSetScissor = true;
1278 mForceSetViewport = true;
Geoff Langc142e9d2013-09-30 15:19:47 -04001279 mForceSetBlendState = true;
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001280
1281 mRenderTargetDesc.width = renderbufferObject->getWidth();
1282 mRenderTargetDesc.height = renderbufferObject->getHeight();
1283 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1284 mRenderTargetDescInitialized = true;
1285 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001286
1287 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001288}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001289
Jamie Madill57a89722013-07-02 11:57:03 -04001290GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001291 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001292{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001293 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001294 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001295 if (err != GL_NO_ERROR)
1296 {
1297 return err;
1298 }
Jamie Madill13a2f852013-12-11 16:35:08 -05001299
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001300 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1301}
1302
1303// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001304GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001305{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001306 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001307
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001308 if (err == GL_NO_ERROR)
1309 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001310 // Directly binding the storage buffer is not supported for d3d9
1311 ASSERT(indexInfo->storage == NULL);
1312
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001313 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001314 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001315 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1316
1317 mDevice->SetIndices(indexBuffer->getBuffer());
1318 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001319 }
1320 }
1321
1322 return err;
1323}
1324
Geoff Langeeba6e12014-02-03 13:12:30 -05001325void Renderer9::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1326{
1327 UNREACHABLE();
1328}
1329
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001330void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001331{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001332 ASSERT(!transformFeedbackActive);
1333
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001334 startScene();
Jamie Madill13a2f852013-12-11 16:35:08 -05001335
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001336 if (mode == GL_LINE_LOOP)
1337 {
1338 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1339 }
1340 else if (instances > 0)
1341 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001342 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001343 if (countingIB)
1344 {
1345 if (mAppliedIBSerial != countingIB->getSerial())
1346 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001347 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1348
1349 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001350 mAppliedIBSerial = countingIB->getSerial();
1351 }
1352
1353 for (int i = 0; i < mRepeatDraw; i++)
1354 {
1355 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1356 }
1357 }
1358 else
1359 {
1360 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001361 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001362 }
1363 }
1364 else // Regular case
1365 {
1366 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1367 }
1368}
1369
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001370void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1371 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001372{
1373 startScene();
1374
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001375 if (mode == GL_POINTS)
1376 {
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001377 drawIndexedPoints(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001378 }
1379 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001380 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001381 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001382 }
1383 else
1384 {
1385 for (int i = 0; i < mRepeatDraw; i++)
1386 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001387 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1388 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001389 }
1390 }
1391}
1392
1393void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1394{
1395 // Get the raw indices for an indexed draw
1396 if (type != GL_NONE && elementArrayBuffer)
1397 {
1398 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001399 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001400 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001401 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001402 }
1403
Geoff Langa36ead42013-08-02 11:54:08 -04001404 unsigned int startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001405
Geoff Langcec35902014-04-16 10:52:36 -04001406 if (getCaps().extensions.elementIndexUint)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001407 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001408 if (!mLineLoopIB)
1409 {
1410 mLineLoopIB = new StreamingIndexBufferInterface(this);
1411 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1412 {
1413 delete mLineLoopIB;
1414 mLineLoopIB = NULL;
1415
1416 ERR("Could not create a 32-bit looping 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 }
1419 }
1420
Geoff Lang57e713e2013-07-31 17:01:58 -04001421 // Checked by Renderer9::applyPrimitiveType
1422 ASSERT(count >= 0);
1423
1424 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001425 {
1426 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1427 return gl::error(GL_OUT_OF_MEMORY);
1428 }
1429
Geoff Lang57e713e2013-07-31 17:01:58 -04001430 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001431 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001432 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001433 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001434 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001435 }
1436
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001437 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001438 unsigned int offset = 0;
1439 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001440 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001441 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001442 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001443 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001444
Geoff Langa36ead42013-08-02 11:54:08 -04001445 startIndex = static_cast<unsigned int>(offset) / 4;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001446 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1447
1448 switch (type)
1449 {
1450 case GL_NONE: // Non-indexed draw
1451 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001452 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001453 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001454 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001455 data[count] = 0;
1456 break;
1457 case GL_UNSIGNED_BYTE:
1458 for (int i = 0; i < count; i++)
1459 {
1460 data[i] = static_cast<const GLubyte*>(indices)[i];
1461 }
1462 data[count] = static_cast<const GLubyte*>(indices)[0];
1463 break;
1464 case GL_UNSIGNED_SHORT:
1465 for (int i = 0; i < count; i++)
1466 {
1467 data[i] = static_cast<const GLushort*>(indices)[i];
1468 }
1469 data[count] = static_cast<const GLushort*>(indices)[0];
1470 break;
1471 case GL_UNSIGNED_INT:
1472 for (int i = 0; i < count; i++)
1473 {
1474 data[i] = static_cast<const GLuint*>(indices)[i];
1475 }
1476 data[count] = static_cast<const GLuint*>(indices)[0];
1477 break;
1478 default: UNREACHABLE();
1479 }
1480
1481 if (!mLineLoopIB->unmapBuffer())
1482 {
1483 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001484 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001485 }
1486 }
1487 else
1488 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001489 if (!mLineLoopIB)
1490 {
1491 mLineLoopIB = new StreamingIndexBufferInterface(this);
1492 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1493 {
1494 delete mLineLoopIB;
1495 mLineLoopIB = NULL;
1496
1497 ERR("Could not create a 16-bit looping 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 }
1500 }
1501
Geoff Lang57e713e2013-07-31 17:01:58 -04001502 // Checked by Renderer9::applyPrimitiveType
1503 ASSERT(count >= 0);
1504
1505 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001506 {
1507 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1508 return gl::error(GL_OUT_OF_MEMORY);
1509 }
1510
Geoff Lang57e713e2013-07-31 17:01:58 -04001511 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001512 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001513 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001514 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001515 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001516 }
1517
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001518 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001519 unsigned int offset;
1520 if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001521 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001522 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001523 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001524 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001525
Geoff Langa36ead42013-08-02 11:54:08 -04001526 startIndex = static_cast<unsigned int>(offset) / 2;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001527 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1528
1529 switch (type)
1530 {
1531 case GL_NONE: // Non-indexed draw
1532 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001533 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001534 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001535 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001536 data[count] = 0;
1537 break;
1538 case GL_UNSIGNED_BYTE:
1539 for (int i = 0; i < count; i++)
1540 {
1541 data[i] = static_cast<const GLubyte*>(indices)[i];
1542 }
1543 data[count] = static_cast<const GLubyte*>(indices)[0];
1544 break;
1545 case GL_UNSIGNED_SHORT:
1546 for (int i = 0; i < count; i++)
1547 {
1548 data[i] = static_cast<const GLushort*>(indices)[i];
1549 }
1550 data[count] = static_cast<const GLushort*>(indices)[0];
1551 break;
1552 case GL_UNSIGNED_INT:
1553 for (int i = 0; i < count; i++)
1554 {
1555 data[i] = static_cast<const GLuint*>(indices)[i];
1556 }
1557 data[count] = static_cast<const GLuint*>(indices)[0];
1558 break;
1559 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001560 }
1561
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001562 if (!mLineLoopIB->unmapBuffer())
1563 {
1564 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001565 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001566 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001567 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001568
1569 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001570 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001571 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1572
1573 mDevice->SetIndices(indexBuffer->getBuffer());
1574 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001575 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001576
1577 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001578}
1579
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001580template <typename T>
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001581static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices, int minIndex)
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001582{
1583 for (int i = 0; i < count; i++)
1584 {
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001585 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]) - minIndex;
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001586 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1587 }
1588}
1589
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001590void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001591{
1592 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1593 // for each individual point. This call is not expected to happen often.
1594
1595 if (elementArrayBuffer)
1596 {
1597 BufferStorage *storage = elementArrayBuffer->getStorage();
1598 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1599 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1600 }
1601
1602 switch (type)
1603 {
Geoff Langf8c2f5c2013-12-05 13:52:33 -05001604 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices, minIndex); break;
1605 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices, minIndex); break;
1606 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices, minIndex); break;
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001607 default: UNREACHABLE();
1608 }
1609}
1610
Geoff Lang04fb89a2014-06-09 15:05:36 -04001611void Renderer9::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1612 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001613{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001614 ASSERT(!transformFeedbackActive);
Geoff Lang0550d032014-01-30 11:29:07 -05001615 ASSERT(!rasterizerDiscard);
1616
Jamie Madillc5a83002014-02-14 16:41:25 -05001617 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001618 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001619
1620 IDirect3DVertexShader9 *vertexShader = (vertexExe ? ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader() : NULL);
1621 IDirect3DPixelShader9 *pixelShader = (pixelExe ? ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader() : NULL);
1622
Jamie Madill6246dc82014-01-29 09:26:47 -05001623 if (vertexShader != mAppliedVertexShader)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001624 {
daniel@transgaming.come4991412012-12-20 20:55:34 +00001625 mDevice->SetVertexShader(vertexShader);
Jamie Madill6246dc82014-01-29 09:26:47 -05001626 mAppliedVertexShader = vertexShader;
Jamie Madill6246dc82014-01-29 09:26:47 -05001627 }
daniel@transgaming.come4991412012-12-20 20:55:34 +00001628
Jamie Madill6246dc82014-01-29 09:26:47 -05001629 if (pixelShader != mAppliedPixelShader)
1630 {
1631 mDevice->SetPixelShader(pixelShader);
1632 mAppliedPixelShader = pixelShader;
Jamie Madill6246dc82014-01-29 09:26:47 -05001633 }
1634
Jamie Madilla5c9a142014-05-26 16:39:33 -04001635 // D3D9 has a quirk where creating multiple shaders with the same content
1636 // can return the same shader pointer. Because GL programs store different data
1637 // per-program, checking the program serial guarantees we upload fresh
1638 // uniform data even if our shader pointers are the same.
1639 // https://code.google.com/p/angleproject/issues/detail?id=661
1640 unsigned int programSerial = programBinary->getSerial();
1641 if (programSerial != mAppliedProgramSerial)
Jamie Madill6246dc82014-01-29 09:26:47 -05001642 {
1643 programBinary->dirtyAllUniforms();
Jamie Madilla5c9a142014-05-26 16:39:33 -04001644 mDxUniformsDirty = true;
1645 mAppliedProgramSerial = programSerial;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001646 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001647}
1648
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001649void Renderer9::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001650{
Jamie Madill834e8b72014-04-11 13:33:58 -04001651 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001652
1653 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001654 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001655 gl::LinkedUniform *targetUniform = uniformArray[uniformIndex];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001656
1657 if (targetUniform->dirty)
1658 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001659 GLfloat *f = (GLfloat*)targetUniform->data;
1660 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001661
1662 switch (targetUniform->type)
1663 {
1664 case GL_SAMPLER_2D:
1665 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001666 break;
1667 case GL_BOOL:
1668 case GL_BOOL_VEC2:
1669 case GL_BOOL_VEC3:
1670 case GL_BOOL_VEC4:
1671 applyUniformnbv(targetUniform, i);
1672 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001673 case GL_FLOAT:
1674 case GL_FLOAT_VEC2:
1675 case GL_FLOAT_VEC3:
1676 case GL_FLOAT_VEC4:
1677 case GL_FLOAT_MAT2:
1678 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001679 case GL_FLOAT_MAT4:
1680 applyUniformnfv(targetUniform, f);
1681 break;
1682 case GL_INT:
1683 case GL_INT_VEC2:
1684 case GL_INT_VEC3:
1685 case GL_INT_VEC4:
1686 applyUniformniv(targetUniform, i);
1687 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001688 default:
1689 UNREACHABLE();
1690 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001691 }
1692 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001693
1694 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001695 if (mDxUniformsDirty)
1696 {
1697 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1698 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1699 mDxUniformsDirty = false;
1700 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001701}
1702
Jamie Madill834e8b72014-04-11 13:33:58 -04001703void Renderer9::applyUniformnfv(gl::LinkedUniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001704{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001705 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001706 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001707 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001708 }
1709
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001710 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001711 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001712 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001713 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001714}
1715
Jamie Madill834e8b72014-04-11 13:33:58 -04001716void Renderer9::applyUniformniv(gl::LinkedUniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001717{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001718 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1719 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001720
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001721 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001722 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001723 vector[i][0] = (GLfloat)v[4 * i + 0];
1724 vector[i][1] = (GLfloat)v[4 * i + 1];
1725 vector[i][2] = (GLfloat)v[4 * i + 2];
1726 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001727 }
1728
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001729 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001730}
1731
Jamie Madill834e8b72014-04-11 13:33:58 -04001732void Renderer9::applyUniformnbv(gl::LinkedUniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001733{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001734 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1735 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001737 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001738 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001739 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1740 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1741 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1742 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001743 }
1744
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001745 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001746}
1747
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001748void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001749{
Geoff Langda507fe2013-08-20 12:01:42 -04001750 if (clearParams.colorClearType != GL_FLOAT)
1751 {
1752 // Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0
1753 UNREACHABLE();
1754 return;
1755 }
1756
1757 bool clearColor = clearParams.clearColor[0];
1758 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1759 {
1760 if (clearParams.clearColor[i] != clearColor)
1761 {
1762 // Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0
1763 UNREACHABLE();
1764 return;
1765 }
1766 }
1767
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001768 float depth = gl::clamp01(clearParams.depthClearValue);
Geoff Lange8bc3192014-02-14 13:04:03 -05001769 DWORD stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001770
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001771 unsigned int stencilUnmasked = 0x0;
Geoff Langda507fe2013-08-20 12:01:42 -04001772 if (clearParams.clearStencil && frameBuffer->hasStencil())
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001773 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001774 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1775 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001776 stencilUnmasked = (0x1 << stencilSize) - 1;
1777 }
1778
Geoff Langda507fe2013-08-20 12:01:42 -04001779 const bool needMaskedStencilClear = clearParams.clearStencil &&
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001780 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
Geoff Lange8bc3192014-02-14 13:04:03 -05001781
1782 bool needMaskedColorClear = false;
1783 D3DCOLOR color = D3DCOLOR_ARGB(255, 0, 0, 0);
1784 if (clearColor)
1785 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04001786 gl::FramebufferAttachment *attachment = frameBuffer->getFirstColorbuffer();
1787 GLenum internalFormat = attachment->getInternalFormat();
1788 GLenum actualFormat = attachment->getActualFormat();
Geoff Lange8bc3192014-02-14 13:04:03 -05001789
1790 GLuint clientVersion = getCurrentClientVersion();
1791 GLuint internalRedBits = gl::GetRedBits(internalFormat, clientVersion);
1792 GLuint internalGreenBits = gl::GetGreenBits(internalFormat, clientVersion);
1793 GLuint internalBlueBits = gl::GetBlueBits(internalFormat, clientVersion);
1794 GLuint internalAlphaBits = gl::GetAlphaBits(internalFormat, clientVersion);
1795
1796 GLuint actualRedBits = gl::GetRedBits(actualFormat, clientVersion);
1797 GLuint actualGreenBits = gl::GetGreenBits(actualFormat, clientVersion);
1798 GLuint actualBlueBits = gl::GetBlueBits(actualFormat, clientVersion);
1799 GLuint actualAlphaBits = gl::GetAlphaBits(actualFormat, clientVersion);
1800
1801 color = D3DCOLOR_ARGB(gl::unorm<8>((internalAlphaBits == 0 && actualAlphaBits > 0) ? 1.0f : clearParams.colorFClearValue.alpha),
1802 gl::unorm<8>((internalRedBits == 0 && actualRedBits > 0) ? 0.0f : clearParams.colorFClearValue.red),
1803 gl::unorm<8>((internalGreenBits == 0 && actualGreenBits > 0) ? 0.0f : clearParams.colorFClearValue.green),
1804 gl::unorm<8>((internalBlueBits == 0 && actualBlueBits > 0) ? 0.0f : clearParams.colorFClearValue.blue));
1805
1806 if ((internalRedBits > 0 && !clearParams.colorMaskRed) ||
1807 (internalGreenBits > 0 && !clearParams.colorMaskGreen) ||
1808 (internalBlueBits > 0 && !clearParams.colorMaskBlue) ||
1809 (internalAlphaBits > 0 && !clearParams.colorMaskAlpha))
1810 {
1811 needMaskedColorClear = true;
1812 }
1813 }
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001814
1815 if (needMaskedColorClear || needMaskedStencilClear)
1816 {
1817 // State which is altered in all paths from this point to the clear call is saved.
1818 // State which is altered in only some paths will be flagged dirty in the case that
1819 // that path is taken.
1820 HRESULT hr;
1821 if (mMaskedClearSavedState == NULL)
1822 {
1823 hr = mDevice->BeginStateBlock();
1824 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1825
1826 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1827 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1828 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1829 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1830 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1831 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1832 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1833 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1834 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1835 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1836 mDevice->SetPixelShader(NULL);
1837 mDevice->SetVertexShader(NULL);
1838 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1839 mDevice->SetStreamSource(0, NULL, 0, 0);
1840 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1841 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1842 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1843 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1844 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1845 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1846 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1847
1848 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1849 {
1850 mDevice->SetStreamSourceFreq(i, 1);
1851 }
1852
1853 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1854 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1855 }
1856
1857 ASSERT(mMaskedClearSavedState != NULL);
1858
1859 if (mMaskedClearSavedState != NULL)
1860 {
1861 hr = mMaskedClearSavedState->Capture();
1862 ASSERT(SUCCEEDED(hr));
1863 }
1864
1865 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1866 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1867 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1868 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1869 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1870 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1871 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1872 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1873
Geoff Langda507fe2013-08-20 12:01:42 -04001874 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001875 {
1876 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1877 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1878 clearParams.colorMaskGreen,
1879 clearParams.colorMaskBlue,
1880 clearParams.colorMaskAlpha));
1881 }
1882 else
1883 {
1884 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1885 }
1886
Geoff Langda507fe2013-08-20 12:01:42 -04001887 if (stencilUnmasked != 0x0 && clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001888 {
1889 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1890 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1891 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1892 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1893 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1894 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1895 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1896 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1897 }
1898 else
1899 {
1900 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1901 }
1902
1903 mDevice->SetPixelShader(NULL);
1904 mDevice->SetVertexShader(NULL);
1905 mDevice->SetFVF(D3DFVF_XYZRHW);
1906 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1907 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1908 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1909 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1910 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1911 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1912 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1913
1914 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1915 {
1916 mDevice->SetStreamSourceFreq(i, 1);
1917 }
1918
1919 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1920 quad[0][0] = -0.5f;
1921 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1922 quad[0][2] = 0.0f;
1923 quad[0][3] = 1.0f;
1924
1925 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1926 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1927 quad[1][2] = 0.0f;
1928 quad[1][3] = 1.0f;
1929
1930 quad[2][0] = -0.5f;
1931 quad[2][1] = -0.5f;
1932 quad[2][2] = 0.0f;
1933 quad[2][3] = 1.0f;
1934
1935 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1936 quad[3][1] = -0.5f;
1937 quad[3][2] = 0.0f;
1938 quad[3][3] = 1.0f;
1939
1940 startScene();
1941 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1942
Geoff Langda507fe2013-08-20 12:01:42 -04001943 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001944 {
1945 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1946 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1947 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1948 }
1949
1950 if (mMaskedClearSavedState != NULL)
1951 {
1952 mMaskedClearSavedState->Apply();
1953 }
1954 }
Geoff Langda507fe2013-08-20 12:01:42 -04001955 else if (clearColor || clearParams.clearDepth || clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001956 {
1957 DWORD dxClearFlags = 0;
Geoff Langda507fe2013-08-20 12:01:42 -04001958 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001959 {
1960 dxClearFlags |= D3DCLEAR_TARGET;
1961 }
Geoff Langda507fe2013-08-20 12:01:42 -04001962 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001963 {
1964 dxClearFlags |= D3DCLEAR_ZBUFFER;
1965 }
Geoff Langda507fe2013-08-20 12:01:42 -04001966 if (clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001967 {
1968 dxClearFlags |= D3DCLEAR_STENCIL;
1969 }
1970
1971 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1972 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001973}
1974
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001975void Renderer9::markAllStateDirty()
1976{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001977 mAppliedRenderTargetSerial = 0;
1978 mAppliedDepthbufferSerial = 0;
1979 mAppliedStencilbufferSerial = 0;
1980 mDepthStencilInitialized = false;
1981 mRenderTargetDescInitialized = false;
1982
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001983 mForceSetDepthStencilState = true;
1984 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001985 mForceSetScissor = true;
1986 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001987 mForceSetBlendState = true;
1988
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001989 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001990 {
1991 mForceSetVertexSamplerStates[i] = true;
1992 mCurVertexTextureSerials[i] = 0;
1993 }
1994 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1995 {
1996 mForceSetPixelSamplerStates[i] = true;
1997 mCurPixelTextureSerials[i] = 0;
1998 }
1999
daniel@transgaming.com9a067372012-12-20 20:55:24 +00002000 mAppliedIBSerial = 0;
Jamie Madill6246dc82014-01-29 09:26:47 -05002001 mAppliedVertexShader = NULL;
2002 mAppliedPixelShader = NULL;
Jamie Madilla5c9a142014-05-26 16:39:33 -04002003 mAppliedProgramSerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00002004 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002005
2006 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00002007}
2008
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002009void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002010{
Geoff Langea228632013-07-30 15:17:12 -04002011 for (size_t i = 0; i < mEventQueryPool.size(); i++)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002012 {
Geoff Langea228632013-07-30 15:17:12 -04002013 SafeRelease(mEventQueryPool[i]);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002014 }
Geoff Langea228632013-07-30 15:17:12 -04002015 mEventQueryPool.clear();
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002016
Geoff Langea228632013-07-30 15:17:12 -04002017 SafeRelease(mMaskedClearSavedState);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00002018
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002019 mVertexShaderCache.clear();
2020 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002021
Geoff Langea228632013-07-30 15:17:12 -04002022 SafeDelete(mBlit);
2023 SafeDelete(mVertexDataManager);
2024 SafeDelete(mIndexDataManager);
2025 SafeDelete(mLineLoopIB);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002026
2027 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2028 {
Geoff Langea228632013-07-30 15:17:12 -04002029 SafeDelete(mNullColorbufferCache[i].buffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002030 }
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002031}
2032
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002033void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002034{
2035 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002036 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002037}
2038
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002039bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002040{
2041 return mDeviceLost;
2042}
2043
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002044// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002045bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002046{
Jamie Madillb7935e52013-11-20 15:47:47 -05002047 HRESULT status = getDeviceStatusCode();
Jamie Madillcd65ae12013-12-17 15:20:14 -05002048 bool isLost = FAILED(status);
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002049
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002050 if (isLost)
2051 {
2052 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002053 // we'll probably get this done again by notifyDeviceLost
2054 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002055 // Note that we don't want to clear the device loss status here
2056 // -- this needs to be done by resetDevice
2057 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002058 if (notify)
2059 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002060 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002061 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002062 }
2063
2064 return isLost;
2065}
2066
Jamie Madillb7935e52013-11-20 15:47:47 -05002067HRESULT Renderer9::getDeviceStatusCode()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002068{
2069 HRESULT status = D3D_OK;
2070
2071 if (mDeviceEx)
2072 {
2073 status = mDeviceEx->CheckDeviceState(NULL);
2074 }
2075 else if (mDevice)
2076 {
2077 status = mDevice->TestCooperativeLevel();
2078 }
2079
Jamie Madillb7935e52013-11-20 15:47:47 -05002080 return status;
2081}
2082
2083bool Renderer9::testDeviceResettable()
2084{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002085 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002086 // DEVICEREMOVED indicates the device has been stopped and must be recreated
Jamie Madillb7935e52013-11-20 15:47:47 -05002087 switch (getDeviceStatusCode())
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002088 {
2089 case D3DERR_DEVICENOTRESET:
2090 case D3DERR_DEVICEHUNG:
2091 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002092 case D3DERR_DEVICELOST:
2093 return (mDeviceEx != NULL);
2094 case D3DERR_DEVICEREMOVED:
Jamie Madillb7935e52013-11-20 15:47:47 -05002095 ASSERT(mDeviceEx != NULL);
Jamie Madill88f779d2013-12-03 10:57:56 -05002096 return isRemovedDeviceResettable();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002097 default:
2098 return false;
2099 }
2100}
2101
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002102bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002103{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002104 releaseDeviceResources();
2105
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002106 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2107
2108 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002109 bool lost = testDeviceLost(false);
Jamie Madillb7935e52013-11-20 15:47:47 -05002110 bool removedDevice = (getDeviceStatusCode() == D3DERR_DEVICEREMOVED);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002111
Jamie Madill88f779d2013-12-03 10:57:56 -05002112 // Device Removed is a feature which is only present with D3D9Ex
2113 ASSERT(mDeviceEx != NULL || !removedDevice);
2114
2115 for (int attempts = 3; lost && attempts > 0; attempts--)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002116 {
Jamie Madill88f779d2013-12-03 10:57:56 -05002117 if (removedDevice)
2118 {
2119 // Device removed, which may trigger on driver reinstallation,
2120 // may cause a longer wait other reset attempts before the
2121 // system is ready to handle creating a new device.
2122 Sleep(800);
2123 lost = !resetRemovedDevice();
2124 }
2125 else if (mDeviceEx)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002126 {
2127 Sleep(500); // Give the graphics driver some CPU time
Jamie Madill88f779d2013-12-03 10:57:56 -05002128 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2129 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002130 }
2131 else
2132 {
2133 result = mDevice->TestCooperativeLevel();
2134 while (result == D3DERR_DEVICELOST)
2135 {
2136 Sleep(100); // Give the graphics driver some CPU time
2137 result = mDevice->TestCooperativeLevel();
2138 }
2139
2140 if (result == D3DERR_DEVICENOTRESET)
2141 {
2142 result = mDevice->Reset(&presentParameters);
2143 }
Jamie Madill88f779d2013-12-03 10:57:56 -05002144 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002145 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002146 }
2147
2148 if (FAILED(result))
2149 {
2150 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2151 return false;
2152 }
2153
Jamie Madill88f779d2013-12-03 10:57:56 -05002154 if (removedDevice && lost)
2155 {
2156 ERR("Device lost reset failed multiple times");
2157 return false;
2158 }
2159
Jamie Madillb7935e52013-11-20 15:47:47 -05002160 // If the device was removed, we already finished re-initialization in resetRemovedDevice
2161 if (!removedDevice)
2162 {
2163 // reset device defaults
2164 initializeDevice();
2165 }
2166
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002167 mDeviceLost = false;
2168
2169 return true;
2170}
2171
Jamie Madill88f779d2013-12-03 10:57:56 -05002172bool Renderer9::isRemovedDeviceResettable() const
2173{
2174 bool success = false;
2175
2176#ifdef ANGLE_ENABLE_D3D9EX
2177 IDirect3D9Ex *d3d9Ex = NULL;
2178 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
2179 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
2180
2181 if (Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &d3d9Ex)))
2182 {
2183 D3DCAPS9 deviceCaps;
2184 HRESULT result = d3d9Ex->GetDeviceCaps(mAdapter, mDeviceType, &deviceCaps);
2185 success = SUCCEEDED(result);
2186 }
2187
2188 SafeRelease(d3d9Ex);
2189#else
2190 ASSERT(UNREACHABLE());
2191#endif
2192
2193 return success;
2194}
2195
2196bool Renderer9::resetRemovedDevice()
Jamie Madillb7935e52013-11-20 15:47:47 -05002197{
2198 // From http://msdn.microsoft.com/en-us/library/windows/desktop/bb172554(v=vs.85).aspx:
2199 // The hardware adapter has been removed. Application must destroy the device, do enumeration of
2200 // adapters and create another Direct3D device. If application continues rendering without
2201 // calling Reset, the rendering calls will succeed. Applies to Direct3D 9Ex only.
Geoff Langcec35902014-04-16 10:52:36 -04002202 release();
Jamie Madill88f779d2013-12-03 10:57:56 -05002203 return (initialize() == EGL_SUCCESS);
Jamie Madillb7935e52013-11-20 15:47:47 -05002204}
2205
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002206DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002207{
2208 return mAdapterIdentifier.VendorId;
2209}
2210
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002211std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002212{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002213 std::ostringstream rendererString;
2214
2215 rendererString << mAdapterIdentifier.Description;
2216 if (getShareHandleSupport())
2217 {
2218 rendererString << " Direct3D9Ex";
2219 }
2220 else
2221 {
2222 rendererString << " Direct3D9";
2223 }
2224
2225 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2226 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2227
2228 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002229}
2230
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002231GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002232{
2233 return mAdapterIdentifier.DeviceIdentifier;
2234}
2235
Geoff Lang61e49a52013-05-29 10:22:58 -04002236Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002237{
Geoff Lang61e49a52013-05-29 10:22:58 -04002238 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002239
Geoff Lang61e49a52013-05-29 10:22:58 -04002240 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2241 {
2242 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2243 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2244
2245 if (SUCCEEDED(result))
2246 {
2247 support.supportedSamples[multiSampleIndex] = true;
2248 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2249 {
2250 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2251 }
2252 }
2253 else
2254 {
2255 support.supportedSamples[multiSampleIndex] = false;
2256 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002257 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002258
2259 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002260}
2261
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002262unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002263{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002264 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2265 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002266}
2267
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002268unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2269{
2270 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2271}
2272
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002273unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002274{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002275 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002276}
2277
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002278unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002279{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002280 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002281}
2282
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002283unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002284{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002285 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2286}
2287
2288unsigned int Renderer9::getMaxFragmentUniformVectors() const
2289{
2290 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2291
2292 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2293}
2294
2295unsigned int Renderer9::getMaxVaryingVectors() const
2296{
2297 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002298}
2299
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002300unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2301{
2302 return 0;
2303}
2304
2305unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2306{
2307 return 0;
2308}
2309
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002310unsigned int Renderer9::getReservedVertexUniformBuffers() const
2311{
2312 return 0;
2313}
2314
2315unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2316{
2317 return 0;
2318}
2319
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002320unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2321{
2322 return 0;
2323}
2324
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002325unsigned int Renderer9::getMaxTransformFeedbackSeparateComponents() const
2326{
2327 return 0;
2328}
2329
2330unsigned int Renderer9::getMaxTransformFeedbackInterleavedComponents() const
2331{
2332 return 0;
2333}
2334
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002335unsigned int Renderer9::getMaxUniformBufferSize() const
2336{
2337 return 0;
2338}
2339
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002340bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002341{
2342 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002343 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002344}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002345
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002346bool Renderer9::getPostSubBufferSupport() const
2347{
2348 return true;
2349}
2350
Jamie Madill13a2f852013-12-11 16:35:08 -05002351int Renderer9::getMaxRecommendedElementsIndices() const
2352{
2353 // ES3 only
2354 UNREACHABLE();
2355 return 0;
2356}
2357
2358int Renderer9::getMaxRecommendedElementsVertices() const
2359{
2360 // ES3 only
2361 UNREACHABLE();
2362 return 0;
2363}
2364
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002365int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002366{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002367 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002368}
2369
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002370float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002371{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002372 // Point size clamped at 1.0f for SM2
2373 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002374}
2375
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002376int Renderer9::getMaxViewportDimension() const
2377{
2378 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002379 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002380 return maxTextureDimension;
2381}
2382
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002383int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002384{
2385 return (int)mDeviceCaps.MaxTextureWidth;
2386}
2387
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002388int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002389{
2390 return (int)mDeviceCaps.MaxTextureHeight;
2391}
2392
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002393int Renderer9::getMaxTextureDepth() const
2394{
2395 // 3D textures are not available in the D3D9 backend.
2396 return 1;
2397}
2398
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002399int Renderer9::getMaxTextureArrayLayers() const
2400{
2401 // 2D array textures are not available in the D3D9 backend.
2402 return 1;
2403}
2404
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002405DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002406{
2407 return mDeviceCaps.DeclTypes;
2408}
2409
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002410int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002411{
2412 return mMinSwapInterval;
2413}
2414
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002415int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002416{
2417 return mMaxSwapInterval;
2418}
2419
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002420int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002421{
2422 return mMaxSupportedSamples;
2423}
2424
Geoff Lang005df412013-10-16 14:12:50 -04002425GLsizei Renderer9::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002426{
Geoff Langcec35902014-04-16 10:52:36 -04002427 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat);
Geoff Lang0e120e32013-05-29 10:23:55 -04002428 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2429 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2430}
2431
Geoff Lang005df412013-10-16 14:12:50 -04002432GLsizei Renderer9::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002433{
Geoff Langcec35902014-04-16 10:52:36 -04002434 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002435 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
Jamie Madill13a2f852013-12-11 16:35:08 -05002436
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002437 unsigned int numCounts = 0;
2438 if (iter != mMultiSampleSupport.end())
2439 {
2440 const MultisampleSupportInfo& info = iter->second;
2441 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2442 {
2443 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2444 {
2445 numCounts++;
2446 }
2447 }
2448 }
2449
2450 return numCounts;
2451}
2452
Geoff Lang005df412013-10-16 14:12:50 -04002453void Renderer9::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002454{
Geoff Langcec35902014-04-16 10:52:36 -04002455 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002456 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2457
2458 if (iter != mMultiSampleSupport.end())
2459 {
2460 const MultisampleSupportInfo& info = iter->second;
2461 int bufPos = 0;
2462 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2463 {
2464 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2465 {
2466 params[bufPos++] = i;
2467 }
2468 }
2469 }
2470}
2471
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002472int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002473{
2474 if (requested == 0)
2475 {
2476 return requested;
2477 }
2478
Geoff Lang61e49a52013-05-29 10:22:58 -04002479 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002480 if (itr == mMultiSampleSupport.end())
2481 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002482 if (format == D3DFMT_UNKNOWN)
2483 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002484 return -1;
2485 }
2486
Geoff Lang61e49a52013-05-29 10:22:58 -04002487 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002488 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002489 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002490 {
2491 return i;
2492 }
2493 }
2494
2495 return -1;
2496}
2497
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002498unsigned int Renderer9::getMaxRenderTargets() const
2499{
2500 // we do not support MRT in d3d9
2501 return 1;
2502}
2503
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002504bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002505{
2506 bool result = false;
2507
2508 if (source && dest)
2509 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002510 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2511 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002512
Nicolas Capensbf712d02014-03-31 14:23:35 -04002513 int levels = source9->getLevelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002514 for (int i = 0; i < levels; ++i)
2515 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002516 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2517 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
Jamie Madill13a2f852013-12-11 16:35:08 -05002518
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002519 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002520
Geoff Langea228632013-07-30 15:17:12 -04002521 SafeRelease(srcSurf);
2522 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002523
2524 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002525 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002526 return false;
Geoff Langea228632013-07-30 15:17:12 -04002527 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002528 }
2529 }
2530
2531 return result;
2532}
2533
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002534bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002535{
2536 bool result = false;
2537
2538 if (source && dest)
2539 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002540 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2541 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
Nicolas Capensbf712d02014-03-31 14:23:35 -04002542 int levels = source9->getLevelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002543 for (int f = 0; f < 6; f++)
2544 {
2545 for (int i = 0; i < levels; i++)
2546 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002547 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2548 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002549
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002550 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002551
Geoff Langea228632013-07-30 15:17:12 -04002552 SafeRelease(srcSurf);
2553 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002554
2555 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002556 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002557 return false;
Geoff Langea228632013-07-30 15:17:12 -04002558 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002559 }
2560 }
2561 }
2562
2563 return result;
2564}
2565
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002566bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2567{
2568 // 3D textures are not available in the D3D9 backend.
2569 UNREACHABLE();
2570 return false;
2571}
2572
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002573bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2574{
2575 // 2D array textures are not supported by the D3D9 backend.
2576 UNREACHABLE();
2577 return false;
2578}
2579
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002580D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002581{
2582 if (mD3d9Ex != NULL)
2583 {
2584 return D3DPOOL_DEFAULT;
2585 }
2586 else
2587 {
2588 if (!(usage & D3DUSAGE_DYNAMIC))
2589 {
2590 return D3DPOOL_MANAGED;
2591 }
2592 }
2593
2594 return D3DPOOL_DEFAULT;
2595}
2596
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002597bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002598 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002599{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002600 RECT rect;
2601 rect.left = sourceRect.x;
2602 rect.top = sourceRect.y;
2603 rect.right = sourceRect.x + sourceRect.width;
2604 rect.bottom = sourceRect.y + sourceRect.height;
2605
2606 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002607}
2608
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002609bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002610 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002611{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002612 RECT rect;
2613 rect.left = sourceRect.x;
2614 rect.top = sourceRect.y;
2615 rect.right = sourceRect.x + sourceRect.width;
2616 rect.bottom = sourceRect.y + sourceRect.height;
2617
2618 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002619}
2620
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002621bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2622 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2623{
2624 // 3D textures are not available in the D3D9 backend.
2625 UNREACHABLE();
2626 return false;
2627}
2628
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002629bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2630 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2631{
2632 // 2D array textures are not available in the D3D9 backend.
2633 UNREACHABLE();
2634 return false;
2635}
2636
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002637bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002638 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002639{
Geoff Lang758d5b22013-06-11 11:42:50 -04002640 ASSERT(filter == GL_NEAREST);
2641
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002642 endScene();
2643
2644 if (blitRenderTarget)
2645 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002646 gl::FramebufferAttachment *readBuffer = readFramebuffer->getColorbuffer(0);
2647 gl::FramebufferAttachment *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002648 RenderTarget9 *readRenderTarget = NULL;
2649 RenderTarget9 *drawRenderTarget = NULL;
2650 IDirect3DSurface9* readSurface = NULL;
2651 IDirect3DSurface9* drawSurface = NULL;
2652
2653 if (readBuffer)
2654 {
2655 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2656 }
2657 if (drawBuffer)
2658 {
2659 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2660 }
2661
2662 if (readRenderTarget)
2663 {
2664 readSurface = readRenderTarget->getSurface();
2665 }
2666 if (drawRenderTarget)
2667 {
2668 drawSurface = drawRenderTarget->getSurface();
2669 }
2670
2671 if (!readSurface || !drawSurface)
2672 {
2673 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002674 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002675 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002676
Geoff Lang125deab2013-08-09 13:34:16 -04002677 gl::Extents srcSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
2678 gl::Extents dstSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
2679
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002680 RECT srcRect;
2681 srcRect.left = readRect.x;
2682 srcRect.right = readRect.x + readRect.width;
2683 srcRect.top = readRect.y;
2684 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002685
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002686 RECT dstRect;
2687 dstRect.left = drawRect.x;
2688 dstRect.right = drawRect.x + drawRect.width;
2689 dstRect.top = drawRect.y;
2690 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002691
Geoff Lang125deab2013-08-09 13:34:16 -04002692 // Clip the rectangles to the scissor rectangle
2693 if (scissor)
2694 {
2695 if (dstRect.left < scissor->x)
2696 {
2697 srcRect.left += (scissor->x - dstRect.left);
2698 dstRect.left = scissor->x;
2699 }
2700 if (dstRect.top < scissor->y)
2701 {
2702 srcRect.top += (scissor->y - dstRect.top);
2703 dstRect.top = scissor->y;
2704 }
2705 if (dstRect.right > scissor->x + scissor->width)
2706 {
2707 srcRect.right -= (dstRect.right - (scissor->x + scissor->width));
2708 dstRect.right = scissor->x + scissor->width;
2709 }
2710 if (dstRect.bottom > scissor->y + scissor->height)
2711 {
2712 srcRect.bottom -= (dstRect.bottom - (scissor->y + scissor->height));
2713 dstRect.bottom = scissor->y + scissor->height;
2714 }
2715 }
2716
2717 // Clip the rectangles to the destination size
2718 if (dstRect.left < 0)
2719 {
2720 srcRect.left += -dstRect.left;
2721 dstRect.left = 0;
2722 }
2723 if (dstRect.right > dstSize.width)
2724 {
2725 srcRect.right -= (dstRect.right - dstSize.width);
2726 dstRect.right = dstSize.width;
2727 }
2728 if (dstRect.top < 0)
2729 {
2730 srcRect.top += -dstRect.top;
2731 dstRect.top = 0;
2732 }
2733 if (dstRect.bottom > dstSize.height)
2734 {
2735 srcRect.bottom -= (dstRect.bottom - dstSize.height);
2736 dstRect.bottom = dstSize.height;
2737 }
2738
2739 // Clip the rectangles to the source size
2740 if (srcRect.left < 0)
2741 {
2742 dstRect.left += -srcRect.left;
2743 srcRect.left = 0;
2744 }
2745 if (srcRect.right > srcSize.width)
2746 {
2747 dstRect.right -= (srcRect.right - srcSize.width);
2748 srcRect.right = srcSize.width;
2749 }
2750 if (srcRect.top < 0)
2751 {
2752 dstRect.top += -srcRect.top;
2753 srcRect.top = 0;
2754 }
2755 if (srcRect.bottom > srcSize.height)
2756 {
2757 dstRect.bottom -= (srcRect.bottom - srcSize.height);
2758 srcRect.bottom = srcSize.height;
2759 }
2760
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002761 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002762
Geoff Langea228632013-07-30 15:17:12 -04002763 SafeRelease(readSurface);
2764 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002765
2766 if (FAILED(result))
2767 {
2768 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2769 return false;
2770 }
2771 }
2772
Geoff Lang685806d2013-06-12 11:16:36 -04002773 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002774 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002775 gl::FramebufferAttachment *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2776 gl::FramebufferAttachment *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002777 RenderTarget9 *readDepthStencil = NULL;
2778 RenderTarget9 *drawDepthStencil = NULL;
2779 IDirect3DSurface9* readSurface = NULL;
2780 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002781
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002782 if (readBuffer)
2783 {
2784 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2785 }
2786 if (drawBuffer)
2787 {
2788 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2789 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002790
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002791 if (readDepthStencil)
2792 {
2793 readSurface = readDepthStencil->getSurface();
2794 }
2795 if (drawDepthStencil)
2796 {
2797 drawSurface = drawDepthStencil->getSurface();
2798 }
2799
2800 if (!readSurface || !drawSurface)
2801 {
2802 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002803 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002804 }
2805
2806 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2807
Geoff Langea228632013-07-30 15:17:12 -04002808 SafeRelease(readSurface);
2809 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002810
2811 if (FAILED(result))
2812 {
2813 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2814 return false;
2815 }
2816 }
2817
2818 return true;
2819}
2820
Jamie Madilleb9baab2014-03-24 13:19:43 -04002821void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2822 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002823{
Jamie Madilleb9baab2014-03-24 13:19:43 -04002824 ASSERT(pack.pixelBuffer.get() == NULL);
2825
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002826 RenderTarget9 *renderTarget = NULL;
2827 IDirect3DSurface9 *surface = NULL;
Jamie Madill3c7fa222014-06-05 13:08:51 -04002828 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002829
2830 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002831 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002832 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2833 }
Jamie Madill13a2f852013-12-11 16:35:08 -05002834
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002835 if (renderTarget)
2836 {
2837 surface = renderTarget->getSurface();
2838 }
2839
2840 if (!surface)
2841 {
2842 // context must be lost
2843 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002844 }
2845
2846 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002847 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002848
2849 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2850 {
2851 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
Geoff Langea228632013-07-30 15:17:12 -04002852 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002853 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002854 }
2855
2856 HRESULT result;
2857 IDirect3DSurface9 *systemSurface = NULL;
Jamie Madilleb9baab2014-03-24 13:19:43 -04002858 bool directToPixels = !pack.reverseRowOrder && pack.alignment <= 4 && getShareHandleSupport() &&
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002859 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2860 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2861 if (directToPixels)
2862 {
2863 // Use the pixels ptr as a shared handle to write directly into client's memory
2864 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2865 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2866 if (FAILED(result))
2867 {
2868 // Try again without the shared handle
2869 directToPixels = false;
2870 }
2871 }
2872
2873 if (!directToPixels)
2874 {
2875 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2876 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2877 if (FAILED(result))
2878 {
2879 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
Geoff Langea228632013-07-30 15:17:12 -04002880 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002881 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002882 }
2883 }
2884
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002885 result = mDevice->GetRenderTargetData(surface, systemSurface);
Geoff Langea228632013-07-30 15:17:12 -04002886 SafeRelease(surface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002887
2888 if (FAILED(result))
2889 {
Geoff Langea228632013-07-30 15:17:12 -04002890 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002891
2892 // It turns out that D3D will sometimes produce more error
2893 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002894 if (d3d9::isDeviceLostError(result))
2895 {
2896 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002897 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002898 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002899 else
2900 {
2901 UNREACHABLE();
2902 return;
2903 }
2904
2905 }
2906
2907 if (directToPixels)
2908 {
Geoff Langea228632013-07-30 15:17:12 -04002909 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002910 return;
2911 }
2912
2913 RECT rect;
2914 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2915 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2916 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2917 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2918
2919 D3DLOCKED_RECT lock;
2920 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2921
2922 if (FAILED(result))
2923 {
2924 UNREACHABLE();
Geoff Langea228632013-07-30 15:17:12 -04002925 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002926
2927 return; // No sensible error to generate
2928 }
2929
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002930 unsigned char *source;
2931 int inputPitch;
Jamie Madilleb9baab2014-03-24 13:19:43 -04002932 if (pack.reverseRowOrder)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002933 {
2934 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2935 inputPitch = -lock.Pitch;
2936 }
2937 else
2938 {
2939 source = (unsigned char*)lock.pBits;
2940 inputPitch = lock.Pitch;
2941 }
2942
Geoff Lang697ad3e2013-06-04 10:11:28 -04002943 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002944
Geoff Lang005df412013-10-16 14:12:50 -04002945 GLenum sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04002946 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
2947 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002948
Geoff Lang697ad3e2013-06-04 10:11:28 -04002949 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2950
2951 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002952 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002953 // Direct copy possible
2954 unsigned char *dest = static_cast<unsigned char*>(pixels);
2955 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002956 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002957 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002958 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002959 }
2960 else
2961 {
Geoff Lang005df412013-10-16 14:12:50 -04002962 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04002963 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
2964 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2965
2966 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
2967 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002968 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002969 // Fast copy is possible through some special function
2970 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002971 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002972 for (int x = 0; x < rect.right - rect.left; x++)
2973 {
2974 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2975 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
2976
2977 fastCopyFunc(src, dest);
2978 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002979 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002980 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002981 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002982 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002983 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
2984 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002985
Geoff Lang697ad3e2013-06-04 10:11:28 -04002986 gl::ColorF temp;
2987
2988 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002989 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002990 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002991 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002992 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2993 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002994
Geoff Lang697ad3e2013-06-04 10:11:28 -04002995 // readFunc and writeFunc will be using the same type of color, CopyTexImage
2996 // will not allow the copy otherwise.
2997 readFunc(src, &temp);
2998 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002999 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003000 }
3001 }
3002 }
3003
3004 systemSurface->UnlockRect();
Geoff Langea228632013-07-30 15:17:12 -04003005 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003006}
3007
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003008RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
3009{
3010 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3011 IDirect3DSurface9 *surface = NULL;
3012 if (depth)
3013 {
3014 surface = swapChain9->getDepthStencil();
3015 }
3016 else
3017 {
3018 surface = swapChain9->getRenderTarget();
3019 }
3020
3021 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3022
3023 return renderTarget;
3024}
3025
Geoff Langa2d97f12013-06-11 11:44:02 -04003026RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003027{
3028 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3029 return renderTarget;
3030}
3031
Geoff Lang48dcae72014-02-05 16:28:24 -05003032ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type,
3033 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
3034 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003035{
Geoff Lang48dcae72014-02-05 16:28:24 -05003036 // Transform feedback is not supported in ES2 or D3D9
3037 ASSERT(transformFeedbackVaryings.size() == 0);
3038
daniel@transgaming.com55318902012-11-28 20:58:58 +00003039 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003040
3041 switch (type)
3042 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003043 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003044 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003045 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003046 if (vshader)
3047 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003048 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003049 }
3050 }
3051 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003052 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003053 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003054 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003055 if (pshader)
3056 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003057 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003058 }
3059 }
3060 break;
3061 default:
3062 UNREACHABLE();
3063 break;
3064 }
3065
3066 return executable;
3067}
3068
Geoff Lang48dcae72014-02-05 16:28:24 -05003069ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
3070 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
3071 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003072{
Geoff Lang48dcae72014-02-05 16:28:24 -05003073 // Transform feedback is not supported in ES2 or D3D9
3074 ASSERT(transformFeedbackVaryings.size() == 0);
3075
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003076 const char *profile = NULL;
3077
3078 switch (type)
3079 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003080 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003081 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3082 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003083 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003084 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3085 break;
3086 default:
3087 UNREACHABLE();
3088 return NULL;
3089 }
3090
Nicolas Capens93faad92014-05-10 12:14:13 -04003091 UINT flags = ANGLE_COMPILE_OPTIMIZATION_LEVEL;
Nicolas Capens655fe362014-04-11 13:12:34 -04003092
3093 if (workaround == ANGLE_D3D_WORKAROUND_SKIP_OPTIMIZATION)
3094 {
Nicolas Capens93faad92014-05-10 12:14:13 -04003095 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
Nicolas Capens655fe362014-04-11 13:12:34 -04003096 }
3097 else if (workaround == ANGLE_D3D_WORKAROUND_MAX_OPTIMIZATION)
3098 {
Nicolas Capens93faad92014-05-10 12:14:13 -04003099 flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
Nicolas Capens655fe362014-04-11 13:12:34 -04003100 }
3101 else ASSERT(workaround == ANGLE_D3D_WORKAROUND_NONE);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003102
Nicolas Capens93faad92014-05-10 12:14:13 -04003103 if (gl::perfActive())
3104 {
3105#ifndef NDEBUG
3106 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
3107#endif
3108
3109 flags |= D3DCOMPILE_DEBUG;
3110
3111 std::string sourcePath = getTempPath();
3112 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
3113 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
3114 }
3115
3116 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
3117 // Try the default flags first and if compilation fails, try some alternatives.
3118 const UINT extraFlags[] =
3119 {
3120 flags,
3121 flags | D3DCOMPILE_AVOID_FLOW_CONTROL,
3122 flags | D3DCOMPILE_PREFER_FLOW_CONTROL
3123 };
3124
3125 const static char *extraFlagNames[] =
3126 {
3127 "default",
3128 "avoid flow control",
3129 "prefer flow control"
3130 };
3131
3132 int attempts = ArraySize(extraFlags);
3133
3134 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003135 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04003136 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003137 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04003138 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003139
Geoff Lang48dcae72014-02-05 16:28:24 -05003140 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type,
3141 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04003142 SafeRelease(binary);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003143
3144 return executable;
3145}
3146
Jamie Madill8ff21ae2014-02-04 16:04:05 -05003147rx::UniformStorage *Renderer9::createUniformStorage(size_t storageSize)
3148{
3149 return new UniformStorage(storageSize);
3150}
3151
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003152bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3153{
3154 return mBlit->boxFilter(source, dest);
3155}
3156
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003157D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003158{
3159 if (mD3d9Ex != NULL)
3160 {
3161 return D3DPOOL_DEFAULT;
3162 }
3163 else
3164 {
3165 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3166 {
3167 return D3DPOOL_MANAGED;
3168 }
3169 }
3170
3171 return D3DPOOL_DEFAULT;
3172}
3173
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003174bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3175{
3176 if (source && dest)
3177 {
3178 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003179
3180 if (fromManaged)
3181 {
3182 D3DSURFACE_DESC desc;
3183 source->GetDesc(&desc);
3184
3185 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003186 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003187
3188 if (SUCCEEDED(result))
3189 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003190 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003191 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
Geoff Langea228632013-07-30 15:17:12 -04003192 SafeRelease(surf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003193 }
3194 }
3195 else
3196 {
3197 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003198 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003199 }
3200
3201 if (FAILED(result))
3202 {
3203 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3204 return false;
3205 }
3206 }
3207
3208 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003209}
3210
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003211Image *Renderer9::createImage()
3212{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003213 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003214}
3215
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003216void Renderer9::generateMipmap(Image *dest, Image *src)
3217{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003218 Image9 *src9 = Image9::makeImage9(src);
3219 Image9 *dst9 = Image9::makeImage9(dest);
3220 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003221}
3222
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003223TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3224{
3225 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3226 return new TextureStorage9_2D(this, swapChain9);
3227}
3228
Nicolas Capensbf712d02014-03-31 14:23:35 -04003229TextureStorage *Renderer9::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003230{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003231 return new TextureStorage9_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003232}
3233
Nicolas Capensbf712d02014-03-31 14:23:35 -04003234TextureStorage *Renderer9::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003235{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003236 return new TextureStorage9_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003237}
3238
Nicolas Capensbf712d02014-03-31 14:23:35 -04003239TextureStorage *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 +00003240{
3241 // 3D textures are not supported by the D3D9 backend.
3242 UNREACHABLE();
3243
3244 return NULL;
3245}
3246
Nicolas Capensbf712d02014-03-31 14:23:35 -04003247TextureStorage *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 +00003248{
3249 // 2D array textures are not supported by the D3D9 backend.
3250 UNREACHABLE();
3251
3252 return NULL;
3253}
3254
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003255bool Renderer9::getLUID(LUID *adapterLuid) const
3256{
3257 adapterLuid->HighPart = 0;
3258 adapterLuid->LowPart = 0;
3259
3260 if (mD3d9Ex)
3261 {
3262 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3263 return true;
3264 }
3265
3266 return false;
3267}
3268
Geoff Lang005df412013-10-16 14:12:50 -04003269GLenum Renderer9::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003270{
Geoff Langcec35902014-04-16 10:52:36 -04003271 return d3d9_gl::GetInternalFormat(gl_d3d9::GetTextureFormat(internalFormat));
Jamie Madillc8c102b2013-10-10 15:10:24 -04003272}
3273
Jamie Madill95ffb862014-01-29 09:26:59 -05003274rx::VertexConversionType Renderer9::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3275{
3276 return d3d9::GetVertexConversionType(vertexFormat);
3277}
3278
3279GLenum Renderer9::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3280{
3281 D3DDECLTYPE declType = d3d9::GetNativeVertexFormat(vertexFormat);
3282 return d3d9::GetDeclTypeComponentType(declType);
3283}
3284
Geoff Langcec35902014-04-16 10:52:36 -04003285gl::Caps Renderer9::generateCaps() const
3286{
3287 return d3d9_gl::GenerateCaps(mD3d9, mDevice, mDeviceType, mAdapter);
3288}
3289
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003290}