blob: c699fc9aec8c4da8cf43e3f00de9d76ff7c50056 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002//
daniel@transgaming.com669c9952013-01-11 04:08:16 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00008// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009
Geoff Lang2a64ee42013-05-31 11:22:40 -040010#include "common/utilities.h"
11
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/main.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000014#include "libGLESv2/Texture.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000015#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000016#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000017#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000018#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000019#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000020#include "libGLESv2/renderer/renderer9_utils.h"
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000021#include "libGLESv2/renderer/formatutils9.h"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000022#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000023#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com34da3972012-12-20 21:10:29 +000024#include "libGLESv2/renderer/TextureStorage9.h"
daniel@transgaming.com4ba24062012-12-20 20:54:24 +000025#include "libGLESv2/renderer/Image9.h"
Geoff Langdce735c2013-06-04 10:21:18 -040026#include "libGLESv2/renderer/Blit9.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000027#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com3f255b42012-12-20 21:07:35 +000028#include "libGLESv2/renderer/VertexBuffer9.h"
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +000029#include "libGLESv2/renderer/IndexBuffer9.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000030#include "libGLESv2/renderer/BufferStorage9.h"
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +000031#include "libGLESv2/renderer/Query9.h"
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +000032#include "libGLESv2/renderer/Fence9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000033
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
daniel@transgaming.com621ce052012-10-31 17:52:29 +000036// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
37#define REF_RAST 0
38
39// The "Debug This Pixel..." feature in PIX often fails when using the
40// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
41// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
42#if !defined(ANGLE_ENABLE_D3D9EX)
43// Enables use of the IDirect3D9Ex interface, when available
44#define ANGLE_ENABLE_D3D9EX 1
45#endif // !defined(ANGLE_ENABLE_D3D9EX)
46
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +000047#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
48#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
49#endif
50
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000051const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z')));
52const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')));
53
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000054namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000055{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000056static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000057 {
58 D3DFMT_A1R5G5B5,
59 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
60 D3DFMT_A8R8G8B8,
61 D3DFMT_R5G6B5,
62 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
63 D3DFMT_X8R8G8B8
64 };
65
daniel@transgaming.com222ee082012-11-28 19:31:49 +000066static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000067 {
68 D3DFMT_UNKNOWN,
69 // D3DFMT_D16_LOCKABLE,
70 D3DFMT_D32,
71 // D3DFMT_D15S1,
72 D3DFMT_D24S8,
73 D3DFMT_D24X8,
74 // D3DFMT_D24X4S4,
75 D3DFMT_D16,
76 // D3DFMT_D32F_LOCKABLE,
77 // D3DFMT_D24FS8
78 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000079
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000080enum
81{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +000082 MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
83 MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32,
84 MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224,
85 MAX_VARYING_VECTORS_SM2 = 8,
86 MAX_VARYING_VECTORS_SM3 = 10,
87
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000088 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
89};
90
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000091Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000092{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000093 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000094
95 mD3d9 = NULL;
96 mD3d9Ex = NULL;
97 mDevice = NULL;
98 mDeviceEx = NULL;
99 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000100 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000101
102 mAdapter = D3DADAPTER_DEFAULT;
103
104 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
105 mDeviceType = D3DDEVTYPE_REF;
106 #else
107 mDeviceType = D3DDEVTYPE_HAL;
108 #endif
109
110 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000111
112 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000113
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +0000114 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000115
116 mVertexDataManager = NULL;
117 mIndexDataManager = NULL;
118 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000119
120 mMaxNullColorbufferLRU = 0;
121 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
122 {
123 mNullColorbufferCache[i].lruCount = 0;
124 mNullColorbufferCache[i].width = 0;
125 mNullColorbufferCache[i].height = 0;
126 mNullColorbufferCache[i].buffer = NULL;
127 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000128}
129
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000130Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000131{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000132 releaseDeviceResources();
133
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000134 if (mDevice)
135 {
136 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000137 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000138 {
139 resetDevice();
140 }
141
Geoff Langea228632013-07-30 15:17:12 -0400142 SafeRelease(mDevice);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000143 }
144
Geoff Langea228632013-07-30 15:17:12 -0400145 SafeRelease(mDeviceEx);
146 SafeRelease(mD3d9);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000147
148 if (mDeviceWindow)
149 {
150 DestroyWindow(mDeviceWindow);
151 mDeviceWindow = NULL;
152 }
153
Geoff Langea228632013-07-30 15:17:12 -0400154 SafeRelease(mD3d9Ex);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000155
Geoff Langea228632013-07-30 15:17:12 -0400156 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000157}
158
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000159Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
160{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000161 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000162 return static_cast<rx::Renderer9*>(renderer);
163}
164
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000165EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000166{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000167 if (!initializeCompiler())
168 {
169 return EGL_NOT_INITIALIZED;
170 }
171
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000172 if (mSoftwareDevice)
173 {
174 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
175 }
176 else
177 {
178 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
179 }
180
181 if (mD3d9Module == NULL)
182 {
183 ERR("No D3D9 module found - aborting!\n");
184 return EGL_NOT_INITIALIZED;
185 }
186
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000187 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
188 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
189
190 // Use Direct3D9Ex if available. Among other things, this version is less
191 // inclined to report a lost context, for example when the user switches
192 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
193 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
194 {
195 ASSERT(mD3d9Ex);
196 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
197 ASSERT(mD3d9);
198 }
199 else
200 {
201 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
202 }
203
204 if (!mD3d9)
205 {
206 ERR("Could not create D3D9 device - aborting!\n");
207 return EGL_NOT_INITIALIZED;
208 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000209
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000210 if (mDc != NULL)
211 {
212 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
213 }
214
215 HRESULT result;
216
217 // Give up on getting device caps after about one second.
218 for (int i = 0; i < 10; ++i)
219 {
220 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
221 if (SUCCEEDED(result))
222 {
223 break;
224 }
225 else if (result == D3DERR_NOTAVAILABLE)
226 {
227 Sleep(100); // Give the driver some time to initialize/recover
228 }
229 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
230 {
231 ERR("failed to get device caps (0x%x)\n", result);
232 return EGL_NOT_INITIALIZED;
233 }
234 }
235
236 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
237 {
238 ERR("Renderer does not support PS 2.0. aborting!\n");
239 return EGL_NOT_INITIALIZED;
240 }
241
242 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
243 // This is required by Texture2D::convertToRenderTarget.
244 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
245 {
246 ERR("Renderer does not support stretctrect from textures!\n");
247 return EGL_NOT_INITIALIZED;
248 }
249
250 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
251
252 // ATI cards on XP have problems with non-power-of-two textures.
253 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
254 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
255 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
256 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
257
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000258 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
259 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
260
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000261 mMinSwapInterval = 4;
262 mMaxSwapInterval = 0;
263
264 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
265 {
266 mMinSwapInterval = std::min(mMinSwapInterval, 0);
267 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
268 }
269 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
270 {
271 mMinSwapInterval = std::min(mMinSwapInterval, 1);
272 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
273 }
274 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
275 {
276 mMinSwapInterval = std::min(mMinSwapInterval, 2);
277 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
278 }
279 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
280 {
281 mMinSwapInterval = std::min(mMinSwapInterval, 3);
282 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
283 }
284 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
285 {
286 mMinSwapInterval = std::min(mMinSwapInterval, 4);
287 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
288 }
289
Geoff Lang61e49a52013-05-29 10:22:58 -0400290 mMaxSupportedSamples = 0;
291
292 const d3d9::D3DFormatSet &d3d9Formats = d3d9::GetAllUsedD3DFormats();
293 for (d3d9::D3DFormatSet::const_iterator i = d3d9Formats.begin(); i != d3d9Formats.end(); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000294 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400295 MultisampleSupportInfo support = getMultiSampleSupport(*i);
296 mMultiSampleSupport[*i] = support;
297 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
daniel@transgaming.com92955622012-10-31 18:38:41 +0000298 }
299
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000300 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
301 static const TCHAR className[] = TEXT("STATIC");
302
303 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
304
305 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
306 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
307
308 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
309 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
310 {
311 return EGL_BAD_ALLOC;
312 }
313
314 if (FAILED(result))
315 {
316 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
317
318 if (FAILED(result))
319 {
320 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
321 return EGL_BAD_ALLOC;
322 }
323 }
324
325 if (mD3d9Ex)
326 {
327 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
328 ASSERT(SUCCEEDED(result));
329 }
330
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000331 mVertexShaderCache.initialize(mDevice);
332 mPixelShaderCache.initialize(mDevice);
333
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000334 // Check occlusion query support
335 IDirect3DQuery9 *occlusionQuery = NULL;
336 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
337 {
Geoff Langea228632013-07-30 15:17:12 -0400338 SafeRelease(occlusionQuery);
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000339 mOcclusionQuerySupport = true;
340 }
341 else
342 {
343 mOcclusionQuerySupport = false;
344 }
345
346 // Check event query support
347 IDirect3DQuery9 *eventQuery = NULL;
348 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
349 {
Geoff Langea228632013-07-30 15:17:12 -0400350 SafeRelease(eventQuery);
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000351 mEventQuerySupport = true;
352 }
353 else
354 {
355 mEventQuerySupport = false;
356 }
357
358 D3DDISPLAYMODE currentDisplayMode;
359 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
360
361 // Check vertex texture support
362 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
363 // We test this using D3D9 by checking support for the R16F format.
364 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
365 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
366 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
367
Geoff Langd42cf4e2013-06-05 16:09:17 -0400368 // Check RGB565 texture support
369 mRGB565TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
370 D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_R5G6B5));
371
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000372 // Check depth texture support
373 // we use INTZ for depth textures in Direct3D9
374 // we also want NULL texture support to ensure the we can make depth-only FBOs
375 // see http://aras-p.info/texts/D3D9GPUHacks.html
376 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
377 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
378 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
379 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
380
381 // Check 32 bit floating point texture support
382 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
383 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
384 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
385 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
386
387 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
388 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
389 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
390 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
391
392 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
393 {
394 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
395 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
396 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
397 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
398 }
399 else
400 {
401 mFloat32TextureSupport = true;
402 }
403
404 // Check 16 bit floating point texture support
405 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
406 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
407 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
408 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
409
410 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
411 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
412 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
413 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
414
415 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
416 {
417 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
418 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
419 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
420 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
421 }
422 else
423 {
424 mFloat16TextureSupport = true;
425 }
426
427 // Check DXT texture support
428 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
429 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
430 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
431
432 // Check luminance[alpha] texture support
433 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
434 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
435
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000436 initializeDevice();
437
438 return EGL_SUCCESS;
439}
440
441// do any one-time device initialization
442// NOTE: this is also needed after a device lost/reset
443// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000444void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000445{
446 // Permanent non-default states
447 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
448 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
449
450 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
451 {
452 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
453 }
454 else
455 {
456 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
457 }
458
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000459 markAllStateDirty();
460
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000461 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000462
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000463 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400464 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000465 mVertexDataManager = new rx::VertexDataManager(this);
466 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000467}
468
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000469D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000470{
471 D3DPRESENT_PARAMETERS presentParameters = {0};
472
473 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
474 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
475 presentParameters.BackBufferCount = 1;
476 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
477 presentParameters.BackBufferWidth = 1;
478 presentParameters.BackBufferHeight = 1;
479 presentParameters.EnableAutoDepthStencil = FALSE;
480 presentParameters.Flags = 0;
481 presentParameters.hDeviceWindow = mDeviceWindow;
482 presentParameters.MultiSampleQuality = 0;
483 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
484 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
485 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
486 presentParameters.Windowed = TRUE;
487
488 return presentParameters;
489}
490
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000491int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000492{
493 D3DDISPLAYMODE currentDisplayMode;
494 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
495
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000496 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
497 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000498 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
499 int numConfigs = 0;
500
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000501 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000502 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000503 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000504
505 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
506
507 if (SUCCEEDED(result))
508 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000509 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000510 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000511 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000512 HRESULT result = D3D_OK;
513
514 if(depthStencilFormat != D3DFMT_UNKNOWN)
515 {
516 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
517 }
518
519 if (SUCCEEDED(result))
520 {
521 if(depthStencilFormat != D3DFMT_UNKNOWN)
522 {
523 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
524 }
525
526 if (SUCCEEDED(result))
527 {
528 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000529 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
530 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000531 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
532 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000533 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000534
535 (*configDescList)[numConfigs++] = newConfig;
536 }
537 }
538 }
539 }
540 }
541
542 return numConfigs;
543}
544
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000545void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000546{
547 delete [] (configDescList);
548}
549
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000550void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000551{
552 if (!mSceneStarted)
553 {
554 long result = mDevice->BeginScene();
555 if (SUCCEEDED(result)) {
556 // This is defensive checking against the device being
557 // lost at unexpected times.
558 mSceneStarted = true;
559 }
560 }
561}
562
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000563void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000564{
565 if (mSceneStarted)
566 {
567 // EndScene can fail if the device was lost, for example due
568 // to a TDR during a draw call.
569 mDevice->EndScene();
570 mSceneStarted = false;
571 }
572}
573
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000574void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000575{
576 HRESULT result;
577
578 IDirect3DQuery9* query = allocateEventQuery();
579 if (!query)
580 {
581 return;
582 }
583
584 result = query->Issue(D3DISSUE_END);
585 ASSERT(SUCCEEDED(result));
586
587 do
588 {
589 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
590
591 if(block && result == S_FALSE)
592 {
593 // Keep polling, but allow other threads to do something useful first
594 Sleep(0);
595 // explicitly check for device loss
596 // some drivers seem to return S_FALSE even if the device is lost
597 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000598 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000599 {
600 result = D3DERR_DEVICELOST;
601 }
602 }
603 }
604 while(block && result == S_FALSE);
605
606 freeEventQuery(query);
607
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000608 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000609 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000610 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000611 }
612}
613
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000614SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
615{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000616 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000617}
618
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000619IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000620{
621 IDirect3DQuery9 *query = NULL;
622
623 if (mEventQueryPool.empty())
624 {
625 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
626 ASSERT(SUCCEEDED(result));
627 }
628 else
629 {
630 query = mEventQueryPool.back();
631 mEventQueryPool.pop_back();
632 }
633
634 return query;
635}
636
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000637void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000638{
639 if (mEventQueryPool.size() > 1000)
640 {
Geoff Langea228632013-07-30 15:17:12 -0400641 SafeRelease(query);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000642 }
643 else
644 {
645 mEventQueryPool.push_back(query);
646 }
647}
648
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000649IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000650{
651 return mVertexShaderCache.create(function, length);
652}
653
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000654IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000655{
656 return mPixelShaderCache.create(function, length);
657}
658
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000659HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
660{
661 D3DPOOL Pool = getBufferPool(Usage);
662 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
663}
664
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000665VertexBuffer *Renderer9::createVertexBuffer()
666{
667 return new VertexBuffer9(this);
668}
669
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000670HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
671{
672 D3DPOOL Pool = getBufferPool(Usage);
673 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
674}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000675
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000676IndexBuffer *Renderer9::createIndexBuffer()
677{
678 return new IndexBuffer9(this);
679}
680
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000681BufferStorage *Renderer9::createBufferStorage()
682{
683 return new BufferStorage9();
684}
685
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000686QueryImpl *Renderer9::createQuery(GLenum type)
687{
688 return new Query9(this, type);
689}
690
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000691FenceImpl *Renderer9::createFence()
692{
693 return new Fence9(this);
694}
695
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000696void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000697{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000698 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
699 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000700
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000701 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000702 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000703 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
704 int d3dSampler = index + d3dSamplerOffset;
705
706 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
707 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
708
709 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
710 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
711 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
712 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
713 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
714 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
715 if (mSupportsTextureFilterAnisotropy)
716 {
717 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
718 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000719 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000720
721 forceSetSamplers[index] = false;
722 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000723}
724
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000725void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000726{
727 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
728 int d3dSampler = index + d3dSamplerOffset;
729 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000730 unsigned int serial = 0;
731 bool forceSetTexture = false;
732
733 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000734
735 if (texture)
736 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000737 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000738 if (texStorage)
739 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000740 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000741 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000742 }
743 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000744 // in the texture class and we're unexpectedly missing the d3d texture
745 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000746
747 serial = texture->getTextureSerial();
748 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000749 }
750
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000751 if (forceSetTexture || appliedSerials[index] != serial)
752 {
753 mDevice->SetTexture(d3dSampler, d3dTexture);
754 }
755
756 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000757}
758
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000759bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
760{
761 // No effect in ES2/D3D9
762 return true;
763}
764
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000765void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000766{
767 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000768
769 if (rasterStateChanged)
770 {
771 // Set the cull mode
772 if (rasterState.cullFace)
773 {
774 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
775 }
776 else
777 {
778 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
779 }
780
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000781 if (rasterState.polygonOffsetFill)
782 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000783 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000784 {
785 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
786
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000787 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000788 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
789 }
790 }
791 else
792 {
793 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
794 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
795 }
796
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000797 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000798 }
799
800 mForceSetRasterState = false;
801}
802
Geoff Lang2a64ee42013-05-31 11:22:40 -0400803void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor, unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000804{
805 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400806 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000807 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
808
809 if (blendStateChanged || blendColorChanged)
810 {
811 if (blendState.blend)
812 {
813 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
814
815 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
816 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
817 {
818 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
819 }
820 else
821 {
822 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
823 gl::unorm<8>(blendColor.alpha),
824 gl::unorm<8>(blendColor.alpha),
825 gl::unorm<8>(blendColor.alpha)));
826 }
827
828 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
829 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
830 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
831
832 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
833 blendState.destBlendRGB != blendState.destBlendAlpha ||
834 blendState.blendEquationRGB != blendState.blendEquationAlpha)
835 {
836 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
837
838 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
839 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
840 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
841 }
842 else
843 {
844 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
845 }
846 }
847 else
848 {
849 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
850 }
851
852 if (blendState.sampleAlphaToCoverage)
853 {
854 FIXME("Sample alpha to coverage is unimplemented.");
855 }
856
857 // Set the color mask
858 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
859 // Apparently some ATI cards have a bug where a draw with a zero color
860 // write mask can cause later draws to have incorrect results. Instead,
861 // set a nonzero color write mask but modify the blend state so that no
862 // drawing is done.
863 // http://code.google.com/p/angleproject/issues/detail?id=169
864
865 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
866 blendState.colorMaskBlue, blendState.colorMaskAlpha);
867 if (colorMask == 0 && !zeroColorMaskAllowed)
868 {
869 // Enable green channel, but set blending so nothing will be drawn.
870 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
871 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
872
873 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
874 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
875 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
876 }
877 else
878 {
879 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
880 }
881
882 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
883
884 mCurBlendState = blendState;
885 mCurBlendColor = blendColor;
886 }
887
888 if (sampleMaskChanged)
889 {
890 // Set the multisample mask
891 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
892 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
893
894 mCurSampleMask = sampleMask;
895 }
896
897 mForceSetBlendState = false;
898}
899
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000900void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000901 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000902{
903 bool depthStencilStateChanged = mForceSetDepthStencilState ||
904 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000905 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
906 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000907 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000908
909 if (depthStencilStateChanged)
910 {
911 if (depthStencilState.depthTest)
912 {
913 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
914 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
915 }
916 else
917 {
918 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
919 }
920
921 mCurDepthStencilState = depthStencilState;
922 }
923
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000924 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000925 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000926 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000927 {
928 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
929 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
930
931 // FIXME: Unsupported by D3D9
932 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
933 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
934 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
935 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000936 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000937 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
938 {
939 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000940 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000941 }
942
943 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000944 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000945
946 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
947 depthStencilState.stencilWritemask);
948 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
949 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
950
951 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000952 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000953 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
954 depthStencilState.stencilMask);
955
956 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
957 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
958 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
959 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
960 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
961 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
962
963 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
964 depthStencilState.stencilBackWritemask);
965 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
966 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
967
968 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000969 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000970 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
971 depthStencilState.stencilBackMask);
972
973 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
974 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
975 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
976 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
977 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
978 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
979 }
980 else
981 {
982 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
983 }
984
985 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
986
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000987 mCurStencilRef = stencilRef;
988 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000989 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000990 }
991
992 mForceSetDepthStencilState = false;
993}
994
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000995void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000996{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000997 bool scissorChanged = mForceSetScissor ||
998 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
999 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001000
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001001 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001002 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001003 if (enabled)
1004 {
1005 RECT rect;
1006 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1007 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1008 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1009 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1010 mDevice->SetScissorRect(&rect);
1011 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001012
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001013 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1014
1015 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001016 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001017 }
1018
1019 mForceSetScissor = false;
1020}
1021
daniel@transgaming.com12985182012-12-20 20:56:31 +00001022bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001023 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001024{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001025 gl::Rectangle actualViewport = viewport;
1026 float actualZNear = gl::clamp01(zNear);
1027 float actualZFar = gl::clamp01(zFar);
1028 if (ignoreViewport)
1029 {
1030 actualViewport.x = 0;
1031 actualViewport.y = 0;
1032 actualViewport.width = mRenderTargetDesc.width;
1033 actualViewport.height = mRenderTargetDesc.height;
1034 actualZNear = 0.0f;
1035 actualZFar = 1.0f;
1036 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001037
1038 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001039 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1040 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1041 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1042 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1043 dxViewport.MinZ = actualZNear;
1044 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001045
1046 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1047 {
1048 return false; // Nothing to render
1049 }
1050
Geoff Langde14d602013-08-14 12:28:33 -04001051 float depthFront = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);
1052
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001053 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
Geoff Langde14d602013-08-14 12:28:33 -04001054 actualZNear != mCurNear || actualZFar != mCurFar || mCurDepthFront != depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001055 if (viewportChanged)
1056 {
1057 mDevice->SetViewport(&dxViewport);
1058
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001059 mCurViewport = actualViewport;
1060 mCurNear = actualZNear;
1061 mCurFar = actualZFar;
Geoff Langde14d602013-08-14 12:28:33 -04001062 mCurDepthFront = depthFront;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001063
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001064 dx_VertexConstants vc = {0};
1065 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001066
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001067 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1068 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1069 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1070 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001071
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001072 pc.viewCoords[0] = actualViewport.width * 0.5f;
1073 pc.viewCoords[1] = actualViewport.height * 0.5f;
1074 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1075 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001076
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001077 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1078 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
Geoff Langde14d602013-08-14 12:28:33 -04001079 pc.depthFront[2] = depthFront;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001080
1081 vc.depthRange[0] = actualZNear;
1082 vc.depthRange[1] = actualZFar;
1083 vc.depthRange[2] = actualZFar - actualZNear;
1084
1085 pc.depthRange[0] = actualZNear;
1086 pc.depthRange[1] = actualZFar;
1087 pc.depthRange[2] = actualZFar - actualZNear;
1088
1089 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1090 {
1091 mVertexConstants = vc;
1092 mDxUniformsDirty = true;
1093 }
1094
1095 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1096 {
1097 mPixelConstants = pc;
1098 mDxUniformsDirty = true;
1099 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001100 }
1101
1102 mForceSetViewport = false;
1103 return true;
1104}
1105
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1107{
1108 switch (mode)
1109 {
1110 case GL_POINTS:
1111 mPrimitiveType = D3DPT_POINTLIST;
1112 mPrimitiveCount = count;
1113 break;
1114 case GL_LINES:
1115 mPrimitiveType = D3DPT_LINELIST;
1116 mPrimitiveCount = count / 2;
1117 break;
1118 case GL_LINE_LOOP:
1119 mPrimitiveType = D3DPT_LINESTRIP;
1120 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1121 break;
1122 case GL_LINE_STRIP:
1123 mPrimitiveType = D3DPT_LINESTRIP;
1124 mPrimitiveCount = count - 1;
1125 break;
1126 case GL_TRIANGLES:
1127 mPrimitiveType = D3DPT_TRIANGLELIST;
1128 mPrimitiveCount = count / 3;
1129 break;
1130 case GL_TRIANGLE_STRIP:
1131 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1132 mPrimitiveCount = count - 2;
1133 break;
1134 case GL_TRIANGLE_FAN:
1135 mPrimitiveType = D3DPT_TRIANGLEFAN;
1136 mPrimitiveCount = count - 2;
1137 break;
1138 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001139 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001140 }
1141
1142 return mPrimitiveCount > 0;
1143}
1144
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001145
1146gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1147{
1148 if (!depthbuffer)
1149 {
1150 ERR("Unexpected null depthbuffer for depth-only FBO.");
1151 return NULL;
1152 }
1153
1154 GLsizei width = depthbuffer->getWidth();
1155 GLsizei height = depthbuffer->getHeight();
1156
1157 // search cached nullcolorbuffers
1158 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1159 {
1160 if (mNullColorbufferCache[i].buffer != NULL &&
1161 mNullColorbufferCache[i].width == width &&
1162 mNullColorbufferCache[i].height == height)
1163 {
1164 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1165 return mNullColorbufferCache[i].buffer;
1166 }
1167 }
1168
1169 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1170
1171 // add nullbuffer to the cache
1172 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1173 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1174 {
1175 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1176 {
1177 oldest = &mNullColorbufferCache[i];
1178 }
1179 }
1180
1181 delete oldest->buffer;
1182 oldest->buffer = nullbuffer;
1183 oldest->lruCount = ++mMaxNullColorbufferLRU;
1184 oldest->width = width;
1185 oldest->height = height;
1186
1187 return nullbuffer;
1188}
1189
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001190bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001191{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001192 // if there is no color attachment we must synthesize a NULL colorattachment
1193 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1194 gl::Renderbuffer *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001195 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001196 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001197 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001198 }
1199 else
1200 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001201 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001202 }
1203 if (!renderbufferObject)
1204 {
1205 ERR("unable to locate renderbuffer for FBO.");
1206 return false;
1207 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001208
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001209 bool renderTargetChanged = false;
1210 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1211 if (renderTargetSerial != mAppliedRenderTargetSerial)
1212 {
1213 // Apply the render target on the device
1214 IDirect3DSurface9 *renderTargetSurface = NULL;
1215
1216 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1217 if (renderTarget)
1218 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001219 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001220 }
1221
1222 if (!renderTargetSurface)
1223 {
1224 ERR("render target pointer unexpectedly null.");
1225 return false; // Context must be lost
1226 }
1227
1228 mDevice->SetRenderTarget(0, renderTargetSurface);
Geoff Langea228632013-07-30 15:17:12 -04001229 SafeRelease(renderTargetSurface);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001230
1231 mAppliedRenderTargetSerial = renderTargetSerial;
1232 renderTargetChanged = true;
1233 }
1234
1235 gl::Renderbuffer *depthStencil = NULL;
1236 unsigned int depthbufferSerial = 0;
1237 unsigned int stencilbufferSerial = 0;
1238 if (framebuffer->getDepthbufferType() != GL_NONE)
1239 {
1240 depthStencil = framebuffer->getDepthbuffer();
1241 if (!depthStencil)
1242 {
1243 ERR("Depth stencil pointer unexpectedly null.");
1244 return false;
1245 }
1246
1247 depthbufferSerial = depthStencil->getSerial();
1248 }
1249 else if (framebuffer->getStencilbufferType() != GL_NONE)
1250 {
1251 depthStencil = framebuffer->getStencilbuffer();
1252 if (!depthStencil)
1253 {
1254 ERR("Depth stencil pointer unexpectedly null.");
1255 return false;
1256 }
1257
1258 stencilbufferSerial = depthStencil->getSerial();
1259 }
1260
1261 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1262 stencilbufferSerial != mAppliedStencilbufferSerial ||
1263 !mDepthStencilInitialized)
1264 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001265 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001266 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001267
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001268 // Apply the depth stencil on the device
1269 if (depthStencil)
1270 {
1271 IDirect3DSurface9 *depthStencilSurface = NULL;
1272 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1273
1274 if (depthStencilRenderTarget)
1275 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001276 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001277 }
1278
1279 if (!depthStencilSurface)
1280 {
1281 ERR("depth stencil pointer unexpectedly null.");
1282 return false; // Context must be lost
1283 }
1284
1285 mDevice->SetDepthStencilSurface(depthStencilSurface);
Geoff Langea228632013-07-30 15:17:12 -04001286 SafeRelease(depthStencilSurface);
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001287
1288 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001289 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001290 }
1291 else
1292 {
1293 mDevice->SetDepthStencilSurface(NULL);
1294 }
1295
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001296 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1297 {
1298 mCurDepthSize = depthSize;
1299 mForceSetRasterState = true;
1300 }
1301
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001302 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1303 {
1304 mCurStencilSize = stencilSize;
1305 mForceSetDepthStencilState = true;
1306 }
1307
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001308 mAppliedDepthbufferSerial = depthbufferSerial;
1309 mAppliedStencilbufferSerial = stencilbufferSerial;
1310 mDepthStencilInitialized = true;
1311 }
1312
1313 if (renderTargetChanged || !mRenderTargetDescInitialized)
1314 {
1315 mForceSetScissor = true;
1316 mForceSetViewport = true;
1317
1318 mRenderTargetDesc.width = renderbufferObject->getWidth();
1319 mRenderTargetDesc.height = renderbufferObject->getHeight();
1320 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1321 mRenderTargetDescInitialized = true;
1322 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001323
1324 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001325}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001326
Jamie Madill57a89722013-07-02 11:57:03 -04001327GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001328 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001329{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001330 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001331 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001332 if (err != GL_NO_ERROR)
1333 {
1334 return err;
1335 }
1336
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001337 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1338}
1339
1340// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001341GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001342{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001343 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001344
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001345 if (err == GL_NO_ERROR)
1346 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001347 // Directly binding the storage buffer is not supported for d3d9
1348 ASSERT(indexInfo->storage == NULL);
1349
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001350 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001351 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001352 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1353
1354 mDevice->SetIndices(indexBuffer->getBuffer());
1355 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001356 }
1357 }
1358
1359 return err;
1360}
1361
1362void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1363{
1364 startScene();
1365
1366 if (mode == GL_LINE_LOOP)
1367 {
1368 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1369 }
1370 else if (instances > 0)
1371 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001372 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001373 if (countingIB)
1374 {
1375 if (mAppliedIBSerial != countingIB->getSerial())
1376 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001377 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1378
1379 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001380 mAppliedIBSerial = countingIB->getSerial();
1381 }
1382
1383 for (int i = 0; i < mRepeatDraw; i++)
1384 {
1385 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1386 }
1387 }
1388 else
1389 {
1390 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001391 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001392 }
1393 }
1394 else // Regular case
1395 {
1396 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1397 }
1398}
1399
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001400void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001401{
1402 startScene();
1403
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001404 if (mode == GL_POINTS)
1405 {
1406 drawIndexedPoints(count, type, indices, elementArrayBuffer);
1407 }
1408 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001409 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001410 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001411 }
1412 else
1413 {
1414 for (int i = 0; i < mRepeatDraw; i++)
1415 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001416 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1417 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001418 }
1419 }
1420}
1421
1422void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1423{
1424 // Get the raw indices for an indexed draw
1425 if (type != GL_NONE && elementArrayBuffer)
1426 {
1427 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001428 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001429 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001430 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001431 }
1432
Geoff Langa36ead42013-08-02 11:54:08 -04001433 unsigned int startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001434
1435 if (get32BitIndexSupport())
1436 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001437 if (!mLineLoopIB)
1438 {
1439 mLineLoopIB = new StreamingIndexBufferInterface(this);
1440 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1441 {
1442 delete mLineLoopIB;
1443 mLineLoopIB = NULL;
1444
1445 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001446 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001447 }
1448 }
1449
Geoff Lang57e713e2013-07-31 17:01:58 -04001450 // Checked by Renderer9::applyPrimitiveType
1451 ASSERT(count >= 0);
1452
1453 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001454 {
1455 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1456 return gl::error(GL_OUT_OF_MEMORY);
1457 }
1458
Geoff Lang57e713e2013-07-31 17:01:58 -04001459 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001460 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001461 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001462 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001463 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001464 }
1465
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001466 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001467 unsigned int offset = 0;
1468 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001469 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001470 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001471 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001472 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001473
Geoff Langa36ead42013-08-02 11:54:08 -04001474 startIndex = static_cast<unsigned int>(offset) / 4;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001475 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1476
1477 switch (type)
1478 {
1479 case GL_NONE: // Non-indexed draw
1480 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001481 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001482 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001483 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001484 data[count] = 0;
1485 break;
1486 case GL_UNSIGNED_BYTE:
1487 for (int i = 0; i < count; i++)
1488 {
1489 data[i] = static_cast<const GLubyte*>(indices)[i];
1490 }
1491 data[count] = static_cast<const GLubyte*>(indices)[0];
1492 break;
1493 case GL_UNSIGNED_SHORT:
1494 for (int i = 0; i < count; i++)
1495 {
1496 data[i] = static_cast<const GLushort*>(indices)[i];
1497 }
1498 data[count] = static_cast<const GLushort*>(indices)[0];
1499 break;
1500 case GL_UNSIGNED_INT:
1501 for (int i = 0; i < count; i++)
1502 {
1503 data[i] = static_cast<const GLuint*>(indices)[i];
1504 }
1505 data[count] = static_cast<const GLuint*>(indices)[0];
1506 break;
1507 default: UNREACHABLE();
1508 }
1509
1510 if (!mLineLoopIB->unmapBuffer())
1511 {
1512 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001513 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001514 }
1515 }
1516 else
1517 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001518 if (!mLineLoopIB)
1519 {
1520 mLineLoopIB = new StreamingIndexBufferInterface(this);
1521 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1522 {
1523 delete mLineLoopIB;
1524 mLineLoopIB = NULL;
1525
1526 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001527 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001528 }
1529 }
1530
Geoff Lang57e713e2013-07-31 17:01:58 -04001531 // Checked by Renderer9::applyPrimitiveType
1532 ASSERT(count >= 0);
1533
1534 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001535 {
1536 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1537 return gl::error(GL_OUT_OF_MEMORY);
1538 }
1539
Geoff Lang57e713e2013-07-31 17:01:58 -04001540 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001541 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001542 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001543 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001544 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001545 }
1546
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001547 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001548 unsigned int offset;
1549 if (mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001550 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001551 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001552 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001553 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001554
Geoff Langa36ead42013-08-02 11:54:08 -04001555 startIndex = static_cast<unsigned int>(offset) / 2;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001556 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1557
1558 switch (type)
1559 {
1560 case GL_NONE: // Non-indexed draw
1561 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001562 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001563 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001564 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001565 data[count] = 0;
1566 break;
1567 case GL_UNSIGNED_BYTE:
1568 for (int i = 0; i < count; i++)
1569 {
1570 data[i] = static_cast<const GLubyte*>(indices)[i];
1571 }
1572 data[count] = static_cast<const GLubyte*>(indices)[0];
1573 break;
1574 case GL_UNSIGNED_SHORT:
1575 for (int i = 0; i < count; i++)
1576 {
1577 data[i] = static_cast<const GLushort*>(indices)[i];
1578 }
1579 data[count] = static_cast<const GLushort*>(indices)[0];
1580 break;
1581 case GL_UNSIGNED_INT:
1582 for (int i = 0; i < count; i++)
1583 {
1584 data[i] = static_cast<const GLuint*>(indices)[i];
1585 }
1586 data[count] = static_cast<const GLuint*>(indices)[0];
1587 break;
1588 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001589 }
1590
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001591 if (!mLineLoopIB->unmapBuffer())
1592 {
1593 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001594 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001595 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001596 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001597
1598 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001599 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001600 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1601
1602 mDevice->SetIndices(indexBuffer->getBuffer());
1603 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001604 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001605
1606 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001607}
1608
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001609template <typename T>
1610static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1611{
1612 for (int i = 0; i < count; i++)
1613 {
1614 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1615 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1616 }
1617}
1618
1619void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1620{
1621 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1622 // for each individual point. This call is not expected to happen often.
1623
1624 if (elementArrayBuffer)
1625 {
1626 BufferStorage *storage = elementArrayBuffer->getStorage();
1627 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1628 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1629 }
1630
1631 switch (type)
1632 {
1633 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1634 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1635 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1636 default: UNREACHABLE();
1637 }
1638}
1639
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001640void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1641{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001642 unsigned int programBinarySerial = programBinary->getSerial();
1643 if (programBinarySerial != mAppliedProgramBinarySerial)
1644 {
1645 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1646 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001647
daniel@transgaming.come4991412012-12-20 20:55:34 +00001648 IDirect3DVertexShader9 *vertexShader = NULL;
1649 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001650
daniel@transgaming.come4991412012-12-20 20:55:34 +00001651 IDirect3DPixelShader9 *pixelShader = NULL;
1652 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001653
daniel@transgaming.come4991412012-12-20 20:55:34 +00001654 mDevice->SetPixelShader(pixelShader);
1655 mDevice->SetVertexShader(vertexShader);
1656 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001657 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001658
1659 mAppliedProgramBinarySerial = programBinarySerial;
1660 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001661}
1662
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001663void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001664{
1665 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1666 {
1667 gl::Uniform *targetUniform = *ub;
1668
1669 if (targetUniform->dirty)
1670 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001671 GLfloat *f = (GLfloat*)targetUniform->data;
1672 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001673
1674 switch (targetUniform->type)
1675 {
1676 case GL_SAMPLER_2D:
1677 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001678 break;
1679 case GL_BOOL:
1680 case GL_BOOL_VEC2:
1681 case GL_BOOL_VEC3:
1682 case GL_BOOL_VEC4:
1683 applyUniformnbv(targetUniform, i);
1684 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001685 case GL_FLOAT:
1686 case GL_FLOAT_VEC2:
1687 case GL_FLOAT_VEC3:
1688 case GL_FLOAT_VEC4:
1689 case GL_FLOAT_MAT2:
1690 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001691 case GL_FLOAT_MAT4:
1692 applyUniformnfv(targetUniform, f);
1693 break;
1694 case GL_INT:
1695 case GL_INT_VEC2:
1696 case GL_INT_VEC3:
1697 case GL_INT_VEC4:
1698 applyUniformniv(targetUniform, i);
1699 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001700 default:
1701 UNREACHABLE();
1702 }
1703
1704 targetUniform->dirty = false;
1705 }
1706 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001707
1708 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001709 if (mDxUniformsDirty)
1710 {
1711 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1712 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1713 mDxUniformsDirty = false;
1714 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001715}
1716
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001717void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001718{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001719 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001720 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001721 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001722 }
1723
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001724 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001725 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001726 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001727 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001728}
1729
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001730void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001731{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001732 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1733 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001734
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001735 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001737 vector[i][0] = (GLfloat)v[4 * i + 0];
1738 vector[i][1] = (GLfloat)v[4 * i + 1];
1739 vector[i][2] = (GLfloat)v[4 * i + 2];
1740 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001741 }
1742
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001743 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001744}
1745
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001746void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001747{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001748 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1749 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001750
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001751 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001752 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001753 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1754 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1755 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1756 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001757 }
1758
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001759 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001760}
1761
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001762void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001763{
Geoff Langda507fe2013-08-20 12:01:42 -04001764 if (clearParams.colorClearType != GL_FLOAT)
1765 {
1766 // Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0
1767 UNREACHABLE();
1768 return;
1769 }
1770
1771 bool clearColor = clearParams.clearColor[0];
1772 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1773 {
1774 if (clearParams.clearColor[i] != clearColor)
1775 {
1776 // Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0
1777 UNREACHABLE();
1778 return;
1779 }
1780 }
1781
1782 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorFClearValue.alpha),
1783 gl::unorm<8>(clearParams.colorFClearValue.red),
1784 gl::unorm<8>(clearParams.colorFClearValue.green),
1785 gl::unorm<8>(clearParams.colorFClearValue.blue));
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001786 float depth = gl::clamp01(clearParams.depthClearValue);
1787 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001788
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001789 unsigned int stencilUnmasked = 0x0;
Geoff Langda507fe2013-08-20 12:01:42 -04001790 if (clearParams.clearStencil && frameBuffer->hasStencil())
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001791 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001792 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1793 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001794 stencilUnmasked = (0x1 << stencilSize) - 1;
1795 }
1796
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001797 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1798 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001799
Geoff Langda507fe2013-08-20 12:01:42 -04001800 const bool needMaskedStencilClear = clearParams.clearStencil &&
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001801 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
Geoff Langda507fe2013-08-20 12:01:42 -04001802 const bool needMaskedColorClear = clearColor && !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1803 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001804
1805 if (needMaskedColorClear || needMaskedStencilClear)
1806 {
1807 // State which is altered in all paths from this point to the clear call is saved.
1808 // State which is altered in only some paths will be flagged dirty in the case that
1809 // that path is taken.
1810 HRESULT hr;
1811 if (mMaskedClearSavedState == NULL)
1812 {
1813 hr = mDevice->BeginStateBlock();
1814 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1815
1816 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1817 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1818 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1819 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1820 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1821 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1822 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1823 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1824 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1825 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1826 mDevice->SetPixelShader(NULL);
1827 mDevice->SetVertexShader(NULL);
1828 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1829 mDevice->SetStreamSource(0, NULL, 0, 0);
1830 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1831 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1832 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1833 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1834 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1835 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1836 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1837
1838 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1839 {
1840 mDevice->SetStreamSourceFreq(i, 1);
1841 }
1842
1843 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1844 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1845 }
1846
1847 ASSERT(mMaskedClearSavedState != NULL);
1848
1849 if (mMaskedClearSavedState != NULL)
1850 {
1851 hr = mMaskedClearSavedState->Capture();
1852 ASSERT(SUCCEEDED(hr));
1853 }
1854
1855 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1856 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1857 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1858 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1859 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1860 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1861 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1862 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1863
Geoff Langda507fe2013-08-20 12:01:42 -04001864 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001865 {
1866 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1867 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1868 clearParams.colorMaskGreen,
1869 clearParams.colorMaskBlue,
1870 clearParams.colorMaskAlpha));
1871 }
1872 else
1873 {
1874 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1875 }
1876
Geoff Langda507fe2013-08-20 12:01:42 -04001877 if (stencilUnmasked != 0x0 && clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001878 {
1879 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1880 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1881 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1882 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1883 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1884 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1885 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1886 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1887 }
1888 else
1889 {
1890 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1891 }
1892
1893 mDevice->SetPixelShader(NULL);
1894 mDevice->SetVertexShader(NULL);
1895 mDevice->SetFVF(D3DFVF_XYZRHW);
1896 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1897 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1898 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1899 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1900 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1901 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1902 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1903
1904 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1905 {
1906 mDevice->SetStreamSourceFreq(i, 1);
1907 }
1908
1909 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1910 quad[0][0] = -0.5f;
1911 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1912 quad[0][2] = 0.0f;
1913 quad[0][3] = 1.0f;
1914
1915 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1916 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1917 quad[1][2] = 0.0f;
1918 quad[1][3] = 1.0f;
1919
1920 quad[2][0] = -0.5f;
1921 quad[2][1] = -0.5f;
1922 quad[2][2] = 0.0f;
1923 quad[2][3] = 1.0f;
1924
1925 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1926 quad[3][1] = -0.5f;
1927 quad[3][2] = 0.0f;
1928 quad[3][3] = 1.0f;
1929
1930 startScene();
1931 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1932
Geoff Langda507fe2013-08-20 12:01:42 -04001933 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001934 {
1935 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1936 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1937 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1938 }
1939
1940 if (mMaskedClearSavedState != NULL)
1941 {
1942 mMaskedClearSavedState->Apply();
1943 }
1944 }
Geoff Langda507fe2013-08-20 12:01:42 -04001945 else if (clearColor || clearParams.clearDepth || clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001946 {
1947 DWORD dxClearFlags = 0;
Geoff Langda507fe2013-08-20 12:01:42 -04001948 if (clearColor)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001949 {
1950 dxClearFlags |= D3DCLEAR_TARGET;
1951 }
Geoff Langda507fe2013-08-20 12:01:42 -04001952 if (clearParams.clearDepth)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001953 {
1954 dxClearFlags |= D3DCLEAR_ZBUFFER;
1955 }
Geoff Langda507fe2013-08-20 12:01:42 -04001956 if (clearParams.clearStencil)
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001957 {
1958 dxClearFlags |= D3DCLEAR_STENCIL;
1959 }
1960
1961 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1962 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001963}
1964
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001965void Renderer9::markAllStateDirty()
1966{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001967 mAppliedRenderTargetSerial = 0;
1968 mAppliedDepthbufferSerial = 0;
1969 mAppliedStencilbufferSerial = 0;
1970 mDepthStencilInitialized = false;
1971 mRenderTargetDescInitialized = false;
1972
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001973 mForceSetDepthStencilState = true;
1974 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001975 mForceSetScissor = true;
1976 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001977 mForceSetBlendState = true;
1978
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001979 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001980 {
1981 mForceSetVertexSamplerStates[i] = true;
1982 mCurVertexTextureSerials[i] = 0;
1983 }
1984 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1985 {
1986 mForceSetPixelSamplerStates[i] = true;
1987 mCurPixelTextureSerials[i] = 0;
1988 }
1989
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001990 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001991 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001992 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001993
1994 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001995}
1996
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001997void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001998{
Geoff Langea228632013-07-30 15:17:12 -04001999 for (size_t i = 0; i < mEventQueryPool.size(); i++)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002000 {
Geoff Langea228632013-07-30 15:17:12 -04002001 SafeRelease(mEventQueryPool[i]);
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002002 }
Geoff Langea228632013-07-30 15:17:12 -04002003 mEventQueryPool.clear();
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002004
Geoff Langea228632013-07-30 15:17:12 -04002005 SafeRelease(mMaskedClearSavedState);
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00002006
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002007 mVertexShaderCache.clear();
2008 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002009
Geoff Langea228632013-07-30 15:17:12 -04002010 SafeDelete(mBlit);
2011 SafeDelete(mVertexDataManager);
2012 SafeDelete(mIndexDataManager);
2013 SafeDelete(mLineLoopIB);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002014
2015 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2016 {
Geoff Langea228632013-07-30 15:17:12 -04002017 SafeDelete(mNullColorbufferCache[i].buffer);
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002018 }
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002019}
2020
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002021void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002022{
2023 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002024 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002025}
2026
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002027bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002028{
2029 return mDeviceLost;
2030}
2031
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002032// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002033bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002034{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002035 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002036
2037 if (mDeviceEx)
2038 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002039 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002040 }
2041 else if (mDevice)
2042 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002043 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002044 }
2045 else
2046 {
2047 // No device yet, so no reset required
2048 }
2049
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002050 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2051
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002052 if (isLost)
2053 {
2054 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002055 // we'll probably get this done again by notifyDeviceLost
2056 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002057 // Note that we don't want to clear the device loss status here
2058 // -- this needs to be done by resetDevice
2059 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002060 if (notify)
2061 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002062 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002063 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002064 }
2065
2066 return isLost;
2067}
2068
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002069bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002070{
2071 HRESULT status = D3D_OK;
2072
2073 if (mDeviceEx)
2074 {
2075 status = mDeviceEx->CheckDeviceState(NULL);
2076 }
2077 else if (mDevice)
2078 {
2079 status = mDevice->TestCooperativeLevel();
2080 }
2081
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002082 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002083 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002084 switch (status)
2085 {
2086 case D3DERR_DEVICENOTRESET:
2087 case D3DERR_DEVICEHUNG:
2088 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002089 case D3DERR_DEVICELOST:
2090 return (mDeviceEx != NULL);
2091 case D3DERR_DEVICEREMOVED:
2092 UNIMPLEMENTED();
2093 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002094 default:
2095 return false;
2096 }
2097}
2098
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002099bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002100{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002101 releaseDeviceResources();
2102
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002103 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2104
2105 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002106 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002107 int attempts = 3;
2108
2109 while (lost && attempts > 0)
2110 {
2111 if (mDeviceEx)
2112 {
2113 Sleep(500); // Give the graphics driver some CPU time
2114 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2115 }
2116 else
2117 {
2118 result = mDevice->TestCooperativeLevel();
2119 while (result == D3DERR_DEVICELOST)
2120 {
2121 Sleep(100); // Give the graphics driver some CPU time
2122 result = mDevice->TestCooperativeLevel();
2123 }
2124
2125 if (result == D3DERR_DEVICENOTRESET)
2126 {
2127 result = mDevice->Reset(&presentParameters);
2128 }
2129 }
2130
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002131 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002132 attempts --;
2133 }
2134
2135 if (FAILED(result))
2136 {
2137 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2138 return false;
2139 }
2140
2141 // reset device defaults
2142 initializeDevice();
2143 mDeviceLost = false;
2144
2145 return true;
2146}
2147
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002148DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002149{
2150 return mAdapterIdentifier.VendorId;
2151}
2152
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002153std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002154{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002155 std::ostringstream rendererString;
2156
2157 rendererString << mAdapterIdentifier.Description;
2158 if (getShareHandleSupport())
2159 {
2160 rendererString << " Direct3D9Ex";
2161 }
2162 else
2163 {
2164 rendererString << " Direct3D9";
2165 }
2166
2167 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2168 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2169
2170 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002171}
2172
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002173GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002174{
2175 return mAdapterIdentifier.DeviceIdentifier;
2176}
2177
Geoff Lang61e49a52013-05-29 10:22:58 -04002178Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002179{
Geoff Lang61e49a52013-05-29 10:22:58 -04002180 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002181
Geoff Lang61e49a52013-05-29 10:22:58 -04002182 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2183 {
2184 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2185 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2186
2187 if (SUCCEEDED(result))
2188 {
2189 support.supportedSamples[multiSampleIndex] = true;
2190 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2191 {
2192 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2193 }
2194 }
2195 else
2196 {
2197 support.supportedSamples[multiSampleIndex] = false;
2198 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002199 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002200
2201 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002202}
2203
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002204bool Renderer9::getBGRATextureSupport() const
2205{
2206 // DirectX 9 always supports BGRA
2207 return true;
2208}
2209
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002210bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002211{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002212 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002213}
2214
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002215bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002216{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002217 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002218}
2219
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002220bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002221{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002222 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002223}
2224
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002225bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002226{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002227 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002228}
2229
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002230bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002231{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002232 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002233}
2234
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002235bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002236{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002237 return mFloat32FilterSupport;
2238}
2239
2240bool Renderer9::getFloat32TextureRenderingSupport() const
2241{
2242 return mFloat32RenderSupport;
2243}
2244
2245bool Renderer9::getFloat16TextureSupport() const
2246{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002247 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002248}
2249
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002250bool Renderer9::getFloat16TextureFilteringSupport() const
2251{
2252 return mFloat16FilterSupport;
2253}
2254
2255bool Renderer9::getFloat16TextureRenderingSupport() const
2256{
2257 return mFloat16RenderSupport;
2258}
2259
Geoff Langd42cf4e2013-06-05 16:09:17 -04002260bool Renderer9::getRGB565TextureSupport() const
2261{
2262 return mRGB565TextureSupport;
2263}
2264
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002265bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002266{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002267 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002268}
2269
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002270bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002271{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002272 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002273}
2274
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002275bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002276{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002277 return mSupportsTextureFilterAnisotropy;
2278}
2279
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002280float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002281{
2282 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002283 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002284 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002285 }
2286 return 1.0f;
2287}
2288
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002289bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002290{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002291 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002292}
2293
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002294unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002295{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002296 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2297 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002298}
2299
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002300unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2301{
2302 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2303}
2304
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002305unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002306{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002307 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002308}
2309
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002310unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002311{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002312 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002313}
2314
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002315unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002316{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002317 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2318}
2319
2320unsigned int Renderer9::getMaxFragmentUniformVectors() const
2321{
2322 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2323
2324 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2325}
2326
2327unsigned int Renderer9::getMaxVaryingVectors() const
2328{
2329 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002330}
2331
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002332unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2333{
2334 return 0;
2335}
2336
2337unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2338{
2339 return 0;
2340}
2341
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002342unsigned int Renderer9::getReservedVertexUniformBuffers() const
2343{
2344 return 0;
2345}
2346
2347unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2348{
2349 return 0;
2350}
2351
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002352unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2353{
2354 return 0;
2355}
2356
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002357unsigned int Renderer9::getMaxUniformBufferSize() const
2358{
2359 return 0;
2360}
2361
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002362bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002363{
2364 return mSupportsNonPower2Textures;
2365}
2366
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002367bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002368{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002369 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002370}
2371
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002372bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002373{
2374 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2375}
2376
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002377bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002378{
2379 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002380 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002381}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002382
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002383bool Renderer9::getDerivativeInstructionSupport() const
2384{
2385 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2386}
2387
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002388bool Renderer9::getPostSubBufferSupport() const
2389{
2390 return true;
2391}
2392
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002393int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002394{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002395 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002396}
2397
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002398float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002399{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002400 // Point size clamped at 1.0f for SM2
2401 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002402}
2403
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002404int Renderer9::getMaxViewportDimension() const
2405{
2406 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002407 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002408 return maxTextureDimension;
2409}
2410
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002411int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002412{
2413 return (int)mDeviceCaps.MaxTextureWidth;
2414}
2415
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002416int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002417{
2418 return (int)mDeviceCaps.MaxTextureHeight;
2419}
2420
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002421int Renderer9::getMaxTextureDepth() const
2422{
2423 // 3D textures are not available in the D3D9 backend.
2424 return 1;
2425}
2426
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002427int Renderer9::getMaxTextureArrayLayers() const
2428{
2429 // 2D array textures are not available in the D3D9 backend.
2430 return 1;
2431}
2432
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002433bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002434{
2435 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2436}
2437
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002438DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002439{
2440 return mDeviceCaps.DeclTypes;
2441}
2442
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002443int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002444{
2445 return mMinSwapInterval;
2446}
2447
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002448int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002449{
2450 return mMaxSwapInterval;
2451}
2452
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002453int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002454{
2455 return mMaxSupportedSamples;
2456}
2457
Geoff Lang0e120e32013-05-29 10:23:55 -04002458GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2459{
Shannon Woodsddb785c2013-07-08 10:32:13 -04002460 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
Geoff Lang0e120e32013-05-29 10:23:55 -04002461 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2462 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2463}
2464
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002465GLsizei Renderer9::getNumSampleCounts(GLint internalFormat) const
2466{
2467 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2468 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2469
2470 unsigned int numCounts = 0;
2471 if (iter != mMultiSampleSupport.end())
2472 {
2473 const MultisampleSupportInfo& info = iter->second;
2474 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2475 {
2476 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2477 {
2478 numCounts++;
2479 }
2480 }
2481 }
2482
2483 return numCounts;
2484}
2485
2486void Renderer9::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2487{
2488 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2489 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2490
2491 if (iter != mMultiSampleSupport.end())
2492 {
2493 const MultisampleSupportInfo& info = iter->second;
2494 int bufPos = 0;
2495 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2496 {
2497 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2498 {
2499 params[bufPos++] = i;
2500 }
2501 }
2502 }
2503}
2504
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002505int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002506{
2507 if (requested == 0)
2508 {
2509 return requested;
2510 }
2511
Geoff Lang61e49a52013-05-29 10:22:58 -04002512 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002513 if (itr == mMultiSampleSupport.end())
2514 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002515 if (format == D3DFMT_UNKNOWN)
2516 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002517 return -1;
2518 }
2519
Geoff Lang61e49a52013-05-29 10:22:58 -04002520 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002521 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002522 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002523 {
2524 return i;
2525 }
2526 }
2527
2528 return -1;
2529}
2530
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002531unsigned int Renderer9::getMaxRenderTargets() const
2532{
2533 // we do not support MRT in d3d9
2534 return 1;
2535}
2536
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002537D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2538{
2539 switch (internalformat)
2540 {
2541 case GL_DEPTH_COMPONENT16:
2542 case GL_DEPTH_COMPONENT32_OES:
2543 case GL_DEPTH24_STENCIL8_OES:
2544 return D3DFMT_INTZ;
2545 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2546 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2547 return D3DFMT_DXT1;
2548 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2549 return D3DFMT_DXT3;
2550 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2551 return D3DFMT_DXT5;
2552 case GL_RGBA32F_EXT:
2553 case GL_RGB32F_EXT:
2554 case GL_ALPHA32F_EXT:
2555 case GL_LUMINANCE32F_EXT:
2556 case GL_LUMINANCE_ALPHA32F_EXT:
2557 return D3DFMT_A32B32G32R32F;
2558 case GL_RGBA16F_EXT:
2559 case GL_RGB16F_EXT:
2560 case GL_ALPHA16F_EXT:
2561 case GL_LUMINANCE16F_EXT:
2562 case GL_LUMINANCE_ALPHA16F_EXT:
2563 return D3DFMT_A16B16G16R16F;
2564 case GL_LUMINANCE8_EXT:
2565 if (getLuminanceTextureSupport())
2566 {
2567 return D3DFMT_L8;
2568 }
2569 break;
2570 case GL_LUMINANCE8_ALPHA8_EXT:
2571 if (getLuminanceAlphaTextureSupport())
2572 {
2573 return D3DFMT_A8L8;
2574 }
2575 break;
2576 case GL_RGB8_OES:
2577 case GL_RGB565:
2578 return D3DFMT_X8R8G8B8;
2579 }
2580
2581 return D3DFMT_A8R8G8B8;
2582}
2583
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002584bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002585{
2586 bool result = false;
2587
2588 if (source && dest)
2589 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002590 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2591 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002592
2593 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002594 for (int i = 0; i < levels; ++i)
2595 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002596 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2597 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002598
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002599 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002600
Geoff Langea228632013-07-30 15:17:12 -04002601 SafeRelease(srcSurf);
2602 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002603
2604 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002605 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002606 return false;
Geoff Langea228632013-07-30 15:17:12 -04002607 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002608 }
2609 }
2610
2611 return result;
2612}
2613
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002614bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002615{
2616 bool result = false;
2617
2618 if (source && dest)
2619 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002620 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2621 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002622 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002623 for (int f = 0; f < 6; f++)
2624 {
2625 for (int i = 0; i < levels; i++)
2626 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002627 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2628 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002629
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002630 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002631
Geoff Langea228632013-07-30 15:17:12 -04002632 SafeRelease(srcSurf);
2633 SafeRelease(dstSurf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002634
2635 if (!result)
Geoff Langea228632013-07-30 15:17:12 -04002636 {
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002637 return false;
Geoff Langea228632013-07-30 15:17:12 -04002638 }
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002639 }
2640 }
2641 }
2642
2643 return result;
2644}
2645
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002646bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2647{
2648 // 3D textures are not available in the D3D9 backend.
2649 UNREACHABLE();
2650 return false;
2651}
2652
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002653bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2654{
2655 // 2D array textures are not supported by the D3D9 backend.
2656 UNREACHABLE();
2657 return false;
2658}
2659
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002660D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002661{
2662 if (mD3d9Ex != NULL)
2663 {
2664 return D3DPOOL_DEFAULT;
2665 }
2666 else
2667 {
2668 if (!(usage & D3DUSAGE_DYNAMIC))
2669 {
2670 return D3DPOOL_MANAGED;
2671 }
2672 }
2673
2674 return D3DPOOL_DEFAULT;
2675}
2676
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002677bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002678 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002679{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002680 RECT rect;
2681 rect.left = sourceRect.x;
2682 rect.top = sourceRect.y;
2683 rect.right = sourceRect.x + sourceRect.width;
2684 rect.bottom = sourceRect.y + sourceRect.height;
2685
2686 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002687}
2688
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002689bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002690 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002691{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002692 RECT rect;
2693 rect.left = sourceRect.x;
2694 rect.top = sourceRect.y;
2695 rect.right = sourceRect.x + sourceRect.width;
2696 rect.bottom = sourceRect.y + sourceRect.height;
2697
2698 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002699}
2700
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002701bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2702 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2703{
2704 // 3D textures are not available in the D3D9 backend.
2705 UNREACHABLE();
2706 return false;
2707}
2708
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002709bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2710 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2711{
2712 // 2D array textures are not available in the D3D9 backend.
2713 UNREACHABLE();
2714 return false;
2715}
2716
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002717bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002718 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002719{
Geoff Lang758d5b22013-06-11 11:42:50 -04002720 ASSERT(filter == GL_NEAREST);
2721
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002722 endScene();
2723
2724 if (blitRenderTarget)
2725 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002726 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2727 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002728 RenderTarget9 *readRenderTarget = NULL;
2729 RenderTarget9 *drawRenderTarget = NULL;
2730 IDirect3DSurface9* readSurface = NULL;
2731 IDirect3DSurface9* drawSurface = NULL;
2732
2733 if (readBuffer)
2734 {
2735 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2736 }
2737 if (drawBuffer)
2738 {
2739 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2740 }
2741
2742 if (readRenderTarget)
2743 {
2744 readSurface = readRenderTarget->getSurface();
2745 }
2746 if (drawRenderTarget)
2747 {
2748 drawSurface = drawRenderTarget->getSurface();
2749 }
2750
2751 if (!readSurface || !drawSurface)
2752 {
2753 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002754 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002755 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002756
Geoff Lang125deab2013-08-09 13:34:16 -04002757 gl::Extents srcSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
2758 gl::Extents dstSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
2759
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002760 RECT srcRect;
2761 srcRect.left = readRect.x;
2762 srcRect.right = readRect.x + readRect.width;
2763 srcRect.top = readRect.y;
2764 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002765
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002766 RECT dstRect;
2767 dstRect.left = drawRect.x;
2768 dstRect.right = drawRect.x + drawRect.width;
2769 dstRect.top = drawRect.y;
2770 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002771
Geoff Lang125deab2013-08-09 13:34:16 -04002772 // Clip the rectangles to the scissor rectangle
2773 if (scissor)
2774 {
2775 if (dstRect.left < scissor->x)
2776 {
2777 srcRect.left += (scissor->x - dstRect.left);
2778 dstRect.left = scissor->x;
2779 }
2780 if (dstRect.top < scissor->y)
2781 {
2782 srcRect.top += (scissor->y - dstRect.top);
2783 dstRect.top = scissor->y;
2784 }
2785 if (dstRect.right > scissor->x + scissor->width)
2786 {
2787 srcRect.right -= (dstRect.right - (scissor->x + scissor->width));
2788 dstRect.right = scissor->x + scissor->width;
2789 }
2790 if (dstRect.bottom > scissor->y + scissor->height)
2791 {
2792 srcRect.bottom -= (dstRect.bottom - (scissor->y + scissor->height));
2793 dstRect.bottom = scissor->y + scissor->height;
2794 }
2795 }
2796
2797 // Clip the rectangles to the destination size
2798 if (dstRect.left < 0)
2799 {
2800 srcRect.left += -dstRect.left;
2801 dstRect.left = 0;
2802 }
2803 if (dstRect.right > dstSize.width)
2804 {
2805 srcRect.right -= (dstRect.right - dstSize.width);
2806 dstRect.right = dstSize.width;
2807 }
2808 if (dstRect.top < 0)
2809 {
2810 srcRect.top += -dstRect.top;
2811 dstRect.top = 0;
2812 }
2813 if (dstRect.bottom > dstSize.height)
2814 {
2815 srcRect.bottom -= (dstRect.bottom - dstSize.height);
2816 dstRect.bottom = dstSize.height;
2817 }
2818
2819 // Clip the rectangles to the source size
2820 if (srcRect.left < 0)
2821 {
2822 dstRect.left += -srcRect.left;
2823 srcRect.left = 0;
2824 }
2825 if (srcRect.right > srcSize.width)
2826 {
2827 dstRect.right -= (srcRect.right - srcSize.width);
2828 srcRect.right = srcSize.width;
2829 }
2830 if (srcRect.top < 0)
2831 {
2832 dstRect.top += -srcRect.top;
2833 srcRect.top = 0;
2834 }
2835 if (srcRect.bottom > srcSize.height)
2836 {
2837 dstRect.bottom -= (srcRect.bottom - srcSize.height);
2838 srcRect.bottom = srcSize.height;
2839 }
2840
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002841 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002842
Geoff Langea228632013-07-30 15:17:12 -04002843 SafeRelease(readSurface);
2844 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002845
2846 if (FAILED(result))
2847 {
2848 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2849 return false;
2850 }
2851 }
2852
Geoff Lang685806d2013-06-12 11:16:36 -04002853 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002854 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002855 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2856 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2857 RenderTarget9 *readDepthStencil = NULL;
2858 RenderTarget9 *drawDepthStencil = NULL;
2859 IDirect3DSurface9* readSurface = NULL;
2860 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002861
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002862 if (readBuffer)
2863 {
2864 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2865 }
2866 if (drawBuffer)
2867 {
2868 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2869 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002870
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002871 if (readDepthStencil)
2872 {
2873 readSurface = readDepthStencil->getSurface();
2874 }
2875 if (drawDepthStencil)
2876 {
2877 drawSurface = drawDepthStencil->getSurface();
2878 }
2879
2880 if (!readSurface || !drawSurface)
2881 {
2882 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002883 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002884 }
2885
2886 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2887
Geoff Langea228632013-07-30 15:17:12 -04002888 SafeRelease(readSurface);
2889 SafeRelease(drawSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002890
2891 if (FAILED(result))
2892 {
2893 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2894 return false;
2895 }
2896 }
2897
2898 return true;
2899}
2900
2901void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2902 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2903{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002904 RenderTarget9 *renderTarget = NULL;
2905 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002906 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002907
2908 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002909 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002910 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2911 }
2912
2913 if (renderTarget)
2914 {
2915 surface = renderTarget->getSurface();
2916 }
2917
2918 if (!surface)
2919 {
2920 // context must be lost
2921 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002922 }
2923
2924 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002925 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002926
2927 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2928 {
2929 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
Geoff Langea228632013-07-30 15:17:12 -04002930 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002931 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002932 }
2933
2934 HRESULT result;
2935 IDirect3DSurface9 *systemSurface = NULL;
2936 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2937 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2938 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2939 if (directToPixels)
2940 {
2941 // Use the pixels ptr as a shared handle to write directly into client's memory
2942 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2943 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2944 if (FAILED(result))
2945 {
2946 // Try again without the shared handle
2947 directToPixels = false;
2948 }
2949 }
2950
2951 if (!directToPixels)
2952 {
2953 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2954 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2955 if (FAILED(result))
2956 {
2957 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
Geoff Langea228632013-07-30 15:17:12 -04002958 SafeRelease(surface);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002959 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002960 }
2961 }
2962
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002963 result = mDevice->GetRenderTargetData(surface, systemSurface);
Geoff Langea228632013-07-30 15:17:12 -04002964 SafeRelease(surface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002965
2966 if (FAILED(result))
2967 {
Geoff Langea228632013-07-30 15:17:12 -04002968 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002969
2970 // It turns out that D3D will sometimes produce more error
2971 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002972 if (d3d9::isDeviceLostError(result))
2973 {
2974 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002975 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002976 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002977 else
2978 {
2979 UNREACHABLE();
2980 return;
2981 }
2982
2983 }
2984
2985 if (directToPixels)
2986 {
Geoff Langea228632013-07-30 15:17:12 -04002987 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002988 return;
2989 }
2990
2991 RECT rect;
2992 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2993 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2994 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2995 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2996
2997 D3DLOCKED_RECT lock;
2998 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2999
3000 if (FAILED(result))
3001 {
3002 UNREACHABLE();
Geoff Langea228632013-07-30 15:17:12 -04003003 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003004
3005 return; // No sensible error to generate
3006 }
3007
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003008 unsigned char *source;
3009 int inputPitch;
3010 if (packReverseRowOrder)
3011 {
3012 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
3013 inputPitch = -lock.Pitch;
3014 }
3015 else
3016 {
3017 source = (unsigned char*)lock.pBits;
3018 inputPitch = lock.Pitch;
3019 }
3020
Geoff Lang697ad3e2013-06-04 10:11:28 -04003021 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003022
Geoff Lang697ad3e2013-06-04 10:11:28 -04003023 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
3024 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3025 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003026
Geoff Lang697ad3e2013-06-04 10:11:28 -04003027 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3028
3029 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003030 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003031 // Direct copy possible
3032 unsigned char *dest = static_cast<unsigned char*>(pixels);
3033 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003034 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003035 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003036 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04003037 }
3038 else
3039 {
3040 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3041 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3042 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3043
3044 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
3045 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003046 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003047 // Fast copy is possible through some special function
3048 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003049 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003050 for (int x = 0; x < rect.right - rect.left; x++)
3051 {
3052 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3053 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3054
3055 fastCopyFunc(src, dest);
3056 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003057 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003058 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04003059 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003060 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003061 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
3062 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003063
Geoff Lang697ad3e2013-06-04 10:11:28 -04003064 gl::ColorF temp;
3065
3066 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003067 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003068 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003069 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003070 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3071 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003072
Geoff Lang697ad3e2013-06-04 10:11:28 -04003073 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3074 // will not allow the copy otherwise.
3075 readFunc(src, &temp);
3076 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003077 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003078 }
3079 }
3080 }
3081
3082 systemSurface->UnlockRect();
Geoff Langea228632013-07-30 15:17:12 -04003083 SafeRelease(systemSurface);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003084}
3085
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003086RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
3087{
3088 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3089 IDirect3DSurface9 *surface = NULL;
3090 if (depth)
3091 {
3092 surface = swapChain9->getDepthStencil();
3093 }
3094 else
3095 {
3096 surface = swapChain9->getRenderTarget();
3097 }
3098
3099 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3100
3101 return renderTarget;
3102}
3103
Geoff Langa2d97f12013-06-11 11:44:02 -04003104RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003105{
3106 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3107 return renderTarget;
3108}
3109
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003110ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003111{
3112 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003113
3114 switch (type)
3115 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003116 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003117 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003118 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003119 if (vshader)
3120 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003121 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003122 }
3123 }
3124 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003125 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003126 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003127 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003128 if (pshader)
3129 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003130 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003131 }
3132 }
3133 break;
3134 default:
3135 UNREACHABLE();
3136 break;
3137 }
3138
3139 return executable;
3140}
3141
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003142ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003143{
3144 const char *profile = NULL;
3145
3146 switch (type)
3147 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003148 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003149 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3150 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003151 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003152 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3153 break;
3154 default:
3155 UNREACHABLE();
3156 return NULL;
3157 }
3158
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003159 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003160 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04003161 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003162 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04003163 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003164
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003165 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04003166 SafeRelease(binary);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003167
3168 return executable;
3169}
3170
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003171bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3172{
3173 return mBlit->boxFilter(source, dest);
3174}
3175
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003176D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003177{
3178 if (mD3d9Ex != NULL)
3179 {
3180 return D3DPOOL_DEFAULT;
3181 }
3182 else
3183 {
3184 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3185 {
3186 return D3DPOOL_MANAGED;
3187 }
3188 }
3189
3190 return D3DPOOL_DEFAULT;
3191}
3192
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003193bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3194{
3195 if (source && dest)
3196 {
3197 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003198
3199 if (fromManaged)
3200 {
3201 D3DSURFACE_DESC desc;
3202 source->GetDesc(&desc);
3203
3204 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003205 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003206
3207 if (SUCCEEDED(result))
3208 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003209 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003210 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
Geoff Langea228632013-07-30 15:17:12 -04003211 SafeRelease(surf);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003212 }
3213 }
3214 else
3215 {
3216 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003217 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003218 }
3219
3220 if (FAILED(result))
3221 {
3222 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3223 return false;
3224 }
3225 }
3226
3227 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003228}
3229
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003230Image *Renderer9::createImage()
3231{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003232 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003233}
3234
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003235void Renderer9::generateMipmap(Image *dest, Image *src)
3236{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003237 Image9 *src9 = Image9::makeImage9(src);
3238 Image9 *dst9 = Image9::makeImage9(dest);
3239 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003240}
3241
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003242TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3243{
3244 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3245 return new TextureStorage9_2D(this, swapChain9);
3246}
3247
3248TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3249{
3250 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3251}
3252
3253TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3254{
3255 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3256}
3257
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003258TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3259{
3260 // 3D textures are not supported by the D3D9 backend.
3261 UNREACHABLE();
3262
3263 return NULL;
3264}
3265
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003266TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3267{
3268 // 2D array textures are not supported by the D3D9 backend.
3269 UNREACHABLE();
3270
3271 return NULL;
3272}
3273
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003274bool Renderer9::getLUID(LUID *adapterLuid) const
3275{
3276 adapterLuid->HighPart = 0;
3277 adapterLuid->LowPart = 0;
3278
3279 if (mD3d9Ex)
3280 {
3281 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3282 return true;
3283 }
3284
3285 return false;
3286}
3287
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003288}