blob: 27867bcda6e5cce370fc77723ead5872561408df [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002//
daniel@transgaming.com669c9952013-01-11 04:08:16 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00008// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009
Geoff Lang2a64ee42013-05-31 11:22:40 -040010#include "common/utilities.h"
11
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/main.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000014#include "libGLESv2/Texture.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000015#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000016#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000017#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000018#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000019#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000020#include "libGLESv2/renderer/renderer9_utils.h"
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000021#include "libGLESv2/renderer/formatutils9.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000022#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000023#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com34da3972012-12-20 21:10:29 +000024#include "libGLESv2/renderer/TextureStorage9.h"
daniel@transgaming.com4ba24062012-12-20 20:54:24 +000025#include "libGLESv2/renderer/Image9.h"
Geoff Langdce735c2013-06-04 10:21:18 -040026#include "libGLESv2/renderer/Blit9.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000027#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com3f255b42012-12-20 21:07:35 +000028#include "libGLESv2/renderer/VertexBuffer9.h"
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +000029#include "libGLESv2/renderer/IndexBuffer9.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000030#include "libGLESv2/renderer/BufferStorage9.h"
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +000031#include "libGLESv2/renderer/Query9.h"
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +000032#include "libGLESv2/renderer/Fence9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000033
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
daniel@transgaming.com621ce052012-10-31 17:52:29 +000036// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
37#define REF_RAST 0
38
39// The "Debug This Pixel..." feature in PIX often fails when using the
40// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
41// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
42#if !defined(ANGLE_ENABLE_D3D9EX)
43// Enables use of the IDirect3D9Ex interface, when available
44#define ANGLE_ENABLE_D3D9EX 1
45#endif // !defined(ANGLE_ENABLE_D3D9EX)
46
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000047#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
48#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
49#endif
50
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000051const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z')));
52const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')));
53
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000054namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000055{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000056static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000057 {
58 D3DFMT_A1R5G5B5,
59 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
60 D3DFMT_A8R8G8B8,
61 D3DFMT_R5G6B5,
62 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
63 D3DFMT_X8R8G8B8
64 };
65
daniel@transgaming.com222ee082012-11-28 19:31:49 +000066static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000067 {
68 D3DFMT_UNKNOWN,
69 // D3DFMT_D16_LOCKABLE,
70 D3DFMT_D32,
71 // D3DFMT_D15S1,
72 D3DFMT_D24S8,
73 D3DFMT_D24X8,
74 // D3DFMT_D24X4S4,
75 D3DFMT_D16,
76 // D3DFMT_D32F_LOCKABLE,
77 // D3DFMT_D24FS8
78 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000079
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000080enum
81{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +000082 MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
83 MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32,
84 MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224,
85 MAX_VARYING_VECTORS_SM2 = 8,
86 MAX_VARYING_VECTORS_SM3 = 10,
87
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000088 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
89};
90
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000091Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000092{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000093 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000094
95 mD3d9 = NULL;
96 mD3d9Ex = NULL;
97 mDevice = NULL;
98 mDeviceEx = NULL;
99 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000100 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000101
102 mAdapter = D3DADAPTER_DEFAULT;
103
104 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
105 mDeviceType = D3DDEVTYPE_REF;
106 #else
107 mDeviceType = D3DDEVTYPE_HAL;
108 #endif
109
110 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000111
112 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000113
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +0000114 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000115
116 mVertexDataManager = NULL;
117 mIndexDataManager = NULL;
118 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000119
120 mMaxNullColorbufferLRU = 0;
121 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
122 {
123 mNullColorbufferCache[i].lruCount = 0;
124 mNullColorbufferCache[i].width = 0;
125 mNullColorbufferCache[i].height = 0;
126 mNullColorbufferCache[i].buffer = NULL;
127 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000128}
129
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000130Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000131{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000132 releaseDeviceResources();
133
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000134 if (mDevice)
135 {
136 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000137 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000138 {
139 resetDevice();
140 }
141
Geoff Langea228632013-07-30 15:17:12 -0400142 SafeRelease(mDevice);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000143 }
144
Geoff Langea228632013-07-30 15:17:12 -0400145 SafeRelease(mDeviceEx);
146 SafeRelease(mD3d9);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000147
148 if (mDeviceWindow)
149 {
150 DestroyWindow(mDeviceWindow);
151 mDeviceWindow = NULL;
152 }
153
Geoff Langea228632013-07-30 15:17:12 -0400154 SafeRelease(mD3d9Ex);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000155
Geoff Langea228632013-07-30 15:17:12 -0400156 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000157}
158
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000159Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
160{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000161 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000162 return static_cast<rx::Renderer9*>(renderer);
163}
164
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000165EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000166{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000167 if (!initializeCompiler())
168 {
169 return EGL_NOT_INITIALIZED;
170 }
171
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000172 if (mSoftwareDevice)
173 {
174 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
175 }
176 else
177 {
178 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
179 }
180
181 if (mD3d9Module == NULL)
182 {
183 ERR("No D3D9 module found - aborting!\n");
184 return EGL_NOT_INITIALIZED;
185 }
186
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000187 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
188 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
189
190 // Use Direct3D9Ex if available. Among other things, this version is less
191 // inclined to report a lost context, for example when the user switches
192 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
193 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
194 {
195 ASSERT(mD3d9Ex);
196 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
197 ASSERT(mD3d9);
198 }
199 else
200 {
201 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
202 }
203
204 if (!mD3d9)
205 {
206 ERR("Could not create D3D9 device - aborting!\n");
207 return EGL_NOT_INITIALIZED;
208 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000209
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000210 if (mDc != NULL)
211 {
212 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
213 }
214
215 HRESULT result;
216
217 // Give up on getting device caps after about one second.
218 for (int i = 0; i < 10; ++i)
219 {
220 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
221 if (SUCCEEDED(result))
222 {
223 break;
224 }
225 else if (result == D3DERR_NOTAVAILABLE)
226 {
227 Sleep(100); // Give the driver some time to initialize/recover
228 }
229 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
230 {
231 ERR("failed to get device caps (0x%x)\n", result);
232 return EGL_NOT_INITIALIZED;
233 }
234 }
235
236 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
237 {
238 ERR("Renderer does not support PS 2.0. aborting!\n");
239 return EGL_NOT_INITIALIZED;
240 }
241
242 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
243 // This is required by Texture2D::convertToRenderTarget.
244 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
245 {
246 ERR("Renderer does not support stretctrect from textures!\n");
247 return EGL_NOT_INITIALIZED;
248 }
249
250 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
251
252 // ATI cards on XP have problems with non-power-of-two textures.
253 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
254 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
255 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
256 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
257
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000258 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
259 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
260
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000261 mMinSwapInterval = 4;
262 mMaxSwapInterval = 0;
263
264 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
265 {
266 mMinSwapInterval = std::min(mMinSwapInterval, 0);
267 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
268 }
269 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
270 {
271 mMinSwapInterval = std::min(mMinSwapInterval, 1);
272 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
273 }
274 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
275 {
276 mMinSwapInterval = std::min(mMinSwapInterval, 2);
277 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
278 }
279 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
280 {
281 mMinSwapInterval = std::min(mMinSwapInterval, 3);
282 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
283 }
284 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
285 {
286 mMinSwapInterval = std::min(mMinSwapInterval, 4);
287 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
288 }
289
Geoff Lang61e49a52013-05-29 10:22:58 -0400290 mMaxSupportedSamples = 0;
291
292 const d3d9::D3DFormatSet &d3d9Formats = d3d9::GetAllUsedD3DFormats();
293 for (d3d9::D3DFormatSet::const_iterator i = d3d9Formats.begin(); i != d3d9Formats.end(); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000294 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400295 MultisampleSupportInfo support = getMultiSampleSupport(*i);
296 mMultiSampleSupport[*i] = support;
297 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
daniel@transgaming.com92955622012-10-31 18:38:41 +0000298 }
299
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000300 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
301 static const TCHAR className[] = TEXT("STATIC");
302
303 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
304
305 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
306 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
307
308 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
309 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
310 {
311 return EGL_BAD_ALLOC;
312 }
313
314 if (FAILED(result))
315 {
316 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
317
318 if (FAILED(result))
319 {
320 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
321 return EGL_BAD_ALLOC;
322 }
323 }
324
325 if (mD3d9Ex)
326 {
327 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
328 ASSERT(SUCCEEDED(result));
329 }
330
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000331 mVertexShaderCache.initialize(mDevice);
332 mPixelShaderCache.initialize(mDevice);
333
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000334 // Check occlusion query support
335 IDirect3DQuery9 *occlusionQuery = NULL;
336 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
337 {
Geoff Langea228632013-07-30 15:17:12 -0400338 SafeRelease(occlusionQuery);
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000339 mOcclusionQuerySupport = true;
340 }
341 else
342 {
343 mOcclusionQuerySupport = false;
344 }
345
346 // Check event query support
347 IDirect3DQuery9 *eventQuery = NULL;
348 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
349 {
Geoff Langea228632013-07-30 15:17:12 -0400350 SafeRelease(eventQuery);
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000351 mEventQuerySupport = true;
352 }
353 else
354 {
355 mEventQuerySupport = false;
356 }
357
358 D3DDISPLAYMODE currentDisplayMode;
359 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
360
361 // Check vertex texture support
362 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
363 // We test this using D3D9 by checking support for the R16F format.
364 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
365 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
366 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
367
Geoff Langd42cf4e2013-06-05 16:09:17 -0400368 // Check RGB565 texture support
369 mRGB565TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
370 D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_R5G6B5));
371
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000372 // Check depth texture support
373 // we use INTZ for depth textures in Direct3D9
374 // we also want NULL texture support to ensure the we can make depth-only FBOs
375 // see http://aras-p.info/texts/D3D9GPUHacks.html
376 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
377 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
378 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
379 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
380
381 // Check 32 bit floating point texture support
382 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
383 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
384 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
385 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
386
387 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
388 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
389 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
390 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
391
392 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
393 {
394 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
395 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
396 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
397 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
398 }
399 else
400 {
401 mFloat32TextureSupport = true;
402 }
403
404 // Check 16 bit floating point texture support
405 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
406 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
407 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
408 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
409
410 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
411 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
412 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
413 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
414
415 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
416 {
417 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
418 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
419 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
420 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
421 }
422 else
423 {
424 mFloat16TextureSupport = true;
425 }
426
427 // Check DXT texture support
428 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
429 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
430 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
431
432 // Check luminance[alpha] texture support
433 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
434 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
435
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000436 initializeDevice();
437
438 return EGL_SUCCESS;
439}
440
441// do any one-time device initialization
442// NOTE: this is also needed after a device lost/reset
443// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000444void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000445{
446 // Permanent non-default states
447 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
448 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
449
450 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
451 {
452 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
453 }
454 else
455 {
456 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
457 }
458
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000459 markAllStateDirty();
460
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000461 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000462
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000463 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400464 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000465 mVertexDataManager = new rx::VertexDataManager(this);
466 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000467}
468
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000469D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000470{
471 D3DPRESENT_PARAMETERS presentParameters = {0};
472
473 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
474 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
475 presentParameters.BackBufferCount = 1;
476 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
477 presentParameters.BackBufferWidth = 1;
478 presentParameters.BackBufferHeight = 1;
479 presentParameters.EnableAutoDepthStencil = FALSE;
480 presentParameters.Flags = 0;
481 presentParameters.hDeviceWindow = mDeviceWindow;
482 presentParameters.MultiSampleQuality = 0;
483 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
484 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
485 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
486 presentParameters.Windowed = TRUE;
487
488 return presentParameters;
489}
490
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000491int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000492{
493 D3DDISPLAYMODE currentDisplayMode;
494 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
495
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000496 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
497 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000498 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
499 int numConfigs = 0;
500
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000501 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000502 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000503 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000504
505 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
506
507 if (SUCCEEDED(result))
508 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000509 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000510 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000511 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000512 HRESULT result = D3D_OK;
513
514 if(depthStencilFormat != D3DFMT_UNKNOWN)
515 {
516 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
517 }
518
519 if (SUCCEEDED(result))
520 {
521 if(depthStencilFormat != D3DFMT_UNKNOWN)
522 {
523 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
524 }
525
526 if (SUCCEEDED(result))
527 {
528 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000529 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
530 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000531 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
532 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000533 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000534
535 (*configDescList)[numConfigs++] = newConfig;
536 }
537 }
538 }
539 }
540 }
541
542 return numConfigs;
543}
544
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000545void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000546{
547 delete [] (configDescList);
548}
549
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000550void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000551{
552 if (!mSceneStarted)
553 {
554 long result = mDevice->BeginScene();
555 if (SUCCEEDED(result)) {
556 // This is defensive checking against the device being
557 // lost at unexpected times.
558 mSceneStarted = true;
559 }
560 }
561}
562
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000563void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000564{
565 if (mSceneStarted)
566 {
567 // EndScene can fail if the device was lost, for example due
568 // to a TDR during a draw call.
569 mDevice->EndScene();
570 mSceneStarted = false;
571 }
572}
573
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000574void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000575{
576 HRESULT result;
577
578 IDirect3DQuery9* query = allocateEventQuery();
579 if (!query)
580 {
581 return;
582 }
583
584 result = query->Issue(D3DISSUE_END);
585 ASSERT(SUCCEEDED(result));
586
587 do
588 {
589 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
590
591 if(block && result == S_FALSE)
592 {
593 // Keep polling, but allow other threads to do something useful first
594 Sleep(0);
595 // explicitly check for device loss
596 // some drivers seem to return S_FALSE even if the device is lost
597 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000598 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000599 {
600 result = D3DERR_DEVICELOST;
601 }
602 }
603 }
604 while(block && result == S_FALSE);
605
606 freeEventQuery(query);
607
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000608 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000609 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000610 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000611 }
612}
613
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000614SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
615{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000616 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000617}
618
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000619IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000620{
621 IDirect3DQuery9 *query = NULL;
622
623 if (mEventQueryPool.empty())
624 {
625 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
626 ASSERT(SUCCEEDED(result));
627 }
628 else
629 {
630 query = mEventQueryPool.back();
631 mEventQueryPool.pop_back();
632 }
633
634 return query;
635}
636
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000637void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000638{
639 if (mEventQueryPool.size() > 1000)
640 {
Geoff Langea228632013-07-30 15:17:12 -0400641 SafeRelease(query);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000642 }
643 else
644 {
645 mEventQueryPool.push_back(query);
646 }
647}
648
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000649IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000650{
651 return mVertexShaderCache.create(function, length);
652}
653
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000654IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000655{
656 return mPixelShaderCache.create(function, length);
657}
658
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000659HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
660{
661 D3DPOOL Pool = getBufferPool(Usage);
662 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
663}
664
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000665VertexBuffer *Renderer9::createVertexBuffer()
666{
667 return new VertexBuffer9(this);
668}
669
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000670HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
671{
672 D3DPOOL Pool = getBufferPool(Usage);
673 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
674}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000675
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000676IndexBuffer *Renderer9::createIndexBuffer()
677{
678 return new IndexBuffer9(this);
679}
680
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000681BufferStorage *Renderer9::createBufferStorage()
682{
683 return new BufferStorage9();
684}
685
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000686QueryImpl *Renderer9::createQuery(GLenum type)
687{
688 return new Query9(this, type);
689}
690
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000691FenceImpl *Renderer9::createFence()
692{
693 return new Fence9(this);
694}
695
Jamie Madilla21eea32013-09-18 14:36:25 -0400696bool Renderer9::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
697 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
698{
699 // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
700 UNREACHABLE();
701 return false;
702}
703
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000704void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000705{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000706 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
707 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000708
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000709 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000710 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000711 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
712 int d3dSampler = index + d3dSamplerOffset;
713
714 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
715 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
716
717 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
718 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
719 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
720 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
721 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
722 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
723 if (mSupportsTextureFilterAnisotropy)
724 {
725 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
726 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000727 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000728
729 forceSetSamplers[index] = false;
730 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000731}
732
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000733void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000734{
735 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
736 int d3dSampler = index + d3dSamplerOffset;
737 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000738 unsigned int serial = 0;
739 bool forceSetTexture = false;
740
741 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000742
743 if (texture)
744 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000745 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000746 if (texStorage)
747 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000748 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000749 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000750 }
751 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000752 // in the texture class and we're unexpectedly missing the d3d texture
753 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000754
755 serial = texture->getTextureSerial();
756 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000757 }
758
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000759 if (forceSetTexture || appliedSerials[index] != serial)
760 {
761 mDevice->SetTexture(d3dSampler, d3dTexture);
762 }
763
764 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000765}
766
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000767bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
768{
769 // No effect in ES2/D3D9
770 return true;
771}
772
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000773void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000774{
775 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000776
777 if (rasterStateChanged)
778 {
779 // Set the cull mode
780 if (rasterState.cullFace)
781 {
782 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
783 }
784 else
785 {
786 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
787 }
788
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000789 if (rasterState.polygonOffsetFill)
790 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000791 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000792 {
793 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
794
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000795 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000796 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
797 }
798 }
799 else
800 {
801 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
802 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
803 }
804
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000805 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000806 }
807
808 mForceSetRasterState = false;
809}
810
Geoff Lang2a64ee42013-05-31 11:22:40 -0400811void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor, unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000812{
813 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400814 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000815 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
816
817 if (blendStateChanged || blendColorChanged)
818 {
819 if (blendState.blend)
820 {
821 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
822
823 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
824 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
825 {
826 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
827 }
828 else
829 {
830 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
831 gl::unorm<8>(blendColor.alpha),
832 gl::unorm<8>(blendColor.alpha),
833 gl::unorm<8>(blendColor.alpha)));
834 }
835
836 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
837 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
838 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
839
840 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
841 blendState.destBlendRGB != blendState.destBlendAlpha ||
842 blendState.blendEquationRGB != blendState.blendEquationAlpha)
843 {
844 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
845
846 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
847 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
848 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
849 }
850 else
851 {
852 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
853 }
854 }
855 else
856 {
857 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
858 }
859
860 if (blendState.sampleAlphaToCoverage)
861 {
862 FIXME("Sample alpha to coverage is unimplemented.");
863 }
864
865 // Set the color mask
866 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
867 // Apparently some ATI cards have a bug where a draw with a zero color
868 // write mask can cause later draws to have incorrect results. Instead,
869 // set a nonzero color write mask but modify the blend state so that no
870 // drawing is done.
871 // http://code.google.com/p/angleproject/issues/detail?id=169
872
873 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
874 blendState.colorMaskBlue, blendState.colorMaskAlpha);
875 if (colorMask == 0 && !zeroColorMaskAllowed)
876 {
877 // Enable green channel, but set blending so nothing will be drawn.
878 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
879 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
880
881 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
882 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
883 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
884 }
885 else
886 {
887 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
888 }
889
890 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
891
892 mCurBlendState = blendState;
893 mCurBlendColor = blendColor;
894 }
895
896 if (sampleMaskChanged)
897 {
898 // Set the multisample mask
899 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
900 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
901
902 mCurSampleMask = sampleMask;
903 }
904
905 mForceSetBlendState = false;
906}
907
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000908void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000909 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000910{
911 bool depthStencilStateChanged = mForceSetDepthStencilState ||
912 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000913 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
914 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000915 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000916
917 if (depthStencilStateChanged)
918 {
919 if (depthStencilState.depthTest)
920 {
921 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
922 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
923 }
924 else
925 {
926 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
927 }
928
929 mCurDepthStencilState = depthStencilState;
930 }
931
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000932 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000933 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000934 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000935 {
936 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
937 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
938
939 // FIXME: Unsupported by D3D9
940 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
941 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
942 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
943 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000944 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000945 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
946 {
947 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 +0000948 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000949 }
950
951 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000952 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000953
954 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
955 depthStencilState.stencilWritemask);
956 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
957 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
958
959 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000960 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000961 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
962 depthStencilState.stencilMask);
963
964 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
965 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
966 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
967 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
968 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
969 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
970
971 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
972 depthStencilState.stencilBackWritemask);
973 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
974 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
975
976 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000977 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000978 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
979 depthStencilState.stencilBackMask);
980
981 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
982 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
983 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
984 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
985 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
986 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
987 }
988 else
989 {
990 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
991 }
992
993 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
994
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000995 mCurStencilRef = stencilRef;
996 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000997 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000998 }
999
1000 mForceSetDepthStencilState = false;
1001}
1002
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001003void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001004{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001005 bool scissorChanged = mForceSetScissor ||
1006 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1007 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001008
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001009 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001010 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001011 if (enabled)
1012 {
1013 RECT rect;
1014 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1015 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1016 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1017 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1018 mDevice->SetScissorRect(&rect);
1019 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001020
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001021 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1022
1023 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001024 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001025 }
1026
1027 mForceSetScissor = false;
1028}
1029
daniel@transgaming.com12985182012-12-20 20:56:31 +00001030bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001031 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001032{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001033 gl::Rectangle actualViewport = viewport;
1034 float actualZNear = gl::clamp01(zNear);
1035 float actualZFar = gl::clamp01(zFar);
1036 if (ignoreViewport)
1037 {
1038 actualViewport.x = 0;
1039 actualViewport.y = 0;
1040 actualViewport.width = mRenderTargetDesc.width;
1041 actualViewport.height = mRenderTargetDesc.height;
1042 actualZNear = 0.0f;
1043 actualZFar = 1.0f;
1044 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001045
1046 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001047 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1048 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1049 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1050 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1051 dxViewport.MinZ = actualZNear;
1052 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001053
1054 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1055 {
1056 return false; // Nothing to render
1057 }
1058
Geoff Langde14d602013-08-14 12:28:33 -04001059 float depthFront = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
1060
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001061 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
Geoff Langde14d602013-08-14 12:28:33 -04001062 actualZNear != mCurNear || actualZFar != mCurFar || mCurDepthFront != depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001063 if (viewportChanged)
1064 {
1065 mDevice->SetViewport(&dxViewport);
1066
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001067 mCurViewport = actualViewport;
1068 mCurNear = actualZNear;
1069 mCurFar = actualZFar;
Geoff Langde14d602013-08-14 12:28:33 -04001070 mCurDepthFront = depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001071
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001072 dx_VertexConstants vc = {0};
1073 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001074
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001075 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1076 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1077 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1078 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001079
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001080 pc.viewCoords[0] = actualViewport.width * 0.5f;
1081 pc.viewCoords[1] = actualViewport.height * 0.5f;
1082 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1083 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001084
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001085 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1086 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
Geoff Langde14d602013-08-14 12:28:33 -04001087 pc.depthFront[2] = depthFront;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001088
1089 vc.depthRange[0] = actualZNear;
1090 vc.depthRange[1] = actualZFar;
1091 vc.depthRange[2] = actualZFar - actualZNear;
1092
1093 pc.depthRange[0] = actualZNear;
1094 pc.depthRange[1] = actualZFar;
1095 pc.depthRange[2] = actualZFar - actualZNear;
1096
1097 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1098 {
1099 mVertexConstants = vc;
1100 mDxUniformsDirty = true;
1101 }
1102
1103 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1104 {
1105 mPixelConstants = pc;
1106 mDxUniformsDirty = true;
1107 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001108 }
1109
1110 mForceSetViewport = false;
1111 return true;
1112}
1113
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001114bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1115{
1116 switch (mode)
1117 {
1118 case GL_POINTS:
1119 mPrimitiveType = D3DPT_POINTLIST;
1120 mPrimitiveCount = count;
1121 break;
1122 case GL_LINES:
1123 mPrimitiveType = D3DPT_LINELIST;
1124 mPrimitiveCount = count / 2;
1125 break;
1126 case GL_LINE_LOOP:
1127 mPrimitiveType = D3DPT_LINESTRIP;
1128 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1129 break;
1130 case GL_LINE_STRIP:
1131 mPrimitiveType = D3DPT_LINESTRIP;
1132 mPrimitiveCount = count - 1;
1133 break;
1134 case GL_TRIANGLES:
1135 mPrimitiveType = D3DPT_TRIANGLELIST;
1136 mPrimitiveCount = count / 3;
1137 break;
1138 case GL_TRIANGLE_STRIP:
1139 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1140 mPrimitiveCount = count - 2;
1141 break;
1142 case GL_TRIANGLE_FAN:
1143 mPrimitiveType = D3DPT_TRIANGLEFAN;
1144 mPrimitiveCount = count - 2;
1145 break;
1146 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001147 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001148 }
1149
1150 return mPrimitiveCount > 0;
1151}
1152
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001153
1154gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1155{
1156 if (!depthbuffer)
1157 {
1158 ERR("Unexpected null depthbuffer for depth-only FBO.");
1159 return NULL;
1160 }
1161
1162 GLsizei width = depthbuffer->getWidth();
1163 GLsizei height = depthbuffer->getHeight();
1164
1165 // search cached nullcolorbuffers
1166 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1167 {
1168 if (mNullColorbufferCache[i].buffer != NULL &&
1169 mNullColorbufferCache[i].width == width &&
1170 mNullColorbufferCache[i].height == height)
1171 {
1172 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1173 return mNullColorbufferCache[i].buffer;
1174 }
1175 }
1176
1177 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1178
1179 // add nullbuffer to the cache
1180 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1181 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1182 {
1183 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1184 {
1185 oldest = &mNullColorbufferCache[i];
1186 }
1187 }
1188
1189 delete oldest->buffer;
1190 oldest->buffer = nullbuffer;
1191 oldest->lruCount = ++mMaxNullColorbufferLRU;
1192 oldest->width = width;
1193 oldest->height = height;
1194
1195 return nullbuffer;
1196}
1197
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001198bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001199{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001200 // if there is no color attachment we must synthesize a NULL colorattachment
1201 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1202 gl::Renderbuffer *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001203 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001204 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001205 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001206 }
1207 else
1208 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001209 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001210 }
1211 if (!renderbufferObject)
1212 {
1213 ERR("unable to locate renderbuffer for FBO.");
1214 return false;
1215 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001216
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001217 bool renderTargetChanged = false;
1218 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1219 if (renderTargetSerial != mAppliedRenderTargetSerial)
1220 {
1221 // Apply the render target on the device
1222 IDirect3DSurface9 *renderTargetSurface = NULL;
1223
1224 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1225 if (renderTarget)
1226 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001227 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001228 }
1229
1230 if (!renderTargetSurface)
1231 {
1232 ERR("render target pointer unexpectedly null.");
1233 return false; // Context must be lost
1234 }
1235
1236 mDevice->SetRenderTarget(0, renderTargetSurface);
Geoff Langea228632013-07-30 15:17:12 -04001237 SafeRelease(renderTargetSurface);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001238
1239 mAppliedRenderTargetSerial = renderTargetSerial;
1240 renderTargetChanged = true;
1241 }
1242
1243 gl::Renderbuffer *depthStencil = NULL;
1244 unsigned int depthbufferSerial = 0;
1245 unsigned int stencilbufferSerial = 0;
1246 if (framebuffer->getDepthbufferType() != GL_NONE)
1247 {
1248 depthStencil = framebuffer->getDepthbuffer();
1249 if (!depthStencil)
1250 {
1251 ERR("Depth stencil pointer unexpectedly null.");
1252 return false;
1253 }
1254
1255 depthbufferSerial = depthStencil->getSerial();
1256 }
1257 else if (framebuffer->getStencilbufferType() != GL_NONE)
1258 {
1259 depthStencil = framebuffer->getStencilbuffer();
1260 if (!depthStencil)
1261 {
1262 ERR("Depth stencil pointer unexpectedly null.");
1263 return false;
1264 }
1265
1266 stencilbufferSerial = depthStencil->getSerial();
1267 }
1268
1269 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1270 stencilbufferSerial != mAppliedStencilbufferSerial ||
1271 !mDepthStencilInitialized)
1272 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001273 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001274 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001275
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001276 // Apply the depth stencil on the device
1277 if (depthStencil)
1278 {
1279 IDirect3DSurface9 *depthStencilSurface = NULL;
1280 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1281
1282 if (depthStencilRenderTarget)
1283 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001284 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001285 }
1286
1287 if (!depthStencilSurface)
1288 {
1289 ERR("depth stencil pointer unexpectedly null.");
1290 return false; // Context must be lost
1291 }
1292
1293 mDevice->SetDepthStencilSurface(depthStencilSurface);
Geoff Langea228632013-07-30 15:17:12 -04001294 SafeRelease(depthStencilSurface);
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001295
1296 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001297 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001298 }
1299 else
1300 {
1301 mDevice->SetDepthStencilSurface(NULL);
1302 }
1303
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001304 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1305 {
1306 mCurDepthSize = depthSize;
1307 mForceSetRasterState = true;
1308 }
1309
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001310 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1311 {
1312 mCurStencilSize = stencilSize;
1313 mForceSetDepthStencilState = true;
1314 }
1315
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001316 mAppliedDepthbufferSerial = depthbufferSerial;
1317 mAppliedStencilbufferSerial = stencilbufferSerial;
1318 mDepthStencilInitialized = true;
1319 }
1320
1321 if (renderTargetChanged || !mRenderTargetDescInitialized)
1322 {
1323 mForceSetScissor = true;
1324 mForceSetViewport = true;
1325
1326 mRenderTargetDesc.width = renderbufferObject->getWidth();
1327 mRenderTargetDesc.height = renderbufferObject->getHeight();
1328 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1329 mRenderTargetDescInitialized = true;
1330 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001331
1332 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001333}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001334
Jamie Madill57a89722013-07-02 11:57:03 -04001335GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001336 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001337{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001338 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001339 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001340 if (err != GL_NO_ERROR)
1341 {
1342 return err;
1343 }
1344
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001345 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1346}
1347
1348// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001349GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001350{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001351 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001352
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001353 if (err == GL_NO_ERROR)
1354 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001355 // Directly binding the storage buffer is not supported for d3d9
1356 ASSERT(indexInfo->storage == NULL);
1357
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001358 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001359 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001360 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1361
1362 mDevice->SetIndices(indexBuffer->getBuffer());
1363 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001364 }
1365 }
1366
1367 return err;
1368}
1369
1370void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1371{
1372 startScene();
1373
1374 if (mode == GL_LINE_LOOP)
1375 {
1376 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1377 }
1378 else if (instances > 0)
1379 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001380 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001381 if (countingIB)
1382 {
1383 if (mAppliedIBSerial != countingIB->getSerial())
1384 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001385 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1386
1387 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001388 mAppliedIBSerial = countingIB->getSerial();
1389 }
1390
1391 for (int i = 0; i < mRepeatDraw; i++)
1392 {
1393 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1394 }
1395 }
1396 else
1397 {
1398 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001399 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001400 }
1401 }
1402 else // Regular case
1403 {
1404 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1405 }
1406}
1407
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001408void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001409{
1410 startScene();
1411
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001412 if (mode == GL_POINTS)
1413 {
1414 drawIndexedPoints(count, type, indices, elementArrayBuffer);
1415 }
1416 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001417 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001418 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001419 }
1420 else
1421 {
1422 for (int i = 0; i < mRepeatDraw; i++)
1423 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001424 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1425 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001426 }
1427 }
1428}
1429
1430void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1431{
1432 // Get the raw indices for an indexed draw
1433 if (type != GL_NONE && elementArrayBuffer)
1434 {
1435 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001436 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001437 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001438 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001439 }
1440
Geoff Langa36ead42013-08-02 11:54:08 -04001441 unsigned int startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001442
1443 if (get32BitIndexSupport())
1444 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001445 if (!mLineLoopIB)
1446 {
1447 mLineLoopIB = new StreamingIndexBufferInterface(this);
1448 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1449 {
1450 delete mLineLoopIB;
1451 mLineLoopIB = NULL;
1452
1453 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001454 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001455 }
1456 }
1457
Geoff Lang57e713e2013-07-31 17:01:58 -04001458 // Checked by Renderer9::applyPrimitiveType
1459 ASSERT(count >= 0);
1460
1461 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001462 {
1463 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1464 return gl::error(GL_OUT_OF_MEMORY);
1465 }
1466
Geoff Lang57e713e2013-07-31 17:01:58 -04001467 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001468 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001469 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001470 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001471 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001472 }
1473
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001474 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001475 unsigned int offset = 0;
1476 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001477 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001478 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001479 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001480 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001481
Geoff Langa36ead42013-08-02 11:54:08 -04001482 startIndex = static_cast<unsigned int>(offset) / 4;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001483 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1484
1485 switch (type)
1486 {
1487 case GL_NONE: // Non-indexed draw
1488 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001489 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001490 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001491 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001492 data[count] = 0;
1493 break;
1494 case GL_UNSIGNED_BYTE:
1495 for (int i = 0; i < count; i++)
1496 {
1497 data[i] = static_cast<const GLubyte*>(indices)[i];
1498 }
1499 data[count] = static_cast<const GLubyte*>(indices)[0];
1500 break;
1501 case GL_UNSIGNED_SHORT:
1502 for (int i = 0; i < count; i++)
1503 {
1504 data[i] = static_cast<const GLushort*>(indices)[i];
1505 }
1506 data[count] = static_cast<const GLushort*>(indices)[0];
1507 break;
1508 case GL_UNSIGNED_INT:
1509 for (int i = 0; i < count; i++)
1510 {
1511 data[i] = static_cast<const GLuint*>(indices)[i];
1512 }
1513 data[count] = static_cast<const GLuint*>(indices)[0];
1514 break;
1515 default: UNREACHABLE();
1516 }
1517
1518 if (!mLineLoopIB->unmapBuffer())
1519 {
1520 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001521 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001522 }
1523 }
1524 else
1525 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001526 if (!mLineLoopIB)
1527 {
1528 mLineLoopIB = new StreamingIndexBufferInterface(this);
1529 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1530 {
1531 delete mLineLoopIB;
1532 mLineLoopIB = NULL;
1533
1534 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001535 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001536 }
1537 }
1538
Geoff Lang57e713e2013-07-31 17:01:58 -04001539 // Checked by Renderer9::applyPrimitiveType
1540 ASSERT(count >= 0);
1541
1542 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001543 {
1544 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1545 return gl::error(GL_OUT_OF_MEMORY);
1546 }
1547
Geoff Lang57e713e2013-07-31 17:01:58 -04001548 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001549 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001550 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001551 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001552 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001553 }
1554
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001555 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001556 unsigned int offset;
1557 if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001558 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001559 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001560 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001561 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001562
Geoff Langa36ead42013-08-02 11:54:08 -04001563 startIndex = static_cast<unsigned int>(offset) / 2;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001564 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1565
1566 switch (type)
1567 {
1568 case GL_NONE: // Non-indexed draw
1569 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001570 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001571 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001572 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001573 data[count] = 0;
1574 break;
1575 case GL_UNSIGNED_BYTE:
1576 for (int i = 0; i < count; i++)
1577 {
1578 data[i] = static_cast<const GLubyte*>(indices)[i];
1579 }
1580 data[count] = static_cast<const GLubyte*>(indices)[0];
1581 break;
1582 case GL_UNSIGNED_SHORT:
1583 for (int i = 0; i < count; i++)
1584 {
1585 data[i] = static_cast<const GLushort*>(indices)[i];
1586 }
1587 data[count] = static_cast<const GLushort*>(indices)[0];
1588 break;
1589 case GL_UNSIGNED_INT:
1590 for (int i = 0; i < count; i++)
1591 {
1592 data[i] = static_cast<const GLuint*>(indices)[i];
1593 }
1594 data[count] = static_cast<const GLuint*>(indices)[0];
1595 break;
1596 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001597 }
1598
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001599 if (!mLineLoopIB->unmapBuffer())
1600 {
1601 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001602 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001603 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001604 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001605
1606 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001607 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001608 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1609
1610 mDevice->SetIndices(indexBuffer->getBuffer());
1611 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001612 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001613
1614 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001615}
1616
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001617template <typename T>
1618static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1619{
1620 for (int i = 0; i < count; i++)
1621 {
1622 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1623 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1624 }
1625}
1626
1627void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1628{
1629 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1630 // for each individual point. This call is not expected to happen often.
1631
1632 if (elementArrayBuffer)
1633 {
1634 BufferStorage *storage = elementArrayBuffer->getStorage();
1635 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1636 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1637 }
1638
1639 switch (type)
1640 {
1641 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1642 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1643 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1644 default: UNREACHABLE();
1645 }
1646}
1647
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001648void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1649{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001650 unsigned int programBinarySerial = programBinary->getSerial();
1651 if (programBinarySerial != mAppliedProgramBinarySerial)
1652 {
1653 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1654 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001655
daniel@transgaming.come4991412012-12-20 20:55:34 +00001656 IDirect3DVertexShader9 *vertexShader = NULL;
1657 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001658
daniel@transgaming.come4991412012-12-20 20:55:34 +00001659 IDirect3DPixelShader9 *pixelShader = NULL;
1660 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001661
daniel@transgaming.come4991412012-12-20 20:55:34 +00001662 mDevice->SetPixelShader(pixelShader);
1663 mDevice->SetVertexShader(vertexShader);
1664 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001665 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001666
1667 mAppliedProgramBinarySerial = programBinarySerial;
1668 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001669}
1670
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001671void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001672{
1673 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1674 {
1675 gl::Uniform *targetUniform = *ub;
1676
1677 if (targetUniform->dirty)
1678 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001679 GLfloat *f = (GLfloat*)targetUniform->data;
1680 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001681
1682 switch (targetUniform->type)
1683 {
1684 case GL_SAMPLER_2D:
1685 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001686 break;
1687 case GL_BOOL:
1688 case GL_BOOL_VEC2:
1689 case GL_BOOL_VEC3:
1690 case GL_BOOL_VEC4:
1691 applyUniformnbv(targetUniform, i);
1692 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001693 case GL_FLOAT:
1694 case GL_FLOAT_VEC2:
1695 case GL_FLOAT_VEC3:
1696 case GL_FLOAT_VEC4:
1697 case GL_FLOAT_MAT2:
1698 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001699 case GL_FLOAT_MAT4:
1700 applyUniformnfv(targetUniform, f);
1701 break;
1702 case GL_INT:
1703 case GL_INT_VEC2:
1704 case GL_INT_VEC3:
1705 case GL_INT_VEC4:
1706 applyUniformniv(targetUniform, i);
1707 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001708 default:
1709 UNREACHABLE();
1710 }
1711
1712 targetUniform->dirty = false;
1713 }
1714 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001715
1716 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001717 if (mDxUniformsDirty)
1718 {
1719 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1720 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1721 mDxUniformsDirty = false;
1722 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001723}
1724
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001725void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001726{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001727 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001728 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001729 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001730 }
1731
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001732 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001733 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001734 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001735 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736}
1737
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001738void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001740 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1741 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001743 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001744 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001745 vector[i][0] = (GLfloat)v[4 * i + 0];
1746 vector[i][1] = (GLfloat)v[4 * i + 1];
1747 vector[i][2] = (GLfloat)v[4 * i + 2];
1748 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001749 }
1750
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001751 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001752}
1753
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001754void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001755{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001756 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1757 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001758
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001759 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001760 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001761 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1762 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1763 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1764 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001765 }
1766
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001767 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001768}
1769
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001770void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001771{
Geoff Langda507fe2013-08-20 12:01:42 -04001772 if (clearParams.colorClearType != GL_FLOAT)
1773 {
1774 // Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0
1775 UNREACHABLE();
1776 return;
1777 }
1778
1779 bool clearColor = clearParams.clearColor[0];
1780 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1781 {
1782 if (clearParams.clearColor[i] != clearColor)
1783 {
1784 // Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0
1785 UNREACHABLE();
1786 return;
1787 }
1788 }
1789
1790 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorFClearValue.alpha),
1791 gl::unorm<8>(clearParams.colorFClearValue.red),
1792 gl::unorm<8>(clearParams.colorFClearValue.green),
1793 gl::unorm<8>(clearParams.colorFClearValue.blue));
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001794 float depth = gl::clamp01(clearParams.depthClearValue);
1795 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001796
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001797 unsigned int stencilUnmasked = 0x0;
Geoff Langda507fe2013-08-20 12:01:42 -04001798 if (clearParams.clearStencil && frameBuffer->hasStencil())
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001799 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001800 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1801 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001802 stencilUnmasked = (0x1 << stencilSize) - 1;
1803 }
1804
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001805 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1806 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001807
Geoff Langda507fe2013-08-20 12:01:42 -04001808 const bool needMaskedStencilClear = clearParams.clearStencil &&
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001809 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
Geoff Langda507fe2013-08-20 12:01:42 -04001810 const bool needMaskedColorClear = clearColor && !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1811 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001812
1813 if (needMaskedColorClear || needMaskedStencilClear)
1814 {
1815 // State which is altered in all paths from this point to the clear call is saved.
1816 // State which is altered in only some paths will be flagged dirty in the case that
1817 // that path is taken.
1818 HRESULT hr;
1819 if (mMaskedClearSavedState == NULL)
1820 {
1821 hr = mDevice->BeginStateBlock();
1822 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1823
1824 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1825 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1826 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1827 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1828 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1829 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1830 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1831 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1832 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1833 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1834 mDevice->SetPixelShader(NULL);
1835 mDevice->SetVertexShader(NULL);
1836 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1837 mDevice->SetStreamSource(0, NULL, 0, 0);
1838 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1839 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1840 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1841 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1842 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1843 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1844 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1845
1846 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1847 {
1848 mDevice->SetStreamSourceFreq(i, 1);
1849 }
1850
1851 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1852 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1853 }
1854
1855 ASSERT(mMaskedClearSavedState != NULL);
1856
1857 if (mMaskedClearSavedState != NULL)
1858 {
1859 hr = mMaskedClearSavedState->Capture();
1860 ASSERT(SUCCEEDED(hr));
1861 }
1862
1863 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1864 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1865 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1866 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1867 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1868 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1869 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1870 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1871
Geoff Langda507fe2013-08-20 12:01:42 -04001872 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001873 {
1874 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1875 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1876 clearParams.colorMaskGreen,
1877 clearParams.colorMaskBlue,
1878 clearParams.colorMaskAlpha));
1879 }
1880 else
1881 {
1882 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1883 }
1884
Geoff Langda507fe2013-08-20 12:01:42 -04001885 if (stencilUnmasked != 0x0 && clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001886 {
1887 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1888 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1889 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1890 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1891 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1892 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1893 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1894 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1895 }
1896 else
1897 {
1898 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1899 }
1900
1901 mDevice->SetPixelShader(NULL);
1902 mDevice->SetVertexShader(NULL);
1903 mDevice->SetFVF(D3DFVF_XYZRHW);
1904 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1905 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1906 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1907 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1908 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1909 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1910 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1911
1912 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1913 {
1914 mDevice->SetStreamSourceFreq(i, 1);
1915 }
1916
1917 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1918 quad[0][0] = -0.5f;
1919 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1920 quad[0][2] = 0.0f;
1921 quad[0][3] = 1.0f;
1922
1923 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1924 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1925 quad[1][2] = 0.0f;
1926 quad[1][3] = 1.0f;
1927
1928 quad[2][0] = -0.5f;
1929 quad[2][1] = -0.5f;
1930 quad[2][2] = 0.0f;
1931 quad[2][3] = 1.0f;
1932
1933 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1934 quad[3][1] = -0.5f;
1935 quad[3][2] = 0.0f;
1936 quad[3][3] = 1.0f;
1937
1938 startScene();
1939 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1940
Geoff Langda507fe2013-08-20 12:01:42 -04001941 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001942 {
1943 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1944 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1945 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1946 }
1947
1948 if (mMaskedClearSavedState != NULL)
1949 {
1950 mMaskedClearSavedState->Apply();
1951 }
1952 }
Geoff Langda507fe2013-08-20 12:01:42 -04001953 else if (clearColor || clearParams.clearDepth || clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001954 {
1955 DWORD dxClearFlags = 0;
Geoff Langda507fe2013-08-20 12:01:42 -04001956 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001957 {
1958 dxClearFlags |= D3DCLEAR_TARGET;
1959 }
Geoff Langda507fe2013-08-20 12:01:42 -04001960 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001961 {
1962 dxClearFlags |= D3DCLEAR_ZBUFFER;
1963 }
Geoff Langda507fe2013-08-20 12:01:42 -04001964 if (clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001965 {
1966 dxClearFlags |= D3DCLEAR_STENCIL;
1967 }
1968
1969 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1970 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001971}
1972
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001973void Renderer9::markAllStateDirty()
1974{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001975 mAppliedRenderTargetSerial = 0;
1976 mAppliedDepthbufferSerial = 0;
1977 mAppliedStencilbufferSerial = 0;
1978 mDepthStencilInitialized = false;
1979 mRenderTargetDescInitialized = false;
1980
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001981 mForceSetDepthStencilState = true;
1982 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001983 mForceSetScissor = true;
1984 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001985 mForceSetBlendState = true;
1986
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001987 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001988 {
1989 mForceSetVertexSamplerStates[i] = true;
1990 mCurVertexTextureSerials[i] = 0;
1991 }
1992 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1993 {
1994 mForceSetPixelSamplerStates[i] = true;
1995 mCurPixelTextureSerials[i] = 0;
1996 }
1997
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001998 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001999 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00002000 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002001
2002 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00002003}
2004
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002005void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002006{
Geoff Langea228632013-07-30 15:17:12 -04002007 for (size_t i = 0; i < mEventQueryPool.size(); i++)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002008 {
Geoff Langea228632013-07-30 15:17:12 -04002009 SafeRelease(mEventQueryPool[i]);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002010 }
Geoff Langea228632013-07-30 15:17:12 -04002011 mEventQueryPool.clear();
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002012
Geoff Langea228632013-07-30 15:17:12 -04002013 SafeRelease(mMaskedClearSavedState);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00002014
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002015 mVertexShaderCache.clear();
2016 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002017
Geoff Langea228632013-07-30 15:17:12 -04002018 SafeDelete(mBlit);
2019 SafeDelete(mVertexDataManager);
2020 SafeDelete(mIndexDataManager);
2021 SafeDelete(mLineLoopIB);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002022
2023 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2024 {
Geoff Langea228632013-07-30 15:17:12 -04002025 SafeDelete(mNullColorbufferCache[i].buffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002026 }
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002027}
2028
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002029void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002030{
2031 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002032 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002033}
2034
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002035bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002036{
2037 return mDeviceLost;
2038}
2039
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002040// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002041bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002042{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002043 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002044
2045 if (mDeviceEx)
2046 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002047 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002048 }
2049 else if (mDevice)
2050 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002051 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002052 }
2053 else
2054 {
2055 // No device yet, so no reset required
2056 }
2057
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002058 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2059
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002060 if (isLost)
2061 {
2062 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002063 // we'll probably get this done again by notifyDeviceLost
2064 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002065 // Note that we don't want to clear the device loss status here
2066 // -- this needs to be done by resetDevice
2067 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002068 if (notify)
2069 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002070 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002071 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002072 }
2073
2074 return isLost;
2075}
2076
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002077bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002078{
2079 HRESULT status = D3D_OK;
2080
2081 if (mDeviceEx)
2082 {
2083 status = mDeviceEx->CheckDeviceState(NULL);
2084 }
2085 else if (mDevice)
2086 {
2087 status = mDevice->TestCooperativeLevel();
2088 }
2089
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002090 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002091 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002092 switch (status)
2093 {
2094 case D3DERR_DEVICENOTRESET:
2095 case D3DERR_DEVICEHUNG:
2096 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002097 case D3DERR_DEVICELOST:
2098 return (mDeviceEx != NULL);
2099 case D3DERR_DEVICEREMOVED:
2100 UNIMPLEMENTED();
2101 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002102 default:
2103 return false;
2104 }
2105}
2106
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002107bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002108{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002109 releaseDeviceResources();
2110
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002111 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2112
2113 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002114 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002115 int attempts = 3;
2116
2117 while (lost && attempts > 0)
2118 {
2119 if (mDeviceEx)
2120 {
2121 Sleep(500); // Give the graphics driver some CPU time
2122 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2123 }
2124 else
2125 {
2126 result = mDevice->TestCooperativeLevel();
2127 while (result == D3DERR_DEVICELOST)
2128 {
2129 Sleep(100); // Give the graphics driver some CPU time
2130 result = mDevice->TestCooperativeLevel();
2131 }
2132
2133 if (result == D3DERR_DEVICENOTRESET)
2134 {
2135 result = mDevice->Reset(&presentParameters);
2136 }
2137 }
2138
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002139 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002140 attempts --;
2141 }
2142
2143 if (FAILED(result))
2144 {
2145 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2146 return false;
2147 }
2148
2149 // reset device defaults
2150 initializeDevice();
2151 mDeviceLost = false;
2152
2153 return true;
2154}
2155
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002156DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002157{
2158 return mAdapterIdentifier.VendorId;
2159}
2160
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002161std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002162{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002163 std::ostringstream rendererString;
2164
2165 rendererString << mAdapterIdentifier.Description;
2166 if (getShareHandleSupport())
2167 {
2168 rendererString << " Direct3D9Ex";
2169 }
2170 else
2171 {
2172 rendererString << " Direct3D9";
2173 }
2174
2175 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2176 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2177
2178 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002179}
2180
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002181GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002182{
2183 return mAdapterIdentifier.DeviceIdentifier;
2184}
2185
Geoff Lang61e49a52013-05-29 10:22:58 -04002186Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002187{
Geoff Lang61e49a52013-05-29 10:22:58 -04002188 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002189
Geoff Lang61e49a52013-05-29 10:22:58 -04002190 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2191 {
2192 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2193 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2194
2195 if (SUCCEEDED(result))
2196 {
2197 support.supportedSamples[multiSampleIndex] = true;
2198 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2199 {
2200 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2201 }
2202 }
2203 else
2204 {
2205 support.supportedSamples[multiSampleIndex] = false;
2206 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002207 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002208
2209 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002210}
2211
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002212bool Renderer9::getBGRATextureSupport() const
2213{
2214 // DirectX 9 always supports BGRA
2215 return true;
2216}
2217
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002218bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002219{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002220 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002221}
2222
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002223bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002224{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002225 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002226}
2227
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002228bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002229{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002230 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002231}
2232
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002233bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002234{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002235 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002236}
2237
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002238bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002239{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002240 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002241}
2242
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002243bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002244{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002245 return mFloat32FilterSupport;
2246}
2247
2248bool Renderer9::getFloat32TextureRenderingSupport() const
2249{
2250 return mFloat32RenderSupport;
2251}
2252
2253bool Renderer9::getFloat16TextureSupport() const
2254{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002255 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002256}
2257
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002258bool Renderer9::getFloat16TextureFilteringSupport() const
2259{
2260 return mFloat16FilterSupport;
2261}
2262
2263bool Renderer9::getFloat16TextureRenderingSupport() const
2264{
2265 return mFloat16RenderSupport;
2266}
2267
Geoff Langd42cf4e2013-06-05 16:09:17 -04002268bool Renderer9::getRGB565TextureSupport() const
2269{
2270 return mRGB565TextureSupport;
2271}
2272
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002273bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002274{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002275 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002276}
2277
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002278bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002279{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002280 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002281}
2282
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002283bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002284{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002285 return mSupportsTextureFilterAnisotropy;
2286}
2287
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002288float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002289{
2290 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002291 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002292 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002293 }
2294 return 1.0f;
2295}
2296
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002297bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002298{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002299 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002300}
2301
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002302unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002303{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002304 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2305 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002306}
2307
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002308unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2309{
2310 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2311}
2312
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002313unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002314{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002315 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002316}
2317
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002318unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002319{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002320 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002321}
2322
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002323unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002324{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002325 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2326}
2327
2328unsigned int Renderer9::getMaxFragmentUniformVectors() const
2329{
2330 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2331
2332 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2333}
2334
2335unsigned int Renderer9::getMaxVaryingVectors() const
2336{
2337 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002338}
2339
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002340unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2341{
2342 return 0;
2343}
2344
2345unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2346{
2347 return 0;
2348}
2349
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002350unsigned int Renderer9::getReservedVertexUniformBuffers() const
2351{
2352 return 0;
2353}
2354
2355unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2356{
2357 return 0;
2358}
2359
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002360unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2361{
2362 return 0;
2363}
2364
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002365unsigned int Renderer9::getMaxUniformBufferSize() const
2366{
2367 return 0;
2368}
2369
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002370bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002371{
2372 return mSupportsNonPower2Textures;
2373}
2374
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002375bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002376{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002377 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002378}
2379
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002380bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002381{
2382 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2383}
2384
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002385bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002386{
2387 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002388 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002389}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002390
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002391bool Renderer9::getDerivativeInstructionSupport() const
2392{
2393 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2394}
2395
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002396bool Renderer9::getPostSubBufferSupport() const
2397{
2398 return true;
2399}
2400
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002401int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002402{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002403 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002404}
2405
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002406float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002407{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002408 // Point size clamped at 1.0f for SM2
2409 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002410}
2411
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002412int Renderer9::getMaxViewportDimension() const
2413{
2414 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002415 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002416 return maxTextureDimension;
2417}
2418
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002419int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002420{
2421 return (int)mDeviceCaps.MaxTextureWidth;
2422}
2423
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002424int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002425{
2426 return (int)mDeviceCaps.MaxTextureHeight;
2427}
2428
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002429int Renderer9::getMaxTextureDepth() const
2430{
2431 // 3D textures are not available in the D3D9 backend.
2432 return 1;
2433}
2434
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002435int Renderer9::getMaxTextureArrayLayers() const
2436{
2437 // 2D array textures are not available in the D3D9 backend.
2438 return 1;
2439}
2440
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002441bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002442{
2443 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2444}
2445
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002446DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002447{
2448 return mDeviceCaps.DeclTypes;
2449}
2450
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002451int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002452{
2453 return mMinSwapInterval;
2454}
2455
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002456int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002457{
2458 return mMaxSwapInterval;
2459}
2460
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002461int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002462{
2463 return mMaxSupportedSamples;
2464}
2465
Geoff Lang0e120e32013-05-29 10:23:55 -04002466GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2467{
Shannon Woodsddb785c2013-07-08 10:32:13 -04002468 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
Geoff Lang0e120e32013-05-29 10:23:55 -04002469 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2470 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2471}
2472
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002473GLsizei Renderer9::getNumSampleCounts(GLint internalFormat) const
2474{
2475 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2476 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2477
2478 unsigned int numCounts = 0;
2479 if (iter != mMultiSampleSupport.end())
2480 {
2481 const MultisampleSupportInfo& info = iter->second;
2482 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2483 {
2484 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2485 {
2486 numCounts++;
2487 }
2488 }
2489 }
2490
2491 return numCounts;
2492}
2493
2494void Renderer9::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2495{
2496 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2497 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2498
2499 if (iter != mMultiSampleSupport.end())
2500 {
2501 const MultisampleSupportInfo& info = iter->second;
2502 int bufPos = 0;
2503 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2504 {
2505 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2506 {
2507 params[bufPos++] = i;
2508 }
2509 }
2510 }
2511}
2512
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002513int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002514{
2515 if (requested == 0)
2516 {
2517 return requested;
2518 }
2519
Geoff Lang61e49a52013-05-29 10:22:58 -04002520 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002521 if (itr == mMultiSampleSupport.end())
2522 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002523 if (format == D3DFMT_UNKNOWN)
2524 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002525 return -1;
2526 }
2527
Geoff Lang61e49a52013-05-29 10:22:58 -04002528 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002529 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002530 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002531 {
2532 return i;
2533 }
2534 }
2535
2536 return -1;
2537}
2538
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002539unsigned int Renderer9::getMaxRenderTargets() const
2540{
2541 // we do not support MRT in d3d9
2542 return 1;
2543}
2544
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002545D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2546{
2547 switch (internalformat)
2548 {
2549 case GL_DEPTH_COMPONENT16:
2550 case GL_DEPTH_COMPONENT32_OES:
2551 case GL_DEPTH24_STENCIL8_OES:
2552 return D3DFMT_INTZ;
2553 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2554 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2555 return D3DFMT_DXT1;
2556 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2557 return D3DFMT_DXT3;
2558 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2559 return D3DFMT_DXT5;
2560 case GL_RGBA32F_EXT:
2561 case GL_RGB32F_EXT:
2562 case GL_ALPHA32F_EXT:
2563 case GL_LUMINANCE32F_EXT:
2564 case GL_LUMINANCE_ALPHA32F_EXT:
2565 return D3DFMT_A32B32G32R32F;
2566 case GL_RGBA16F_EXT:
2567 case GL_RGB16F_EXT:
2568 case GL_ALPHA16F_EXT:
2569 case GL_LUMINANCE16F_EXT:
2570 case GL_LUMINANCE_ALPHA16F_EXT:
2571 return D3DFMT_A16B16G16R16F;
2572 case GL_LUMINANCE8_EXT:
2573 if (getLuminanceTextureSupport())
2574 {
2575 return D3DFMT_L8;
2576 }
2577 break;
2578 case GL_LUMINANCE8_ALPHA8_EXT:
2579 if (getLuminanceAlphaTextureSupport())
2580 {
2581 return D3DFMT_A8L8;
2582 }
2583 break;
2584 case GL_RGB8_OES:
2585 case GL_RGB565:
2586 return D3DFMT_X8R8G8B8;
2587 }
2588
2589 return D3DFMT_A8R8G8B8;
2590}
2591
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002592bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002593{
2594 bool result = false;
2595
2596 if (source && dest)
2597 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002598 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2599 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002600
2601 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002602 for (int i = 0; i < levels; ++i)
2603 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002604 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2605 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002606
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002607 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002608
Geoff Langea228632013-07-30 15:17:12 -04002609 SafeRelease(srcSurf);
2610 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002611
2612 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002613 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002614 return false;
Geoff Langea228632013-07-30 15:17:12 -04002615 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002616 }
2617 }
2618
2619 return result;
2620}
2621
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002622bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002623{
2624 bool result = false;
2625
2626 if (source && dest)
2627 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002628 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2629 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002630 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002631 for (int f = 0; f < 6; f++)
2632 {
2633 for (int i = 0; i < levels; i++)
2634 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002635 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2636 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002637
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002638 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002639
Geoff Langea228632013-07-30 15:17:12 -04002640 SafeRelease(srcSurf);
2641 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002642
2643 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002644 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002645 return false;
Geoff Langea228632013-07-30 15:17:12 -04002646 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002647 }
2648 }
2649 }
2650
2651 return result;
2652}
2653
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002654bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2655{
2656 // 3D textures are not available in the D3D9 backend.
2657 UNREACHABLE();
2658 return false;
2659}
2660
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002661bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2662{
2663 // 2D array textures are not supported by the D3D9 backend.
2664 UNREACHABLE();
2665 return false;
2666}
2667
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002668D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002669{
2670 if (mD3d9Ex != NULL)
2671 {
2672 return D3DPOOL_DEFAULT;
2673 }
2674 else
2675 {
2676 if (!(usage & D3DUSAGE_DYNAMIC))
2677 {
2678 return D3DPOOL_MANAGED;
2679 }
2680 }
2681
2682 return D3DPOOL_DEFAULT;
2683}
2684
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002685bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002686 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002687{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002688 RECT rect;
2689 rect.left = sourceRect.x;
2690 rect.top = sourceRect.y;
2691 rect.right = sourceRect.x + sourceRect.width;
2692 rect.bottom = sourceRect.y + sourceRect.height;
2693
2694 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002695}
2696
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002697bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002698 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002699{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002700 RECT rect;
2701 rect.left = sourceRect.x;
2702 rect.top = sourceRect.y;
2703 rect.right = sourceRect.x + sourceRect.width;
2704 rect.bottom = sourceRect.y + sourceRect.height;
2705
2706 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002707}
2708
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002709bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2710 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2711{
2712 // 3D textures are not available in the D3D9 backend.
2713 UNREACHABLE();
2714 return false;
2715}
2716
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002717bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2718 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2719{
2720 // 2D array textures are not available in the D3D9 backend.
2721 UNREACHABLE();
2722 return false;
2723}
2724
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002725bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002726 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002727{
Geoff Lang758d5b22013-06-11 11:42:50 -04002728 ASSERT(filter == GL_NEAREST);
2729
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002730 endScene();
2731
2732 if (blitRenderTarget)
2733 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002734 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2735 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002736 RenderTarget9 *readRenderTarget = NULL;
2737 RenderTarget9 *drawRenderTarget = NULL;
2738 IDirect3DSurface9* readSurface = NULL;
2739 IDirect3DSurface9* drawSurface = NULL;
2740
2741 if (readBuffer)
2742 {
2743 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2744 }
2745 if (drawBuffer)
2746 {
2747 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2748 }
2749
2750 if (readRenderTarget)
2751 {
2752 readSurface = readRenderTarget->getSurface();
2753 }
2754 if (drawRenderTarget)
2755 {
2756 drawSurface = drawRenderTarget->getSurface();
2757 }
2758
2759 if (!readSurface || !drawSurface)
2760 {
2761 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002762 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002763 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002764
Geoff Lang125deab2013-08-09 13:34:16 -04002765 gl::Extents srcSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
2766 gl::Extents dstSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
2767
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002768 RECT srcRect;
2769 srcRect.left = readRect.x;
2770 srcRect.right = readRect.x + readRect.width;
2771 srcRect.top = readRect.y;
2772 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002773
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002774 RECT dstRect;
2775 dstRect.left = drawRect.x;
2776 dstRect.right = drawRect.x + drawRect.width;
2777 dstRect.top = drawRect.y;
2778 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002779
Geoff Lang125deab2013-08-09 13:34:16 -04002780 // Clip the rectangles to the scissor rectangle
2781 if (scissor)
2782 {
2783 if (dstRect.left < scissor->x)
2784 {
2785 srcRect.left += (scissor->x - dstRect.left);
2786 dstRect.left = scissor->x;
2787 }
2788 if (dstRect.top < scissor->y)
2789 {
2790 srcRect.top += (scissor->y - dstRect.top);
2791 dstRect.top = scissor->y;
2792 }
2793 if (dstRect.right > scissor->x + scissor->width)
2794 {
2795 srcRect.right -= (dstRect.right - (scissor->x + scissor->width));
2796 dstRect.right = scissor->x + scissor->width;
2797 }
2798 if (dstRect.bottom > scissor->y + scissor->height)
2799 {
2800 srcRect.bottom -= (dstRect.bottom - (scissor->y + scissor->height));
2801 dstRect.bottom = scissor->y + scissor->height;
2802 }
2803 }
2804
2805 // Clip the rectangles to the destination size
2806 if (dstRect.left < 0)
2807 {
2808 srcRect.left += -dstRect.left;
2809 dstRect.left = 0;
2810 }
2811 if (dstRect.right > dstSize.width)
2812 {
2813 srcRect.right -= (dstRect.right - dstSize.width);
2814 dstRect.right = dstSize.width;
2815 }
2816 if (dstRect.top < 0)
2817 {
2818 srcRect.top += -dstRect.top;
2819 dstRect.top = 0;
2820 }
2821 if (dstRect.bottom > dstSize.height)
2822 {
2823 srcRect.bottom -= (dstRect.bottom - dstSize.height);
2824 dstRect.bottom = dstSize.height;
2825 }
2826
2827 // Clip the rectangles to the source size
2828 if (srcRect.left < 0)
2829 {
2830 dstRect.left += -srcRect.left;
2831 srcRect.left = 0;
2832 }
2833 if (srcRect.right > srcSize.width)
2834 {
2835 dstRect.right -= (srcRect.right - srcSize.width);
2836 srcRect.right = srcSize.width;
2837 }
2838 if (srcRect.top < 0)
2839 {
2840 dstRect.top += -srcRect.top;
2841 srcRect.top = 0;
2842 }
2843 if (srcRect.bottom > srcSize.height)
2844 {
2845 dstRect.bottom -= (srcRect.bottom - srcSize.height);
2846 srcRect.bottom = srcSize.height;
2847 }
2848
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002849 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002850
Geoff Langea228632013-07-30 15:17:12 -04002851 SafeRelease(readSurface);
2852 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002853
2854 if (FAILED(result))
2855 {
2856 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2857 return false;
2858 }
2859 }
2860
Geoff Lang685806d2013-06-12 11:16:36 -04002861 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002862 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002863 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2864 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2865 RenderTarget9 *readDepthStencil = NULL;
2866 RenderTarget9 *drawDepthStencil = NULL;
2867 IDirect3DSurface9* readSurface = NULL;
2868 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002869
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002870 if (readBuffer)
2871 {
2872 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2873 }
2874 if (drawBuffer)
2875 {
2876 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2877 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002878
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002879 if (readDepthStencil)
2880 {
2881 readSurface = readDepthStencil->getSurface();
2882 }
2883 if (drawDepthStencil)
2884 {
2885 drawSurface = drawDepthStencil->getSurface();
2886 }
2887
2888 if (!readSurface || !drawSurface)
2889 {
2890 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002891 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002892 }
2893
2894 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2895
Geoff Langea228632013-07-30 15:17:12 -04002896 SafeRelease(readSurface);
2897 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002898
2899 if (FAILED(result))
2900 {
2901 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2902 return false;
2903 }
2904 }
2905
2906 return true;
2907}
2908
2909void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2910 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2911{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002912 RenderTarget9 *renderTarget = NULL;
2913 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002914 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002915
2916 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002917 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002918 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2919 }
2920
2921 if (renderTarget)
2922 {
2923 surface = renderTarget->getSurface();
2924 }
2925
2926 if (!surface)
2927 {
2928 // context must be lost
2929 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002930 }
2931
2932 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002933 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002934
2935 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2936 {
2937 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
Geoff Langea228632013-07-30 15:17:12 -04002938 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002939 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002940 }
2941
2942 HRESULT result;
2943 IDirect3DSurface9 *systemSurface = NULL;
2944 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2945 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2946 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2947 if (directToPixels)
2948 {
2949 // Use the pixels ptr as a shared handle to write directly into client's memory
2950 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2951 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2952 if (FAILED(result))
2953 {
2954 // Try again without the shared handle
2955 directToPixels = false;
2956 }
2957 }
2958
2959 if (!directToPixels)
2960 {
2961 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2962 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2963 if (FAILED(result))
2964 {
2965 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
Geoff Langea228632013-07-30 15:17:12 -04002966 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002967 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002968 }
2969 }
2970
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002971 result = mDevice->GetRenderTargetData(surface, systemSurface);
Geoff Langea228632013-07-30 15:17:12 -04002972 SafeRelease(surface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002973
2974 if (FAILED(result))
2975 {
Geoff Langea228632013-07-30 15:17:12 -04002976 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002977
2978 // It turns out that D3D will sometimes produce more error
2979 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002980 if (d3d9::isDeviceLostError(result))
2981 {
2982 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002983 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002984 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002985 else
2986 {
2987 UNREACHABLE();
2988 return;
2989 }
2990
2991 }
2992
2993 if (directToPixels)
2994 {
Geoff Langea228632013-07-30 15:17:12 -04002995 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002996 return;
2997 }
2998
2999 RECT rect;
3000 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
3001 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
3002 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
3003 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
3004
3005 D3DLOCKED_RECT lock;
3006 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
3007
3008 if (FAILED(result))
3009 {
3010 UNREACHABLE();
Geoff Langea228632013-07-30 15:17:12 -04003011 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003012
3013 return; // No sensible error to generate
3014 }
3015
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003016 unsigned char *source;
3017 int inputPitch;
3018 if (packReverseRowOrder)
3019 {
3020 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
3021 inputPitch = -lock.Pitch;
3022 }
3023 else
3024 {
3025 source = (unsigned char*)lock.pBits;
3026 inputPitch = lock.Pitch;
3027 }
3028
Geoff Lang697ad3e2013-06-04 10:11:28 -04003029 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003030
Geoff Lang697ad3e2013-06-04 10:11:28 -04003031 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
3032 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3033 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003034
Geoff Lang697ad3e2013-06-04 10:11:28 -04003035 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3036
3037 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003038 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003039 // Direct copy possible
3040 unsigned char *dest = static_cast<unsigned char*>(pixels);
3041 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003042 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003043 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003044 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04003045 }
3046 else
3047 {
3048 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3049 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3050 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3051
3052 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
3053 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003054 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003055 // Fast copy is possible through some special function
3056 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003057 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003058 for (int x = 0; x < rect.right - rect.left; x++)
3059 {
3060 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3061 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3062
3063 fastCopyFunc(src, dest);
3064 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003065 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003066 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04003067 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003068 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003069 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
3070 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003071
Geoff Lang697ad3e2013-06-04 10:11:28 -04003072 gl::ColorF temp;
3073
3074 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003075 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003076 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003077 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003078 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3079 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003080
Geoff Lang697ad3e2013-06-04 10:11:28 -04003081 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3082 // will not allow the copy otherwise.
3083 readFunc(src, &temp);
3084 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003085 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003086 }
3087 }
3088 }
3089
3090 systemSurface->UnlockRect();
Geoff Langea228632013-07-30 15:17:12 -04003091 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003092}
3093
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003094RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
3095{
3096 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3097 IDirect3DSurface9 *surface = NULL;
3098 if (depth)
3099 {
3100 surface = swapChain9->getDepthStencil();
3101 }
3102 else
3103 {
3104 surface = swapChain9->getRenderTarget();
3105 }
3106
3107 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3108
3109 return renderTarget;
3110}
3111
Geoff Langa2d97f12013-06-11 11:44:02 -04003112RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003113{
3114 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3115 return renderTarget;
3116}
3117
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003118ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003119{
3120 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003121
3122 switch (type)
3123 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003124 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003125 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003126 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003127 if (vshader)
3128 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003129 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003130 }
3131 }
3132 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003133 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003134 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003135 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003136 if (pshader)
3137 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003138 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003139 }
3140 }
3141 break;
3142 default:
3143 UNREACHABLE();
3144 break;
3145 }
3146
3147 return executable;
3148}
3149
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003150ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003151{
3152 const char *profile = NULL;
3153
3154 switch (type)
3155 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003156 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003157 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3158 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003159 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003160 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3161 break;
3162 default:
3163 UNREACHABLE();
3164 return NULL;
3165 }
3166
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003167 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003168 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04003169 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003170 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04003171 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003172
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003173 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04003174 SafeRelease(binary);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003175
3176 return executable;
3177}
3178
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003179bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3180{
3181 return mBlit->boxFilter(source, dest);
3182}
3183
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003184D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003185{
3186 if (mD3d9Ex != NULL)
3187 {
3188 return D3DPOOL_DEFAULT;
3189 }
3190 else
3191 {
3192 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3193 {
3194 return D3DPOOL_MANAGED;
3195 }
3196 }
3197
3198 return D3DPOOL_DEFAULT;
3199}
3200
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003201bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3202{
3203 if (source && dest)
3204 {
3205 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003206
3207 if (fromManaged)
3208 {
3209 D3DSURFACE_DESC desc;
3210 source->GetDesc(&desc);
3211
3212 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003213 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003214
3215 if (SUCCEEDED(result))
3216 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003217 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003218 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
Geoff Langea228632013-07-30 15:17:12 -04003219 SafeRelease(surf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003220 }
3221 }
3222 else
3223 {
3224 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003225 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003226 }
3227
3228 if (FAILED(result))
3229 {
3230 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3231 return false;
3232 }
3233 }
3234
3235 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003236}
3237
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003238Image *Renderer9::createImage()
3239{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003240 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003241}
3242
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003243void Renderer9::generateMipmap(Image *dest, Image *src)
3244{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003245 Image9 *src9 = Image9::makeImage9(src);
3246 Image9 *dst9 = Image9::makeImage9(dest);
3247 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003248}
3249
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003250TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3251{
3252 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3253 return new TextureStorage9_2D(this, swapChain9);
3254}
3255
3256TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3257{
3258 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3259}
3260
3261TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3262{
3263 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3264}
3265
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003266TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3267{
3268 // 3D textures are not supported by the D3D9 backend.
3269 UNREACHABLE();
3270
3271 return NULL;
3272}
3273
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003274TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3275{
3276 // 2D array textures are not supported by the D3D9 backend.
3277 UNREACHABLE();
3278
3279 return NULL;
3280}
3281
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003282bool Renderer9::getLUID(LUID *adapterLuid) const
3283{
3284 adapterLuid->HighPart = 0;
3285 adapterLuid->LowPart = 0;
3286
3287 if (mD3d9Ex)
3288 {
3289 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3290 return true;
3291 }
3292
3293 return false;
3294}
3295
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003296}